| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188 |
- import { DatabaseSync } from "node:sqlite";
- import { join, resolve } from "node:path";
- import { createToolSearchCliFixture } from "#e2e/helpers/toolsearch-cli-process";
- import { TOOLSEARCH_CALL_ID } from "#e2e/helpers/toolsearch-provider";
- interface StoredPart {
- id: string;
- session_id: string;
- data: string;
- }
- describe("ToolSearch 冷恢复异常与当前权限(真实 CLI / SQLite)", () => {
- for (const metadata of ["legacy", "corrupt"] as const) {
- it(`TS05: ${metadata} metadata 沿旧文本路径恢复,不伪造发现`, async function () {
- this.timeout(90_000);
- const fixture = await createToolSearchCliFixture("conversation-session-toolsearch-recovery");
- try {
- const run = await fixture.start([
- "--toolsearch-mode",
- "native",
- "-p",
- "E2E_TOOLSEARCH_INITIAL 搜索。",
- ]).done;
- expect(run.code).toBe(0);
- const { sessionId } = JSON.parse(run.stdout) as { sessionId: string };
- const db = new DatabaseSync(join(fixture.root, "session.sqlite"));
- try {
- const row = db
- .prepare("select id, data from part where json_extract(data, '$.callID') = ?")
- .get(TOOLSEARCH_CALL_ID) as unknown as StoredPart;
- expect(row).toBeDefined();
- const part = JSON.parse(row.data);
- expect(part.state.metadata.modelContent).toEqual([
- { type: "tool_reference", toolName: "TodoRead" },
- ]);
- if (metadata === "legacy") delete part.state.metadata.modelContent;
- else part.state.metadata.modelContent = [{ type: "tool_reference", toolName: 7 }];
- db.prepare("update part set data = ? where id = ?").run(JSON.stringify(part), row.id);
- } finally {
- db.close();
- }
- const start = fixture.requests.length;
- const cold = await fixture.start([
- "--toolsearch-mode",
- "native",
- "--resume",
- sessionId,
- "-p",
- "E2E_TOOLSEARCH_COLD 检查恢复。",
- ]).done;
- expect(cold.code).toBe(0);
- const first = fixture.requests.slice(start).find(({ body }) => body.tools?.length)!.body;
- expect(first.tools?.some((tool) => tool.name === "TodoRead")).toBe(false);
- expect(JSON.stringify(first.messages)).not.toContain('"type":"tool_reference"');
- expect(JSON.stringify(first.messages)).toContain(TOOLSEARCH_CALL_ID);
- } finally {
- await fixture.close();
- }
- });
- }
- it("TS04: MCP 移除后过滤旧引用,重新启用后恢复发现", async function () {
- this.timeout(120_000);
- const fixture = await createToolSearchCliFixture("conversation-session-toolsearch-recovery");
- const name = "mcp__catalog__lookup";
- const server = {
- type: "stdio",
- command: process.execPath,
- args: [resolve("test/e2e/fixtures/mcp/toolsearch-server.mjs")],
- };
- try {
- await fixture.configure({
- features: { memory: false, skill: false, subagent: false, mcp: true },
- mcp: { servers: { catalog: server } },
- });
- fixture.setResponse((body, response) => {
- const text = JSON.stringify(body.messages);
- fixture.reply(
- body,
- response,
- body.tools?.length &&
- !text.includes(TOOLSEARCH_CALL_ID) &&
- !text.includes("E2E_TOOLSEARCH_COLD")
- ? "mcp-search"
- : "done",
- );
- });
- const initial = await fixture.start([
- "--toolsearch-mode",
- "native",
- "-p",
- "E2E_TOOLSEARCH_INITIAL 搜索 MCP。",
- ]).done;
- expect(initial.code).toBe(0);
- const { sessionId } = JSON.parse(initial.stdout) as { sessionId: string };
- expect(JSON.stringify(fixture.requests.at(-1)!.body.messages)).toContain(
- `"tool_name":"${name}"`,
- );
- for (const enabled of [false, true]) {
- await fixture.configure({
- mcp: {
- servers: {
- catalog: { ...server, enabled, env: { E2E_TOOLSEARCH_SCHEMA_REVISION: "2" } },
- },
- },
- });
- const start = fixture.requests.length;
- const result = await fixture.start([
- "--toolsearch-mode",
- "native",
- "--resume",
- sessionId,
- "-p",
- "E2E_TOOLSEARCH_COLD 检查当前工具池。",
- ]).done;
- expect(result.code).toBe(0);
- const first = fixture.requests.slice(start).find(({ body }) => body.tools?.length)!.body;
- expect(first.tools?.some((tool) => tool.name === name)).toBe(enabled);
- expect(JSON.stringify(first.messages).includes(`"tool_name":"${name}"`)).toBe(enabled);
- if (enabled) {
- expect(first.tools?.find((tool) => tool.name === name)?.input_schema).toMatchObject({
- required: ["value", "revision"],
- });
- }
- }
- } finally {
- await fixture.close();
- }
- });
- it("TS07: 搜索参数仍在流入时中断,冷恢复不产生发现", async function () {
- this.timeout(120_000);
- const fixture = await createToolSearchCliFixture("conversation-session-toolsearch-recovery");
- const interruptedCall = "toolu_search_interrupted";
- try {
- const seed = await fixture.start([
- "--toolsearch-mode",
- "native",
- "-p",
- "E2E_TOOLSEARCH_SEED 稳定历史。",
- ]).done;
- expect(seed.code).toBe(0);
- const { sessionId } = JSON.parse(seed.stdout) as { sessionId: string };
- fixture.setResponse((body, response) => {
- // 参数流 fixture 故意不结束;取消屏障仍是 CLI 实际发出的输入流事件。
- fixture.reply(
- body,
- response,
- body.tools?.length ? "search-partial" : "auxiliary",
- !body.tools?.length,
- );
- });
- const active = fixture.start(
- [
- "--toolsearch-mode",
- "native",
- "--resume",
- sessionId,
- "-p",
- "E2E_TOOLSEARCH_CANCEL 中断搜索。",
- ],
- "stream-json",
- );
- await browser.waitUntil(async () => active.output().includes(interruptedCall), {
- timeout: 30_000,
- timeoutMsg: "CLI 尚未发出 ToolSearch 输入流事件",
- });
- active.child.kill("SIGINT");
- await active.done;
- fixture.setResponse((body, response) => fixture.reply(body, response));
- const start = fixture.requests.length;
- const cold = await fixture.start([
- "--toolsearch-mode",
- "native",
- "--resume",
- sessionId,
- "-p",
- "E2E_TOOLSEARCH_COLD_CANCEL 恢复。",
- ]).done;
- expect(cold.code).toBe(0);
- const first = fixture.requests.slice(start).find(({ body }) => body.tools?.length)!.body;
- expect(first.tools?.some((tool) => tool.name === "TodoRead")).toBe(false);
- expect(JSON.stringify(first.messages)).not.toContain('"type":"tool_reference"');
- } finally {
- await fixture.close();
- }
- });
- });
|