import { act, cleanup, fireEvent, render, screen } from "@testing-library/react"; import { afterEach, expect, it, vi } from "vitest"; import { ScadaAlertAtmosphere } from "./scada-alert-atmosphere"; import type { ScadaAlertAtmosphereState } from "../hooks/use-scada-alert-notices"; const preview: ScadaAlertAtmosphereState = { id: "preview-1", projectId: "project-1", preview: true, alert: null }; afterEach(() => { cleanup(); vi.useRealTimers(); }); it("shows a local preview with a dismiss button and no investigation action", () => { const onDismiss = vi.fn(), onOpen = vi.fn(); render(); expect(screen.getByRole("status")).toHaveTextContent("爆管告警效果预览"); expect(screen.queryByRole("button", { name: "查看调查" })).not.toBeInTheDocument(); const close = screen.getByRole("button", { name: "关闭告警灯效" }); expect(close).toHaveTextContent("关闭灯效"); fireEvent.click(close); expect(onDismiss).toHaveBeenCalledWith("preview-1"); expect(onOpen).not.toHaveBeenCalled(); }); it("opens the actual linked investigation while retaining the visual warning", () => { const onDismiss = vi.fn(), onOpen = vi.fn(); const alert = { id: "incident-1", conditionId: "operational-task:task-1", title: "SCADA 指标异常", description: "已启动调查", time: "08:00" }; render(); expect(screen.getByRole("status")).toHaveTextContent("爆管风险调查中"); fireEvent.click(screen.getByRole("button", { name: "查看调查" })); expect(onDismiss).not.toHaveBeenCalled(); expect(onOpen).toHaveBeenCalledWith(alert); }); it("keeps the warning for a long-running event until its state is removed", () => { vi.useFakeTimers(); const onDismiss = vi.fn(), onOpen = vi.fn(); const { rerender } = render(); act(() => vi.advanceTimersByTime(24 * 60 * 60_000)); expect(onDismiss).not.toHaveBeenCalled(); expect(screen.getByRole("status")).toBeVisible(); rerender(); expect(screen.queryByRole("status")).not.toBeInTheDocument(); });