Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 86 additions & 0 deletions src/main/persistence.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,23 @@ import {
ONBOARDING_FINAL_STEP,
ONBOARDING_FLOW_VERSION
} from '../shared/constants'
import { SshConnectionStore } from './ssh/ssh-connection-store'

// Shared mutable state so the electron mock can reference a per-test directory
const testState = { dir: '' }

// Stub the ~/.ssh/config parser so the SSH-import integration test below drives
// the real Store (real normalizeSshTarget + disk round-trip) with deterministic
// config hosts instead of the operator's actual ~/.ssh/config.
const { loadUserSshConfigMock, sshConfigHostsToTargetsMock } = vi.hoisted(() => ({
loadUserSshConfigMock: vi.fn(),
sshConfigHostsToTargetsMock: vi.fn()
}))

vi.mock('./ssh/ssh-config-parser', () => ({
loadUserSshConfig: loadUserSshConfigMock,
sshConfigHostsToTargets: sshConfigHostsToTargetsMock
}))
const TEST_LEAF_1 = '11111111-1111-4111-8111-111111111111'
const TEST_LEAF_2 = '22222222-2222-4222-8222-222222222222'
const TEST_LEAF_LIVE = '33333333-3333-4333-8333-333333333333'
Expand Down Expand Up @@ -732,6 +746,78 @@ describe('Store', () => {
}
})

it('persists the SSH target source field through add, update, and disk round-trip', async () => {
const store = await createStore()
store.addSshTarget({
id: 'ssh-src-1',
label: 'cluster',
configHost: 'cluster',
host: '10.0.0.5',
port: 2200,
username: 'dev',
source: 'ssh-config'
})

// normalizeSshTarget must not strip `source` on update, and the new port
// must take effect — this is the persistence-layer guard for #4684 item #1.
const updated = store.updateSshTarget('ssh-src-1', { port: 2222, source: 'ssh-config' })
expect(updated?.port).toBe(2222)
expect(updated?.source).toBe('ssh-config')

expect(store.getSshTarget('ssh-src-1')?.source).toBe('ssh-config')
expect(store.getSshTarget('ssh-src-1')?.port).toBe(2222)

store.flush()
const persisted = readDataFile() as { sshTargets?: Record<string, unknown>[] }
const onDisk = persisted.sshTargets?.find((t) => t.id === 'ssh-src-1')
expect(onDisk?.source).toBe('ssh-config')
expect(onDisk?.port).toBe(2222)
})

it('upserts ~/.ssh/config through the real store: rotated port updates in place and persists', async () => {
loadUserSshConfigMock.mockReturnValue([{ host: 'cluster' }])
const candidate = (port: number, id: string) => [
{ id, label: 'cluster', configHost: 'cluster', host: '10.0.0.5', port, username: 'dev' }
]

const store = await createStore()
const sshStore = new SshConnectionStore(store)

// First sync inserts the config host, stamped as config-managed.
sshConfigHostsToTargetsMock.mockReturnValue(candidate(2200, 'ssh-cfg-1'))
const inserted = sshStore.importFromSshConfig()
expect(inserted).toHaveLength(1)
expect(inserted[0]?.source).toBe('ssh-config')
expect(inserted[0]?.port).toBe(2200)

// Rotated port: the upsert must update the SAME target in place — and the
// real normalizeSshTarget must keep `source` and not falsely re-derive
// configHost into a permanently-dirty state.
sshConfigHostsToTargetsMock.mockReturnValue(candidate(2222, 'ssh-cfg-2'))
const changed = sshStore.importFromSshConfig()
expect(changed).toHaveLength(1)
expect(changed[0]?.port).toBe(2222)
expect(changed[0]?.source).toBe('ssh-config')

// A third identical sync is a no-op (dirty-check against the real persisted
// fields) — proving repeated auto-sync on every pane open writes nothing.
expect(sshStore.importFromSshConfig()).toHaveLength(0)

// Exactly one cluster target on disk with the rotated port and source kept.
store.flush()
const onDisk = (readDataFile() as { sshTargets?: Record<string, unknown>[] }).sshTargets
const clusterTargets = (onDisk ?? []).filter((t) => t.configHost === 'cluster')
expect(clusterTargets).toHaveLength(1)
expect(clusterTargets[0]?.port).toBe(2222)
expect(clusterTargets[0]?.source).toBe('ssh-config')

// Survives a fresh load from the same data file.
const reloaded = await createStore()
const reloadedCluster = reloaded.getSshTargets().find((t) => t.configHost === 'cluster')
expect(reloadedCluster?.port).toBe(2222)
expect(reloadedCluster?.source).toBe('ssh-config')
})

it('drops malformed migration-unsupported PTY entries on load', async () => {
const repo = makeRepo()
writeDataFile({
Expand Down
227 changes: 198 additions & 29 deletions src/main/ssh/ssh-connection-store.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,27 @@ describe('SshConnectionStore', () => {
expect(mockStore.addSshTarget).toHaveBeenCalledWith(target)
})

it('addTarget stamps source as manual by default', () => {
const target = sshStore.addTarget({
label: 'My Server',
host: 'example.com',
port: 22,
username: 'deploy'
})
expect(target.source).toBe('manual')
})

it('addTarget preserves an explicitly provided source', () => {
const target = sshStore.addTarget({
label: 'My Server',
host: 'example.com',
port: 22,
username: 'deploy',
source: 'ssh-config'
})
expect(target.source).toBe('ssh-config')
})

it('updateTarget delegates to store', () => {
const original: SshTarget = {
id: 'ssh-1',
Expand All @@ -91,46 +112,194 @@ describe('SshConnectionStore', () => {
})

describe('importFromSshConfig', () => {
it('imports new hosts from SSH config', () => {
const configHosts = [{ host: 'staging', hostname: 'staging.example.com' }]
const newTargets: SshTarget[] = [
{
id: 'ssh-new-1',
label: 'staging',
host: 'staging.example.com',
port: 22,
username: ''
}
]

loadUserSshConfigMock.mockReturnValue(configHosts)
sshConfigHostsToTargetsMock.mockReturnValue(newTargets)
function candidate(overrides: Partial<SshTarget> & { configHost: string }): SshTarget {
return {
id: `tmp-${overrides.configHost}`,
label: overrides.configHost,
host: `${overrides.configHost}.example.com`,
port: 22,
username: '',
...overrides
}
}

it('inserts a new config host stamped as ssh-config', () => {
loadUserSshConfigMock.mockReturnValue([{ host: 'staging' }])
sshConfigHostsToTargetsMock.mockReturnValue([
candidate({ configHost: 'staging', host: 'staging.example.com' })
])

const result = sshStore.importFromSshConfig()

expect(result).toEqual(newTargets)
expect(mockStore.addSshTarget).toHaveBeenCalledWith(newTargets[0])
expect(mockStore.addSshTarget).toHaveBeenCalledWith(
expect.objectContaining({ configHost: 'staging', source: 'ssh-config' })
)
expect(result).toHaveLength(1)
expect(result[0].source).toBe('ssh-config')
})

it('passes existing target labels to avoid duplicates', () => {
const existing: SshTarget = {
id: 'ssh-existing',
label: 'production',
host: 'prod.example.com',
port: 22,
username: 'deploy'
}
mockStore.addSshTarget(existing)

loadUserSshConfigMock.mockReturnValue([])
it('asks the parser for all hosts — reconciliation happens in the store', () => {
loadUserSshConfigMock.mockReturnValue([{ host: 'a' }])
sshConfigHostsToTargetsMock.mockReturnValue([])

sshStore.importFromSshConfig()

expect(sshConfigHostsToTargetsMock).toHaveBeenCalledWith([], new Set(['production']))
expect(sshConfigHostsToTargetsMock).toHaveBeenCalledWith([{ host: 'a' }], new Set())
})

// PRIMARY regression (#4684 item #1): a rotated port must take effect on
// re-import instead of silently keeping the stale value.
it('updates an existing config-sourced target when the port changed', () => {
mockStore.addSshTarget({
id: 'ssh-1',
label: 'cluster',
configHost: 'cluster',
host: '10.0.0.5',
port: 2200,
username: 'dev',
source: 'ssh-config'
})
loadUserSshConfigMock.mockReturnValue([{ host: 'cluster' }])
sshConfigHostsToTargetsMock.mockReturnValue([
candidate({ configHost: 'cluster', host: '10.0.0.5', port: 2222, username: 'dev' })
])

const result = sshStore.importFromSshConfig()

expect(mockStore.updateSshTarget).toHaveBeenCalledWith(
'ssh-1',
expect.objectContaining({ port: 2222, source: 'ssh-config' })
)
// Only the seed insert — no duplicate target created.
expect(mockStore.addSshTarget).toHaveBeenCalledTimes(1)
expect(result).toHaveLength(1)
expect(result[0].port).toBe(2222)
})

it('refreshes host, username, and jump host on sync, not just port', () => {
mockStore.addSshTarget({
id: 'ssh-1',
label: 'box',
configHost: 'box',
host: 'old.example.com',
port: 22,
username: 'old',
source: 'ssh-config'
})
loadUserSshConfigMock.mockReturnValue([{ host: 'box' }])
sshConfigHostsToTargetsMock.mockReturnValue([
candidate({
configHost: 'box',
host: 'new.example.com',
port: 2200,
username: 'newuser',
jumpHost: 'bastion'
})
])

sshStore.importFromSshConfig()

expect(mockStore.updateSshTarget).toHaveBeenCalledWith(
'ssh-1',
expect.objectContaining({
host: 'new.example.com',
port: 2200,
username: 'newuser',
jumpHost: 'bastion'
})
)
})

it('never overwrites a manual target that owns the alias', () => {
mockStore.addSshTarget({
id: 'ssh-m',
label: 'cluster',
configHost: 'cluster',
host: 'manual.example.com',
port: 22,
username: 'me',
source: 'manual'
})
loadUserSshConfigMock.mockReturnValue([{ host: 'cluster' }])
sshConfigHostsToTargetsMock.mockReturnValue([
candidate({ configHost: 'cluster', host: '10.0.0.9', port: 2222, username: 'dev' })
])

const result = sshStore.importFromSshConfig()

expect(mockStore.updateSshTarget).not.toHaveBeenCalled()
// Only the manual seed insert — the config alias is not duplicated.
expect(mockStore.addSshTarget).toHaveBeenCalledTimes(1)
expect(result).toEqual([])
})

it('adopts a legacy unsourced target into config-sync', () => {
mockStore.addSshTarget({
id: 'ssh-legacy',
label: 'cluster',
configHost: 'cluster',
host: '10.0.0.5',
port: 2200,
username: 'dev'
// no source — predates the field
})
loadUserSshConfigMock.mockReturnValue([{ host: 'cluster' }])
sshConfigHostsToTargetsMock.mockReturnValue([
candidate({ configHost: 'cluster', host: '10.0.0.5', port: 2222, username: 'dev' })
])

const result = sshStore.importFromSshConfig()

expect(mockStore.updateSshTarget).toHaveBeenCalledWith(
'ssh-legacy',
expect.objectContaining({ port: 2222, source: 'ssh-config' })
)
expect(result[0].source).toBe('ssh-config')
})

it('does not overwrite a legacy unsourced manual target with the same alias', () => {
mockStore.addSshTarget({
id: 'ssh-legacy-manual',
label: 'cluster',
configHost: 'cluster',
host: 'cluster',
port: 2200,
username: 'me'
// no source — predates the field, but does not look like a config import
})
loadUserSshConfigMock.mockReturnValue([{ host: 'cluster' }])
sshConfigHostsToTargetsMock.mockReturnValue([
candidate({ configHost: 'cluster', host: '10.0.0.5', port: 2222, username: 'dev' })
])

const result = sshStore.importFromSshConfig()

expect(mockStore.updateSshTarget).not.toHaveBeenCalled()
expect(mockStore.addSshTarget).toHaveBeenCalledTimes(1)
expect(result).toEqual([])
})

it('does not rewrite an unchanged config-sourced target', () => {
mockStore.addSshTarget({
id: 'ssh-1',
label: 'cluster',
configHost: 'cluster',
host: 'cluster.example.com',
port: 22,
username: '',
source: 'ssh-config'
})
loadUserSshConfigMock.mockReturnValue([{ host: 'cluster' }])
// Candidate is identical to the persisted target (same default fields).
sshConfigHostsToTargetsMock.mockReturnValue([candidate({ configHost: 'cluster' })])

const result = sshStore.importFromSshConfig()

expect(mockStore.updateSshTarget).not.toHaveBeenCalled()
expect(result).toEqual([])
})

it('returns empty array when no new hosts found', () => {
it('returns empty array when nothing changed', () => {
loadUserSshConfigMock.mockReturnValue([])
sshConfigHostsToTargetsMock.mockReturnValue([])

Expand Down
Loading