| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959 |
- import { describe, expect, it, vi } from "vitest";
- import { appSettingsSchema, type AppSettings } from "@zcode/shared";
- import type { ICredentialService, ISettingService } from "@zcode/services";
- import { SourceServerDirectory } from "../src/host/sourceServerDirectory.js";
- const profileId = "17ef20b3-622d-4a53-98c1-0cdff29f3e6d";
- const sourceServerId = "3cd4f9da-cd79-426c-9d70-75f36e9f6a87";
- const replacementSourceServerId = "fbc717a0-86d5-464d-b976-8eb55db6f1db";
- const replacementProfileId = "8a116c93-1306-48a8-b8bf-4bdc318b8684";
- function createSettingService(initial: Partial<AppSettings>): {
- service: ISettingService;
- read: () => AppSettings;
- } {
- let settings = appSettingsSchema.parse(initial);
- return {
- read: () => settings,
- service: {
- get: vi.fn(async () => settings),
- update: vi.fn(async (patch) => {
- settings = appSettingsSchema.parse({ ...settings, ...patch });
- }),
- updateDataBaseDir: vi.fn(),
- },
- };
- }
- function createCredentialService(initial: Record<string, string>): {
- service: ICredentialService;
- read: (key: string) => string | undefined;
- } {
- const values = new Map(Object.entries(initial));
- return {
- read: (key) => values.get(key),
- service: {
- load: vi.fn(async (key) => values.get(key) ?? null),
- save: vi.fn(async (key, value) => {
- values.set(key, value);
- }),
- delete: vi.fn(async (key) => {
- values.delete(key);
- }),
- },
- };
- }
- describe("SourceServerDirectory", () => {
- it("includes saved profiles that have never completed their first handshake", async () => {
- const settings = createSettingService({
- sourceServerDirectoryMigrationVersion: 1,
- connectionProfiles: [
- {
- profileId,
- transport: "docker",
- target: { kind: "docker", container: "new" },
- createdAt: 1,
- },
- ],
- });
- const directory = new SourceServerDirectory({
- settingService: settings.service,
- credentialService: createCredentialService({}).service,
- });
- await expect(directory.listStartupReconnectCandidates()).resolves.toEqual([
- {
- profile: settings.read().connectionProfiles?.[0],
- target: { kind: "docker", container: "new" },
- },
- ]);
- });
- it("最近在线保存最后在线时刻,迟到写入不能倒退或复活已移除设备", async () => {
- const settings = createSettingService({
- sourceServers: [
- { sourceServerId, displayName: "Build", installState: "installed", lastOnlineAt: 1000 },
- ],
- });
- const directory = new SourceServerDirectory({
- settingService: settings.service,
- credentialService: createCredentialService({}).service,
- });
- await directory.recordLastOnline(sourceServerId, 9000);
- await directory.recordLastOnline(sourceServerId, 4000);
- expect((await directory.snapshot()).servers[0]?.lastOnlineAt).toBe(9000);
- await directory.removeSourceServer(sourceServerId);
- await directory.recordLastOnline(sourceServerId, 12000);
- expect((await directory.snapshot()).servers).toEqual([]);
- });
- it("stores a Source-scoped conversation backing path outside strict project history", async () => {
- const conversationPath = "/home/dev/.zcode/workspace/default";
- const realProject = {
- profileId,
- sourceServerId,
- workspacePath: "/home/dev/project",
- workspaceIdentity: `remote:server:${sourceServerId}:/home/dev/project`,
- lastOpenedAt: 10,
- };
- const otherSourceConversationPath = {
- profileId: replacementProfileId,
- sourceServerId: replacementSourceServerId,
- workspacePath: conversationPath,
- workspaceIdentity: `remote:server:${replacementSourceServerId}:${conversationPath}`,
- lastOpenedAt: 11,
- };
- const settings = createSettingService({
- sourceServerWorkspaceHistory: [
- {
- profileId,
- sourceServerId,
- workspacePath: conversationPath,
- workspaceIdentity: `remote:server:${sourceServerId}:${conversationPath}`,
- lastOpenedAt: 9,
- },
- realProject,
- otherSourceConversationPath,
- ],
- });
- const directory = new SourceServerDirectory({
- settingService: settings.service,
- credentialService: createCredentialService({}).service,
- now: () => 42,
- });
- await directory.recordConversationWorkspace(sourceServerId, conversationPath);
- expect(settings.read().remoteConversationWorkspaceBySourceServerId).toEqual({
- [sourceServerId]: {
- workspacePath: conversationPath,
- lastResolvedAt: 42,
- },
- });
- expect(settings.read().sourceServerWorkspaceHistory).toEqual([
- realProject,
- otherSourceConversationPath,
- ]);
- });
- it("allows a failed initialization flight to be retried", async () => {
- const settings = createSettingService({});
- vi.mocked(settings.service.get).mockRejectedValueOnce(new Error("settings temporarily locked"));
- const directory = new SourceServerDirectory({
- settingService: settings.service,
- credentialService: createCredentialService({}).service,
- });
- await expect(directory.initialize()).rejects.toThrow("settings temporarily locked");
- await expect(directory.initialize()).resolves.toBeUndefined();
- expect(settings.read().sourceServerDirectoryMigrationVersion).toBe(1);
- });
- it("rolls back migrated SSH credentials when settings persistence fails before a retry", async () => {
- const legacyPasswordKey = "remote-workspace:legacy:password";
- const firstProfilePasswordKey = `connection-profile:${profileId}:password`;
- const replacementProfilePasswordKey = `connection-profile:${replacementProfileId}:password`;
- const settings = createSettingService({
- lastWorkspaceSession: [
- {
- kind: "remote",
- workspacePath: "/srv/project",
- workspaceIdentity: "remote:ssh:example.com:22:alice:/srv/project",
- target: {
- kind: "ssh",
- host: "example.com",
- username: "alice",
- passwordCredentialKey: legacyPasswordKey,
- },
- lastOpenedAt: 10,
- lastConnectionStatus: "connected",
- },
- ],
- });
- vi.mocked(settings.service.update).mockRejectedValueOnce(new Error("settings write failed"));
- const credentials = createCredentialService({
- [legacyPasswordKey]: "saved-password",
- });
- const createProfileId = vi
- .fn<() => string>()
- .mockReturnValueOnce(profileId)
- .mockReturnValueOnce(replacementProfileId);
- const directory = new SourceServerDirectory({
- settingService: settings.service,
- credentialService: credentials.service,
- createProfileId,
- });
- await expect(directory.initialize()).rejects.toThrow("settings write failed");
- expect(credentials.read(firstProfilePasswordKey)).toBeUndefined();
- expect(credentials.read(legacyPasswordKey)).toBe("saved-password");
- await expect(directory.initialize()).resolves.toBeUndefined();
- expect(credentials.read(firstProfilePasswordKey)).toBeUndefined();
- expect(credentials.read(replacementProfilePasswordKey)).toBe("saved-password");
- expect(settings.read().connectionProfiles?.[0]).toMatchObject({
- profileId: replacementProfileId,
- target: { passwordCredentialKey: replacementProfilePasswordKey },
- });
- });
- it("waits for every migrated SSH credential write before rolling back a partial failure", async () => {
- const legacyPasswordKey = "remote-workspace:legacy:password";
- const legacyPassphraseKey = "remote-workspace:legacy:private-key-passphrase";
- const passwordKey = `connection-profile:${profileId}:password`;
- const passphraseKey = `connection-profile:${profileId}:private-key-passphrase`;
- const settings = createSettingService({
- lastWorkspaceSession: [
- {
- kind: "remote",
- workspacePath: "/srv/project",
- target: {
- kind: "ssh",
- host: "example.com",
- username: "alice",
- passwordCredentialKey: legacyPasswordKey,
- privateKeyPath: "/home/alice/.ssh/id_ed25519",
- privateKeyPassphraseCredentialKey: legacyPassphraseKey,
- },
- lastOpenedAt: 10,
- lastConnectionStatus: "connected",
- },
- ],
- });
- const values = new Map([
- [legacyPasswordKey, "saved-password"],
- [legacyPassphraseKey, "saved-passphrase"],
- ]);
- let resolvePasswordWrite!: () => void;
- const passwordWriteCompleted = new Promise<void>((resolve) => {
- resolvePasswordWrite = resolve;
- });
- const credentialService: ICredentialService = {
- load: vi.fn(async (key) => values.get(key) ?? null),
- save: vi.fn(async (key, value) => {
- if (key === passwordKey) {
- await new Promise<void>((resolve) => setImmediate(resolve));
- values.set(key, value);
- resolvePasswordWrite();
- return;
- }
- if (key === passphraseKey) {
- throw new Error("passphrase write failed");
- }
- values.set(key, value);
- }),
- delete: vi.fn(async (key) => {
- values.delete(key);
- }),
- };
- const directory = new SourceServerDirectory({
- settingService: settings.service,
- credentialService,
- createProfileId: () => profileId,
- });
- await expect(directory.initialize()).rejects.toThrow("passphrase write failed");
- await passwordWriteCompleted;
- expect(values.get(passwordKey)).toBeUndefined();
- expect(values.get(passphraseKey)).toBeUndefined();
- expect(settings.read().sourceServerDirectoryMigrationVersion).toBeUndefined();
- });
- it("does not persist credentials for a generated Profile excluded from the migration commit", async () => {
- const legacyPasswordKey = "remote-workspace:legacy:password";
- const excludedPasswordKey = `connection-profile:${replacementProfileId}:password`;
- const settings = createSettingService({
- connectionProfiles: [
- {
- profileId,
- transport: "wsl",
- target: { kind: "wsl", distro: "Ubuntu" },
- createdAt: 1,
- },
- ],
- lastWorkspaceSession: [
- {
- kind: "remote",
- workspacePath: "/srv/project",
- target: {
- kind: "ssh",
- host: "example.com",
- username: "alice",
- passwordCredentialKey: legacyPasswordKey,
- },
- lastOpenedAt: 10,
- lastConnectionStatus: "connected",
- },
- ],
- });
- const credentials = createCredentialService({
- [legacyPasswordKey]: "saved-password",
- });
- const directory = new SourceServerDirectory({
- settingService: settings.service,
- credentialService: credentials.service,
- createProfileId: () => replacementProfileId,
- });
- await directory.initialize();
- expect(settings.read().connectionProfiles).toEqual([expect.objectContaining({ profileId })]);
- expect(credentials.read(excludedPasswordKey)).toBeUndefined();
- expect(credentials.read(legacyPasswordKey)).toBe("saved-password");
- });
- it("stores new SSH secrets on the Connection Profile and refreshes them on reuse", async () => {
- const settings = createSettingService({});
- const credentials = createCredentialService({});
- const directory = new SourceServerDirectory({
- settingService: settings.service,
- credentialService: credentials.service,
- createProfileId: () => profileId,
- now: () => 10,
- });
- const first = await directory.ensureProfile({
- kind: "ssh",
- host: "EXAMPLE.com",
- username: "alice",
- password: "first-password",
- privateKeyPath: "/home/alice/.ssh/id_ed25519",
- privateKeyPassphrase: "first-passphrase",
- });
- expect(first.target).toMatchObject({
- kind: "ssh",
- host: "example.com",
- port: 22,
- username: "alice",
- passwordCredentialKey: `connection-profile:${profileId}:password`,
- privateKeyPassphraseCredentialKey: `connection-profile:${profileId}:private-key-passphrase`,
- });
- expect(credentials.read(`connection-profile:${profileId}:password`)).toBe("first-password");
- expect(credentials.read(`connection-profile:${profileId}:private-key-passphrase`)).toBe(
- "first-passphrase",
- );
- const reused = await directory.ensureProfile({
- kind: "ssh",
- host: "example.com",
- port: 22,
- username: "alice",
- password: "updated-password",
- privateKeyPath: "/home/alice/.ssh/id_ed25519",
- privateKeyPassphrase: "updated-passphrase",
- });
- expect(reused.profileId).toBe(profileId);
- expect(settings.read().connectionProfiles).toHaveLength(1);
- expect(credentials.read(`connection-profile:${profileId}:password`)).toBe("updated-password");
- expect(credentials.read(`connection-profile:${profileId}:private-key-passphrase`)).toBe(
- "updated-passphrase",
- );
- });
- it("reuses an SSH Profile when only the asset install mode changes", async () => {
- const settings = createSettingService({
- sourceServerDirectoryMigrationVersion: 1,
- connectionProfiles: [
- {
- profileId,
- sourceServerId,
- displayName: "linux-amd64-dev",
- transport: "ssh",
- target: {
- kind: "ssh",
- host: "localhost",
- port: 2222,
- username: "dev",
- assetInstallMode: "remote-download",
- },
- createdAt: 1,
- lifecycleState: "active",
- },
- ],
- sourceServers: [
- {
- sourceServerId,
- displayName: "linux-amd64-dev",
- preferredProfileId: profileId,
- installState: "installed",
- declaredWorkspaces: [],
- },
- ],
- });
- const directory = new SourceServerDirectory({
- settingService: settings.service,
- credentialService: createCredentialService({}).service,
- });
- const reused = await directory.ensureProfile({
- kind: "ssh",
- host: "localhost",
- port: 2222,
- username: "dev",
- assetInstallMode: "local-download-upload",
- });
- expect(reused.profileId).toBe(profileId);
- expect(settings.read().connectionProfiles).toHaveLength(1);
- expect(settings.read().connectionProfiles?.[0]).toMatchObject({
- profileId,
- sourceServerId,
- target: {
- host: "localhost",
- port: 2222,
- username: "dev",
- assetInstallMode: "local-download-upload",
- },
- });
- });
- it("updates an SSH Profile in place without changing its Source or workspace history", async () => {
- const passwordKey = `connection-profile:${profileId}:password`;
- const passphraseKey = `connection-profile:${profileId}:private-key-passphrase`;
- const history = {
- profileId,
- sourceServerId,
- workspacePath: "/srv/project",
- workspaceIdentity: `remote:server:${sourceServerId}:/srv/project`,
- lastOpenedAt: 10,
- };
- const settings = createSettingService({
- sourceServerDirectoryMigrationVersion: 1,
- connectionProfiles: [
- {
- profileId,
- sourceServerId,
- transport: "ssh",
- displayName: "old connection",
- target: {
- kind: "ssh",
- host: "old.example.com",
- port: 22,
- username: "old-user",
- privateKeyPath: "/old/key",
- passwordCredentialKey: passwordKey,
- privateKeyPassphraseCredentialKey: passphraseKey,
- },
- createdAt: 1,
- lifecycleState: "active",
- },
- ],
- sourceServers: [
- {
- sourceServerId,
- displayName: "Old device",
- preferredProfileId: profileId,
- installState: "installed",
- declaredWorkspaces: [],
- },
- ],
- sourceServerWorkspaceHistory: [history],
- });
- const credentials = createCredentialService({
- [passwordKey]: "old-password",
- [passphraseKey]: "old-passphrase",
- });
- const directory = new SourceServerDirectory({
- settingService: settings.service,
- credentialService: credentials.service,
- });
- await directory.updateProfile({
- profileId,
- profileDisplayName: "primary connection",
- sourceDisplayName: "Build server",
- sshAuthMethod: "password",
- target: {
- kind: "ssh",
- host: "NEW.example.com",
- port: 2222,
- username: "dev",
- password: "new-password",
- },
- });
- expect(settings.read().connectionProfiles).toEqual([
- expect.objectContaining({
- profileId,
- sourceServerId,
- displayName: "primary connection",
- target: expect.objectContaining({
- kind: "ssh",
- host: "new.example.com",
- port: 2222,
- username: "dev",
- passwordCredentialKey: passwordKey,
- }),
- }),
- ]);
- expect(settings.read().sourceServers?.[0]).toMatchObject({
- sourceServerId,
- displayName: "Build server",
- preferredProfileId: profileId,
- });
- expect(settings.read().sourceServerWorkspaceHistory).toEqual([history]);
- expect(credentials.read(passwordKey)).toBe("new-password");
- expect(credentials.read(passphraseKey)).toBeUndefined();
- });
- it("rejects changing the transport of an existing Profile", async () => {
- const settings = createSettingService({
- sourceServerDirectoryMigrationVersion: 1,
- connectionProfiles: [
- {
- profileId,
- sourceServerId,
- transport: "ssh",
- displayName: "primary",
- target: { kind: "ssh", host: "example.com", username: "dev" },
- createdAt: 1,
- lifecycleState: "active",
- },
- ],
- });
- const directory = new SourceServerDirectory({
- settingService: settings.service,
- credentialService: createCredentialService({}).service,
- });
- await expect(
- directory.updateProfile({
- profileId,
- profileDisplayName: "primary",
- target: { kind: "docker", container: "dev" },
- }),
- ).rejects.toThrow("transport cannot be changed");
- expect(settings.read().connectionProfiles?.[0]).toMatchObject({
- profileId,
- transport: "ssh",
- target: { kind: "ssh", host: "example.com", username: "dev" },
- });
- });
- it("rejects an invalid target without changing the existing Profile", async () => {
- const settings = createSettingService({
- sourceServerDirectoryMigrationVersion: 1,
- connectionProfiles: [
- {
- profileId,
- sourceServerId,
- transport: "docker",
- displayName: "dev container",
- target: { kind: "docker", container: "zcode-dev" },
- createdAt: 1,
- lifecycleState: "active",
- },
- ],
- });
- const directory = new SourceServerDirectory({
- settingService: settings.service,
- credentialService: createCredentialService({}).service,
- });
- await expect(
- directory.updateProfile({
- profileId,
- profileDisplayName: "dev container",
- target: { kind: "docker", container: " " },
- }),
- ).rejects.toThrow("Docker container is required");
- expect(settings.read().connectionProfiles?.[0]).toMatchObject({
- profileId,
- target: { kind: "docker", container: "zcode-dev" },
- });
- });
- it("migrates the newest available sibling credential when the latest route entry is dangling", async () => {
- const settings = createSettingService({
- lastWorkspaceSession: [
- {
- kind: "remote",
- workspacePath: "/srv/a",
- workspaceIdentity: "remote:ssh:example.com:22:alice:/srv/a",
- target: {
- kind: "ssh",
- host: "EXAMPLE.com",
- username: "alice",
- passwordCredentialKey: "remote-workspace:a:password",
- },
- lastOpenedAt: 10,
- lastConnectionStatus: "connected",
- },
- {
- kind: "remote",
- workspacePath: "/srv/b",
- workspaceIdentity: "remote:ssh:example.com:22:alice:/srv/b",
- target: {
- kind: "ssh",
- host: "example.COM",
- port: 22,
- username: "alice",
- passwordCredentialKey: "remote-workspace:b:password",
- },
- lastOpenedAt: 20,
- lastConnectionStatus: "connected",
- },
- ],
- });
- const credentials = createCredentialService({
- "remote-workspace:a:password": "older-password",
- });
- const directory = new SourceServerDirectory({
- settingService: settings.service,
- credentialService: credentials.service,
- createProfileId: () => profileId,
- });
- await directory.initialize();
- await directory.initialize();
- expect(settings.read().connectionProfiles).toHaveLength(1);
- expect(settings.read().connectionProfiles?.[0]).toMatchObject({
- profileId,
- transport: "ssh",
- target: {
- kind: "ssh",
- host: "example.com",
- port: 22,
- username: "alice",
- passwordCredentialKey: `connection-profile:${profileId}:password`,
- },
- });
- expect(settings.read().sourceServerWorkspaceHistory).toHaveLength(2);
- expect(
- settings.read().sourceServerWorkspaceHistory?.map((entry) => entry.legacyWorkspaceIdentity),
- ).toEqual(["remote:ssh:example.com:22:alice:/srv/a", "remote:ssh:example.com:22:alice:/srv/b"]);
- expect(credentials.read(`connection-profile:${profileId}:password`)).toBe("older-password");
- expect(credentials.read("remote-workspace:b:password")).toBeUndefined();
- });
- it("repairs a migrated Profile whose canonical password is missing from an exact-route legacy sibling", async () => {
- const canonicalPasswordKey = `connection-profile:${profileId}:password`;
- const settings = createSettingService({
- sourceServerDirectoryMigrationVersion: 1,
- connectionProfiles: [
- {
- profileId,
- transport: "ssh",
- target: {
- kind: "ssh",
- host: "example.com",
- port: 22,
- username: "alice",
- passwordCredentialKey: canonicalPasswordKey,
- },
- createdAt: 1,
- lifecycleState: "active",
- },
- ],
- lastWorkspaceSession: [
- {
- kind: "remote",
- workspacePath: "/srv/latest",
- workspaceIdentity: "remote:ssh:example.com:22:alice:/srv/latest",
- target: {
- kind: "ssh",
- host: "example.com",
- port: 22,
- username: "alice",
- passwordCredentialKey: "remote-workspace:latest:password",
- },
- lastOpenedAt: 20,
- lastConnectionStatus: "connected",
- },
- {
- kind: "remote",
- workspacePath: "/srv/older",
- workspaceIdentity: "remote:ssh:example.com:22:alice:/srv/older",
- target: {
- kind: "ssh",
- host: "example.com",
- port: 22,
- username: "alice",
- passwordCredentialKey: "remote-workspace:older:password",
- },
- lastOpenedAt: 10,
- lastConnectionStatus: "connected",
- },
- ],
- });
- const credentials = createCredentialService({
- "remote-workspace:older:password": "recovered-password",
- });
- const directory = new SourceServerDirectory({
- settingService: settings.service,
- credentialService: credentials.service,
- });
- await directory.initialize();
- expect(credentials.read(canonicalPasswordKey)).toBe("recovered-password");
- expect(settings.read().connectionProfiles?.[0]?.profileId).toBe(profileId);
- });
- it("does not create a Profile password ref when every exact-route legacy value is missing", async () => {
- const settings = createSettingService({
- lastWorkspaceSession: [
- {
- kind: "remote",
- workspacePath: "/srv/project",
- workspaceIdentity: "remote:ssh:example.com:22:alice:/srv/project",
- target: {
- kind: "ssh",
- host: "example.com",
- port: 22,
- username: "alice",
- passwordCredentialKey: "remote-workspace:missing:password",
- },
- lastOpenedAt: 10,
- lastConnectionStatus: "connected",
- },
- ],
- });
- const directory = new SourceServerDirectory({
- settingService: settings.service,
- credentialService: createCredentialService({}).service,
- createProfileId: () => profileId,
- });
- await directory.initialize();
- expect(settings.read().connectionProfiles?.[0]?.target).toEqual({
- kind: "ssh",
- host: "example.com",
- port: 22,
- username: "alice",
- });
- });
- it("does not create a Profile passphrase ref when legacy password auth has no private key", async () => {
- const legacyPasswordKey = "remote-workspace:legacy:password";
- const legacyPassphraseKey = "remote-workspace:legacy:private-key-passphrase";
- const settings = createSettingService({
- lastWorkspaceSession: [
- {
- kind: "remote",
- workspacePath: "/srv/project",
- workspaceIdentity: "remote:ssh:example.com:22:alice:/srv/project",
- target: {
- kind: "ssh",
- host: "example.com",
- username: "alice",
- passwordCredentialKey: legacyPasswordKey,
- privateKeyPassphraseCredentialKey: legacyPassphraseKey,
- },
- lastOpenedAt: 10,
- lastConnectionStatus: "connected",
- },
- ],
- });
- const credentials = createCredentialService({
- [legacyPasswordKey]: "saved-password",
- });
- const directory = new SourceServerDirectory({
- settingService: settings.service,
- credentialService: credentials.service,
- createProfileId: () => profileId,
- });
- await directory.initialize();
- expect(settings.read().connectionProfiles?.[0]?.target).toEqual({
- kind: "ssh",
- host: "example.com",
- port: 22,
- username: "alice",
- passwordCredentialKey: `connection-profile:${profileId}:password`,
- });
- expect(
- credentials.read(`connection-profile:${profileId}:private-key-passphrase`),
- ).toBeUndefined();
- });
- it("binds a successful Profile handshake to one stable Source Server and rekeys its workspace history", async () => {
- const settings = createSettingService({
- connectionProfiles: [
- {
- profileId,
- transport: "docker",
- target: { kind: "docker", container: "zcode-dev" },
- createdAt: 1,
- },
- ],
- sourceServerWorkspaceHistory: [
- {
- workspacePath: "/workspace",
- legacyWorkspaceIdentity: "remote:docker:zcode-dev:/workspace",
- profileId,
- lastOpenedAt: 1,
- },
- ],
- sourceServerDirectoryMigrationVersion: 1,
- });
- const credentials = createCredentialService({});
- const directory = new SourceServerDirectory({
- settingService: settings.service,
- credentialService: credentials.service,
- migrateControllerIdentity: vi.fn(async () => ({
- migrationId: "migration-1",
- status: "completed",
- before: {} as never,
- after: {} as never,
- })),
- });
- await directory.initialize();
- await directory.recordHandshake(profileId, {
- sourceServerId,
- osUser: "alice",
- hostname: "devbox",
- version: "3.8.0",
- connectedAt: 100,
- });
- expect(settings.read().sourceServers).toEqual([
- {
- sourceServerId,
- displayName: "devbox · alice",
- osUser: "alice",
- hostname: "devbox",
- installState: "installed",
- lastSeenVersion: "3.8.0",
- lastOnlineAt: 100,
- preferredProfileId: profileId,
- declaredWorkspaces: [],
- },
- ]);
- expect(settings.read().connectionProfiles?.[0]).toMatchObject({
- sourceServerId,
- lastConnectedAt: 100,
- });
- expect(settings.read().sourceServerWorkspaceHistory?.[0]).toMatchObject({
- sourceServerId,
- workspaceIdentity: `remote:server:${sourceServerId}:/workspace`,
- legacyWorkspaceIdentity: "remote:docker:zcode-dev:/workspace",
- });
- });
- it("uses the SSH config alias as a new device name without overwriting later user renames", async () => {
- const settings = createSettingService({
- connectionProfiles: [
- {
- profileId,
- transport: "ssh",
- target: {
- kind: "ssh",
- host: "10.0.0.4",
- port: 22,
- username: "dev",
- sshConfigAlias: " linux-arm64-dev ",
- },
- createdAt: 1,
- },
- ],
- sourceServerDirectoryMigrationVersion: 1,
- });
- const directory = new SourceServerDirectory({
- settingService: settings.service,
- credentialService: createCredentialService({}).service,
- });
- await directory.recordHandshake(profileId, {
- sourceServerId,
- hostname: "931b148d01e8",
- osUser: "dev",
- version: "3.8.0",
- });
- expect(settings.read().sourceServers?.[0]?.displayName).toBe("linux-arm64-dev");
- await directory.renameSource(sourceServerId, "Team build box");
- await directory.recordHandshake(profileId, {
- sourceServerId,
- hostname: "new-hostname",
- osUser: "dev",
- version: "3.8.1",
- });
- expect(settings.read().sourceServers?.[0]?.displayName).toBe("Team build box");
- });
- it("projects successful remote workspaces onto the device record and preserves them on failure", async () => {
- const settings = createSettingService({
- sourceServerDirectoryMigrationVersion: 1,
- connectionProfiles: [
- {
- profileId,
- transport: "docker",
- target: { kind: "docker", container: "zcode-dev" },
- createdAt: 1,
- },
- ],
- });
- const directory = new SourceServerDirectory({
- settingService: settings.service,
- credentialService: createCredentialService({}).service,
- now: () => 100,
- });
- await directory.recordHandshake(profileId, {
- sourceServerId,
- version: "3.8.0",
- workspaces: [
- { path: "/srv/a", label: "A" },
- { path: "/srv/b", workspaceIdentity: "invalid-identity" },
- ],
- });
- expect(settings.read().sourceServers?.[0]?.declaredWorkspaces).toEqual([
- {
- workspacePath: "/srv/a",
- workspaceIdentity: `remote:server:${sourceServerId}:/srv/a`,
- label: "A",
- },
- {
- workspacePath: "/srv/b",
- workspaceIdentity: `remote:server:${sourceServerId}:/srv/b`,
- },
- ]);
- await expect(
- directory.recordHandshake("missing-profile", {
- sourceServerId,
- version: "3.8.0",
- workspaces: [],
- }),
- ).rejects.toThrow();
- expect(settings.read().sourceServers?.[0]?.declaredWorkspaces).toHaveLength(2);
- });
- it("keeps identity-change pending free of writes, then atomically retires the old route", async () => {
- const passwordKey = `connection-profile:${profileId}:password`;
- const replacementPasswordKey = `connection-profile:${replacementProfileId}:password`;
- const settings = createSettingService({
- sourceServerDirectoryMigrationVersion: 1,
- connectionProfiles: [
- {
- profileId,
- transport: "ssh",
- target: {
- kind: "ssh",
- host: "example.com",
- username: "alice",
- passwordCredentialKey: passwordKey,
- },
- sourceServerId,
- createdAt: 1,
- },
- ],
- sourceServers: [
- {
- sourceServerId,
- displayName: "old install",
- preferredProfileId: profileId,
- },
- ],
- sourceServerWorkspaceHistory: [
- {
- workspacePath: "/workspace",
- workspaceIdentity: `remote:server:${sourceServerId}:/workspace`,
- profileId,
- sourceServerId,
- lastOpenedAt: 1,
- },
- ],
- });
- const credentials = createCredentialService({
- [passwordKey]: "saved-password",
- });
- const directory = new SourceServerDirectory({
- settingService: settings.service,
- credentialService: credentials.service,
- createProfileId: () => replacementProfileId,
- now: () => 100,
- });
- const before = structuredClone(settings.read());
- await expect(
- directory.recordHandshake(profileId, {
- sourceServerId: replacementSourceServerId,
- version: "3.8.0",
- }),
- ).resolves.toMatchObject({
- identityChanged: true,
- effectiveProfileId: profileId,
- });
- expect(settings.read()).toEqual(before);
- expect(credentials.read(replacementPasswordKey)).toBeUndefined();
- await expect(
- directory.recordHandshake(
- profileId,
- {
- sourceServerId: replacementSourceServerId,
- version: "3.8.0",
- connectedAt: 100,
- },
- { confirmIdentityChange: true },
- ),
- ).resolves.toMatchObject({
- identityChanged: true,
- effectiveProfileId: replacementProfileId,
- });
- expect(settings.read().connectionProfiles).toEqual([
- expect.objectContaining({
- profileId,
- lifecycleState: "retired",
- retiredReason: "source-reinstalled",
- retiredAt: 100,
- }),
- expect.objectContaining({
- profileId: replacementProfileId,
- sourceServerId: replacementSourceServerId,
- lifecycleState: "active",
- target: expect.objectContaining({
- passwordCredentialKey: replacementPasswordKey,
- }),
- }),
- ]);
- expect(settings.read().sourceServers).toEqual([
- expect.objectContaining({
- sourceServerId,
- installState: "uninstalled",
- preferredProfileId: undefined,
- }),
- expect.objectContaining({
- sourceServerId: replacementSourceServerId,
- installState: "installed",
- preferredProfileId: replacementProfileId,
- }),
- ]);
- expect(settings.read().sourceServerWorkspaceHistory).toEqual(
- before.sourceServerWorkspaceHistory,
- );
- expect(credentials.read(replacementPasswordKey)).toBe("saved-password");
- });
- it("deduplicates targets against active profiles only", async () => {
- const settings = createSettingService({
- sourceServerDirectoryMigrationVersion: 1,
- connectionProfiles: [
- {
- profileId,
- transport: "docker",
- target: { kind: "docker", container: "demo" },
- lifecycleState: "retired",
- retiredReason: "source-reinstalled",
- retiredAt: 2,
- createdAt: 1,
- },
- {
- profileId: replacementProfileId,
- transport: "docker",
- target: { kind: "docker", container: "demo" },
- lifecycleState: "active",
- createdAt: 2,
- },
- ],
- });
- const directory = new SourceServerDirectory({
- settingService: settings.service,
- credentialService: createCredentialService({}).service,
- });
- await expect(
- directory.ensureProfile({ kind: "docker", container: "demo" }),
- ).resolves.toMatchObject({
- profileId: replacementProfileId,
- lifecycleState: "active",
- });
- });
- it("removes every local record for a device while preserving remote data", async () => {
- const passwordKey = `connection-profile:${profileId}:password`;
- const settings = createSettingService({
- sourceServerDirectoryMigrationVersion: 1,
- sourceServers: [
- {
- sourceServerId,
- displayName: "devbox",
- declaredWorkspaces: [
- {
- workspacePath: "/srv/project",
- workspaceIdentity: `remote:server:${sourceServerId}:/srv/project`,
- },
- ],
- },
- ],
- connectionProfiles: [
- {
- profileId,
- sourceServerId,
- transport: "ssh",
- target: {
- kind: "ssh",
- host: "example.com",
- username: "alice",
- passwordCredentialKey: passwordKey,
- },
- createdAt: 1,
- },
- ],
- sourceServerWorkspaceHistory: [
- {
- workspacePath: "/srv/project",
- workspaceIdentity: `remote:server:${sourceServerId}:/srv/project`,
- sourceServerId,
- profileId,
- lastOpenedAt: 1,
- },
- {
- workspacePath: "/local/other",
- workspaceIdentity: "local:other",
- profileId: replacementProfileId,
- lastOpenedAt: 2,
- },
- ],
- });
- const credentials = createCredentialService({ [passwordKey]: "saved" });
- const directory = new SourceServerDirectory({
- settingService: settings.service,
- credentialService: credentials.service,
- });
- await directory.removeSourceServer(sourceServerId);
- expect(settings.read().sourceServers).toEqual([]);
- expect(settings.read().connectionProfiles).toEqual([]);
- expect(settings.read().sourceServerWorkspaceHistory).toEqual([
- expect.objectContaining({ profileId: replacementProfileId }),
- ]);
- expect(credentials.read(passwordKey)).toBeUndefined();
- });
- it("rolls back replacement credentials when the identity transaction cannot persist", async () => {
- const passwordKey = `connection-profile:${profileId}:password`;
- const replacementPasswordKey = `connection-profile:${replacementProfileId}:password`;
- const settings = createSettingService({
- sourceServerDirectoryMigrationVersion: 1,
- connectionProfiles: [
- {
- profileId,
- transport: "ssh",
- target: {
- kind: "ssh",
- host: "example.com",
- username: "alice",
- passwordCredentialKey: passwordKey,
- },
- sourceServerId,
- createdAt: 1,
- },
- ],
- sourceServers: [{ sourceServerId, displayName: "old", preferredProfileId: profileId }],
- });
- const credentials = createCredentialService({
- [passwordKey]: "saved-password",
- });
- vi.mocked(settings.service.update).mockRejectedValueOnce(new Error("settings write failed"));
- const directory = new SourceServerDirectory({
- settingService: settings.service,
- credentialService: credentials.service,
- createProfileId: () => replacementProfileId,
- now: () => 100,
- });
- const before = structuredClone(settings.read());
- await expect(
- directory.recordHandshake(
- profileId,
- { sourceServerId: replacementSourceServerId, version: "3.8.0" },
- { confirmIdentityChange: true },
- ),
- ).rejects.toThrow("settings write failed");
- expect(settings.read()).toEqual(before);
- expect(credentials.read(replacementPasswordKey)).toBeUndefined();
- expect(credentials.read(passwordKey)).toBe("saved-password");
- });
- it("prefers the preserved legacy workspace identity over the current route fallback", async () => {
- const settings = createSettingService({
- sourceServerDirectoryMigrationVersion: 1,
- sourceServerWorkspaceHistory: [
- {
- workspacePath: "/workspace",
- workspaceIdentity: `remote:server:${sourceServerId}:/workspace`,
- legacyWorkspaceIdentity: "remote:ssh:old.example.com:22:alice:/workspace",
- profileId,
- sourceServerId,
- lastOpenedAt: 1,
- },
- ],
- });
- const directory = new SourceServerDirectory({
- settingService: settings.service,
- credentialService: createCredentialService({}).service,
- });
- await expect(
- directory.resolveLegacyWorkspaceIdentity({
- profileId,
- workspacePath: "/workspace",
- fallback: "remote:ssh:new.example.com:22:alice:/workspace",
- }),
- ).resolves.toBe("remote:ssh:old.example.com:22:alice:/workspace");
- await expect(
- directory.resolveLegacyWorkspaceIdentity({
- profileId,
- workspacePath: "/new-workspace",
- fallback: "remote:ssh:new.example.com:22:alice:/new-workspace",
- }),
- ).resolves.toBe("remote:ssh:new.example.com:22:alice:/new-workspace");
- });
- it("preserves the frozen legacy identity when recording a stable workspace open", async () => {
- const legacyWorkspaceIdentity = "remote:ssh:old.example.com:22:alice:/workspace";
- const stableWorkspaceIdentity = `remote:server:${sourceServerId}:/workspace`;
- const settings = createSettingService({
- sourceServerDirectoryMigrationVersion: 1,
- sourceServerWorkspaceHistory: [
- {
- workspacePath: "/workspace",
- workspaceIdentity: stableWorkspaceIdentity,
- legacyWorkspaceIdentity,
- profileId,
- sourceServerId,
- lastOpenedAt: 1,
- },
- ],
- });
- const directory = new SourceServerDirectory({
- settingService: settings.service,
- credentialService: createCredentialService({}).service,
- now: () => 2,
- });
- await directory.recordWorkspaceOpen({
- profileId,
- sourceServerId,
- workspacePath: "/workspace",
- workspaceIdentity: stableWorkspaceIdentity,
- });
- expect(settings.read().sourceServerWorkspaceHistory).toEqual([
- {
- workspacePath: "/workspace",
- workspaceIdentity: stableWorkspaceIdentity,
- legacyWorkspaceIdentity,
- profileId,
- sourceServerId,
- lastOpenedAt: 2,
- },
- ]);
- });
- it("does not persist workspace history after a bind request is canceled", async () => {
- const settings = createSettingService({
- sourceServerDirectoryMigrationVersion: 1,
- });
- const directory = new SourceServerDirectory({
- settingService: settings.service,
- credentialService: createCredentialService({}).service,
- });
- const controller = new AbortController();
- controller.abort();
- await expect(
- directory.recordWorkspaceOpen({
- profileId,
- sourceServerId,
- workspacePath: "/workspace",
- workspaceIdentity: `remote:server:${sourceServerId}:/workspace`,
- signal: controller.signal,
- }),
- ).rejects.toMatchObject({ name: "AbortError" });
- expect(settings.read().sourceServerWorkspaceHistory).toEqual([]);
- });
- it("returns saved default Profiles regardless of last online age, while requiring silent credentials", async () => {
- const settings = createSettingService({
- connectionProfiles: [
- {
- profileId,
- transport: "ssh",
- target: {
- kind: "ssh",
- host: "example.com",
- port: 22,
- username: "alice",
- passwordCredentialKey: `connection-profile:${profileId}:password`,
- },
- sourceServerId,
- lastConnectedAt: 90,
- createdAt: 1,
- },
- ],
- sourceServers: [
- {
- sourceServerId,
- displayName: "devbox · alice",
- lastOnlineAt: 90,
- preferredProfileId: profileId,
- },
- ],
- sourceServerDirectoryMigrationVersion: 1,
- });
- const credentials = createCredentialService({
- [`connection-profile:${profileId}:password`]: "saved-password",
- });
- const directory = new SourceServerDirectory({
- settingService: settings.service,
- credentialService: credentials.service,
- });
- await expect(directory.listStartupReconnectCandidates()).resolves.toEqual([
- {
- profile: settings.read().connectionProfiles?.[0],
- target: {
- kind: "ssh",
- host: "example.com",
- port: 22,
- username: "alice",
- password: "saved-password",
- },
- },
- ]);
- await expect(
- new SourceServerDirectory({
- settingService: settings.service,
- credentialService: createCredentialService({}).service,
- }).listStartupReconnectCandidates(),
- ).resolves.toEqual([]);
- });
- it("prioritizes the most recently online Sources for bounded startup reconnect workers", async () => {
- const olderProfileId = "4f0f4d21-2798-4608-9d44-889f9f3dc777";
- const olderSourceServerId = "70d16aa4-4404-41f1-99c4-1fe475e21fea";
- const settings = createSettingService({
- connectionProfiles: [
- {
- profileId: olderProfileId,
- transport: "docker",
- target: { kind: "docker", container: "older-source" },
- sourceServerId: olderSourceServerId,
- lastConnectedAt: 10,
- createdAt: 1,
- },
- {
- profileId,
- transport: "docker",
- target: { kind: "docker", container: "recent-source" },
- sourceServerId,
- lastConnectedAt: 90,
- createdAt: 2,
- },
- ],
- sourceServers: [
- {
- sourceServerId: olderSourceServerId,
- displayName: "older-source · root",
- lastOnlineAt: 10,
- preferredProfileId: olderProfileId,
- },
- {
- sourceServerId,
- displayName: "recent-source · root",
- lastOnlineAt: 90,
- preferredProfileId: profileId,
- },
- ],
- sourceServerDirectoryMigrationVersion: 1,
- });
- const directory = new SourceServerDirectory({
- settingService: settings.service,
- credentialService: createCredentialService({}).service,
- });
- await expect(directory.listStartupReconnectCandidates()).resolves.toEqual([
- {
- profile: settings.read().connectionProfiles?.[1],
- target: { kind: "docker", container: "recent-source" },
- },
- {
- profile: settings.read().connectionProfiles?.[0],
- target: { kind: "docker", container: "older-source" },
- },
- ]);
- });
- it("owns Source rename, preferred Profile, target resolution and Profile removal atomically", async () => {
- const secondProfileId = "4f0f4d21-2798-4608-9d44-889f9f3dc777";
- const passwordKey = `connection-profile:${profileId}:password`;
- const settings = createSettingService({
- sourceServerDirectoryMigrationVersion: 1,
- connectionProfiles: [
- {
- profileId,
- transport: "ssh",
- target: {
- kind: "ssh",
- host: "example.com",
- username: "alice",
- passwordCredentialKey: passwordKey,
- },
- sourceServerId,
- createdAt: 1,
- },
- {
- profileId: secondProfileId,
- transport: "docker",
- target: { kind: "docker", container: "dev" },
- sourceServerId,
- createdAt: 2,
- },
- ],
- sourceServers: [
- {
- sourceServerId,
- displayName: "old name",
- preferredProfileId: profileId,
- },
- ],
- sourceServerWorkspaceHistory: [
- {
- workspacePath: "/workspace",
- profileId,
- sourceServerId,
- lastOpenedAt: 1,
- },
- ],
- });
- const credentials = createCredentialService({
- [passwordKey]: "saved-password",
- });
- const directory = new SourceServerDirectory({
- settingService: settings.service,
- credentialService: credentials.service,
- });
- await directory.renameSource(sourceServerId, " renamed ");
- await directory.setPreferredProfile(sourceServerId, secondProfileId);
- await expect(directory.resolveProfileTarget(profileId)).resolves.toMatchObject({
- kind: "ssh",
- host: "example.com",
- username: "alice",
- password: "saved-password",
- });
- await directory.removeProfile(profileId);
- expect(settings.read().sourceServers).toEqual([
- {
- sourceServerId,
- displayName: "renamed",
- installState: "installed",
- preferredProfileId: secondProfileId,
- declaredWorkspaces: [],
- },
- ]);
- expect(settings.read().connectionProfiles?.map((item) => item.profileId)).toEqual([
- secondProfileId,
- ]);
- expect(settings.read().sourceServerWorkspaceHistory).toEqual([]);
- expect(credentials.read(passwordKey)).toBeUndefined();
- });
- it("uses complete stored password auth when a legacy passphrase ref has no private key path", async () => {
- const passwordKey = `connection-profile:${profileId}:password`;
- const stalePassphraseKey = `connection-profile:${profileId}:private-key-passphrase`;
- const settings = createSettingService({
- sourceServerDirectoryMigrationVersion: 1,
- connectionProfiles: [
- {
- profileId,
- transport: "ssh",
- target: {
- kind: "ssh",
- host: "example.com",
- username: "alice",
- passwordCredentialKey: passwordKey,
- privateKeyPassphraseCredentialKey: stalePassphraseKey,
- },
- sourceServerId,
- createdAt: 1,
- },
- ],
- });
- const directory = new SourceServerDirectory({
- settingService: settings.service,
- credentialService: createCredentialService({
- [passwordKey]: "saved-password",
- }).service,
- });
- await expect(directory.resolveProfileTarget(profileId)).resolves.toEqual({
- kind: "ssh",
- host: "example.com",
- username: "alice",
- password: "saved-password",
- });
- });
- it("omits an incomplete encrypted private key when stored password auth is complete", async () => {
- const passwordKey = `connection-profile:${profileId}:password`;
- const missingPassphraseKey = `connection-profile:${profileId}:private-key-passphrase`;
- const settings = createSettingService({
- sourceServerDirectoryMigrationVersion: 1,
- connectionProfiles: [
- {
- profileId,
- transport: "ssh",
- target: {
- kind: "ssh",
- host: "example.com",
- username: "alice",
- privateKeyPath: "/home/alice/.ssh/id_ed25519",
- passwordCredentialKey: passwordKey,
- privateKeyPassphraseCredentialKey: missingPassphraseKey,
- },
- sourceServerId,
- createdAt: 1,
- },
- ],
- });
- const directory = new SourceServerDirectory({
- settingService: settings.service,
- credentialService: createCredentialService({
- [passwordKey]: "saved-password",
- }).service,
- });
- await expect(directory.resolveProfileTarget(profileId)).resolves.toEqual({
- kind: "ssh",
- host: "example.com",
- username: "alice",
- password: "saved-password",
- });
- });
- it("still requires interaction when an encrypted private key is the only auth method and its passphrase is missing", async () => {
- const missingPassphraseKey = `connection-profile:${profileId}:private-key-passphrase`;
- const settings = createSettingService({
- sourceServerDirectoryMigrationVersion: 1,
- connectionProfiles: [
- {
- profileId,
- transport: "ssh",
- target: {
- kind: "ssh",
- host: "example.com",
- username: "alice",
- privateKeyPath: "/home/alice/.ssh/id_ed25519",
- privateKeyPassphraseCredentialKey: missingPassphraseKey,
- },
- sourceServerId,
- createdAt: 1,
- },
- ],
- });
- const directory = new SourceServerDirectory({
- settingService: settings.service,
- credentialService: createCredentialService({}).service,
- });
- await expect(directory.resolveProfileTarget(profileId)).rejects.toThrow(
- "Connection Profile requires interactive credentials",
- );
- await expect(directory.resolveProfileTarget(profileId)).rejects.toMatchObject({
- code: "credentials-required",
- });
- });
- it("uses complete private key auth when a stale password ref has no value", async () => {
- const stalePasswordKey = `connection-profile:${profileId}:password`;
- const settings = createSettingService({
- sourceServerDirectoryMigrationVersion: 1,
- connectionProfiles: [
- {
- profileId,
- transport: "ssh",
- target: {
- kind: "ssh",
- host: "example.com",
- username: "alice",
- privateKeyPath: "/home/alice/.ssh/id_ed25519",
- passwordCredentialKey: stalePasswordKey,
- },
- sourceServerId,
- createdAt: 1,
- },
- ],
- });
- const directory = new SourceServerDirectory({
- settingService: settings.service,
- credentialService: createCredentialService({}).service,
- });
- await expect(directory.resolveProfileTarget(profileId)).resolves.toEqual({
- kind: "ssh",
- host: "example.com",
- username: "alice",
- privateKeyPath: "/home/alice/.ssh/id_ed25519",
- });
- });
- it("keeps valid canonical identities, merges same-path duplicates and truncates at 20 entries", async () => {
- const settings = createSettingService({
- sourceServerDirectoryMigrationVersion: 1,
- connectionProfiles: [
- {
- profileId,
- transport: "docker",
- target: { kind: "docker", container: "zcode-dev" },
- createdAt: 1,
- },
- ],
- });
- const logDebug = vi.fn();
- const directory = new SourceServerDirectory({
- settingService: settings.service,
- credentialService: createCredentialService({}).service,
- logDebug,
- });
- const canonicalA = `remote:server:${sourceServerId}:/srv/a`;
- await directory.recordHandshake(profileId, {
- sourceServerId,
- version: "3.8.0",
- workspaces: [
- // 场景①:远端返回合法设备级 identity,canonical 化后原样保留。
- { path: "/srv/a", workspaceIdentity: canonicalA },
- // 场景④前半:同设备同路径携带线路级 identity,覆盖为 canonical 后与上一条合并。
- {
- path: "/srv/a",
- workspaceIdentity: "remote:ssh:example.com:22:alice:/srv/a",
- },
- // 场景⑧:合并后仍超过 20 条的部分被截断。
- ...Array.from({ length: 21 }, (_, index) => ({
- path: `/srv/bulk-${index}`,
- })),
- ],
- });
- const declared = settings.read().sourceServers?.[0]?.declaredWorkspaces;
- expect(declared).toHaveLength(20);
- expect(declared?.[0]).toEqual({
- workspacePath: "/srv/a",
- workspaceIdentity: canonicalA,
- });
- expect(new Set(declared?.map((entry) => entry.workspaceIdentity)).size).toBe(20);
- // 合同 §8.1.1:identity 被 canonical 覆盖只记 debug 日志、不失败。
- expect(logDebug).toHaveBeenCalledWith(
- expect.stringContaining("remote:ssh:example.com:22:alice:/srv/a"),
- );
- });
- it("keeps same-path workspaces on different devices independent", async () => {
- const settings = createSettingService({
- sourceServerDirectoryMigrationVersion: 1,
- connectionProfiles: [
- {
- profileId,
- sourceServerId,
- transport: "docker",
- target: { kind: "docker", container: "device-a" },
- createdAt: 1,
- },
- {
- profileId: replacementProfileId,
- sourceServerId: replacementSourceServerId,
- transport: "docker",
- target: { kind: "docker", container: "device-b" },
- createdAt: 1,
- },
- ],
- });
- const directory = new SourceServerDirectory({
- settingService: settings.service,
- credentialService: createCredentialService({}).service,
- });
- await directory.recordHandshake(profileId, {
- sourceServerId,
- version: "3.8.0",
- workspaces: [{ path: "/srv/shared" }],
- });
- await directory.recordHandshake(replacementProfileId, {
- sourceServerId: replacementSourceServerId,
- version: "3.8.0",
- workspaces: [{ path: "/srv/shared" }],
- });
- // 场景④后半:workspaceKey 含 serverId,跨设备同路径互不合并、各自独立保存。
- const servers = settings.read().sourceServers ?? [];
- expect(
- servers.find((server) => server.sourceServerId === sourceServerId)?.declaredWorkspaces,
- ).toEqual([
- {
- workspacePath: "/srv/shared",
- workspaceIdentity: `remote:server:${sourceServerId}:/srv/shared`,
- },
- ]);
- expect(
- servers.find((server) => server.sourceServerId === replacementSourceServerId)
- ?.declaredWorkspaces,
- ).toEqual([
- {
- workspacePath: "/srv/shared",
- workspaceIdentity: `remote:server:${replacementSourceServerId}:/srv/shared`,
- },
- ]);
- });
- it("clears declared workspaces on a successful empty handshake", async () => {
- const settings = createSettingService({
- sourceServerDirectoryMigrationVersion: 1,
- connectionProfiles: [
- {
- profileId,
- sourceServerId,
- transport: "docker",
- target: { kind: "docker", container: "zcode-dev" },
- createdAt: 1,
- },
- ],
- sourceServers: [
- {
- sourceServerId,
- displayName: "devbox",
- declaredWorkspaces: [
- {
- workspacePath: "/srv/a",
- workspaceIdentity: `remote:server:${sourceServerId}:/srv/a`,
- },
- ],
- },
- ],
- });
- const directory = new SourceServerDirectory({
- settingService: settings.service,
- credentialService: createCredentialService({}).service,
- });
- // 场景⑤:成功握手返回空数组时整体替换为清空,而非保留旧值。
- await directory.recordHandshake(profileId, {
- sourceServerId,
- version: "3.8.1",
- workspaces: [],
- });
- expect(settings.read().sourceServers?.[0]?.declaredWorkspaces).toEqual([]);
- });
- it("starts the replacement device from empty declared workspaces after an identity change", async () => {
- const legacyWorkspace = {
- workspacePath: "/srv/legacy",
- workspaceIdentity: `remote:server:${sourceServerId}:/srv/legacy`,
- };
- const settings = createSettingService({
- sourceServerDirectoryMigrationVersion: 1,
- connectionProfiles: [
- {
- profileId,
- sourceServerId,
- transport: "docker",
- target: { kind: "docker", container: "zcode-dev" },
- createdAt: 1,
- },
- ],
- sourceServers: [
- {
- sourceServerId,
- displayName: "old-device",
- declaredWorkspaces: [legacyWorkspace],
- },
- ],
- });
- const directory = new SourceServerDirectory({
- settingService: settings.service,
- credentialService: createCredentialService({}).service,
- });
- // 场景⑦:身份换代 = 新设备记录,declaredWorkspaces 从本次握手投影、不继承旧设备。
- await directory.recordHandshake(
- profileId,
- {
- sourceServerId: replacementSourceServerId,
- version: "3.9.0",
- workspaces: [],
- },
- { confirmIdentityChange: true },
- );
- const servers = settings.read().sourceServers ?? [];
- expect(
- servers.find((server) => server.sourceServerId === replacementSourceServerId)
- ?.declaredWorkspaces,
- ).toEqual([]);
- expect(
- servers.find((server) => server.sourceServerId === sourceServerId)?.declaredWorkspaces,
- ).toEqual([legacyWorkspace]);
- });
- it("removes orphan history entries written before identification", async () => {
- const settings = createSettingService({
- sourceServerDirectoryMigrationVersion: 1,
- connectionProfiles: [
- {
- profileId,
- sourceServerId,
- transport: "docker",
- target: { kind: "docker", container: "zcode-dev" },
- createdAt: 1,
- },
- {
- profileId: replacementProfileId,
- transport: "docker",
- target: { kind: "docker", container: "other" },
- createdAt: 1,
- },
- ],
- sourceServerWorkspaceHistory: [
- // Bug 场景:识别前写入的条目没有 sourceServerId,只能通过被删 Profile 关联。
- {
- workspacePath: "/srv/orphan",
- profileId,
- lastOpenedAt: 1,
- },
- {
- workspacePath: "/srv/kept",
- profileId: replacementProfileId,
- lastOpenedAt: 2,
- },
- ],
- });
- const directory = new SourceServerDirectory({
- settingService: settings.service,
- credentialService: createCredentialService({}).service,
- });
- await directory.removeSourceServer(sourceServerId);
- expect(settings.read().sourceServerWorkspaceHistory).toEqual([
- expect.objectContaining({ workspacePath: "/srv/kept" }),
- ]);
- });
- it("keeps device records and restores credentials when removal persistence fails", async () => {
- const passwordKey = `connection-profile:${profileId}:password`;
- const initial = {
- sourceServerDirectoryMigrationVersion: 1 as const,
- sourceServers: [{ sourceServerId, displayName: "devbox" }],
- connectionProfiles: [
- {
- profileId,
- sourceServerId,
- transport: "ssh" as const,
- target: {
- kind: "ssh" as const,
- host: "example.com",
- username: "alice",
- passwordCredentialKey: passwordKey,
- },
- createdAt: 1,
- },
- ],
- sourceServerWorkspaceHistory: [
- {
- workspacePath: "/srv/project",
- profileId,
- sourceServerId,
- lastOpenedAt: 1,
- },
- ],
- };
- const settings = createSettingService(initial);
- const credentials = createCredentialService({ [passwordKey]: "saved" });
- const directory = new SourceServerDirectory({
- settingService: settings.service,
- credentialService: credentials.service,
- });
- vi.mocked(settings.service.update).mockRejectedValueOnce(new Error("disk full"));
- // 合同 §8.1.3:持久化失败进入「设备保留、连接已断开」恢复态,凭据按备份回滚。
- await expect(directory.removeSourceServer(sourceServerId)).rejects.toThrow("disk full");
- expect(settings.read().sourceServers).toHaveLength(1);
- expect(settings.read().connectionProfiles).toHaveLength(1);
- expect(settings.read().sourceServerWorkspaceHistory).toHaveLength(1);
- expect(credentials.read(passwordKey)).toBe("saved");
- });
- it("treats removing an unknown device as a successful no-op without firing change events", async () => {
- const settings = createSettingService({
- sourceServerDirectoryMigrationVersion: 1,
- sourceServers: [{ sourceServerId, displayName: "devbox" }],
- });
- const directory = new SourceServerDirectory({
- settingService: settings.service,
- credentialService: createCredentialService({}).service,
- });
- let changeCount = 0;
- directory.onDidChange(() => {
- changeCount += 1;
- });
- await expect(directory.removeSourceServer(replacementSourceServerId)).resolves.toBeUndefined();
- expect(settings.read().sourceServers).toHaveLength(1);
- // 幂等空操作没有持久化写入,不得驱动消费方权威重读。
- expect(changeCount).toBe(0);
- });
- it("fires onDidChange only after an actual persisted write", async () => {
- const settings = createSettingService({
- sourceServerDirectoryMigrationVersion: 1,
- connectionProfiles: [
- {
- profileId,
- sourceServerId,
- transport: "docker",
- target: { kind: "docker", container: "zcode-dev" },
- createdAt: 1,
- },
- ],
- });
- const directory = new SourceServerDirectory({
- settingService: settings.service,
- credentialService: createCredentialService({}).service,
- });
- let changeCount = 0;
- directory.onDidChange(() => {
- changeCount += 1;
- });
- // 已迁移短路:initialize 无写入,不 fire。
- await directory.initialize();
- expect(changeCount).toBe(0);
- // identity-pending 零写入分支:不 fire。
- await directory.recordHandshake(profileId, {
- sourceServerId: replacementSourceServerId,
- version: "3.9.0",
- });
- expect(changeCount).toBe(0);
- // 目标不存在的 no-op:不 fire。
- await directory.removeProfile(replacementProfileId);
- await directory.markSourceUninstalled(replacementProfileId);
- expect(changeCount).toBe(0);
- // 真实写入:每次持久化成功 fire 一次。
- await directory.recordWorkspaceOpen({
- profileId,
- sourceServerId,
- workspacePath: "/srv/project",
- workspaceIdentity: `remote:server:${sourceServerId}:/srv/project`,
- });
- expect(changeCount).toBe(1);
- });
- });
|