conversation-session-toolsearch-tui.test.ts 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. import { DatabaseSync } from "node:sqlite";
  2. import { join } from "node:path";
  3. import { mkdir, writeFile } from "node:fs/promises";
  4. import { createToolSearchCliFixture } from "#e2e/helpers/toolsearch-cli-process";
  5. import { startToolSearchTerminal } from "#e2e/helpers/toolsearch-terminal";
  6. import {
  7. TOOLSEARCH_NOTICE,
  8. TOOLSEARCH_DONE,
  9. toolSearchResponseStage,
  10. type ToolSearchRequest,
  11. } from "#e2e/helpers/toolsearch-provider";
  12. describe("ToolSearch TUI 真实终端", () => {
  13. it("TS02-TUI: 强开搜索、resume 默认关闭、continue 再开启恢复真实引用", async function () {
  14. this.timeout(180_000);
  15. const fixture = await createToolSearchCliFixture("conversation-session-toolsearch-tui");
  16. let terminal: ReturnType<typeof startToolSearchTerminal> | undefined;
  17. const typeAndPaint = async (keys: string) => {
  18. const before = terminal!.frameCount();
  19. terminal!.write(keys);
  20. await browser.waitUntil(async () => terminal!.frameCount() > before, {
  21. timeout: 10_000,
  22. timeoutMsg: "TUI 按键后没有绘制新帧",
  23. });
  24. };
  25. const submit = async (value: string) => {
  26. // 动画也会触发重绘;仅等帧数会在输入状态尚未提交时按 Enter,导致旧文本回写。
  27. // 使用终端粘贴协议,并等待本次输入实际绘制,保持输入与提交的真实顺序。
  28. const before = terminal!.output().length;
  29. terminal!.write(`\x1b[200~${value}\x1b[201~`);
  30. await browser.waitUntil(async () => terminal!.output().slice(before).includes(value), {
  31. timeout: 10_000,
  32. timeoutMsg: "TUI 本次输入尚未绘制",
  33. });
  34. terminal!.write("\r");
  35. };
  36. const turn = async (args: string[], marker: string) => {
  37. const before = fixture.requests.length;
  38. const completed = `${TOOLSEARCH_DONE}_${marker}`;
  39. fixture.setResponse((body, response) => {
  40. const stage = toolSearchResponseStage(body);
  41. fixture.reply(
  42. body,
  43. response,
  44. stage === "done" ? `done-${marker.split("_").at(-1)!.toLowerCase()}` : stage,
  45. );
  46. });
  47. terminal = startToolSearchTerminal(fixture, args);
  48. await browser.waitUntil(async () => terminal!.ready(), {
  49. timeout: 30_000,
  50. timeoutMsg: "TUI 未完成首帧终端绘制",
  51. });
  52. const beforeModel = terminal.output().length;
  53. await submit("/model toolsearch-process/test-toolsearch");
  54. await browser.waitUntil(
  55. async () => {
  56. const rendered = terminal!.output().slice(beforeModel);
  57. // 终端增量绘制只会重画变化后缀;完整占位文字不一定再次出现在 PTY 流中。
  58. return rendered.includes("Model switched to toolsearch-process/test-toolsearch");
  59. },
  60. {
  61. timeout: 15_000,
  62. timeoutMsg: "TUI 未切换到隔离的普通 Anthropic provider",
  63. },
  64. );
  65. const beforeEffort = terminal.output().length;
  66. await submit("/effort disabled");
  67. await browser.waitUntil(
  68. async () => {
  69. const rendered = terminal!.output().slice(beforeEffort);
  70. return rendered.includes("Reasoning effort switched to disabled");
  71. },
  72. { timeout: 15_000, timeoutMsg: "TUI 未确认测试模型的思考档位" },
  73. );
  74. await submit(marker);
  75. await browser.waitUntil(async () => terminal!.output().includes(completed), {
  76. timeout: 30_000,
  77. timeoutMsg: "TUI 没有显示模型完成文本",
  78. });
  79. // 增量重绘只输出变化的字符,原始 PTY 文本不保证含完整退出提示;等新帧后确认退出。
  80. await typeAndPaint("\x03");
  81. terminal.write("\x03");
  82. expect(await terminal.done).toBe(0);
  83. await terminal.close();
  84. terminal = undefined;
  85. return fixture.requests
  86. .slice(before)
  87. .filter(({ body }) => body.tools?.length)
  88. .map(({ body }) => body);
  89. };
  90. const loaded = (body: ToolSearchRequest) => {
  91. expect(body.tools?.find((tool) => tool.name === "TodoRead")?.defer_loading).toBe(true);
  92. expect(JSON.stringify(body.messages)).toContain(
  93. '"type":"tool_reference","tool_name":"TodoRead"',
  94. );
  95. expect(JSON.stringify(body.messages).split(TOOLSEARCH_NOTICE)).toHaveLength(2);
  96. };
  97. try {
  98. // TUI 现有登录门槛要求一个有效计划连接;请求仍选择隔离的普通测试 Provider。
  99. // 只在隔离 HOME 中写虚构身份,不读取或复用本机登录凭据。
  100. await mkdir(join(fixture.root, ".zcode", "v2"), { recursive: true });
  101. await writeFile(
  102. join(fixture.root, ".zcode", "v2", "credentials.json"),
  103. JSON.stringify({
  104. "account-provider:account:zai-individual-coding-plan:identity": "toolsearch-e2e",
  105. "account-provider:coding-plan:account:zai-individual-coding-plan:account:toolsearch-e2e:api-key":
  106. "test-only",
  107. }),
  108. );
  109. const initial = await turn(["--toolsearch-mode", "native"], "E2E_TOOLSEARCH_INITIAL");
  110. expect(initial).toHaveLength(3);
  111. expect(initial[0]!.tools?.some((tool) => tool.name === "TodoRead")).toBe(false);
  112. loaded(initial[1]!);
  113. const db = new DatabaseSync(join(fixture.root, "session.sqlite"), { readOnly: true });
  114. let sessionId: string;
  115. try {
  116. sessionId = (
  117. db.prepare("SELECT id FROM session ORDER BY time_created DESC LIMIT 1").get() as {
  118. id: string;
  119. }
  120. ).id;
  121. } finally {
  122. db.close();
  123. }
  124. const off = await turn(["--resume", sessionId], "E2E_TOOLSEARCH_OFF");
  125. expect(off).toHaveLength(1);
  126. expect(off[0]!.tools?.some((tool) => tool.name === "TodoRead")).toBe(true);
  127. expect(off[0]!.tools?.some((tool) => tool.name === "ToolSearch" || tool.defer_loading)).toBe(
  128. false,
  129. );
  130. expect(JSON.stringify(off[0]!.messages)).not.toContain("tool_reference");
  131. expect(JSON.stringify(off[0]!.messages)).not.toContain(TOOLSEARCH_NOTICE);
  132. const on = await turn(["--toolsearch-mode", "native", "--continue"], "E2E_TOOLSEARCH_ON");
  133. expect(on).toHaveLength(1);
  134. loaded(on[0]!);
  135. } finally {
  136. await terminal?.close();
  137. await fixture.close();
  138. }
  139. });
  140. });