feat: Sprint 2 — test coverage, Dependabot, coverage gates, E2E expansion
API Router Integration Tests (43 new tests): - dashboard-router.test.ts: 11 tests (all 5 queries + RBAC) - project-router.test.ts: 17 tests (full CRUD + batch ops + RBAC) - resource-router-crud.test.ts: 15 tests (CRUD + hover card + skill import) - Fix: mock budget-alerts + cache in existing allocation/timeline tests E2E Test Suite Expansion (29 new tests, 7 spec files): - dashboard.spec.ts: widget grid, stat cards, add widget modal - allocations.spec.ts: list, create modal, filters, column toggle - estimates.spec.ts: list, wizard steps, navigation - vacations.spec.ts: self-service, management, team calendar - staffing.spec.ts: search, suggestions, skill tags - admin.spec.ts: settings, users, roles, blueprints - navigation.spec.ts: nav links, sidebar collapse, theme, mobile menu Coverage Gates: - api package: 80% lines, 75% branches - application package: 80% lines, 75% branches (new vitest.config.ts) - shared package: 70% lines, 65% branches - CI updated to run per-package vitest --coverage Dependabot: - Weekly npm dependency checks with grouped minor+patch - GitHub Actions version checks - 10 PR limit for npm, 5 for Actions Co-Authored-By: claude-flow <ruv@ruv.net>
This commit is contained in:
@@ -0,0 +1,487 @@
|
||||
import { OrderType, AllocationType, ProjectStatus, SystemRole } from "@planarchy/shared";
|
||||
import { describe, expect, it, vi, beforeEach } from "vitest";
|
||||
import { projectRouter } from "../router/project.js";
|
||||
import { createCallerFactory } from "../trpc.js";
|
||||
|
||||
vi.mock("@planarchy/application", async (importOriginal) => {
|
||||
const actual = await importOriginal<typeof import("@planarchy/application")>();
|
||||
return {
|
||||
...actual,
|
||||
countPlanningEntries: vi.fn().mockResolvedValue({ countsByProjectId: new Map() }),
|
||||
listAssignmentBookings: vi.fn().mockResolvedValue([]),
|
||||
};
|
||||
});
|
||||
|
||||
vi.mock("../router/blueprint-validation.js", () => ({
|
||||
assertBlueprintDynamicFields: vi.fn().mockResolvedValue(undefined),
|
||||
}));
|
||||
|
||||
vi.mock("../router/project-planning-read-model.js", () => ({
|
||||
loadProjectPlanningReadModel: vi.fn().mockResolvedValue({
|
||||
readModel: { assignments: [], demands: [] },
|
||||
}),
|
||||
}));
|
||||
|
||||
vi.mock("../lib/cache.js", () => ({
|
||||
invalidateDashboardCache: vi.fn().mockResolvedValue(undefined),
|
||||
}));
|
||||
|
||||
vi.mock("../ai-client.js", () => ({
|
||||
isDalleConfigured: vi.fn().mockReturnValue(false),
|
||||
createDalleClient: vi.fn(),
|
||||
parseAiError: vi.fn(),
|
||||
}));
|
||||
|
||||
const createCaller = createCallerFactory(projectRouter);
|
||||
|
||||
function createManagerCaller(db: Record<string, unknown>) {
|
||||
return createCaller({
|
||||
session: {
|
||||
user: { email: "manager@example.com", name: "Manager", image: null },
|
||||
expires: "2099-01-01T00:00:00.000Z",
|
||||
},
|
||||
db: db as never,
|
||||
dbUser: {
|
||||
id: "mgr_1",
|
||||
systemRole: SystemRole.MANAGER,
|
||||
permissionOverrides: null,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
function createAdminCaller(db: Record<string, unknown>) {
|
||||
return createCaller({
|
||||
session: {
|
||||
user: { email: "admin@example.com", name: "Admin", image: null },
|
||||
expires: "2099-01-01T00:00:00.000Z",
|
||||
},
|
||||
db: db as never,
|
||||
dbUser: {
|
||||
id: "admin_1",
|
||||
systemRole: SystemRole.ADMIN,
|
||||
permissionOverrides: null,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
function createControllerCaller(db: Record<string, unknown>) {
|
||||
return createCaller({
|
||||
session: {
|
||||
user: { email: "controller@example.com", name: "Controller", image: null },
|
||||
expires: "2099-01-01T00:00:00.000Z",
|
||||
},
|
||||
db: db as never,
|
||||
dbUser: {
|
||||
id: "ctrl_1",
|
||||
systemRole: SystemRole.CONTROLLER,
|
||||
permissionOverrides: null,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
function createProtectedCaller(db: Record<string, unknown>) {
|
||||
return createCaller({
|
||||
session: {
|
||||
user: { email: "user@example.com", name: "User", image: null },
|
||||
expires: "2099-01-01T00:00:00.000Z",
|
||||
},
|
||||
db: db as never,
|
||||
dbUser: {
|
||||
id: "user_1",
|
||||
systemRole: SystemRole.USER,
|
||||
permissionOverrides: null,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
const sampleProject = {
|
||||
id: "project_1",
|
||||
shortCode: "PRJ-001",
|
||||
name: "Test Project",
|
||||
orderType: OrderType.CHARGEABLE,
|
||||
allocationType: AllocationType.INT,
|
||||
winProbability: 80,
|
||||
budgetCents: 500_000_00,
|
||||
startDate: new Date("2026-01-01"),
|
||||
endDate: new Date("2026-06-30"),
|
||||
status: ProjectStatus.ACTIVE,
|
||||
responsiblePerson: "Alice",
|
||||
dynamicFields: {},
|
||||
staffingReqs: [],
|
||||
blueprintId: null,
|
||||
color: null,
|
||||
coverImageUrl: null,
|
||||
coverFocusY: 50,
|
||||
utilizationCategoryId: null,
|
||||
clientId: null,
|
||||
createdAt: new Date("2026-01-01"),
|
||||
updatedAt: new Date("2026-01-01"),
|
||||
};
|
||||
|
||||
describe("project router", () => {
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
});
|
||||
|
||||
// ─── create ───────────────────────────────────────────────────────────────
|
||||
|
||||
describe("create", () => {
|
||||
it("creates a project and returns its id", async () => {
|
||||
const created = { ...sampleProject, id: "project_new" };
|
||||
const db = {
|
||||
project: {
|
||||
findUnique: vi.fn().mockResolvedValue(null), // no shortCode conflict
|
||||
create: vi.fn().mockResolvedValue(created),
|
||||
},
|
||||
auditLog: { create: vi.fn().mockResolvedValue({}) },
|
||||
};
|
||||
|
||||
const caller = createManagerCaller(db);
|
||||
const result = await caller.create({
|
||||
shortCode: "PRJ-001",
|
||||
name: "Test Project",
|
||||
orderType: OrderType.CHARGEABLE,
|
||||
allocationType: AllocationType.INT,
|
||||
winProbability: 80,
|
||||
budgetCents: 500_000_00,
|
||||
startDate: new Date("2026-01-01"),
|
||||
endDate: new Date("2026-06-30"),
|
||||
});
|
||||
|
||||
expect(result.id).toBe("project_new");
|
||||
expect(db.project.create).toHaveBeenCalled();
|
||||
expect(db.auditLog.create).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("throws CONFLICT when shortCode already exists", async () => {
|
||||
const db = {
|
||||
project: {
|
||||
findUnique: vi.fn().mockResolvedValue(sampleProject),
|
||||
create: vi.fn(),
|
||||
},
|
||||
auditLog: { create: vi.fn() },
|
||||
};
|
||||
|
||||
const caller = createManagerCaller(db);
|
||||
await expect(
|
||||
caller.create({
|
||||
shortCode: "PRJ-001",
|
||||
name: "Duplicate",
|
||||
orderType: OrderType.CHARGEABLE,
|
||||
allocationType: AllocationType.INT,
|
||||
budgetCents: 100_00,
|
||||
startDate: new Date("2026-01-01"),
|
||||
endDate: new Date("2026-06-30"),
|
||||
}),
|
||||
).rejects.toThrow(
|
||||
expect.objectContaining({ code: "CONFLICT" }),
|
||||
);
|
||||
});
|
||||
|
||||
it("blocks USER role from creating projects", async () => {
|
||||
const db = {
|
||||
project: { findUnique: vi.fn(), create: vi.fn() },
|
||||
auditLog: { create: vi.fn() },
|
||||
};
|
||||
|
||||
const caller = createProtectedCaller(db);
|
||||
await expect(
|
||||
caller.create({
|
||||
shortCode: "PRJ-002",
|
||||
name: "Blocked",
|
||||
orderType: OrderType.CHARGEABLE,
|
||||
allocationType: AllocationType.INT,
|
||||
budgetCents: 100_00,
|
||||
startDate: new Date("2026-01-01"),
|
||||
endDate: new Date("2026-06-30"),
|
||||
}),
|
||||
).rejects.toThrow(
|
||||
expect.objectContaining({ code: "FORBIDDEN" }),
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
// ─── getById ──────────────────────────────────────────────────────────────
|
||||
|
||||
describe("getById", () => {
|
||||
it("returns the correct project with allocations and demands", async () => {
|
||||
const db = {
|
||||
project: {
|
||||
findUnique: vi.fn().mockResolvedValue({ ...sampleProject, blueprint: null }),
|
||||
},
|
||||
allocation: { findMany: vi.fn().mockResolvedValue([]) },
|
||||
demandRequirement: { findMany: vi.fn().mockResolvedValue([]) },
|
||||
assignment: { findMany: vi.fn().mockResolvedValue([]) },
|
||||
};
|
||||
|
||||
const caller = createProtectedCaller(db);
|
||||
const result = await caller.getById({ id: "project_1" });
|
||||
|
||||
expect(result.id).toBe("project_1");
|
||||
expect(result.name).toBe("Test Project");
|
||||
expect(result).toHaveProperty("allocations");
|
||||
expect(result).toHaveProperty("demands");
|
||||
expect(result).toHaveProperty("assignments");
|
||||
});
|
||||
|
||||
it("throws NOT_FOUND when project does not exist", async () => {
|
||||
const db = {
|
||||
project: { findUnique: vi.fn().mockResolvedValue(null) },
|
||||
allocation: { findMany: vi.fn().mockResolvedValue([]) },
|
||||
demandRequirement: { findMany: vi.fn().mockResolvedValue([]) },
|
||||
assignment: { findMany: vi.fn().mockResolvedValue([]) },
|
||||
};
|
||||
|
||||
const caller = createProtectedCaller(db);
|
||||
await expect(caller.getById({ id: "missing" })).rejects.toThrow(
|
||||
expect.objectContaining({ code: "NOT_FOUND" }),
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
// ─── update ───────────────────────────────────────────────────────────────
|
||||
|
||||
describe("update", () => {
|
||||
it("updates project fields", async () => {
|
||||
const updated = { ...sampleProject, name: "Updated Name" };
|
||||
const db = {
|
||||
project: {
|
||||
findUnique: vi.fn().mockResolvedValue(sampleProject),
|
||||
update: vi.fn().mockResolvedValue(updated),
|
||||
},
|
||||
auditLog: { create: vi.fn().mockResolvedValue({}) },
|
||||
};
|
||||
|
||||
const caller = createManagerCaller(db);
|
||||
const result = await caller.update({
|
||||
id: "project_1",
|
||||
data: { name: "Updated Name" },
|
||||
});
|
||||
|
||||
expect(result.name).toBe("Updated Name");
|
||||
expect(db.project.update).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
where: { id: "project_1" },
|
||||
}),
|
||||
);
|
||||
expect(db.auditLog.create).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("throws NOT_FOUND when updating non-existent project", async () => {
|
||||
const db = {
|
||||
project: {
|
||||
findUnique: vi.fn().mockResolvedValue(null),
|
||||
update: vi.fn(),
|
||||
},
|
||||
auditLog: { create: vi.fn() },
|
||||
};
|
||||
|
||||
const caller = createManagerCaller(db);
|
||||
await expect(
|
||||
caller.update({ id: "missing", data: { name: "X" } }),
|
||||
).rejects.toThrow(
|
||||
expect.objectContaining({ code: "NOT_FOUND" }),
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
// ─── updateStatus ─────────────────────────────────────────────────────────
|
||||
|
||||
describe("updateStatus", () => {
|
||||
it("transitions project status", async () => {
|
||||
const updated = { ...sampleProject, status: ProjectStatus.COMPLETED };
|
||||
const db = {
|
||||
project: {
|
||||
update: vi.fn().mockResolvedValue(updated),
|
||||
},
|
||||
};
|
||||
|
||||
const caller = createManagerCaller(db);
|
||||
const result = await caller.updateStatus({
|
||||
id: "project_1",
|
||||
status: ProjectStatus.COMPLETED,
|
||||
});
|
||||
|
||||
expect(result.status).toBe(ProjectStatus.COMPLETED);
|
||||
expect(db.project.update).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
where: { id: "project_1" },
|
||||
data: { status: ProjectStatus.COMPLETED },
|
||||
}),
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
// ─── batchUpdateStatus ────────────────────────────────────────────────────
|
||||
|
||||
describe("batchUpdateStatus", () => {
|
||||
it("updates multiple projects and returns count", async () => {
|
||||
const db = {
|
||||
project: {
|
||||
update: vi.fn().mockResolvedValue(sampleProject),
|
||||
},
|
||||
auditLog: { create: vi.fn().mockResolvedValue({}) },
|
||||
$transaction: vi.fn((calls: unknown[]) =>
|
||||
Promise.all((calls as Promise<unknown>[]).map(() => sampleProject)),
|
||||
),
|
||||
};
|
||||
|
||||
const caller = createManagerCaller(db);
|
||||
const result = await caller.batchUpdateStatus({
|
||||
ids: ["project_1", "project_2", "project_3"],
|
||||
status: ProjectStatus.ON_HOLD,
|
||||
});
|
||||
|
||||
expect(result.count).toBe(3);
|
||||
expect(db.auditLog.create).toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
|
||||
// ─── delete ───────────────────────────────────────────────────────────────
|
||||
|
||||
describe("delete", () => {
|
||||
it("deletes a project and cascades related records", async () => {
|
||||
const db = {
|
||||
project: {
|
||||
findUnique: vi.fn().mockResolvedValue({ id: "project_1", name: "Test", shortCode: "PRJ" }),
|
||||
delete: vi.fn().mockResolvedValue({}),
|
||||
},
|
||||
assignment: { deleteMany: vi.fn().mockResolvedValue({ count: 0 }) },
|
||||
demandRequirement: { deleteMany: vi.fn().mockResolvedValue({ count: 0 }) },
|
||||
calculationRule: { updateMany: vi.fn().mockResolvedValue({ count: 0 }) },
|
||||
auditLog: { create: vi.fn().mockResolvedValue({}) },
|
||||
$transaction: vi.fn(async (callback: (tx: unknown) => unknown) => callback(db)),
|
||||
};
|
||||
|
||||
const caller = createAdminCaller(db);
|
||||
const result = await caller.delete({ id: "project_1" });
|
||||
|
||||
expect(result).toMatchObject({ id: "project_1", name: "Test" });
|
||||
});
|
||||
|
||||
it("throws NOT_FOUND when deleting non-existent project", async () => {
|
||||
const db = {
|
||||
project: { findUnique: vi.fn().mockResolvedValue(null) },
|
||||
$transaction: vi.fn(async (callback: (tx: unknown) => unknown) => callback(db)),
|
||||
};
|
||||
|
||||
const caller = createAdminCaller(db);
|
||||
await expect(caller.delete({ id: "missing" })).rejects.toThrow(
|
||||
expect.objectContaining({ code: "NOT_FOUND" }),
|
||||
);
|
||||
});
|
||||
|
||||
it("requires admin role — blocks manager", async () => {
|
||||
const db = {
|
||||
project: { findUnique: vi.fn() },
|
||||
$transaction: vi.fn(),
|
||||
};
|
||||
|
||||
const caller = createManagerCaller(db);
|
||||
await expect(caller.delete({ id: "project_1" })).rejects.toThrow(
|
||||
expect.objectContaining({ code: "FORBIDDEN" }),
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
// ─── batchDelete ──────────────────────────────────────────────────────────
|
||||
|
||||
describe("batchDelete", () => {
|
||||
it("deletes multiple projects in a transaction", async () => {
|
||||
const projects = [
|
||||
{ id: "p1", name: "A", shortCode: "A1" },
|
||||
{ id: "p2", name: "B", shortCode: "B1" },
|
||||
];
|
||||
const db = {
|
||||
project: {
|
||||
findMany: vi.fn().mockResolvedValue(projects),
|
||||
deleteMany: vi.fn().mockResolvedValue({ count: 2 }),
|
||||
},
|
||||
assignment: { deleteMany: vi.fn().mockResolvedValue({ count: 0 }) },
|
||||
demandRequirement: { deleteMany: vi.fn().mockResolvedValue({ count: 0 }) },
|
||||
calculationRule: { updateMany: vi.fn().mockResolvedValue({ count: 0 }) },
|
||||
auditLog: { create: vi.fn().mockResolvedValue({}) },
|
||||
$transaction: vi.fn(async (callback: (tx: unknown) => unknown) => callback(db)),
|
||||
};
|
||||
|
||||
const caller = createAdminCaller(db);
|
||||
const result = await caller.batchDelete({ ids: ["p1", "p2"] });
|
||||
|
||||
expect(result.count).toBe(2);
|
||||
});
|
||||
|
||||
it("throws NOT_FOUND when no projects match the ids", async () => {
|
||||
const db = {
|
||||
project: { findMany: vi.fn().mockResolvedValue([]) },
|
||||
$transaction: vi.fn(async (callback: (tx: unknown) => unknown) => callback(db)),
|
||||
};
|
||||
|
||||
const caller = createAdminCaller(db);
|
||||
await expect(caller.batchDelete({ ids: ["missing"] })).rejects.toThrow(
|
||||
expect.objectContaining({ code: "NOT_FOUND" }),
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
// ─── listWithCosts ────────────────────────────────────────────────────────
|
||||
|
||||
describe("listWithCosts", () => {
|
||||
it("returns projects with cost data", async () => {
|
||||
const db = {
|
||||
project: {
|
||||
findMany: vi.fn().mockResolvedValue([sampleProject]),
|
||||
},
|
||||
};
|
||||
|
||||
const { listAssignmentBookings } = await import("@planarchy/application");
|
||||
vi.mocked(listAssignmentBookings).mockResolvedValue([]);
|
||||
|
||||
const caller = createControllerCaller(db);
|
||||
const result = await caller.listWithCosts({ limit: 20 });
|
||||
|
||||
expect(result.projects).toHaveLength(1);
|
||||
expect(result.projects[0]).toHaveProperty("totalCostCents");
|
||||
expect(result.projects[0]).toHaveProperty("totalPersonDays");
|
||||
expect(result.projects[0]).toHaveProperty("utilizationPercent");
|
||||
});
|
||||
|
||||
it("calculates cost from assignment bookings", async () => {
|
||||
const db = {
|
||||
project: {
|
||||
findMany: vi.fn().mockResolvedValue([{ ...sampleProject, budgetCents: 100_000_00 }]),
|
||||
},
|
||||
};
|
||||
|
||||
const { listAssignmentBookings } = await import("@planarchy/application");
|
||||
vi.mocked(listAssignmentBookings).mockResolvedValue([
|
||||
{
|
||||
id: "a1",
|
||||
projectId: "project_1",
|
||||
resourceId: "res_1",
|
||||
startDate: new Date("2026-01-01"),
|
||||
endDate: new Date("2026-01-05"),
|
||||
hoursPerDay: 8,
|
||||
dailyCostCents: 50000,
|
||||
status: "CONFIRMED",
|
||||
project: { id: "project_1", name: "Test", shortCode: "PRJ", status: "ACTIVE", orderType: "CHARGEABLE", dynamicFields: null },
|
||||
resource: { id: "res_1", displayName: "Alice", chapter: "CGI" },
|
||||
},
|
||||
]);
|
||||
|
||||
const caller = createControllerCaller(db);
|
||||
const result = await caller.listWithCosts({ limit: 20 });
|
||||
|
||||
expect(result.projects[0]?.totalCostCents).toBeGreaterThan(0);
|
||||
expect(result.projects[0]?.totalPersonDays).toBeGreaterThan(0);
|
||||
});
|
||||
|
||||
it("requires controller role — blocks USER", async () => {
|
||||
const db = { project: { findMany: vi.fn() } };
|
||||
|
||||
const caller = createProtectedCaller(db);
|
||||
await expect(caller.listWithCosts({ limit: 20 })).rejects.toThrow(
|
||||
expect.objectContaining({ code: "FORBIDDEN" }),
|
||||
);
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user