e2eAsyncMcpFixtures.test.ts 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. import { readFile } from "node:fs/promises";
  2. import { describe, expect, it } from "vitest";
  3. import { assertProviderToolContracts } from "./e2e/helpers/provider-prefix.js";
  4. import {
  5. findFixtureIndex,
  6. type UpstreamReplayFixture,
  7. } from "./e2e/helpers/upstream-replay-server.js";
  8. const builtin = { name: "Read", input_schema: { type: "object" } };
  9. const mcp = { name: "mcp__node_repl__js", input_schema: { type: "object" } };
  10. const wait = { name: "WaitForMcpServers", input_schema: { type: "object" } };
  11. describe("异步 MCP 的 E2E 请求断言", () => {
  12. it("允许晚到和冷恢复的 MCP 成员变化,忽略移动的缓存标记", () => {
  13. expect(() => assertProviderToolContracts([builtin, wait], [builtin, mcp, wait])).not.toThrow();
  14. expect(() => assertProviderToolContracts([builtin, mcp, wait], [builtin])).not.toThrow();
  15. expect(() =>
  16. assertProviderToolContracts(
  17. [{ ...builtin, cache_control: { type: "ephemeral" } }],
  18. [builtin, mcp],
  19. ),
  20. ).not.toThrow();
  21. });
  22. it("仍拒绝内置工具增删和已存在工具的 schema 改写", () => {
  23. expect(() => assertProviderToolContracts([builtin], [])).toThrow();
  24. expect(() => assertProviderToolContracts([], [builtin])).toThrow();
  25. expect(() =>
  26. assertProviderToolContracts([builtin], [{ ...builtin, description: "changed" }]),
  27. ).toThrow();
  28. expect(() =>
  29. assertProviderToolContracts([builtin, mcp], [builtin, { ...mcp, input_schema: {} }]),
  30. ).toThrow();
  31. });
  32. });
  33. describe("Browser Use case-local 异步连接回放", () => {
  34. for (const [name, marker, actionId] of [
  35. [
  36. "conversation-session-browser-use-iab-multitab-lifecycle",
  37. "E2E_BROWSER_USE_MULTITAB_TURN_1",
  38. "upstream-browser-use-multitab-create",
  39. ],
  40. [
  41. "conversation-session-browser-use-agent-element-picker",
  42. "E2E_AGENT_ELEMENT_PICKER_TURN",
  43. "upstream-agent-element-picker-open",
  44. ],
  45. [
  46. "conversation-session-browser-use-keypress-host-isolation",
  47. "E2E_BROWSER_USE_KEYPRESS_ISOLATION",
  48. "upstream-browser-use-keypress-action",
  49. ],
  50. [
  51. "conversation-session-browser-zoom-coordinates",
  52. "E2E_BROWSER_ZOOM_COORDINATES E2E_ZOOM_ROUND_0",
  53. "zoom-action-0",
  54. ],
  55. ]) {
  56. it(`${name} 从 pending 经真实等待结果进入工具调用,也支持首请求已就绪`, async () => {
  57. const { fixtures } = JSON.parse(
  58. await readFile(
  59. new URL(`./e2e/fixtures/upstream/conversation-session/${name}.json`, import.meta.url),
  60. "utf8",
  61. ),
  62. ) as { fixtures: UpstreamReplayFixture[] };
  63. const counts = new Map<string, number>();
  64. const messages: unknown[] = [
  65. { role: "user", content: `${marker} E2E_BROWSER_FIXTURE_URL:http://localhost:4321/` },
  66. ];
  67. const match = (tools: unknown[]) => {
  68. const body = { tools, messages };
  69. return fixtures[
  70. findFixtureIndex(
  71. fixtures,
  72. { method: "POST", path: "/v1/messages" },
  73. JSON.stringify(body),
  74. body,
  75. counts,
  76. )
  77. ];
  78. };
  79. expect(match([builtin, mcp])?.id).toBe(actionId);
  80. const callIds = new Set<string>();
  81. for (let attempt = 0; attempt < 3; attempt++) {
  82. const fixture = match([builtin, wait]);
  83. expect(fixture?.response.toolUse?.name).toBe("WaitForMcpServers");
  84. const call = fixture!.response.toolUse!;
  85. if (!call.id) throw new Error("等待工具必须有独立调用 ID");
  86. expect(call.input).toEqual({ servers: ["node_repl"] });
  87. expect(callIds.has(call.id)).toBe(false);
  88. callIds.add(call.id);
  89. counts.set(fixture!.id, 1);
  90. messages.push({ role: "assistant", content: [{ type: "tool_use", ...call }] });
  91. messages.push({
  92. role: "user",
  93. content: [
  94. {
  95. type: "tool_result",
  96. tool_use_id: call.id,
  97. content: "ready: false\nStill connecting (try again or proceed without): node_repl",
  98. is_error: true,
  99. },
  100. ],
  101. });
  102. }
  103. expect(match([builtin, mcp, wait])?.id).toBe(actionId);
  104. });
  105. }
  106. });