11import fs from 'node:fs' ;
22import path from 'node:path' ;
3- import { expect , test , onTestFinished } from 'vitest' ;
4- import type { AgentDeviceDaemonTransport } from '@agent-device/contracts/client' ;
3+ import { expect , test , onTestFinished , vi } from 'vitest' ;
4+ import * as codeSignature from '@agent-device/host-kit/code-signature' ;
5+ import { resolveLocalDaemonCodeIdentity } from '../../../src/daemon-client/daemon-launch-spec.ts' ;
56import { parseReplayScriptDetailed } from '@agent-device/ad-script' ;
67import { createProviderScenarioHarness } from './harness.ts' ;
78import { PROVIDER_SCENARIO_ANDROID } from './fixtures.ts' ;
@@ -18,6 +19,13 @@ import { createCommandToolExecutor } from '../../../src/mcp/command-tools.ts';
1819import { installIsolatedCliTestEnv } from '../../../src/__tests__/cli-test-env.ts' ;
1920
2021async function repairPublication ( root : string ) {
22+ // The runtime runs inside Vitest, so publish the daemon entry's identity instead of Vitest's.
23+ const identity = await resolveLocalDaemonCodeIdentity ( ) ;
24+ if ( identity . origin !== 'checkout' ) throw new Error ( 'expected checkout identity' ) ;
25+ const signature = vi
26+ . spyOn ( codeSignature , 'resolveDaemonCodeSignature' )
27+ . mockReturnValue ( identity . codeSignature ) ;
28+ onTestFinished ( ( ) => signature . mockRestore ( ) ) ;
2129 const shellState = createAndroidProviderShellState ( ) ;
2230 const daemon = await createProviderScenarioHarness ( {
2331 androidAdbProvider : ( ) => ( {
@@ -53,17 +61,8 @@ async function repairPublication(root: string) {
5361 } ) => error . details . divergence . resume ,
5462 ) ;
5563 expect ( divergence . repairSessionHeld ) . toBe ( true ) ;
56- const transport : AgentDeviceDaemonTransport = async ( req ) => {
57- const result = await daemon . callCommand ( req . command , req . positionals , {
58- ...req . flags ,
59- ...selection ,
60- } ) ;
61- return result . json . error
62- ? { ok : false , error : result . json . error . data }
63- : { ok : true , data : result . json . result . data } ;
64- } ;
6564 return {
66- transport ,
65+ config : { stateDir : daemon . stateDir , daemonTransport : 'http' as const , ... selection } ,
6766 close : ( ) => daemon . close ( ) ,
6867 commands : async ( ) => {
6968 await client . replay . run ( {
@@ -88,17 +87,33 @@ test.each([false, true])(
8887 'CLI project record default retains a repair snapshot only with explicit opt-in=%s' ,
8988 async ( explicit ) => {
9089 const { root, home, project } = makeTempWorkspace ( ) ;
91- fs . writeFileSync ( path . join ( project , 'agent-device.json' ) , JSON . stringify ( { record : true } ) ) ;
9290 const publication = await repairPublication ( root ) ;
91+ fs . writeFileSync ( path . join ( project , 'agent-device.json' ) , JSON . stringify ( { record : true } ) ) ;
9392 onTestFinished ( ( ) => publication . close ( ) ) ;
94- const result = await runCliCapture ( [ 'snapshot' , '--json' , ...( explicit ? [ '--record' ] : [ ] ) ] , {
95- cwd : project ,
96- env : { HOME : home , USERPROFILE : home } ,
97- sendToDaemon : publication . transport ,
98- } ) ;
99- expect ( result . calls ) . toHaveLength ( 1 ) ;
100- expect ( result . calls [ 0 ] ?. flags ?. record ) . toBe ( true ) ;
101- expect ( await publication . commands ( ) ) . toEqual (
93+ const result = await runCliCapture (
94+ [
95+ 'snapshot' ,
96+ '--json' ,
97+ '--session' ,
98+ 'default' ,
99+ '--state-dir' ,
100+ publication . config . stateDir ,
101+ '--daemon-transport' ,
102+ 'http' ,
103+ '--platform' ,
104+ 'android' ,
105+ '--serial' ,
106+ publication . config . serial ,
107+ ...( explicit ? [ '--record' ] : [ ] ) ,
108+ ] ,
109+ {
110+ cwd : project ,
111+ env : { HOME : home , USERPROFILE : home } ,
112+ useRealDaemonClient : true ,
113+ } ,
114+ ) ;
115+ expect ( result . code , result . stderr || result . stdout ) . not . toBe ( 1 ) ;
116+ expect ( await publication . commands ( ) , JSON . stringify ( result ) ) . toEqual (
102117 explicit ? [ 'snapshot' , 'snapshot' , 'is' , 'close' ] : [ 'snapshot' , 'is' , 'close' ] ,
103118 ) ;
104119 } ,
@@ -108,20 +123,26 @@ test.each([false, true])(
108123 'MCP project record default retains a repair snapshot only with explicit opt-in=%s' ,
109124 async ( explicit ) => {
110125 const { root, home, project } = makeTempWorkspace ( ) ;
111- fs . writeFileSync ( path . join ( project , 'agent-device.json' ) , JSON . stringify ( { record : true } ) ) ;
112126 const publication = await repairPublication ( root ) ;
127+ fs . writeFileSync ( path . join ( project , 'agent-device.json' ) , JSON . stringify ( { record : true } ) ) ;
113128 onTestFinished ( ( ) => publication . close ( ) ) ;
114- const restoreEnv = installIsolatedCliTestEnv ( { HOME : home , USERPROFILE : home } ) ;
129+ const restoreEnv = installIsolatedCliTestEnv ( {
130+ HOME : home ,
131+ USERPROFILE : home ,
132+ AGENT_DEVICE_STATE_DIR : publication . config . stateDir ,
133+ AGENT_DEVICE_DAEMON_TRANSPORT : 'http' ,
134+ } ) ;
115135 const previousCwd = process . cwd ( ) ;
116136 process . chdir ( project ) ;
117137 try {
118- const executor = createCommandToolExecutor ( {
119- createClient : ( config , ingress ) =>
120- createAgentDeviceClient ( config , { ...ingress , transport : publication . transport } ) ,
138+ const executor = createCommandToolExecutor ( ) ;
139+ const result = await executor . execute ( 'snapshot' , {
140+ platform : 'android' ,
141+ serial : publication . config . serial ,
142+ ...( explicit ? { record : true } : { } ) ,
121143 } ) ;
122- const result = await executor . execute ( 'snapshot' , explicit ? { record : true } : { } ) ;
123- expect ( result . isError ) . toBe ( false ) ;
124- expect ( await publication . commands ( ) ) . toEqual (
144+ expect ( result . isError , JSON . stringify ( result ) ) . toBe ( false ) ;
145+ expect ( await publication . commands ( ) , JSON . stringify ( result ) ) . toEqual (
125146 explicit ? [ 'snapshot' , 'snapshot' , 'is' , 'close' ] : [ 'snapshot' , 'is' , 'close' ] ,
126147 ) ;
127148 } finally {
@@ -135,7 +156,7 @@ test('SDK per-call record explicitly retains a corrective snapshot', async () =>
135156 const { root } = makeTempWorkspace ( ) ;
136157 const publication = await repairPublication ( root ) ;
137158 onTestFinished ( ( ) => publication . close ( ) ) ;
138- const client = createAgentDeviceClient ( { } , { transport : publication . transport } ) ;
159+ const client = createAgentDeviceClient ( publication . config ) ;
139160 await client . capture . snapshot ( { record : true } ) ;
140161 expect ( await publication . commands ( ) ) . toEqual ( [ 'snapshot' , 'snapshot' , 'is' , 'close' ] ) ;
141162} ) ;
0 commit comments