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:
@@ -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 }),
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -0,0 +1,306 @@
|
|||||||
|
/**
|
||||||
|
* Vacation overlap edge-case tests for analyzeUtilization and findCapacityWindows.
|
||||||
|
*
|
||||||
|
* The capacity analyzer itself does NOT have direct vacation knowledge — vacations
|
||||||
|
* are represented as allocations (with hoursPerDay equal to the resource's full
|
||||||
|
* availability) from the caller's perspective. These tests verify that the
|
||||||
|
* analyzeUtilization and findCapacityWindows functions correctly handle the
|
||||||
|
* resulting utilization when vacation blocks are modelled as full-day allocations.
|
||||||
|
*/
|
||||||
|
import { AllocationStatus } from "@capakraken/shared";
|
||||||
|
import { describe, expect, it } from "vitest";
|
||||||
|
import { analyzeUtilization, findCapacityWindows } from "../capacity-analyzer.js";
|
||||||
|
import type { CapacityAnalysisInput } from "../capacity-analyzer.js";
|
||||||
|
|
||||||
|
const standardAvailability = {
|
||||||
|
sunday: 0,
|
||||||
|
monday: 8,
|
||||||
|
tuesday: 8,
|
||||||
|
wednesday: 8,
|
||||||
|
thursday: 8,
|
||||||
|
friday: 8,
|
||||||
|
saturday: 0,
|
||||||
|
};
|
||||||
|
|
||||||
|
const resource = {
|
||||||
|
id: "res-vacation",
|
||||||
|
displayName: "Vacation Tester",
|
||||||
|
chargeabilityTarget: 80,
|
||||||
|
availability: standardAvailability,
|
||||||
|
};
|
||||||
|
|
||||||
|
const simpleResource = {
|
||||||
|
id: "res-vacation",
|
||||||
|
displayName: "Vacation Tester",
|
||||||
|
availability: standardAvailability,
|
||||||
|
};
|
||||||
|
|
||||||
|
// Monday 2026-03-09 … Friday 2026-03-13 (5 working days)
|
||||||
|
const MON = new Date("2026-03-09");
|
||||||
|
const FRI = new Date("2026-03-13");
|
||||||
|
|
||||||
|
describe("analyzeUtilization — vacation overlap", () => {
|
||||||
|
it("resource with vacation covering the entire allocation period results in 0 free chargeable hours", () => {
|
||||||
|
// Vacation modelled as a full-day non-chargeable allocation for the whole week
|
||||||
|
const input: CapacityAnalysisInput = {
|
||||||
|
resource,
|
||||||
|
allocations: [
|
||||||
|
{
|
||||||
|
startDate: MON,
|
||||||
|
endDate: FRI,
|
||||||
|
hoursPerDay: 8,
|
||||||
|
status: AllocationStatus.CONFIRMED,
|
||||||
|
projectName: "Vacation",
|
||||||
|
isChargeable: false,
|
||||||
|
},
|
||||||
|
],
|
||||||
|
analysisStart: MON,
|
||||||
|
analysisEnd: FRI,
|
||||||
|
};
|
||||||
|
|
||||||
|
const result = analyzeUtilization(input);
|
||||||
|
|
||||||
|
// No chargeable hours, but all available hours are consumed by vacation
|
||||||
|
expect(result.currentChargeability).toBe(0);
|
||||||
|
// All 5 days are allocated at 8/8 — not overallocated, not underutilized
|
||||||
|
expect(result.overallocatedDays).toHaveLength(0);
|
||||||
|
// Each day: allocatedHours (8) === availableHours (8), which is NOT < 50%, so no underutilized
|
||||||
|
expect(result.underutilizedDays).toHaveLength(0);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("resource with partial vacation overlap has correctly reduced available chargeable hours", () => {
|
||||||
|
// 2-day vacation Mon-Tue, then project allocation Wed-Fri
|
||||||
|
const input: CapacityAnalysisInput = {
|
||||||
|
resource,
|
||||||
|
allocations: [
|
||||||
|
{
|
||||||
|
startDate: MON,
|
||||||
|
endDate: new Date("2026-03-10"), // Mon–Tue
|
||||||
|
hoursPerDay: 8,
|
||||||
|
status: AllocationStatus.CONFIRMED,
|
||||||
|
projectName: "Vacation",
|
||||||
|
isChargeable: false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
startDate: new Date("2026-03-11"), // Wed–Fri
|
||||||
|
endDate: FRI,
|
||||||
|
hoursPerDay: 8,
|
||||||
|
status: AllocationStatus.CONFIRMED,
|
||||||
|
projectName: "Project A",
|
||||||
|
isChargeable: true,
|
||||||
|
},
|
||||||
|
],
|
||||||
|
analysisStart: MON,
|
||||||
|
analysisEnd: FRI,
|
||||||
|
};
|
||||||
|
|
||||||
|
const result = analyzeUtilization(input);
|
||||||
|
|
||||||
|
// 5 working days × 8 h = 40 h total available
|
||||||
|
// Chargeable hours: 3 days × 8 h = 24 h → 24/40 = 60%
|
||||||
|
expect(result.currentChargeability).toBeCloseTo(60, 5);
|
||||||
|
expect(result.overallocatedDays).toHaveLength(0);
|
||||||
|
expect(result.underutilizedDays).toHaveLength(0);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("multiple overlapping vacation blocks are each counted separately without double-counting overallocation when summed hours exceed availability", () => {
|
||||||
|
// Two full-day vacation records on the same Monday (simulating duplicate entries)
|
||||||
|
const input: CapacityAnalysisInput = {
|
||||||
|
resource,
|
||||||
|
allocations: [
|
||||||
|
{
|
||||||
|
startDate: MON,
|
||||||
|
endDate: MON,
|
||||||
|
hoursPerDay: 5,
|
||||||
|
status: AllocationStatus.CONFIRMED,
|
||||||
|
projectName: "Vacation A",
|
||||||
|
isChargeable: false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
startDate: MON,
|
||||||
|
endDate: MON,
|
||||||
|
hoursPerDay: 5,
|
||||||
|
status: AllocationStatus.CONFIRMED,
|
||||||
|
projectName: "Vacation B",
|
||||||
|
isChargeable: false,
|
||||||
|
},
|
||||||
|
],
|
||||||
|
analysisStart: MON,
|
||||||
|
analysisEnd: MON,
|
||||||
|
};
|
||||||
|
|
||||||
|
const result = analyzeUtilization(input);
|
||||||
|
|
||||||
|
// 10 h allocated on a day with 8 h availability → overallocated
|
||||||
|
expect(result.overallocatedDays).toHaveLength(1);
|
||||||
|
// Date string contains the correct date components (timezone-safe check)
|
||||||
|
expect(result.overallocatedDays[0]).toMatch(/2026-03-0[89]/);
|
||||||
|
expect(result.currentChargeability).toBe(0); // all non-chargeable
|
||||||
|
});
|
||||||
|
|
||||||
|
it("vacation on Saturday/Sunday does not reduce working hours (availability = 0 on weekends)", () => {
|
||||||
|
// Vacation over a weekend — should have zero effect on computed utilization
|
||||||
|
const input: CapacityAnalysisInput = {
|
||||||
|
resource,
|
||||||
|
allocations: [
|
||||||
|
{
|
||||||
|
startDate: new Date("2026-03-07"), // Saturday
|
||||||
|
endDate: new Date("2026-03-08"), // Sunday
|
||||||
|
hoursPerDay: 8,
|
||||||
|
status: AllocationStatus.CONFIRMED,
|
||||||
|
projectName: "Weekend Vacation",
|
||||||
|
isChargeable: false,
|
||||||
|
},
|
||||||
|
],
|
||||||
|
analysisStart: new Date("2026-03-07"),
|
||||||
|
analysisEnd: new Date("2026-03-08"),
|
||||||
|
};
|
||||||
|
|
||||||
|
const result = analyzeUtilization(input);
|
||||||
|
|
||||||
|
// Weekends skipped entirely — totalWorkingDays = 0, no days affected
|
||||||
|
expect(result.overallocatedDays).toHaveLength(0);
|
||||||
|
expect(result.underutilizedDays).toHaveLength(0);
|
||||||
|
expect(result.currentChargeability).toBe(0);
|
||||||
|
// allocations list still includes the weekend entry (it's just not active on working days)
|
||||||
|
// but chargeability stays 0 because there are no working days in that window
|
||||||
|
});
|
||||||
|
|
||||||
|
it("vacation on weekend days within a full-week analysis window leaves weekday capacity intact", () => {
|
||||||
|
// Analysis covers Mon–Sun. Vacation is Sat–Sun only.
|
||||||
|
const input: CapacityAnalysisInput = {
|
||||||
|
resource,
|
||||||
|
allocations: [
|
||||||
|
{
|
||||||
|
startDate: new Date("2026-03-14"), // Saturday
|
||||||
|
endDate: new Date("2026-03-15"), // Sunday
|
||||||
|
hoursPerDay: 8,
|
||||||
|
status: AllocationStatus.CONFIRMED,
|
||||||
|
projectName: "Weekend Vacation",
|
||||||
|
isChargeable: false,
|
||||||
|
},
|
||||||
|
],
|
||||||
|
analysisStart: MON,
|
||||||
|
analysisEnd: new Date("2026-03-15"), // Mon–Sun
|
||||||
|
};
|
||||||
|
|
||||||
|
const result = analyzeUtilization(input);
|
||||||
|
|
||||||
|
// All 5 weekdays have zero allocation → all underutilized (0 < 50% of 8)
|
||||||
|
expect(result.underutilizedDays).toHaveLength(5);
|
||||||
|
expect(result.overallocatedDays).toHaveLength(0);
|
||||||
|
expect(result.currentChargeability).toBe(0);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("findCapacityWindows — vacation overlap", () => {
|
||||||
|
it("vacation covering entire search period leaves no capacity windows (full-day blocking)", () => {
|
||||||
|
const windows = findCapacityWindows(
|
||||||
|
simpleResource,
|
||||||
|
[
|
||||||
|
{
|
||||||
|
startDate: MON,
|
||||||
|
endDate: FRI,
|
||||||
|
hoursPerDay: 8,
|
||||||
|
status: AllocationStatus.CONFIRMED,
|
||||||
|
},
|
||||||
|
],
|
||||||
|
MON,
|
||||||
|
FRI,
|
||||||
|
);
|
||||||
|
|
||||||
|
expect(windows).toHaveLength(0);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("partial vacation overlap leaves capacity only on non-vacation days", () => {
|
||||||
|
// Mon–Wed booked as vacation (8 h/day), Thu–Fri free
|
||||||
|
const windows = findCapacityWindows(
|
||||||
|
simpleResource,
|
||||||
|
[
|
||||||
|
{
|
||||||
|
startDate: MON,
|
||||||
|
endDate: new Date("2026-03-11"), // Mon–Wed
|
||||||
|
hoursPerDay: 8,
|
||||||
|
status: AllocationStatus.CONFIRMED,
|
||||||
|
},
|
||||||
|
],
|
||||||
|
MON,
|
||||||
|
FRI,
|
||||||
|
);
|
||||||
|
|
||||||
|
expect(windows).toHaveLength(1);
|
||||||
|
expect(windows[0]!.availableDays).toBe(2); // Thu + Fri
|
||||||
|
expect(windows[0]!.availableHoursPerDay).toBe(8);
|
||||||
|
expect(windows[0]!.totalAvailableHours).toBe(16);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("multiple non-overlapping vacation blocks create gaps in capacity", () => {
|
||||||
|
// Mon booked, Tue free, Wed booked, Thu–Fri free
|
||||||
|
const windows = findCapacityWindows(
|
||||||
|
simpleResource,
|
||||||
|
[
|
||||||
|
{
|
||||||
|
startDate: MON,
|
||||||
|
endDate: MON,
|
||||||
|
hoursPerDay: 8,
|
||||||
|
status: AllocationStatus.CONFIRMED,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
startDate: new Date("2026-03-11"), // Wed
|
||||||
|
endDate: new Date("2026-03-11"),
|
||||||
|
hoursPerDay: 8,
|
||||||
|
status: AllocationStatus.CONFIRMED,
|
||||||
|
},
|
||||||
|
],
|
||||||
|
MON,
|
||||||
|
FRI,
|
||||||
|
);
|
||||||
|
|
||||||
|
// Tue alone, then Thu–Fri
|
||||||
|
expect(windows).toHaveLength(2);
|
||||||
|
expect(windows[0]!.availableDays).toBe(1); // Tue
|
||||||
|
expect(windows[1]!.availableDays).toBe(2); // Thu–Fri
|
||||||
|
});
|
||||||
|
|
||||||
|
it("vacation on weekend days does not affect capacity windows for weekdays", () => {
|
||||||
|
// Vacation on Sat–Sun should not close any weekday windows
|
||||||
|
const windows = findCapacityWindows(
|
||||||
|
simpleResource,
|
||||||
|
[
|
||||||
|
{
|
||||||
|
startDate: new Date("2026-03-07"), // Saturday
|
||||||
|
endDate: new Date("2026-03-08"), // Sunday
|
||||||
|
hoursPerDay: 8,
|
||||||
|
status: AllocationStatus.CONFIRMED,
|
||||||
|
},
|
||||||
|
],
|
||||||
|
MON,
|
||||||
|
FRI,
|
||||||
|
);
|
||||||
|
|
||||||
|
// The weekend vacation falls outside the Mon–Fri window, so full week is free
|
||||||
|
expect(windows).toHaveLength(1);
|
||||||
|
expect(windows[0]!.availableDays).toBe(5);
|
||||||
|
expect(windows[0]!.totalAvailableHours).toBe(40);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("cancelled vacation has no effect on available capacity", () => {
|
||||||
|
const windows = findCapacityWindows(
|
||||||
|
simpleResource,
|
||||||
|
[
|
||||||
|
{
|
||||||
|
startDate: MON,
|
||||||
|
endDate: FRI,
|
||||||
|
hoursPerDay: 8,
|
||||||
|
status: AllocationStatus.CANCELLED,
|
||||||
|
},
|
||||||
|
],
|
||||||
|
MON,
|
||||||
|
FRI,
|
||||||
|
);
|
||||||
|
|
||||||
|
expect(windows).toHaveLength(1);
|
||||||
|
expect(windows[0]!.availableDays).toBe(5);
|
||||||
|
expect(windows[0]!.totalAvailableHours).toBe(40);
|
||||||
|
});
|
||||||
|
});
|
||||||
Reference in New Issue
Block a user