1- import test from 'node:test' ;
2- import assert from 'node:assert/strict' ;
1+ import { test , expect , vi , beforeEach } from 'vitest' ;
32import fs from 'node:fs' ;
43import os from 'node:os' ;
54import path from 'node:path' ;
5+
6+ vi . mock ( '../../core/dispatch.ts' , async ( importOriginal ) => {
7+ const actual = await importOriginal < typeof import ( '../../core/dispatch.ts' ) > ( ) ;
8+ return { ...actual , dispatchCommand : vi . fn ( async ( ) => ( { } ) ) } ;
9+ } ) ;
10+
11+ import { dispatchCommand } from '../../core/dispatch.ts' ;
612import { createRequestHandler } from '../request-router.ts' ;
713import { SessionStore } from '../session-store.ts' ;
814import type { SessionState } from '../types.ts' ;
915import { LeaseRegistry } from '../lease-registry.ts' ;
1016
17+ const mockDispatch = vi . mocked ( dispatchCommand ) ;
18+
1119function makeStore ( ) : SessionStore {
1220 const tempRoot = fs . mkdtempSync ( path . join ( os . tmpdir ( ) , 'agent-device-router-lock-' ) ) ;
1321 return new SessionStore ( path . join ( tempRoot , 'sessions' ) ) ;
@@ -30,21 +38,21 @@ function makeIosSession(name: string): SessionState {
3038 } ;
3139}
3240
41+ beforeEach ( ( ) => {
42+ mockDispatch . mockReset ( ) ;
43+ mockDispatch . mockResolvedValue ( { } ) ;
44+ } ) ;
45+
3346test ( 'direct daemon requests cannot bypass reject lock policy for existing sessions' , async ( ) => {
3447 const sessionStore = makeStore ( ) ;
3548 sessionStore . set ( 'qa-ios' , makeIosSession ( 'qa-ios' ) ) ;
36- let dispatchCalls = 0 ;
3749
3850 const handler = createRequestHandler ( {
3951 logPath : path . join ( os . tmpdir ( ) , 'daemon.log' ) ,
4052 token : 'test-token' ,
4153 sessionStore,
4254 leaseRegistry : new LeaseRegistry ( ) ,
4355 trackDownloadableArtifact : ( ) => 'artifact-id' ,
44- dispatchCommand : async ( ) => {
45- dispatchCalls += 1 ;
46- return { } ;
47- } ,
4856 } ) ;
4957
5058 const response = await handler ( {
@@ -60,29 +68,24 @@ test('direct daemon requests cannot bypass reject lock policy for existing sessi
6068 } ,
6169 } ) ;
6270
63- assert . equal ( dispatchCalls , 0 ) ;
64- assert . equal ( response . ok , false ) ;
71+ expect ( mockDispatch ) . not . toHaveBeenCalled ( ) ;
72+ expect ( response . ok ) . toBe ( false ) ;
6573 if ( ! response . ok ) {
66- assert . equal ( response . error . code , 'INVALID_ARGS' ) ;
67- assert . match ( response . error . message , / - - u d i d = S I M - 9 9 9 / i) ;
74+ expect ( response . error . code ) . toBe ( 'INVALID_ARGS' ) ;
75+ expect ( response . error . message ) . toMatch ( / - - u d i d = S I M - 9 9 9 / i) ;
6876 }
6977} ) ;
7078
7179test ( 'batch steps cannot bypass reject lock policy on nested direct requests' , async ( ) => {
7280 const sessionStore = makeStore ( ) ;
7381 sessionStore . set ( 'qa-ios' , makeIosSession ( 'qa-ios' ) ) ;
74- let dispatchCalls = 0 ;
7582
7683 const handler = createRequestHandler ( {
7784 logPath : path . join ( os . tmpdir ( ) , 'daemon.log' ) ,
7885 token : 'test-token' ,
7986 sessionStore,
8087 leaseRegistry : new LeaseRegistry ( ) ,
8188 trackDownloadableArtifact : ( ) => 'artifact-id' ,
82- dispatchCommand : async ( ) => {
83- dispatchCalls += 1 ;
84- return { } ;
85- } ,
8689 } ) ;
8790
8891 const response = await handler ( {
@@ -105,30 +108,30 @@ test('batch steps cannot bypass reject lock policy on nested direct requests', a
105108 } ,
106109 } ) ;
107110
108- assert . equal ( dispatchCalls , 0 ) ;
109- assert . equal ( response . ok , false ) ;
111+ expect ( mockDispatch ) . not . toHaveBeenCalled ( ) ;
112+ expect ( response . ok ) . toBe ( false ) ;
110113 if ( ! response . ok ) {
111- assert . equal ( response . error . code , 'INVALID_ARGS' ) ;
112- assert . match ( response . error . message , / B a t c h f a i l e d a t s t e p 1 / i) ;
113- assert . match ( response . error . message , / - - s e r i a l = e m u l a t o r - 5 5 5 4 / i) ;
114+ expect ( response . error . code ) . toBe ( 'INVALID_ARGS' ) ;
115+ expect ( response . error . message ) . toMatch ( / B a t c h f a i l e d a t s t e p 1 / i) ;
116+ expect ( response . error . message ) . toMatch ( / - - s e r i a l = e m u l a t o r - 5 5 5 4 / i) ;
114117 }
115118} ) ;
116119
117120test ( 'direct daemon requests apply strip lock policy for existing sessions before dispatch' , async ( ) => {
118121 const sessionStore = makeStore ( ) ;
119122 sessionStore . set ( 'qa-ios' , makeIosSession ( 'qa-ios' ) ) ;
120123 let dispatchCalls = 0 ;
124+ mockDispatch . mockImplementation ( async ( ) => {
125+ dispatchCalls += 1 ;
126+ return { } ;
127+ } ) ;
121128
122129 const handler = createRequestHandler ( {
123130 logPath : path . join ( os . tmpdir ( ) , 'daemon.log' ) ,
124131 token : 'test-token' ,
125132 sessionStore,
126133 leaseRegistry : new LeaseRegistry ( ) ,
127134 trackDownloadableArtifact : ( ) => 'artifact-id' ,
128- dispatchCommand : async ( ) => {
129- dispatchCalls += 1 ;
130- return { } ;
131- } ,
132135 } ) ;
133136
134137 const response = await handler ( {
@@ -146,13 +149,13 @@ test('direct daemon requests apply strip lock policy for existing sessions befor
146149 } ,
147150 } ) ;
148151
149- assert . equal ( dispatchCalls , 1 ) ;
150- assert . equal ( response . ok , true ) ;
152+ expect ( dispatchCalls ) . toBe ( 1 ) ;
153+ expect ( response . ok ) . toBe ( true ) ;
151154 const action = sessionStore . get ( 'qa-ios' ) ?. actions . at ( - 1 ) ;
152- assert . equal ( action ?. flags . platform , 'ios' ) ;
153- assert . equal ( action ?. flags . udid , undefined ) ;
154- assert . equal ( action ?. flags . target , undefined ) ;
155- assert . equal ( action ?. flags . device , 'iPhone 16' ) ;
155+ expect ( action ?. flags . platform ) . toBe ( 'ios' ) ;
156+ expect ( action ?. flags . udid ) . toBe ( undefined ) ;
157+ expect ( action ?. flags . target ) . toBe ( undefined ) ;
158+ expect ( action ?. flags . device ) . toBe ( 'iPhone 16' ) ;
156159} ) ;
157160
158161test ( 'batch preserves tenant-scoped session names across nested requests' , async ( ) => {
@@ -164,17 +167,17 @@ test('batch preserves tenant-scoped session names across nested requests', async
164167 runId : 'run-1' ,
165168 } ) ;
166169 let dispatchCalls = 0 ;
170+ mockDispatch . mockImplementation ( async ( ) => {
171+ dispatchCalls += 1 ;
172+ return { } ;
173+ } ) ;
167174
168175 const handler = createRequestHandler ( {
169176 logPath : path . join ( os . tmpdir ( ) , 'daemon.log' ) ,
170177 token : 'test-token' ,
171178 sessionStore,
172179 leaseRegistry,
173180 trackDownloadableArtifact : ( ) => 'artifact-id' ,
174- dispatchCommand : async ( ) => {
175- dispatchCalls += 1 ;
176- return { } ;
177- } ,
178181 } ) ;
179182
180183 const response = await handler ( {
@@ -193,7 +196,7 @@ test('batch preserves tenant-scoped session names across nested requests', async
193196 } ,
194197 } ) ;
195198
196- assert . equal ( response . ok , true ) ;
197- assert . equal ( dispatchCalls , 1 ) ;
198- assert . equal ( sessionStore . get ( 'tenant-a:default' ) ?. actions . at ( - 1 ) ?. command , 'home' ) ;
199+ expect ( response . ok ) . toBe ( true ) ;
200+ expect ( dispatchCalls ) . toBe ( 1 ) ;
201+ expect ( sessionStore . get ( 'tenant-a:default' ) ?. actions . at ( - 1 ) ?. command ) . toBe ( 'home' ) ;
199202} ) ;
0 commit comments