|
| 1 | +import { expect, test } from 'vitest'; |
| 2 | +import fs from 'node:fs'; |
| 3 | +import { AppError } from '@agent-device/kernel/errors'; |
| 4 | +import { connectionWorkspace, createTestClient } from './remote-connection.fixtures.ts'; |
| 5 | +import { materializeRemoteConnectionForCommand } from '../cli/commands/connection-runtime.ts'; |
| 6 | +import { readRemoteConnectionState } from '../remote/remote-connection-state.ts'; |
| 7 | +import { LeaseRegistry } from '../daemon/lease-registry.ts'; |
| 8 | + |
| 9 | +const LEASE_ID = 'a1b2c3d4e5f60718293a4b5c6d7e8f90'; |
| 10 | + |
| 11 | +function writeHostIssuedConfig(remoteConfigPath: string): void { |
| 12 | + fs.writeFileSync( |
| 13 | + remoteConfigPath, |
| 14 | + JSON.stringify({ |
| 15 | + daemonBaseUrl: 'https://host.example/agent/', |
| 16 | + daemonAuthToken: 'client-token', |
| 17 | + tenant: 'stim', |
| 18 | + runId: 'session-1', |
| 19 | + leaseId: LEASE_ID, |
| 20 | + leaseBackend: 'macos-app', |
| 21 | + leaseProvider: 'proxy', |
| 22 | + clientId: 'client-1', |
| 23 | + deviceKey: 'com.example.app@4242', |
| 24 | + platform: 'macos', |
| 25 | + }), |
| 26 | + ); |
| 27 | +} |
| 28 | + |
| 29 | +function flagsFor(stateDir: string, remoteConfig: string) { |
| 30 | + return { |
| 31 | + json: true, |
| 32 | + help: false, |
| 33 | + version: false, |
| 34 | + stateDir, |
| 35 | + remoteConfig, |
| 36 | + session: 'hosted', |
| 37 | + }; |
| 38 | +} |
| 39 | + |
| 40 | +test('a macos-app remote config opens on the lease the host allocated, unchanged', async () => { |
| 41 | + const { stateDir, remoteConfigPath } = connectionWorkspace('macos-app-open-'); |
| 42 | + writeHostIssuedConfig(remoteConfigPath); |
| 43 | + const registry = new LeaseRegistry(); |
| 44 | + registry.putHostLease(LEASE_ID, { |
| 45 | + tenantId: 'stim', |
| 46 | + runId: 'session-1', |
| 47 | + clientId: 'client-1', |
| 48 | + leaseBackend: 'macos-app', |
| 49 | + leaseProvider: 'proxy', |
| 50 | + deviceKey: 'com.example.app@4242', |
| 51 | + }); |
| 52 | + const client = createTestClient({ |
| 53 | + listDevices: async () => { |
| 54 | + throw new Error('a macos-app connection resolves no device into a new key'); |
| 55 | + }, |
| 56 | + allocate: async () => { |
| 57 | + throw new Error('a macos-app connection never allocates'); |
| 58 | + }, |
| 59 | + heartbeat: async (request) => registry.heartbeatLease({ ...request, tenantId: request.tenant }), |
| 60 | + }); |
| 61 | + const flags = flagsFor(stateDir, remoteConfigPath); |
| 62 | + const materialized = await materializeRemoteConnectionForCommand({ |
| 63 | + command: 'open', |
| 64 | + positionals: ['com.example.app'], |
| 65 | + flags, |
| 66 | + client, |
| 67 | + }); |
| 68 | + expect(materialized.flags).toMatchObject({ |
| 69 | + leaseId: LEASE_ID, |
| 70 | + leaseBackend: 'macos-app', |
| 71 | + platform: 'macos', |
| 72 | + }); |
| 73 | + expect(materialized.connection).toMatchObject({ deviceKey: 'com.example.app@4242' }); |
| 74 | + expect(readRemoteConnectionState({ stateDir, session: 'hosted' })).toMatchObject({ |
| 75 | + leaseId: LEASE_ID, |
| 76 | + deviceKey: 'com.example.app@4242', |
| 77 | + }); |
| 78 | +}); |
| 79 | + |
| 80 | +test('an inactive host lease is reported, never replaced by a tenant allocation', async () => { |
| 81 | + const { stateDir, remoteConfigPath } = connectionWorkspace('macos-app-inactive-'); |
| 82 | + writeHostIssuedConfig(remoteConfigPath); |
| 83 | + let allocations = 0; |
| 84 | + const client = createTestClient({ |
| 85 | + allocate: async () => { |
| 86 | + allocations += 1; |
| 87 | + throw new Error('unreachable'); |
| 88 | + }, |
| 89 | + heartbeat: async () => { |
| 90 | + throw new AppError('UNAUTHORIZED', 'Lease is not active', { reason: 'LEASE_NOT_FOUND' }); |
| 91 | + }, |
| 92 | + }); |
| 93 | + await expect( |
| 94 | + materializeRemoteConnectionForCommand({ |
| 95 | + command: 'snapshot', |
| 96 | + flags: flagsFor(stateDir, remoteConfigPath), |
| 97 | + client, |
| 98 | + }), |
| 99 | + ).rejects.toMatchObject({ code: 'UNAUTHORIZED', details: { reason: 'LEASE_NOT_FOUND' } }); |
| 100 | + expect(allocations).toBe(0); |
| 101 | +}); |
0 commit comments