import { api } from "@/lib/api"; import { exportSensorPlacementExcel, getSensorPlacementScheme, listSensorPlacementSchemes, optimizeSensorPlacement, overwriteSensorPlacementScheme, } from "./schemeApi"; jest.mock("@/lib/api", () => ({ api: { get: jest.fn(), post: jest.fn(), put: jest.fn(), }, })); const run = { run_id: "9fb546cb-5f72-4ddd-8a46-d13e9aee2c6b", name: "sensor-plan-a", sensor_count: 2, min_diameter: 300, created_by: "operator", created_at: "2026-08-25T08:00:00Z", sensor_locations: ["J-1", "J-2"], sensor_points: [], can_edit: true, status: "completed", }; describe("sensor placement runs API adapter", () => { beforeEach(() => jest.clearAllMocks()); it("lists paged runs and maps run fields to the existing screen model", async () => { jest.mocked(api.get).mockResolvedValue({ data: { items: [run], total: 1, limit: 1000, offset: 0 }, }); await expect(listSensorPlacementSchemes()).resolves.toEqual([ expect.objectContaining({ id: run.run_id, scheme_name: run.name, sensor_number: 2, sensor_location: ["J-1", "J-2"], }), ]); expect(api.get).toHaveBeenCalledWith( expect.stringContaining("/api/v1/sensor-placement-runs"), { params: { limit: 1000, offset: 0 } }, ); }); it("uses run endpoints for creation and detail lookup", async () => { jest.mocked(api.post).mockResolvedValue({ data: run }); jest.mocked(api.get).mockResolvedValue({ data: run }); await optimizeSensorPlacement({ run_name: run.name, sensor_type: "pressure", method: "sensitivity", sensor_count: 2, min_diameter: 300, }); await getSensorPlacementScheme(run.run_id); expect(api.post).toHaveBeenCalledWith( expect.stringMatching(/\/api\/v1\/sensor-placement-runs$/), expect.objectContaining({ run_name: run.name }), ); expect(api.get).toHaveBeenLastCalledWith( expect.stringContaining(`/api/v1/sensor-placement-runs/${run.run_id}`), ); }); it("uses the plural result fields required by update and export", async () => { jest.mocked(api.put).mockResolvedValue({ data: run }); jest.mocked(api.post).mockResolvedValue({ data: new Blob() }); await overwriteSensorPlacementScheme(run.run_id, ["J-1"], ["J-2"]); await exportSensorPlacementExcel(run.run_id, ["J-2"], { "J-2": "added" }); expect(api.put).toHaveBeenCalledWith( expect.stringContaining(`/sensor-placement-runs/${run.run_id}`), { expected_sensor_locations: ["J-1"], sensor_locations: ["J-2"], }, ); expect(api.post).toHaveBeenCalledWith( expect.stringContaining(`/sensor-placement-runs/${run.run_id}/exports/excel`), { sensor_locations: ["J-2"], adjustment_status: { "J-2": "added" }, }, { responseType: "blob" }, ); }); });