|
| 1 | +import assert from 'node:assert/strict'; |
| 2 | +import path from 'node:path'; |
| 3 | +import { test } from 'vitest'; |
| 4 | +import { |
| 5 | + buildRunnerSessionXctestrunDeviceCleanupPattern, |
| 6 | + buildRunnerSessionXctestrunPathCleanupPattern, |
| 7 | + buildRunnerSessionXctestrunSuffix, |
| 8 | +} from '../runner-artifact-env.ts'; |
| 9 | + |
| 10 | +// Released daemons and clients pkill runner launches by these exact bytes, and the timeout client |
| 11 | +// pins its own copy rather than deriving it, so it survives a rename here. Each expectation below |
| 12 | +// is a literal on purpose: deriving it from this same module would survive any rename, which is |
| 13 | +// precisely the change these lines exist to catch. |
| 14 | + |
| 15 | +const OWNED_ARTIFACT_PATH = |
| 16 | + '/derived/Build/Products/AgentDeviceRunner.env.session-SIM-001-owner-4242-ab12cd34-8123.xctestrun'; |
| 17 | + |
| 18 | +test('the session suffix keeps the bytes and field order the matchers rely on', () => { |
| 19 | + assert.equal( |
| 20 | + buildRunnerSessionXctestrunSuffix({ |
| 21 | + deviceId: 'SIM-001', |
| 22 | + ownerToken: 'owner-4242-ab12cd34', |
| 23 | + port: 8123, |
| 24 | + }), |
| 25 | + 'session-SIM-001-owner-4242-ab12cd34-8123', |
| 26 | + ); |
| 27 | +}); |
| 28 | + |
| 29 | +test('the lease cleanup pattern keeps the bytes released daemons pkilled', () => { |
| 30 | + const owned = buildRunnerSessionXctestrunPathCleanupPattern(OWNED_ARTIFACT_PATH); |
| 31 | + |
| 32 | + assert.equal( |
| 33 | + owned, |
| 34 | + String.raw`AgentDeviceRunner\.env\.session-SIM-001-owner-4242-ab12cd34-8123\.xctestrun`, |
| 35 | + ); |
| 36 | + assert.equal(new RegExp(owned ?? '').test(argvFor(path.basename(OWNED_ARTIFACT_PATH))), true); |
| 37 | + assert.equal( |
| 38 | + new RegExp(owned ?? '').test(argvFor(sessionFileName('SIM-002', 'owner-4242-ab12cd34'))), |
| 39 | + false, |
| 40 | + 'another device must not match', |
| 41 | + ); |
| 42 | + assert.equal( |
| 43 | + new RegExp(owned ?? '').test(argvFor(sessionFileName('SIM-001', 'owner-9999-ffee00'))), |
| 44 | + false, |
| 45 | + 'another owner must not match', |
| 46 | + ); |
| 47 | + assert.equal( |
| 48 | + new RegExp(owned ?? '').test( |
| 49 | + argvFor('AgentDeviceRunner.env.session-SIM-001-owner-4242-ab12cd34-8124.xctestrun'), |
| 50 | + ), |
| 51 | + false, |
| 52 | + 'the same session on another port must not match', |
| 53 | + ); |
| 54 | +}); |
| 55 | + |
| 56 | +test('the device sweep pattern keeps selecting the pre-owner-token name and only that', () => { |
| 57 | + // A reclaiming daemon with no lease to read knows the device but not the artifact, so it sweeps by |
| 58 | + // device. The released bytes require a digit right after the device, which is the pre-owner-token |
| 59 | + // spelling; an owner-token name needs its own path to be selected. |
| 60 | + const sweep = buildRunnerSessionXctestrunDeviceCleanupPattern('SIM-002'); |
| 61 | + |
| 62 | + assert.equal(sweep, String.raw`AgentDeviceRunner\.env\.session-SIM-002-[0-9]`); |
| 63 | + assert.equal( |
| 64 | + new RegExp(sweep).test(argvFor('AgentDeviceRunner.env.session-SIM-002-8123.xctestrun')), |
| 65 | + true, |
| 66 | + ); |
| 67 | + assert.equal( |
| 68 | + new RegExp(sweep).test(argvFor(sessionFileName('SIM-002', 'owner-1-ff'))), |
| 69 | + false, |
| 70 | + 'a launch a lease still names must not fall to the device sweep', |
| 71 | + ); |
| 72 | +}); |
| 73 | + |
| 74 | +test('a flattened name field stays selectable', () => { |
| 75 | + // A device id is caller-supplied and the writer flattens it onto disk, so both matchers have to |
| 76 | + // survive that flattening: the recorded path already holds the bytes the launch carries, and the |
| 77 | + // device sweep has to flatten the id it spells. |
| 78 | + const suffix = buildRunnerSessionXctestrunSuffix({ |
| 79 | + deviceId: 'SIM 01/x', |
| 80 | + ownerToken: 'owner 7', |
| 81 | + port: 80, |
| 82 | + }); |
| 83 | + |
| 84 | + assert.equal(suffix, 'session-SIM_01_x-owner_7-80'); |
| 85 | + const flattenedPath = `/derived/Build/Products/AgentDeviceRunner.env.${suffix}.xctestrun`; |
| 86 | + assert.equal( |
| 87 | + new RegExp(buildRunnerSessionXctestrunPathCleanupPattern(flattenedPath) ?? '').test( |
| 88 | + argvFor(`AgentDeviceRunner.env.${suffix}.xctestrun`), |
| 89 | + ), |
| 90 | + true, |
| 91 | + ); |
| 92 | + // The pre-owner-token spelling of the same flattened device, which is the only name a sweep selects. |
| 93 | + assert.equal( |
| 94 | + new RegExp(buildRunnerSessionXctestrunDeviceCleanupPattern('SIM 01/x')).test( |
| 95 | + argvFor('AgentDeviceRunner.env.session-SIM_01_x-8123.xctestrun'), |
| 96 | + ), |
| 97 | + true, |
| 98 | + ); |
| 99 | +}); |
| 100 | + |
| 101 | +test('a detached lease still selects the launch it started', () => { |
| 102 | + // Detaching rewrites `ownerToken` to `detached-<token>` while the launch keeps running under the |
| 103 | + // name the writer gave it. The bytes a token-derived pattern would have carried are spelled here |
| 104 | + // literally: they select a file that never existed, which is the gap following the recorded path |
| 105 | + // closes. |
| 106 | + const detachedTokenPattern = String.raw`AgentDeviceRunner\.env\.session-SIM-001-detached-owner-4242-ab12cd34-`; |
| 107 | + const argv = argvFor(path.basename(OWNED_ARTIFACT_PATH)); |
| 108 | + |
| 109 | + assert.equal(new RegExp(detachedTokenPattern).test(argv), false); |
| 110 | + assert.equal( |
| 111 | + new RegExp(buildRunnerSessionXctestrunPathCleanupPattern(OWNED_ARTIFACT_PATH) ?? '').test(argv), |
| 112 | + true, |
| 113 | + ); |
| 114 | +}); |
| 115 | + |
| 116 | +test('a lease artifact path outside the runner session name declines to a pattern', () => { |
| 117 | + // A basename like `runner.xctestrun` would escape into a pattern loose enough to signal unrelated |
| 118 | + // xcodebuilds, so the caller has to fall back to the device sweep. |
| 119 | + assert.equal(buildRunnerSessionXctestrunPathCleanupPattern('/tmp/runner.xctestrun'), undefined); |
| 120 | + assert.equal(buildRunnerSessionXctestrunPathCleanupPattern(''), undefined); |
| 121 | + assert.equal(buildRunnerSessionXctestrunPathCleanupPattern(undefined), undefined); |
| 122 | +}); |
| 123 | + |
| 124 | +function sessionFileName(deviceId: string, ownerToken: string): string { |
| 125 | + return `AgentDeviceRunner.env.${buildRunnerSessionXctestrunSuffix({ deviceId, ownerToken, port: 8123 })}.xctestrun`; |
| 126 | +} |
| 127 | + |
| 128 | +function argvFor(fileName: string): string { |
| 129 | + return `xcodebuild test-without-building -xctestrun /derived/Build/Products/${fileName}`; |
| 130 | +} |
0 commit comments