import assert from "node:assert/strict";
import { test } from "node:test";
import {
MARK_BLANK,
MARK_CELL,
MARK_COLS,
MARK_GLYPH_WIDTH,
MARK_INDENT,
MARK_ROWS,
MARK_WIDTH,
POWERED_BY_SENTENCE,
attachSideText,
composeHeaderLines,
formatTitleLine,
markCellFilled,
markLines,
poweredByLines,
shortenPath,
} from "./logo.ts";
/** 去掉 ANSI,只看几何。 */
const plain = (line: string): string => line.replace(/\u001b\[[0-9;]*m/g, "");
/** 恒等主题:剥掉 ANSI 后就是纯文本。 */
const identityTheme = {
fg: (_color: string, text: string) => text,
bold: (text: string) => text,
};
test("印记是 4×4 格子,本体宽 12、左侧留白一格所以整行 13", () => {
assert.equal(MARK_CELL, "███");
assert.equal(MARK_BLANK, " ");
assert.equal(MARK_INDENT, " ");
assert.equal(MARK_ROWS, 4);
assert.equal(MARK_COLS, 4);
assert.equal(MARK_GLYPH_WIDTH, 12);
assert.equal(MARK_WIDTH, MARK_INDENT.length + MARK_GLYPH_WIDTH);
assert.equal(MARK_WIDTH, 13);
});
test("markLines 的四行形状(含补回来的那一格)与左侧留白", () => {
assert.deepEqual(markLines(), [
" █████████ ", // 上横:三格实心
" ███ ███ ", // 左竖 + 右腿上身
" ██████ ███", // 左竖 + 内衬 + 右腿(上游落定帧漏掉的 (5,5))
" ███ ███", // 左竖 + 右腿脚
]);
for (const line of markLines()) {
assert.ok(line.startsWith(MARK_INDENT), "每行都要顶一个空格,不能贴左边缘");
assert.equal(line.length, MARK_WIDTH, "每行可见宽度必须一致,否则侧栏歪");
}
});
test("markCellFilled 与格子表一致,越界当空格", () => {
markLines().forEach((line, row) => {
for (let col = 0; col < MARK_COLS; col++) {
assert.equal(
markCellFilled(row, col),
line.slice(MARK_INDENT.length + col * 3, MARK_INDENT.length + col * 3 + 3) === MARK_CELL,
`(${row},${col}) 取色与整行渲染不一致`,
);
}
});
assert.equal(markCellFilled(9, 9), false);
assert.equal(markCellFilled(-1, 0), false);
});
test("右腿不是悬空的一只脚(回归:上游 phase 6 少了 (2,3) 那格)", () => {
// 最后一行最右格实心 → 它上一行最右格也必须实心,否则腿断了
assert.equal(markCellFilled(MARK_ROWS - 1, MARK_COLS - 1), true);
assert.equal(markCellFilled(MARK_ROWS - 2, MARK_COLS - 1), true);
});
test("attachSideText:两条侧栏落在第 1、2 行,其余行原样等宽", () => {
const out = attachSideText(markLines().map((l) => `[${l}]`), ["V", "C"]);
assert.equal(out.length, MARK_ROWS);
assert.equal(out[1], `[${markLines()[1]}] V`);
assert.equal(out[2], `[${markLines()[2]}] C`);
assert.equal(out[0], `[${markLines()[0]}]`);
assert.equal(out[3], `[${markLines()[3]}]`);
});
test("attachSideText:ANSI 不参与列计算", () => {
const colored = markLines().map((l) => `\x1b[38;2;121;214;193m${l}\x1b[39m`);
const out = attachSideText(colored, ["A", "B"]);
// 挂侧栏的行:剥掉 ANSI 后,侧栏前正好是整行宽度 + 2 空格
for (const [i, text] of [
[1, "A"],
[2, "B"],
] as const) {
const stripped = plain(out[i]!);
assert.equal(stripped, `${markLines()[i]} ${text}`);
assert.equal(stripped.indexOf(text), MARK_WIDTH + 2);
}
// 没挂侧栏的行仍是完整等宽
assert.equal(plain(out[0]!), " █████████ ");
assert.equal(plain(out[0]!).length, MARK_WIDTH);
});
test("attachSideText:侧栏为空 / 比行数多 / 行数为 0 都不炸", () => {
const lines = markLines();
assert.deepEqual(attachSideText(lines, []), [...lines]);
assert.equal(attachSideText(lines, ["a", "b", "c", "d", "e"]).length, MARK_ROWS);
assert.deepEqual(attachSideText([], ["a"]), []);
// 空字符串那条不占位:只有一行被贴字,其余保持等宽
const blanked = attachSideText(lines, ["", "only"]);
assert.equal(blanked.filter((l) => l.endsWith("only")).length, 1);
assert.equal(blanked.filter((l) => l.endsWith("only") && l !== blanked[blanked.findIndex((x) => x.endsWith("only"))]).length, 0);
});
test("每行可见宽度恒等于 MARK_WIDTH(挂不挂侧栏的行都等宽,侧栏列才不会歪)", () => {
for (const line of markLines()) assert.equal(plain(line).length, MARK_WIDTH);
for (const line of markLines((f) => (f ? `\x1b[31m${MARK_CELL}\x1b[39m` : MARK_BLANK))) {
assert.equal(plain(line).length, MARK_WIDTH);
}
});
test("composeHeaderLines:印记下方留一个空行,提示行与说明段之间也留一个空行(两段都缩进一格)", () => {
// 印记段是「大 logo」,它下面那条空行必须保留(用户 2026-10-05 定);提示行与说明段
// 之间那条 2026-10-05 曾去掉,2026-11-01 用户又要求加回来 —— 两处空行不是同一条规则。
assert.deepEqual(composeHeaderLines({ logo: ["a", "b"], hints: "h", onboarding: ["o"] }), ["a", "b", "", " h", "", " o"]);
assert.deepEqual(composeHeaderLines({ logo: ["a"], hints: "h" }), ["a", "", " h"]);
assert.deepEqual(composeHeaderLines({ logo: ["a"], onboarding: ["o"] }), ["a", "", " o"]);
// 没有印记段就没有那条空行(窄终端回退时印记段仍在,这条钉的是「不凭空留行」);
// 提示行与说明段之间的空行只看这两段是否都在场,与印记段无关。
assert.deepEqual(composeHeaderLines({ logo: [], hints: "h", onboarding: ["o"] }), [" h", "", " o"]);
assert.deepEqual(composeHeaderLines({ logo: [] }), []);
assert.deepEqual(composeHeaderLines({ logo: [], onboarding: ["o"] }), [" o"]);
// 一句话说清:整份 header 里恰好两个空行 —— 印记下方一条、提示行与说明段之间一条
const all = composeHeaderLines({ logo: markLines(), hints: "h", onboarding: ["o"] });
assert.equal(all.filter((line) => line === "").length, 2, "空行该有两条:印记下方 + 提示行与说明段之间");
assert.equal(all[MARK_ROWS], "", "第一条空行紧跟在印记最后一行之后");
assert.equal(all[MARK_ROWS + 2], "", "第二条空行在提示行与说明段之间");
for (const line of all) {
if (line === "") continue; // 空行本身就是空的,不适用「不顶格」
assert.ok(line.startsWith(MARK_INDENT), `除了空行,每一行都不能顶格:${JSON.stringify(line)}`);
}
});
test("composeHeaderLines:`leading` 在印记之前补一条空行(静音 pi 内置 header 时用)", () => {
// 用户 2026-10-22 定:pi 装内置 header 时自带 `Spacer(1) + header + Spacer(1)`,被静音后
// 那条 spacer 也没了,印记会直接贴终端顶边。这条空行由调用方按「本进程是否真静音」传入。
const withLeading = composeHeaderLines({ logo: ["a", "b"], hints: "h", onboarding: ["o"], leading: true });
assert.deepEqual(withLeading, ["", "a", "b", "", " h", "", " o"]);
assert.equal(withLeading[0], "", "顶部空行必须在第一行");
assert.equal(withLeading[1], "a", "顶部空行只一条,紧跟就是印记第一行");
// 其余两条不受影响:三条空行各管一处,不是同一条规则。
assert.equal(withLeading.filter((line) => line === "").length, 3);
// 不传就是原样(pi 自己那条 spacer 还在时,不要多补一条)。
assert.deepEqual(composeHeaderLines({ logo: ["a"], hints: "h" }), ["a", "", " h"]);
assert.deepEqual(composeHeaderLines({ logo: ["a"], hints: "h", leading: false }), ["a", "", " h"]);
// 窄终端回退(只剩 wordmark 一行)也要补;空 logo 加 `leading` 也照样补,不凭空吞掉。
assert.deepEqual(composeHeaderLines({ logo: ["w"], leading: true }), ["", "w"]);
assert.deepEqual(composeHeaderLines({ logo: [], leading: true }), [""]);
});
test("poweredByLines:说明段只剩一行普通字符的文案,方块字形一个都不留", () => {
const lines = poweredByLines(identityTheme).map(plain);
assert.deepEqual(lines, ["This pi harness is powered by latest @bachi/pi-coder"]);
assert.equal(lines[0], POWERED_BY_SENTENCE, "导出的整句常量必须与实际渲染一致");
for (const line of lines) {
assert.equal(/[╭╰╯╴╶─]/.test(line), false, `方块字形一个都不能留:${JSON.stringify(line)}`);
}
assert.equal(lines[0]!.endsWith("."), false, "用户给的文案没有尾部句点");
});
test("poweredByLines:`@bachi/pi-coder` 加粗走前景色,句子其余部分仍走 dim", () => {
const slots: string[] = [];
const marked = {
fg: (color: string, text: string) => {
slots.push(color);
return `<${color}>${text}>`;
},
bold: (text: string) => `${text}`,
};
const lines = poweredByLines(marked);
assert.equal(lines.length, 1);
// 前缀一个 `dim` 段,包名一个 `text`(前景色)段并整体加粗 —— 加粗包在色段外面,
// 与 `formatTitleLine` 里 `bold(fg("accent", …))` 的写法同构。
assert.equal(lines[0], "This pi harness is powered by latest >@bachi/pi-coder>");
assert.deepEqual(slots, ["dim", "text"], "只该用到 dim 与前景色两个槽");
for (const line of lines) {
assert.equal(line.includes("accent"), false, `说明段不许用 accent:${JSON.stringify(line)}`);
}
});
test("formatTitleLine:版本号后面跟模型与推理档位", () => {
assert.equal(
formatTitleLine(identityTheme, "0.99.2", "deepseek-flash-qd", "max"),
"pi v0.99.2 (deepseek-flash-qd with max effort)",
);
// 档位读不到(非推理模型 / stale ctx)时只剩模型
assert.equal(formatTitleLine(identityTheme, "0.99.2", "deepseek-flash-qd", undefined), "pi v0.99.2 (deepseek-flash-qd)");
// 模型读不到时整个括号都不出现
assert.equal(formatTitleLine(identityTheme, "0.99.2", undefined, "max"), "pi v0.99.2");
assert.equal(formatTitleLine(identityTheme, "0.99.2", undefined, undefined), "pi v0.99.2");
});
test("formatTitleLine:painted 主题下模型 / 档位各走自己的色槽(与 statusline 的模型段同色)", () => {
const slots: string[] = [];
const painted = {
fg: (color: string, text: string) => {
slots.push(color);
return `\u001b[38;5;1m${text}\u001b[39m`;
},
bold: (text: string) => text,
};
const line = formatTitleLine(painted, "0.99.2", "qwen3.8-max", "xhigh");
assert.ok(slots.includes("accent"), "模型 id 用 accent");
assert.ok(slots.includes("syntaxFunction"), "档位用 syntaxFunction");
// 剥掉 ANSI 后坐标不变:每一截都仍按顺序拼在一起
assert.equal(
plain(line),
"pi v0.99.2 (qwen3.8-max with xhigh effort)",
);
});
test("shortenPath:家目录内缩写,外面原样", () => {
assert.equal(shortenPath("/Users/bachi", "/Users/bachi"), "~");
assert.equal(shortenPath("/Users/bachi/jaylli/litellm-any", "/Users/bachi"), "~/jaylli/litellm-any");
assert.equal(shortenPath("/tmp/x", "/Users/bachi"), "/tmp/x");
// 同前缀但不同目录(/Users/bachix)不能被当成子路径缩写
assert.equal(shortenPath("/Users/bachix", "/Users/bachi"), "/Users/bachix");
assert.equal(shortenPath("/srv/app", undefined), "/srv/app");
});