|
| 1 | +import assert from 'node:assert/strict'; |
| 2 | +import fs from 'node:fs'; |
| 3 | +import { afterEach, beforeEach, test } from 'vitest'; |
| 4 | +import { appleRunnerTestHost } from '../test-host.ts'; |
| 5 | +import { |
| 6 | + prepareRunnerLeaseForStartup, |
| 7 | + runnerOwnerStartTime, |
| 8 | + writeRunnerLease, |
| 9 | + type RunnerLease, |
| 10 | + type RunnerLeaseCleanupAdapter, |
| 11 | +} from '../runner-lease.ts'; |
| 12 | +import { makeRunnerLease } from './runner-session-fixtures.ts'; |
| 13 | +import { mkdtempForTestSync } from './tmp-dir.ts'; |
| 14 | + |
| 15 | +// #1320 retained-runner rule, tested at the lease seam directly: device claims |
| 16 | +// are exclusive per device, so claim authority proves a busy lease's owner |
| 17 | +// released the device and merely kept its runner warm. |
| 18 | + |
| 19 | +let ownerStateDir: string; |
| 20 | + |
| 21 | +beforeEach(() => { |
| 22 | + process.env.AGENT_DEVICE_IOS_RUNNER_LEASE_DIR = mkdtempForTestSync( |
| 23 | + 'agent-device-claim-takeover-lease-', |
| 24 | + ); |
| 25 | + // The owner state dir must exist on disk: an owner whose state dir is gone |
| 26 | + // classifies as stale (reclaimable) instead of busy. |
| 27 | + ownerStateDir = mkdtempForTestSync('agent-device-claim-takeover-owner-'); |
| 28 | +}); |
| 29 | + |
| 30 | +afterEach(() => { |
| 31 | + fs.rmSync(ownerStateDir, { recursive: true, force: true }); |
| 32 | +}); |
| 33 | + |
| 34 | +function liveForeignLease(deviceId: string, overrides: Partial<RunnerLease> = {}): RunnerLease { |
| 35 | + return makeRunnerLease({ |
| 36 | + deviceId, |
| 37 | + ownerToken: 'owner-foreign-live', |
| 38 | + ownerPid: process.pid, |
| 39 | + ownerStartTime: runnerOwnerStartTime(), |
| 40 | + ownerStateDir, |
| 41 | + ...overrides, |
| 42 | + }); |
| 43 | +} |
| 44 | + |
| 45 | +function recordingCleanupAdapter(): RunnerLeaseCleanupAdapter & { calls: string[] } { |
| 46 | + const calls: string[] = []; |
| 47 | + return { |
| 48 | + calls, |
| 49 | + cleanupRunnerProcessTree: async (_pid, signal) => { |
| 50 | + calls.push(`process-tree:${signal}`); |
| 51 | + }, |
| 52 | + cleanupRunnerXcodebuildProcesses: async (_deviceId, ownerToken) => { |
| 53 | + calls.push(`xcodebuild:${ownerToken ?? 'any'}`); |
| 54 | + }, |
| 55 | + cleanupTempFile: (filePath) => { |
| 56 | + calls.push(`temp:${filePath}`); |
| 57 | + }, |
| 58 | + }; |
| 59 | +} |
| 60 | + |
| 61 | +test('device-claim authority reclaims a live foreign claim-aware lease', async () => { |
| 62 | + const deviceId = 'claim-takeover-sim'; |
| 63 | + writeRunnerLease(liveForeignLease(deviceId, { deviceClaimProtocol: 1 })); |
| 64 | + appleRunnerTestHost.update({ hasDeviceClaimAuthority: (id) => id === deviceId }); |
| 65 | + const cleanup = recordingCleanupAdapter(); |
| 66 | + |
| 67 | + await prepareRunnerLeaseForStartup(deviceId, cleanup); |
| 68 | + |
| 69 | + assert.ok(cleanup.calls.includes('xcodebuild:owner-foreign-live')); |
| 70 | + // The lease was released as part of the takeover, so the next classification |
| 71 | + // sees an empty store instead of the foreign owner. |
| 72 | + const emptyCleanup = recordingCleanupAdapter(); |
| 73 | + await prepareRunnerLeaseForStartup(deviceId, emptyCleanup); |
| 74 | + assert.ok(emptyCleanup.calls.includes('xcodebuild:any')); |
| 75 | +}); |
| 76 | + |
| 77 | +test('a claim-aware lease still refuses without device-claim authority', async () => { |
| 78 | + const deviceId = 'claim-no-authority-sim'; |
| 79 | + writeRunnerLease(liveForeignLease(deviceId, { deviceClaimProtocol: 1 })); |
| 80 | + const cleanup = recordingCleanupAdapter(); |
| 81 | + |
| 82 | + await assert.rejects( |
| 83 | + async () => await prepareRunnerLeaseForStartup(deviceId, cleanup), |
| 84 | + /already owned by another agent-device daemon/, |
| 85 | + ); |
| 86 | + assert.deepEqual(cleanup.calls, []); |
| 87 | +}); |
| 88 | + |
| 89 | +test('a pre-claims lease is never preempted despite device-claim authority', async () => { |
| 90 | + // A lease without deviceClaimProtocol was written by a build that never |
| 91 | + // arbitrates through device claims, so its owner may be actively using the |
| 92 | + // runner without holding any claim. |
| 93 | + const deviceId = 'legacy-lease-sim'; |
| 94 | + writeRunnerLease(liveForeignLease(deviceId)); |
| 95 | + appleRunnerTestHost.update({ hasDeviceClaimAuthority: () => true }); |
| 96 | + const cleanup = recordingCleanupAdapter(); |
| 97 | + |
| 98 | + await assert.rejects( |
| 99 | + async () => await prepareRunnerLeaseForStartup(deviceId, cleanup), |
| 100 | + /already owned by another agent-device daemon/, |
| 101 | + ); |
| 102 | + assert.deepEqual(cleanup.calls, []); |
| 103 | +}); |
0 commit comments