93 lines
3.2 KiB
TypeScript
93 lines
3.2 KiB
TypeScript
import { afterEach, describe, expect, it, vi } from "vitest";
|
|
import { render, screen, waitFor } from "@testing-library/react";
|
|
import userEvent from "@testing-library/user-event";
|
|
|
|
import type { PeripheralSampleClient, PeripheralTestDataRecord } from "./client";
|
|
import { ScadaSystemSection } from "./scada-system-section";
|
|
|
|
describe("SCADA peripheral system", () => {
|
|
afterEach(() => {
|
|
vi.unstubAllGlobals();
|
|
});
|
|
|
|
it("persists a synthetic abnormal reading before triggering Agent handling", async () => {
|
|
const fetchMock = vi
|
|
.fn()
|
|
.mockResolvedValueOnce(new Response(JSON.stringify({
|
|
items: [{ project_id: "project-1", gs_workspace: "tjwater_next", status: "active" }],
|
|
total: 1,
|
|
limit: 1000,
|
|
offset: 0
|
|
}), { status: 200 }))
|
|
.mockResolvedValueOnce(new Response(JSON.stringify({
|
|
items: [{
|
|
device_id: "PRESSURE-17",
|
|
device_type: "pressure",
|
|
measurement_unit: "MPa",
|
|
transmission_mode: "realtime",
|
|
transmission_frequency: "15min",
|
|
observed_at: "2026-10-22T01:02:03Z",
|
|
monitored_value: 0.31,
|
|
cleaned_value: 0.305
|
|
}],
|
|
total: 1,
|
|
limit: 500,
|
|
offset: 0,
|
|
observed_count: 1,
|
|
latest_observed_at: "2026-10-22T01:02:03Z"
|
|
}), { status: 200 }));
|
|
vi.stubGlobal("fetch", fetchMock);
|
|
|
|
const record: PeripheralTestDataRecord = {
|
|
record_id: "50000000-0000-4000-8000-000000000017",
|
|
project_id: "324ff7ee-c2d4-5184-a84b-48fb14a2933e",
|
|
revision: 1,
|
|
label: "SCADA 异常模拟 · PRESSURE-17",
|
|
point_id: "PRESSURE-17",
|
|
observed_at: "2026-10-22T02:03:00Z",
|
|
value: 0.08,
|
|
unit: "MPa",
|
|
quality: "valid",
|
|
notes: "[SCADA_ANOMALY] 压力突降",
|
|
origin: "synthetic",
|
|
created_at: "2026-10-22T02:03:01Z",
|
|
updated_at: "2026-10-22T02:03:01Z",
|
|
created_by: "tester",
|
|
updated_by: "tester"
|
|
};
|
|
const createTestData = vi.fn().mockResolvedValue(record);
|
|
const peripheralClient = {
|
|
listTestData: vi.fn().mockResolvedValue({ items: [], next_cursor: null }),
|
|
createTestData
|
|
} as unknown as PeripheralSampleClient;
|
|
const onAgentTrigger = vi.fn().mockResolvedValue(undefined);
|
|
const user = userEvent.setup();
|
|
|
|
render(
|
|
<ScadaSystemSection
|
|
active
|
|
refreshGeneration={0}
|
|
peripheralClient={peripheralClient}
|
|
canSimulate
|
|
onAgentTrigger={onAgentTrigger}
|
|
/>
|
|
);
|
|
|
|
await screen.findByText("PRESSURE-17", { exact: true });
|
|
await user.click(screen.getByRole("button", { name: "设定读数" }));
|
|
await user.type(screen.getByPlaceholderText("输入需要演练的异常值"), "0.08");
|
|
await user.type(screen.getByPlaceholderText("例如:压力突降,偏离近一小时稳定区间"), "压力突降");
|
|
await user.click(screen.getByRole("button", { name: "保存并触发 Agent" }));
|
|
|
|
await waitFor(() => expect(createTestData).toHaveBeenCalledOnce());
|
|
expect(createTestData).toHaveBeenCalledWith(expect.objectContaining({
|
|
point_id: "PRESSURE-17",
|
|
value: 0.08,
|
|
unit: "MPa",
|
|
quality: "valid",
|
|
notes: "[SCADA_ANOMALY] 压力突降"
|
|
}));
|
|
expect(onAgentTrigger).toHaveBeenCalledWith(record);
|
|
});
|
|
});
|