import { beforeEach, describe, expect, it, vi } from "vitest"; import { API_PATHS } from "@/api/api-paths"; import type { ServiceInput } from "@/domain/services/types/service"; const apiGet = vi.fn(); const apiPost = vi.fn(); const apiPostNoContent = vi.fn(); const apiPut = vi.fn(); vi.mock("@/api/api", () => ({ apiGet: (...args: unknown[]) => apiGet(...args), apiPost: (...args: unknown[]) => apiPost(...args), apiPostNoContent: (...args: unknown[]) => apiPostNoContent(...args), apiPut: (...args: unknown[]) => apiPut(...args), })); import { servicesApi } from "@/domain/services/api/services-api"; describe("servicesApi", () => { beforeEach(() => { apiGet.mockReset(); apiPost.mockReset(); apiPostNoContent.mockReset(); apiPut.mockReset(); }); it("loads active services and maps the linked completion template", async () => { apiGet.mockResolvedValue({ items: [ { Id: 7, Name: "HVAC inspection", Trade: "HVAC", IconKey: "droplets", RequiresCompletionDocument: true, CompletionDocTemplate: { Id: 4, Name: "HVAC checklist" }, IsActive: true, SupportedWorkOrderTypes: [2, 6], }, ], }); const services = await servicesApi.getAll(true); expect(apiGet).toHaveBeenCalledWith(API_PATHS.services.list, { searchParams: { isActive: true }, }); expect(services[0]).toMatchObject({ id: 7, name: "HVAC inspection", completionDocTemplate: { id: 4, name: "HVAC checklist" }, supportedWorkOrderTypes: ["PM", "Reactive"], }); }); it("loads active services filtered by the selected work-order type", async () => { apiGet.mockResolvedValue([ { id: 7, name: "HVAC inspection", trade: "HVAC", iconKey: "hvac-inspection", isActive: true, supportedWorkOrderTypes: [2, "Reactive"], }, ]); const services = await servicesApi.getActiveForWorkOrderType("PM"); expect(apiGet).toHaveBeenCalledWith(API_PATHS.services.list, { searchParams: { isActive: true, workOrderType: 2 }, }); expect(services[0]).toMatchObject({ id: 7, name: "HVAC inspection", trade: "HVAC", supportedWorkOrderTypes: ["PM", "Reactive"], }); }); it("does not request services for the unsupported Overdue type", async () => { const services = await servicesApi.getActiveForWorkOrderType("Overdue"); expect(apiGet).not.toHaveBeenCalled(); expect(services).toEqual([]); }); it("creates, updates, and deactivates through the registry routes", async () => { const input: ServiceInput = { name: "Boiler inspection", trade: "HVAC", iconKey: "droplets", requiresCompletionDocument: false, completionDocTemplateId: null, supportedWorkOrderTypes: ["PM"], }; apiPost.mockResolvedValue({ id: 8, ...input, isActive: true, supportedWorkOrderTypes: [2] }); apiPut.mockResolvedValue({ id: 8, ...input, completionDocTemplateId: 12, isActive: true, supportedWorkOrderTypes: [2], }); apiPostNoContent.mockResolvedValue(undefined); await servicesApi.create(input); const updated = await servicesApi.update(8, input); await servicesApi.deactivate(8); expect(apiPost).toHaveBeenNthCalledWith(1, API_PATHS.services.list, { ...input, supportedWorkOrderTypes: [2], }); expect(apiPostNoContent).toHaveBeenCalledWith(API_PATHS.services.deactivate(8)); expect(apiPut).toHaveBeenCalledWith(API_PATHS.services.byId(8), { ...input, supportedWorkOrderTypes: [2], }); expect(updated.completionDocTemplate).toEqual({ id: 12, name: "" }); }); it("loads active Completion Document Template options from the completion route", async () => { apiGet.mockResolvedValue([{ Id: 4, Name: "HVAC checklist", IsActive: true }]); const templates = await servicesApi.getCompletionTemplates(); expect(apiGet).toHaveBeenCalledWith(API_PATHS.workOrder.completionTemplates); expect(templates).toEqual([{ id: 4, name: "HVAC checklist" }]); }); });