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:
2026-03-19 21:29:58 +01:00
parent 4118995319
commit 6e5b9ec85b
17 changed files with 1792 additions and 2 deletions
@@ -10,6 +10,14 @@ vi.mock("../sse/event-bus.js", () => ({
emitAllocationUpdated: vi.fn(),
}));
vi.mock("../lib/budget-alerts.js", () => ({
checkBudgetThresholds: vi.fn(),
}));
vi.mock("../lib/cache.js", () => ({
invalidateDashboardCache: vi.fn(),
}));
const createCaller = createCallerFactory(allocationRouter);
function createManagerCaller(db: Record<string, unknown>) {
@@ -0,0 +1,305 @@
import { SystemRole } from "@planarchy/shared";
import { beforeEach, describe, expect, it, vi } from "vitest";
vi.mock("@planarchy/application", async (importOriginal) => {
const actual = await importOriginal<typeof import("@planarchy/application")>();
return {
...actual,
getDashboardOverview: vi.fn(),
getDashboardPeakTimes: vi.fn(),
getDashboardDemand: vi.fn(),
getDashboardTopValueResources: vi.fn(),
getDashboardChargeabilityOverview: vi.fn(),
};
});
vi.mock("../lib/cache.js", () => ({
cacheGet: vi.fn().mockResolvedValue(null),
cacheSet: vi.fn().mockResolvedValue(undefined),
}));
vi.mock("../lib/anonymization.js", () => ({
anonymizeResources: vi.fn((resources: unknown[]) => resources),
getAnonymizationDirectory: vi.fn().mockResolvedValue(null),
}));
import {
getDashboardOverview,
getDashboardPeakTimes,
getDashboardDemand,
getDashboardTopValueResources,
getDashboardChargeabilityOverview,
} from "@planarchy/application";
import { dashboardRouter } from "../router/dashboard.js";
import { createCallerFactory } from "../trpc.js";
const createCaller = createCallerFactory(dashboardRouter);
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,
},
});
}
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: "user_2",
systemRole: SystemRole.CONTROLLER,
permissionOverrides: null,
},
});
}
function createUnauthenticatedCaller(db: Record<string, unknown>) {
return createCaller({
session: null,
db: db as never,
dbUser: null,
});
}
describe("dashboard router", () => {
beforeEach(() => {
vi.clearAllMocks();
});
// ─── getOverview ──────────────────────────────────────────────────────────
describe("getOverview", () => {
it("returns expected shape with resource and project counts", async () => {
const overview = {
totalResources: 42,
activeResources: 38,
totalProjects: 15,
activeProjects: 10,
draftProjects: 3,
completedProjects: 2,
totalBudgetCents: 5_000_000_00,
avgWinProbability: 78,
};
vi.mocked(getDashboardOverview).mockResolvedValue(overview);
const caller = createProtectedCaller({});
const result = await caller.getOverview();
expect(result).toMatchObject({
totalResources: 42,
activeResources: 38,
totalProjects: 15,
activeProjects: 10,
});
expect(getDashboardOverview).toHaveBeenCalledTimes(1);
});
it("rejects unauthenticated users", async () => {
const caller = createUnauthenticatedCaller({});
await expect(caller.getOverview()).rejects.toThrow("Authentication required");
});
});
// ─── getPeakTimes ─────────────────────────────────────────────────────────
describe("getPeakTimes", () => {
it("returns array of time periods", async () => {
const peakData = [
{ period: "2026-03", totalHours: 1200, entries: 15 },
{ period: "2026-04", totalHours: 1400, entries: 18 },
];
vi.mocked(getDashboardPeakTimes).mockResolvedValue(peakData);
const caller = createProtectedCaller({});
const result = await caller.getPeakTimes({
startDate: "2026-03-01T00:00:00.000Z",
endDate: "2026-06-30T00:00:00.000Z",
granularity: "month",
groupBy: "project",
});
expect(result).toHaveLength(2);
expect(result[0]).toHaveProperty("period", "2026-03");
expect(getDashboardPeakTimes).toHaveBeenCalledWith(
expect.anything(),
expect.objectContaining({
granularity: "month",
groupBy: "project",
}),
);
});
it("passes week granularity to application layer", async () => {
vi.mocked(getDashboardPeakTimes).mockResolvedValue([]);
const caller = createProtectedCaller({});
await caller.getPeakTimes({
startDate: "2026-03-01T00:00:00.000Z",
endDate: "2026-03-31T00:00:00.000Z",
granularity: "week",
groupBy: "chapter",
});
expect(getDashboardPeakTimes).toHaveBeenCalledWith(
expect.anything(),
expect.objectContaining({
granularity: "week",
groupBy: "chapter",
}),
);
});
});
// ─── getDemand ────────────────────────────────────────────────────────────
describe("getDemand", () => {
it("returns demand entries grouped by project", async () => {
const demandData = [
{ groupKey: "Project Alpha", totalHours: 500, headcount: 3 },
{ groupKey: "Project Beta", totalHours: 300, headcount: 2 },
];
vi.mocked(getDashboardDemand).mockResolvedValue(demandData);
const caller = createProtectedCaller({});
const result = await caller.getDemand({
startDate: "2026-01-01T00:00:00.000Z",
endDate: "2026-12-31T00:00:00.000Z",
groupBy: "project",
});
expect(result).toHaveLength(2);
expect(getDashboardDemand).toHaveBeenCalledWith(
expect.anything(),
expect.objectContaining({ groupBy: "project" }),
);
});
it("supports grouping by chapter", async () => {
vi.mocked(getDashboardDemand).mockResolvedValue([]);
const caller = createProtectedCaller({});
await caller.getDemand({
startDate: "2026-06-01T00:00:00.000Z",
endDate: "2026-06-30T00:00:00.000Z",
groupBy: "chapter",
});
expect(getDashboardDemand).toHaveBeenCalledWith(
expect.anything(),
expect.objectContaining({ groupBy: "chapter" }),
);
});
});
// ─── getTopValueResources ─────────────────────────────────────────────────
describe("getTopValueResources", () => {
it("returns sorted resources with default limit", async () => {
const resources = [
{ id: "res_1", displayName: "Alice", valueScore: 95 },
{ id: "res_2", displayName: "Bob", valueScore: 88 },
];
vi.mocked(getDashboardTopValueResources).mockResolvedValue(resources);
const caller = createProtectedCaller({});
const result = await caller.getTopValueResources({ limit: 10 });
expect(result).toHaveLength(2);
expect(getDashboardTopValueResources).toHaveBeenCalledWith(
expect.anything(),
expect.objectContaining({ limit: 10 }),
);
});
it("respects custom limit", async () => {
vi.mocked(getDashboardTopValueResources).mockResolvedValue([]);
const caller = createProtectedCaller({});
await caller.getTopValueResources({ limit: 5 });
expect(getDashboardTopValueResources).toHaveBeenCalledWith(
expect.anything(),
expect.objectContaining({ limit: 5 }),
);
});
});
// ─── getChargeabilityOverview ─────────────────────────────────────────────
describe("getChargeabilityOverview", () => {
it("returns chargeability data with top and watchlist arrays", async () => {
const overview = {
avgChargeability: 72,
top: [{ id: "res_1", displayName: "Alice", chargeability: 95 }],
watchlist: [{ id: "res_3", displayName: "Carol", chargeability: 30 }],
};
vi.mocked(getDashboardChargeabilityOverview).mockResolvedValue(overview);
const caller = createControllerCaller({});
const result = await caller.getChargeabilityOverview({
includeProposed: false,
topN: 10,
watchlistThreshold: 15,
});
expect(result).toHaveProperty("top");
expect(result).toHaveProperty("watchlist");
expect(result.top).toHaveLength(1);
expect(result.watchlist).toHaveLength(1);
});
it("passes includeProposed flag to application layer", async () => {
vi.mocked(getDashboardChargeabilityOverview).mockResolvedValue({
avgChargeability: 60,
top: [],
watchlist: [],
});
const caller = createControllerCaller({});
await caller.getChargeabilityOverview({
includeProposed: true,
topN: 5,
watchlistThreshold: 20,
});
expect(getDashboardChargeabilityOverview).toHaveBeenCalledWith(
expect.anything(),
expect.objectContaining({
includeProposed: true,
topN: 5,
watchlistThreshold: 20,
}),
);
});
it("requires controller role — blocks USER", async () => {
const caller = createProtectedCaller({});
await expect(
caller.getChargeabilityOverview({
includeProposed: false,
topN: 10,
watchlistThreshold: 15,
}),
).rejects.toThrow(
expect.objectContaining({ code: "FORBIDDEN" }),
);
});
});
});
@@ -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" }),
);
});
});
});
@@ -0,0 +1,454 @@
import { SystemRole } from "@planarchy/shared";
import { beforeEach, describe, expect, it, vi } from "vitest";
vi.mock("@planarchy/application", async (importOriginal) => {
const actual = await importOriginal<typeof import("@planarchy/application")>();
return {
...actual,
isChargeabilityActualBooking: actual.isChargeabilityActualBooking,
isChargeabilityRelevantProject: actual.isChargeabilityRelevantProject,
listAssignmentBookings: vi.fn(),
recomputeResourceValueScores: vi.fn(),
};
});
vi.mock("../router/blueprint-validation.js", () => ({
assertBlueprintDynamicFields: vi.fn().mockResolvedValue(undefined),
}));
vi.mock("../lib/anonymization.js", () => ({
anonymizeResource: vi.fn((r: Record<string, unknown>) => r),
anonymizeResources: vi.fn((rs: unknown[]) => rs),
anonymizeSearchMatches: vi.fn((rs: unknown[]) => rs),
getAnonymizationDirectory: vi.fn().mockResolvedValue(null),
resolveResourceIdsByDisplayedEids: vi.fn().mockResolvedValue(new Map()),
}));
import { resourceRouter } from "../router/resource.js";
import { createCallerFactory } from "../trpc.js";
const createCaller = createCallerFactory(resourceRouter);
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 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 sampleResource = {
id: "res_1",
eid: "E-001",
displayName: "Alice",
email: "alice@example.com",
chapter: "CGI",
lcrCents: 5000,
ucrCents: 9000,
currency: "EUR",
chargeabilityTarget: 80,
availability: {
monday: 8,
tuesday: 8,
wednesday: 8,
thursday: 8,
friday: 8,
},
skills: [],
dynamicFields: {},
blueprintId: null,
blueprint: null,
isActive: true,
createdAt: new Date("2026-03-01"),
updatedAt: new Date("2026-03-01"),
roleId: null,
portfolioUrl: null,
postalCode: null,
federalState: null,
valueScore: null,
valueScoreBreakdown: null,
valueScoreUpdatedAt: null,
userId: null,
resourceRoles: [],
areaRole: null,
countryId: null,
metroCityId: null,
orgUnitId: null,
managementLevelGroupId: null,
managementLevelId: null,
resourceType: null,
chgResponsibility: null,
rolledOff: false,
departed: false,
enterpriseId: null,
clientUnitId: null,
fte: 1,
};
describe("resource router CRUD", () => {
beforeEach(() => {
vi.clearAllMocks();
});
// ─── list ─────────────────────────────────────────────────────────────────
describe("list", () => {
it("returns paginated results with total count", async () => {
const db = {
resource: {
findMany: vi.fn().mockResolvedValue([sampleResource]),
count: vi.fn().mockResolvedValue(1),
},
};
const caller = createProtectedCaller(db);
const result = await caller.list({ limit: 50 });
expect(result.resources).toHaveLength(1);
expect(result.resources[0]?.displayName).toBe("Alice");
expect(db.resource.findMany).toHaveBeenCalled();
});
it("applies search filter", async () => {
const db = {
resource: {
findMany: vi.fn().mockResolvedValue([]),
count: vi.fn().mockResolvedValue(0),
},
};
const caller = createProtectedCaller(db);
await caller.list({ search: "Alice", limit: 50 });
expect(db.resource.findMany).toHaveBeenCalled();
});
});
// ─── getById ──────────────────────────────────────────────────────────────
describe("getById", () => {
it("returns correct resource", async () => {
const db = {
resource: {
findUnique: vi.fn().mockResolvedValue(sampleResource),
findMany: vi.fn().mockResolvedValue([]),
},
systemSettings: {
findUnique: vi.fn().mockResolvedValue(null),
},
};
const caller = createProtectedCaller(db);
const result = await caller.getById({ id: "res_1" });
expect(result.id).toBe("res_1");
expect(result.displayName).toBe("Alice");
});
it("throws NOT_FOUND when resource does not exist", async () => {
const db = {
resource: {
findUnique: vi.fn().mockResolvedValue(null),
findMany: vi.fn().mockResolvedValue([]),
},
systemSettings: {
findUnique: vi.fn().mockResolvedValue(null),
},
};
const caller = createProtectedCaller(db);
await expect(caller.getById({ id: "missing" })).rejects.toThrow(
expect.objectContaining({ code: "NOT_FOUND" }),
);
});
it("sets isOwnedByCurrentUser when userId matches", async () => {
const ownedResource = { ...sampleResource, userId: "user_1" };
const db = {
resource: {
findUnique: vi.fn().mockResolvedValue(ownedResource),
findMany: vi.fn().mockResolvedValue([]),
},
systemSettings: {
findUnique: vi.fn().mockResolvedValue(null),
},
};
const caller = createProtectedCaller(db);
const result = await caller.getById({ id: "res_1" });
expect(result.isOwnedByCurrentUser).toBe(true);
});
});
// ─── create ───────────────────────────────────────────────────────────────
describe("create", () => {
it("creates a resource and returns it", async () => {
const created = { ...sampleResource, id: "res_new", resourceRoles: [] };
const db = {
resource: {
findFirst: vi.fn().mockResolvedValue(null),
create: vi.fn().mockResolvedValue(created),
},
auditLog: { create: vi.fn().mockResolvedValue({}) },
};
const caller = createManagerCaller(db);
const result = await caller.create({
eid: "E-NEW",
displayName: "New Resource",
email: "new@example.com",
lcrCents: 4000,
ucrCents: 8000,
});
expect(result.id).toBe("res_new");
expect(db.resource.create).toHaveBeenCalled();
expect(db.auditLog.create).toHaveBeenCalled();
});
it("throws CONFLICT on duplicate eid or email", async () => {
const db = {
resource: {
findFirst: vi.fn().mockResolvedValue(sampleResource),
create: vi.fn(),
},
auditLog: { create: vi.fn() },
};
const caller = createManagerCaller(db);
await expect(
caller.create({
eid: "E-001",
displayName: "Duplicate",
email: "alice@example.com",
lcrCents: 5000,
ucrCents: 9000,
}),
).rejects.toThrow(
expect.objectContaining({ code: "CONFLICT" }),
);
});
it("blocks USER role from creating resources", async () => {
const db = {
resource: { findFirst: vi.fn(), create: vi.fn() },
auditLog: { create: vi.fn() },
};
const caller = createProtectedCaller(db);
await expect(
caller.create({
eid: "E-002",
displayName: "Blocked",
email: "blocked@example.com",
lcrCents: 4000,
ucrCents: 8000,
}),
).rejects.toThrow(
expect.objectContaining({ code: "FORBIDDEN" }),
);
});
});
// ─── update ───────────────────────────────────────────────────────────────
describe("update", () => {
it("updates resource fields", async () => {
const updated = { ...sampleResource, displayName: "Alice Updated" };
const db = {
resource: {
findUnique: vi.fn().mockResolvedValue(sampleResource),
update: vi.fn().mockResolvedValue(updated),
},
resourceRole: { deleteMany: vi.fn().mockResolvedValue({ count: 0 }) },
auditLog: { create: vi.fn().mockResolvedValue({}) },
};
const caller = createManagerCaller(db);
const result = await caller.update({
id: "res_1",
data: { displayName: "Alice Updated" },
});
expect(result.displayName).toBe("Alice Updated");
expect(db.resource.update).toHaveBeenCalledWith(
expect.objectContaining({ where: { id: "res_1" } }),
);
});
it("throws NOT_FOUND when resource does not exist", async () => {
const db = {
resource: {
findUnique: vi.fn().mockResolvedValue(null),
update: vi.fn(),
},
auditLog: { create: vi.fn() },
};
const caller = createManagerCaller(db);
await expect(
caller.update({ id: "missing", data: { displayName: "X" } }),
).rejects.toThrow(
expect.objectContaining({ code: "NOT_FOUND" }),
);
});
});
// ─── deactivate ───────────────────────────────────────────────────────────
describe("deactivate", () => {
it("sets isActive to false", async () => {
const deactivated = { ...sampleResource, isActive: false };
const db = {
resource: {
update: vi.fn().mockResolvedValue(deactivated),
},
auditLog: { create: vi.fn().mockResolvedValue({}) },
};
const caller = createManagerCaller(db);
const result = await caller.deactivate({ id: "res_1" });
expect(result.isActive).toBe(false);
expect(db.resource.update).toHaveBeenCalledWith(
expect.objectContaining({
where: { id: "res_1" },
data: { isActive: false },
}),
);
});
});
// ─── getHoverCard ─────────────────────────────────────────────────────────
describe("getHoverCard", () => {
it("returns expected shape with key fields", async () => {
const db = {
resource: {
findUnique: vi.fn().mockResolvedValue({
id: "res_1",
displayName: "Alice",
eid: "E-001",
email: "alice@example.com",
chapter: "CGI",
lcrCents: 5000,
ucrCents: 9000,
currency: "EUR",
chargeabilityTarget: 80,
skills: [],
availability: { monday: 8, tuesday: 8, wednesday: 8, thursday: 8, friday: 8 },
isActive: true,
areaRole: null,
country: null,
managementLevel: null,
resourceType: null,
}),
},
systemSettings: {
findUnique: vi.fn().mockResolvedValue(null),
},
};
const caller = createProtectedCaller(db);
const result = await caller.getHoverCard({ id: "res_1" });
expect(result).toMatchObject({
id: "res_1",
displayName: "Alice",
chapter: "CGI",
lcrCents: 5000,
isActive: true,
});
});
it("throws NOT_FOUND for missing resource", async () => {
const db = {
resource: { findUnique: vi.fn().mockResolvedValue(null) },
systemSettings: { findUnique: vi.fn().mockResolvedValue(null) },
};
const caller = createProtectedCaller(db);
await expect(caller.getHoverCard({ id: "missing" })).rejects.toThrow(
expect.objectContaining({ code: "NOT_FOUND" }),
);
});
});
// ─── importSkillMatrix ────────────────────────────────────────────────────
describe("importSkillMatrix", () => {
it("imports skills for the current user resource", async () => {
const updatedResource = {
...sampleResource,
skills: [{ skill: "Maya", proficiency: 4 }],
skillMatrixUpdatedAt: new Date(),
};
const db = {
user: {
findUnique: vi.fn().mockResolvedValue({
id: "user_1",
email: "user@example.com",
resource: { id: "res_1" },
}),
},
resource: {
update: vi.fn().mockResolvedValue(updatedResource),
},
};
const caller = createProtectedCaller(db);
const result = await caller.importSkillMatrix({
skills: [{ skill: "Maya", proficiency: 4 }],
});
expect(db.resource.update).toHaveBeenCalledWith(
expect.objectContaining({
where: { id: "res_1" },
}),
);
});
it("throws NOT_FOUND when user has no linked resource", async () => {
const db = {
user: {
findUnique: vi.fn().mockResolvedValue({
id: "user_1",
email: "user@example.com",
resource: null,
}),
},
};
const caller = createProtectedCaller(db);
await expect(
caller.importSkillMatrix({
skills: [{ skill: "Nuke", proficiency: 3 }],
}),
).rejects.toThrow("No resource linked to your account");
});
});
});
@@ -10,6 +10,14 @@ vi.mock("../sse/event-bus.js", () => ({
emitProjectShifted: vi.fn(),
}));
vi.mock("../lib/budget-alerts.js", () => ({
checkBudgetThresholds: vi.fn(),
}));
vi.mock("../lib/cache.js", () => ({
invalidateDashboardCache: vi.fn(),
}));
const createCaller = createCallerFactory(timelineRouter);
function createManagerCaller(db: Record<string, unknown>) {
+9
View File
@@ -4,5 +4,14 @@ export default defineConfig({
test: {
globals: true,
environment: "node",
coverage: {
provider: "v8",
thresholds: {
lines: 80,
functions: 75,
branches: 75,
statements: 80,
},
},
},
});
+17
View File
@@ -0,0 +1,17 @@
import { defineConfig } from "vitest/config";
export default defineConfig({
test: {
globals: true,
environment: "node",
coverage: {
provider: "v8",
thresholds: {
lines: 80,
functions: 75,
branches: 75,
statements: 80,
},
},
},
});
+9
View File
@@ -4,5 +4,14 @@ export default defineConfig({
test: {
globals: true,
environment: "node",
coverage: {
provider: "v8",
thresholds: {
lines: 70,
functions: 70,
branches: 65,
statements: 70,
},
},
},
});