webRemoteControlSharedHostAttachments.test.ts 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. import { EventEmitter } from "node:events";
  2. import { describe, expect, it, vi } from "vitest";
  3. import { HostMessageTypes } from "@zcode/shared";
  4. import { createWebRemoteControlSharedHostAttachments } from "../src/main/webRemoteControlSharedHostAttachments.js";
  5. const { browserWindowMock } = vi.hoisted(() => ({
  6. browserWindowMock: {
  7. fromId: vi.fn(() => null),
  8. },
  9. }));
  10. vi.mock("electron", () => ({
  11. BrowserWindow: browserWindowMock,
  12. MessageChannelMain: vi.fn(),
  13. }));
  14. class MockUtilityProcess extends EventEmitter {
  15. kill = vi.fn();
  16. postMessage = vi.fn();
  17. }
  18. function createPortPair() {
  19. const port1 = { close: vi.fn(), start: vi.fn(), on: vi.fn(), off: vi.fn(), postMessage: vi.fn() };
  20. const port2 = { close: vi.fn(), start: vi.fn(), on: vi.fn(), off: vi.fn(), postMessage: vi.fn() };
  21. return { port1, port2 };
  22. }
  23. describe("createWebRemoteControlSharedHostAttachments", () => {
  24. it("attaches a local bridge to the existing desktop host process", async () => {
  25. const child = new MockUtilityProcess();
  26. const ports = createPortPair();
  27. browserWindowMock.fromId.mockReturnValueOnce({
  28. id: 7,
  29. webContents: { id: 99 },
  30. });
  31. const manager = createWebRemoteControlSharedHostAttachments({
  32. windowHostProcessMap: new Map([[99, child as never]]),
  33. createMessageChannel: () => ports as never,
  34. attachRemoteWorkspaceSessionHost: vi.fn(),
  35. logger: { info: vi.fn(), warn: vi.fn(), error: vi.fn() },
  36. });
  37. const attachment = await manager.attachWorkspaceHost(7, {
  38. kind: "local",
  39. workspacePath: "/repo/demo",
  40. });
  41. expect(attachment.entryId).toBe("desktop-host:7");
  42. expect(attachment.attachmentId).toMatch(/^shared-host-attachment-/);
  43. expect(attachment.process).toBe(child);
  44. expect(attachment.port).toBe(ports.port1);
  45. expect(child.postMessage).toHaveBeenCalledWith(
  46. {
  47. type: HostMessageTypes.AttachServicePort,
  48. requestId: expect.any(String),
  49. attachmentId: attachment.attachmentId,
  50. clientMode: "web-remote-replayable",
  51. scope: { kind: "local" },
  52. },
  53. [ports.port2],
  54. );
  55. manager.releaseAttachment(attachment.attachmentId);
  56. expect(ports.port1.close).toHaveBeenCalledTimes(1);
  57. });
  58. it("throws desktop-disconnected when the desktop host is missing", async () => {
  59. const manager = createWebRemoteControlSharedHostAttachments({
  60. windowHostProcessMap: new Map(),
  61. createMessageChannel: createPortPair as never,
  62. attachRemoteWorkspaceSessionHost: vi.fn(),
  63. logger: { info: vi.fn(), warn: vi.fn(), error: vi.fn() },
  64. });
  65. await expect(
  66. manager.attachWorkspaceHost(7, {
  67. kind: "local",
  68. workspacePath: "/repo/demo",
  69. }),
  70. ).rejects.toMatchObject({ code: "DESKTOP_HOST_MISSING" });
  71. });
  72. it("rejects remote attach without workspaceIdentity", async () => {
  73. const attachRemoteWorkspaceSessionHost = vi.fn();
  74. const manager = createWebRemoteControlSharedHostAttachments({
  75. windowHostProcessMap: new Map(),
  76. createMessageChannel: createPortPair as never,
  77. attachRemoteWorkspaceSessionHost,
  78. logger: { info: vi.fn(), warn: vi.fn(), error: vi.fn() },
  79. });
  80. await expect(
  81. manager.attachWorkspaceHost(7, {
  82. kind: "remote",
  83. workspacePath: "/repo/demo",
  84. remoteSessionId: "remote-session-1",
  85. }),
  86. ).rejects.toMatchObject({ code: "REMOTE_WORKSPACE_IDENTITY_MISSING" });
  87. expect(attachRemoteWorkspaceSessionHost).not.toHaveBeenCalled();
  88. });
  89. it("WPL-012 reuses one remote Window Host without copying Plugin state into the mobile attachment", async () => {
  90. const workspacePluginSecret = "E2E_WPL_012_WORKSPACE_PLUGIN_SECRET";
  91. const child = Object.assign(new MockUtilityProcess(), {
  92. workspacePluginRuntime: {
  93. configStoreId: "remote-host-user-store",
  94. runtimeId: "remote-cli-runtime-1",
  95. secret: workspacePluginSecret,
  96. },
  97. });
  98. const ports = createPortPair();
  99. const attachRemoteWorkspaceSessionHost = vi.fn(() => ({
  100. process: child as never,
  101. port: ports.port1 as never,
  102. remoteKind: "ssh" as const,
  103. }));
  104. const manager = createWebRemoteControlSharedHostAttachments({
  105. windowHostProcessMap: new Map(),
  106. createMessageChannel: createPortPair as never,
  107. attachRemoteWorkspaceSessionHost,
  108. logger: { info: vi.fn(), warn: vi.fn(), error: vi.fn() },
  109. });
  110. const attachment = await manager.attachWorkspaceHost(7, {
  111. kind: "remote",
  112. workspacePath: "/workspace",
  113. workspaceIdentity: "remote:ssh:host:22:user:/workspace",
  114. remoteSessionId: "remote-session-1",
  115. initialTaskId: "task-1",
  116. });
  117. expect(attachRemoteWorkspaceSessionHost).toHaveBeenCalledTimes(1);
  118. expect(attachRemoteWorkspaceSessionHost).toHaveBeenCalledWith({
  119. windowId: 7,
  120. remoteSessionId: "remote-session-1",
  121. workspacePath: "/workspace",
  122. workspaceIdentity: "remote:ssh:host:22:user:/workspace",
  123. workspaceKey: "remote:ssh:host:22:user:/workspace",
  124. clientMode: "web-remote-replayable",
  125. });
  126. expect(attachment).toMatchObject({
  127. entryId: "remote-session-host:remote-session-1",
  128. process: child,
  129. port: ports.port1,
  130. remoteKind: "ssh",
  131. });
  132. expect(JSON.stringify(attachRemoteWorkspaceSessionHost.mock.calls)).not.toContain(
  133. workspacePluginSecret,
  134. );
  135. expect(child.workspacePluginRuntime).toEqual({
  136. configStoreId: "remote-host-user-store",
  137. runtimeId: "remote-cli-runtime-1",
  138. secret: workspacePluginSecret,
  139. });
  140. manager.releaseAttachment(attachment.attachmentId);
  141. expect(ports.port1.close).toHaveBeenCalledTimes(1);
  142. expect(child.kill).not.toHaveBeenCalled();
  143. expect(child.workspacePluginRuntime.runtimeId).toBe("remote-cli-runtime-1");
  144. });
  145. });