| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166 |
- import { DatabaseSync } from "node:sqlite";
- import { join } from "node:path";
- import { mkdir, writeFile } from "node:fs/promises";
- import { createToolSearchCliFixture } from "#e2e/helpers/toolsearch-cli-process";
- import {
- ensureToolSearchTuiBuild,
- startToolSearchTerminal,
- } from "#e2e/helpers/toolsearch-terminal";
- import {
- TOOLSEARCH_NOTICE,
- TOOLSEARCH_DONE,
- toolSearchResponseStage,
- type ToolSearchRequest,
- } from "#e2e/helpers/toolsearch-provider";
- // 修复原因:wdio 的 Mocha 单 case 上限固定 120s,用例内 this.timeout 不生效(实测 this.timeout(300s) 仍在 120s 被截断)。
- // 本例要启动 3 次真实 TUI,Windows 上每次冷启动 20-80s(tsx 编译 TUI 源码约 10s + 启动数据加载约 10s,
- // 负载高时翻倍),三轮串在一个 it 里必然超过 120s。拆成共享同一 fixture/会话、顺序执行的三个 it,
- // 每轮独占一份 120s 预算,断言与原用例一致。
- describe("ToolSearch TUI 真实终端", () => {
- let fixture: Awaited<ReturnType<typeof createToolSearchCliFixture>>;
- let terminal: ReturnType<typeof startToolSearchTerminal> | undefined;
- let sessionId: string;
- const typeAndPaint = async (keys: string) => {
- const before = terminal!.frameCount();
- terminal!.write(keys);
- await browser.waitUntil(async () => terminal!.frameCount() > before, {
- timeout: 10_000,
- timeoutMsg: "TUI 按键后没有绘制新帧",
- });
- };
- const submit = async (value: string) => {
- // 动画也会触发重绘;仅等帧数会在输入状态尚未提交时按 Enter,导致旧文本回写。
- // 使用终端粘贴协议,并等待本次输入实际绘制,保持输入与提交的真实顺序。
- const before = terminal!.output().length;
- terminal!.write(`\x1b[200~${value}\x1b[201~`);
- await browser.waitUntil(async () => terminal!.output().slice(before).includes(value), {
- timeout: 10_000,
- timeoutMsg: "TUI 本次输入尚未绘制",
- });
- terminal!.write("\r");
- };
- const turn = async (args: string[], marker: string) => {
- const before = fixture.requests.length;
- const completed = `${TOOLSEARCH_DONE}_${marker}`;
- fixture.setResponse((body, response) => {
- const stage = toolSearchResponseStage(body);
- fixture.reply(
- body,
- response,
- stage === "done" ? `done-${marker.split("_").at(-1)!.toLowerCase()}` : stage,
- );
- });
- terminal = startToolSearchTerminal(fixture, args);
- await browser.waitUntil(async () => terminal!.ready(), {
- // 修复原因:全新 HOME 冷启动 TUI 实测 20-80s(见文件头说明),原 30s 不够。
- timeout: 100_000,
- timeoutMsg: "TUI 未完成首帧终端绘制",
- });
- const beforeModel = terminal.output().length;
- await submit("/model toolsearch-process/test-toolsearch");
- await browser.waitUntil(
- async () => {
- const rendered = terminal!.output().slice(beforeModel);
- // 终端增量绘制只会重画变化后缀;完整占位文字不一定再次出现在 PTY 流中。
- return rendered.includes("Model switched to toolsearch-process/test-toolsearch");
- },
- {
- timeout: 15_000,
- timeoutMsg: "TUI 未切换到隔离的普通 Anthropic provider",
- },
- );
- const beforeEffort = terminal.output().length;
- await submit("/effort disabled");
- await browser.waitUntil(
- async () => {
- const rendered = terminal!.output().slice(beforeEffort);
- return rendered.includes("Reasoning effort switched to disabled");
- },
- { timeout: 15_000, timeoutMsg: "TUI 未确认测试模型的思考档位" },
- );
- await submit(marker);
- await browser.waitUntil(async () => terminal!.output().includes(completed), {
- timeout: 30_000,
- timeoutMsg: "TUI 没有显示模型完成文本",
- });
- // 增量重绘只输出变化的字符,原始 PTY 文本不保证含完整退出提示;等新帧后确认退出。
- await typeAndPaint("\x03");
- terminal.write("\x03");
- expect(await terminal.done).toBe(0);
- await terminal.close();
- terminal = undefined;
- return fixture.requests
- .slice(before)
- .filter(({ body }) => body.tools?.length)
- .map(({ body }) => body);
- };
- const loaded = (body: ToolSearchRequest) => {
- expect(body.tools?.find((tool) => tool.name === "TodoRead")?.defer_loading).toBe(true);
- expect(JSON.stringify(body.messages)).toContain(
- '"type":"tool_reference","tool_name":"TodoRead"',
- );
- expect(JSON.stringify(body.messages).split(TOOLSEARCH_NOTICE)).toHaveLength(2);
- };
- // TUI 构建单独占一个 hook,不挤占 fixture 准备和首轮 TUI 冷启动的 120s 预算。
- before(async () => {
- await ensureToolSearchTuiBuild();
- });
- before(async () => {
- fixture = await createToolSearchCliFixture("conversation-session-toolsearch-tui");
- // TUI 现有登录门槛要求一个有效计划连接;请求仍选择隔离的普通测试 Provider。
- // 只在隔离 HOME 中写虚构身份,不读取或复用本机登录凭据。
- await mkdir(join(fixture.root, ".zcode", "v2"), { recursive: true });
- await writeFile(
- join(fixture.root, ".zcode", "v2", "credentials.json"),
- JSON.stringify({
- "account-provider:account:zai-individual-coding-plan:identity": "toolsearch-e2e",
- "account-provider:coding-plan:account:zai-individual-coding-plan:account:toolsearch-e2e:api-key":
- "test-only",
- }),
- );
- });
- afterEach(async () => {
- await terminal?.close();
- terminal = undefined;
- });
- after(async () => {
- await fixture?.close();
- });
- it("TS02-TUI: 强开搜索首轮,工具延迟加载并写入真实引用", async () => {
- const initial = await turn(["--toolsearch-mode", "native"], "E2E_TOOLSEARCH_INITIAL");
- expect(initial).toHaveLength(3);
- expect(initial[0]!.tools?.some((tool) => tool.name === "TodoRead")).toBe(false);
- loaded(initial[1]!);
- const db = new DatabaseSync(join(fixture.root, "session.sqlite"), { readOnly: true });
- try {
- sessionId = (
- db.prepare("SELECT id FROM session ORDER BY time_created DESC LIMIT 1").get() as {
- id: string;
- }
- ).id;
- } finally {
- db.close();
- }
- });
- it("TS02-TUI: resume 不带 flag 默认关闭并降级", async () => {
- const off = await turn(["--resume", sessionId], "E2E_TOOLSEARCH_OFF");
- expect(off).toHaveLength(1);
- expect(off[0]!.tools?.some((tool) => tool.name === "TodoRead")).toBe(true);
- expect(off[0]!.tools?.some((tool) => tool.name === "ToolSearch" || tool.defer_loading)).toBe(
- false,
- );
- expect(JSON.stringify(off[0]!.messages)).not.toContain("tool_reference");
- expect(JSON.stringify(off[0]!.messages)).not.toContain(TOOLSEARCH_NOTICE);
- });
- it("TS02-TUI: continue 再开启并恢复真实引用", async () => {
- const on = await turn(["--toolsearch-mode", "native", "--continue"], "E2E_TOOLSEARCH_ON");
- expect(on).toHaveLength(1);
- loaded(on[0]!);
- });
- });
|