remoteWorkspaceServicePortBridge.test.ts 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. import { describe, expect, it, vi } from "vitest";
  2. import { InternalChannels } from "@zcode/shared";
  3. import {
  4. notifyRemoteWorkspaceServicePortReady,
  5. parseRemoteWorkspaceServicePortMessage,
  6. } from "../src/renderer/src/remoteWorkspaceServicePortBridge.js";
  7. describe("remoteWorkspaceServicePortBridge", () => {
  8. it("解析带 attachmentId 的 scoped service port", () => {
  9. const port = {} as MessagePort;
  10. const target = { kind: "ssh", host: "dev.internal", username: "developer" } as const;
  11. const workspacePath = "/remote/project";
  12. const workspaceIdentity = "remote:server:source-1:/remote/project";
  13. expect(
  14. parseRemoteWorkspaceServicePortMessage({
  15. data: {
  16. type: InternalChannels.ScopedServicePort,
  17. attachmentId: "attachment-b",
  18. sessionId: "remote-session-1",
  19. target,
  20. sourceServerId: "source-1",
  21. workspacePath,
  22. workspaceIdentity,
  23. workspacePurpose: "project",
  24. },
  25. ports: [port],
  26. } as unknown as MessageEvent),
  27. ).toEqual({
  28. attachmentId: "attachment-b",
  29. port,
  30. sessionId: "remote-session-1",
  31. target,
  32. sourceServerId: "source-1",
  33. workspacePath,
  34. workspaceIdentity,
  35. workspacePurpose: "project",
  36. });
  37. });
  38. it("拒绝缺少 workspace scope 的 scoped service port", () => {
  39. const port = {} as MessagePort;
  40. expect(
  41. parseRemoteWorkspaceServicePortMessage({
  42. data: {
  43. type: InternalChannels.ScopedServicePort,
  44. attachmentId: "attachment-b",
  45. sessionId: "remote-session-1",
  46. target: { kind: "docker", container: "dev" },
  47. },
  48. ports: [port],
  49. } as unknown as MessageEvent),
  50. ).toBeNull();
  51. });
  52. it("services 注册完成后发送 attachment-ready ACK", () => {
  53. const postMessage = vi.fn();
  54. notifyRemoteWorkspaceServicePortReady(
  55. { attachmentId: "attachment-b", sessionId: "remote-session-1" },
  56. postMessage,
  57. );
  58. expect(postMessage).toHaveBeenCalledWith(
  59. {
  60. type: InternalChannels.ScopedServicePortReady,
  61. attachmentId: "attachment-b",
  62. sessionId: "remote-session-1",
  63. },
  64. "*",
  65. );
  66. });
  67. });