mcp-ui-connection-ready.test.ts 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. import { describe, expect, it, vi } from "vitest";
  2. import { mcpUiInstances } from "../src/zcode-protocol/mcp-ui/instances.js";
  3. import { openMcpUiInstance } from "../src/zcode-protocol/mcp-ui/protocol.js";
  4. import { SessionLifecycleController } from "../src/session-lifecycle/index.js";
  5. import { binding } from "./mcp-ui-connection-ready-fixture.js";
  6. describe("MCP App connection readiness", () => {
  7. it.each(["missing"])(
  8. "shares session readiness before opening concurrent pages: %s",
  9. async (outcome) => {
  10. const source = { identity: "resumed-source", generation: 1 };
  11. const gate = Promise.withResolvers<void>();
  12. const activating = Promise.withResolvers<void>();
  13. const record = {
  14. app: {
  15. getMcpAppConnectionSnapshot: () => source,
  16. waitForMcpAppConnection: vi.fn(async () => source),
  17. onMcpAppConnectionInvalidated: () => () => {},
  18. unsubscribeMcpResourcesForUi: async () => {},
  19. },
  20. };
  21. const sessions = new Map<string, typeof record>();
  22. const activate = vi.fn(async () => {
  23. activating.resolve();
  24. await gate.promise;
  25. sessions.set(binding.sessionId, record);
  26. });
  27. const sessionLifecycle = new SessionLifecycleController({
  28. exists: async () => outcome !== "missing",
  29. hasRuntime: (id) => sessions.has(id),
  30. activate,
  31. assertCanStop: () => {},
  32. release: async () => {
  33. sessions.clear();
  34. },
  35. });
  36. const context = { sessions, sessionLifecycle } as unknown as Parameters<
  37. typeof openMcpUiInstance
  38. >[0];
  39. const params = {
  40. ...binding,
  41. workspace: { workspacePath: binding.workspace, workspaceKey: binding.workspace },
  42. };
  43. const restoring =
  44. outcome === "already-starting"
  45. ? sessionLifecycle.ensureReady(binding.sessionId)
  46. : undefined;
  47. const settled = vi.fn();
  48. const openings = [openMcpUiInstance(context, params), openMcpUiInstance(context, params)];
  49. for (const opening of openings) void opening.then(settled, settled);
  50. if (outcome === "missing") {
  51. for (const opening of openings) await expect(opening).rejects.toThrow("Session not found");
  52. expect(activate).not.toHaveBeenCalled();
  53. return;
  54. }
  55. await activating.promise;
  56. expect(settled).not.toHaveBeenCalled();
  57. expect(record.app.waitForMcpAppConnection).not.toHaveBeenCalled();
  58. if (outcome === "failed") {
  59. gate.reject(new Error("fixture session restore failed"));
  60. for (const opening of openings)
  61. await expect(opening).rejects.toThrow("fixture session restore failed");
  62. expect(record.app.waitForMcpAppConnection).not.toHaveBeenCalled();
  63. expect((await sessionLifecycle.getState(binding.sessionId)).state).toBe("unloaded");
  64. } else {
  65. gate.resolve();
  66. const [first, second] = await Promise.all(openings);
  67. await restoring;
  68. expect(first).toEqual(second);
  69. expect(mcpUiInstances.validate(first!, params, source).lease).toEqual(first);
  70. mcpUiInstances.close(first!);
  71. }
  72. expect(activate).toHaveBeenCalledTimes(1);
  73. },
  74. );
  75. });