|
| 1 | +import assert from "node:assert/strict"; |
| 2 | +import test from "node:test"; |
| 3 | +import React from "react"; |
| 4 | +import { act, create } from "react-test-renderer"; |
| 5 | +import { AppRouterContext } from "next/dist/shared/lib/app-router-context.shared-runtime"; |
| 6 | +import type { SystemHealthResponse } from "@shared/contracts"; |
| 7 | +import { HealthScreen } from "./health"; |
| 8 | + |
| 9 | +(globalThis as typeof globalThis & { React: typeof React }).React = React; |
| 10 | +(globalThis as { IS_REACT_ACT_ENVIRONMENT?: boolean }).IS_REACT_ACT_ENVIRONMENT = true; |
| 11 | + |
| 12 | +const data: SystemHealthResponse = { |
| 13 | + generatedAt: "2026-08-20T12:00:00.000Z", |
| 14 | + summary: { total: 2, live: 1, down: 1, notConfigured: 0, criticalDown: 1 }, |
| 15 | + integrations: [ |
| 16 | + { |
| 17 | + id: "database", |
| 18 | + label: "Database", |
| 19 | + group: "core", |
| 20 | + envVars: ["DATABASE_URL"], |
| 21 | + critical: true, |
| 22 | + mode: "live", |
| 23 | + ping: { ok: true, latencyMs: 12 }, |
| 24 | + }, |
| 25 | + { |
| 26 | + id: "jira", |
| 27 | + label: "Jira", |
| 28 | + group: "core", |
| 29 | + envVars: ["JIRA_API_TOKEN"], |
| 30 | + critical: true, |
| 31 | + mode: "down", |
| 32 | + ping: { ok: false, latencyMs: 40, error: "Jira authentication check failed." }, |
| 33 | + }, |
| 34 | + ], |
| 35 | + alerts: [ |
| 36 | + { |
| 37 | + severity: "critical", |
| 38 | + integrationId: "jira", |
| 39 | + message: "Jira: Jira authentication check failed.", |
| 40 | + fixHint: "Check JIRA_API_TOKEN and the provider setup.", |
| 41 | + }, |
| 42 | + ], |
| 43 | +}; |
| 44 | + |
| 45 | +function textOf(value: unknown): string { |
| 46 | + if (typeof value === "string" || typeof value === "number") return String(value); |
| 47 | + if (Array.isArray(value)) return value.map(textOf).join(" "); |
| 48 | + if (value && typeof value === "object" && "children" in value) { |
| 49 | + return textOf((value as { children?: unknown }).children); |
| 50 | + } |
| 51 | + return ""; |
| 52 | +} |
| 53 | + |
| 54 | +test("health screen exposes the failed service, fix hint, and safe env names", () => { |
| 55 | + const router = { refresh() {} }; |
| 56 | + let renderer!: ReturnType<typeof create>; |
| 57 | + act(() => { |
| 58 | + renderer = create( |
| 59 | + <AppRouterContext.Provider value={router as never}> |
| 60 | + <HealthScreen data={data} /> |
| 61 | + </AppRouterContext.Provider>, |
| 62 | + ); |
| 63 | + }); |
| 64 | + const text = textOf(renderer.toJSON()); |
| 65 | + assert.match(text, /Action required/); |
| 66 | + assert.match(text, /Jira authentication check failed/); |
| 67 | + assert.match(text, /JIRA_API_TOKEN/); |
| 68 | + assert.match(text, /DATABASE_URL/); |
| 69 | + assert.doesNotMatch(text, /secret-value/); |
| 70 | + act(() => renderer.unmount()); |
| 71 | +}); |
| 72 | + |
| 73 | +test("scan again refreshes the server data", () => { |
| 74 | + let refreshes = 0; |
| 75 | + const router = { refresh: () => refreshes++ }; |
| 76 | + let renderer!: ReturnType<typeof create>; |
| 77 | + act(() => { |
| 78 | + renderer = create( |
| 79 | + <AppRouterContext.Provider value={router as never}> |
| 80 | + <HealthScreen data={{ ...data, alerts: [], summary: { ...data.summary, down: 0, criticalDown: 0 } }} /> |
| 81 | + </AppRouterContext.Provider>, |
| 82 | + ); |
| 83 | + }); |
| 84 | + const button = renderer.root.findByProps({ children: "Scan again" }); |
| 85 | + act(() => button.props.onClick()); |
| 86 | + assert.equal(refreshes, 1); |
| 87 | + act(() => renderer.unmount()); |
| 88 | +}); |
0 commit comments