sourceServerDirectory.test.ts 63 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959
  1. import { describe, expect, it, vi } from "vitest";
  2. import { appSettingsSchema, type AppSettings } from "@zcode/shared";
  3. import type { ICredentialService, ISettingService } from "@zcode/services";
  4. import { SourceServerDirectory } from "../src/host/sourceServerDirectory.js";
  5. const profileId = "17ef20b3-622d-4a53-98c1-0cdff29f3e6d";
  6. const sourceServerId = "3cd4f9da-cd79-426c-9d70-75f36e9f6a87";
  7. const replacementSourceServerId = "fbc717a0-86d5-464d-b976-8eb55db6f1db";
  8. const replacementProfileId = "8a116c93-1306-48a8-b8bf-4bdc318b8684";
  9. function createSettingService(initial: Partial<AppSettings>): {
  10. service: ISettingService;
  11. read: () => AppSettings;
  12. } {
  13. let settings = appSettingsSchema.parse(initial);
  14. return {
  15. read: () => settings,
  16. service: {
  17. get: vi.fn(async () => settings),
  18. update: vi.fn(async (patch) => {
  19. settings = appSettingsSchema.parse({ ...settings, ...patch });
  20. }),
  21. updateDataBaseDir: vi.fn(),
  22. },
  23. };
  24. }
  25. function createCredentialService(initial: Record<string, string>): {
  26. service: ICredentialService;
  27. read: (key: string) => string | undefined;
  28. } {
  29. const values = new Map(Object.entries(initial));
  30. return {
  31. read: (key) => values.get(key),
  32. service: {
  33. load: vi.fn(async (key) => values.get(key) ?? null),
  34. save: vi.fn(async (key, value) => {
  35. values.set(key, value);
  36. }),
  37. delete: vi.fn(async (key) => {
  38. values.delete(key);
  39. }),
  40. },
  41. };
  42. }
  43. describe("SourceServerDirectory", () => {
  44. it("includes saved profiles that have never completed their first handshake", async () => {
  45. const settings = createSettingService({
  46. sourceServerDirectoryMigrationVersion: 1,
  47. connectionProfiles: [
  48. {
  49. profileId,
  50. transport: "docker",
  51. target: { kind: "docker", container: "new" },
  52. createdAt: 1,
  53. },
  54. ],
  55. });
  56. const directory = new SourceServerDirectory({
  57. settingService: settings.service,
  58. credentialService: createCredentialService({}).service,
  59. });
  60. await expect(directory.listStartupReconnectCandidates()).resolves.toEqual([
  61. {
  62. profile: settings.read().connectionProfiles?.[0],
  63. target: { kind: "docker", container: "new" },
  64. },
  65. ]);
  66. });
  67. it("最近在线保存最后在线时刻,迟到写入不能倒退或复活已移除设备", async () => {
  68. const settings = createSettingService({
  69. sourceServers: [
  70. { sourceServerId, displayName: "Build", installState: "installed", lastOnlineAt: 1000 },
  71. ],
  72. });
  73. const directory = new SourceServerDirectory({
  74. settingService: settings.service,
  75. credentialService: createCredentialService({}).service,
  76. });
  77. await directory.recordLastOnline(sourceServerId, 9000);
  78. await directory.recordLastOnline(sourceServerId, 4000);
  79. expect((await directory.snapshot()).servers[0]?.lastOnlineAt).toBe(9000);
  80. await directory.removeSourceServer(sourceServerId);
  81. await directory.recordLastOnline(sourceServerId, 12000);
  82. expect((await directory.snapshot()).servers).toEqual([]);
  83. });
  84. it("stores a Source-scoped conversation backing path outside strict project history", async () => {
  85. const conversationPath = "/home/dev/.zcode/workspace/default";
  86. const realProject = {
  87. profileId,
  88. sourceServerId,
  89. workspacePath: "/home/dev/project",
  90. workspaceIdentity: `remote:server:${sourceServerId}:/home/dev/project`,
  91. lastOpenedAt: 10,
  92. };
  93. const otherSourceConversationPath = {
  94. profileId: replacementProfileId,
  95. sourceServerId: replacementSourceServerId,
  96. workspacePath: conversationPath,
  97. workspaceIdentity: `remote:server:${replacementSourceServerId}:${conversationPath}`,
  98. lastOpenedAt: 11,
  99. };
  100. const settings = createSettingService({
  101. sourceServerWorkspaceHistory: [
  102. {
  103. profileId,
  104. sourceServerId,
  105. workspacePath: conversationPath,
  106. workspaceIdentity: `remote:server:${sourceServerId}:${conversationPath}`,
  107. lastOpenedAt: 9,
  108. },
  109. realProject,
  110. otherSourceConversationPath,
  111. ],
  112. });
  113. const directory = new SourceServerDirectory({
  114. settingService: settings.service,
  115. credentialService: createCredentialService({}).service,
  116. now: () => 42,
  117. });
  118. await directory.recordConversationWorkspace(sourceServerId, conversationPath);
  119. expect(settings.read().remoteConversationWorkspaceBySourceServerId).toEqual({
  120. [sourceServerId]: {
  121. workspacePath: conversationPath,
  122. lastResolvedAt: 42,
  123. },
  124. });
  125. expect(settings.read().sourceServerWorkspaceHistory).toEqual([
  126. realProject,
  127. otherSourceConversationPath,
  128. ]);
  129. });
  130. it("allows a failed initialization flight to be retried", async () => {
  131. const settings = createSettingService({});
  132. vi.mocked(settings.service.get).mockRejectedValueOnce(new Error("settings temporarily locked"));
  133. const directory = new SourceServerDirectory({
  134. settingService: settings.service,
  135. credentialService: createCredentialService({}).service,
  136. });
  137. await expect(directory.initialize()).rejects.toThrow("settings temporarily locked");
  138. await expect(directory.initialize()).resolves.toBeUndefined();
  139. expect(settings.read().sourceServerDirectoryMigrationVersion).toBe(1);
  140. });
  141. it("rolls back migrated SSH credentials when settings persistence fails before a retry", async () => {
  142. const legacyPasswordKey = "remote-workspace:legacy:password";
  143. const firstProfilePasswordKey = `connection-profile:${profileId}:password`;
  144. const replacementProfilePasswordKey = `connection-profile:${replacementProfileId}:password`;
  145. const settings = createSettingService({
  146. lastWorkspaceSession: [
  147. {
  148. kind: "remote",
  149. workspacePath: "/srv/project",
  150. workspaceIdentity: "remote:ssh:example.com:22:alice:/srv/project",
  151. target: {
  152. kind: "ssh",
  153. host: "example.com",
  154. username: "alice",
  155. passwordCredentialKey: legacyPasswordKey,
  156. },
  157. lastOpenedAt: 10,
  158. lastConnectionStatus: "connected",
  159. },
  160. ],
  161. });
  162. vi.mocked(settings.service.update).mockRejectedValueOnce(new Error("settings write failed"));
  163. const credentials = createCredentialService({
  164. [legacyPasswordKey]: "saved-password",
  165. });
  166. const createProfileId = vi
  167. .fn<() => string>()
  168. .mockReturnValueOnce(profileId)
  169. .mockReturnValueOnce(replacementProfileId);
  170. const directory = new SourceServerDirectory({
  171. settingService: settings.service,
  172. credentialService: credentials.service,
  173. createProfileId,
  174. });
  175. await expect(directory.initialize()).rejects.toThrow("settings write failed");
  176. expect(credentials.read(firstProfilePasswordKey)).toBeUndefined();
  177. expect(credentials.read(legacyPasswordKey)).toBe("saved-password");
  178. await expect(directory.initialize()).resolves.toBeUndefined();
  179. expect(credentials.read(firstProfilePasswordKey)).toBeUndefined();
  180. expect(credentials.read(replacementProfilePasswordKey)).toBe("saved-password");
  181. expect(settings.read().connectionProfiles?.[0]).toMatchObject({
  182. profileId: replacementProfileId,
  183. target: { passwordCredentialKey: replacementProfilePasswordKey },
  184. });
  185. });
  186. it("waits for every migrated SSH credential write before rolling back a partial failure", async () => {
  187. const legacyPasswordKey = "remote-workspace:legacy:password";
  188. const legacyPassphraseKey = "remote-workspace:legacy:private-key-passphrase";
  189. const passwordKey = `connection-profile:${profileId}:password`;
  190. const passphraseKey = `connection-profile:${profileId}:private-key-passphrase`;
  191. const settings = createSettingService({
  192. lastWorkspaceSession: [
  193. {
  194. kind: "remote",
  195. workspacePath: "/srv/project",
  196. target: {
  197. kind: "ssh",
  198. host: "example.com",
  199. username: "alice",
  200. passwordCredentialKey: legacyPasswordKey,
  201. privateKeyPath: "/home/alice/.ssh/id_ed25519",
  202. privateKeyPassphraseCredentialKey: legacyPassphraseKey,
  203. },
  204. lastOpenedAt: 10,
  205. lastConnectionStatus: "connected",
  206. },
  207. ],
  208. });
  209. const values = new Map([
  210. [legacyPasswordKey, "saved-password"],
  211. [legacyPassphraseKey, "saved-passphrase"],
  212. ]);
  213. let resolvePasswordWrite!: () => void;
  214. const passwordWriteCompleted = new Promise<void>((resolve) => {
  215. resolvePasswordWrite = resolve;
  216. });
  217. const credentialService: ICredentialService = {
  218. load: vi.fn(async (key) => values.get(key) ?? null),
  219. save: vi.fn(async (key, value) => {
  220. if (key === passwordKey) {
  221. await new Promise<void>((resolve) => setImmediate(resolve));
  222. values.set(key, value);
  223. resolvePasswordWrite();
  224. return;
  225. }
  226. if (key === passphraseKey) {
  227. throw new Error("passphrase write failed");
  228. }
  229. values.set(key, value);
  230. }),
  231. delete: vi.fn(async (key) => {
  232. values.delete(key);
  233. }),
  234. };
  235. const directory = new SourceServerDirectory({
  236. settingService: settings.service,
  237. credentialService,
  238. createProfileId: () => profileId,
  239. });
  240. await expect(directory.initialize()).rejects.toThrow("passphrase write failed");
  241. await passwordWriteCompleted;
  242. expect(values.get(passwordKey)).toBeUndefined();
  243. expect(values.get(passphraseKey)).toBeUndefined();
  244. expect(settings.read().sourceServerDirectoryMigrationVersion).toBeUndefined();
  245. });
  246. it("does not persist credentials for a generated Profile excluded from the migration commit", async () => {
  247. const legacyPasswordKey = "remote-workspace:legacy:password";
  248. const excludedPasswordKey = `connection-profile:${replacementProfileId}:password`;
  249. const settings = createSettingService({
  250. connectionProfiles: [
  251. {
  252. profileId,
  253. transport: "wsl",
  254. target: { kind: "wsl", distro: "Ubuntu" },
  255. createdAt: 1,
  256. },
  257. ],
  258. lastWorkspaceSession: [
  259. {
  260. kind: "remote",
  261. workspacePath: "/srv/project",
  262. target: {
  263. kind: "ssh",
  264. host: "example.com",
  265. username: "alice",
  266. passwordCredentialKey: legacyPasswordKey,
  267. },
  268. lastOpenedAt: 10,
  269. lastConnectionStatus: "connected",
  270. },
  271. ],
  272. });
  273. const credentials = createCredentialService({
  274. [legacyPasswordKey]: "saved-password",
  275. });
  276. const directory = new SourceServerDirectory({
  277. settingService: settings.service,
  278. credentialService: credentials.service,
  279. createProfileId: () => replacementProfileId,
  280. });
  281. await directory.initialize();
  282. expect(settings.read().connectionProfiles).toEqual([expect.objectContaining({ profileId })]);
  283. expect(credentials.read(excludedPasswordKey)).toBeUndefined();
  284. expect(credentials.read(legacyPasswordKey)).toBe("saved-password");
  285. });
  286. it("stores new SSH secrets on the Connection Profile and refreshes them on reuse", async () => {
  287. const settings = createSettingService({});
  288. const credentials = createCredentialService({});
  289. const directory = new SourceServerDirectory({
  290. settingService: settings.service,
  291. credentialService: credentials.service,
  292. createProfileId: () => profileId,
  293. now: () => 10,
  294. });
  295. const first = await directory.ensureProfile({
  296. kind: "ssh",
  297. host: "EXAMPLE.com",
  298. username: "alice",
  299. password: "first-password",
  300. privateKeyPath: "/home/alice/.ssh/id_ed25519",
  301. privateKeyPassphrase: "first-passphrase",
  302. });
  303. expect(first.target).toMatchObject({
  304. kind: "ssh",
  305. host: "example.com",
  306. port: 22,
  307. username: "alice",
  308. passwordCredentialKey: `connection-profile:${profileId}:password`,
  309. privateKeyPassphraseCredentialKey: `connection-profile:${profileId}:private-key-passphrase`,
  310. });
  311. expect(credentials.read(`connection-profile:${profileId}:password`)).toBe("first-password");
  312. expect(credentials.read(`connection-profile:${profileId}:private-key-passphrase`)).toBe(
  313. "first-passphrase",
  314. );
  315. const reused = await directory.ensureProfile({
  316. kind: "ssh",
  317. host: "example.com",
  318. port: 22,
  319. username: "alice",
  320. password: "updated-password",
  321. privateKeyPath: "/home/alice/.ssh/id_ed25519",
  322. privateKeyPassphrase: "updated-passphrase",
  323. });
  324. expect(reused.profileId).toBe(profileId);
  325. expect(settings.read().connectionProfiles).toHaveLength(1);
  326. expect(credentials.read(`connection-profile:${profileId}:password`)).toBe("updated-password");
  327. expect(credentials.read(`connection-profile:${profileId}:private-key-passphrase`)).toBe(
  328. "updated-passphrase",
  329. );
  330. });
  331. it("reuses an SSH Profile when only the asset install mode changes", async () => {
  332. const settings = createSettingService({
  333. sourceServerDirectoryMigrationVersion: 1,
  334. connectionProfiles: [
  335. {
  336. profileId,
  337. sourceServerId,
  338. displayName: "linux-amd64-dev",
  339. transport: "ssh",
  340. target: {
  341. kind: "ssh",
  342. host: "localhost",
  343. port: 2222,
  344. username: "dev",
  345. assetInstallMode: "remote-download",
  346. },
  347. createdAt: 1,
  348. lifecycleState: "active",
  349. },
  350. ],
  351. sourceServers: [
  352. {
  353. sourceServerId,
  354. displayName: "linux-amd64-dev",
  355. preferredProfileId: profileId,
  356. installState: "installed",
  357. declaredWorkspaces: [],
  358. },
  359. ],
  360. });
  361. const directory = new SourceServerDirectory({
  362. settingService: settings.service,
  363. credentialService: createCredentialService({}).service,
  364. });
  365. const reused = await directory.ensureProfile({
  366. kind: "ssh",
  367. host: "localhost",
  368. port: 2222,
  369. username: "dev",
  370. assetInstallMode: "local-download-upload",
  371. });
  372. expect(reused.profileId).toBe(profileId);
  373. expect(settings.read().connectionProfiles).toHaveLength(1);
  374. expect(settings.read().connectionProfiles?.[0]).toMatchObject({
  375. profileId,
  376. sourceServerId,
  377. target: {
  378. host: "localhost",
  379. port: 2222,
  380. username: "dev",
  381. assetInstallMode: "local-download-upload",
  382. },
  383. });
  384. });
  385. it("updates an SSH Profile in place without changing its Source or workspace history", async () => {
  386. const passwordKey = `connection-profile:${profileId}:password`;
  387. const passphraseKey = `connection-profile:${profileId}:private-key-passphrase`;
  388. const history = {
  389. profileId,
  390. sourceServerId,
  391. workspacePath: "/srv/project",
  392. workspaceIdentity: `remote:server:${sourceServerId}:/srv/project`,
  393. lastOpenedAt: 10,
  394. };
  395. const settings = createSettingService({
  396. sourceServerDirectoryMigrationVersion: 1,
  397. connectionProfiles: [
  398. {
  399. profileId,
  400. sourceServerId,
  401. transport: "ssh",
  402. displayName: "old connection",
  403. target: {
  404. kind: "ssh",
  405. host: "old.example.com",
  406. port: 22,
  407. username: "old-user",
  408. privateKeyPath: "/old/key",
  409. passwordCredentialKey: passwordKey,
  410. privateKeyPassphraseCredentialKey: passphraseKey,
  411. },
  412. createdAt: 1,
  413. lifecycleState: "active",
  414. },
  415. ],
  416. sourceServers: [
  417. {
  418. sourceServerId,
  419. displayName: "Old device",
  420. preferredProfileId: profileId,
  421. installState: "installed",
  422. declaredWorkspaces: [],
  423. },
  424. ],
  425. sourceServerWorkspaceHistory: [history],
  426. });
  427. const credentials = createCredentialService({
  428. [passwordKey]: "old-password",
  429. [passphraseKey]: "old-passphrase",
  430. });
  431. const directory = new SourceServerDirectory({
  432. settingService: settings.service,
  433. credentialService: credentials.service,
  434. });
  435. await directory.updateProfile({
  436. profileId,
  437. profileDisplayName: "primary connection",
  438. sourceDisplayName: "Build server",
  439. sshAuthMethod: "password",
  440. target: {
  441. kind: "ssh",
  442. host: "NEW.example.com",
  443. port: 2222,
  444. username: "dev",
  445. password: "new-password",
  446. },
  447. });
  448. expect(settings.read().connectionProfiles).toEqual([
  449. expect.objectContaining({
  450. profileId,
  451. sourceServerId,
  452. displayName: "primary connection",
  453. target: expect.objectContaining({
  454. kind: "ssh",
  455. host: "new.example.com",
  456. port: 2222,
  457. username: "dev",
  458. passwordCredentialKey: passwordKey,
  459. }),
  460. }),
  461. ]);
  462. expect(settings.read().sourceServers?.[0]).toMatchObject({
  463. sourceServerId,
  464. displayName: "Build server",
  465. preferredProfileId: profileId,
  466. });
  467. expect(settings.read().sourceServerWorkspaceHistory).toEqual([history]);
  468. expect(credentials.read(passwordKey)).toBe("new-password");
  469. expect(credentials.read(passphraseKey)).toBeUndefined();
  470. });
  471. it("rejects changing the transport of an existing Profile", async () => {
  472. const settings = createSettingService({
  473. sourceServerDirectoryMigrationVersion: 1,
  474. connectionProfiles: [
  475. {
  476. profileId,
  477. sourceServerId,
  478. transport: "ssh",
  479. displayName: "primary",
  480. target: { kind: "ssh", host: "example.com", username: "dev" },
  481. createdAt: 1,
  482. lifecycleState: "active",
  483. },
  484. ],
  485. });
  486. const directory = new SourceServerDirectory({
  487. settingService: settings.service,
  488. credentialService: createCredentialService({}).service,
  489. });
  490. await expect(
  491. directory.updateProfile({
  492. profileId,
  493. profileDisplayName: "primary",
  494. target: { kind: "docker", container: "dev" },
  495. }),
  496. ).rejects.toThrow("transport cannot be changed");
  497. expect(settings.read().connectionProfiles?.[0]).toMatchObject({
  498. profileId,
  499. transport: "ssh",
  500. target: { kind: "ssh", host: "example.com", username: "dev" },
  501. });
  502. });
  503. it("rejects an invalid target without changing the existing Profile", async () => {
  504. const settings = createSettingService({
  505. sourceServerDirectoryMigrationVersion: 1,
  506. connectionProfiles: [
  507. {
  508. profileId,
  509. sourceServerId,
  510. transport: "docker",
  511. displayName: "dev container",
  512. target: { kind: "docker", container: "zcode-dev" },
  513. createdAt: 1,
  514. lifecycleState: "active",
  515. },
  516. ],
  517. });
  518. const directory = new SourceServerDirectory({
  519. settingService: settings.service,
  520. credentialService: createCredentialService({}).service,
  521. });
  522. await expect(
  523. directory.updateProfile({
  524. profileId,
  525. profileDisplayName: "dev container",
  526. target: { kind: "docker", container: " " },
  527. }),
  528. ).rejects.toThrow("Docker container is required");
  529. expect(settings.read().connectionProfiles?.[0]).toMatchObject({
  530. profileId,
  531. target: { kind: "docker", container: "zcode-dev" },
  532. });
  533. });
  534. it("migrates the newest available sibling credential when the latest route entry is dangling", async () => {
  535. const settings = createSettingService({
  536. lastWorkspaceSession: [
  537. {
  538. kind: "remote",
  539. workspacePath: "/srv/a",
  540. workspaceIdentity: "remote:ssh:example.com:22:alice:/srv/a",
  541. target: {
  542. kind: "ssh",
  543. host: "EXAMPLE.com",
  544. username: "alice",
  545. passwordCredentialKey: "remote-workspace:a:password",
  546. },
  547. lastOpenedAt: 10,
  548. lastConnectionStatus: "connected",
  549. },
  550. {
  551. kind: "remote",
  552. workspacePath: "/srv/b",
  553. workspaceIdentity: "remote:ssh:example.com:22:alice:/srv/b",
  554. target: {
  555. kind: "ssh",
  556. host: "example.COM",
  557. port: 22,
  558. username: "alice",
  559. passwordCredentialKey: "remote-workspace:b:password",
  560. },
  561. lastOpenedAt: 20,
  562. lastConnectionStatus: "connected",
  563. },
  564. ],
  565. });
  566. const credentials = createCredentialService({
  567. "remote-workspace:a:password": "older-password",
  568. });
  569. const directory = new SourceServerDirectory({
  570. settingService: settings.service,
  571. credentialService: credentials.service,
  572. createProfileId: () => profileId,
  573. });
  574. await directory.initialize();
  575. await directory.initialize();
  576. expect(settings.read().connectionProfiles).toHaveLength(1);
  577. expect(settings.read().connectionProfiles?.[0]).toMatchObject({
  578. profileId,
  579. transport: "ssh",
  580. target: {
  581. kind: "ssh",
  582. host: "example.com",
  583. port: 22,
  584. username: "alice",
  585. passwordCredentialKey: `connection-profile:${profileId}:password`,
  586. },
  587. });
  588. expect(settings.read().sourceServerWorkspaceHistory).toHaveLength(2);
  589. expect(
  590. settings.read().sourceServerWorkspaceHistory?.map((entry) => entry.legacyWorkspaceIdentity),
  591. ).toEqual(["remote:ssh:example.com:22:alice:/srv/a", "remote:ssh:example.com:22:alice:/srv/b"]);
  592. expect(credentials.read(`connection-profile:${profileId}:password`)).toBe("older-password");
  593. expect(credentials.read("remote-workspace:b:password")).toBeUndefined();
  594. });
  595. it("repairs a migrated Profile whose canonical password is missing from an exact-route legacy sibling", async () => {
  596. const canonicalPasswordKey = `connection-profile:${profileId}:password`;
  597. const settings = createSettingService({
  598. sourceServerDirectoryMigrationVersion: 1,
  599. connectionProfiles: [
  600. {
  601. profileId,
  602. transport: "ssh",
  603. target: {
  604. kind: "ssh",
  605. host: "example.com",
  606. port: 22,
  607. username: "alice",
  608. passwordCredentialKey: canonicalPasswordKey,
  609. },
  610. createdAt: 1,
  611. lifecycleState: "active",
  612. },
  613. ],
  614. lastWorkspaceSession: [
  615. {
  616. kind: "remote",
  617. workspacePath: "/srv/latest",
  618. workspaceIdentity: "remote:ssh:example.com:22:alice:/srv/latest",
  619. target: {
  620. kind: "ssh",
  621. host: "example.com",
  622. port: 22,
  623. username: "alice",
  624. passwordCredentialKey: "remote-workspace:latest:password",
  625. },
  626. lastOpenedAt: 20,
  627. lastConnectionStatus: "connected",
  628. },
  629. {
  630. kind: "remote",
  631. workspacePath: "/srv/older",
  632. workspaceIdentity: "remote:ssh:example.com:22:alice:/srv/older",
  633. target: {
  634. kind: "ssh",
  635. host: "example.com",
  636. port: 22,
  637. username: "alice",
  638. passwordCredentialKey: "remote-workspace:older:password",
  639. },
  640. lastOpenedAt: 10,
  641. lastConnectionStatus: "connected",
  642. },
  643. ],
  644. });
  645. const credentials = createCredentialService({
  646. "remote-workspace:older:password": "recovered-password",
  647. });
  648. const directory = new SourceServerDirectory({
  649. settingService: settings.service,
  650. credentialService: credentials.service,
  651. });
  652. await directory.initialize();
  653. expect(credentials.read(canonicalPasswordKey)).toBe("recovered-password");
  654. expect(settings.read().connectionProfiles?.[0]?.profileId).toBe(profileId);
  655. });
  656. it("does not create a Profile password ref when every exact-route legacy value is missing", async () => {
  657. const settings = createSettingService({
  658. lastWorkspaceSession: [
  659. {
  660. kind: "remote",
  661. workspacePath: "/srv/project",
  662. workspaceIdentity: "remote:ssh:example.com:22:alice:/srv/project",
  663. target: {
  664. kind: "ssh",
  665. host: "example.com",
  666. port: 22,
  667. username: "alice",
  668. passwordCredentialKey: "remote-workspace:missing:password",
  669. },
  670. lastOpenedAt: 10,
  671. lastConnectionStatus: "connected",
  672. },
  673. ],
  674. });
  675. const directory = new SourceServerDirectory({
  676. settingService: settings.service,
  677. credentialService: createCredentialService({}).service,
  678. createProfileId: () => profileId,
  679. });
  680. await directory.initialize();
  681. expect(settings.read().connectionProfiles?.[0]?.target).toEqual({
  682. kind: "ssh",
  683. host: "example.com",
  684. port: 22,
  685. username: "alice",
  686. });
  687. });
  688. it("does not create a Profile passphrase ref when legacy password auth has no private key", async () => {
  689. const legacyPasswordKey = "remote-workspace:legacy:password";
  690. const legacyPassphraseKey = "remote-workspace:legacy:private-key-passphrase";
  691. const settings = createSettingService({
  692. lastWorkspaceSession: [
  693. {
  694. kind: "remote",
  695. workspacePath: "/srv/project",
  696. workspaceIdentity: "remote:ssh:example.com:22:alice:/srv/project",
  697. target: {
  698. kind: "ssh",
  699. host: "example.com",
  700. username: "alice",
  701. passwordCredentialKey: legacyPasswordKey,
  702. privateKeyPassphraseCredentialKey: legacyPassphraseKey,
  703. },
  704. lastOpenedAt: 10,
  705. lastConnectionStatus: "connected",
  706. },
  707. ],
  708. });
  709. const credentials = createCredentialService({
  710. [legacyPasswordKey]: "saved-password",
  711. });
  712. const directory = new SourceServerDirectory({
  713. settingService: settings.service,
  714. credentialService: credentials.service,
  715. createProfileId: () => profileId,
  716. });
  717. await directory.initialize();
  718. expect(settings.read().connectionProfiles?.[0]?.target).toEqual({
  719. kind: "ssh",
  720. host: "example.com",
  721. port: 22,
  722. username: "alice",
  723. passwordCredentialKey: `connection-profile:${profileId}:password`,
  724. });
  725. expect(
  726. credentials.read(`connection-profile:${profileId}:private-key-passphrase`),
  727. ).toBeUndefined();
  728. });
  729. it("binds a successful Profile handshake to one stable Source Server and rekeys its workspace history", async () => {
  730. const settings = createSettingService({
  731. connectionProfiles: [
  732. {
  733. profileId,
  734. transport: "docker",
  735. target: { kind: "docker", container: "zcode-dev" },
  736. createdAt: 1,
  737. },
  738. ],
  739. sourceServerWorkspaceHistory: [
  740. {
  741. workspacePath: "/workspace",
  742. legacyWorkspaceIdentity: "remote:docker:zcode-dev:/workspace",
  743. profileId,
  744. lastOpenedAt: 1,
  745. },
  746. ],
  747. sourceServerDirectoryMigrationVersion: 1,
  748. });
  749. const credentials = createCredentialService({});
  750. const directory = new SourceServerDirectory({
  751. settingService: settings.service,
  752. credentialService: credentials.service,
  753. migrateControllerIdentity: vi.fn(async () => ({
  754. migrationId: "migration-1",
  755. status: "completed",
  756. before: {} as never,
  757. after: {} as never,
  758. })),
  759. });
  760. await directory.initialize();
  761. await directory.recordHandshake(profileId, {
  762. sourceServerId,
  763. osUser: "alice",
  764. hostname: "devbox",
  765. version: "3.8.0",
  766. connectedAt: 100,
  767. });
  768. expect(settings.read().sourceServers).toEqual([
  769. {
  770. sourceServerId,
  771. displayName: "devbox · alice",
  772. osUser: "alice",
  773. hostname: "devbox",
  774. installState: "installed",
  775. lastSeenVersion: "3.8.0",
  776. lastOnlineAt: 100,
  777. preferredProfileId: profileId,
  778. declaredWorkspaces: [],
  779. },
  780. ]);
  781. expect(settings.read().connectionProfiles?.[0]).toMatchObject({
  782. sourceServerId,
  783. lastConnectedAt: 100,
  784. });
  785. expect(settings.read().sourceServerWorkspaceHistory?.[0]).toMatchObject({
  786. sourceServerId,
  787. workspaceIdentity: `remote:server:${sourceServerId}:/workspace`,
  788. legacyWorkspaceIdentity: "remote:docker:zcode-dev:/workspace",
  789. });
  790. });
  791. it("uses the SSH config alias as a new device name without overwriting later user renames", async () => {
  792. const settings = createSettingService({
  793. connectionProfiles: [
  794. {
  795. profileId,
  796. transport: "ssh",
  797. target: {
  798. kind: "ssh",
  799. host: "10.0.0.4",
  800. port: 22,
  801. username: "dev",
  802. sshConfigAlias: " linux-arm64-dev ",
  803. },
  804. createdAt: 1,
  805. },
  806. ],
  807. sourceServerDirectoryMigrationVersion: 1,
  808. });
  809. const directory = new SourceServerDirectory({
  810. settingService: settings.service,
  811. credentialService: createCredentialService({}).service,
  812. });
  813. await directory.recordHandshake(profileId, {
  814. sourceServerId,
  815. hostname: "931b148d01e8",
  816. osUser: "dev",
  817. version: "3.8.0",
  818. });
  819. expect(settings.read().sourceServers?.[0]?.displayName).toBe("linux-arm64-dev");
  820. await directory.renameSource(sourceServerId, "Team build box");
  821. await directory.recordHandshake(profileId, {
  822. sourceServerId,
  823. hostname: "new-hostname",
  824. osUser: "dev",
  825. version: "3.8.1",
  826. });
  827. expect(settings.read().sourceServers?.[0]?.displayName).toBe("Team build box");
  828. });
  829. it("projects successful remote workspaces onto the device record and preserves them on failure", async () => {
  830. const settings = createSettingService({
  831. sourceServerDirectoryMigrationVersion: 1,
  832. connectionProfiles: [
  833. {
  834. profileId,
  835. transport: "docker",
  836. target: { kind: "docker", container: "zcode-dev" },
  837. createdAt: 1,
  838. },
  839. ],
  840. });
  841. const directory = new SourceServerDirectory({
  842. settingService: settings.service,
  843. credentialService: createCredentialService({}).service,
  844. now: () => 100,
  845. });
  846. await directory.recordHandshake(profileId, {
  847. sourceServerId,
  848. version: "3.8.0",
  849. workspaces: [
  850. { path: "/srv/a", label: "A" },
  851. { path: "/srv/b", workspaceIdentity: "invalid-identity" },
  852. ],
  853. });
  854. expect(settings.read().sourceServers?.[0]?.declaredWorkspaces).toEqual([
  855. {
  856. workspacePath: "/srv/a",
  857. workspaceIdentity: `remote:server:${sourceServerId}:/srv/a`,
  858. label: "A",
  859. },
  860. {
  861. workspacePath: "/srv/b",
  862. workspaceIdentity: `remote:server:${sourceServerId}:/srv/b`,
  863. },
  864. ]);
  865. await expect(
  866. directory.recordHandshake("missing-profile", {
  867. sourceServerId,
  868. version: "3.8.0",
  869. workspaces: [],
  870. }),
  871. ).rejects.toThrow();
  872. expect(settings.read().sourceServers?.[0]?.declaredWorkspaces).toHaveLength(2);
  873. });
  874. it("keeps identity-change pending free of writes, then atomically retires the old route", async () => {
  875. const passwordKey = `connection-profile:${profileId}:password`;
  876. const replacementPasswordKey = `connection-profile:${replacementProfileId}:password`;
  877. const settings = createSettingService({
  878. sourceServerDirectoryMigrationVersion: 1,
  879. connectionProfiles: [
  880. {
  881. profileId,
  882. transport: "ssh",
  883. target: {
  884. kind: "ssh",
  885. host: "example.com",
  886. username: "alice",
  887. passwordCredentialKey: passwordKey,
  888. },
  889. sourceServerId,
  890. createdAt: 1,
  891. },
  892. ],
  893. sourceServers: [
  894. {
  895. sourceServerId,
  896. displayName: "old install",
  897. preferredProfileId: profileId,
  898. },
  899. ],
  900. sourceServerWorkspaceHistory: [
  901. {
  902. workspacePath: "/workspace",
  903. workspaceIdentity: `remote:server:${sourceServerId}:/workspace`,
  904. profileId,
  905. sourceServerId,
  906. lastOpenedAt: 1,
  907. },
  908. ],
  909. });
  910. const credentials = createCredentialService({
  911. [passwordKey]: "saved-password",
  912. });
  913. const directory = new SourceServerDirectory({
  914. settingService: settings.service,
  915. credentialService: credentials.service,
  916. createProfileId: () => replacementProfileId,
  917. now: () => 100,
  918. });
  919. const before = structuredClone(settings.read());
  920. await expect(
  921. directory.recordHandshake(profileId, {
  922. sourceServerId: replacementSourceServerId,
  923. version: "3.8.0",
  924. }),
  925. ).resolves.toMatchObject({
  926. identityChanged: true,
  927. effectiveProfileId: profileId,
  928. });
  929. expect(settings.read()).toEqual(before);
  930. expect(credentials.read(replacementPasswordKey)).toBeUndefined();
  931. await expect(
  932. directory.recordHandshake(
  933. profileId,
  934. {
  935. sourceServerId: replacementSourceServerId,
  936. version: "3.8.0",
  937. connectedAt: 100,
  938. },
  939. { confirmIdentityChange: true },
  940. ),
  941. ).resolves.toMatchObject({
  942. identityChanged: true,
  943. effectiveProfileId: replacementProfileId,
  944. });
  945. expect(settings.read().connectionProfiles).toEqual([
  946. expect.objectContaining({
  947. profileId,
  948. lifecycleState: "retired",
  949. retiredReason: "source-reinstalled",
  950. retiredAt: 100,
  951. }),
  952. expect.objectContaining({
  953. profileId: replacementProfileId,
  954. sourceServerId: replacementSourceServerId,
  955. lifecycleState: "active",
  956. target: expect.objectContaining({
  957. passwordCredentialKey: replacementPasswordKey,
  958. }),
  959. }),
  960. ]);
  961. expect(settings.read().sourceServers).toEqual([
  962. expect.objectContaining({
  963. sourceServerId,
  964. installState: "uninstalled",
  965. preferredProfileId: undefined,
  966. }),
  967. expect.objectContaining({
  968. sourceServerId: replacementSourceServerId,
  969. installState: "installed",
  970. preferredProfileId: replacementProfileId,
  971. }),
  972. ]);
  973. expect(settings.read().sourceServerWorkspaceHistory).toEqual(
  974. before.sourceServerWorkspaceHistory,
  975. );
  976. expect(credentials.read(replacementPasswordKey)).toBe("saved-password");
  977. });
  978. it("deduplicates targets against active profiles only", async () => {
  979. const settings = createSettingService({
  980. sourceServerDirectoryMigrationVersion: 1,
  981. connectionProfiles: [
  982. {
  983. profileId,
  984. transport: "docker",
  985. target: { kind: "docker", container: "demo" },
  986. lifecycleState: "retired",
  987. retiredReason: "source-reinstalled",
  988. retiredAt: 2,
  989. createdAt: 1,
  990. },
  991. {
  992. profileId: replacementProfileId,
  993. transport: "docker",
  994. target: { kind: "docker", container: "demo" },
  995. lifecycleState: "active",
  996. createdAt: 2,
  997. },
  998. ],
  999. });
  1000. const directory = new SourceServerDirectory({
  1001. settingService: settings.service,
  1002. credentialService: createCredentialService({}).service,
  1003. });
  1004. await expect(
  1005. directory.ensureProfile({ kind: "docker", container: "demo" }),
  1006. ).resolves.toMatchObject({
  1007. profileId: replacementProfileId,
  1008. lifecycleState: "active",
  1009. });
  1010. });
  1011. it("removes every local record for a device while preserving remote data", async () => {
  1012. const passwordKey = `connection-profile:${profileId}:password`;
  1013. const settings = createSettingService({
  1014. sourceServerDirectoryMigrationVersion: 1,
  1015. sourceServers: [
  1016. {
  1017. sourceServerId,
  1018. displayName: "devbox",
  1019. declaredWorkspaces: [
  1020. {
  1021. workspacePath: "/srv/project",
  1022. workspaceIdentity: `remote:server:${sourceServerId}:/srv/project`,
  1023. },
  1024. ],
  1025. },
  1026. ],
  1027. connectionProfiles: [
  1028. {
  1029. profileId,
  1030. sourceServerId,
  1031. transport: "ssh",
  1032. target: {
  1033. kind: "ssh",
  1034. host: "example.com",
  1035. username: "alice",
  1036. passwordCredentialKey: passwordKey,
  1037. },
  1038. createdAt: 1,
  1039. },
  1040. ],
  1041. sourceServerWorkspaceHistory: [
  1042. {
  1043. workspacePath: "/srv/project",
  1044. workspaceIdentity: `remote:server:${sourceServerId}:/srv/project`,
  1045. sourceServerId,
  1046. profileId,
  1047. lastOpenedAt: 1,
  1048. },
  1049. {
  1050. workspacePath: "/local/other",
  1051. workspaceIdentity: "local:other",
  1052. profileId: replacementProfileId,
  1053. lastOpenedAt: 2,
  1054. },
  1055. ],
  1056. });
  1057. const credentials = createCredentialService({ [passwordKey]: "saved" });
  1058. const directory = new SourceServerDirectory({
  1059. settingService: settings.service,
  1060. credentialService: credentials.service,
  1061. });
  1062. await directory.removeSourceServer(sourceServerId);
  1063. expect(settings.read().sourceServers).toEqual([]);
  1064. expect(settings.read().connectionProfiles).toEqual([]);
  1065. expect(settings.read().sourceServerWorkspaceHistory).toEqual([
  1066. expect.objectContaining({ profileId: replacementProfileId }),
  1067. ]);
  1068. expect(credentials.read(passwordKey)).toBeUndefined();
  1069. });
  1070. it("rolls back replacement credentials when the identity transaction cannot persist", async () => {
  1071. const passwordKey = `connection-profile:${profileId}:password`;
  1072. const replacementPasswordKey = `connection-profile:${replacementProfileId}:password`;
  1073. const settings = createSettingService({
  1074. sourceServerDirectoryMigrationVersion: 1,
  1075. connectionProfiles: [
  1076. {
  1077. profileId,
  1078. transport: "ssh",
  1079. target: {
  1080. kind: "ssh",
  1081. host: "example.com",
  1082. username: "alice",
  1083. passwordCredentialKey: passwordKey,
  1084. },
  1085. sourceServerId,
  1086. createdAt: 1,
  1087. },
  1088. ],
  1089. sourceServers: [{ sourceServerId, displayName: "old", preferredProfileId: profileId }],
  1090. });
  1091. const credentials = createCredentialService({
  1092. [passwordKey]: "saved-password",
  1093. });
  1094. vi.mocked(settings.service.update).mockRejectedValueOnce(new Error("settings write failed"));
  1095. const directory = new SourceServerDirectory({
  1096. settingService: settings.service,
  1097. credentialService: credentials.service,
  1098. createProfileId: () => replacementProfileId,
  1099. now: () => 100,
  1100. });
  1101. const before = structuredClone(settings.read());
  1102. await expect(
  1103. directory.recordHandshake(
  1104. profileId,
  1105. { sourceServerId: replacementSourceServerId, version: "3.8.0" },
  1106. { confirmIdentityChange: true },
  1107. ),
  1108. ).rejects.toThrow("settings write failed");
  1109. expect(settings.read()).toEqual(before);
  1110. expect(credentials.read(replacementPasswordKey)).toBeUndefined();
  1111. expect(credentials.read(passwordKey)).toBe("saved-password");
  1112. });
  1113. it("prefers the preserved legacy workspace identity over the current route fallback", async () => {
  1114. const settings = createSettingService({
  1115. sourceServerDirectoryMigrationVersion: 1,
  1116. sourceServerWorkspaceHistory: [
  1117. {
  1118. workspacePath: "/workspace",
  1119. workspaceIdentity: `remote:server:${sourceServerId}:/workspace`,
  1120. legacyWorkspaceIdentity: "remote:ssh:old.example.com:22:alice:/workspace",
  1121. profileId,
  1122. sourceServerId,
  1123. lastOpenedAt: 1,
  1124. },
  1125. ],
  1126. });
  1127. const directory = new SourceServerDirectory({
  1128. settingService: settings.service,
  1129. credentialService: createCredentialService({}).service,
  1130. });
  1131. await expect(
  1132. directory.resolveLegacyWorkspaceIdentity({
  1133. profileId,
  1134. workspacePath: "/workspace",
  1135. fallback: "remote:ssh:new.example.com:22:alice:/workspace",
  1136. }),
  1137. ).resolves.toBe("remote:ssh:old.example.com:22:alice:/workspace");
  1138. await expect(
  1139. directory.resolveLegacyWorkspaceIdentity({
  1140. profileId,
  1141. workspacePath: "/new-workspace",
  1142. fallback: "remote:ssh:new.example.com:22:alice:/new-workspace",
  1143. }),
  1144. ).resolves.toBe("remote:ssh:new.example.com:22:alice:/new-workspace");
  1145. });
  1146. it("preserves the frozen legacy identity when recording a stable workspace open", async () => {
  1147. const legacyWorkspaceIdentity = "remote:ssh:old.example.com:22:alice:/workspace";
  1148. const stableWorkspaceIdentity = `remote:server:${sourceServerId}:/workspace`;
  1149. const settings = createSettingService({
  1150. sourceServerDirectoryMigrationVersion: 1,
  1151. sourceServerWorkspaceHistory: [
  1152. {
  1153. workspacePath: "/workspace",
  1154. workspaceIdentity: stableWorkspaceIdentity,
  1155. legacyWorkspaceIdentity,
  1156. profileId,
  1157. sourceServerId,
  1158. lastOpenedAt: 1,
  1159. },
  1160. ],
  1161. });
  1162. const directory = new SourceServerDirectory({
  1163. settingService: settings.service,
  1164. credentialService: createCredentialService({}).service,
  1165. now: () => 2,
  1166. });
  1167. await directory.recordWorkspaceOpen({
  1168. profileId,
  1169. sourceServerId,
  1170. workspacePath: "/workspace",
  1171. workspaceIdentity: stableWorkspaceIdentity,
  1172. });
  1173. expect(settings.read().sourceServerWorkspaceHistory).toEqual([
  1174. {
  1175. workspacePath: "/workspace",
  1176. workspaceIdentity: stableWorkspaceIdentity,
  1177. legacyWorkspaceIdentity,
  1178. profileId,
  1179. sourceServerId,
  1180. lastOpenedAt: 2,
  1181. },
  1182. ]);
  1183. });
  1184. it("does not persist workspace history after a bind request is canceled", async () => {
  1185. const settings = createSettingService({
  1186. sourceServerDirectoryMigrationVersion: 1,
  1187. });
  1188. const directory = new SourceServerDirectory({
  1189. settingService: settings.service,
  1190. credentialService: createCredentialService({}).service,
  1191. });
  1192. const controller = new AbortController();
  1193. controller.abort();
  1194. await expect(
  1195. directory.recordWorkspaceOpen({
  1196. profileId,
  1197. sourceServerId,
  1198. workspacePath: "/workspace",
  1199. workspaceIdentity: `remote:server:${sourceServerId}:/workspace`,
  1200. signal: controller.signal,
  1201. }),
  1202. ).rejects.toMatchObject({ name: "AbortError" });
  1203. expect(settings.read().sourceServerWorkspaceHistory).toEqual([]);
  1204. });
  1205. it("returns saved default Profiles regardless of last online age, while requiring silent credentials", async () => {
  1206. const settings = createSettingService({
  1207. connectionProfiles: [
  1208. {
  1209. profileId,
  1210. transport: "ssh",
  1211. target: {
  1212. kind: "ssh",
  1213. host: "example.com",
  1214. port: 22,
  1215. username: "alice",
  1216. passwordCredentialKey: `connection-profile:${profileId}:password`,
  1217. },
  1218. sourceServerId,
  1219. lastConnectedAt: 90,
  1220. createdAt: 1,
  1221. },
  1222. ],
  1223. sourceServers: [
  1224. {
  1225. sourceServerId,
  1226. displayName: "devbox · alice",
  1227. lastOnlineAt: 90,
  1228. preferredProfileId: profileId,
  1229. },
  1230. ],
  1231. sourceServerDirectoryMigrationVersion: 1,
  1232. });
  1233. const credentials = createCredentialService({
  1234. [`connection-profile:${profileId}:password`]: "saved-password",
  1235. });
  1236. const directory = new SourceServerDirectory({
  1237. settingService: settings.service,
  1238. credentialService: credentials.service,
  1239. });
  1240. await expect(directory.listStartupReconnectCandidates()).resolves.toEqual([
  1241. {
  1242. profile: settings.read().connectionProfiles?.[0],
  1243. target: {
  1244. kind: "ssh",
  1245. host: "example.com",
  1246. port: 22,
  1247. username: "alice",
  1248. password: "saved-password",
  1249. },
  1250. },
  1251. ]);
  1252. await expect(
  1253. new SourceServerDirectory({
  1254. settingService: settings.service,
  1255. credentialService: createCredentialService({}).service,
  1256. }).listStartupReconnectCandidates(),
  1257. ).resolves.toEqual([]);
  1258. });
  1259. it("prioritizes the most recently online Sources for bounded startup reconnect workers", async () => {
  1260. const olderProfileId = "4f0f4d21-2798-4608-9d44-889f9f3dc777";
  1261. const olderSourceServerId = "70d16aa4-4404-41f1-99c4-1fe475e21fea";
  1262. const settings = createSettingService({
  1263. connectionProfiles: [
  1264. {
  1265. profileId: olderProfileId,
  1266. transport: "docker",
  1267. target: { kind: "docker", container: "older-source" },
  1268. sourceServerId: olderSourceServerId,
  1269. lastConnectedAt: 10,
  1270. createdAt: 1,
  1271. },
  1272. {
  1273. profileId,
  1274. transport: "docker",
  1275. target: { kind: "docker", container: "recent-source" },
  1276. sourceServerId,
  1277. lastConnectedAt: 90,
  1278. createdAt: 2,
  1279. },
  1280. ],
  1281. sourceServers: [
  1282. {
  1283. sourceServerId: olderSourceServerId,
  1284. displayName: "older-source · root",
  1285. lastOnlineAt: 10,
  1286. preferredProfileId: olderProfileId,
  1287. },
  1288. {
  1289. sourceServerId,
  1290. displayName: "recent-source · root",
  1291. lastOnlineAt: 90,
  1292. preferredProfileId: profileId,
  1293. },
  1294. ],
  1295. sourceServerDirectoryMigrationVersion: 1,
  1296. });
  1297. const directory = new SourceServerDirectory({
  1298. settingService: settings.service,
  1299. credentialService: createCredentialService({}).service,
  1300. });
  1301. await expect(directory.listStartupReconnectCandidates()).resolves.toEqual([
  1302. {
  1303. profile: settings.read().connectionProfiles?.[1],
  1304. target: { kind: "docker", container: "recent-source" },
  1305. },
  1306. {
  1307. profile: settings.read().connectionProfiles?.[0],
  1308. target: { kind: "docker", container: "older-source" },
  1309. },
  1310. ]);
  1311. });
  1312. it("owns Source rename, preferred Profile, target resolution and Profile removal atomically", async () => {
  1313. const secondProfileId = "4f0f4d21-2798-4608-9d44-889f9f3dc777";
  1314. const passwordKey = `connection-profile:${profileId}:password`;
  1315. const settings = createSettingService({
  1316. sourceServerDirectoryMigrationVersion: 1,
  1317. connectionProfiles: [
  1318. {
  1319. profileId,
  1320. transport: "ssh",
  1321. target: {
  1322. kind: "ssh",
  1323. host: "example.com",
  1324. username: "alice",
  1325. passwordCredentialKey: passwordKey,
  1326. },
  1327. sourceServerId,
  1328. createdAt: 1,
  1329. },
  1330. {
  1331. profileId: secondProfileId,
  1332. transport: "docker",
  1333. target: { kind: "docker", container: "dev" },
  1334. sourceServerId,
  1335. createdAt: 2,
  1336. },
  1337. ],
  1338. sourceServers: [
  1339. {
  1340. sourceServerId,
  1341. displayName: "old name",
  1342. preferredProfileId: profileId,
  1343. },
  1344. ],
  1345. sourceServerWorkspaceHistory: [
  1346. {
  1347. workspacePath: "/workspace",
  1348. profileId,
  1349. sourceServerId,
  1350. lastOpenedAt: 1,
  1351. },
  1352. ],
  1353. });
  1354. const credentials = createCredentialService({
  1355. [passwordKey]: "saved-password",
  1356. });
  1357. const directory = new SourceServerDirectory({
  1358. settingService: settings.service,
  1359. credentialService: credentials.service,
  1360. });
  1361. await directory.renameSource(sourceServerId, " renamed ");
  1362. await directory.setPreferredProfile(sourceServerId, secondProfileId);
  1363. await expect(directory.resolveProfileTarget(profileId)).resolves.toMatchObject({
  1364. kind: "ssh",
  1365. host: "example.com",
  1366. username: "alice",
  1367. password: "saved-password",
  1368. });
  1369. await directory.removeProfile(profileId);
  1370. expect(settings.read().sourceServers).toEqual([
  1371. {
  1372. sourceServerId,
  1373. displayName: "renamed",
  1374. installState: "installed",
  1375. preferredProfileId: secondProfileId,
  1376. declaredWorkspaces: [],
  1377. },
  1378. ]);
  1379. expect(settings.read().connectionProfiles?.map((item) => item.profileId)).toEqual([
  1380. secondProfileId,
  1381. ]);
  1382. expect(settings.read().sourceServerWorkspaceHistory).toEqual([]);
  1383. expect(credentials.read(passwordKey)).toBeUndefined();
  1384. });
  1385. it("uses complete stored password auth when a legacy passphrase ref has no private key path", async () => {
  1386. const passwordKey = `connection-profile:${profileId}:password`;
  1387. const stalePassphraseKey = `connection-profile:${profileId}:private-key-passphrase`;
  1388. const settings = createSettingService({
  1389. sourceServerDirectoryMigrationVersion: 1,
  1390. connectionProfiles: [
  1391. {
  1392. profileId,
  1393. transport: "ssh",
  1394. target: {
  1395. kind: "ssh",
  1396. host: "example.com",
  1397. username: "alice",
  1398. passwordCredentialKey: passwordKey,
  1399. privateKeyPassphraseCredentialKey: stalePassphraseKey,
  1400. },
  1401. sourceServerId,
  1402. createdAt: 1,
  1403. },
  1404. ],
  1405. });
  1406. const directory = new SourceServerDirectory({
  1407. settingService: settings.service,
  1408. credentialService: createCredentialService({
  1409. [passwordKey]: "saved-password",
  1410. }).service,
  1411. });
  1412. await expect(directory.resolveProfileTarget(profileId)).resolves.toEqual({
  1413. kind: "ssh",
  1414. host: "example.com",
  1415. username: "alice",
  1416. password: "saved-password",
  1417. });
  1418. });
  1419. it("omits an incomplete encrypted private key when stored password auth is complete", async () => {
  1420. const passwordKey = `connection-profile:${profileId}:password`;
  1421. const missingPassphraseKey = `connection-profile:${profileId}:private-key-passphrase`;
  1422. const settings = createSettingService({
  1423. sourceServerDirectoryMigrationVersion: 1,
  1424. connectionProfiles: [
  1425. {
  1426. profileId,
  1427. transport: "ssh",
  1428. target: {
  1429. kind: "ssh",
  1430. host: "example.com",
  1431. username: "alice",
  1432. privateKeyPath: "/home/alice/.ssh/id_ed25519",
  1433. passwordCredentialKey: passwordKey,
  1434. privateKeyPassphraseCredentialKey: missingPassphraseKey,
  1435. },
  1436. sourceServerId,
  1437. createdAt: 1,
  1438. },
  1439. ],
  1440. });
  1441. const directory = new SourceServerDirectory({
  1442. settingService: settings.service,
  1443. credentialService: createCredentialService({
  1444. [passwordKey]: "saved-password",
  1445. }).service,
  1446. });
  1447. await expect(directory.resolveProfileTarget(profileId)).resolves.toEqual({
  1448. kind: "ssh",
  1449. host: "example.com",
  1450. username: "alice",
  1451. password: "saved-password",
  1452. });
  1453. });
  1454. it("still requires interaction when an encrypted private key is the only auth method and its passphrase is missing", async () => {
  1455. const missingPassphraseKey = `connection-profile:${profileId}:private-key-passphrase`;
  1456. const settings = createSettingService({
  1457. sourceServerDirectoryMigrationVersion: 1,
  1458. connectionProfiles: [
  1459. {
  1460. profileId,
  1461. transport: "ssh",
  1462. target: {
  1463. kind: "ssh",
  1464. host: "example.com",
  1465. username: "alice",
  1466. privateKeyPath: "/home/alice/.ssh/id_ed25519",
  1467. privateKeyPassphraseCredentialKey: missingPassphraseKey,
  1468. },
  1469. sourceServerId,
  1470. createdAt: 1,
  1471. },
  1472. ],
  1473. });
  1474. const directory = new SourceServerDirectory({
  1475. settingService: settings.service,
  1476. credentialService: createCredentialService({}).service,
  1477. });
  1478. await expect(directory.resolveProfileTarget(profileId)).rejects.toThrow(
  1479. "Connection Profile requires interactive credentials",
  1480. );
  1481. await expect(directory.resolveProfileTarget(profileId)).rejects.toMatchObject({
  1482. code: "credentials-required",
  1483. });
  1484. });
  1485. it("uses complete private key auth when a stale password ref has no value", async () => {
  1486. const stalePasswordKey = `connection-profile:${profileId}:password`;
  1487. const settings = createSettingService({
  1488. sourceServerDirectoryMigrationVersion: 1,
  1489. connectionProfiles: [
  1490. {
  1491. profileId,
  1492. transport: "ssh",
  1493. target: {
  1494. kind: "ssh",
  1495. host: "example.com",
  1496. username: "alice",
  1497. privateKeyPath: "/home/alice/.ssh/id_ed25519",
  1498. passwordCredentialKey: stalePasswordKey,
  1499. },
  1500. sourceServerId,
  1501. createdAt: 1,
  1502. },
  1503. ],
  1504. });
  1505. const directory = new SourceServerDirectory({
  1506. settingService: settings.service,
  1507. credentialService: createCredentialService({}).service,
  1508. });
  1509. await expect(directory.resolveProfileTarget(profileId)).resolves.toEqual({
  1510. kind: "ssh",
  1511. host: "example.com",
  1512. username: "alice",
  1513. privateKeyPath: "/home/alice/.ssh/id_ed25519",
  1514. });
  1515. });
  1516. it("keeps valid canonical identities, merges same-path duplicates and truncates at 20 entries", async () => {
  1517. const settings = createSettingService({
  1518. sourceServerDirectoryMigrationVersion: 1,
  1519. connectionProfiles: [
  1520. {
  1521. profileId,
  1522. transport: "docker",
  1523. target: { kind: "docker", container: "zcode-dev" },
  1524. createdAt: 1,
  1525. },
  1526. ],
  1527. });
  1528. const logDebug = vi.fn();
  1529. const directory = new SourceServerDirectory({
  1530. settingService: settings.service,
  1531. credentialService: createCredentialService({}).service,
  1532. logDebug,
  1533. });
  1534. const canonicalA = `remote:server:${sourceServerId}:/srv/a`;
  1535. await directory.recordHandshake(profileId, {
  1536. sourceServerId,
  1537. version: "3.8.0",
  1538. workspaces: [
  1539. // 场景①:远端返回合法设备级 identity,canonical 化后原样保留。
  1540. { path: "/srv/a", workspaceIdentity: canonicalA },
  1541. // 场景④前半:同设备同路径携带线路级 identity,覆盖为 canonical 后与上一条合并。
  1542. {
  1543. path: "/srv/a",
  1544. workspaceIdentity: "remote:ssh:example.com:22:alice:/srv/a",
  1545. },
  1546. // 场景⑧:合并后仍超过 20 条的部分被截断。
  1547. ...Array.from({ length: 21 }, (_, index) => ({
  1548. path: `/srv/bulk-${index}`,
  1549. })),
  1550. ],
  1551. });
  1552. const declared = settings.read().sourceServers?.[0]?.declaredWorkspaces;
  1553. expect(declared).toHaveLength(20);
  1554. expect(declared?.[0]).toEqual({
  1555. workspacePath: "/srv/a",
  1556. workspaceIdentity: canonicalA,
  1557. });
  1558. expect(new Set(declared?.map((entry) => entry.workspaceIdentity)).size).toBe(20);
  1559. // 合同 §8.1.1:identity 被 canonical 覆盖只记 debug 日志、不失败。
  1560. expect(logDebug).toHaveBeenCalledWith(
  1561. expect.stringContaining("remote:ssh:example.com:22:alice:/srv/a"),
  1562. );
  1563. });
  1564. it("keeps same-path workspaces on different devices independent", async () => {
  1565. const settings = createSettingService({
  1566. sourceServerDirectoryMigrationVersion: 1,
  1567. connectionProfiles: [
  1568. {
  1569. profileId,
  1570. sourceServerId,
  1571. transport: "docker",
  1572. target: { kind: "docker", container: "device-a" },
  1573. createdAt: 1,
  1574. },
  1575. {
  1576. profileId: replacementProfileId,
  1577. sourceServerId: replacementSourceServerId,
  1578. transport: "docker",
  1579. target: { kind: "docker", container: "device-b" },
  1580. createdAt: 1,
  1581. },
  1582. ],
  1583. });
  1584. const directory = new SourceServerDirectory({
  1585. settingService: settings.service,
  1586. credentialService: createCredentialService({}).service,
  1587. });
  1588. await directory.recordHandshake(profileId, {
  1589. sourceServerId,
  1590. version: "3.8.0",
  1591. workspaces: [{ path: "/srv/shared" }],
  1592. });
  1593. await directory.recordHandshake(replacementProfileId, {
  1594. sourceServerId: replacementSourceServerId,
  1595. version: "3.8.0",
  1596. workspaces: [{ path: "/srv/shared" }],
  1597. });
  1598. // 场景④后半:workspaceKey 含 serverId,跨设备同路径互不合并、各自独立保存。
  1599. const servers = settings.read().sourceServers ?? [];
  1600. expect(
  1601. servers.find((server) => server.sourceServerId === sourceServerId)?.declaredWorkspaces,
  1602. ).toEqual([
  1603. {
  1604. workspacePath: "/srv/shared",
  1605. workspaceIdentity: `remote:server:${sourceServerId}:/srv/shared`,
  1606. },
  1607. ]);
  1608. expect(
  1609. servers.find((server) => server.sourceServerId === replacementSourceServerId)
  1610. ?.declaredWorkspaces,
  1611. ).toEqual([
  1612. {
  1613. workspacePath: "/srv/shared",
  1614. workspaceIdentity: `remote:server:${replacementSourceServerId}:/srv/shared`,
  1615. },
  1616. ]);
  1617. });
  1618. it("clears declared workspaces on a successful empty handshake", async () => {
  1619. const settings = createSettingService({
  1620. sourceServerDirectoryMigrationVersion: 1,
  1621. connectionProfiles: [
  1622. {
  1623. profileId,
  1624. sourceServerId,
  1625. transport: "docker",
  1626. target: { kind: "docker", container: "zcode-dev" },
  1627. createdAt: 1,
  1628. },
  1629. ],
  1630. sourceServers: [
  1631. {
  1632. sourceServerId,
  1633. displayName: "devbox",
  1634. declaredWorkspaces: [
  1635. {
  1636. workspacePath: "/srv/a",
  1637. workspaceIdentity: `remote:server:${sourceServerId}:/srv/a`,
  1638. },
  1639. ],
  1640. },
  1641. ],
  1642. });
  1643. const directory = new SourceServerDirectory({
  1644. settingService: settings.service,
  1645. credentialService: createCredentialService({}).service,
  1646. });
  1647. // 场景⑤:成功握手返回空数组时整体替换为清空,而非保留旧值。
  1648. await directory.recordHandshake(profileId, {
  1649. sourceServerId,
  1650. version: "3.8.1",
  1651. workspaces: [],
  1652. });
  1653. expect(settings.read().sourceServers?.[0]?.declaredWorkspaces).toEqual([]);
  1654. });
  1655. it("starts the replacement device from empty declared workspaces after an identity change", async () => {
  1656. const legacyWorkspace = {
  1657. workspacePath: "/srv/legacy",
  1658. workspaceIdentity: `remote:server:${sourceServerId}:/srv/legacy`,
  1659. };
  1660. const settings = createSettingService({
  1661. sourceServerDirectoryMigrationVersion: 1,
  1662. connectionProfiles: [
  1663. {
  1664. profileId,
  1665. sourceServerId,
  1666. transport: "docker",
  1667. target: { kind: "docker", container: "zcode-dev" },
  1668. createdAt: 1,
  1669. },
  1670. ],
  1671. sourceServers: [
  1672. {
  1673. sourceServerId,
  1674. displayName: "old-device",
  1675. declaredWorkspaces: [legacyWorkspace],
  1676. },
  1677. ],
  1678. });
  1679. const directory = new SourceServerDirectory({
  1680. settingService: settings.service,
  1681. credentialService: createCredentialService({}).service,
  1682. });
  1683. // 场景⑦:身份换代 = 新设备记录,declaredWorkspaces 从本次握手投影、不继承旧设备。
  1684. await directory.recordHandshake(
  1685. profileId,
  1686. {
  1687. sourceServerId: replacementSourceServerId,
  1688. version: "3.9.0",
  1689. workspaces: [],
  1690. },
  1691. { confirmIdentityChange: true },
  1692. );
  1693. const servers = settings.read().sourceServers ?? [];
  1694. expect(
  1695. servers.find((server) => server.sourceServerId === replacementSourceServerId)
  1696. ?.declaredWorkspaces,
  1697. ).toEqual([]);
  1698. expect(
  1699. servers.find((server) => server.sourceServerId === sourceServerId)?.declaredWorkspaces,
  1700. ).toEqual([legacyWorkspace]);
  1701. });
  1702. it("removes orphan history entries written before identification", async () => {
  1703. const settings = createSettingService({
  1704. sourceServerDirectoryMigrationVersion: 1,
  1705. connectionProfiles: [
  1706. {
  1707. profileId,
  1708. sourceServerId,
  1709. transport: "docker",
  1710. target: { kind: "docker", container: "zcode-dev" },
  1711. createdAt: 1,
  1712. },
  1713. {
  1714. profileId: replacementProfileId,
  1715. transport: "docker",
  1716. target: { kind: "docker", container: "other" },
  1717. createdAt: 1,
  1718. },
  1719. ],
  1720. sourceServerWorkspaceHistory: [
  1721. // Bug 场景:识别前写入的条目没有 sourceServerId,只能通过被删 Profile 关联。
  1722. {
  1723. workspacePath: "/srv/orphan",
  1724. profileId,
  1725. lastOpenedAt: 1,
  1726. },
  1727. {
  1728. workspacePath: "/srv/kept",
  1729. profileId: replacementProfileId,
  1730. lastOpenedAt: 2,
  1731. },
  1732. ],
  1733. });
  1734. const directory = new SourceServerDirectory({
  1735. settingService: settings.service,
  1736. credentialService: createCredentialService({}).service,
  1737. });
  1738. await directory.removeSourceServer(sourceServerId);
  1739. expect(settings.read().sourceServerWorkspaceHistory).toEqual([
  1740. expect.objectContaining({ workspacePath: "/srv/kept" }),
  1741. ]);
  1742. });
  1743. it("keeps device records and restores credentials when removal persistence fails", async () => {
  1744. const passwordKey = `connection-profile:${profileId}:password`;
  1745. const initial = {
  1746. sourceServerDirectoryMigrationVersion: 1 as const,
  1747. sourceServers: [{ sourceServerId, displayName: "devbox" }],
  1748. connectionProfiles: [
  1749. {
  1750. profileId,
  1751. sourceServerId,
  1752. transport: "ssh" as const,
  1753. target: {
  1754. kind: "ssh" as const,
  1755. host: "example.com",
  1756. username: "alice",
  1757. passwordCredentialKey: passwordKey,
  1758. },
  1759. createdAt: 1,
  1760. },
  1761. ],
  1762. sourceServerWorkspaceHistory: [
  1763. {
  1764. workspacePath: "/srv/project",
  1765. profileId,
  1766. sourceServerId,
  1767. lastOpenedAt: 1,
  1768. },
  1769. ],
  1770. };
  1771. const settings = createSettingService(initial);
  1772. const credentials = createCredentialService({ [passwordKey]: "saved" });
  1773. const directory = new SourceServerDirectory({
  1774. settingService: settings.service,
  1775. credentialService: credentials.service,
  1776. });
  1777. vi.mocked(settings.service.update).mockRejectedValueOnce(new Error("disk full"));
  1778. // 合同 §8.1.3:持久化失败进入「设备保留、连接已断开」恢复态,凭据按备份回滚。
  1779. await expect(directory.removeSourceServer(sourceServerId)).rejects.toThrow("disk full");
  1780. expect(settings.read().sourceServers).toHaveLength(1);
  1781. expect(settings.read().connectionProfiles).toHaveLength(1);
  1782. expect(settings.read().sourceServerWorkspaceHistory).toHaveLength(1);
  1783. expect(credentials.read(passwordKey)).toBe("saved");
  1784. });
  1785. it("treats removing an unknown device as a successful no-op without firing change events", async () => {
  1786. const settings = createSettingService({
  1787. sourceServerDirectoryMigrationVersion: 1,
  1788. sourceServers: [{ sourceServerId, displayName: "devbox" }],
  1789. });
  1790. const directory = new SourceServerDirectory({
  1791. settingService: settings.service,
  1792. credentialService: createCredentialService({}).service,
  1793. });
  1794. let changeCount = 0;
  1795. directory.onDidChange(() => {
  1796. changeCount += 1;
  1797. });
  1798. await expect(directory.removeSourceServer(replacementSourceServerId)).resolves.toBeUndefined();
  1799. expect(settings.read().sourceServers).toHaveLength(1);
  1800. // 幂等空操作没有持久化写入,不得驱动消费方权威重读。
  1801. expect(changeCount).toBe(0);
  1802. });
  1803. it("fires onDidChange only after an actual persisted write", async () => {
  1804. const settings = createSettingService({
  1805. sourceServerDirectoryMigrationVersion: 1,
  1806. connectionProfiles: [
  1807. {
  1808. profileId,
  1809. sourceServerId,
  1810. transport: "docker",
  1811. target: { kind: "docker", container: "zcode-dev" },
  1812. createdAt: 1,
  1813. },
  1814. ],
  1815. });
  1816. const directory = new SourceServerDirectory({
  1817. settingService: settings.service,
  1818. credentialService: createCredentialService({}).service,
  1819. });
  1820. let changeCount = 0;
  1821. directory.onDidChange(() => {
  1822. changeCount += 1;
  1823. });
  1824. // 已迁移短路:initialize 无写入,不 fire。
  1825. await directory.initialize();
  1826. expect(changeCount).toBe(0);
  1827. // identity-pending 零写入分支:不 fire。
  1828. await directory.recordHandshake(profileId, {
  1829. sourceServerId: replacementSourceServerId,
  1830. version: "3.9.0",
  1831. });
  1832. expect(changeCount).toBe(0);
  1833. // 目标不存在的 no-op:不 fire。
  1834. await directory.removeProfile(replacementProfileId);
  1835. await directory.markSourceUninstalled(replacementProfileId);
  1836. expect(changeCount).toBe(0);
  1837. // 真实写入:每次持久化成功 fire 一次。
  1838. await directory.recordWorkspaceOpen({
  1839. profileId,
  1840. sourceServerId,
  1841. workspacePath: "/srv/project",
  1842. workspaceIdentity: `remote:server:${sourceServerId}:/srv/project`,
  1843. });
  1844. expect(changeCount).toBe(1);
  1845. });
  1846. });