@@ -2,6 +2,7 @@ import assert from 'node:assert/strict';
22import { test } from 'vitest' ;
33import {
44 isCommandTimeoutError ,
5+ withCommandExecutorOverride ,
56 type ExecOptions ,
67 type ExecResult ,
78} from '@agent-device/host-kit/command' ;
@@ -19,6 +20,65 @@ import { execKillTimeoutError } from './__tests__/exec-timeout-fixture.ts';
1920// actually blocks for the timeout it was handed, so a case that claims the budget was spent had to
2021// spend it.
2122
23+ test ( 'a Rosetta-translated process builds for the arm64 simulator the Mac runs' , async ( ) => {
24+ const platform = Object . getOwnPropertyDescriptor ( process , 'platform' ) ! ;
25+ const arch = Object . getOwnPropertyDescriptor ( process , 'arch' ) ! ;
26+ Object . defineProperty ( process , 'platform' , { ...platform , value : 'darwin' } ) ;
27+ Object . defineProperty ( process , 'arch' , { ...arch , value : 'x64' } ) ;
28+ try {
29+ const host = createNativeBuildHost ( async ( command , args ) => toolchainAnswer ( command , args ) ) ;
30+ const identity = await withCommandExecutorOverride (
31+ async ( command , args ) => {
32+ assert . deepEqual ( [ command , ...args ] , [ '/usr/sbin/sysctl' , '-n' , 'hw.optional.arm64' ] ) ;
33+ return { stdout : '1\n' , stderr : '' , exitCode : 0 } ;
34+ } ,
35+ ( ) => readHostToolchainIdentity ( host , fakeClockDeadline ( 40_000 , { nowMs : 0 } ) ) ,
36+ ) ;
37+ assert . equal ( identity . architecture , 'arm64' ) ;
38+ } finally {
39+ Object . defineProperty ( process , 'platform' , platform ) ;
40+ Object . defineProperty ( process , 'arch' , arch ) ;
41+ }
42+ } ) ;
43+
44+ test ( 'a request cancelled while the host arch is still resolving stops waiting for it' , async ( ) => {
45+ const request = new AbortController ( ) ;
46+ const host : NativeBuildHost = {
47+ ...createNativeBuildHost ( async ( command , args ) => toolchainAnswer ( command , args ) ) ,
48+ cpuArch : ( ) => {
49+ request . abort ( ) ;
50+ return new Promise < string > ( ( ) => { } ) ;
51+ } ,
52+ } ;
53+
54+ await assert . rejects (
55+ readHostToolchainIdentity ( host , createNativeBuildDeadline ( 40_000 , request . signal ) ) ,
56+ ( error : unknown ) =>
57+ error instanceof NativeBuildError &&
58+ error . buildFailureKind === 'cancelled' &&
59+ error . buildFailureCode === 'abort-signal' ,
60+ ) ;
61+ } ) ;
62+
63+ test ( 'a host arch that outlives the request budget reports the deadline' , async ( ) => {
64+ const clock = { nowMs : 0 } ;
65+ const host : NativeBuildHost = {
66+ ...createNativeBuildHost ( async ( command , args ) => {
67+ if ( command === 'sw_vers' && args . includes ( '-buildVersion' ) ) clock . nowMs = 39_990 ;
68+ return toolchainAnswer ( command , args ) ;
69+ } ) ,
70+ cpuArch : ( ) => new Promise < string > ( ( ) => { } ) ,
71+ } ;
72+
73+ await assert . rejects (
74+ readHostToolchainIdentity ( host , fakeClockDeadline ( 40_000 , clock ) ) ,
75+ ( error : unknown ) =>
76+ error instanceof NativeBuildError &&
77+ error . buildFailureKind === 'timeout' &&
78+ error . buildFailureCode === 'host-arch-deadline' ,
79+ ) ;
80+ } ) ;
81+
2282test ( 'a cold-start toolchain probe recovers on retry, and the retry gets only what the stall left' , async ( ) => {
2383 const clock = { nowMs : 0 } ;
2484 const timeouts : number [ ] = [ ] ;
@@ -39,9 +99,9 @@ test('a cold-start toolchain probe recovers on retry, and the retry gets only wh
3999 // retry runs on the 10 s the shared deadline has left, not a second 30 s.
40100 assert . deepEqual ( timeouts . slice ( 0 , 2 ) , [ 30_000 , 10_000 ] ) ;
41101 assert . equal ( clock . nowMs , 30_000 ) ;
42- // 4 baseline probes (xcodebuild, sw_vers x2, uname ) plus the one
102+ // 3 baseline probes (xcodebuild, sw_vers x2) plus the one
43103 // retry that recovered the first, timed-out call.
44- assert . equal ( calls , 5 ) ;
104+ assert . equal ( calls , 4 ) ;
45105} ) ;
46106
47107// The identity read is allowed to exec one Xcode-owned binary. The Simulator SDK a second
@@ -58,7 +118,7 @@ test('the toolchain identity execs one Xcode-owned binary, and no xcrun', async
58118
59119 await readHostToolchainIdentity ( host , fakeClockDeadline ( 120_000 , clock ) ) ;
60120
61- assert . deepEqual ( probed , [ 'xcodebuild' , 'sw_vers' , 'sw_vers' , 'uname' ] ) ;
121+ assert . deepEqual ( probed , [ 'xcodebuild' , 'sw_vers' , 'sw_vers' ] ) ;
62122} ) ;
63123
64124test ( 'a toolchain host that never returns reports the stalled probe after one retry' , async ( ) => {
@@ -301,12 +361,14 @@ function toolchainAnswer(command: string, args: string[]): ExecResult {
301361 if ( command === 'sw_vers' ) {
302362 return { stdout : args . includes ( '-buildVersion' ) ? '24G90' : '15.6' , stderr : '' , exitCode : 0 } ;
303363 }
304- if ( command === 'uname' ) return { stdout : 'arm64' , stderr : '' , exitCode : 0 } ;
305364 throw new Error ( `the identity read execed ${ command } ${ args . join ( ' ' ) } ` ) ;
306365}
307366
308367function fakeToolchainHost (
309368 run : ( command : string , args : string [ ] , options : ExecOptions ) => ExecResult | Promise < ExecResult > ,
310369) : NativeBuildHost {
311- return createNativeBuildHost ( async ( command , args , options ) => run ( command , args , options ?? { } ) ) ;
370+ return {
371+ ...createNativeBuildHost ( async ( command , args , options ) => run ( command , args , options ?? { } ) ) ,
372+ cpuArch : async ( ) => 'arm64' ,
373+ } ;
312374}
0 commit comments