11import { CodexAcpClient } from '../CodexAcpClient' ;
2- import { type ClientTransportEvent , CodexAppServerClient } from '../CodexAppServerClient' ;
2+ import { type CodexConnectionEvent , CodexAppServerClient } from '../CodexAppServerClient' ;
33import { startCodexConnection } from "../CodexJsonRpcConnection" ;
44import { CodexACPAgent } from "../CodexACPAgent" ;
5+ import type { AgentSideConnection } from "@agentclientprotocol/sdk" ;
6+
7+ export type MethodCallEvent = { method : string ; args : any [ ] } ;
8+
9+ function createSmartMock < T extends object > ( onCall : ( event : MethodCallEvent ) => void ) {
10+ return new Proxy ( { } as T , {
11+ get ( _ , prop ) {
12+ return ( ...args : any [ ] ) => {
13+ onCall ( { method : String ( prop ) , args } ) ;
14+ return { mock : "Mocked return" } ;
15+ } ;
16+ }
17+ } ) ;
18+ }
519
620export interface TestFixture {
721 getCodexAppServerClient ( ) : CodexAppServerClient ,
822 getCodexAcpClient ( ) : CodexAcpClient ,
923 getCodexAcpAgent ( ) : CodexACPAgent ,
10- getTransportEvents ( ) : ClientTransportEvent [ ] ,
11- getTransportDump ( ignoredFields : string [ ] ) : string ,
12- clearTransportDump ( ) : void
24+
25+ onCodexConnectionEvent ( handler : ( event : CodexConnectionEvent ) => void ) : void ,
26+ getCodexConnectionDump ( ignoredFields : string [ ] ) : string ,
27+ clearCodexConnectionDump ( ) : void ,
28+
29+ onAcpConnectionEvent ( handler : ( event : MethodCallEvent ) => void ) : void ,
30+ getAcpConnectionDump ( ignoredFields : string [ ] ) : string ,
31+ clearAcpConnectionDump ( ) : void ,
1332}
1433
1534export function createTestFixture ( ) : TestFixture {
1635 const pathToCodex = "././node_modules/.bin/codex"
17- const mockedAcpConnection = { } as any ;
36+ const acpConnectionEvents : MethodCallEvent [ ] = [ ]
37+ const acpEventHandlers : ( ( event : MethodCallEvent ) => void ) [ ] = [ ] ;
38+ const acpConnection = createSmartMock < AgentSideConnection > ( ( event ) => {
39+ acpConnectionEvents . push ( event ) ;
40+ acpEventHandlers . forEach ( handler => handler ( event ) ) ;
41+ } ) ;
1842 const codexAppServerClient = new CodexAppServerClient ( startCodexConnection ( pathToCodex ) ) ;
1943
2044 const codexAcpClient = new CodexAcpClient ( codexAppServerClient ) ;
21- const codexAcpAgent = new CodexACPAgent ( mockedAcpConnection , codexAcpClient ) ;
45+ const codexAcpAgent = new CodexACPAgent ( acpConnection , codexAcpClient ) ;
2246
23- const transportEvents : ClientTransportEvent [ ] = [ ]
47+ const transportEvents : CodexConnectionEvent [ ] = [ ]
48+ const codexEventHandlers : ( ( event : CodexConnectionEvent ) => void ) [ ] = [ ] ;
2449 codexAppServerClient . onClientTransportEvent ( ( event ) => {
2550 transportEvents . push ( event ) ;
51+ codexEventHandlers . forEach ( handler => handler ( event ) ) ;
2652 } ) ;
2753
2854 return {
@@ -32,23 +58,38 @@ export function createTestFixture(): TestFixture {
3258 getCodexAcpClient ( ) : CodexAcpClient {
3359 return codexAcpClient ;
3460 } ,
35- getTransportDump ( ignoredFields : string [ ] ) : string {
36- function stringify ( obj : any , anonymizedFields : string [ ] = [ ] ) {
37- function fieldAnonymizer ( key : string , value : any ) : any {
38- return anonymizedFields . includes ( key ) ? key : value ;
39- }
40- return JSON . stringify ( obj , fieldAnonymizer , 2 ) ;
41- }
42- return this . getTransportEvents ( ) . map ( event => stringify ( event , ignoredFields ) ) . join ( "\n" ) ;
61+ getCodexConnectionDump ( ignoredFields : string [ ] ) : string {
62+ return createArrayDump ( transportEvents , ignoredFields ) ;
4363 } ,
44- getTransportEvents ( ) : ClientTransportEvent [ ] {
45- return transportEvents ;
64+ onCodexConnectionEvent ( handler : ( event : CodexConnectionEvent ) => void ) : void {
65+ codexEventHandlers . push ( handler ) ;
4666 } ,
4767 getCodexAppServerClient ( ) : CodexAppServerClient {
4868 return codexAppServerClient ;
4969 } ,
50- clearTransportDump ( ) : void {
70+ clearCodexConnectionDump ( ) : void {
5171 transportEvents . splice ( 0 , transportEvents . length ) ;
72+ } ,
73+ onAcpConnectionEvent ( handler : ( event : MethodCallEvent ) => void ) : void {
74+ acpEventHandlers . push ( handler ) ;
75+ } ,
76+ getAcpConnectionDump ( ignoredFields : string [ ] ) : string {
77+ return createArrayDump ( acpConnectionEvents , ignoredFields ) ;
78+ } ,
79+ clearAcpConnectionDump ( ) {
80+ acpConnectionEvents . splice ( 0 , acpConnectionEvents . length ) ;
5281 }
5382 } ;
83+ }
84+
85+
86+ function createObjectDump ( obj : any , anonymizedFields : string [ ] = [ ] ) {
87+ function fieldAnonymizer ( key : string , value : any ) : any {
88+ return anonymizedFields . includes ( key ) ? key : value ;
89+ }
90+ return JSON . stringify ( obj , fieldAnonymizer , 2 ) ;
91+ }
92+
93+ function createArrayDump ( objects : any [ ] , anonymizedFields : string [ ] ) : string {
94+ return objects . map ( event => createObjectDump ( event , anonymizedFields ) ) . join ( "\n" ) ;
5495}
0 commit comments