Files
goodbuddy/src/main/ssh/ssh-host-directory-browser.test.ts
T
mesalogo 459cc21e3d
GoodBuddy Agents / Validate Agent source (push) Canceled after 0s
GoodBuddy Agents / Build Linux arm64 Agent (push) Canceled after 0s
GoodBuddy Agents / Build Linux x64 Agent (push) Canceled after 0s
Cross-platform packages / Validate source (push) Canceled after 0s
Cross-platform packages / Authorize desktop packaging (push) Canceled after 0s
Cross-platform packages / linux arm64 (push) Canceled after 0s
Cross-platform packages / macos arm64 (push) Canceled after 0s
Cross-platform packages / windows arm64 (push) Canceled after 0s
Cross-platform packages / linux x64 (push) Canceled after 0s
Cross-platform packages / macos x64 (push) Canceled after 0s
Cross-platform packages / windows x64 (push) Canceled after 0s
Cross-platform packages / Publish GitHub and OSS release (push) Canceled after 0s
fix: make managed SSH sessions fast and resilient
Managed SSH project selection repeatedly activated the remote Host and could
leave idle ACP bindings rejected as stale-controller after a controller
generation change. Host environment updates also coupled project state to
component installation evidence and presented a crowded control surface.

Project selection is now local-only, one Host connection serves compatible
project and conversation callers, idle bindings are safely replaced only
after proven controller takeover, and active prompts keep exact resume/replay.
Remote environment setup now uses explicit signed compound package
preparation with bounded transfer, rollback, recovery, and clearer settings.

Managed Hosts must be updated to separately distributed Agent 0.11.6 before
the daemon-side controller recovery fix takes effect. Desktop packages do not
embed the Agent or remote Runtime, and existing project data is preserved.

Release note: 托管 SSH 项目切换不再重复连接,同一 Host 可顺畅承载多个项目和对话;同时修复重连后的 stale-controller,并优化远程环境安装、更新与状态布局。
2026-08-29 10:58:19 +08:00

269 lines
7.1 KiB
TypeScript

import { describe, expect, it, vi } from 'vitest'
import type { SshConnectionTarget } from './ssh-host-store'
import type {
SshConnectionLease,
SshPoolConnectionIdentity
} from './ssh-connection-pool'
import { SshHostDirectoryBrowser } from './ssh-host-directory-browser'
const hostId = '00000000-0000-4000-8000-000000000301'
function createTarget(
overrides: Partial<SshConnectionTarget> = {}
): SshConnectionTarget {
return {
host: {
id: hostId,
name: 'Browse host',
hostname: 'browse.example.com',
port: 22,
username: 'browser',
authentication: 'password',
password: 'secret',
hostKey: {
algorithm: 'ssh-ed25519',
publicKeyBase64: 'key',
fingerprintSha256: `SHA256:${'A'.repeat(43)}`,
generation: 4
}
},
hostRevision: 9,
hostKeyGeneration: 4,
...overrides
}
}
function createLease(
identity: Partial<SshPoolConnectionIdentity> = {},
listDirectories = vi.fn(async () => ({
path: '/home/browser',
homeDirectory: '/home/browser',
parentPath: '/home',
entries: [],
truncated: false
}))
): SshConnectionLease {
return {
identity: {
hostId,
hostRevision: 9,
hostKeyGeneration: 4,
authenticationIdentity: 'a'.repeat(64),
...identity
},
isUsable: vi.fn(() => true),
openAgentAttach: vi.fn(),
runAgentDoctor: vi.fn(),
runAgentBootstrapProbe: vi.fn(),
probeRemotePackageBootstrap: vi.fn(),
prepareRemotePackageBootstrap: vi.fn(),
commitRemotePackageBootstrap: vi.fn(),
cleanupRemotePackageBootstrap: vi.fn(),
runAgentLifecycleAction: vi.fn(),
runAgentRuntimeAction: vi.fn(),
openStagedSftp: vi.fn(),
listDirectories,
release: vi.fn()
}
}
function createBrowser(options: {
target?: SshConnectionTarget
lease?: ReturnType<typeof createLease>
events?: string[]
}) {
const events = options.events ?? []
const target = options.target ?? createTarget()
const lease = options.lease ?? createLease()
const resolveConnectionTarget = vi.fn(async () => {
events.push('resolve')
return target
})
const assertConnectionTargetCurrent = vi.fn(() => {
events.push('current')
})
const acquire = vi.fn(async () => {
events.push('acquire')
return lease
})
const originalList = lease.listDirectories
lease.listDirectories = vi.fn(async (path, signal) => {
events.push('list')
return originalList(path, signal)
})
const originalRelease = lease.release
lease.release = vi.fn(() => {
events.push('release')
originalRelease()
})
const browser = new SshHostDirectoryBrowser({
sshHosts: {
resolveConnectionTarget,
assertConnectionTargetCurrent
},
sshPool: { acquire }
})
return {
browser,
events,
target,
lease,
resolveConnectionTarget,
assertConnectionTargetCurrent,
acquire
}
}
describe('SSH host directory browser', () => {
it('checks the current target before and after browsing and always releases', async () => {
const test = createBrowser({})
const signal = new AbortController().signal
await expect(
test.browser.listDirectories(
hostId,
'/srv/projects',
signal
)
).resolves.toMatchObject({
path: '/home/browser',
homeDirectory: '/home/browser'
})
expect(test.events).toEqual([
'resolve',
'current',
'acquire',
'list',
'current',
'release'
])
expect(
test.assertConnectionTargetCurrent
).toHaveBeenNthCalledWith(1, {
hostId,
hostRevision: 9,
hostKeyGeneration: 4,
username: 'browser'
})
expect(
test.assertConnectionTargetCurrent
).toHaveBeenNthCalledWith(2, {
hostId,
hostRevision: 9,
hostKeyGeneration: 4,
username: 'browser'
})
expect(test.acquire).toHaveBeenCalledWith(test.target, signal)
expect(test.lease.listDirectories).toHaveBeenCalledWith(
'/srv/projects',
signal
)
expect(test.lease.release).toHaveBeenCalledOnce()
})
it.each([
['host id', { hostId: 'other-host' }],
['host revision', { hostRevision: 10 }],
['host-key generation', { hostKeyGeneration: 5 }]
])(
'rejects a lease with a mismatched %s and releases it',
async (_label, identity) => {
const test = createBrowser({
lease: createLease(identity)
})
await expect(
test.browser.listDirectories(hostId)
).rejects.toThrow('连接身份不匹配')
expect(test.lease.listDirectories).not.toHaveBeenCalled()
expect(test.lease.release).toHaveBeenCalledOnce()
expect(
test.assertConnectionTargetCurrent
).toHaveBeenCalledOnce()
}
)
it('rejects a target resolved for another host before acquiring', async () => {
const target = createTarget()
target.host.id = '00000000-0000-4000-8000-000000000302'
const test = createBrowser({ target })
await expect(
test.browser.listDirectories(hostId)
).rejects.toThrow('连接目标无效')
expect(test.acquire).not.toHaveBeenCalled()
expect(
test.assertConnectionTargetCurrent
).not.toHaveBeenCalled()
})
it('rejects a host change after listing and releases the lease', async () => {
const test = createBrowser({})
test.assertConnectionTargetCurrent
.mockImplementationOnce(() => {
test.events.push('current')
})
.mockImplementationOnce(() => {
test.events.push('current')
throw new Error('SSH 主机配置已变化,请重新验证')
})
await expect(
test.browser.listDirectories(hostId)
).rejects.toThrow('主机配置已变化')
expect(test.events.at(-1)).toBe('release')
expect(test.lease.release).toHaveBeenCalledOnce()
})
it('releases the lease when directory browsing fails', async () => {
const test = createBrowser({
lease: createLease(
{},
vi.fn(async () => {
throw new Error('SFTP unavailable')
})
)
})
await expect(
test.browser.listDirectories(hostId)
).rejects.toThrow('SFTP unavailable')
expect(test.lease.release).toHaveBeenCalledOnce()
expect(
test.assertConnectionTargetCurrent
).toHaveBeenCalledOnce()
})
it('honors cancellation before target resolution and after acquisition', async () => {
const alreadyCanceled = new AbortController()
alreadyCanceled.abort()
const early = createBrowser({})
await expect(
early.browser.listDirectories(
hostId,
undefined,
alreadyCanceled.signal
)
).rejects.toMatchObject({ name: 'AbortError' })
expect(early.resolveConnectionTarget).not.toHaveBeenCalled()
const controller = new AbortController()
const late = createBrowser({})
late.acquire.mockImplementationOnce(async () => {
late.events.push('acquire')
controller.abort()
return late.lease
})
await expect(
late.browser.listDirectories(
hostId,
undefined,
controller.signal
)
).rejects.toMatchObject({ name: 'AbortError' })
expect(late.lease.listDirectories).not.toHaveBeenCalled()
expect(late.lease.release).toHaveBeenCalledOnce()
})
})