conversationShareAttachmentService.test.ts 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. import { Event } from "@zcode/rpc";
  2. import { conversationShareConnectionScopeFactory } from "@zcode/services/node";
  3. import { describe, expect, it, vi } from "vitest";
  4. import { scopeConversationShareServiceForAttachment } from "../src/host/conversationShareAttachmentService.js";
  5. function service() {
  6. const scoped = {
  7. getCapabilities: vi.fn(),
  8. publish: vi.fn(),
  9. onDynamicPublishProgress: () => Event.None,
  10. importShare: vi.fn(),
  11. onDynamicImportProgress: () => Event.None,
  12. getPreview: vi.fn(),
  13. getContinuation: vi.fn(),
  14. };
  15. return {
  16. getCapabilities: vi.fn(),
  17. publish: vi.fn(),
  18. onDynamicPublishProgress: () => Event.None,
  19. importShare: vi.fn(),
  20. onDynamicImportProgress: () => Event.None,
  21. getPreview: vi.fn(),
  22. getContinuation: vi.fn(),
  23. [conversationShareConnectionScopeFactory]: vi.fn(() => scoped),
  24. scoped,
  25. };
  26. }
  27. describe("conversation share attachment scope", () => {
  28. it("Desktop 绑定 attachment Agent scope,mobile 不创建 scope 并拒绝发布", async () => {
  29. const desktop = service();
  30. const agentService = { connection: "desktop" };
  31. expect(
  32. scopeConversationShareServiceForAttachment(
  33. desktop,
  34. "desktop-continuous",
  35. agentService as never,
  36. ),
  37. ).toBe(desktop.scoped);
  38. expect(desktop[conversationShareConnectionScopeFactory]).toHaveBeenCalledWith(agentService);
  39. const mobileService = service();
  40. const mobile = scopeConversationShareServiceForAttachment(
  41. mobileService,
  42. "web-remote-replayable",
  43. { connection: "mobile" } as never,
  44. ) as ReturnType<typeof service>;
  45. expect(mobileService[conversationShareConnectionScopeFactory]).not.toHaveBeenCalled();
  46. await expect(mobile.publish({} as never, "op")).rejects.toMatchObject({
  47. kind: "feature_disabled",
  48. });
  49. await expect(
  50. mobile.getContinuation({ shareCode: "share", clientRequestId: "request" }),
  51. ).rejects.toMatchObject({ kind: "feature_disabled" });
  52. await expect(
  53. mobile.importShare({ shareCode: "share", clientRequestId: "request" }, "op"),
  54. ).rejects.toMatchObject({ kind: "feature_disabled" });
  55. });
  56. it("Desktop 缺少 Agent scope 时只拒绝发布,导入仍委托原服务", async () => {
  57. const desktop = service();
  58. desktop.importShare.mockResolvedValueOnce({ workspacePath: "/imported" });
  59. const scoped = scopeConversationShareServiceForAttachment(desktop, "desktop-continuous");
  60. await expect(scoped.publish({} as never, "op")).rejects.toMatchObject({
  61. kind: "connection_unavailable",
  62. });
  63. await expect(
  64. scoped.importShare({ shareCode: "share", clientRequestId: "request" }, "import-op"),
  65. ).resolves.toEqual({ workspacePath: "/imported" });
  66. expect(desktop.importShare).toHaveBeenCalledTimes(1);
  67. });
  68. });