remoteWorkspaceProjection.test.ts 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. import { describe, expect, it } from "vitest";
  2. import type { SourceServerManagementSnapshot } from "@zcode/shared";
  3. import { buildRemoteWorkspaceProjection } from "@/remote/remoteWorkspaceProjection.js";
  4. function createSnapshot(): SourceServerManagementSnapshot {
  5. return {
  6. generation: 3,
  7. servers: [
  8. {
  9. sourceServerId: "11111111-1111-4111-8111-111111111111",
  10. displayName: "Dev Linux",
  11. osUser: "dev",
  12. installState: "installed",
  13. declaredWorkspaces: [],
  14. },
  15. ],
  16. profiles: [
  17. {
  18. profileId: "22222222-2222-4222-8222-222222222222",
  19. sourceServerId: "11111111-1111-4111-8111-111111111111",
  20. transport: "ssh",
  21. target: { kind: "ssh", host: "linux-amd64-dev", username: "dev" },
  22. createdAt: 1,
  23. lifecycleState: "active",
  24. },
  25. ],
  26. states: [
  27. {
  28. profileId: "22222222-2222-4222-8222-222222222222",
  29. sourceServerId: "11111111-1111-4111-8111-111111111111",
  30. state: "online",
  31. updatedAt: 3,
  32. },
  33. ],
  34. workspaceHistory: [
  35. {
  36. sourceServerId: "11111111-1111-4111-8111-111111111111",
  37. profileId: "22222222-2222-4222-8222-222222222222",
  38. workspacePath: "/workspace/project",
  39. workspaceIdentity: "remote:server:11111111-1111-4111-8111-111111111111:/workspace/project",
  40. lastOpenedAt: 2,
  41. },
  42. ],
  43. failedPathCount: 0,
  44. abandonedPathCount: 0,
  45. migrationCounts: [],
  46. };
  47. }
  48. describe("RemoteWorkspaceProjection", () => {
  49. it("joins device status, project history and open attachments without treating online as ready", () => {
  50. const projection = buildRemoteWorkspaceProjection({
  51. managementSnapshot: createSnapshot(),
  52. tabs: [
  53. {
  54. id: "conversation",
  55. kind: "workspace",
  56. workspacePath: "/workspace/conversation",
  57. workspaceIdentity:
  58. "remote:server:11111111-1111-4111-8111-111111111111:/workspace/conversation",
  59. workspacePurpose: "conversation",
  60. label: "conversation",
  61. remoteSessionId: "session-conversation",
  62. },
  63. {
  64. id: "project",
  65. kind: "workspace",
  66. workspacePath: "/workspace/project",
  67. workspaceIdentity:
  68. "remote:server:11111111-1111-4111-8111-111111111111:/workspace/project",
  69. workspacePurpose: "project",
  70. label: "project",
  71. },
  72. ],
  73. attachments: [
  74. {
  75. remoteSessionId: "session-conversation",
  76. workspaceKey:
  77. "remote:server:11111111-1111-4111-8111-111111111111:/workspace/conversation",
  78. sourceServerId: "11111111-1111-4111-8111-111111111111",
  79. state: "ready",
  80. },
  81. ],
  82. });
  83. expect(projection.devices).toEqual([
  84. expect.objectContaining({
  85. sourceServerId: "11111111-1111-4111-8111-111111111111",
  86. aggregatedStatus: "online",
  87. }),
  88. ]);
  89. expect(projection.openWorkspaces).toEqual([
  90. expect.objectContaining({
  91. workspacePath: "/workspace/conversation",
  92. purpose: "conversation",
  93. attachmentState: "ready",
  94. sourceServerId: "11111111-1111-4111-8111-111111111111",
  95. }),
  96. expect.objectContaining({
  97. workspacePath: "/workspace/project",
  98. purpose: "project",
  99. attachmentState: "disconnected",
  100. }),
  101. ]);
  102. expect(projection.projectHistory).toEqual([
  103. expect.objectContaining({ workspacePath: "/workspace/project" }),
  104. ]);
  105. });
  106. it("keeps the same path with different identities isolated", () => {
  107. const snapshot = createSnapshot();
  108. snapshot.workspaceHistory = [
  109. ...snapshot.workspaceHistory,
  110. {
  111. sourceServerId: "11111111-1111-4111-8111-111111111111",
  112. profileId: "22222222-2222-4222-8222-222222222222",
  113. workspacePath: "/workspace/shared",
  114. workspaceIdentity: "remote:server:other:/workspace/shared",
  115. lastOpenedAt: 4,
  116. },
  117. ];
  118. const projection = buildRemoteWorkspaceProjection({
  119. managementSnapshot: snapshot,
  120. tabs: [
  121. {
  122. id: "remote-a",
  123. kind: "workspace",
  124. workspacePath: "/workspace/shared",
  125. workspaceIdentity: "remote:server:source-a:/workspace/shared",
  126. workspacePurpose: "project",
  127. label: "shared-a",
  128. },
  129. {
  130. id: "remote-b",
  131. kind: "workspace",
  132. workspacePath: "/workspace/shared",
  133. workspaceIdentity: "remote:server:source-b:/workspace/shared",
  134. workspacePurpose: "conversation",
  135. label: "shared-b",
  136. },
  137. ],
  138. attachments: [],
  139. });
  140. expect(projection.openWorkspaces).toHaveLength(2);
  141. expect(new Set(projection.openWorkspaces.map((item) => item.workspaceKey))).toEqual(
  142. new Set([
  143. "remote:server:source-a:/workspace/shared",
  144. "remote:server:source-b:/workspace/shared",
  145. ]),
  146. );
  147. expect(projection.projectHistory).toHaveLength(2);
  148. });
  149. it("keeps local tabs in execution contexts without projecting them as remote workspaces", () => {
  150. const projection = buildRemoteWorkspaceProjection({
  151. tabs: [
  152. {
  153. id: "local",
  154. kind: "workspace",
  155. workspacePath: "/local/project",
  156. workspacePurpose: "project",
  157. label: "project",
  158. },
  159. {
  160. id: "remote",
  161. kind: "workspace",
  162. workspacePath: "/remote/project",
  163. workspaceIdentity: "remote:server:11111111-1111-4111-8111-111111111111:/remote/project",
  164. workspacePurpose: "project",
  165. label: "project",
  166. },
  167. ],
  168. });
  169. expect(projection.openWorkspaces.map((item) => item.workspacePath)).toEqual([
  170. "/remote/project",
  171. ]);
  172. expect(projection.executionContexts).toEqual([
  173. expect.objectContaining({
  174. kind: "local",
  175. workspacePath: "/local/project",
  176. }),
  177. expect.objectContaining({
  178. kind: "remote",
  179. workspacePath: "/remote/project",
  180. }),
  181. ]);
  182. expect(projection.executionContexts[0]).not.toHaveProperty("attachmentState");
  183. });
  184. it("没有可信 attachment fact 时不会把在线 remote session 冒充成 ready", () => {
  185. const projection = buildRemoteWorkspaceProjection({
  186. managementSnapshot: createSnapshot(),
  187. tabs: [
  188. {
  189. id: "remote-session",
  190. kind: "workspace",
  191. workspacePath: "/workspace/project",
  192. workspaceIdentity:
  193. "remote:server:11111111-1111-4111-8111-111111111111:/workspace/project",
  194. remoteSessionId: "session-without-attachment-fact",
  195. label: "project",
  196. },
  197. ],
  198. });
  199. expect(projection.openWorkspaces[0]).toEqual(
  200. expect.objectContaining({ attachmentState: "attaching" }),
  201. );
  202. });
  203. });