|
| 1 | +import assert from 'node:assert/strict'; |
| 2 | +import { test, vi, expect } from 'vitest'; |
| 3 | +import type { DeviceLease } from '@agent-device/contracts/device'; |
| 4 | +import { createLimrunRuntime } from './runtime.ts'; |
| 5 | +import { stubLimrunRuntimeDependencies } from './runtime-dependencies.fixtures.ts'; |
| 6 | +import { mkdtempForTestSync } from './tmp-dir.fixtures.ts'; |
| 7 | +import type { LimrunRuntimeDependencies } from './runtime-dependencies.ts'; |
| 8 | + |
| 9 | +/* |
| 10 | + * A disposal starts its capture end and its local teardown beside the instance delete and joins them only |
| 11 | + * after it. A step of either one that refuses inside that window has this disposal as its observer from the |
| 12 | + * moment it started: nothing a disposal begins may reach the process as an unhandled rejection, and a |
| 13 | + * refused capture or teardown never changes what the delete answered for. |
| 14 | + */ |
| 15 | + |
| 16 | +const state = vi.hoisted(() => ({ |
| 17 | + /** Instance deletes this runtime asked Limrun for; each waits for its case to answer it. */ |
| 18 | + deletes: [] as Array<{ instanceId: string; report: () => void }>, |
| 19 | + disconnects: 0, |
| 20 | + /** Made to throw by the case whose session disconnect refuses while its delete is still owed. */ |
| 21 | + disconnectThrows: false, |
| 22 | + captureCloses: 0, |
| 23 | + /** Made to throw by the case whose capture tunnel close refuses as the release writes its record. */ |
| 24 | + captureCloseThrows: false, |
| 25 | +})); |
| 26 | + |
| 27 | +vi.mock('@limrun/api', () => ({ |
| 28 | + default: class MockLimrun { |
| 29 | + readonly iosInstances = { create: vi.fn(), list: vi.fn(), delete: vi.fn() }; |
| 30 | + readonly androidInstances = { |
| 31 | + create: async () => ({ |
| 32 | + metadata: { id: 'android-instance-1' }, |
| 33 | + status: { |
| 34 | + token: 'instance-token', |
| 35 | + apiUrl: 'https://android.example', |
| 36 | + adbWebSocketUrl: 'wss://adb.example', |
| 37 | + }, |
| 38 | + }), |
| 39 | + list: vi.fn(async () => ({ getPaginatedItems: () => [] })), |
| 40 | + // Every delete waits for its case to answer it, so a release can be held at its delete while the |
| 41 | + // local teardown it started beside has long since refused. |
| 42 | + delete: vi.fn(async (instanceId: string) => { |
| 43 | + let report!: () => void; |
| 44 | + const answered = new Promise<void>((answeredNow) => { |
| 45 | + report = answeredNow; |
| 46 | + }); |
| 47 | + state.deletes.push({ instanceId, report }); |
| 48 | + await answered; |
| 49 | + }), |
| 50 | + }; |
| 51 | + readonly assets = { getOrUpload: vi.fn(), list: vi.fn(async () => []) }; |
| 52 | + }, |
| 53 | +})); |
| 54 | + |
| 55 | +vi.mock('@limrun/api/instance-client', () => ({ |
| 56 | + createInstanceClient: vi.fn(async () => ({ |
| 57 | + disconnect: () => { |
| 58 | + state.disconnects += 1; |
| 59 | + if (state.disconnectThrows) throw new Error('instance client refused to disconnect'); |
| 60 | + }, |
| 61 | + keepAlive: () => {}, |
| 62 | + openUrl: vi.fn(), |
| 63 | + sendAsset: vi.fn(), |
| 64 | + pressKey: vi.fn(), |
| 65 | + setText: vi.fn(), |
| 66 | + tap: vi.fn(), |
| 67 | + startAdbTunnel: vi.fn(async () => { |
| 68 | + throw new Error('no ADB tunnel is raised by these cases'); |
| 69 | + }), |
| 70 | + startTunnel: vi.fn(async () => ({ |
| 71 | + tunnelId: 't1', |
| 72 | + selectors: [], |
| 73 | + close: () => { |
| 74 | + state.captureCloses += 1; |
| 75 | + if (state.captureCloseThrows) throw new Error('capture tunnel refused to close'); |
| 76 | + }, |
| 77 | + })), |
| 78 | + getTunnelStatus: vi.fn(async () => ({})), |
| 79 | + })), |
| 80 | +})); |
| 81 | + |
| 82 | +test('a local teardown that refuses beside a pending delete is reported, not loosed on the process', async () => { |
| 83 | + const rejections = watchUnhandledRejections(); |
| 84 | + state.deletes.length = 0; |
| 85 | + state.disconnectThrows = true; |
| 86 | + const runtime = createLimrunRuntime({ apiKey: 'lim_test_key' }, testDependencies()); |
| 87 | + const lease = androidLease('lease-throwing-disconnect'); |
| 88 | + let reportDelete: (() => void) | undefined; |
| 89 | + try { |
| 90 | + await runtime.leaseLifecycle.allocate?.(lease); |
| 91 | + // The release holds at its delete while the session disconnect it started beside refuses. |
| 92 | + const released = runtime.leaseLifecycle.release?.(lease); |
| 93 | + await vi.waitFor(() => expect(state.deletes).toHaveLength(1)); |
| 94 | + expect(state.disconnects).toBe(1); |
| 95 | + reportDelete = state.deletes[0]?.report; |
| 96 | + reportDelete?.(); |
| 97 | + |
| 98 | + // The delete's confirmation is the release's answer; what the local teardown came to rides beside it |
| 99 | + // as the warning it is: a host resource this process could not put down. |
| 100 | + const record = (await released) as Record<string, unknown>; |
| 101 | + expect(record.limrunInstanceId).toBe('android-instance-1'); |
| 102 | + const warnings = warningsOf(record); |
| 103 | + expect(warnings.map((warning) => warning.code)).toEqual(['LIMRUN_TEARDOWN_INCOMPLETE']); |
| 104 | + expect(String(warnings[0]?.message)).toContain('Local teardown refused to finish'); |
| 105 | + rejections.assertIsSilent(); |
| 106 | + } finally { |
| 107 | + rejections.stop(); |
| 108 | + reportDelete?.(); |
| 109 | + state.deletes.forEach(({ report }) => report()); |
| 110 | + state.deletes.length = 0; |
| 111 | + state.disconnectThrows = false; |
| 112 | + await runtime.shutdown(); |
| 113 | + } |
| 114 | +}); |
| 115 | + |
| 116 | +test('a capture close that refuses still leaves the delete it ran beside answered for', async () => { |
| 117 | + const rejections = watchUnhandledRejections(); |
| 118 | + const harDir = mkdtempForTestSync('limrun-har-observe-'); |
| 119 | + state.deletes.length = 0; |
| 120 | + state.captureCloseThrows = true; |
| 121 | + const runtime = createLimrunRuntime( |
| 122 | + { apiKey: 'lim_test_key', networkHarDir: harDir }, |
| 123 | + testDependencies(), |
| 124 | + ); |
| 125 | + const lease = androidLease('lease-throwing-capture-close'); |
| 126 | + try { |
| 127 | + await runtime.leaseLifecycle.allocate?.(lease); |
| 128 | + await runtime.leaseLifecycle.allocate?.(lease, { flags: { providerNetworkLogs: true } }); |
| 129 | + let reportDelete: (() => void) | undefined; |
| 130 | + try { |
| 131 | + const released = runtime.leaseLifecycle.release?.(lease); |
| 132 | + await vi.waitFor(() => expect(state.deletes).toHaveLength(1)); |
| 133 | + reportDelete = state.deletes[0]?.report; |
| 134 | + reportDelete?.(); |
| 135 | + |
| 136 | + // The record's write ends with the tunnel's close, and that close refuses inside the disposal's |
| 137 | + // join window: the instance the delete confirmed is gone, and the release answers for it. |
| 138 | + const record = (await released) as Record<string, unknown>; |
| 139 | + expect(record.limrunInstanceId).toBe('android-instance-1'); |
| 140 | + expect(state.captureCloses).toBe(1); |
| 141 | + expect(warningsOf(record).map((warning) => warning.message)).toEqual([ |
| 142 | + 'Capture record refused to be written', |
| 143 | + ]); |
| 144 | + rejections.assertIsSilent(); |
| 145 | + } finally { |
| 146 | + reportDelete?.(); |
| 147 | + } |
| 148 | + } finally { |
| 149 | + rejections.stop(); |
| 150 | + state.deletes.forEach(({ report }) => report()); |
| 151 | + state.deletes.length = 0; |
| 152 | + state.captureCloseThrows = false; |
| 153 | + await runtime.shutdown(); |
| 154 | + } |
| 155 | +}); |
| 156 | + |
| 157 | +function warningsOf(record: Record<string, unknown>): Array<Record<string, unknown>> { |
| 158 | + return Array.isArray(record.warnings) ? (record.warnings as Record<string, unknown>[]) : []; |
| 159 | +} |
| 160 | + |
| 161 | +/** |
| 162 | + * Collects the rejections the process would die on: the daemon's failure mode for one is to take every |
| 163 | + * other session down with it, so a case whose disposal looses one fails here rather than in production. |
| 164 | + */ |
| 165 | +function watchUnhandledRejections(): { |
| 166 | + assertIsSilent: () => void; |
| 167 | + stop: () => void; |
| 168 | +} { |
| 169 | + const seen: unknown[] = []; |
| 170 | + const watch = (reason: unknown): void => { |
| 171 | + seen.push(reason); |
| 172 | + }; |
| 173 | + process.on('unhandledRejection', watch); |
| 174 | + return { |
| 175 | + assertIsSilent: () => { |
| 176 | + if (seen.length === 0) return; |
| 177 | + assert.fail(`expected no unhandled rejection, got ${String(seen[0])}`); |
| 178 | + }, |
| 179 | + stop: () => process.off('unhandledRejection', watch), |
| 180 | + }; |
| 181 | +} |
| 182 | + |
| 183 | +function androidLease(leaseId: string): DeviceLease { |
| 184 | + return { |
| 185 | + leaseId, |
| 186 | + tenantId: 'team-a', |
| 187 | + runId: 'run-a', |
| 188 | + backend: 'android-instance', |
| 189 | + leaseProvider: 'limrun', |
| 190 | + createdAt: 1, |
| 191 | + heartbeatAt: 1, |
| 192 | + expiresAt: 60_001, |
| 193 | + }; |
| 194 | +} |
| 195 | + |
| 196 | +/** |
| 197 | + * The host and platform adapters these cases run on: every one of them answers a fixed value. The client |
| 198 | + * connection and the instance delete are driven from the mock `state` above. |
| 199 | + */ |
| 200 | +function testDependencies(): LimrunRuntimeDependencies { |
| 201 | + return stubLimrunRuntimeDependencies(); |
| 202 | +} |
0 commit comments