|
| 1 | +import { expect, test } from 'vitest'; |
| 2 | +import fs from 'node:fs'; |
| 3 | +import { |
| 4 | + connectionWorkspace, |
| 5 | + createTestClient, |
| 6 | + seedConnectionState, |
| 7 | +} from './remote-connection.fixtures.ts'; |
| 8 | +import { materializeRemoteConnectionForCommand } from '../cli/commands/connection-runtime.ts'; |
| 9 | +import { disconnectCommand } from '../cli/commands/connection.ts'; |
| 10 | +import { readRemoteConnectionState } from '../remote/remote-connection-state.ts'; |
| 11 | +import { LeaseRegistry } from '../daemon/lease-registry.ts'; |
| 12 | + |
| 13 | +test.each(['apple', 'harmonyos', 'ios', 'android'] as const)( |
| 14 | + 'stored Harmony runtime compatibility respects %s selection', |
| 15 | + async (platform) => { |
| 16 | + const { stateDir, remoteConfigPath } = connectionWorkspace('harmonyos-runtime-'); |
| 17 | + fs.writeFileSync(remoteConfigPath, JSON.stringify({ daemonBaseUrl: 'https://daemon.example' })); |
| 18 | + seedConnectionState({ |
| 19 | + stateDir, |
| 20 | + state: { |
| 21 | + session: 'runtime-compat', |
| 22 | + remoteConfigPath, |
| 23 | + daemon: { baseUrl: 'https://daemon.example' }, |
| 24 | + tenant: 'proxy', |
| 25 | + runId: 'compat-run', |
| 26 | + leaseId: 'compat-existing', |
| 27 | + leaseBackend: 'harmonyos-instance', |
| 28 | + runtime: { platform: 'harmonyos', launchUrl: 'demo://open' }, |
| 29 | + }, |
| 30 | + }); |
| 31 | + const materialized = await materializeRemoteConnectionForCommand({ |
| 32 | + command: 'snapshot', |
| 33 | + client: createTestClient(), |
| 34 | + flags: { |
| 35 | + json: true, |
| 36 | + help: false, |
| 37 | + version: false, |
| 38 | + stateDir, |
| 39 | + remoteConfig: remoteConfigPath, |
| 40 | + session: 'runtime-compat', |
| 41 | + platform, |
| 42 | + }, |
| 43 | + }); |
| 44 | + if (platform === 'apple' || platform === 'harmonyos') { |
| 45 | + expect(materialized.runtime?.platform).toBe('harmonyos'); |
| 46 | + } else { |
| 47 | + expect(materialized.runtime).toBeUndefined(); |
| 48 | + } |
| 49 | + }, |
| 50 | +); |
| 51 | + |
| 52 | +test('proxy HarmonyOS inventory materializes a scoped lease and closes that same lease', async () => { |
| 53 | + const { stateDir, remoteConfigPath } = connectionWorkspace('harmonyos-proxy-'); |
| 54 | + fs.writeFileSync(remoteConfigPath, JSON.stringify({ daemonBaseUrl: 'https://daemon.example' })); |
| 55 | + seedConnectionState({ |
| 56 | + stateDir, |
| 57 | + state: { |
| 58 | + session: 'harmony-proxy', |
| 59 | + remoteConfigPath, |
| 60 | + daemon: { baseUrl: 'https://daemon.example' }, |
| 61 | + tenant: 'proxy', |
| 62 | + runId: 'harmony-run', |
| 63 | + leaseProvider: 'proxy', |
| 64 | + clientId: 'harmony-client', |
| 65 | + }, |
| 66 | + }); |
| 67 | + const registry = new LeaseRegistry(); |
| 68 | + const allocated: string[] = []; |
| 69 | + const released: string[] = []; |
| 70 | + const client = createTestClient({ |
| 71 | + listDevices: async () => [ |
| 72 | + { |
| 73 | + platform: 'harmonyos', |
| 74 | + target: 'mobile', |
| 75 | + kind: 'emulator', |
| 76 | + id: '127.0.0.1:16001', |
| 77 | + name: 'Harmony Emulator', |
| 78 | + booted: true, |
| 79 | + identifiers: { serial: '127.0.0.1:16001' }, |
| 80 | + harmonyos: { serial: '127.0.0.1:16001' }, |
| 81 | + }, |
| 82 | + ], |
| 83 | + allocate: async (request) => { |
| 84 | + expect(request.leaseBackend).toBe('harmonyos-instance'); |
| 85 | + expect(request.deviceKey).toBe('harmonyos:mobile:127.0.0.1:16001'); |
| 86 | + const lease = registry.allocateLease({ ...request, tenantId: request.tenant }); |
| 87 | + allocated.push(lease.leaseId); |
| 88 | + return lease; |
| 89 | + }, |
| 90 | + heartbeat: async (request) => registry.heartbeatLease({ ...request, tenantId: request.tenant }), |
| 91 | + release: async (request) => { |
| 92 | + const result = registry.releaseLease({ ...request, tenantId: request.tenant }); |
| 93 | + expect(result.released).toBe(true); |
| 94 | + released.push(request.leaseId); |
| 95 | + return result; |
| 96 | + }, |
| 97 | + }); |
| 98 | + const flags = { |
| 99 | + json: true, |
| 100 | + help: false, |
| 101 | + version: false, |
| 102 | + stateDir, |
| 103 | + remoteConfig: remoteConfigPath, |
| 104 | + session: 'harmony-proxy', |
| 105 | + }; |
| 106 | + const materialized = await materializeRemoteConnectionForCommand({ |
| 107 | + command: 'open', |
| 108 | + flags, |
| 109 | + client, |
| 110 | + }); |
| 111 | + expect(materialized.flags.platform).toBe('harmonyos'); |
| 112 | + expect(materialized.flags.serial).toBe('127.0.0.1:16001'); |
| 113 | + expect(materialized.flags.leaseBackend).toBe('harmonyos-instance'); |
| 114 | + expect(materialized.flags.leaseId).toBe(allocated[0]); |
| 115 | + expect(readRemoteConnectionState({ stateDir, session: flags.session })?.leaseId).toBe( |
| 116 | + allocated[0], |
| 117 | + ); |
| 118 | + expect(() => |
| 119 | + registry.assertLeaseAdmission({ |
| 120 | + leaseId: allocated[0], |
| 121 | + tenantId: 'proxy', |
| 122 | + runId: 'harmony-run', |
| 123 | + leaseBackend: 'android-instance', |
| 124 | + }), |
| 125 | + ).toThrow(); |
| 126 | + await disconnectCommand({ positionals: [], flags, client }); |
| 127 | + expect(released).toEqual(allocated); |
| 128 | + expect(readRemoteConnectionState({ stateDir, session: flags.session })).toBeNull(); |
| 129 | +}); |
0 commit comments