feat(planning): ship holiday-aware planning and assistant upgrades

This commit is contained in:
2026-03-28 22:49:28 +01:00
parent 2a005794e7
commit 4f48afe7b4
151 changed files with 17738 additions and 1940 deletions
@@ -1,13 +1,14 @@
import { AllocationStatus, SystemRole } from "@capakraken/shared";
import { describe, expect, it, vi } from "vitest";
import { allocationRouter } from "../router/allocation.js";
import { emitAllocationCreated, emitAllocationDeleted } from "../sse/event-bus.js";
import { emitAllocationCreated, emitAllocationDeleted, emitNotificationCreated } from "../sse/event-bus.js";
import { createCallerFactory } from "../trpc.js";
vi.mock("../sse/event-bus.js", () => ({
emitAllocationCreated: vi.fn(),
emitAllocationDeleted: vi.fn(),
emitAllocationUpdated: vi.fn(),
emitNotificationCreated: vi.fn(),
}));
vi.mock("../lib/budget-alerts.js", () => ({
@@ -18,6 +19,10 @@ vi.mock("../lib/cache.js", () => ({
invalidateDashboardCache: vi.fn(),
}));
vi.mock("../lib/webhook-dispatcher.js", () => ({
dispatchWebhooks: vi.fn().mockResolvedValue(undefined),
}));
const createCaller = createCallerFactory(allocationRouter);
function createManagerCaller(db: Record<string, unknown>) {
@@ -35,7 +40,100 @@ function createManagerCaller(db: Record<string, unknown>) {
});
}
function createDemandWorkflowDb(overrides: Record<string, unknown> = {}) {
const db = {
project: {
findUnique: vi.fn().mockResolvedValue({ id: "project_1", name: "Project One" }),
},
role: {
findUnique: vi.fn().mockResolvedValue({ name: "FX Artist" }),
},
user: {
findMany: vi.fn().mockResolvedValue([{ id: "mgr_1" }, { id: "admin_1" }]),
},
notification: {
create: vi.fn().mockImplementation(async ({ data }: { data: { userId: string } }) => ({
id: `notif_${data.userId}`,
})),
},
auditLog: {
create: vi.fn().mockResolvedValue({}),
},
};
return {
...db,
...overrides,
project: { ...db.project, ...(overrides.project as Record<string, unknown> | undefined) },
role: { ...db.role, ...(overrides.role as Record<string, unknown> | undefined) },
user: { ...db.user, ...(overrides.user as Record<string, unknown> | undefined) },
notification: {
...db.notification,
...(overrides.notification as Record<string, unknown> | undefined),
},
auditLog: { ...db.auditLog, ...(overrides.auditLog as Record<string, unknown> | undefined) },
};
}
describe("allocation entry resolution router", () => {
it("excludes regional holidays from resource availability coverage", async () => {
const db = {
resource: {
findUnique: vi.fn().mockResolvedValue({
id: "resource_1",
displayName: "Bruce Banner",
eid: "E-001",
fte: 1,
availability: {
monday: 8,
tuesday: 8,
wednesday: 8,
thursday: 8,
friday: 8,
saturday: 0,
sunday: 0,
},
countryId: "country_de",
federalState: "BY",
metroCityId: null,
country: { dailyWorkingHours: 8, code: "DE" },
metroCity: null,
}),
},
assignment: {
findMany: vi.fn().mockResolvedValue([
{
id: "assignment_1",
startDate: new Date("2026-01-05T00:00:00.000Z"),
endDate: new Date("2026-01-06T00:00:00.000Z"),
hoursPerDay: 8,
status: AllocationStatus.CONFIRMED,
project: { name: "Gamma", shortCode: "GAM" },
},
]),
},
};
const caller = createManagerCaller(db);
const result = await caller.checkResourceAvailability({
resourceId: "resource_1",
startDate: new Date("2026-01-05T00:00:00.000Z"),
endDate: new Date("2026-01-06T00:00:00.000Z"),
hoursPerDay: 8,
});
expect(result).toMatchObject({
dailyCapacity: 8,
totalWorkingDays: 1,
availableDays: 0,
partialDays: 0,
conflictDays: 1,
totalAvailableHours: 0,
totalRequestedHours: 8,
coveragePercent: 0,
});
});
it("creates an open demand through allocation.create without requiring isPlaceholder", async () => {
const createdDemandRequirement = {
id: "demand_1",
@@ -187,6 +285,7 @@ describe("allocation entry resolution router", () => {
it("creates an explicit demand requirement without dual-writing a legacy allocation row", async () => {
vi.mocked(emitAllocationCreated).mockClear();
vi.mocked(emitNotificationCreated).mockClear();
const createdDemandRequirement = {
id: "demand_explicit_1",
@@ -206,18 +305,14 @@ describe("allocation entry resolution router", () => {
roleEntity: { id: "role_fx", name: "FX Artist", color: "#222222" },
};
const db = {
project: {
findUnique: vi.fn().mockResolvedValue({ id: "project_1" }),
},
const db = createDemandWorkflowDb({
demandRequirement: {
create: vi.fn().mockResolvedValue(createdDemandRequirement),
},
auditLog: {
create: vi.fn().mockResolvedValue({}),
},
}) as Record<string, unknown>;
Object.assign(db, {
$transaction: vi.fn(async (callback: (tx: unknown) => unknown) => callback(db)),
};
});
const caller = createManagerCaller(db);
const result = await caller.createDemandRequirement({
@@ -247,6 +342,8 @@ describe("allocation entry resolution router", () => {
projectId: "project_1",
resourceId: null,
});
expect(db.notification.create).toHaveBeenCalledTimes(2);
expect(emitNotificationCreated).toHaveBeenCalledTimes(2);
});
it("creates an explicit assignment without dual-writing a legacy allocation row", async () => {
@@ -730,4 +827,3 @@ describe("allocation entry resolution router", () => {
});
});
});