189 lines
7.8 KiB
TypeScript
189 lines
7.8 KiB
TypeScript
import { beforeEach, describe, expect, it, vi } from "vitest";
|
|
|
|
const mocks = vi.hoisted(() => ({
|
|
loadProjectContext: vi.fn(),
|
|
projectActionErrorResponse: vi.fn(),
|
|
requireProjectPermission: vi.fn(),
|
|
requireProjectView: vi.fn(),
|
|
loadPlantafel: vi.fn(),
|
|
listEditablePlantafelProjectOptions: vi.fn(),
|
|
listAssignableUsers: vi.fn(),
|
|
resolveUserLabels: vi.fn(),
|
|
upsertScheduleEntry: vi.fn(),
|
|
}));
|
|
|
|
vi.mock("~/utils/integrations/projects/projectAccess.server", () => ({
|
|
loadProjectContext: mocks.loadProjectContext,
|
|
projectActionErrorResponse: mocks.projectActionErrorResponse,
|
|
projectNotFoundResponse: () => new Response(null, { status: 404 }),
|
|
requireProjectPermission: mocks.requireProjectPermission,
|
|
requireProjectView: mocks.requireProjectView,
|
|
}));
|
|
|
|
vi.mock("~/utils/integrations/projects/plantafelAcceptanceService.server", () => ({
|
|
applyOfflineMutation: vi.fn(),
|
|
bulkReassignScheduleEntries: vi.fn(),
|
|
listEditablePlantafelProjectOptions: mocks.listEditablePlantafelProjectOptions,
|
|
loadPlantafel: mocks.loadPlantafel,
|
|
updateScheduleEntryStatus: vi.fn(),
|
|
upsertScheduleEntry: mocks.upsertScheduleEntry,
|
|
}));
|
|
|
|
vi.mock("~/utils/integrations/crm/crmService.server", () => ({
|
|
listAssignableUsers: mocks.listAssignableUsers,
|
|
resolveUserLabels: mocks.resolveUserLabels,
|
|
}));
|
|
|
|
import { action, loader } from "./projects.plantafel";
|
|
|
|
const PLANTAFEL = {
|
|
entries: [],
|
|
absences: [],
|
|
projectLabels: [{ id: "project-visible", label: "P-099 · Sichtbares Projekt" }],
|
|
editableProjectIds: ["project-visible"],
|
|
generatedAt: "2026-07-15T11:17:00.000Z",
|
|
};
|
|
|
|
describe("Plantafel loader", () => {
|
|
beforeEach(() => {
|
|
vi.clearAllMocks();
|
|
mocks.loadPlantafel.mockResolvedValue(PLANTAFEL);
|
|
mocks.listEditablePlantafelProjectOptions.mockResolvedValue({
|
|
options: [{ id: "project-1", label: "P-100 · Heizungstausch" }],
|
|
hasMore: true,
|
|
query: "",
|
|
});
|
|
mocks.listAssignableUsers.mockResolvedValue([{ id: "user-2", label: "Anna Schneider" }]);
|
|
mocks.resolveUserLabels.mockResolvedValue(new Map([["user-1", "Max Mustermann"]]));
|
|
});
|
|
|
|
it("returns human-readable project and assignee options for writers", async () => {
|
|
const ctx = {
|
|
tenantId: "tenant-1",
|
|
userId: "user-1",
|
|
permissions: ["plantafel.read_all", "plantafel.write", "projects.read_all"],
|
|
};
|
|
mocks.requireProjectView.mockResolvedValue(ctx);
|
|
const request = new Request("https://app.example.test/acme/projects/plantafel?assignedUserId=user-2");
|
|
|
|
const response = await loader({ request, params: { tenant: "acme" }, context: {} } as never);
|
|
const data = await response.json();
|
|
|
|
expect(mocks.loadPlantafel).toHaveBeenCalledWith(expect.objectContaining({ tenantId: "tenant-1", ctx, assignedUserId: "user-2" }));
|
|
expect(mocks.listEditablePlantafelProjectOptions).toHaveBeenCalledWith({ tenantId: "tenant-1", ctx });
|
|
expect(data).toMatchObject({
|
|
plantafel: PLANTAFEL,
|
|
projectLabels: PLANTAFEL.projectLabels,
|
|
editableProjectIds: PLANTAFEL.editableProjectIds,
|
|
projectOptions: [{ id: "project-1", label: "P-100 · Heizungstausch" }],
|
|
projectOptionsHasMore: true,
|
|
assigneeOptions: [{ id: "user-2", label: "Anna Schneider" }],
|
|
canWrite: true,
|
|
});
|
|
});
|
|
|
|
it("reaches into the past only when the table asks for it, without losing the days ahead", async () => {
|
|
const ctx = {
|
|
tenantId: "tenant-1",
|
|
userId: "user-1",
|
|
permissions: ["plantafel.read_all", "plantafel.write", "projects.read_all"],
|
|
};
|
|
mocks.requireProjectView.mockResolvedValue(ctx);
|
|
const windowOf = async (query: string) => {
|
|
mocks.loadPlantafel.mockClear();
|
|
const request = new Request(`https://app.example.test/acme/projects/plantafel${query}`);
|
|
const response = await loader({ request, params: { tenant: "acme" }, context: {} } as never);
|
|
const data = (await response.json()) as { filter: { includePast: boolean } };
|
|
const call = mocks.loadPlantafel.mock.calls[0]![0] as { from: Date; to: Date };
|
|
return { from: call.from, to: call.to, filter: data.filter };
|
|
};
|
|
|
|
const plain = await windowOf("");
|
|
const past = await windowOf("?past=1");
|
|
|
|
expect(plain.filter.includePast).toBe(false);
|
|
expect(past.filter.includePast).toBe(true);
|
|
// The end of the window is identical - the switch only moves the start.
|
|
expect(past.to.getTime()).toBe(plain.to.getTime());
|
|
expect(past.from.getTime()).toBeLessThan(plain.from.getTime());
|
|
// And it stays inside the service's 31-day cap.
|
|
expect(past.to.getTime() - past.from.getTime()).toBeLessThanOrEqual(31 * 24 * 60 * 60 * 1000);
|
|
});
|
|
|
|
it("only resolves the current user's label for a read-own viewer", async () => {
|
|
const ctx = {
|
|
tenantId: "tenant-1",
|
|
userId: "user-1",
|
|
permissions: ["plantafel.read_own", "projects.read_own_member"],
|
|
};
|
|
mocks.requireProjectView.mockResolvedValue(ctx);
|
|
const request = new Request("https://app.example.test/acme/projects/plantafel");
|
|
|
|
const response = await loader({ request, params: { tenant: "acme" }, context: {} } as never);
|
|
const data = await response.json();
|
|
|
|
expect(mocks.listAssignableUsers).not.toHaveBeenCalled();
|
|
expect(mocks.listEditablePlantafelProjectOptions).not.toHaveBeenCalled();
|
|
expect(mocks.resolveUserLabels).toHaveBeenCalledWith(["user-1"]);
|
|
expect(data).toMatchObject({
|
|
assigneeOptions: [{ id: "user-1", label: "Max Mustermann" }],
|
|
canWrite: false,
|
|
});
|
|
});
|
|
|
|
it("serves a lightweight editable project search without loading the board", async () => {
|
|
const ctx = {
|
|
tenantId: "tenant-1",
|
|
userId: "user-1",
|
|
permissions: ["plantafel.read_all", "plantafel.write", "projects.read_all"],
|
|
};
|
|
mocks.requireProjectView.mockResolvedValue(ctx);
|
|
mocks.listEditablePlantafelProjectOptions.mockResolvedValue({
|
|
options: [{ id: "project-2", label: "P-200 · Wärmepumpe" }],
|
|
hasMore: false,
|
|
query: "wärme",
|
|
});
|
|
const request = new Request("https://app.example.test/acme/projects/plantafel?_resource=projectPicker&projectQuery=w%C3%A4rme");
|
|
|
|
const response = await loader({ request, params: { tenant: "acme" }, context: {} } as never);
|
|
const data = await response.json();
|
|
|
|
expect(mocks.listEditablePlantafelProjectOptions).toHaveBeenCalledWith({ tenantId: "tenant-1", ctx, query: "wärme" });
|
|
expect(mocks.loadPlantafel).not.toHaveBeenCalled();
|
|
expect(mocks.listAssignableUsers).not.toHaveBeenCalled();
|
|
expect(data).toEqual({
|
|
resource: "projectPicker",
|
|
projectOptions: [{ id: "project-2", label: "P-200 · Wärmepumpe" }],
|
|
projectOptionsHasMore: false,
|
|
projectQuery: "wärme",
|
|
});
|
|
});
|
|
|
|
it("returns the failed intent so the create dialog can keep its retry flow open", async () => {
|
|
const ctx = {
|
|
tenantId: "tenant-1",
|
|
userId: "user-1",
|
|
permissions: ["plantafel.read_all", "plantafel.write", "projects.read_all"],
|
|
};
|
|
mocks.loadProjectContext.mockResolvedValue(ctx);
|
|
mocks.upsertScheduleEntry.mockRejectedValue(new Error("blocking conflict"));
|
|
mocks.projectActionErrorResponse.mockReturnValue(Response.json({ error: "projects.error.scheduleConflictOverrideRequired" }, { status: 400 }));
|
|
const formData = new URLSearchParams();
|
|
formData.set("_action", "upsertSchedule");
|
|
formData.set("projektId", "project-1");
|
|
formData.set("assignedUserId", "user-2");
|
|
formData.set("title", "Wartung");
|
|
formData.set("startAt", "2026-07-21T09:00");
|
|
formData.set("endAt", "2026-07-21T11:00");
|
|
const request = new Request("https://app.example.test/acme/projects/plantafel", { method: "POST", body: formData });
|
|
|
|
const response = await action({ request, params: { tenant: "acme" }, context: {} } as never);
|
|
|
|
expect(response.status).toBe(400);
|
|
await expect(response.json()).resolves.toEqual({
|
|
error: "projects.error.scheduleConflictOverrideRequired",
|
|
intent: "upsertSchedule",
|
|
});
|
|
});
|
|
});
|