@@ -5,6 +5,7 @@ import { afterEach, beforeEach, test, vi } from 'vitest';
55import { resetAllProcessMemosForTests } from '@agent-device/kernel/ttl-memo' ;
66import { IOS_DEVICE , IOS_SIMULATOR , TVOS_SIMULATOR } from './device-fixtures.ts' ;
77import { mkdtempForTestSync } from './tmp-dir.ts' ;
8+ import { appleRunnerTestHost } from '../test-host.ts' ;
89import { STUBBED_APPLE_TOOLCHAIN , stubAppleToolchainProbes } from './apple-toolchain-fixtures.ts' ;
910import {
1011 isSameRunnerSimulator ,
@@ -108,84 +109,71 @@ test('an older redirect is undone: the symlink goes and the host set comes back'
108109 const paths = makeLegacyPaths ( ) ;
109110 fs . mkdirSync ( path . join ( paths . backupPath , 'HOST-UDID' ) , { recursive : true } ) ;
110111 fs . symlinkSync ( paths . scopedSetPath , paths . xctestDeviceSetPath , 'dir' ) ;
112+ const phases = recordDiagnosticPhases ( ) ;
111113
112- restoreLegacyXctestDeviceSetRedirect ( IOS_SIMULATOR , paths . xctestDeviceSetPath ) ;
114+ restoreLegacyXctestDeviceSetRedirect ( paths . xctestDeviceSetPath ) ;
113115
114116 assert . equal ( fs . lstatSync ( paths . xctestDeviceSetPath ) . isSymbolicLink ( ) , false ) ;
115117 assert . ok ( fs . existsSync ( path . join ( paths . xctestDeviceSetPath , 'HOST-UDID' ) ) ) ;
116118 assert . equal ( fs . existsSync ( paths . backupPath ) , false ) ;
117119 // Unlinking never follows the link: the scoped set it pointed at keeps its devices.
118120 assert . ok ( fs . existsSync ( path . join ( paths . scopedSetPath , 'SCOPED-UDID' ) ) ) ;
121+ assert . deepEqual ( phases , [
122+ 'ios_runner_legacy_xctest_device_set_link_removed' ,
123+ 'ios_runner_legacy_xctest_device_set_backup_restored' ,
124+ ] ) ;
119125} ) ;
120126
121- test ( 'a symlink an older redirect left into this scoped set without a backup is removed' , ( ) => {
122- const paths = makeLegacyPaths ( ) ;
123- fs . symlinkSync ( paths . scopedSetPath , paths . xctestDeviceSetPath , 'dir' ) ;
124-
125- restoreLegacyXctestDeviceSetRedirect (
126- { ...IOS_SIMULATOR , simulatorSetPath : paths . scopedSetPath } ,
127- paths . xctestDeviceSetPath ,
128- ) ;
129-
130- assert . equal ( fs . lstatSync ( paths . xctestDeviceSetPath , { throwIfNoEntry : false } ) , undefined ) ;
131- assert . ok ( fs . existsSync ( path . join ( paths . scopedSetPath , 'SCOPED-UDID' ) ) ) ;
132- } ) ;
133-
134- test ( 'a host-owned XCTestDevices symlink with no backup survives a default-set startup' , ( ) => {
135- const paths = makeLegacyPaths ( ) ;
136- fs . symlinkSync ( paths . scopedSetPath , paths . xctestDeviceSetPath , 'dir' ) ;
137-
138- restoreLegacyXctestDeviceSetRedirect ( IOS_SIMULATOR , paths . xctestDeviceSetPath ) ;
139-
140- assert . equal ( fs . readlinkSync ( paths . xctestDeviceSetPath ) , paths . scopedSetPath ) ;
141- } ) ;
142-
143- test ( 'a host-owned XCTestDevices symlink elsewhere survives a scoped-set startup' , ( ) => {
144- const paths = makeLegacyPaths ( ) ;
145- const external = path . join ( path . dirname ( paths . scopedSetPath ) , 'external-volume' ) ;
146- fs . mkdirSync ( external ) ;
147- fs . symlinkSync ( external , paths . xctestDeviceSetPath , 'dir' ) ;
127+ test ( 'a symlink an older redirect left without a backup is removed, whatever it points at' , ( ) => {
128+ for ( const target of [ 'scoped' , 'external' ] as const ) {
129+ const paths = makeLegacyPaths ( ) ;
130+ const linkTarget =
131+ target === 'scoped'
132+ ? paths . scopedSetPath
133+ : path . join ( path . dirname ( paths . scopedSetPath ) , 'external-volume' ) ;
134+ fs . mkdirSync ( linkTarget , { recursive : true } ) ;
135+ fs . symlinkSync ( linkTarget , paths . xctestDeviceSetPath , 'dir' ) ;
148136
149- restoreLegacyXctestDeviceSetRedirect (
150- { ...IOS_SIMULATOR , simulatorSetPath : paths . scopedSetPath } ,
151- paths . xctestDeviceSetPath ,
152- ) ;
137+ restoreLegacyXctestDeviceSetRedirect ( paths . xctestDeviceSetPath ) ;
153138
154- assert . equal ( fs . readlinkSync ( paths . xctestDeviceSetPath ) , external ) ;
139+ assert . equal ( fs . lstatSync ( paths . xctestDeviceSetPath , { throwIfNoEntry : false } ) , undefined ) ;
140+ assert . ok ( fs . existsSync ( linkTarget ) , target ) ;
141+ }
155142} ) ;
156143
157144test ( 'a backup never replaces a host set that is already in place' , ( ) => {
158145 const paths = makeLegacyPaths ( ) ;
159146 fs . mkdirSync ( path . join ( paths . xctestDeviceSetPath , 'CURRENT-UDID' ) , { recursive : true } ) ;
160147 fs . mkdirSync ( path . join ( paths . backupPath , 'OLD-UDID' ) , { recursive : true } ) ;
161148
162- restoreLegacyXctestDeviceSetRedirect ( IOS_SIMULATOR , paths . xctestDeviceSetPath ) ;
149+ restoreLegacyXctestDeviceSetRedirect ( paths . xctestDeviceSetPath ) ;
163150
164151 assert . ok ( fs . existsSync ( path . join ( paths . xctestDeviceSetPath , 'CURRENT-UDID' ) ) ) ;
165152 assert . ok ( fs . existsSync ( path . join ( paths . backupPath , 'OLD-UDID' ) ) ) ;
166153} ) ;
167154
168- test ( 'a host with no leftovers is left untouched' , ( ) => {
155+ test ( 'a host with no leftovers is left untouched and reports nothing ' , ( ) => {
169156 const paths = makeLegacyPaths ( ) ;
157+ const phases = recordDiagnosticPhases ( ) ;
170158
171- restoreLegacyXctestDeviceSetRedirect ( IOS_SIMULATOR , paths . xctestDeviceSetPath ) ;
159+ restoreLegacyXctestDeviceSetRedirect ( paths . xctestDeviceSetPath ) ;
172160
173161 assert . equal ( fs . existsSync ( paths . xctestDeviceSetPath ) , false ) ;
174162 assert . equal ( fs . existsSync ( paths . backupPath ) , false ) ;
163+ assert . deepEqual ( phases , [ ] ) ;
175164} ) ;
176165
177- /** Runs `step` with the other daemon's whole restore landing just before this daemon 's `call`. */
166+ /** Runs the restore with the other daemon's whole restore landing just before this one 's `call`. */
178167function withOtherDaemonFinishingBefore (
179- call : 'readlinkSync' | ' unlinkSync' | 'renameSync' ,
168+ call : 'unlinkSync' | 'renameSync' ,
180169 paths : LegacyRedirect ,
181- device : Parameters < typeof restoreLegacyXctestDeviceSetRedirect > [ 0 ] ,
182170) : void {
183171 const original = fs [ call ] as ( ...args : unknown [ ] ) => unknown ;
184172 vi . spyOn ( fs , call ) . mockImplementationOnce ( ( ( ...args : unknown [ ] ) => {
185- restoreLegacyXctestDeviceSetRedirect ( device , paths . xctestDeviceSetPath ) ;
173+ restoreLegacyXctestDeviceSetRedirect ( paths . xctestDeviceSetPath ) ;
186174 return original . apply ( fs , args ) ;
187175 } ) as never ) ;
188- restoreLegacyXctestDeviceSetRedirect ( device , paths . xctestDeviceSetPath ) ;
176+ restoreLegacyXctestDeviceSetRedirect ( paths . xctestDeviceSetPath ) ;
189177}
190178
191179test ( 'a daemon whose unlink or rename the other daemon already made finishes the restore' , ( ) => {
@@ -194,7 +182,7 @@ test('a daemon whose unlink or rename the other daemon already made finishes the
194182 fs . mkdirSync ( path . join ( paths . backupPath , 'HOST-UDID' ) , { recursive : true } ) ;
195183 fs . symlinkSync ( paths . scopedSetPath , paths . xctestDeviceSetPath , 'dir' ) ;
196184
197- withOtherDaemonFinishingBefore ( call , paths , IOS_SIMULATOR ) ;
185+ withOtherDaemonFinishingBefore ( call , paths ) ;
198186
199187 assert . equal ( fs . lstatSync ( paths . xctestDeviceSetPath ) . isSymbolicLink ( ) , false , call ) ;
200188 assert . ok ( fs . existsSync ( path . join ( paths . xctestDeviceSetPath , 'HOST-UDID' ) ) , call ) ;
@@ -204,17 +192,6 @@ test('a daemon whose unlink or rename the other daemon already made finishes the
204192 }
205193} ) ;
206194
207- test ( 'a daemon whose link the other daemon removed before it was read leaves the host alone' , ( ) => {
208- const paths = makeLegacyPaths ( ) ;
209- const scoped = { ...IOS_SIMULATOR , simulatorSetPath : paths . scopedSetPath } ;
210- fs . symlinkSync ( paths . scopedSetPath , paths . xctestDeviceSetPath , 'dir' ) ;
211-
212- withOtherDaemonFinishingBefore ( 'readlinkSync' , paths , scoped ) ;
213-
214- assert . equal ( fs . lstatSync ( paths . xctestDeviceSetPath , { throwIfNoEntry : false } ) , undefined ) ;
215- assert . ok ( fs . existsSync ( path . join ( paths . scopedSetPath , 'SCOPED-UDID' ) ) ) ;
216- } ) ;
217-
218195test ( 'an unlink that fails while the link is still there is reported' , ( ) => {
219196 const paths = makeLegacyPaths ( ) ;
220197 fs . mkdirSync ( paths . backupPath ) ;
@@ -223,8 +200,11 @@ test('an unlink that fails while the link is still there is reported', () => {
223200 throw Object . assign ( new Error ( 'EACCES: permission denied' ) , { code : 'EACCES' } ) ;
224201 } ) ;
225202
226- assert . throws (
227- ( ) => restoreLegacyXctestDeviceSetRedirect ( IOS_SIMULATOR , paths . xctestDeviceSetPath ) ,
228- / E A C C E S / ,
229- ) ;
203+ assert . throws ( ( ) => restoreLegacyXctestDeviceSetRedirect ( paths . xctestDeviceSetPath ) , / E A C C E S / ) ;
230204} ) ;
205+
206+ function recordDiagnosticPhases ( ) : string [ ] {
207+ const phases : string [ ] = [ ] ;
208+ appleRunnerTestHost . update ( { emitDiagnostic : ( event ) => phases . push ( event . phase ) } ) ;
209+ return phases ;
210+ }
0 commit comments