windowHostAttachmentRegistry.test.ts 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. import { describe, expect, it, vi } from "vitest";
  2. import { createWindowHostAttachmentRegistry } from "../src/host/windowHostAttachmentRegistry.js";
  3. function createPort() {
  4. let closeListener: (() => void) | null = null;
  5. return {
  6. port: {
  7. once(event: "close", listener: () => void) {
  8. if (event === "close") {
  9. closeListener = listener;
  10. }
  11. },
  12. },
  13. close() {
  14. closeListener?.();
  15. },
  16. };
  17. }
  18. describe("WindowHostAttachmentRegistry", () => {
  19. it("base 与 remote scope 在同一个 Host 内解析各自 services", () => {
  20. const expose = vi.fn(() => ({ dispose: vi.fn() }));
  21. const registry = createWindowHostAttachmentRegistry({
  22. resolveScope: (scope) =>
  23. scope.kind === "local"
  24. ? { services: "local-services", generation: 1 }
  25. : { services: `remote-services:${scope.remoteSessionId}`, generation: 7 },
  26. expose,
  27. });
  28. const localPort = createPort();
  29. const remotePort = createPort();
  30. registry.attach({
  31. requestId: "attach-local",
  32. attachmentId: "local-1",
  33. clientMode: "desktop-continuous",
  34. scope: { kind: "local" },
  35. port: localPort.port,
  36. });
  37. registry.attach({
  38. requestId: "attach-remote",
  39. attachmentId: "remote-1",
  40. clientMode: "desktop-continuous",
  41. scope: {
  42. kind: "remote",
  43. remoteSessionId: "remote-session-1",
  44. workspacePath: "/work/demo",
  45. workspaceIdentity: "remote:ssh:dev:/work/demo",
  46. },
  47. port: remotePort.port,
  48. });
  49. expect(expose).toHaveBeenNthCalledWith(
  50. 1,
  51. expect.objectContaining({ services: "local-services", clientMode: "desktop-continuous" }),
  52. );
  53. expect(expose).toHaveBeenNthCalledWith(
  54. 2,
  55. expect.objectContaining({
  56. services: "remote-services:remote-session-1",
  57. clientMode: "desktop-continuous",
  58. generation: 7,
  59. }),
  60. );
  61. expect(registry.size()).toBe(2);
  62. });
  63. it("每个 attachment 注入自己的可信 clientMode", () => {
  64. const expose = vi.fn(() => ({ dispose: vi.fn() }));
  65. const registry = createWindowHostAttachmentRegistry({
  66. resolveScope: () => ({ services: "services", generation: 1 }),
  67. expose,
  68. });
  69. registry.attach({
  70. requestId: "desktop-1",
  71. attachmentId: "desktop-1",
  72. clientMode: "desktop-continuous",
  73. scope: { kind: "local" },
  74. port: createPort().port,
  75. });
  76. registry.attach({
  77. requestId: "mobile-1",
  78. attachmentId: "mobile-1",
  79. clientMode: "web-remote-replayable",
  80. scope: { kind: "local" },
  81. port: createPort().port,
  82. });
  83. expect(expose.mock.calls.map(([call]) => call.clientMode)).toEqual([
  84. "desktop-continuous",
  85. "web-remote-replayable",
  86. ]);
  87. });
  88. it("将 remote connection capabilities 一并注入 attachment,供媒体服务按 clientMode 分流", () => {
  89. const expose = vi.fn(() => ({ dispose: vi.fn() }));
  90. const registry = createWindowHostAttachmentRegistry({
  91. resolveScope: () => ({
  92. services: "services",
  93. generation: 1,
  94. capabilities: { remoteMediaPreview: "available" },
  95. }),
  96. expose,
  97. });
  98. registry.attach({
  99. requestId: "remote-1",
  100. attachmentId: "remote-1",
  101. clientMode: "desktop-continuous",
  102. scope: {
  103. kind: "remote",
  104. remoteSessionId: "remote-session-1",
  105. workspacePath: "/work/demo",
  106. workspaceIdentity: "remote:ssh:dev:/work/demo",
  107. },
  108. port: createPort().port,
  109. });
  110. expect(expose).toHaveBeenCalledWith(
  111. expect.objectContaining({ capabilities: { remoteMediaPreview: "available" } }),
  112. );
  113. });
  114. it("相同 attachmentId 替换时只释放旧 port,其他 port 不受影响", () => {
  115. const firstDispose = vi.fn();
  116. const secondDispose = vi.fn();
  117. const mobileDispose = vi.fn();
  118. const expose = vi
  119. .fn()
  120. .mockReturnValueOnce({ dispose: firstDispose })
  121. .mockReturnValueOnce({ dispose: mobileDispose })
  122. .mockReturnValueOnce({ dispose: secondDispose });
  123. const registry = createWindowHostAttachmentRegistry({
  124. resolveScope: () => ({ services: "services", generation: 1 }),
  125. expose,
  126. });
  127. registry.attach({
  128. requestId: "desktop-old",
  129. attachmentId: "desktop",
  130. clientMode: "desktop-continuous",
  131. scope: { kind: "local" },
  132. port: createPort().port,
  133. });
  134. registry.attach({
  135. requestId: "mobile",
  136. attachmentId: "mobile",
  137. clientMode: "web-remote-replayable",
  138. scope: { kind: "local" },
  139. port: createPort().port,
  140. });
  141. registry.attach({
  142. requestId: "desktop-new",
  143. attachmentId: "desktop",
  144. clientMode: "desktop-continuous",
  145. scope: { kind: "local" },
  146. port: createPort().port,
  147. });
  148. expect(firstDispose).toHaveBeenCalledTimes(1);
  149. expect(mobileDispose).not.toHaveBeenCalled();
  150. expect(secondDispose).not.toHaveBeenCalled();
  151. expect(registry.size()).toBe(2);
  152. });
  153. it("port close 和显式 detach 均只释放自身且幂等", () => {
  154. const dispose = vi.fn();
  155. const registry = createWindowHostAttachmentRegistry({
  156. resolveScope: () => ({ services: "services", generation: 1 }),
  157. expose: () => ({ dispose }),
  158. });
  159. const port = createPort();
  160. registry.attach({
  161. requestId: "mobile-1",
  162. attachmentId: "mobile-1",
  163. clientMode: "web-remote-replayable",
  164. scope: { kind: "local" },
  165. port: port.port,
  166. });
  167. port.close();
  168. registry.detach("mobile-1");
  169. registry.detach("mobile-1");
  170. expect(dispose).toHaveBeenCalledTimes(1);
  171. expect(registry.size()).toBe(0);
  172. });
  173. it("scope 验证失败时不暴露 port", () => {
  174. const expose = vi.fn();
  175. const registry = createWindowHostAttachmentRegistry({
  176. resolveScope: () => {
  177. throw new Error("远程 attachment scope 与 logical session 不匹配");
  178. },
  179. expose,
  180. });
  181. expect(() =>
  182. registry.attach({
  183. requestId: "remote-invalid",
  184. attachmentId: "remote-invalid",
  185. clientMode: "desktop-continuous",
  186. scope: {
  187. kind: "remote",
  188. remoteSessionId: "remote-session-1",
  189. workspacePath: "/work/demo",
  190. workspaceIdentity: "remote:ssh:wrong:/work/demo",
  191. },
  192. port: createPort().port,
  193. }),
  194. ).toThrow("远程 attachment scope 与 logical session 不匹配");
  195. expect(expose).not.toHaveBeenCalled();
  196. });
  197. it("logical session generation 变化时只释放该 session 的旧 attachment", () => {
  198. const oldDispose = vi.fn();
  199. const otherDispose = vi.fn();
  200. const localDispose = vi.fn();
  201. const expose = vi
  202. .fn()
  203. .mockReturnValueOnce({ dispose: oldDispose })
  204. .mockReturnValueOnce({ dispose: otherDispose })
  205. .mockReturnValueOnce({ dispose: localDispose });
  206. const registry = createWindowHostAttachmentRegistry({
  207. resolveScope: (scope) => ({
  208. services: "services",
  209. generation: scope.kind === "remote" && scope.remoteSessionId === "remote-1" ? 1 : 2,
  210. }),
  211. expose,
  212. });
  213. registry.attach({
  214. requestId: "old",
  215. attachmentId: "old",
  216. clientMode: "web-remote-replayable",
  217. scope: {
  218. kind: "remote",
  219. remoteSessionId: "remote-1",
  220. workspacePath: "/old",
  221. workspaceIdentity: "remote:ssh:old",
  222. },
  223. port: createPort().port,
  224. });
  225. registry.attach({
  226. requestId: "other",
  227. attachmentId: "other",
  228. clientMode: "desktop-continuous",
  229. scope: {
  230. kind: "remote",
  231. remoteSessionId: "remote-2",
  232. workspacePath: "/other",
  233. workspaceIdentity: "remote:ssh:other",
  234. },
  235. port: createPort().port,
  236. });
  237. registry.attach({
  238. requestId: "local",
  239. attachmentId: "local",
  240. clientMode: "desktop-continuous",
  241. scope: { kind: "local" },
  242. port: createPort().port,
  243. });
  244. registry.detachStaleRemoteSessionAttachments("remote-1", 2);
  245. expect(oldDispose).toHaveBeenCalledTimes(1);
  246. expect(otherDispose).not.toHaveBeenCalled();
  247. expect(localDispose).not.toHaveBeenCalled();
  248. expect(registry.size()).toBe(2);
  249. });
  250. it("remote session 失效时释放该 session 的所有 desktop/mobile attachment 且保持幂等", () => {
  251. const desktopDispose = vi.fn();
  252. const mobileDispose = vi.fn();
  253. const otherRemoteDispose = vi.fn();
  254. const localDispose = vi.fn();
  255. const expose = vi
  256. .fn()
  257. .mockReturnValueOnce({ dispose: desktopDispose })
  258. .mockReturnValueOnce({ dispose: mobileDispose })
  259. .mockReturnValueOnce({ dispose: otherRemoteDispose })
  260. .mockReturnValueOnce({ dispose: localDispose });
  261. const registry = createWindowHostAttachmentRegistry({
  262. resolveScope: () => ({ services: "services", generation: 1 }),
  263. expose,
  264. });
  265. const attachRemote = (
  266. attachmentId: string,
  267. remoteSessionId: string,
  268. clientMode: "desktop-continuous" | "web-remote-replayable",
  269. ) =>
  270. registry.attach({
  271. requestId: `request-${attachmentId}`,
  272. attachmentId,
  273. clientMode,
  274. scope: {
  275. kind: "remote",
  276. remoteSessionId,
  277. workspacePath: "/work/demo",
  278. workspaceIdentity: `remote:ssh:${remoteSessionId}:/work/demo`,
  279. },
  280. port: createPort().port,
  281. });
  282. attachRemote("desktop-old", "remote-old", "desktop-continuous");
  283. attachRemote("mobile-old", "remote-old", "web-remote-replayable");
  284. attachRemote("desktop-current", "remote-current", "desktop-continuous");
  285. registry.attach({
  286. requestId: "request-local",
  287. attachmentId: "local",
  288. clientMode: "desktop-continuous",
  289. scope: { kind: "local" },
  290. port: createPort().port,
  291. });
  292. registry.detachRemoteSessionAttachments("remote-old");
  293. registry.detachRemoteSessionAttachments("remote-old");
  294. expect(desktopDispose).toHaveBeenCalledTimes(1);
  295. expect(mobileDispose).toHaveBeenCalledTimes(1);
  296. expect(otherRemoteDispose).not.toHaveBeenCalled();
  297. expect(localDispose).not.toHaveBeenCalled();
  298. expect(registry.list().map((attachment) => attachment.attachmentId)).toEqual([
  299. "desktop-current",
  300. "local",
  301. ]);
  302. });
  303. });