|
| 1 | +import assert from 'node:assert/strict'; |
| 2 | +import { beforeEach, test, vi } from 'vitest'; |
| 3 | +import type { DeviceLease } from '@agent-device/contracts/device'; |
| 4 | +import { createLimrunRuntime, type LimrunRuntimeDependencies } from '@agent-device/provider-limrun'; |
| 5 | +import { limrunTestDependencies } from '../../../src/platform-runtime-gateway.fixtures.ts'; |
| 6 | +import { createProviderDeviceRuntimeRequestProviders } from '../../../src/provider-device-runtime.ts'; |
| 7 | +import { assertRpcError, assertRpcOk } from './assertions.ts'; |
| 8 | +import { createProviderScenarioHarness } from './harness.ts'; |
| 9 | + |
| 10 | +const BUNDLE_ID = 'com.apple.Preferences'; |
| 11 | + |
| 12 | +const limrunApiState = vi.hoisted(() => ({ |
| 13 | + listApps: vi.fn(), |
| 14 | + launchApp: vi.fn(async () => undefined), |
| 15 | +})); |
| 16 | + |
| 17 | +vi.mock('@limrun/api', () => ({ |
| 18 | + default: class MockLimrun { |
| 19 | + readonly iosInstances = { |
| 20 | + create: vi.fn(async () => ({ |
| 21 | + metadata: { id: 'limrun-appstate-test-instance' }, |
| 22 | + status: { apiUrl: 'https://limrun.example', token: 'limrun-test-token' }, |
| 23 | + })), |
| 24 | + list: vi.fn(), |
| 25 | + delete: vi.fn(async () => undefined), |
| 26 | + }; |
| 27 | + |
| 28 | + readonly androidInstances = { create: vi.fn(), list: vi.fn(), delete: vi.fn() }; |
| 29 | + |
| 30 | + readonly assets = { getOrUpload: vi.fn(), list: vi.fn() }; |
| 31 | + }, |
| 32 | +})); |
| 33 | + |
| 34 | +vi.mock('@limrun/api/ios-client', () => ({ |
| 35 | + createInstanceClient: vi.fn(async () => ({ |
| 36 | + deviceInfo: { screenWidth: 320, screenHeight: 240 }, |
| 37 | + disconnect: vi.fn(), |
| 38 | + launchApp: limrunApiState.launchApp, |
| 39 | + listApps: limrunApiState.listApps, |
| 40 | + })), |
| 41 | +})); |
| 42 | + |
| 43 | +beforeEach(() => { |
| 44 | + limrunApiState.listApps.mockReset(); |
| 45 | + limrunApiState.launchApp.mockClear(); |
| 46 | +}); |
| 47 | + |
| 48 | +test('Limrun iOS appstate stops answering from the session once the instance is gone', async () => { |
| 49 | + limrunApiState.listApps.mockResolvedValue([]); |
| 50 | + const registration = createLimrunRuntime( |
| 51 | + { apiKey: 'limrun-test-key' }, |
| 52 | + limrunRuntimeDependencies(), |
| 53 | + { includePlatformModule: true }, |
| 54 | + ); |
| 55 | + const runtime = registration.runtime; |
| 56 | + const providers = createProviderDeviceRuntimeRequestProviders([runtime]); |
| 57 | + const daemon = await createProviderScenarioHarness({ |
| 58 | + ...providers, |
| 59 | + deviceInventorySource: providers.deviceInventorySource!, |
| 60 | + platformRuntime: { |
| 61 | + providerRuntimes: [runtime], |
| 62 | + providerModules: [{ runtime, module: registration.platformModule }], |
| 63 | + }, |
| 64 | + }); |
| 65 | + |
| 66 | + try { |
| 67 | + const lease = await allocateLease(daemon); |
| 68 | + const flags = { |
| 69 | + platform: 'ios' as const, |
| 70 | + tenant: 'team-a', |
| 71 | + runId: 'run-a', |
| 72 | + leaseId: lease.leaseId, |
| 73 | + leaseProvider: 'limrun', |
| 74 | + }; |
| 75 | + const meta = { |
| 76 | + tenantId: 'team-a', |
| 77 | + runId: 'run-a', |
| 78 | + leaseId: lease.leaseId, |
| 79 | + leaseBackend: 'ios-instance' as const, |
| 80 | + leaseProvider: 'limrun', |
| 81 | + deviceKey: 'ios-a', |
| 82 | + clientId: 'client-a', |
| 83 | + }; |
| 84 | + assertRpcOk(await daemon.callCommand('open', [BUNDLE_ID], flags, { meta })); |
| 85 | + assert.equal(limrunApiState.launchApp.mock.calls.length, 1); |
| 86 | + |
| 87 | + const live = assertRpcOk<{ appBundleId?: string; source?: string }>( |
| 88 | + await daemon.callCommand('appstate', [], flags, { meta }), |
| 89 | + ); |
| 90 | + assert.equal(live.appBundleId, BUNDLE_ID); |
| 91 | + assert.equal(live.source, 'session'); |
| 92 | + |
| 93 | + limrunApiState.listApps.mockRejectedValue(new Error('instance not found')); |
| 94 | + const gone = await daemon.callCommand('appstate', [], flags, { meta }); |
| 95 | + |
| 96 | + const refusal = assertRpcError(gone, 500, 'COMMAND_FAILED', /Could not confirm/); |
| 97 | + assert.equal((refusal.details as { reason?: string }).reason, 'limrun_instance_unreachable'); |
| 98 | + } finally { |
| 99 | + await daemon.close(); |
| 100 | + await runtime.shutdown(); |
| 101 | + } |
| 102 | +}); |
| 103 | + |
| 104 | +async function allocateLease( |
| 105 | + daemon: Awaited<ReturnType<typeof createProviderScenarioHarness>>, |
| 106 | +): Promise<DeviceLease> { |
| 107 | + const response = await daemon.callCommand( |
| 108 | + 'lease_allocate', |
| 109 | + [], |
| 110 | + { platform: 'ios', tenant: 'team-a', runId: 'run-a', leaseProvider: 'limrun' }, |
| 111 | + { |
| 112 | + meta: { |
| 113 | + tenantId: 'team-a', |
| 114 | + runId: 'run-a', |
| 115 | + leaseBackend: 'ios-instance', |
| 116 | + leaseProvider: 'limrun', |
| 117 | + deviceKey: 'ios-a', |
| 118 | + clientId: 'client-a', |
| 119 | + }, |
| 120 | + }, |
| 121 | + ); |
| 122 | + return assertRpcOk<{ lease: DeviceLease }>(response).lease; |
| 123 | +} |
| 124 | + |
| 125 | +function limrunRuntimeDependencies(): LimrunRuntimeDependencies { |
| 126 | + return { |
| 127 | + clientVersion: 'test-version', |
| 128 | + android: {} as LimrunRuntimeDependencies['android'], |
| 129 | + host: {} as LimrunRuntimeDependencies['host'], |
| 130 | + ios: limrunTestDependencies.ios, |
| 131 | + }; |
| 132 | +} |
0 commit comments