remoteConversationShareWiring.test.ts 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. import { describe, expect, it, vi } from "vitest";
  2. import { IConversationShareService, type IServiceAccessor } from "@zcode/services";
  3. import { createServerRemoteWorkspaceServiceCollection } from "@desktop/host/remoteWorkspaceServiceCollection.js";
  4. import { REMOTE_WORKSPACE_RPC_CHANNELS } from "@desktop/host/remoteWorkspaceRpcContract.js";
  5. import { scopeConversationShareServiceForAttachment } from "@desktop/host/conversationShareAttachmentService.js";
  6. const input = {
  7. workspacePath: "/remote/project",
  8. workspaceIdentity: "remote:server:device:/remote/project",
  9. remoteSessionId: "remote-session",
  10. sessionId: "session",
  11. selection: { kind: "productTurns" as const, productTurnIds: ["turn"] },
  12. };
  13. function fixture() {
  14. const preflight = vi.fn(async () => ({ remote: true }));
  15. const credential = {
  16. load: vi.fn(() => {
  17. throw new Error("Desktop 不得读凭据替远端发布");
  18. }),
  19. };
  20. const connectionServices = {
  21. ...Object.fromEntries(REMOTE_WORKSPACE_RPC_CHANNELS.map((key) => [key, {}])),
  22. credentialService: credential,
  23. conversationShareService: { preflight, publish: vi.fn(), getCapabilities: vi.fn() },
  24. } as unknown as IServiceAccessor;
  25. const clientConfigService = { getSnapshot: vi.fn(async () => ({ pluginStoreOrder: null })) };
  26. return { clientConfigService, connectionServices, preflight, credential };
  27. }
  28. describe("R3 远程分享装配", () => {
  29. it.each(["ssh", "wsl", "docker"] as const)(
  30. "%s 直接使用 Source 分享服务,不读取 Desktop 或远端 Secret",
  31. async (targetKind) => {
  32. const f = fixture();
  33. const service = createServerRemoteWorkspaceServiceCollection({ ...f, targetKind }).get(
  34. IConversationShareService,
  35. );
  36. await expect(service.preflight(input)).resolves.toEqual({ remote: true });
  37. expect(f.preflight).toHaveBeenCalledWith(input);
  38. expect(f.credential.load).not.toHaveBeenCalled();
  39. },
  40. );
  41. it("server 直连仍禁止分享", async () => {
  42. const f = fixture();
  43. const service = createServerRemoteWorkspaceServiceCollection({
  44. ...f,
  45. targetKind: "server",
  46. }).get(IConversationShareService);
  47. await expect(service.preflight(input)).rejects.toMatchObject({ kind: "feature_disabled" });
  48. expect(f.preflight).not.toHaveBeenCalled();
  49. });
  50. it("手机即使附着可用 Source 也不能发布", async () => {
  51. const f = fixture();
  52. const raw = createServerRemoteWorkspaceServiceCollection({ ...f, targetKind: "ssh" }).get(
  53. IConversationShareService,
  54. );
  55. const service = scopeConversationShareServiceForAttachment(raw, "web-remote-replayable");
  56. await expect(service.preflight(input)).rejects.toMatchObject({ kind: "feature_disabled" });
  57. await expect(service.publish({} as never, "op")).rejects.toMatchObject({
  58. kind: "feature_disabled",
  59. });
  60. expect(f.preflight).not.toHaveBeenCalled();
  61. });
  62. it("重新装配使用新连接,不能保留旧 Source 服务", async () => {
  63. const old = fixture();
  64. const next = fixture();
  65. createServerRemoteWorkspaceServiceCollection({ ...old, targetKind: "ssh" });
  66. const current = createServerRemoteWorkspaceServiceCollection({
  67. ...next,
  68. targetKind: "ssh",
  69. }).get(IConversationShareService);
  70. await current.preflight(input);
  71. expect(next.preflight).toHaveBeenCalledOnce();
  72. expect(old.preflight).not.toHaveBeenCalled();
  73. });
  74. });