11import { bindTestCommandHost , installFakeManagedAgentBrowser } from '@agent-device/test-kit' ;
22import assert from 'node:assert/strict' ;
3+ import * as hostFile from '@agent-device/host-kit/host-file' ;
34import { ChildProcess } from 'node:child_process' ;
45import fs from 'node:fs' ;
56import path from 'node:path' ;
@@ -10,7 +11,7 @@ import { mkdtempForTestSync } from './__tests__/test-utils.ts';
1011const { runCmdMock, spawnMock, probe } = vi . hoisted ( ( ) => ( {
1112 runCmdMock : vi . fn ( ) ,
1213 spawnMock : vi . fn ( ) ,
13- probe : { unanswered : false } ,
14+ probe : { unanswered : false , platform : undefined as NodeJS . Platform | undefined } ,
1415} ) ) ;
1516
1617vi . mock ( '@agent-device/host-kit/command' , async ( importOriginal ) => ( {
@@ -43,6 +44,7 @@ vi.mock('@agent-device/host-kit/process', async (importOriginal) => {
4344 const actual = await importOriginal < typeof import ( '@agent-device/host-kit/process' ) > ( ) ;
4445 return {
4546 ...actual ,
47+ hostPlatform : ( ) => probe . platform ?? actual . hostPlatform ( ) ,
4648 listHostProcesses : async ( options : Parameters < typeof actual . listHostProcesses > [ 0 ] ) =>
4749 await actual . listHostProcesses ( { ...options , runCommand : runCmdMock } ) ,
4850 readHostProcessIdentityObservationsAsync : async ( pids : Iterable < number > ) =>
@@ -83,6 +85,7 @@ beforeEach(() => {
8385 identitiesMock . mockClear ( ) ;
8486 executablePathsMock . mockReset ( ) ;
8587 probe . unanswered = false ;
88+ probe . platform = undefined ;
8689} ) ;
8790
8891afterEach ( ( ) => {
@@ -521,6 +524,45 @@ function observeProcesses(processes: ProcessFixture[]) {
521524 ) ;
522525}
523526
527+ test . each ( [
528+ [ 'win32' , 'same' , [ 101 ] ] ,
529+ [ 'win32' , 'different-file' , [ ] ] ,
530+ [ 'win32' , 'different-device' , [ ] ] ,
531+ [ 'win32' , 'missing' , [ ] ] ,
532+ [ 'win32' , 'unsafe-inode' , [ ] ] ,
533+ [ 'darwin' , 'same' , [ ] ] ,
534+ ] as const ) (
535+ 'case-only executable spelling on %s requires %s file identity' ,
536+ async ( os , identity , expected ) => {
537+ const stateDir = mkdtempForTestSync ( 'agent-device-web-case-' ) ;
538+ try {
539+ installFakeManagedAgentBrowser ( stateDir ) ;
540+ const status = getManagedAgentBrowserStatus ( {
541+ stateDir,
542+ host : { env : process . env , cwd : process . cwd ( ) } ,
543+ } ) ;
544+ const binary = nativeBinary ( status . installDir ) ;
545+ const observed = binary . replaceAll ( '\\' , '/' ) . toUpperCase ( ) ;
546+ probe . platform = os ;
547+ vi . spyOn ( hostFile , 'hostFileStatSync' ) . mockImplementation ( ( candidate ) => {
548+ if ( candidate === observed && identity === 'missing' ) throw new Error ( 'unavailable' ) ;
549+ const stats = fs . statSync ( candidate === observed ? binary : candidate ) ;
550+ if ( candidate === observed ) {
551+ if ( identity === 'different-file' ) stats . ino += 1 ;
552+ if ( identity === 'different-device' ) stats . dev += 1 ;
553+ if ( identity === 'unsafe-inode' ) stats . ino = Number . MAX_SAFE_INTEGER + 1 ;
554+ }
555+ return stats ;
556+ } ) ;
557+ observeProcesses ( [ { pid : 101 , command : `${ binary } daemon` , executable : observed } ] ) ;
558+ const store = { replace : vi . fn ( ) , clear : vi . fn ( ) , read : vi . fn ( ( ) => [ ] ) } ;
559+ expect ( ( await recordManagedAgentBrowserProcesses ( status , store ) ) . pids ) . toEqual ( expected ) ;
560+ } finally {
561+ fs . rmSync ( stateDir , { recursive : true , force : true } ) ;
562+ }
563+ } ,
564+ ) ;
565+
524566test ( 'a managed binary symlink cannot confer ownership on an executable outside its install' , async ( ) => {
525567 const stateDir = mkdtempForTestSync ( 'agent-device-web-install-boundary-' ) ;
526568 try {
0 commit comments