// MUST be first: sets DATABASE_URL before db_client is evaluated. import "./setup_env"; import { test, expect, describe } from "bun:test"; import { pickNodeTarget } from "../src/cluster"; /** * 「产物只在跑它的那台机器上」。 * * 事故形状(2026-09-10,feature 859e2594):home node 收尾时被同步 git 堵住事件循环, * 心跳停发 167 秒被判 offline;这一批刚报完 manual_review,控制面回头要 ImplReport, * 请求被派给同平台的另一台机器 —— 那台盘上没有这个 workspace,回了 `no impl report`, * 而这条错误是「永久错误、永不重试」。报告就此永久缺失,源文件却一直好端端在 home node。 */ const node = (id: string, status: "online" | "offline" = "online") => ({ id, baseUrl: `http://${id}:5180`, status, platform: "android" as const }); describe("pickNodeTarget", () => { const live = [node("peer-a"), node("peer-b")]; test("绑了 home node 且在线 ⇒ 就发给它,不是代答", () => { const t = pickNodeTarget({ executorNodeId: "home", platform: "android" }, node("home"), live); expect(t).toEqual({ baseUrl: "http://home:5180", nodeId: "home", fallback: false }); }); test("绑了 home node 但它离线 ⇒ null(交给 502 + 重试),绝不换一台代答", () => { expect(pickNodeTarget({ executorNodeId: "home", platform: "android" }, node("home", "offline"), live)).toBeNull(); // 节点行都没了(被 prune 掉)同理 —— 沉默不是让同伴顶上的理由。 expect(pickNodeTarget({ executorNodeId: "home", platform: "android" }, null, live)).toBeNull(); }); test("从来没绑过 home node 的历史任务 ⇒ 才退化到同平台任意在线节点,并标成代答", () => { const t = pickNodeTarget({ executorNodeId: null, platform: "android" }, null, live); expect(t).toEqual({ baseUrl: "http://peer-a:5180", nodeId: "peer-a", fallback: true }); }); test("退化也只在同平台里挑", () => { const ios = [{ id: "ios-01", baseUrl: "http://ios-01:5180", platform: "ios" as const }]; expect(pickNodeTarget({ executorNodeId: null, platform: "android" }, null, ios)).toBeNull(); expect(pickNodeTarget({ executorNodeId: null, platform: "ios" }, null, ios)?.nodeId).toBe("ios-01"); }); test("没有任何在线节点 / 条目为空 ⇒ null", () => { expect(pickNodeTarget({ executorNodeId: null, platform: "android" }, null, [])).toBeNull(); expect(pickNodeTarget(null, node("home"), live)).toBeNull(); }); });