feat(console): CHV production-grade frontend — worker availability, attachment guard, queue hint, build:console fix, vitest
This commit is contained in:
1 parent
036323e620
commit
003ea357f9
13 files changed
+474
-55
No files matched your search
@@ -0,0 +1,62 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { chvAvailability, chvQueueHint, countChvWorkers, isWorkerAlive } from "./chv";
|
||||
|
||||
// 纯逻辑测试:只测 chv.ts,不渲染组件、不发请求、不读 localStorage。
|
||||
// alive 规则与 WorkersSection 现有口径一致:alive 字段优先,
|
||||
// 缺失时用 last_seen 距今 <= 120 秒判定在线。
|
||||
describe("isWorkerAlive", () => {
|
||||
it("alive 字段优先", () => {
|
||||
expect(isWorkerAlive({ executor: "cloud-hypervisor", alive: true })).toBe(true);
|
||||
expect(isWorkerAlive({ executor: "cloud-hypervisor", alive: false, last_seen: Date.now() / 1000 })).toBe(false);
|
||||
});
|
||||
it("无 alive 时用 last_seen 120 秒门槛", () => {
|
||||
const now = Date.now() / 1000;
|
||||
expect(isWorkerAlive({ executor: "cloud-hypervisor", last_seen: now - 10 })).toBe(true);
|
||||
expect(isWorkerAlive({ executor: "cloud-hypervisor", last_seen: now - 121 })).toBe(false);
|
||||
expect(isWorkerAlive({ executor: "cloud-hypervisor" })).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe("countChvWorkers", () => {
|
||||
it("只数 cloud-hypervisor,不数 docker/local", () => {
|
||||
const now = Date.now() / 1000;
|
||||
expect(countChvWorkers([
|
||||
{ executor: "cloud-hypervisor", alive: true },
|
||||
{ executor: "cloud-hypervisor", last_seen: now - 9999 },
|
||||
{ executor: "docker", alive: true },
|
||||
])).toEqual({ online: 1, total: 2 });
|
||||
});
|
||||
});
|
||||
|
||||
describe("chvAvailability", () => {
|
||||
it("有清单时按清单数", () => {
|
||||
expect(chvAvailability([{ executor: "cloud-hypervisor", alive: true }], "", "")).toEqual(
|
||||
{ state: "known", online: 1, total: 1 },
|
||||
);
|
||||
});
|
||||
it("无清单但聚合文案时是未知,不是 0", () => {
|
||||
expect(chvAvailability(null, "在线 2 / 离线 1(共 3;完整 worker 清单仅全局管理员可见)", "")).toEqual(
|
||||
{ state: "unknown", reason: "aggregate-only" },
|
||||
);
|
||||
});
|
||||
it("无 worker 注册时是已知 0", () => {
|
||||
expect(chvAvailability(null, "无 worker 注册", "")).toEqual({ state: "known", online: 0, total: 0 });
|
||||
});
|
||||
it("加载失败时是未知", () => {
|
||||
expect(chvAvailability(null, "", "加载失败:x")).toEqual({ state: "unknown", reason: "load-failed" });
|
||||
});
|
||||
it("尚未加载时是未知", () => {
|
||||
expect(chvAvailability(null, "未加载", "")).toEqual({ state: "unknown", reason: "not-loaded" });
|
||||
});
|
||||
});
|
||||
|
||||
describe("chvQueueHint", () => {
|
||||
it("仅 CHV + 排队 + 已知零在线时给提示", () => {
|
||||
const knownZero = { state: "known" as const, online: 0, total: 0 };
|
||||
expect(chvQueueHint("cloud-hypervisor", "queued", knownZero)).toContain("可能原因");
|
||||
expect(chvQueueHint("docker", "queued", knownZero)).toBe("");
|
||||
expect(chvQueueHint("cloud-hypervisor", "running", knownZero)).toBe("");
|
||||
expect(chvQueueHint("cloud-hypervisor", "queued", { state: "unknown" as const, reason: "aggregate-only" })).toBe("");
|
||||
expect(chvQueueHint("cloud-hypervisor", "queued", { state: "known" as const, online: 1, total: 1 })).toBe("");
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user