218 lines
5.4 KiB
TypeScript
218 lines
5.4 KiB
TypeScript
import { describe, expect, it, vi, beforeEach } from "vitest";
|
|
import { appRouter } from "./routers";
|
|
import type { TrpcContext } from "./_core/context";
|
|
|
|
// Mock database functions
|
|
vi.mock("./db", () => ({
|
|
createProject: vi.fn().mockResolvedValue({
|
|
id: 1,
|
|
userId: 1,
|
|
name: "Test Project",
|
|
description: "Test Description",
|
|
status: "uploading",
|
|
totalPoints: 0,
|
|
totalChunks: 10,
|
|
uploadedChunks: 0,
|
|
totalSize: 0,
|
|
lodLevels: 0,
|
|
boundingBox: null,
|
|
createdAt: new Date(),
|
|
updatedAt: new Date(),
|
|
}),
|
|
getProjectsByUserId: vi.fn().mockResolvedValue([
|
|
{
|
|
id: 1,
|
|
userId: 1,
|
|
name: "Test Project",
|
|
description: "Test Description",
|
|
status: "ready",
|
|
totalPoints: 1000000,
|
|
totalChunks: 10,
|
|
uploadedChunks: 10,
|
|
totalSize: 32000000,
|
|
lodLevels: 8,
|
|
boundingBox: null,
|
|
createdAt: new Date(),
|
|
updatedAt: new Date(),
|
|
},
|
|
]),
|
|
getProjectById: vi.fn().mockResolvedValue({
|
|
id: 1,
|
|
userId: 1,
|
|
name: "Test Project",
|
|
description: "Test Description",
|
|
status: "ready",
|
|
totalPoints: 1000000,
|
|
totalChunks: 10,
|
|
uploadedChunks: 10,
|
|
totalSize: 32000000,
|
|
lodLevels: 8,
|
|
boundingBox: null,
|
|
createdAt: new Date(),
|
|
updatedAt: new Date(),
|
|
}),
|
|
updateProject: vi.fn().mockResolvedValue(undefined),
|
|
createAnalysisResult: vi.fn().mockResolvedValue({
|
|
id: 1,
|
|
projectId: 1,
|
|
cadModelId: null,
|
|
type: "error_analysis",
|
|
status: "pending",
|
|
resultData: null,
|
|
errorStats: null,
|
|
measurements: null,
|
|
createdAt: new Date(),
|
|
updatedAt: new Date(),
|
|
}),
|
|
updateAnalysisResult: vi.fn().mockResolvedValue(undefined),
|
|
}));
|
|
|
|
type AuthenticatedUser = NonNullable<TrpcContext["user"]>;
|
|
|
|
function createAuthContext(): TrpcContext {
|
|
const user: AuthenticatedUser = {
|
|
id: 1,
|
|
openId: "test-user",
|
|
email: "test@example.com",
|
|
name: "Test User",
|
|
loginMethod: "manus",
|
|
role: "user",
|
|
createdAt: new Date(),
|
|
updatedAt: new Date(),
|
|
lastSignedIn: new Date(),
|
|
};
|
|
|
|
return {
|
|
user,
|
|
req: {
|
|
protocol: "https",
|
|
headers: {},
|
|
} as TrpcContext["req"],
|
|
res: {
|
|
clearCookie: vi.fn(),
|
|
} as unknown as TrpcContext["res"],
|
|
};
|
|
}
|
|
|
|
describe("project router", () => {
|
|
beforeEach(() => {
|
|
vi.clearAllMocks();
|
|
});
|
|
|
|
it("creates a new project", async () => {
|
|
const ctx = createAuthContext();
|
|
const caller = appRouter.createCaller(ctx);
|
|
|
|
const result = await caller.project.create({
|
|
name: "Test Project",
|
|
description: "Test Description",
|
|
totalChunks: 10,
|
|
});
|
|
|
|
expect(result).toBeDefined();
|
|
expect(result.name).toBe("Test Project");
|
|
expect(result.status).toBe("uploading");
|
|
});
|
|
|
|
it("lists user projects", async () => {
|
|
const ctx = createAuthContext();
|
|
const caller = appRouter.createCaller(ctx);
|
|
|
|
const result = await caller.project.list();
|
|
|
|
expect(Array.isArray(result)).toBe(true);
|
|
expect(result.length).toBeGreaterThan(0);
|
|
expect(result[0].name).toBe("Test Project");
|
|
});
|
|
|
|
it("gets a project by id", async () => {
|
|
const ctx = createAuthContext();
|
|
const caller = appRouter.createCaller(ctx);
|
|
|
|
const result = await caller.project.get({ id: 1 });
|
|
|
|
expect(result).toBeDefined();
|
|
expect(result?.id).toBe(1);
|
|
expect(result?.name).toBe("Test Project");
|
|
});
|
|
|
|
it("updates a project", async () => {
|
|
const ctx = createAuthContext();
|
|
const caller = appRouter.createCaller(ctx);
|
|
|
|
const result = await caller.project.update({
|
|
id: 1,
|
|
name: "Updated Project",
|
|
status: "ready",
|
|
});
|
|
|
|
expect(result).toEqual({ success: true });
|
|
});
|
|
});
|
|
|
|
describe("analysis router", () => {
|
|
beforeEach(() => {
|
|
vi.clearAllMocks();
|
|
});
|
|
|
|
it("creates an analysis result", async () => {
|
|
const ctx = createAuthContext();
|
|
const caller = appRouter.createCaller(ctx);
|
|
|
|
const result = await caller.analysis.create({
|
|
projectId: 1,
|
|
type: "error_analysis",
|
|
});
|
|
|
|
expect(result).toBeDefined();
|
|
expect(result.type).toBe("error_analysis");
|
|
expect(result.status).toBe("pending");
|
|
});
|
|
|
|
it("runs error analysis simulation", async () => {
|
|
const ctx = createAuthContext();
|
|
const caller = appRouter.createCaller(ctx);
|
|
|
|
const result = await caller.analysis.runErrorAnalysis({
|
|
analysisId: 1,
|
|
projectId: 1,
|
|
});
|
|
|
|
expect(result.success).toBe(true);
|
|
expect(result.errorStats).toBeDefined();
|
|
expect(result.errorStats.min).toBeDefined();
|
|
expect(result.errorStats.max).toBeDefined();
|
|
expect(result.errorStats.mean).toBeDefined();
|
|
});
|
|
|
|
it("runs measurement simulation", async () => {
|
|
const ctx = createAuthContext();
|
|
const caller = appRouter.createCaller(ctx);
|
|
|
|
const result = await caller.analysis.runMeasurement({
|
|
analysisId: 1,
|
|
projectId: 1,
|
|
});
|
|
|
|
expect(result.success).toBe(true);
|
|
expect(result.measurements).toBeDefined();
|
|
expect(Array.isArray(result.measurements)).toBe(true);
|
|
expect(result.measurements.length).toBeGreaterThan(0);
|
|
});
|
|
|
|
it("runs compression simulation", async () => {
|
|
const ctx = createAuthContext();
|
|
const caller = appRouter.createCaller(ctx);
|
|
|
|
const result = await caller.analysis.runCompression({
|
|
analysisId: 1,
|
|
projectId: 1,
|
|
compressionRatio: 0.3,
|
|
});
|
|
|
|
expect(result.success).toBe(true);
|
|
expect(result.resultData).toBeDefined();
|
|
expect(result.resultData.compressionRatio).toBe(0.3);
|
|
});
|
|
});
|