test(application): add unit tests for demand fill logic and capacity vacation overlap

Covers fill-demand-requirement status validation, duplicate detection,
fill-open-demand happy path, and vacation overlap edge cases in
capacity analyzer.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-04-09 14:15:00 +02:00
parent d3bfa8ca98
commit fbca017eaa
3 changed files with 934 additions and 0 deletions
@@ -0,0 +1,404 @@
import { AllocationStatus } from "@capakraken/shared";
import { TRPCError } from "@trpc/server";
import { describe, expect, it, vi } from "vitest";
import { fillDemandRequirement } from "../index.js";
// Minimal assignment shape returned from createAssignment inside the transaction
function makeAssignment(overrides: Record<string, unknown> = {}) {
return {
id: "assignment_1",
demandRequirementId: "demand_1",
resourceId: "resource_1",
projectId: "project_1",
startDate: new Date("2026-03-16"),
endDate: new Date("2026-03-27"),
hoursPerDay: 8,
percentage: 100,
role: "Compositor",
roleId: "role_comp",
dailyCostCents: 40000,
status: AllocationStatus.PROPOSED,
metadata: {},
createdAt: new Date("2026-03-13"),
updatedAt: new Date("2026-03-13"),
resource: { id: "resource_1", displayName: "Alice", eid: "E-001", lcrCents: 5000 },
project: { id: "project_1", name: "Project One", shortCode: "PRJ" },
roleEntity: { id: "role_comp", name: "Compositor", color: "#111111" },
demandRequirement: {
id: "demand_1",
projectId: "project_1",
startDate: new Date("2026-03-16"),
endDate: new Date("2026-03-27"),
hoursPerDay: 8,
percentage: 100,
role: "Compositor",
roleId: "role_comp",
headcount: 1,
status: AllocationStatus.PROPOSED,
},
...overrides,
};
}
function makeDb(demandOverride: Record<string, unknown> = {}, txOverrides: Record<string, unknown> = {}) {
const demand = {
id: "demand_1",
projectId: "project_1",
startDate: new Date("2026-03-16"),
endDate: new Date("2026-03-27"),
hoursPerDay: 8,
role: "Compositor",
roleId: "role_comp",
headcount: 1,
status: AllocationStatus.PROPOSED,
metadata: {},
...demandOverride,
};
const assignmentCreate = vi.fn().mockResolvedValue(makeAssignment());
const demandRequirementUpdate = vi.fn().mockResolvedValue({
id: "demand_1",
projectId: "project_1",
headcount: 0,
status: AllocationStatus.COMPLETED,
});
const auditLogCreate = vi.fn().mockResolvedValue({});
const vacationFindMany = vi.fn().mockResolvedValue([]);
const assignmentFindMany = vi.fn().mockResolvedValue([]);
const resourceFindUnique = vi.fn().mockResolvedValue({
id: "resource_1",
lcrCents: 5000,
availability: {
monday: 8,
tuesday: 8,
wednesday: 8,
thursday: 8,
friday: 8,
saturday: 0,
sunday: 0,
},
});
const projectFindUnique = vi.fn().mockResolvedValue({ id: "project_1" });
return {
demandRequirement: {
findUnique: vi.fn().mockResolvedValue(demand),
},
assignment: { findMany: assignmentFindMany },
$transaction: vi.fn(async (callback: (tx: unknown) => Promise<unknown>) =>
callback({
project: { findUnique: projectFindUnique },
resource: { findUnique: resourceFindUnique },
demandRequirement: {
findUnique: vi.fn().mockResolvedValue({ id: "demand_1", projectId: "project_1" }),
update: demandRequirementUpdate,
},
assignment: {
findMany: assignmentFindMany,
create: assignmentCreate,
},
vacation: { findMany: vacationFindMany },
auditLog: { create: auditLogCreate },
...txOverrides,
}),
),
_assignmentCreate: assignmentCreate,
_demandRequirementUpdate: demandRequirementUpdate,
};
}
describe("fillDemandRequirement", () => {
it("happy path: fills an open demand with a valid resource assignment", async () => {
const db = makeDb();
const result = await fillDemandRequirement(db as never, {
demandRequirementId: "demand_1",
resourceId: "resource_1",
});
expect(result.assignment.id).toBe("assignment_1");
expect(result.assignment.resourceId).toBe("resource_1");
expect(result.updatedDemandRequirement.id).toBe("demand_1");
expect(db._assignmentCreate).toHaveBeenCalledOnce();
expect(db._demandRequirementUpdate).toHaveBeenCalledOnce();
});
it("throws NOT_FOUND when the demand requirement does not exist", async () => {
const db = {
demandRequirement: { findUnique: vi.fn().mockResolvedValue(null) },
assignment: { findMany: vi.fn().mockResolvedValue([]) },
$transaction: vi.fn(),
};
await expect(
fillDemandRequirement(db as never, {
demandRequirementId: "nonexistent",
resourceId: "resource_1",
}),
).rejects.toThrow(TRPCError);
await expect(
fillDemandRequirement(db as never, {
demandRequirementId: "nonexistent",
resourceId: "resource_1",
}),
).rejects.toMatchObject({ code: "NOT_FOUND" });
});
it("throws BAD_REQUEST when demand requirement is CANCELLED", async () => {
const db = makeDb({ status: AllocationStatus.CANCELLED });
await expect(
fillDemandRequirement(db as never, {
demandRequirementId: "demand_1",
resourceId: "resource_1",
}),
).rejects.toMatchObject({ code: "BAD_REQUEST", message: expect.stringContaining("cancelled") });
// No transaction should have been initiated
expect(db.$transaction).not.toHaveBeenCalled();
});
it("throws BAD_REQUEST when demand requirement is already COMPLETED", async () => {
const db = makeDb({ status: AllocationStatus.COMPLETED });
await expect(
fillDemandRequirement(db as never, {
demandRequirementId: "demand_1",
resourceId: "resource_1",
}),
).rejects.toMatchObject({ code: "BAD_REQUEST", message: expect.stringContaining("completed") });
expect(db.$transaction).not.toHaveBeenCalled();
});
it("throws CONFLICT when the same resource is already assigned to the same project with overlapping dates", async () => {
// assignment.findMany returns an existing booking for the same resource+project+dates
const existingBooking = {
id: "assignment_existing",
projectId: "project_1",
resourceId: "resource_1",
startDate: new Date("2026-03-10"),
endDate: new Date("2026-03-20"),
hoursPerDay: 8,
dailyCostCents: 40000,
status: AllocationStatus.CONFIRMED,
project: {
id: "project_1",
name: "Project One",
shortCode: "PRJ",
status: "ACTIVE",
orderType: "EXTERNAL",
clientId: null,
dynamicFields: null,
},
resource: { id: "resource_1", displayName: "Alice", chapter: null },
};
const db = {
demandRequirement: {
findUnique: vi.fn().mockResolvedValue({
id: "demand_1",
projectId: "project_1",
startDate: new Date("2026-03-16"),
endDate: new Date("2026-03-27"),
hoursPerDay: 8,
role: "Compositor",
roleId: "role_comp",
headcount: 1,
status: AllocationStatus.PROPOSED,
metadata: {},
}),
},
assignment: {
findMany: vi.fn().mockResolvedValue([existingBooking]),
},
$transaction: vi.fn(),
};
await expect(
fillDemandRequirement(db as never, {
demandRequirementId: "demand_1",
resourceId: "resource_1",
}),
).rejects.toMatchObject({ code: "CONFLICT" });
expect(db.$transaction).not.toHaveBeenCalled();
});
it("allows filling when existing assignment is for a different project (no duplicate)", async () => {
const differentProjectBooking = {
id: "assignment_other",
projectId: "project_2", // different project — should not trigger duplicate check
resourceId: "resource_1",
startDate: new Date("2026-03-16"),
endDate: new Date("2026-03-27"),
hoursPerDay: 8,
dailyCostCents: 40000,
status: AllocationStatus.CONFIRMED,
project: {
id: "project_2",
name: "Project Two",
shortCode: "PT2",
status: "ACTIVE",
orderType: "EXTERNAL",
clientId: null,
dynamicFields: null,
},
resource: { id: "resource_1", displayName: "Alice", chapter: null },
};
// Outer findMany (duplicate check in fillDemandRequirement) sees the different-project booking.
// Inside the transaction, createAssignment calls its own listAssignmentBookings — that must
// return empty so the availability validator doesn't see an overallocation.
const outerFindMany = vi.fn().mockResolvedValue([differentProjectBooking]);
const txFindMany = vi.fn().mockResolvedValue([]);
const assignmentCreate = vi.fn().mockResolvedValue(makeAssignment());
const demandRequirementUpdate = vi.fn().mockResolvedValue({
id: "demand_1",
projectId: "project_1",
headcount: 0,
status: AllocationStatus.COMPLETED,
});
const db = {
demandRequirement: {
findUnique: vi.fn().mockResolvedValue({
id: "demand_1",
projectId: "project_1",
startDate: new Date("2026-03-16"),
endDate: new Date("2026-03-27"),
hoursPerDay: 8,
role: "Compositor",
roleId: "role_comp",
headcount: 1,
status: AllocationStatus.PROPOSED,
metadata: {},
}),
},
assignment: { findMany: outerFindMany },
$transaction: vi.fn(async (callback: (tx: unknown) => Promise<unknown>) =>
callback({
project: { findUnique: vi.fn().mockResolvedValue({ id: "project_1" }) },
resource: {
findUnique: vi.fn().mockResolvedValue({
id: "resource_1",
lcrCents: 5000,
availability: { monday: 8, tuesday: 8, wednesday: 8, thursday: 8, friday: 8, saturday: 0, sunday: 0 },
}),
},
demandRequirement: {
findUnique: vi.fn().mockResolvedValue({ id: "demand_1", projectId: "project_1" }),
update: demandRequirementUpdate,
},
assignment: { findMany: txFindMany, create: assignmentCreate },
vacation: { findMany: vi.fn().mockResolvedValue([]) },
auditLog: { create: vi.fn().mockResolvedValue({}) },
}),
),
};
const result = await fillDemandRequirement(db as never, {
demandRequirementId: "demand_1",
resourceId: "resource_1",
});
expect(result.assignment.id).toBe("assignment_1");
});
it("uses input hoursPerDay when provided, overriding demand default", async () => {
const db = makeDb();
await fillDemandRequirement(db as never, {
demandRequirementId: "demand_1",
resourceId: "resource_1",
hoursPerDay: 4,
});
// createAssignment is called inside the transaction — verify it received hoursPerDay: 4
expect(db._assignmentCreate).toHaveBeenCalledWith(
expect.objectContaining({
data: expect.objectContaining({ hoursPerDay: 4 }),
}),
);
});
it("decrement headcount when demand has multiple headcount (headcount > 1)", async () => {
const demandRequirementUpdate = vi.fn().mockResolvedValue({
id: "demand_1",
projectId: "project_1",
headcount: 1,
status: AllocationStatus.PROPOSED,
});
const db = {
demandRequirement: {
findUnique: vi.fn().mockResolvedValue({
id: "demand_1",
projectId: "project_1",
startDate: new Date("2026-03-16"),
endDate: new Date("2026-03-27"),
hoursPerDay: 8,
role: "Compositor",
roleId: "role_comp",
headcount: 2,
status: AllocationStatus.PROPOSED,
metadata: {},
}),
},
assignment: { findMany: vi.fn().mockResolvedValue([]) },
$transaction: vi.fn(async (callback: (tx: unknown) => Promise<unknown>) =>
callback({
project: { findUnique: vi.fn().mockResolvedValue({ id: "project_1" }) },
resource: {
findUnique: vi.fn().mockResolvedValue({
id: "resource_1",
lcrCents: 5000,
availability: { monday: 8, tuesday: 8, wednesday: 8, thursday: 8, friday: 8, saturday: 0, sunday: 0 },
}),
},
demandRequirement: {
findUnique: vi.fn().mockResolvedValue({ id: "demand_1", projectId: "project_1" }),
update: demandRequirementUpdate,
},
assignment: {
findMany: vi.fn().mockResolvedValue([]),
create: vi.fn().mockResolvedValue(makeAssignment()),
},
vacation: { findMany: vi.fn().mockResolvedValue([]) },
auditLog: { create: vi.fn().mockResolvedValue({}) },
}),
),
};
const result = await fillDemandRequirement(db as never, {
demandRequirementId: "demand_1",
resourceId: "resource_1",
});
expect(result.updatedDemandRequirement.headcount).toBe(1);
expect(result.updatedDemandRequirement.status).toBe(AllocationStatus.PROPOSED);
expect(demandRequirementUpdate).toHaveBeenCalledWith(
expect.objectContaining({
data: { headcount: 1 },
}),
);
});
it("marks demand COMPLETED when it is the last headcount seat", async () => {
const db = makeDb({ headcount: 1 });
const result = await fillDemandRequirement(db as never, {
demandRequirementId: "demand_1",
resourceId: "resource_1",
});
expect(result.updatedDemandRequirement.status).toBe(AllocationStatus.COMPLETED);
expect(db._demandRequirementUpdate).toHaveBeenCalledWith(
expect.objectContaining({
data: { status: AllocationStatus.COMPLETED },
}),
);
});
});
@@ -0,0 +1,224 @@
import { AllocationStatus } from "@capakraken/shared";
import { TRPCError } from "@trpc/server";
import { describe, expect, it, vi } from "vitest";
import { fillOpenDemand } from "../index.js";
// Full demand requirement shape expected by loadAllocationEntry → buildSplitAllocationReadModel
const makeDemandRequirement = (overrides: Record<string, unknown> = {}) => ({
id: "demand_1",
projectId: "project_1",
startDate: new Date("2026-03-16"),
endDate: new Date("2026-03-27"),
hoursPerDay: 8,
percentage: 100,
role: "Compositor",
roleId: "role_comp",
headcount: 1,
status: AllocationStatus.PROPOSED,
metadata: {},
createdAt: new Date("2026-03-13"),
updatedAt: new Date("2026-03-13"),
project: { id: "project_1", name: "Project One", shortCode: "PRJ" },
roleEntity: { id: "role_comp", name: "Compositor", color: "#111111" },
...overrides,
});
const makeAssignmentRecord = (overrides: Record<string, unknown> = {}) => ({
id: "assignment_1",
demandRequirementId: "demand_1",
resourceId: "resource_1",
projectId: "project_1",
startDate: new Date("2026-03-16"),
endDate: new Date("2026-03-27"),
hoursPerDay: 8,
percentage: 100,
role: "Compositor",
roleId: "role_comp",
dailyCostCents: 40000,
status: AllocationStatus.PROPOSED,
metadata: {},
createdAt: new Date("2026-03-13"),
updatedAt: new Date("2026-03-13"),
resource: { id: "resource_1", displayName: "Alice", eid: "E-001", lcrCents: 5000 },
project: { id: "project_1", name: "Project One", shortCode: "PRJ" },
roleEntity: { id: "role_comp", name: "Compositor", color: "#111111" },
demandRequirement: {
id: "demand_1",
projectId: "project_1",
startDate: new Date("2026-03-16"),
endDate: new Date("2026-03-27"),
hoursPerDay: 8,
percentage: 100,
role: "Compositor",
roleId: "role_comp",
headcount: 1,
status: AllocationStatus.PROPOSED,
},
...overrides,
});
/**
* Builds a minimal DB mock for fillOpenDemand.
* The allocationId is resolved through findAllocationEntry which calls
* demandRequirement.findUnique and assignment.findUnique in parallel.
* If the allocationId matches a demand, that path is taken.
* If the allocationId matches an assignment (non-null), "already filled" is thrown.
*/
function makeDb({
demandRecord = makeDemandRequirement(),
assignmentRecord = null,
}: {
demandRecord?: Record<string, unknown> | null;
assignmentRecord?: Record<string, unknown> | null;
} = {}) {
const assignmentCreate = vi.fn().mockResolvedValue(makeAssignmentRecord());
const demandRequirementUpdate = vi.fn().mockResolvedValue({
id: "demand_1",
projectId: "project_1",
headcount: 0,
status: AllocationStatus.COMPLETED,
});
const auditLogCreate = vi.fn().mockResolvedValue({});
const vacationFindMany = vi.fn().mockResolvedValue([]);
const assignmentFindMany = vi.fn().mockResolvedValue([]);
const resourceFindUnique = vi.fn().mockResolvedValue({
id: "resource_1",
lcrCents: 5000,
availability: {
monday: 8,
tuesday: 8,
wednesday: 8,
thursday: 8,
friday: 8,
saturday: 0,
sunday: 0,
},
});
const projectFindUnique = vi.fn().mockResolvedValue({ id: "project_1" });
return {
demandRequirement: {
// loadAllocationEntry uses findUnique with include (DEMAND_REQUIREMENT_RELATIONS_INCLUDE)
findUnique: vi.fn().mockResolvedValue(demandRecord),
},
assignment: {
// loadAllocationEntry also probes assignment.findUnique
findUnique: vi.fn().mockResolvedValue(assignmentRecord),
findMany: assignmentFindMany,
},
$transaction: vi.fn(async (callback: (tx: unknown) => Promise<unknown>) =>
callback({
project: { findUnique: projectFindUnique },
resource: { findUnique: resourceFindUnique },
demandRequirement: {
findUnique: vi.fn().mockResolvedValue({ id: "demand_1", projectId: "project_1" }),
update: demandRequirementUpdate,
},
assignment: {
findMany: assignmentFindMany,
create: assignmentCreate,
},
vacation: { findMany: vacationFindMany },
auditLog: { create: auditLogCreate },
}),
),
_assignmentCreate: assignmentCreate,
_demandRequirementUpdate: demandRequirementUpdate,
};
}
describe("fillOpenDemand", () => {
it("happy path: fills an open demand entry and returns demand_requirement strategy", async () => {
const db = makeDb();
const result = await fillOpenDemand(db as never, {
allocationId: "demand_1",
resourceId: "resource_1",
});
expect(result.strategy).toBe("demand_requirement");
expect(result.createdAllocation.id).toBe("assignment_1");
expect(result.createdAllocation.projectId).toBe("project_1");
expect(result.createdAllocation.resourceId).toBe("resource_1");
expect(result.updatedAllocation).not.toBeNull();
expect(result.updatedAllocation?.id).toBe("demand_1");
expect(result.updatedAllocation?.resourceId).toBeNull();
});
it("throws NOT_FOUND when the allocationId does not resolve to any record", async () => {
const db = makeDb({ demandRecord: null, assignmentRecord: null });
await expect(
fillOpenDemand(db as never, {
allocationId: "nonexistent",
resourceId: "resource_1",
}),
).rejects.toMatchObject({ code: "NOT_FOUND" });
});
it("throws BAD_REQUEST when the allocation is already filled (is an assignment)", async () => {
// When the allocationId resolves to an assignment (not a demand), it is already filled
const db = makeDb({
demandRecord: null,
assignmentRecord: makeAssignmentRecord() as unknown as Record<string, unknown>,
});
await expect(
fillOpenDemand(db as never, {
allocationId: "assignment_1",
resourceId: "resource_2",
}),
).rejects.toMatchObject({ code: "BAD_REQUEST" });
expect(db.$transaction).not.toHaveBeenCalled();
});
it("propagates CANCELLED demand rejection from fillDemandRequirement", async () => {
const db = makeDb({
demandRecord: makeDemandRequirement({ status: AllocationStatus.CANCELLED }) as Record<string, unknown>,
});
// fillDemandRequirement will re-fetch the demand by ID and throw BAD_REQUEST
// For this we also need the outer findUnique to return cancelled demand
// (loadAllocationEntry uses findUnique with include, fillDemandRequirement uses findUnique with select)
// We need to handle both calls. The mock returns the same shape for both.
await expect(
fillOpenDemand(db as never, {
allocationId: "demand_1",
resourceId: "resource_1",
}),
).rejects.toMatchObject({ code: "BAD_REQUEST" });
});
it("passes optional hoursPerDay override to the underlying fill operation", async () => {
const db = makeDb();
await fillOpenDemand(db as never, {
allocationId: "demand_1",
resourceId: "resource_1",
hoursPerDay: 4,
});
expect(db._assignmentCreate).toHaveBeenCalledWith(
expect.objectContaining({
data: expect.objectContaining({ hoursPerDay: 4 }),
}),
);
});
it("passes optional status override to the underlying fill operation", async () => {
const db = makeDb();
await fillOpenDemand(db as never, {
allocationId: "demand_1",
resourceId: "resource_1",
status: AllocationStatus.CONFIRMED,
});
expect(db._assignmentCreate).toHaveBeenCalledWith(
expect.objectContaining({
data: expect.objectContaining({ status: AllocationStatus.CONFIRMED }),
}),
);
});
});