v4-plugin-ui-background.test.ts 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. import { describe, expect, it, vi } from "vitest";
  2. import { SessionEventType, type SessionEvent } from "@zcode/contracts";
  3. import { ConversationV4Gateway } from "../src/zcode-protocol-v4/index.js";
  4. const ui = { pluginId: "editor@example", resourceUri: "ui://editor", surface: "editor" };
  5. function events(success = true, isError = false): SessionEvent[] {
  6. return [
  7. [SessionEventType.SessionCreated, { mode: "default", contextWindow: 200_000 }],
  8. [SessionEventType.TurnStarted, { turnNumber: 1, input: "open", inputId: "cmd" }],
  9. [
  10. SessionEventType.ToolCallScheduled,
  11. {
  12. toolCallId: "tool",
  13. toolName: "mcp__editor__open",
  14. input: { file: "drawing.png" },
  15. schedule: { parallelGroups: [["tool"]], executionOrder: ["tool"] },
  16. },
  17. ],
  18. [
  19. SessionEventType.ToolCallResult,
  20. {
  21. toolCallId: "tool",
  22. duration: 1,
  23. result: {
  24. success,
  25. content: "opened",
  26. display: {
  27. kind: "mcp_tool",
  28. serverName: "plugin:editor:editor",
  29. toolName: "open",
  30. ui: { ...ui, ...(isError ? { isError: true } : {}) },
  31. },
  32. },
  33. },
  34. ],
  35. ].map(
  36. ([type, payload], index) =>
  37. ({
  38. id: `event-${index}` as never,
  39. sessionId: "session-1" as never,
  40. turnId: "turn-1" as never,
  41. traceId: "trace" as never,
  42. type,
  43. payload,
  44. timestamp: new Date(1000 + index),
  45. sequenceNumber: index + 1,
  46. }) as SessionEvent,
  47. );
  48. }
  49. function setup(history: SessionEvent[] = []) {
  50. const emit = vi.fn(),
  51. frames = vi.fn();
  52. const gateway = new ConversationV4Gateway({
  53. sessionExists: () => true,
  54. executeCommand: async () => undefined,
  55. emitWireFrame: frames,
  56. emitPluginUiToolResult: emit,
  57. loadPersistedEvents: async () => ({ events: history, synthesized: false }),
  58. });
  59. return { gateway, emit, frames };
  60. }
  61. describe("后台 UI 工具结果创建入口", () => {
  62. it("没有 conversation 订阅也投递成功 UI 结果,重复 ingest 不重复投递", () => {
  63. const { gateway, emit, frames } = setup();
  64. try {
  65. const live = events();
  66. for (const event of live) gateway.ingest("session-1", event);
  67. gateway.ingest("session-1", live.at(-1)!);
  68. expect(frames).not.toHaveBeenCalled();
  69. expect(emit).toHaveBeenCalledExactlyOnceWith({
  70. sessionId: "session-1",
  71. toolCallId: "tool",
  72. serverName: "plugin:editor:editor",
  73. ui,
  74. input: { file: "drawing.png" },
  75. });
  76. } finally {
  77. gateway.dispose();
  78. }
  79. });
  80. it.each([
  81. [false, false],
  82. [true, true],
  83. ])("失败结果不创建 success=%s isError=%s", (success, isError) => {
  84. const { gateway, emit } = setup();
  85. try {
  86. for (const event of events(success, isError)) gateway.ingest("session-1", event);
  87. expect(emit).not.toHaveBeenCalled();
  88. } finally {
  89. gateway.dispose();
  90. }
  91. });
  92. it("订阅历史不会重新触发创建副作用", async () => {
  93. const { gateway, emit } = setup(events());
  94. try {
  95. await gateway.subscribe({
  96. connectionId: "history",
  97. clientMode: "desktop-continuous",
  98. topic: "conversation/session-1",
  99. });
  100. expect(emit).not.toHaveBeenCalled();
  101. } finally {
  102. gateway.dispose();
  103. }
  104. });
  105. });