/** * Tests for table.ts — `run_in_background` 结果块末尾那张任务总览表的纯逻辑。 * * Run with: node --test clients/pi/extensions/background-tasks/table.test.ts * * table.ts 不 import pi / pi-tui,宽度量度与上色都由调用方注入。这里用一个与 pi-tui * `visibleWidth` 同口径的本地计数器(ANSI 剥掉、CJK 全角 2 列、`…` 1 列),断言列宽分配、 * 命令截断、行数上限与折叠行这些**决定**。上色与树前缀挂在 index.test.ts 里走真加载器。 */ import assert from "node:assert/strict"; import { describe, it } from "node:test"; import { MAX_TABLE_ROWS, MAX_TABLE_WIDTH, MIN_COMMAND_WIDTH, moreTasksHint, renderTaskTable, selectTableTasks, type TaskTableRow, type TaskTableTask, clampText, } from "./table.ts"; /** 剥 ANSI(这里只是防测试自己带色,实际上色不经过本模块)。 */ function stripAnsi(text: string): string { return text.replace(/\u001b\][^\u0007]*\u0007/g, "").replace(/\u001b\[[0-9;:?]*[a-zA-Z]/g, ""); } /** * 可见宽度,口径与 pi-tui 的 `visibleWidth` 一致(ASCII 1 列、CJK / 全角 2 列)。 * `⚠️` 这类 emoji 区间本用例用不到,只需要覆盖 CJK 这一条真正会踩到的分支。 */ function widthOf(text: string): number { let width = 0; for (const char of stripAnsi(text)) { const code = char.codePointAt(0)!; // CJK 统一表意文字 / 全角标点 / 常用中文标点 const wide = (code >= 0x1100 && code <= 0x115f) || (code >= 0x2e80 && code <= 0xa4cf) || (code >= 0xac00 && code <= 0xd7a3) || (code >= 0xf900 && code <= 0xfaff) || (code >= 0xfe30 && code <= 0xfe6f) || (code >= 0xff00 && code <= 0xff60) || (code >= 0xffe0 && code <= 0xffe6); width += wide ? 2 : 1; } return width; } /** 把片段行拼成可见文本(上色在这里不影响宽度,因为注入的 role 映射是恒等)。 */ function plainRow(row: TaskTableRow): string { return row.map((segment) => segment.text).join(""); } const NOW = 1_000_000; function taskOf(overrides: Partial = {}): TaskTableTask { return { id: "bg_1", command: "npm test", status: "exited", startedAt: NOW - 12_000, endedAt: NOW - 2_000, exitCode: 0, signal: undefined, ...overrides, }; } function render(tasks: TaskTableTask[], width: number, maxRows?: number, focusId?: string): string[] { return renderTaskTable(tasks, { width, now: NOW, widthOf, maxRows, focusId }).map(plainRow); } describe("clampText:宽度口径与省略号", () => { it("装得下就原样返回", () => { assert.equal(clampText("npm test", 20, widthOf), "npm test"); assert.equal(clampText("npm test", 8, widthOf), "npm test", "恰好等于宽度也算装得下"); }); it("装不下就裁到 width - 1 再补 …(尾部空格不留在省略号前面)", () => { assert.equal(clampText("npm test --silent", 10, widthOf), "npm test…"); assert.equal(widthOf(clampText("npm test --silent", 10, widthOf)), 9, "省掉尾空格后是 9 列,padText 补到 10"); assert.equal(clampText("abcdefghijkl", 10, widthOf), "abcdefghi…", "没有空格可略时就是 width - 1 个字 + …"); }); it("width 1 只放得下 …,width 0 / 负数返回空串", () => { assert.equal(clampText("npm test", 1, widthOf), "…"); assert.equal(clampText("npm test", 0, widthOf), ""); assert.equal(clampText("npm test", -3, widthOf), ""); }); it("CJK 按 2 列算,不会超宽", () => { const cut = clampText("运行测试套件并输出详细日志", 11, widthOf); assert.ok(widthOf(cut) <= 11, `宽度 ${widthOf(cut)} 超了 11:${JSON.stringify(cut)}`); assert.ok(cut.endsWith("…"), `应带省略号:${JSON.stringify(cut)}`); }); }); describe("renderTaskTable:形状与列", () => { it("表头是 id / command / status / time(第一列是路由键 id,不是 name)", () => { const lines = render([taskOf()], 60); assert.equal(lines[0]!.startsWith("┌"), true, `首行是顶边框:${JSON.stringify(lines[0])}`); assert.match(lines[1]!, /│ id +│ command +│ status +│ time +│/, `表头列名:${JSON.stringify(lines[1])}`); assert.equal(lines[2]!.startsWith("├"), true, `第三行是表头分隔线:${JSON.stringify(lines[2])}`); assert.equal(lines[lines.length - 1]!.startsWith("└"), true, `末行是底边框:${JSON.stringify(lines[lines.length - 1])}`); }); it("所有行可见宽度严格一致(表格不歪)", () => { const lines = render( [taskOf(), taskOf({ id: "bg_10", command: "sleep 2; echo helloworld", status: "running", endedAt: undefined })], 60, ); const widths = new Set(lines.map((line) => widthOf(line))); assert.equal(widths.size, 1, `各行宽度应一致,实际:${JSON.stringify([...widths])} ${JSON.stringify(lines)}`); }); it("整表宽度恰好等于传入的 width(command 吃掉剩余空间)", () => { for (const width of [40, 60, 80, 120]) { const lines = render([taskOf()], width); assert.equal(widthOf(lines[0]!), Math.min(width, MAX_TABLE_WIDTH), `width=${width} 的表格宽度:${JSON.stringify(lines[0])}`); } }); it("宽度封顶 MAX_TABLE_WIDTH = 55(用户 2026-10-22 定),再宽也不拉伸", () => { assert.equal(MAX_TABLE_WIDTH, 55); for (const width of [55, 56, 80, 120, 200]) { const lines = render([taskOf()], width); assert.equal(widthOf(lines[0]!), 55, `width=${width} 时表格应停在 55:${JSON.stringify(lines[0])}`); } // 窄于 55 时照旧按传入宽度收,封顶不影响窄终端 for (const width of [40, 45, 54]) { assert.equal(widthOf(render([taskOf()], width)[0]!), width, `width=${width} 应原样使用`); } }); it("表头四个格子带 bold + bg 标记,任务行与边框行不带(用户 2026-10-22 定)", () => { const rows = renderTaskTable([taskOf(), taskOf({ id: "bg_2", startedAt: NOW - 1_000 })], { width: 60, now: NOW, widthOf, }); const header = rows[1]!; const headerCells = header.filter((segment) => segment.role === "header"); assert.equal(headerCells.length, 4, `表头四个格子:${JSON.stringify(header)}`); assert.deepEqual( headerCells.map((segment) => ({ bold: segment.bold, bg: segment.bg })), Array.from({ length: 4 }, () => ({ bold: true, bg: true })), ); // 边框(含竖线与格两侧空格)不带标记 —— 只染格子,不通栏 for (const segment of header.filter((segment) => segment.role !== "header")) { assert.equal(segment.bold, undefined, `边框片段不该加粗:${JSON.stringify(segment)}`); assert.equal(segment.bg, undefined, `边框片段不该有底色:${JSON.stringify(segment)}`); } // 任务行与顶/分隔/底边框都不带 for (const row of [rows[0]!, rows[2]!, ...rows.slice(3)]) { for (const segment of row) { assert.notEqual(segment.role, "header"); assert.notEqual(segment.bold, true); assert.notEqual(segment.bg, true); } } }); it("status 走 dock 同一套词汇:running / exit 0 / exit=N / killed(没有 done)", () => { const lines = render( [ taskOf({ id: "bg_1", status: "running", endedAt: undefined, exitCode: undefined }), taskOf({ id: "bg_2", status: "exited", exitCode: 0 }), taskOf({ id: "bg_3", status: "exited", exitCode: 3 }), taskOf({ id: "bg_4", status: "killed", exitCode: undefined, signal: "SIGTERM" }), ], 64, ); const joined = lines.join("\n"); assert.match(joined, /running/); assert.match(joined, /exit 0/); assert.match(joined, /exit=3/, "非 0 退出要带退出码(done 会把这条信息吞掉)"); assert.match(joined, /killed/); assert.ok(!/done/.test(joined), "不采用草图里的 done"); }); it("time 列右对齐、内容是 formatElapsed(无空格)", () => { const lines = render([taskOf({ startedAt: NOW - 662_000, endedAt: NOW - 2_000 })], 60); const row = lines.find((line) => line.includes("bg_1"))!; assert.match(row, /11m0s/, `时长写法与 dock 一致:${JSON.stringify(row)}`); // 右对齐:`11m0s` 后面就是空格 + 收尾 │,前面可能有补位空格 assert.match(row, / +11m0s +│$/); }); }); describe("renderTaskTable:行序、上限与折叠", () => { it("最旧在上、最新在下(与草图 bg_1 → bg_2 一致)", () => { const lines = render( [ taskOf({ id: "bg_1", startedAt: NOW - 30_000 }), taskOf({ id: "bg_2", startedAt: NOW - 20_000, status: "running", endedAt: undefined }), ], 60, ); const row1 = lines.findIndex((line) => line.includes("bg_1")); const row2 = lines.findIndex((line) => line.includes("bg_2")); assert.ok(row1 > 0 && row2 > row1, `bg_1 应在 bg_2 之上:${JSON.stringify(lines)}`); }); it("默认最多 5 行任务,超出时丢最旧的并给折叠提示", () => { const tasks = Array.from({ length: 8 }, (_unused, index) => taskOf({ id: `bg_${index + 1}`, startedAt: NOW - (8 - index) * 1_000, command: `cmd-${index + 1}` }), ); const lines = render(tasks, 60); const body = lines.filter((line) => /bg_\d/.test(line)); assert.equal(body.length, MAX_TABLE_ROWS, `任务行数应被压到 ${MAX_TABLE_ROWS}:${JSON.stringify(lines)}`); assert.match(lines.join("\n"), /…3 bg tasks earlier/, `折叠提示:${JSON.stringify(lines)}`); assert.ok(!lines.join("\n").includes("bg_1 "), "最旧的 bg_1 被丢掉"); assert.ok(lines.join("\n").includes("bg_8"), "最新的 bg_8 保留"); // 折叠行在**表头之下、任务行之上**(用户 2026-10-17 定):被丢掉的正是这些可见行之前的那几个 const hintIndex = lines.findIndex((line) => line.includes("bg tasks earlier")); const firstTask = lines.findIndex((line) => /│ bg_\d/.test(line)); assert.equal(hintIndex, 3, `折叠行在表头分隔线之后:${JSON.stringify(lines)}`); assert.equal(firstTask, hintIndex + 1, `折叠行在第一条任务行之上:${JSON.stringify(lines)}`); assert.equal(lines[lines.length - 2]!.startsWith("│ bg_"), true, `末行任务行紧贴底边框(无折叠行夹在中间):${JSON.stringify(lines)}`); }); it("恰好 5 个时不折叠", () => { const tasks = Array.from({ length: 5 }, (_unused, index) => taskOf({ id: `bg_${index + 1}`, startedAt: NOW - index })); const lines = render(tasks, 60); assert.ok(!lines.join("\n").includes("bg tasks earlier"), `不该出现折叠提示:${JSON.stringify(lines)}`); }); it("maxRows 可覆盖上限(测试与将来调整用)", () => { const tasks = Array.from({ length: 6 }, (_unused, index) => taskOf({ id: `bg_${index + 1}`, startedAt: NOW - index })); const lines = render(tasks, 60, 2); assert.equal(lines.filter((line) => /bg_\d/.test(line)).length, 2); assert.match(lines.join("\n"), /…4 bg tasks earlier/); }); it("折叠行宽度与表格其余行一致", () => { const tasks = Array.from({ length: 7 }, (_unused, index) => taskOf({ id: `bg_${index + 1}`, startedAt: NOW - index })); const lines = render(tasks, 60); const widths = new Set(lines.map((line) => widthOf(line))); assert.equal(widths.size, 1, `含折叠行在内各行宽度应一致:${JSON.stringify([...widths])}`); assert.equal(lines[3]!.startsWith("│"), true, "折叠行用竖边(不是 └)"); assert.equal(lines[lines.length - 1]!.startsWith("└"), true, "末行仍是底边框"); }); it("折叠行是一条普通表格行:文案在 command 格,其余三列填 -(用户 2026-10-17 定)", () => { const tasks = Array.from({ length: 7 }, (_unused, index) => taskOf({ id: `bg_${index + 1}`, startedAt: NOW - index, command: `cmd-${index + 1}` }), ); const lines = render(tasks, 60); const hint = lines.find((line) => line.includes("bg tasks earlier"))!; // 四根竖线一根不少 —— 这是「表格结构不被破坏」的具体含义 assert.equal(hint.split("│").length - 1, 5, `折叠行要保留 4 根内部竖线:${JSON.stringify(hint)}`); assert.equal(hint.startsWith("│"), true, `折叠行以左边框开头:${JSON.stringify(hint)}`); assert.equal(hint.endsWith("│"), true, `折叠行以右边框结尾:${JSON.stringify(hint)}`); // 与表头逐列对齐:文案落在第 2 格(command),id / status / time 三格是 - const cells = hint.split("│").slice(1, -1).map((cell) => cell.trim()); const headerCells = lines.find((line) => line.startsWith("│ id"))!.split("│").slice(1, -1).map((cell) => cell.trim()); assert.deepEqual(cells, ["-", "…2 bg tasks earlier", "-", "-"], `折叠行四格:${JSON.stringify(cells)}`); // 占位 `-` 居中(与用户草图 `│ - │ … │ - │ - │` 一致),不是左对齐 for (const [index, cell] of hint.split("│").slice(1, -1).entries()) { if (index === 1) continue; // command 格是左对齐的文案 const body = cell.slice(1, -1); const left = body.length - body.trimStart().length; const right = body.length - body.trimEnd().length; assert.ok(Math.abs(left - right) <= 1 && left > 0, `第 ${index} 格的 - 应居中:${JSON.stringify(cell)}`); } assert.deepEqual(headerCells, ["id", "command", "status", "time"], "表头不变"); }); it("moreTasksHint 文案固定(英文短句,只占 command 一格)", () => { assert.equal(moreTasksHint(3), "…3 bg tasks earlier"); }); it("封顶宽度下折叠提示整句进得去,不被裁出第二个省略号(用户 2026-10-22 定)", () => { // 用户截图里的场景:5 位数 id(bg_10 / bg_11)+ 7 列的 running 把 command 挤到 25 列, // 旧文案 `…6 background tasks earlier`(27 列)被裁成 `…6 background tasks earl…` const tasks = Array.from({ length: 11 }, (_unused, index) => taskOf({ id: `bg_${index + 1}`, startedAt: NOW - (11 - index) * 1_000, status: "running", endedAt: undefined, }), ); const lines = render(tasks, 80); const hint = lines.find((line) => line.includes("bg tasks earlier"))!; assert.equal(hint.split("│")[2]!.trim(), "…6 bg tasks earlier", `折叠提示不该被再裁一刀:${JSON.stringify(hint)}`); }); }); describe("renderTaskTable:不画的条件", () => { it("没有任务 → 空数组(空表只剩表头,是噪音)", () => { assert.deepEqual(render([], 80), []); }); it("太窄 → 整张表不画(不画半截表)", () => { // 13 帧宽 + 4(id) + 6(status) + 5(time) + 10(command) = 38 是下限 assert.deepEqual(render([taskOf()], 30), []); assert.ok(render([taskOf()], 40).length > 0, "40 列应该画得出来"); }); it("宽度恰好卡在 command 下限时仍画(边界不抖)", () => { const frame = 13; const id = 4; const status = 7; // running const time = 5; const exact = frame + id + status + time + MIN_COMMAND_WIDTH; assert.ok(render([taskOf({ status: "running", endedAt: undefined })], exact).length > 0, `width=${exact} 应可画`); assert.deepEqual(render([taskOf({ status: "running", endedAt: undefined })], exact - 1), [], "少一列就不画"); }); it("width 为 0 / 负数 → 空数组", () => { assert.deepEqual(render([taskOf()], 0), []); assert.deepEqual(render([taskOf()], -5), []); }); }); describe("renderTaskTable:长命令截断", () => { it("命令放不下就裁成 …,且整行不超宽", () => { const lines = render([taskOf({ command: "x".repeat(200) })], 50); const row = lines.find((line) => line.includes("bg_1"))!; assert.ok(row.includes("…"), `应带省略号:${JSON.stringify(row)}`); assert.equal(widthOf(row), 50, `整行应与表格同宽:${JSON.stringify(row)}`); }); it("命令里的换行会被压平(registry 的 truncateCommand 口径)", () => { const lines = render([taskOf({ command: "a\nb" })], 60); const row = lines.find((line) => line.includes("bg_1"))!; assert.ok(!row.includes("\n"), "表格行不能含换行"); }); }); // ============================================================================= // 焦点任务:只列在跑的 + 当前这一个,并用 → 标出来(用户 2026-11-01 定) // ============================================================================= describe("selectTableTasks:行集", () => { it("没有 focusId 时原样全列(旧快照重放,不过滤不标记)", () => { const tasks = [ taskOf({ id: "bg_1", status: "exited", exitCode: 0 }), taskOf({ id: "bg_2", status: "running", endedAt: undefined, startedAt: NOW - 1_000 }), ]; const { rows, focus } = selectTableTasks(tasks); assert.deepEqual( rows.map((task) => task.id), ["bg_1", "bg_2"], "still lists everything, oldest first", ); assert.equal(focus, undefined, "无焦点任务"); }); it("有 focusId 时只留 running + 焦点任务本身", () => { const tasks = [ taskOf({ id: "bg_1", status: "exited", exitCode: 0 }), taskOf({ id: "bg_2", status: "running", endedAt: undefined }), taskOf({ id: "bg_3", status: "killed", signal: "SIGTERM" }), taskOf({ id: "bg_4", status: "running", endedAt: undefined, startedAt: NOW - 1_000 }), ]; // 焦点是一个在跑的任务:表里只有两个 running assert.deepEqual( selectTableTasks(tasks, "bg_4").rows.map((task) => task.id), ["bg_2", "bg_4"], "已结束的 bg_1 / bg_3 不进表", ); // 焦点是刚结束的那个:它在 running 之后补上,于是上下读是「在跑的一串 + 刚结束的那个」 const finished = selectTableTasks(tasks, "bg_3"); assert.deepEqual( finished.rows.map((task) => task.id), ["bg_2", "bg_4", "bg_3"], "刚结束的那个排在最后", ); assert.equal(finished.focus?.id, "bg_3", "焦点能取到"); }); it("行序仍是最旧在上(同刻启动按 id 兜底)", () => { const tasks = [ taskOf({ id: "bg_2", status: "running", endedAt: undefined, startedAt: NOW - 5_000 }), taskOf({ id: "bg_1", status: "running", endedAt: undefined, startedAt: NOW - 5_000 }), ]; assert.deepEqual( selectTableTasks(tasks, "bg_2").rows.map((task) => task.id), ["bg_1", "bg_2"], ); }); }); describe("renderTaskTable:焦点标记 →", () => { const running = (id: string, startedAt: number, command = "npm test") => taskOf({ id, command, status: "running", startedAt, endedAt: undefined, exitCode: undefined }); it("新建任务:只有它在跑时,表里就一行带 →", () => { const lines = render([running("bg_1", NOW - 3_000, "sleep 3 && echo hello")], 60, undefined, "bg_1"); assert.equal(lines.length, 5, `顶框 / 表头 / 分隔 / 1 行 / 底框:${JSON.stringify(lines)}`); assert.match(lines[3]!, /│\s+→ bg_1\s+│ sleep 3 && echo hello\s+│ running │ +3s │/, `焦点行:${JSON.stringify(lines[3])}`); // 焦点就是唯一的内容行 → 上方没有别的行,不该多一条分隔线(否则像第二个表头) assert.equal(lines.filter((line) => line.startsWith("├")).length, 1, `只该有表头分隔线:${JSON.stringify(lines)}`); }); it("新建任务:已有在跑的任务时它们都列,只有新建的那个带 →,且上方多一条分隔线", () => { const lines = render( [ taskOf({ id: "bg_1", command: "npm run build", status: "exited", exitCode: 0 }), running("bg_2", NOW - 9_000, "npm run build"), running("bg_3", NOW - 1_000, "sleep 3 && echo hello"), ], 60, undefined, "bg_3", ); const joined = lines.join("\n"); assert.ok(!joined.includes("bg_1"), `已结束的 bg_1 不该进表:${JSON.stringify(lines)}`); assert.match(joined, /│\s+bg_2 │ npm run build\s+│ running │ +9s │/, `在跑的那行不带标记:${JSON.stringify(lines)}`); assert.match(joined, /│ → bg_3 │ sleep 3 && echo hello\s+│ running │ +1s │/, `焦点行带标记:${JSON.stringify(lines)}`); // 草图里的形态:在跑的一行 + 分隔线 + 焦点行 assert.equal(lines.filter((line) => line.startsWith("├")).length, 2, `表头分隔线 + 焦点分隔线:${JSON.stringify(lines)}`); assert.equal(lines[lines.length - 2]!.includes("bg_3"), true, `焦点行紧贴底边框:${JSON.stringify(lines)}`); }); it("结束任务:在跑的都列,刚结束的带 → 排在最后,其它已结束的不显示", () => { const lines = render( [ taskOf({ id: "bg_1", command: "old", status: "exited", startedAt: NOW - 90_000, endedAt: NOW - 80_000, exitCode: 0 }), running("bg_2", NOW - 9_000, "npm run build"), taskOf({ id: "bg_3", command: "echo done", status: "exited", startedAt: NOW - 5_000, endedAt: NOW - 1_000, exitCode: 0 }), running("bg_4", NOW - 2_000, "sleep 3 && echo hello"), ], 60, undefined, "bg_3", ); const joined = lines.join("\n"); assert.ok(!joined.includes(" old "), `旧终态任务不该进表:${JSON.stringify(lines)}`); assert.match(joined, /│\s+bg_2 │ npm run build\s+│ running │ +9s │/); assert.match(joined, /│\s+bg_4 │ sleep 3 && echo hello\s+│ running │ +2s │/); assert.match(joined, /│ → bg_3 │ echo done\s+│ exit 0\s+│ +4s │/, `刚结束的那行带标记:${JSON.stringify(lines)}`); // 顺序:在跑的在上、刚结束的在下 const order = ["bg_2", "bg_4", "bg_3"].map((id) => lines.findIndex((line) => line.includes(id))); assert.deepEqual(order, [...order].sort((a, b) => a - b), `顺序应为 bg_2 / bg_4 / bg_3:${JSON.stringify(lines)}`); }); it("全部已结束 + 焦点是其中一个时,表里只剩那一行", () => { const lines = render( [ taskOf({ id: "bg_1", status: "exited", exitCode: 0 }), taskOf({ id: "bg_2", status: "killed", signal: "SIGTERM" }), ], 60, undefined, "bg_2", ); assert.equal(lines.length, 5, `只剩一行任务:${JSON.stringify(lines)}`); assert.match(lines[3]!, /│ → bg_2 /, `只剩焦点行:${JSON.stringify(lines[3])}`); }); it("焦点 id 不在快照里时不加 gutter(每行不留白 2 列)", () => { const lines = render([running("bg_1", NOW - 3_000)], 60, undefined, "bg_9"); assert.match(lines[3]!, /│ bg_1\s+│/, `无标记时 id 顶格:${JSON.stringify(lines[3])}`); assert.equal(lines.filter((line) => line.startsWith("├")).length, 1, "不该有焦点分隔线"); }); it("标记占的宽度进 id 列:带 → 与不带 → 的行 id 左边缘对齐", () => { const lines = render( [running("bg_1", NOW - 9_000, "a"), running("bg_12", NOW - 1_000, "b")], 60, undefined, "bg_12", ); const rows = lines.filter((line) => /bg_\d+/.test(line)); const offsets = rows.map((line) => line.indexOf("bg_")); assert.equal(new Set(offsets).size, 1, `id 左边缘应对齐:${JSON.stringify(rows)}`); }); it("焦点行永远留在表里(超出上限时它占最后一个槽位)", () => { const runningTasks = Array.from({ length: 7 }, (_unused, index) => running(`bg_${index + 1}`, NOW - (20 - index) * 1_000, `cmd-${index + 1}`), ); const focus = taskOf({ id: "bg_9", command: "just finished", status: "exited", startedAt: NOW - 500, endedAt: NOW - 100, exitCode: 0 }); const lines = render([...runningTasks, focus], 60, undefined, "bg_9"); const taskRows = lines.filter((line) => /bg_\d/.test(line)); assert.equal(taskRows.length, MAX_TABLE_ROWS, `仍是最多 5 行任务:${JSON.stringify(lines)}`); assert.ok(lines.join("\n").includes("bg_9"), `焦点行不该被挤掉:${JSON.stringify(lines)}`); assert.equal(lines[lines.length - 2]!.includes("bg_9"), true, `焦点行紧贴底边框:${JSON.stringify(lines)}`); assert.match(lines.join("\n"), /…3 bg tasks earlier/, `被略掉的在跑任务仍要报数:${JSON.stringify(lines)}`); }); it("有焦点时行宽仍严格一致(gutter 不会把表格拉歪)", () => { const lines = render( [running("bg_1", NOW - 9_000), taskOf({ id: "bg_2", status: "exited", exitCode: 0 })], 60, undefined, "bg_2", ); assert.equal(new Set(lines.map((line) => widthOf(line))).size, 1, `各行等宽:${JSON.stringify(lines)}`); }); it("表头 `id` 与任务行的 id 同列对齐(不因 gutter 左移两列,用户 2026-11-01 定)", () => { const lines = render( [running("bg_1", NOW - 9_000), running("bg_2", NOW - 2_000)], 60, undefined, "bg_2", ); const header = lines.find((line) => line.includes(" id "))!; const taskRows = lines.filter((line) => /bg_\d+/.test(line)); assert.ok(header && taskRows.length === 2); // 各行的 id(表头是 `id`)落列相同:表头 `id` 的 i 就在 `bg_` 的 b 上方 const headerOffset = header.indexOf(" id ") + 1; for (const row of taskRows) assert.equal(row.indexOf("bg_"), headerOffset, `表头与任务行 id 应同列:${JSON.stringify(lines)}`); // 无焦点(旧快照)时不需要退:表头仍顶格 const plain_ = render([running("bg_1", NOW - 9_000)], 60); assert.equal(plain_.find((line) => line.includes(" id "))!.indexOf(" id ") + 1, plain_.find((line) => line.includes("bg_1"))!.indexOf("bg_")); }); });