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
@@ -17,23 +17,6 @@ vi.mock("@capakraken/staffing", () => ({
},
})),
),
analyzeUtilization: vi.fn().mockReturnValue({
resourceId: "res_1",
displayName: "Alice",
totalDays: 20,
allocatedDays: 15,
utilizationPercent: 75,
chargeablePercent: 60,
overallocatedDays: 0,
dailyBreakdown: [],
}),
findCapacityWindows: vi.fn().mockReturnValue([
{
startDate: new Date("2026-04-01"),
endDate: new Date("2026-04-10"),
availableHoursPerDay: 6,
},
]),
}));
vi.mock("@capakraken/application", () => ({
@@ -76,6 +59,11 @@ function sampleResource(overrides: Record<string, unknown> = {}) {
isActive: true,
valueScore: 85,
chapter: "VFX",
countryId: "country_de",
federalState: "BY",
metroCityId: null,
country: { code: "DE" },
metroCity: null,
...overrides,
};
}
@@ -105,6 +93,30 @@ describe("staffing.getSuggestions", () => {
expect(result).toHaveLength(2);
expect(result[0]).toHaveProperty("resourceId");
expect(result[0]).toHaveProperty("score");
expect(result[0]).toMatchObject({
resourceName: "Alice",
eid: "alice",
location: {
countryCode: "DE",
federalState: "BY",
},
capacity: expect.objectContaining({
requestedHoursPerDay: 8,
baseAvailableHours: expect.any(Number),
effectiveAvailableHours: expect.any(Number),
remainingHoursPerDay: expect.any(Number),
holidayHoursDeduction: expect.any(Number),
}),
conflicts: {
count: expect.any(Number),
conflictDays: expect.any(Array),
details: expect.any(Array),
},
ranking: expect.objectContaining({
rank: 1,
components: expect.any(Array),
}),
});
});
it("filters resources by chapter when provided", async () => {
@@ -175,6 +187,58 @@ describe("staffing.getSuggestions", () => {
}),
);
});
it("uses value score as a transparent tiebreaker within two score points", async () => {
const resources = [
sampleResource({ id: "res_1", displayName: "Alice", eid: "alice", valueScore: 60 }),
sampleResource({ id: "res_2", displayName: "Bob", eid: "bob", valueScore: 95 }),
];
const db = {
resource: {
findMany: vi.fn().mockResolvedValue(resources),
},
};
const { rankResources } = await import("@capakraken/staffing");
vi.mocked(rankResources).mockImplementationOnce((input: { resources: Array<{ id: string }> }) => ([
{
resourceId: input.resources[0]!.id,
score: 80,
breakdown: {
skillScore: 80,
availabilityScore: 80,
costScore: 80,
utilizationScore: 80,
},
},
{
resourceId: input.resources[1]!.id,
score: 79,
breakdown: {
skillScore: 79,
availabilityScore: 79,
costScore: 79,
utilizationScore: 79,
},
},
]));
const caller = createProtectedCaller(db);
const result = await caller.getSuggestions({
requiredSkills: ["Compositing"],
startDate: new Date("2026-04-01"),
endDate: new Date("2026-04-30"),
hoursPerDay: 8,
});
expect(result[0]?.resourceId).toBe("res_2");
expect(result[0]?.ranking).toMatchObject({
rank: 1,
baseRank: 2,
tieBreakerApplied: true,
});
expect(result[0]?.ranking.tieBreakerReason).toContain("value score");
});
});
// ─── analyzeUtilization ──────────────────────────────────────────────────────
@@ -186,6 +250,11 @@ describe("staffing.analyzeUtilization", () => {
displayName: "Alice",
chargeabilityTarget: 80,
availability: { monday: 8, tuesday: 8, wednesday: 8, thursday: 8, friday: 8 },
countryId: "country_de",
federalState: "BY",
metroCityId: null,
country: { code: "DE" },
metroCity: null,
};
const db = {
resource: {
@@ -200,10 +269,56 @@ describe("staffing.analyzeUtilization", () => {
endDate: new Date("2026-04-30"),
});
expect(result).toHaveProperty("utilizationPercent");
expect(result).toHaveProperty("currentChargeability");
expect(result.resourceId).toBe("res_1");
});
it("excludes Bavarian public holidays from chargeability analysis", async () => {
const resource = {
id: "res_1",
displayName: "Alice",
chargeabilityTarget: 80,
availability: { monday: 8, tuesday: 8, wednesday: 8, thursday: 8, friday: 8 },
countryId: "country_de",
federalState: "BY",
metroCityId: null,
country: { code: "DE" },
metroCity: null,
};
const db = {
resource: {
findUnique: vi.fn().mockResolvedValue(resource),
},
};
const { listAssignmentBookings } = await import("@capakraken/application");
vi.mocked(listAssignmentBookings).mockResolvedValue([
{
id: "a1",
projectId: "project_1",
resourceId: "res_1",
startDate: new Date("2026-01-05T00:00:00.000Z"),
endDate: new Date("2026-01-06T00:00:00.000Z"),
hoursPerDay: 8,
dailyCostCents: 0,
status: "CONFIRMED",
project: { id: "project_1", name: "Chargeable", shortCode: "CHG", status: "ACTIVE", orderType: "CHARGEABLE", clientId: null, dynamicFields: null },
resource: { id: "res_1", displayName: "Alice", chapter: "VFX" },
},
]);
const caller = createProtectedCaller(db);
const result = await caller.analyzeUtilization({
resourceId: "res_1",
startDate: new Date("2026-01-05T00:00:00.000Z"),
endDate: new Date("2026-01-06T00:00:00.000Z"),
});
expect(result.currentChargeability).toBe(100);
expect(result.overallocatedDays).toEqual([]);
expect(result.underutilizedDays).toEqual([]);
});
it("throws NOT_FOUND when resource does not exist", async () => {
const db = {
resource: {
@@ -230,6 +345,11 @@ describe("staffing.findCapacity", () => {
id: "res_1",
displayName: "Alice",
availability: { monday: 8, tuesday: 8, wednesday: 8, thursday: 8, friday: 8 },
countryId: "country_de",
federalState: "BY",
metroCityId: null,
country: { code: "DE" },
metroCity: null,
};
const db = {
resource: {
@@ -244,8 +364,53 @@ describe("staffing.findCapacity", () => {
endDate: new Date("2026-04-30"),
});
expect(result).toHaveLength(1);
expect(result.length).toBeGreaterThan(0);
expect(result[0]).toHaveProperty("availableHoursPerDay");
expect(result.every((window) => window.availableHoursPerDay > 0)).toBe(true);
expect(result.reduce((sum, window) => sum + window.availableDays, 0)).toBeGreaterThan(0);
});
it("splits capacity windows around Bavarian public holidays", async () => {
const resource = {
id: "res_1",
displayName: "Alice",
availability: { monday: 8, tuesday: 8, wednesday: 8, thursday: 8, friday: 8 },
countryId: "country_de",
federalState: "BY",
metroCityId: null,
country: { code: "DE" },
metroCity: null,
};
const db = {
resource: {
findUnique: vi.fn().mockResolvedValue(resource),
},
};
const { listAssignmentBookings } = await import("@capakraken/application");
vi.mocked(listAssignmentBookings).mockResolvedValue([]);
const caller = createProtectedCaller(db);
const result = await caller.findCapacity({
resourceId: "res_1",
startDate: new Date("2026-01-05T00:00:00.000Z"),
endDate: new Date("2026-01-07T00:00:00.000Z"),
minAvailableHoursPerDay: 4,
});
expect(result).toHaveLength(2);
expect(result[0]).toEqual(
expect.objectContaining({
startDate: new Date("2026-01-05T00:00:00.000Z"),
endDate: new Date("2026-01-05T00:00:00.000Z"),
}),
);
expect(result[1]).toEqual(
expect.objectContaining({
startDate: new Date("2026-01-07T00:00:00.000Z"),
endDate: new Date("2026-01-07T00:00:00.000Z"),
}),
);
});
it("throws NOT_FOUND when resource does not exist", async () => {
@@ -265,11 +430,16 @@ describe("staffing.findCapacity", () => {
).rejects.toThrow("Resource not found");
});
it("passes minAvailableHoursPerDay to engine", async () => {
it("honors minAvailableHoursPerDay when computing holiday-aware windows", async () => {
const resource = {
id: "res_1",
displayName: "Alice",
availability: { monday: 8, tuesday: 8, wednesday: 8, thursday: 8, friday: 8 },
countryId: "country_de",
federalState: "BY",
metroCityId: null,
country: { code: "DE" },
metroCity: null,
};
const db = {
resource: {
@@ -277,21 +447,30 @@ describe("staffing.findCapacity", () => {
},
};
const { findCapacityWindows } = await import("@capakraken/staffing");
const { listAssignmentBookings } = await import("@capakraken/application");
vi.mocked(listAssignmentBookings).mockResolvedValue([
{
id: "a1",
projectId: "project_1",
resourceId: "res_1",
startDate: new Date("2026-04-01T00:00:00.000Z"),
endDate: new Date("2026-04-30T00:00:00.000Z"),
hoursPerDay: 3,
dailyCostCents: 0,
status: "CONFIRMED",
project: { id: "project_1", name: "Project", shortCode: "PRJ", status: "ACTIVE", orderType: "CHARGEABLE", clientId: null, dynamicFields: null },
resource: { id: "res_1", displayName: "Alice", chapter: "VFX" },
},
]);
const caller = createProtectedCaller(db);
await caller.findCapacity({
const result = await caller.findCapacity({
resourceId: "res_1",
startDate: new Date("2026-04-01"),
endDate: new Date("2026-04-30"),
minAvailableHoursPerDay: 6,
});
expect(findCapacityWindows).toHaveBeenCalledWith(
expect.anything(),
expect.anything(),
expect.any(Date),
expect.any(Date),
6,
);
expect(result.every((window) => window.availableHoursPerDay >= 6)).toBe(true);
});
});