feat-chv-to10:往 10 分方向提升(类型/lint 清零、worker 适配抽取、审计落盘、lockfile 生成脚本、前端四态与统一文案、就绪清单 §13)
This commit is contained in:
1 parent
4fa94edd9b
commit
f42c1f2b9b
22 files changed
+1395
-277
No files matched your search
@@ -1,5 +1,5 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { chvAttachmentBlocked, chvAvailability, chvQueueHint, chvVmBadge, chvVmStatusText, countChvWorkers, isChvExecutor, isWorkerAlive } from "./chv";
|
||||
import { chvAttachmentBlocked, chvAvailability, chvAvailabilityText, chvQueueHint, chvVmBadge, chvVmDisplay, chvVmStatusText, countChvWorkers, isChvExecutor, isWorkerAlive } from "./chv";
|
||||
|
||||
// 纯逻辑测试:只测 chv.ts,不渲染组件、不发请求、不读 localStorage。
|
||||
// alive 规则与 WorkersSection 现有口径一致:alive 字段优先,
|
||||
@@ -103,3 +103,53 @@ describe("chvQueueHint", () => {
|
||||
expect(chvQueueHint("cloud-hypervisor", "queued", { state: "known" as const, online: 1, total: 1 })).toBe("");
|
||||
});
|
||||
});
|
||||
|
||||
describe("chvVmDisplay(to10 四态)", () => {
|
||||
it("字段 present=true → dead", () => {
|
||||
const d = chvVmDisplay({ executor: "cloud-hypervisor", chv_vm_dead: true });
|
||||
expect(d.state).toBe("dead");
|
||||
expect(d.text).toContain("VM 中途死亡");
|
||||
});
|
||||
it("字段 absent → not-reported,不猜 live", () => {
|
||||
const d = chvVmDisplay({ executor: "cloud-hypervisor" });
|
||||
expect(d.state).toBe("not-reported");
|
||||
expect(d.text).toContain("不代表 VM 活着");
|
||||
});
|
||||
it("任务未加载 → unknown", () => {
|
||||
const d = chvVmDisplay(null);
|
||||
expect(d.state).toBe("unknown");
|
||||
expect(d.text).toContain("无法判断");
|
||||
});
|
||||
it("非 CHV → not-applicable,text 为空", () => {
|
||||
const d = chvVmDisplay({ executor: "docker", chv_vm_dead: true });
|
||||
expect(d.state).toBe("not-applicable");
|
||||
expect(d.text).toBe("");
|
||||
});
|
||||
it("旧入口仍是 display 的 text(unknown/not-applicable 回空)", () => {
|
||||
expect(chvVmStatusText({ executor: "cloud-hypervisor", chv_vm_dead: true })).toContain("VM 中途死亡");
|
||||
expect(chvVmStatusText(null)).toBe("");
|
||||
expect(chvVmStatusText({ executor: "docker" })).toBe("");
|
||||
});
|
||||
});
|
||||
|
||||
describe("chvAvailabilityText(to10 四处统一文案)", () => {
|
||||
it("known 零在线:不拦提交、后端是最终权威", () => {
|
||||
const text = chvAvailabilityText({ state: "known", online: 0, total: 2 });
|
||||
expect(text).toContain("在线 0 / 共 2");
|
||||
expect(text).toContain("不拦提交");
|
||||
expect(text).toContain("最终权威");
|
||||
});
|
||||
it("known 非零:只有在线数,无排队警告", () => {
|
||||
const text = chvAvailabilityText({ state: "known", online: 1, total: 1 });
|
||||
expect(text).toContain("在线 1 / 共 1");
|
||||
expect(text).not.toContain("排队");
|
||||
});
|
||||
it("未知三原因各有不同文案,绝不写成 0", () => {
|
||||
expect(chvAvailabilityText({ state: "unknown", reason: "aggregate-only" })).toContain("仅全局管理员可见");
|
||||
expect(chvAvailabilityText({ state: "unknown", reason: "load-failed" })).toContain("加载失败");
|
||||
expect(chvAvailabilityText({ state: "unknown", reason: "not-loaded" })).toContain("尚未加载");
|
||||
for (const reason of ["aggregate-only", "load-failed", "not-loaded"] as const) {
|
||||
expect(chvAvailabilityText({ state: "unknown", reason })).not.toContain("在线 0");
|
||||
}
|
||||
});
|
||||
});
|
||||
@@ -54,27 +54,85 @@ export function chvAttachmentBlocked(
|
||||
return isChvExecutor(executor) && hasAttachments;
|
||||
}
|
||||
|
||||
// VM 只读状态文案:输入只有后端已返回的两个字段,输出
|
||||
// 三种情形之一,绝不编造第四种:
|
||||
// - chv_vm_dead 为 true → 已报告死亡(来源:worker 终局
|
||||
// 写回的 interrupted_reason,见 server _chv_vm_dead_from_row);
|
||||
// - CHV 任务但无该字段 → 未报告死亡,且明写“后端无 live
|
||||
// 状态,不代表 VM 活着”(server 没有 VM 登记表);
|
||||
// - 非 CHV 任务 → 空串(不显示 VM 行)。
|
||||
export function chvVmStatusText(task: {
|
||||
// VM 只读状态:四种情形各有明确文案,绝不编造第五种,
|
||||
// 绝不猜 live(to10 精确化,见 ADR-15):
|
||||
// - dead:CHV 任务且 chv_vm_dead 为 true(字段已 present;
|
||||
// 来源:worker 终局写回的 interrupted_reason,见 server
|
||||
// _chv_vm_dead_from_row);
|
||||
// - not-reported:CHV 任务但字段 absent(后端只在死亡时
|
||||
// 才返回该字段;absent 只表示“未报告”,且明写“后端无
|
||||
// live 状态字段,不代表 VM 活着”,server 没有 VM 登记表);
|
||||
// - unknown:任务详情本身未加载(task 为 null),无法判断;
|
||||
// - not-applicable:非 CHV 任务,不显示 VM 行(text 为空)。
|
||||
export type ChvVmDisplay =
|
||||
| { state: "dead"; text: string }
|
||||
| { state: "not-reported"; text: string }
|
||||
| { state: "unknown"; text: string }
|
||||
| { state: "not-applicable"; text: string };
|
||||
|
||||
export function chvVmDisplay(task: {
|
||||
executor?: string | null;
|
||||
chv_vm_dead?: boolean;
|
||||
}): string {
|
||||
} | null | undefined): ChvVmDisplay {
|
||||
if (task === null || task === undefined) {
|
||||
return {
|
||||
state: "unknown",
|
||||
text: "任务详情未加载,无法判断 VM 状态(后端无 live 状态字段,详情加载后才显示)",
|
||||
};
|
||||
}
|
||||
// 死亡文案同样先过执行器门:后端只对 CHV 任务派生该字段,
|
||||
// 万一某天非 CHV 行误带该字段,前端也不把 docker 任务
|
||||
// 写成“VM 死亡”。
|
||||
if (task.chv_vm_dead && isChvExecutor(task.executor)) {
|
||||
return "VM 中途死亡,任务未完成、断点可续跑(后端只读字段 chv_vm_dead)";
|
||||
return {
|
||||
state: "dead",
|
||||
text: "VM 中途死亡,任务未完成、断点可续跑(后端只读字段 chv_vm_dead)",
|
||||
};
|
||||
}
|
||||
if (isChvExecutor(task.executor)) {
|
||||
return "未报告 VM 死亡(后端无 live 状态字段,不代表 VM 活着)";
|
||||
return {
|
||||
state: "not-reported",
|
||||
text: "未报告 VM 死亡(后端无 live 状态字段,不代表 VM 活着)",
|
||||
};
|
||||
}
|
||||
return "";
|
||||
return { state: "not-applicable", text: "" };
|
||||
}
|
||||
|
||||
// 旧入口(保留给已测调用点):即 chvVmDisplay 的 text,
|
||||
// not-applicable 与 unknown 均回空串(调用点只在已加载的
|
||||
// CHV 行上用它,unknown 分支由详情页的新入口处理)。
|
||||
export function chvVmStatusText(task: {
|
||||
executor?: string | null;
|
||||
chv_vm_dead?: boolean;
|
||||
} | null | undefined): string {
|
||||
const display = chvVmDisplay(task);
|
||||
return display.state === "not-applicable" ||
|
||||
display.state === "unknown"
|
||||
? ""
|
||||
: display.text;
|
||||
}
|
||||
|
||||
// CHV 可用性统一文案(to10):新建任务、Workers、环境
|
||||
// 三处此前各写各的,Workers 的未知分支甚至恒写“仅全局
|
||||
// 管理员可见”(加载失败/未加载时也是这句,是文案 bug)。
|
||||
// 四处统一走本函数:known 零在线时不拦提交、后端是最终
|
||||
// 权威;未知时按 reason 分三句,绝不把未知写成 0。
|
||||
export function chvAvailabilityText(
|
||||
availability: ChvAvailability,
|
||||
): string {
|
||||
if (availability.state === "known") {
|
||||
const base = `CHV worker 在线 ${availability.online} / 共 ${availability.total}(来自 /workers)`;
|
||||
return availability.online === 0
|
||||
? `${base}:当前无 CHV worker 在线,任务将一直排队,请先确认宿主 worker 已起;本提示不拦提交,worker 随后上线可认领,后端是最终权威。`
|
||||
: base;
|
||||
}
|
||||
if (availability.reason === "aggregate-only") {
|
||||
return "CHV worker 在线数未知:完整 worker 清单仅全局管理员可见,非管理员只能看到聚合数,无法按执行器分项。";
|
||||
}
|
||||
if (availability.reason === "load-failed") {
|
||||
return "CHV worker 在线数未知:/workers 加载失败,请到 Workers 区刷新后重试。";
|
||||
}
|
||||
return "CHV worker 在线数未知:/workers 尚未加载,请到 Workers 区刷新。";
|
||||
}
|
||||
|
||||
// 列表行徽标:只有已报告死亡时才出现短徽标;未报告时
|
||||
|
||||
Reference in new issue
Block a user