1+ import { StdioClientTransport } from "./stdio.js" ;
2+ import spawn from "cross-spawn" ;
3+
4+ // mock cross-spawn
5+ jest . mock ( "cross-spawn" ) ;
6+ const mockSpawn = spawn as jest . MockedFunction < typeof spawn > ;
7+
8+ describe ( "StdioClientTransport using cross-spawn" , ( ) => {
9+ beforeEach ( ( ) => {
10+ // mock cross-spawn's return value
11+ mockSpawn . mockImplementation ( ( ) => {
12+ const mockProcess = {
13+ on : jest . fn ( ( event : string , callback : Function ) => {
14+ if ( event === "spawn" ) {
15+ callback ( ) ;
16+ }
17+ return mockProcess ;
18+ } ) ,
19+ stdin : {
20+ on : jest . fn ( ) ,
21+ write : jest . fn ( ) . mockReturnValue ( true )
22+ } ,
23+ stdout : {
24+ on : jest . fn ( )
25+ } ,
26+ stderr : null
27+ } ;
28+ return mockProcess as any ;
29+ } ) ;
30+ } ) ;
31+
32+ afterEach ( ( ) => {
33+ jest . clearAllMocks ( ) ;
34+ } ) ;
35+
36+ test ( "should call cross-spawn correctly" , async ( ) => {
37+ const transport = new StdioClientTransport ( {
38+ command : "test-command" ,
39+ args : [ "arg1" , "arg2" ]
40+ } ) ;
41+
42+ await transport . start ( ) ;
43+
44+ // verify spawn is called correctly
45+ expect ( mockSpawn ) . toHaveBeenCalledWith (
46+ "test-command" ,
47+ [ "arg1" , "arg2" ] ,
48+ expect . objectContaining ( {
49+ shell : false
50+ } )
51+ ) ;
52+ } ) ;
53+
54+ test ( "should pass environment variables correctly" , async ( ) => {
55+ const customEnv = { TEST_VAR : "test-value" } ;
56+ const transport = new StdioClientTransport ( {
57+ command : "test-command" ,
58+ env : customEnv
59+ } ) ;
60+
61+ await transport . start ( ) ;
62+
63+ // verify environment variables are passed correctly
64+ expect ( mockSpawn ) . toHaveBeenCalledWith (
65+ "test-command" ,
66+ [ ] ,
67+ expect . objectContaining ( {
68+ env : customEnv
69+ } )
70+ ) ;
71+ } ) ;
72+
73+ test ( "should send messages correctly" , async ( ) => {
74+ const transport = new StdioClientTransport ( {
75+ command : "test-command"
76+ } ) ;
77+
78+ // get the mock process object
79+ const mockProcess = {
80+ on : jest . fn ( ( event , callback ) => {
81+ if ( event === "spawn" ) {
82+ callback ( ) ;
83+ }
84+ return mockProcess ;
85+ } ) ,
86+ stdin : {
87+ on : jest . fn ( ) ,
88+ write : jest . fn ( ) . mockReturnValue ( true ) ,
89+ once : jest . fn ( )
90+ } ,
91+ stdout : {
92+ on : jest . fn ( )
93+ } ,
94+ stderr : null
95+ } ;
96+
97+ mockSpawn . mockReturnValue ( mockProcess as any ) ;
98+
99+ await transport . start ( ) ;
100+
101+ const message = {
102+ jsonrpc : "2.0" ,
103+ id : "test-id" ,
104+ method : "test-method"
105+ } ;
106+
107+ await transport . send ( message ) ;
108+
109+ // verify message is sent correctly
110+ expect ( mockProcess . stdin . write ) . toHaveBeenCalled ( ) ;
111+ } ) ;
112+ } ) ;
0 commit comments