import { readFileSync } from "node:fs"; import { createRequire } from "node:module"; import assert from "node:assert/strict"; import { test } from "node:test"; import { createElement } from "react"; import { renderToStaticMarkup } from "react-dom/server"; import ts from "typescript"; const require = createRequire(import.meta.url); function load(path, overrides) { const source = readFileSync(new URL(path, import.meta.url), "utf8"); const compiled = ts.transpileModule(source, { fileName: path, compilerOptions: { module: ts.ModuleKind.CommonJS, target: ts.ScriptTarget.ES2022, jsx: ts.JsxEmit.ReactJSX }, }).outputText; const exports = {}; new Function("require", "exports", compiled)(id => overrides[id] ?? require(id), exports); return exports; } const format = { teamColor: team => team === 0 ? "red" : "blue", teamName: team => `队伍${team}`, idText: String, firmwareLabel: () => "测试固件" }; const icon = { Icon: () => null }; const healthModule = load("../src/components/HealthBar.tsx", { "../lib/format": format }); const videoModule = load("../src/components/VideoSurface.tsx", { "../lib/store": {}, "../lib/reticle": {}, "./Icon": icon }); let connected = true; const matchTime = load("../src/lib/match-time.ts", {}); const matchModule = load("../src/components/MatchClock.tsx", { "./Dialog": {}, "./Icon": icon, "../lib/ipc": {}, "../lib/store": { useAppStore: () => ({ connected }) }, "../lib/match-time": matchTime, }); const { TeamPage } = load("../src/pages/TeamPage.tsx", { "../lib/use-operator-control": { useOperatorControl: () => ({ target: { current: null }, activeMode: null, pending: false }) }, "../components/OperatorControls": { OperatorControls: () => null }, "../components/HealthBar": healthModule, "../components/VideoSurface": videoModule, "../components/HitFlash": { HitFlash: () => null }, "../components/MatchBanner": { MatchBanner: () => null }, "../components/MatchClock": matchModule, "../lib/format": format, "../lib/store": { useAppStore: () => ({ connected, lastHit: null }) }, "../lib/version": { BUILD: { protocol: 1 } }, }); const config = { visible: true, width: 0.42, pulse: false, lowThreshold: 31, showTeamName: true, showNumbers: true }; const health = { team: 0, name: "测试队伍", available: true, hp: 100, placeholder: "装甲板未接入", critical: false }; const renderHealth = (hp, align = "left", changes = {}) => renderToStaticMarkup(createElement(healthModule.HealthBar, { health: { ...health, hp, ...changes }, config, align, })); test("health length follows every HP value continuously on both sides, including one HP and zero", () => { for (const align of ["left", "right"]) { for (const hp of [0, 1, 17, 19.5, 37, 99, 100, -3, 104]) { const html = renderHealth(hp, align); const expected = Math.max(0, Math.min(100, hp)); assert.match(html, new RegExp(`class="hp__fill" style="width:${expected}%`)); assert.doesNotMatch(html, /hp__cell/); } } }); test("an unavailable armor board has an empty track and a missing-data label", () => { const html = renderHealth(75, "right", { available: false }); assert.match(html, /hp__fill" style="width:0%/); assert.match(html, /装甲板未接入/); assert.doesNotMatch(html, /hp__value|hp__impact/); }); test("the low-health marker uses the exact threshold in the track's mirrored coordinates", () => { for (const align of ["left", "right"]) { assert.match(renderHealth(37, align), /hp__threshold" style="left:31%/); } }); test("critical health preserves each team's color for name, number, fill and border", () => { for (const [team, color] of [[0, "red"], [1, "blue"]]) { for (const hp of [30, 1, 0]) { const html = renderToStaticMarkup(createElement(healthModule.HealthBar, { health: { ...health, team, hp, critical: true }, config: { ...config, pulse: true }, align: "right", })); assert.match(html, /hp--critical/); assert.match(html, /hp--pulsing/); assert.match(html, new RegExp(`hp__fill" style="width:${hp}%;background-color:${color}`)); assert.match(html, new RegExp(`hp__value num" style="color:${color}"`)); assert.doesNotMatch(html, /var\(--error\)/); } } }); function snapshot() { return { config: { cameraId: 1, armorId: 2, teamNames: ["我方", "对手"] }, team: 0, mirrored: false, demo: false, cameras: [{ id: 1, idText: "1", online: true, compatible: true }], armorBoards: [{ id: 2, online: true, team: 0 }], stats: [{ camera: 1, online: true, hasFrames: true, fps: 20 }], matchClock: { phase: "running", remainingMs: 93_000, elapsedMs: 87_000, connected: true, freshForMs: 1500, source: "测试裁判" }, health: [health, { ...health, team: 1 }], }; } const renderTeam = value => renderToStaticMarkup(createElement(TeamPage, { snapshot: value, hud: { health: config, hitFeedback: {}, showZoom: false }, calibrating: false, onCalibrate() {}, })); test("health bars occupy the operator HUD's first row", () => { const html = renderTeam(snapshot()); assert.ok(html.indexOf('class="cockpit__health"') < html.indexOf('class="cockpit__header"')); assert.ok(html.indexOf('class="hp hp--left') < html.indexOf('class="team-match-clock')); assert.ok(html.indexOf('class="team-match-clock') < html.indexOf('class="hp hp--right')); assert.doesNotMatch(html, /match-readout|match-countdown__number/); }); test("only the last five connected countdown seconds cover the operator view", () => { const value = snapshot(); for (const [remainingMs, expected] of [[6000, null], [5000, 5], [4001, 5], [4000, 4], [1, 1], [0, null]]) { value.matchClock = { ...value.matchClock, phase: "countdown", remainingMs }; const html = renderTeam(value); if (expected == null) assert.doesNotMatch(html, /aria-label="开赛全屏倒计时"/); else assert.match(html, new RegExp(`aria-atomic="true">${expected}`)); } for (const phase of ["preparation", "ready", "running", "paused", "finished"]) { value.matchClock.phase = phase; value.matchClock.remainingMs = 4000; assert.doesNotMatch(renderTeam(value), /aria-label="开赛全屏倒计时"/); } value.matchClock.phase = "countdown"; value.matchClock.connected = false; assert.doesNotMatch(renderTeam(value), /aria-label="开赛全屏倒计时"/); value.matchClock.connected = true; connected = false; assert.doesNotMatch(renderTeam(value), /aria-label="开赛全屏倒计时"/); connected = true; }); test("the last frame is masked for camera loss, backend loss, missing binding, or missing frames, and recovers", () => { const cases = [ value => { value.cameras[0].online = false; }, value => { value.stats[0].online = false; }, value => { value.cameras[0].compatible = false; }, value => { value.stats[0].hasFrames = false; }, value => { value.config.cameraId = null; }, value => { value.cameras = []; }, () => { connected = false; }, ]; for (const change of cases) { const value = snapshot(); change(value); const lost = renderTeam(value); assert.match(lost, /vsurface--signal-lost/); assert.match(lost, /vsurface__signal-mask/); assert.match(lost, / { const value = snapshot(); value.armorBoards[0].online = false; assert.doesNotMatch(renderTeam(value), /vsurface--signal-lost|vsurface__signal-mask/); }); const renderControl = (phase, health = []) => renderToStaticMarkup(createElement(matchModule.RefereeMatchControl, { snapshot: { ...snapshot(), config: { teamNames: ["红队", "蓝队"] }, matchClock: { ...snapshot().matchClock, phase }, health }, onError() {}, })); test("skip preparation is available only while preparing and ready still requires confirmation", () => { assert.match(renderControl("preparation"), /跳过准备倒计时/); for (const phase of ["idle", "ready", "countdown", "running", "paused", "finished"]) { assert.doesNotMatch(renderControl(phase), /跳过准备倒计时/); } assert.match(renderControl("ready"), /确认开赛/); }); test("a verdict exposes one-click reset even before the clock finishes, while ongoing play cannot reset", () => { assert.doesNotMatch(renderControl("running"), /重置到未开始/); assert.match(renderControl("finished"), /重置到未开始/); const verdict = renderControl("running", [{ outcome: "defeat" }, { outcome: "victory" }]); assert.match(verdict, /重置到未开始/); assert.doesNotMatch(verdict, /开始 3 分钟准备|结束本局/); }); test("the operator feed reports horizontal and vertical mirroring independently", () => { const value = snapshot(); value.mirrored = true; assert.match(renderTeam(value), /左右镜像/); assert.doesNotMatch(renderTeam(value), /上下镜像/); value.mirrored = false; value.mirroredVertical = true; assert.match(renderTeam(value), /上下镜像/); assert.doesNotMatch(renderTeam(value), /左右镜像/); });