refactor(api): extract timeline allocation router support

This commit is contained in:
2026-03-31 18:46:08 +02:00
parent 66a33a5ad6
commit 973c909e3d
3 changed files with 265 additions and 31 deletions
@@ -0,0 +1,186 @@
import { beforeEach, describe, expect, it, vi } from "vitest";
vi.mock("../sse/event-bus.js", () => ({
emitAllocationUpdated: vi.fn(),
}));
vi.mock("../router/timeline-allocation-assignment-procedure-support.js", () => ({
createTimelineBatchQuickAssignments: vi.fn(),
createTimelineQuickAssignment: vi.fn(),
}));
vi.mock("../router/timeline-allocation-inline-support.js", () => ({
applyTimelineInlineAllocationUpdate: vi.fn(),
}));
vi.mock("../router/timeline-allocation-procedure-support.js", () => ({
shiftTimelineAllocations: vi.fn(),
}));
import { emitAllocationUpdated } from "../sse/event-bus.js";
import {
createTimelineBatchQuickAssignments,
createTimelineQuickAssignment,
} from "../router/timeline-allocation-assignment-procedure-support.js";
import { applyTimelineInlineAllocationUpdate } from "../router/timeline-allocation-inline-support.js";
import { shiftTimelineAllocations } from "../router/timeline-allocation-procedure-support.js";
import {
applyTimelineAllocationBatchShiftMutation,
createTimelineBatchQuickAssignMutation,
createTimelineQuickAssignMutation,
updateTimelineAllocationInlineMutation,
} from "../router/timeline-allocation-router-support.js";
const emitAllocationUpdatedMock = vi.mocked(emitAllocationUpdated);
const createTimelineBatchQuickAssignmentsMock = vi.mocked(createTimelineBatchQuickAssignments);
const createTimelineQuickAssignmentMock = vi.mocked(createTimelineQuickAssignment);
const applyTimelineInlineAllocationUpdateMock = vi.mocked(applyTimelineInlineAllocationUpdate);
const shiftTimelineAllocationsMock = vi.mocked(shiftTimelineAllocations);
describe("timeline allocation router support", () => {
beforeEach(() => {
vi.clearAllMocks();
});
it("updates an inline allocation and emits an update event", async () => {
const db = {} as never;
applyTimelineInlineAllocationUpdateMock.mockResolvedValueOnce({
id: "allocation_1",
projectId: "project_1",
resourceId: "resource_1",
} as never);
await expect(
updateTimelineAllocationInlineMutation({
db,
allocationId: "allocation_1",
hoursPerDay: 8,
startDate: new Date("2026-04-01T00:00:00.000Z"),
endDate: new Date("2026-04-05T00:00:00.000Z"),
includeSaturday: true,
role: "Lead",
}),
).resolves.toEqual({
id: "allocation_1",
projectId: "project_1",
resourceId: "resource_1",
});
expect(applyTimelineInlineAllocationUpdateMock).toHaveBeenCalledWith({
db,
allocationId: "allocation_1",
hoursPerDay: 8,
startDate: new Date("2026-04-01T00:00:00.000Z"),
endDate: new Date("2026-04-05T00:00:00.000Z"),
includeSaturday: true,
role: "Lead",
});
expect(emitAllocationUpdatedMock).toHaveBeenCalledWith({
id: "allocation_1",
projectId: "project_1",
resourceId: "resource_1",
});
});
it("creates a quick assignment with the router source metadata", async () => {
const db = {} as never;
createTimelineQuickAssignmentMock.mockResolvedValueOnce({ id: "allocation_1" } as never);
await expect(
createTimelineQuickAssignMutation(db, {
resourceId: "resource_1",
projectId: "project_1",
startDate: new Date("2026-04-01T00:00:00.000Z"),
endDate: new Date("2026-04-05T00:00:00.000Z"),
hoursPerDay: 8,
role: "Lead",
status: "PROPOSED",
}),
).resolves.toEqual({ id: "allocation_1" });
expect(createTimelineQuickAssignmentMock).toHaveBeenCalledWith(db, {
resourceId: "resource_1",
projectId: "project_1",
startDate: new Date("2026-04-01T00:00:00.000Z"),
endDate: new Date("2026-04-05T00:00:00.000Z"),
hoursPerDay: 8,
role: "Lead",
status: "PROPOSED",
source: "quickAssign",
});
});
it("maps batch quick assignments to the batch router source metadata", async () => {
const db = {} as never;
createTimelineBatchQuickAssignmentsMock.mockResolvedValueOnce({ count: 2 } as never);
await expect(
createTimelineBatchQuickAssignMutation(db, {
assignments: [
{
resourceId: "resource_1",
projectId: "project_1",
startDate: new Date("2026-04-01T00:00:00.000Z"),
endDate: new Date("2026-04-05T00:00:00.000Z"),
hoursPerDay: 8,
role: "Lead",
status: "PROPOSED",
},
{
resourceId: "resource_2",
projectId: "project_2",
startDate: new Date("2026-04-06T00:00:00.000Z"),
endDate: new Date("2026-04-10T00:00:00.000Z"),
hoursPerDay: 6,
role: "Artist",
status: "PROPOSED",
},
],
}),
).resolves.toEqual({ count: 2 });
expect(createTimelineBatchQuickAssignmentsMock).toHaveBeenCalledWith(db, {
assignments: [
{
resourceId: "resource_1",
projectId: "project_1",
startDate: new Date("2026-04-01T00:00:00.000Z"),
endDate: new Date("2026-04-05T00:00:00.000Z"),
hoursPerDay: 8,
role: "Lead",
status: "PROPOSED",
source: "batchQuickAssign",
},
{
resourceId: "resource_2",
projectId: "project_2",
startDate: new Date("2026-04-06T00:00:00.000Z"),
endDate: new Date("2026-04-10T00:00:00.000Z"),
hoursPerDay: 6,
role: "Artist",
status: "PROPOSED",
source: "batchQuickAssign",
},
],
});
});
it("delegates allocation batch shifts unchanged", async () => {
const db = {} as never;
shiftTimelineAllocationsMock.mockResolvedValueOnce({ count: 3 } as never);
await expect(
applyTimelineAllocationBatchShiftMutation(db, {
allocationIds: ["allocation_1", "allocation_2"],
daysDelta: 5,
mode: "preserve-duration",
}),
).resolves.toEqual({ count: 3 });
expect(shiftTimelineAllocationsMock).toHaveBeenCalledWith(db, {
allocationIds: ["allocation_1", "allocation_2"],
daysDelta: 5,
mode: "preserve-duration",
});
});
});