windowHostAttachmentRegistry.test.ts 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  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("Source 离线时只释放该 Source 的 settings attachments", () => {
  174. const sourceDispose = vi.fn();
  175. const localDispose = vi.fn();
  176. const expose = vi
  177. .fn()
  178. .mockReturnValueOnce({ dispose: sourceDispose })
  179. .mockReturnValueOnce({ dispose: localDispose });
  180. const registry = createWindowHostAttachmentRegistry({
  181. resolveScope: () => ({ services: "services", generation: 1 }),
  182. expose,
  183. });
  184. registry.attach({
  185. requestId: "source-settings",
  186. attachmentId: "source-settings",
  187. clientMode: "desktop-continuous",
  188. scope: { kind: "source-settings", sourceServerId: "source-1" },
  189. port: createPort().port,
  190. });
  191. registry.attach({
  192. requestId: "local",
  193. attachmentId: "local",
  194. clientMode: "desktop-continuous",
  195. scope: { kind: "local" },
  196. port: createPort().port,
  197. });
  198. registry.detachSourceSettingsAttachments("source-1");
  199. expect(sourceDispose).toHaveBeenCalledOnce();
  200. expect(localDispose).not.toHaveBeenCalled();
  201. expect(registry.size()).toBe(1);
  202. });
  203. it("rejects Source settings services from mobile replayable attachments", () => {
  204. const expose = vi.fn();
  205. const resolveScope = vi.fn(() => ({ services: "services", generation: 1 }));
  206. const registry = createWindowHostAttachmentRegistry({ resolveScope, expose });
  207. expect(() =>
  208. registry.attach({
  209. requestId: "mobile-source-settings",
  210. attachmentId: "mobile-source-settings",
  211. clientMode: "web-remote-replayable",
  212. scope: { kind: "source-settings", sourceServerId: "source-1" },
  213. port: createPort().port,
  214. }),
  215. ).toThrow("Desktop-only");
  216. expect(resolveScope).not.toHaveBeenCalled();
  217. expect(expose).not.toHaveBeenCalled();
  218. });
  219. it("scope 验证失败时不暴露 port", () => {
  220. const expose = vi.fn();
  221. const registry = createWindowHostAttachmentRegistry({
  222. resolveScope: () => {
  223. throw new Error("远程 attachment scope 与 logical session 不匹配");
  224. },
  225. expose,
  226. });
  227. expect(() =>
  228. registry.attach({
  229. requestId: "remote-invalid",
  230. attachmentId: "remote-invalid",
  231. clientMode: "desktop-continuous",
  232. scope: {
  233. kind: "remote",
  234. remoteSessionId: "remote-session-1",
  235. workspacePath: "/work/demo",
  236. workspaceIdentity: "remote:ssh:wrong:/work/demo",
  237. },
  238. port: createPort().port,
  239. }),
  240. ).toThrow("远程 attachment scope 与 logical session 不匹配");
  241. expect(expose).not.toHaveBeenCalled();
  242. });
  243. it("logical session generation 变化时只释放该 session 的旧 attachment", () => {
  244. const oldDispose = vi.fn();
  245. const otherDispose = vi.fn();
  246. const localDispose = vi.fn();
  247. const expose = vi
  248. .fn()
  249. .mockReturnValueOnce({ dispose: oldDispose })
  250. .mockReturnValueOnce({ dispose: otherDispose })
  251. .mockReturnValueOnce({ dispose: localDispose });
  252. const registry = createWindowHostAttachmentRegistry({
  253. resolveScope: (scope) => ({
  254. services: "services",
  255. generation: scope.kind === "remote" && scope.remoteSessionId === "remote-1" ? 1 : 2,
  256. }),
  257. expose,
  258. });
  259. registry.attach({
  260. requestId: "old",
  261. attachmentId: "old",
  262. clientMode: "web-remote-replayable",
  263. scope: {
  264. kind: "remote",
  265. remoteSessionId: "remote-1",
  266. workspacePath: "/old",
  267. workspaceIdentity: "remote:ssh:old",
  268. },
  269. port: createPort().port,
  270. });
  271. registry.attach({
  272. requestId: "other",
  273. attachmentId: "other",
  274. clientMode: "desktop-continuous",
  275. scope: {
  276. kind: "remote",
  277. remoteSessionId: "remote-2",
  278. workspacePath: "/other",
  279. workspaceIdentity: "remote:ssh:other",
  280. },
  281. port: createPort().port,
  282. });
  283. registry.attach({
  284. requestId: "local",
  285. attachmentId: "local",
  286. clientMode: "desktop-continuous",
  287. scope: { kind: "local" },
  288. port: createPort().port,
  289. });
  290. registry.detachStaleRemoteSessionAttachments("remote-1", 2);
  291. expect(oldDispose).toHaveBeenCalledTimes(1);
  292. expect(otherDispose).not.toHaveBeenCalled();
  293. expect(localDispose).not.toHaveBeenCalled();
  294. expect(registry.size()).toBe(2);
  295. });
  296. it("remote session 失效时释放该 session 的所有 desktop/mobile attachment 且保持幂等", () => {
  297. const desktopDispose = vi.fn();
  298. const mobileDispose = vi.fn();
  299. const otherRemoteDispose = vi.fn();
  300. const localDispose = vi.fn();
  301. const expose = vi
  302. .fn()
  303. .mockReturnValueOnce({ dispose: desktopDispose })
  304. .mockReturnValueOnce({ dispose: mobileDispose })
  305. .mockReturnValueOnce({ dispose: otherRemoteDispose })
  306. .mockReturnValueOnce({ dispose: localDispose });
  307. const registry = createWindowHostAttachmentRegistry({
  308. resolveScope: () => ({ services: "services", generation: 1 }),
  309. expose,
  310. });
  311. const attachRemote = (
  312. attachmentId: string,
  313. remoteSessionId: string,
  314. clientMode: "desktop-continuous" | "web-remote-replayable",
  315. ) =>
  316. registry.attach({
  317. requestId: `request-${attachmentId}`,
  318. attachmentId,
  319. clientMode,
  320. scope: {
  321. kind: "remote",
  322. remoteSessionId,
  323. workspacePath: "/work/demo",
  324. workspaceIdentity: `remote:ssh:${remoteSessionId}:/work/demo`,
  325. },
  326. port: createPort().port,
  327. });
  328. attachRemote("desktop-old", "remote-old", "desktop-continuous");
  329. attachRemote("mobile-old", "remote-old", "web-remote-replayable");
  330. attachRemote("desktop-current", "remote-current", "desktop-continuous");
  331. registry.attach({
  332. requestId: "request-local",
  333. attachmentId: "local",
  334. clientMode: "desktop-continuous",
  335. scope: { kind: "local" },
  336. port: createPort().port,
  337. });
  338. registry.detachRemoteSessionAttachments("remote-old");
  339. registry.detachRemoteSessionAttachments("remote-old");
  340. expect(desktopDispose).toHaveBeenCalledTimes(1);
  341. expect(mobileDispose).toHaveBeenCalledTimes(1);
  342. expect(otherRemoteDispose).not.toHaveBeenCalled();
  343. expect(localDispose).not.toHaveBeenCalled();
  344. expect(registry.list().map((attachment) => attachment.attachmentId)).toEqual([
  345. "desktop-current",
  346. "local",
  347. ]);
  348. });
  349. });