11import { afterEach , beforeEach , describe , expect , test , vi } from 'vitest' ;
22import assert from 'node:assert/strict' ;
3+ import fs from 'node:fs' ;
4+ import path from 'node:path' ;
35import { isRequestCanceledError } from '@agent-device/kernel/errors' ;
46import { resetAllProcessMemosForTests } from '@agent-device/kernel/ttl-memo' ;
57import type { ExecOptions , ExecResult } from '@agent-device/host-kit/command' ;
68import { IOS_SIMULATOR } from './device-fixtures.ts' ;
79import { appleRunnerTestHost } from '../test-host.ts' ;
810import {
911 appleToolchainProbeResult ,
12+ appleSelectionLink ,
1013 blockForWholeTimeout ,
1114 installToolchainProbeClock ,
1215 selectedDeveloperDirResult ,
1316 STUBBED_APPLE_TOOLCHAIN ,
17+ stubAppleSelectionLink ,
1418 valueProbeCommands ,
1519} from './apple-toolchain-fixtures.ts' ;
1620import {
1721 memoizedRunnerXcodeVersion ,
1822 requireRunnerToolchainFingerprint ,
1923} from '../runner-toolchain-probe.ts' ;
2024import { createRunnerPhaseBudget , type RunnerPhaseBudget } from '../runner-phase-budget.ts' ;
25+ import { mkdtempForTestSync } from './tmp-dir.ts' ;
2126import { COLD_TOOLCHAIN_PROBE_TIMEOUT_MS } from '../apple-runner-platform.ts' ;
2227
2328// The fingerprint memo is process-wide and the selection it keys on is host state, so these cases
@@ -35,9 +40,15 @@ const runCmdSync = vi.fn();
3540let selectedDeveloperDir = XCODE_A ;
3641let nowMs = 1_700_000_000_000 ;
3742
43+ stubAppleSelectionLink ( ) ;
44+
45+ /** A scratch root the case's fake developer dirs and selection links live under. */
46+ let tempRoot : string ;
47+
3848beforeEach ( ( ) => {
3949 resetAllProcessMemosForTests ( ) ;
4050 selectedDeveloperDir = XCODE_A ;
51+ tempRoot = mkdtempForTestSync ( 'runner-selection-' ) ;
4152 runCmdSync . mockReset ( ) . mockImplementation ( appleToolchainProbeResult ) ;
4253 appleRunnerTestHost . update ( {
4354 runCmdSync,
@@ -50,6 +61,16 @@ afterEach(() => {
5061 vi . restoreAllMocks ( ) ;
5162} ) ;
5263
64+ /**
65+ * A developer dir the host can resolve, standing in for an installed Xcode at `dir`. `realpath` only
66+ * answers for a dir that exists, so a key built from a missing one would be no key at all.
67+ */
68+ function realSelectionDir ( dir : string ) : string {
69+ const created = path . join ( tempRoot , dir ) ;
70+ fs . mkdirSync ( created , { recursive : true } ) ;
71+ return created ;
72+ }
73+
5374/** Two Xcodes and the `sudo xcode-select -s` that moves the host's default selection between them. */
5475function scriptSelectableXcodes ( ) : void {
5576 runCmdSync . mockImplementation ( ( command : string , args : readonly string [ ] ) =>
@@ -203,6 +224,87 @@ describe('the developer dir a toolchain fingerprint answers for', () => {
203224 expect ( memoizedRunnerXcodeVersion ( IOS_SIMULATOR ) ) . toBeUndefined ( ) ;
204225 } ) ;
205226
227+ test ( 'a warm read resolves its key from the selection link without spawning anything' , ( ) => {
228+ appleSelectionLink . target = realSelectionDir ( XCODE_A ) ;
229+ readFingerprint ( ) ;
230+ runCmdSync . mockClear ( ) ;
231+
232+ readFingerprint ( ) ;
233+
234+ // The link is what a warm read reads, so the memo costs no process at all. `xcode-select -p` is
235+ // the fallback, not the per-read cost, which is what made the key change expensive before.
236+ expect ( runCmdSync . mock . calls ) . toEqual ( [ ] ) ;
237+ } ) ;
238+
239+ test ( 'a selection link naming a toolchain answers from that toolchain, not from xcode-select' , ( ) => {
240+ appleSelectionLink . target = realSelectionDir ( XCODE_A ) ;
241+ expect ( readFingerprint ( ) . xcodeBuildVersion ) . toBe ( STUBBED_APPLE_TOOLCHAIN . xcodeBuildVersion ) ;
242+
243+ // The two sources disagree, which is what a host whose selection moved without its link keeping
244+ // up looks like. The link is what `xcode-select -s` writes, so it is the one that names it.
245+ scriptSelectableXcodes ( ) ;
246+ selectedDeveloperDir = XCODE_B ;
247+ runCmdSync . mockClear ( ) ;
248+
249+ expect ( readFingerprint ( ) . xcodeBuildVersion ) . toBe ( STUBBED_APPLE_TOOLCHAIN . xcodeBuildVersion ) ;
250+ expect ( runCmdSync . mock . calls ) . toEqual ( [ ] ) ;
251+ } ) ;
252+
253+ test ( 'a selection link moved to another developer dir moves the key with it' , ( ) => {
254+ appleSelectionLink . target = realSelectionDir ( XCODE_A ) ;
255+ readFingerprint ( ) ;
256+
257+ appleSelectionLink . target = realSelectionDir ( XCODE_B ) ;
258+ runCmdSync . mockClear ( ) ;
259+
260+ // The entry under the dir the host left cannot answer for the dir it moved to.
261+ expect ( readFingerprint ( ) . xcodeBuildVersion ) . toBe ( STUBBED_APPLE_TOOLCHAIN . xcodeBuildVersion ) ;
262+ expect ( valueProbeCommands ( runCmdSync . mock . calls ) ) . toEqual ( [ 'xcodebuild' , 'xcrun' , 'xcrun' ] ) ;
263+ expect ( runCmdSync . mock . calls . some ( ( [ command ] ) => command === 'xcode-select' ) ) . toBe ( false ) ;
264+ } ) ;
265+
266+ test ( 'a bundle swapped behind a stable selection path moves the key' , ( ) => {
267+ // What a tool like xcodes does: it replaces the Xcode.app bundle, so the selection keeps naming
268+ // the same path while the toolchain behind it changes. Keying on the link's raw target would
269+ // keep answering for the Xcode that path used to reach, so the key follows the real dir.
270+ const stable = path . join ( tempRoot , 'Xcode.app' ) ;
271+ const first = path . join ( tempRoot , 'Xcode-1.app' ) ;
272+ const second = path . join ( tempRoot , 'Xcode-2.app' ) ;
273+ for ( const bundle of [ first , second ] ) {
274+ fs . mkdirSync ( path . join ( bundle , 'Contents' , 'Developer' ) , { recursive : true } ) ;
275+ }
276+ fs . symlinkSync ( path . basename ( first ) , stable ) ;
277+ appleSelectionLink . target = stable ;
278+
279+ readFingerprint ( ) ;
280+ fs . unlinkSync ( stable ) ;
281+ fs . symlinkSync ( path . basename ( second ) , stable ) ;
282+
283+ runCmdSync . mockClear ( ) ;
284+ expect ( readFingerprint ( ) . xcodeBuildVersion ) . toBe ( STUBBED_APPLE_TOOLCHAIN . xcodeBuildVersion ) ;
285+ expect ( valueProbeCommands ( runCmdSync . mock . calls ) ) . toEqual ( [ 'xcodebuild' , 'xcrun' , 'xcrun' ] ) ;
286+ } ) ;
287+
288+ test ( 'a selection reported with its trailing newline keys on the dir alone' , ( ) => {
289+ // `xcode-select -p` answers with a newline. A key that kept it would never match the dir a
290+ // client's DEVELOPER_DIR names, so one Xcode would look like two toolchains.
291+ const reported = realSelectionDir ( XCODE_A ) ;
292+ runCmdSync . mockImplementation ( ( command : string , args : readonly string [ ] ) =>
293+ command === 'xcode-select'
294+ ? { exitCode : 0 , stdout : `${ reported } \n` , stderr : '' }
295+ : appleToolchainProbeResult ( command , args ) ,
296+ ) ;
297+
298+ expect ( readFingerprint ( ) . xcodeBuildVersion ) . toBe ( STUBBED_APPLE_TOOLCHAIN . xcodeBuildVersion ) ;
299+ runCmdSync . mockClear ( ) ;
300+
301+ appleRunnerTestHost . update ( { commandDeveloperDir : ( ) => reported } ) ;
302+ expect ( readFingerprint ( ) . xcodeBuildVersion ) . toBe ( STUBBED_APPLE_TOOLCHAIN . xcodeBuildVersion ) ;
303+
304+ // The client-named dir hit the entry the newline-built key stored, so nothing re-read.
305+ expect ( runCmdSync . mock . calls ) . toEqual ( [ ] ) ;
306+ } ) ;
307+
206308 test ( 'a selection that answers nothing, or dies on exec, is no dir rather than a bad one' , ( ) => {
207309 for ( const unanswered of [
208310 { exitCode : 0 , stdout : ' \n' , stderr : '' } satisfies ExecResult ,
@@ -325,11 +427,12 @@ describe('the lifetime of a memoized toolchain fingerprint', () => {
325427 nowMs += ms ;
326428 }
327429
328- test ( 'a hit answers to the end of the TTL its read started, and no longer ' , ( ) => {
430+ test ( 'a hit renews the entry it answered from, because its key is still the selection ' , ( ) => {
329431 readFingerprint ( ) ;
330432
331- // Hits used to push expiry ahead of themselves, so a daemon reading more often than the TTL
332- // never re-resolved its selection at all (#3300).
433+ // The key names the dir this host selects, so a hit is itself evidence the entry is still the
434+ // answer for that host. #3300 asked to keep this renewal; the stale answer it could once serve
435+ // now costs a key change instead of a TTL.
333436 advance ( FINGERPRINT_TTL_MS / 2 ) ;
334437 runCmdSync . mockClear ( ) ;
335438 readFingerprint ( ) ;
@@ -338,6 +441,18 @@ describe('the lifetime of a memoized toolchain fingerprint', () => {
338441 advance ( FINGERPRINT_TTL_MS / 2 ) ;
339442 runCmdSync . mockClear ( ) ;
340443 readFingerprint ( ) ;
444+ expect ( valueProbeCommands ( runCmdSync . mock . calls ) ) . toEqual ( [ ] ) ;
445+ } ) ;
446+
447+ test ( 'an entry no read takes expires at the TTL its storing read started' , ( ) => {
448+ readFingerprint ( ) ;
449+
450+ advance ( FINGERPRINT_TTL_MS ) ;
451+ runCmdSync . mockClear ( ) ;
452+ readFingerprint ( ) ;
453+
454+ // Renewal is for entries in use. One that nothing has asked for goes away, which is also the
455+ // ceiling on how long a failure report can name a selection no request is using.
341456 expect ( valueProbeCommands ( runCmdSync . mock . calls ) ) . toEqual ( [ 'xcodebuild' , 'xcrun' , 'xcrun' ] ) ;
342457 } ) ;
343458
0 commit comments