conversation-session-toolsearch-catalog.test.ts 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. import type { ServerResponse } from "node:http";
  2. import { createToolSearchCliFixture } from "../helpers/toolsearch-cli-process.js";
  3. import { startToolSearchRuntimeDriver } from "../helpers/toolsearch-runtime-driver.js";
  4. import {
  5. isToolSearchCompact,
  6. TOOLSEARCH_NOTICE,
  7. type ToolSearchRequest,
  8. } from "../helpers/toolsearch-provider.js";
  9. type Delta = { addedNames: string[]; removedNames: string[]; readdedNames: string[] };
  10. type Entry = { metadata?: { toolSearchAnnouncement?: Delta }; message?: unknown };
  11. const text = (body: ToolSearchRequest) =>
  12. body.messages
  13. .flatMap((message) =>
  14. typeof message.content === "string"
  15. ? [message.content]
  16. : message.content.flatMap((block) => (block.type === "text" ? [block.text ?? ""] : [])),
  17. )
  18. .join("\n");
  19. const deltas = (history: unknown) =>
  20. (history as Entry[]).flatMap((entry) =>
  21. entry.metadata?.toolSearchAnnouncement ? [entry.metadata.toolSearchAnnouncement] : [],
  22. );
  23. function reply(
  24. fixture: Awaited<ReturnType<typeof createToolSearchCliFixture>>,
  25. response: ServerResponse,
  26. body: ToolSearchRequest,
  27. stage = "catalog-done",
  28. ) {
  29. fixture.reply(body, response, `${stage}-${body.stream ? "stream" : "json"}`);
  30. }
  31. // 目录由隔离 driver 注入;HTTP adapter、Runtime、SQLite 和 compact 均是真实实现。
  32. // 与 CLI/TUI 产品 E2E 分开计数,不把此文件声称为 MCP 热更新用户路径。
  33. describe("ToolSearch 动态目录(Runtime / HTTP 集成)", () => {
  34. for (const streaming of ["on", "off"] as const) {
  35. for (const boundary of ["input", "tool batch"] as const) {
  36. it(`TS09: Continue 中目录变化等到 ${boundary} 才公告,streaming=${streaming}`, async function () {
  37. this.timeout(120_000);
  38. const fixture = await createToolSearchCliFixture("conversation-session-toolsearch-catalog");
  39. const driver = await startToolSearchRuntimeDriver(fixture, streaming);
  40. try {
  41. await driver.catalog("TodoWrite", "absent");
  42. let mainCalls = 0;
  43. fixture.setResponse((body, response) => {
  44. void (async () => {
  45. mainCalls++;
  46. if (mainCalls === 1) {
  47. await driver.catalog("TodoWrite", "deferred");
  48. reply(fixture, response, body, "catalog-partial");
  49. } else if (mainCalls === 2 && boundary === "tool batch") {
  50. reply(fixture, response, body, "catalog-search");
  51. } else reply(fixture, response, body);
  52. })().catch((error) => response.destroy(error as Error));
  53. });
  54. await driver.turn("E2E_TOOLSEARCH_CATALOG_CONTINUE");
  55. if (boundary === "input") await driver.turn("E2E_TOOLSEARCH_CATALOG_NEXT");
  56. expect(fixture.requests).toHaveLength(3);
  57. const second = fixture.requests[1]!.body;
  58. expect(text(second)).not.toContain("TodoWrite");
  59. expect(text(second).split(TOOLSEARCH_NOTICE)).toHaveLength(2);
  60. const third = fixture.requests[2]!.body;
  61. expect(
  62. text(third)
  63. .split("\n")
  64. .filter((line) => line === "TodoWrite"),
  65. ).toHaveLength(1);
  66. expect(deltas(await driver.history())).toEqual([
  67. { addedNames: ["WebFetch"], removedNames: [], readdedNames: [] },
  68. { addedNames: ["TodoWrite"], removedNames: [], readdedNames: [] },
  69. ]);
  70. await driver.turn("E2E_TOOLSEARCH_CATALOG_STABLE");
  71. expect(deltas(await driver.history())).toHaveLength(2);
  72. if (boundary === "tool batch")
  73. expect(third.tools?.find((tool) => tool.name === "TodoWrite")?.defer_loading).toBe(
  74. true,
  75. );
  76. } finally {
  77. await driver.close();
  78. await fixture.close();
  79. }
  80. });
  81. }
  82. it(`TS04: 增量尾部连续 reactive compact 补全名称且不重复,streaming=${streaming}`, async function () {
  83. this.timeout(120_000);
  84. const fixture = await createToolSearchCliFixture("conversation-session-toolsearch-catalog");
  85. const driver = await startToolSearchRuntimeDriver(fixture, streaming);
  86. try {
  87. await driver.catalog("TodoWrite", "absent");
  88. let mainCalls = 0;
  89. let summaries = 0;
  90. fixture.setResponse((body, response) => {
  91. if (isToolSearchCompact(body)) {
  92. summaries++;
  93. reply(fixture, response, body, "catalog-summary");
  94. } else if ([3, 5].includes(++mainCalls)) {
  95. fixture.reply(body, response, "context-error");
  96. } else reply(fixture, response, body);
  97. });
  98. await driver.turn("E2E_TOOLSEARCH_CATALOG_SEED_ONE");
  99. await driver.turn("E2E_TOOLSEARCH_CATALOG_SEED_TWO");
  100. await driver.catalog("TodoWrite", "deferred");
  101. await driver.turn("E2E_TOOLSEARCH_CATALOG_EXPAND");
  102. expect(summaries).toBe(1);
  103. expect(deltas(await driver.history())).toEqual([
  104. { addedNames: ["TodoWrite"], removedNames: [], readdedNames: [] },
  105. { addedNames: ["WebFetch"], removedNames: [], readdedNames: [] },
  106. ]);
  107. for (let pass = 0; pass < 2; pass++) {
  108. const last = fixture.requests.at(-1)!.body;
  109. expect(text(last)).toContain("E2E_TOOLSEARCH_CATALOG_SUMMARY");
  110. for (const name of ["WebFetch", "TodoWrite"]) {
  111. expect(
  112. text(last)
  113. .split("\n")
  114. .filter((line) => line === name),
  115. ).toHaveLength(1);
  116. expect(last.tools?.some((tool) => tool.name === name)).toBe(false);
  117. }
  118. if (pass === 0) await driver.turn("E2E_TOOLSEARCH_CATALOG_COMPACT_AGAIN");
  119. }
  120. expect(summaries).toBe(2);
  121. const before = deltas(await driver.history());
  122. await driver.turn("E2E_TOOLSEARCH_CATALOG_STABLE");
  123. expect(deltas(await driver.history())).toEqual(before);
  124. } finally {
  125. await driver.close();
  126. await fixture.close();
  127. }
  128. });
  129. it(`TS06: resident 转换不误删,实际移除后恢复只公告一次,streaming=${streaming}`, async function () {
  130. this.timeout(120_000);
  131. const fixture = await createToolSearchCliFixture("conversation-session-toolsearch-catalog");
  132. const driver = await startToolSearchRuntimeDriver(fixture, streaming);
  133. try {
  134. fixture.setResponse((body, response) => reply(fixture, response, body));
  135. await driver.turn("E2E_TOOLSEARCH_CATALOG_INITIAL");
  136. await driver.catalog("WebFetch", "resident");
  137. await driver.turn("E2E_TOOLSEARCH_CATALOG_RESIDENT");
  138. expect(
  139. fixture.requests.at(-1)!.body.tools?.find((tool) => tool.name === "WebFetch"),
  140. ).toBeDefined();
  141. expect(
  142. fixture.requests.at(-1)!.body.tools?.find((tool) => tool.name === "WebFetch")
  143. ?.defer_loading,
  144. ).toBeUndefined();
  145. expect(deltas(await driver.history())).toHaveLength(1);
  146. await driver.catalog("WebFetch", "deferred");
  147. await driver.turn("E2E_TOOLSEARCH_CATALOG_DEFERRED");
  148. expect(fixture.requests.at(-1)!.body.tools?.some((tool) => tool.name === "WebFetch")).toBe(
  149. false,
  150. );
  151. expect(text(fixture.requests.at(-1)!.body)).not.toContain("no longer available");
  152. await driver.catalog("WebFetch", "absent");
  153. await driver.turn("E2E_TOOLSEARCH_CATALOG_REMOVED");
  154. expect(deltas(await driver.history()).at(-1)?.removedNames).toEqual(["WebFetch"]);
  155. expect(text(fixture.requests.at(-1)!.body)).toContain("no longer available");
  156. await driver.catalog("WebFetch", "deferred");
  157. await driver.turn("E2E_TOOLSEARCH_CATALOG_RESTORED");
  158. expect(deltas(await driver.history()).at(-1)).toEqual({
  159. addedNames: ["WebFetch"],
  160. removedNames: [],
  161. readdedNames: ["WebFetch"],
  162. });
  163. expect(text(fixture.requests.at(-1)!.body)).toContain("available again");
  164. await driver.turn("E2E_TOOLSEARCH_CATALOG_STABLE");
  165. expect(deltas(await driver.history())).toHaveLength(3);
  166. expect(text(fixture.requests.at(-1)!.body).split("available again")).toHaveLength(2);
  167. } finally {
  168. await driver.close();
  169. await fixture.close();
  170. }
  171. });
  172. }
  173. });