import { describe, expect, it } from "vitest"; import { chvAttachmentBlocked, chvAvailability, chvAvailabilityText, chvQueueHint, chvVmBadge, chvVmDisplay, chvVmStatusText, countChvWorkers, isWorkerAlive } from "./chv"; // closeup 轨补边角:上一轨 chv.test.ts 已覆主路径后仍缺的 // 显式 false、undefined、优先级与“旧 bug 文案不复发”断言。 // 只测 chv.ts 纯逻辑,不渲染组件、不发请求。 describe("chvVmDisplay 边角(closeup)", () => { it("字段显式 false 与 absent 同属 not-reported", () => { const d = chvVmDisplay({ executor: "cloud-hypervisor", chv_vm_dead: false }); expect(d.state).toBe("not-reported"); expect(d.text).toContain("不代表 VM 活着"); }); it("任务 undefined 与 null 同属 unknown", () => { expect(chvVmDisplay(undefined).state).toBe("unknown"); expect(chvVmDisplay(undefined).text).toContain("无法判断"); }); it("非 CHV 且无 executor 字段时也是 not-applicable", () => { expect(chvVmDisplay({}).state).toBe("not-applicable"); expect(chvVmDisplay({ executor: undefined }).text).toBe(""); expect(chvVmStatusText({ executor: "cloud-hypervisor", chv_vm_dead: false })).toContain("未报告 VM 死亡"); }); it("列表徽标:显式 false 不出徽标", () => { expect(chvVmBadge({ executor: "cloud-hypervisor", chv_vm_dead: false })).toBe(""); }); }); describe("chvAvailability 优先级(closeup)", () => { it("加载失败优先于聚合文案:不把失败写成 aggregate-only", () => { expect(chvAvailability(null, "完整 worker 清单仅全局管理员可见", "加载失败:x")).toEqual( { state: "unknown", reason: "load-failed" }, ); }); it("有清单时忽略错误与聚合文案", () => { expect(chvAvailability([{ executor: "cloud-hypervisor", alive: false }], "完整 worker 清单仅全局管理员可见", "加载失败:x")).toEqual( { state: "known", online: 0, total: 1 }, ); }); it("空清单与空文案是 not-loaded,不是已知 0", () => { expect(chvAvailability(null, "", "")).toEqual({ state: "unknown", reason: "not-loaded" }); }); it("空 worker 数组是已知 0(与 null 区分)", () => { expect(chvAvailability([], "", "")).toEqual({ state: "known", online: 0, total: 0 }); expect(countChvWorkers([])).toEqual({ online: 0, total: 0 }); }); }); describe("chvAvailabilityText:Workers 旧 bug 不复发(closeup)", () => { it("load-failed / not-loaded 绝不写“仅全局管理员可见”", () => { expect(chvAvailabilityText({ state: "unknown", reason: "load-failed" })).not.toContain("仅全局管理员可见"); expect(chvAvailabilityText({ state: "unknown", reason: "not-loaded" })).not.toContain("仅全局管理员可见"); }); it("known 总数为 0 时仍是已知 0,不写未知", () => { const text = chvAvailabilityText({ state: "known", online: 0, total: 0 }); expect(text).toContain("在线 0 / 共 0"); expect(text).not.toContain("未知"); }); }); describe("附件拦截与排队提示边角(closeup)", () => { it("executor 为 undefined/null 时附件不拦", () => { expect(chvAttachmentBlocked(undefined, true)).toBe(false); expect(chvAttachmentBlocked(null, true)).toBe(false); }); it("排队提示:未知三原因一律为空串", () => { for (const reason of ["aggregate-only", "load-failed", "not-loaded"] as const) { expect(chvQueueHint("cloud-hypervisor", "queued", { state: "unknown", reason })).toBe(""); } }); }); describe("isWorkerAlive 边界(closeup)", () => { it("last_seen 恰好 120 秒算在线(<=120)", () => { const now = Date.now() / 1000; expect(isWorkerAlive({ executor: "cloud-hypervisor", last_seen: now - 120 })).toBe(true); }); });