|
| 1 | +import { |
| 2 | + afterEach, |
| 3 | + beforeEach, |
| 4 | + describe, |
| 5 | + expect, |
| 6 | + it, |
| 7 | + vi, |
| 8 | +} from "vitest"; |
| 9 | +import { act, render, screen, waitFor } from "@testing-library/react"; |
| 10 | + |
| 11 | +vi.mock("@/api/client", () => ({ |
| 12 | + api: { |
| 13 | + authHeaders: vi.fn(() => ({ "X-API-Key": "test" })), |
| 14 | + setApiKey: vi.fn(), |
| 15 | + isMockMode: false, |
| 16 | + onMockChange: vi.fn(() => () => {}), |
| 17 | + }, |
| 18 | +})); |
| 19 | + |
| 20 | +vi.mock("@/lib/constants", () => ({ |
| 21 | + API_BASE_URL: "/api", |
| 22 | + POLL_INTERVAL: 5000, |
| 23 | +})); |
| 24 | + |
| 25 | +// Recharts uses ResizeObserver which jsdom does not implement. |
| 26 | +class _ResizeObserver implements ResizeObserver { |
| 27 | + observe(_target: Element, _options?: ResizeObserverOptions): void {} |
| 28 | + unobserve(_target: Element): void {} |
| 29 | + disconnect(): void {} |
| 30 | +} |
| 31 | +if (typeof globalThis.ResizeObserver === "undefined") { |
| 32 | + globalThis.ResizeObserver = _ResizeObserver as unknown as typeof ResizeObserver; |
| 33 | +} |
| 34 | + |
| 35 | +import { WorkQueuePanel } from "./WorkQueuePanel"; |
| 36 | + |
| 37 | +// --------------------------------------------------------------------------- |
| 38 | +// Fake EventSource |
| 39 | +// --------------------------------------------------------------------------- |
| 40 | + |
| 41 | +type Listener = (ev: MessageEvent | Event) => void; |
| 42 | + |
| 43 | +class FakeEventSource { |
| 44 | + static instances: FakeEventSource[] = []; |
| 45 | + url: string; |
| 46 | + withCredentials: boolean; |
| 47 | + readyState = 0; |
| 48 | + private listeners = new Map<string, Set<Listener>>(); |
| 49 | + onerror: ((ev: Event) => void) | null = null; |
| 50 | + onopen: ((ev: Event) => void) | null = null; |
| 51 | + |
| 52 | + constructor(url: string, init?: { withCredentials?: boolean }) { |
| 53 | + this.url = url; |
| 54 | + this.withCredentials = Boolean(init?.withCredentials); |
| 55 | + FakeEventSource.instances.push(this); |
| 56 | + // Defer "open" to the next microtask so subscribers can attach first. |
| 57 | + queueMicrotask(() => { |
| 58 | + this.readyState = 1; |
| 59 | + this.dispatch("open", new Event("open")); |
| 60 | + }); |
| 61 | + } |
| 62 | + |
| 63 | + addEventListener(type: string, fn: Listener) { |
| 64 | + if (!this.listeners.has(type)) this.listeners.set(type, new Set()); |
| 65 | + this.listeners.get(type)!.add(fn); |
| 66 | + } |
| 67 | + |
| 68 | + removeEventListener(type: string, fn: Listener) { |
| 69 | + this.listeners.get(type)?.delete(fn); |
| 70 | + } |
| 71 | + |
| 72 | + close() { |
| 73 | + this.readyState = 2; |
| 74 | + } |
| 75 | + |
| 76 | + dispatch(type: string, ev: MessageEvent | Event) { |
| 77 | + this.listeners.get(type)?.forEach((fn) => fn(ev)); |
| 78 | + } |
| 79 | + |
| 80 | + emitWorkStats(payload: Record<string, unknown>) { |
| 81 | + const ev = new MessageEvent("work_stats", { |
| 82 | + data: JSON.stringify(payload), |
| 83 | + }); |
| 84 | + this.dispatch("work_stats", ev); |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +beforeEach(() => { |
| 89 | + FakeEventSource.instances = []; |
| 90 | + // @ts-expect-error - install fake on global |
| 91 | + globalThis.EventSource = FakeEventSource; |
| 92 | +}); |
| 93 | + |
| 94 | +afterEach(() => { |
| 95 | + vi.restoreAllMocks(); |
| 96 | +}); |
| 97 | + |
| 98 | +function sample(depth: number, extra: Record<string, unknown> = {}) { |
| 99 | + return { |
| 100 | + depth, |
| 101 | + pending: 0, |
| 102 | + oldest_queued_at: null, |
| 103 | + workers_polling: 0, |
| 104 | + ts: Date.now() / 1000, |
| 105 | + ...extra, |
| 106 | + }; |
| 107 | +} |
| 108 | + |
| 109 | +async function waitForSource() { |
| 110 | + await waitFor(() => |
| 111 | + expect(FakeEventSource.instances.length).toBeGreaterThan(0) |
| 112 | + ); |
| 113 | + return FakeEventSource.instances[FakeEventSource.instances.length - 1]; |
| 114 | +} |
| 115 | + |
| 116 | +describe("WorkQueuePanel", () => { |
| 117 | + it("returns null when environmentId is missing", () => { |
| 118 | + const { container } = render(<WorkQueuePanel environmentId={null} />); |
| 119 | + expect(container.firstChild).toBeNull(); |
| 120 | + }); |
| 121 | + |
| 122 | + it("renders depth from the first work_stats event", async () => { |
| 123 | + render(<WorkQueuePanel environmentId="env_1" />); |
| 124 | + const es = await waitForSource(); |
| 125 | + |
| 126 | + await act(async () => { |
| 127 | + es.emitWorkStats(sample(3)); |
| 128 | + }); |
| 129 | + |
| 130 | + expect(screen.getByTestId("work-queue-depth")).toHaveTextContent("3"); |
| 131 | + }); |
| 132 | + |
| 133 | + it("flips the status pill to amber at depth=10", async () => { |
| 134 | + render(<WorkQueuePanel environmentId="env_1" />); |
| 135 | + const es = await waitForSource(); |
| 136 | + |
| 137 | + await act(async () => { |
| 138 | + es.emitWorkStats(sample(2)); |
| 139 | + }); |
| 140 | + expect(screen.getByTestId("work-queue-pill")).toHaveAttribute( |
| 141 | + "data-level", |
| 142 | + "green" |
| 143 | + ); |
| 144 | + |
| 145 | + await act(async () => { |
| 146 | + es.emitWorkStats(sample(10)); |
| 147 | + }); |
| 148 | + expect(screen.getByTestId("work-queue-pill")).toHaveAttribute( |
| 149 | + "data-level", |
| 150 | + "amber" |
| 151 | + ); |
| 152 | + }); |
| 153 | + |
| 154 | + it("collects multiple samples for the sparkline", async () => { |
| 155 | + render(<WorkQueuePanel environmentId="env_1" />); |
| 156 | + const es = await waitForSource(); |
| 157 | + |
| 158 | + await act(async () => { |
| 159 | + es.emitWorkStats(sample(1)); |
| 160 | + es.emitWorkStats(sample(2)); |
| 161 | + es.emitWorkStats(sample(3)); |
| 162 | + es.emitWorkStats(sample(4)); |
| 163 | + es.emitWorkStats(sample(5)); |
| 164 | + }); |
| 165 | + |
| 166 | + const spark = screen.getByTestId("work-queue-sparkline"); |
| 167 | + expect(spark.getAttribute("data-sample-count")).toBe("5"); |
| 168 | + }); |
| 169 | + |
| 170 | + it("subscribes to the correct SSE endpoint", async () => { |
| 171 | + render(<WorkQueuePanel environmentId="env_xyz" />); |
| 172 | + const es = await waitForSource(); |
| 173 | + expect(es.url).toContain("/admin/environments/env_xyz/work/stream"); |
| 174 | + }); |
| 175 | + |
| 176 | + it("renders secondary metrics", async () => { |
| 177 | + render(<WorkQueuePanel environmentId="env_1" />); |
| 178 | + const es = await waitForSource(); |
| 179 | + |
| 180 | + await act(async () => { |
| 181 | + es.emitWorkStats( |
| 182 | + sample(0, { |
| 183 | + pending: 4, |
| 184 | + workers_polling: 7, |
| 185 | + }) |
| 186 | + ); |
| 187 | + }); |
| 188 | + |
| 189 | + expect(screen.getByTestId("work-queue-pending")).toHaveTextContent("4"); |
| 190 | + expect(screen.getByTestId("work-queue-workers")).toHaveTextContent("7"); |
| 191 | + }); |
| 192 | + |
| 193 | + it("renders the depth value with aria-live polite", async () => { |
| 194 | + render(<WorkQueuePanel environmentId="env_1" />); |
| 195 | + await waitForSource(); |
| 196 | + expect(screen.getByTestId("work-queue-depth")).toHaveAttribute( |
| 197 | + "aria-live", |
| 198 | + "polite" |
| 199 | + ); |
| 200 | + }); |
| 201 | +}); |
0 commit comments