|
1 | | -import { expect, test } from 'vitest'; |
| 1 | +import { expect, test, vi } from 'vitest'; |
2 | 2 | import fs from 'node:fs'; |
3 | 3 | import { AppError } from '@agent-device/kernel/errors'; |
4 | 4 | import { connectionWorkspace, createTestClient } from './remote-connection.fixtures.ts'; |
| 5 | +import type { AgentDeviceClient } from '../agent-device-client.ts'; |
| 6 | +import { connectCommand } from '../cli/commands/connection.ts'; |
5 | 7 | import { materializeRemoteConnectionForCommand } from '../cli/commands/connection-runtime.ts'; |
6 | 8 | import { readRemoteConnectionState } from '../remote/remote-connection-state.ts'; |
7 | 9 | import { LeaseRegistry } from '../daemon/lease-registry.ts'; |
@@ -99,3 +101,92 @@ test('an inactive host lease is reported, never replaced by a tenant allocation' |
99 | 101 | ).rejects.toMatchObject({ code: 'UNAUTHORIZED', details: { reason: 'LEASE_NOT_FOUND' } }); |
100 | 102 | expect(allocations).toBe(0); |
101 | 103 | }); |
| 104 | + |
| 105 | +async function connectHostedMacosApp( |
| 106 | + workspace: { stateDir: string; remoteConfigPath: string }, |
| 107 | + overrides: { leaseId?: string; force?: boolean } = {}, |
| 108 | + release: AgentDeviceClient['leases']['release'] = async () => ({ released: true }), |
| 109 | +): Promise<void> { |
| 110 | + const stdout = vi.spyOn(process.stdout, 'write').mockImplementation(() => true); |
| 111 | + try { |
| 112 | + await connectCommand({ |
| 113 | + positionals: [], |
| 114 | + flags: { |
| 115 | + ...flagsFor(workspace.stateDir, workspace.remoteConfigPath), |
| 116 | + daemonBaseUrl: 'https://host.example/agent/', |
| 117 | + tenant: 'stim', |
| 118 | + runId: 'session-1', |
| 119 | + leaseId: LEASE_ID, |
| 120 | + leaseBackend: 'macos-app', |
| 121 | + platform: 'macos', |
| 122 | + ...overrides, |
| 123 | + }, |
| 124 | + client: createTestClient({ |
| 125 | + release, |
| 126 | + listDevices: async () => { |
| 127 | + throw new Error('a macos-app connection resolves no device into a new key'); |
| 128 | + }, |
| 129 | + allocate: async () => { |
| 130 | + throw new Error('a macos-app connection never allocates'); |
| 131 | + }, |
| 132 | + }), |
| 133 | + }); |
| 134 | + } finally { |
| 135 | + stdout.mockRestore(); |
| 136 | + } |
| 137 | +} |
| 138 | + |
| 139 | +test('connect binds the lease and device a macos-app remote config names, and open uses them', async () => { |
| 140 | + const workspace = connectionWorkspace('macos-app-connect-'); |
| 141 | + writeHostIssuedConfig(workspace.remoteConfigPath); |
| 142 | + await connectHostedMacosApp(workspace); |
| 143 | + expect( |
| 144 | + readRemoteConnectionState({ stateDir: workspace.stateDir, session: 'hosted' }), |
| 145 | + ).toMatchObject({ |
| 146 | + leaseId: LEASE_ID, |
| 147 | + leaseBackend: 'macos-app', |
| 148 | + deviceKey: 'com.example.app@4242', |
| 149 | + }); |
| 150 | + |
| 151 | + const materialized = await materializeRemoteConnectionForCommand({ |
| 152 | + command: 'open', |
| 153 | + positionals: ['com.example.app'], |
| 154 | + flags: { ...flagsFor(workspace.stateDir, workspace.remoteConfigPath), platform: 'macos' }, |
| 155 | + client: createTestClient({ |
| 156 | + allocate: async () => { |
| 157 | + throw new Error('a macos-app connection never allocates'); |
| 158 | + }, |
| 159 | + }), |
| 160 | + }); |
| 161 | + expect(materialized.flags).toMatchObject({ leaseId: LEASE_ID, leaseBackend: 'macos-app' }); |
| 162 | +}); |
| 163 | + |
| 164 | +test('connect with an explicit --lease-id binds that lease over the one in the config', async () => { |
| 165 | + const workspace = connectionWorkspace('macos-app-connect-flag-'); |
| 166 | + writeHostIssuedConfig(workspace.remoteConfigPath); |
| 167 | + const flagLeaseId = 'ffffffffffffffffffffffffffffffff'; |
| 168 | + await connectHostedMacosApp(workspace, { leaseId: flagLeaseId }); |
| 169 | + expect( |
| 170 | + readRemoteConnectionState({ stateDir: workspace.stateDir, session: 'hosted' }), |
| 171 | + ).toMatchObject({ |
| 172 | + leaseId: flagLeaseId, |
| 173 | + deviceKey: 'com.example.app@4242', |
| 174 | + }); |
| 175 | +}); |
| 176 | + |
| 177 | +test('a forced reconnect keeps the host lease it binds again', async () => { |
| 178 | + const workspace = connectionWorkspace('macos-app-connect-force-'); |
| 179 | + writeHostIssuedConfig(workspace.remoteConfigPath); |
| 180 | + await connectHostedMacosApp(workspace); |
| 181 | + const released: string[] = []; |
| 182 | + await connectHostedMacosApp(workspace, { force: true }, async (request) => { |
| 183 | + released.push(request.leaseId); |
| 184 | + return { released: true }; |
| 185 | + }); |
| 186 | + expect(released).toEqual([]); |
| 187 | + expect( |
| 188 | + readRemoteConnectionState({ stateDir: workspace.stateDir, session: 'hosted' }), |
| 189 | + ).toMatchObject({ |
| 190 | + leaseId: LEASE_ID, |
| 191 | + }); |
| 192 | +}); |
0 commit comments