Files
TJWaterFrontend_Refine/src/components/olmap/MonitoringPlaceOptimization/schemeApi.test.ts
T
jiang 29b8babd68 fix(api): align frontend with project-scoped backend
Use project-scoped endpoints and run IDs for current project, SCADA metadata, historical time series, migrated DMA results, and sensor placement runs.

The regressions recurred because tests mocked pre-interceptor and new-only payload shapes, while history queries relied on implicit global scheme state and unbounded request fan-out. Cover post-interceptor pages, migrated result_rows, explicit run_id routing, multi-element requests, request limits, and cancellation.
2026-09-01 14:37:51 +08:00

102 lines
2.9 KiB
TypeScript

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.each([
["contract page", { items: [run], total: 1, limit: 1000, offset: 0 }],
["interceptor-unwrapped page", [run]],
])("maps runs from the %s response", async (_label, data) => {
jest.mocked(api.get).mockResolvedValue({
data,
});
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" },
);
});
});