pluginUiBackground.test.ts 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. import { afterEach, describe, expect, it, vi } from "vitest";
  2. import type { IServiceAccessor } from "@zcode/services";
  3. import type { IPlatformService } from "@zcode/shared";
  4. import { attachPluginUiToolResults } from "@/plugin-ui/adapters/pluginUiBackground.js";
  5. import { acquirePluginUiPage, pluginUiPages } from "@/plugin-ui/adapters/pluginUiPageRegistry.js";
  6. import { createPage } from "@/plugin-ui/adapters/pluginUiManagedPage.js";
  7. import { buildPluginUiSessionKey } from "@/plugin-ui/contract.js";
  8. vi.mock("@/plugin-ui/adapters/pluginUiManagedPage.js", () => ({
  9. createPage: vi.fn((key, input) => ({
  10. kind: "mcp",
  11. key,
  12. task: buildPluginUiSessionKey(input.scope),
  13. visible: false,
  14. start: vi.fn(),
  15. updateToolFeed: vi.fn(),
  16. update: vi.fn(),
  17. destroy: vi.fn(),
  18. running: () => true,
  19. busy: () => false,
  20. suspend: async () => false,
  21. })),
  22. }));
  23. afterEach(() => {
  24. pluginUiPages.clear();
  25. vi.clearAllMocks();
  26. });
  27. const result = {
  28. workspacePath: "/repo",
  29. sessionId: "background",
  30. toolCallId: "open-1",
  31. serverName: "plugin:editor:editor",
  32. input: { file: "drawing.png" },
  33. ui: { pluginId: "editor@example", resourceUri: "ui://editor", surface: "editor" },
  34. };
  35. function setup(supported = true) {
  36. let accept = (_value: typeof result) => {};
  37. const dispose = vi.fn();
  38. const subscribe = vi.fn(() => (listener: typeof accept) => {
  39. accept = listener;
  40. return { dispose };
  41. });
  42. const services = {
  43. zcodeAgentService: { onDynamicPluginUiToolResult: subscribe },
  44. pluginUiBridgeService: {},
  45. } as unknown as IServiceAccessor;
  46. const platform = (supported ? { pluginSandbox: {} } : {}) as IPlatformService;
  47. const off = attachPluginUiToolResults(services, platform, () => ({
  48. theme: "light",
  49. locale: "en",
  50. }));
  51. return { send: (value = result) => accept(value), off, dispose, subscribe };
  52. }
  53. describe("窗口级后台插件创建", () => {
  54. it("无卡片也创建,并发卡片请求复用同一准入;后续结果只更新工具数据", async () => {
  55. const host = setup();
  56. host.send();
  57. const page = vi.mocked(createPage).mock.results[0]!.value;
  58. expect(
  59. await acquirePluginUiPage(page.key, () => {
  60. throw new Error("duplicate guest");
  61. }),
  62. ).toBe(page);
  63. await vi.waitFor(() => expect(page.start).toHaveBeenCalledTimes(1));
  64. host.send({ ...result, toolCallId: "open-2" });
  65. await vi.waitFor(() => expect(page.updateToolFeed).toHaveBeenCalledTimes(2));
  66. expect(createPage).toHaveBeenCalledTimes(1);
  67. expect(page.update).not.toHaveBeenCalled();
  68. host.off();
  69. expect(host.dispose).toHaveBeenCalledOnce();
  70. });
  71. it("同插件不同会话隔离", async () => {
  72. const host = setup();
  73. host.send();
  74. host.send({ ...result, sessionId: "other" });
  75. await vi.waitFor(() => expect(createPage).toHaveBeenCalledTimes(2));
  76. expect(vi.mocked(createPage).mock.calls[0]![0]).not.toBe(
  77. vi.mocked(createPage).mock.calls[1]![0],
  78. );
  79. host.off();
  80. });
  81. it("Web 不订阅也不创建沙箱", () => {
  82. const host = setup(false);
  83. expect(host.subscribe).not.toHaveBeenCalled();
  84. host.send();
  85. expect(createPage).not.toHaveBeenCalled();
  86. host.off();
  87. });
  88. it("接收器退出后的迟到通知不复活页面", () => {
  89. const host = setup();
  90. host.off();
  91. host.send();
  92. expect(createPage).not.toHaveBeenCalled();
  93. });
  94. });