11import { describe , it , expect , beforeEach , afterEach } from 'bun:test'
22import { startWebServer , stopWebServer , getServerUrl } from '../src/web/server/server.ts'
33import { PTYManager } from '../src/plugin/pty/manager.ts'
4+ import { manager } from '../src/plugin/pty/manager.ts'
45
56describe . serial ( 'Web Server' , ( ) => {
67 const fakeClient = {
@@ -11,16 +12,14 @@ describe.serial('Web Server', () => {
1112 } ,
1213 } as any
1314
14- let testManager : PTYManager
15-
1615 beforeEach ( ( ) => {
1716 testManager = new PTYManager ( )
1817 testManager . init ( fakeClient )
1918 } )
2019
2120 afterEach ( ( ) => {
2221 stopWebServer ( )
23- testManager . cleanupAll ( ) // Ensure cleanup after each test
22+ manager . clearAllSessions ( ) // Ensure cleanup after each test
2423 } )
2524
2625 describe ( 'Server Lifecycle' , ( ) => {
@@ -53,8 +52,8 @@ describe.serial('Web Server', () => {
5352 let serverUrl : string
5453
5554 beforeEach ( async ( ) => {
56- testManager . cleanupAll ( ) // Clean up any leftover sessions
57- serverUrl = await startWebServer ( { port : 8771 } , testManager )
55+ manager . clearAllSessions ( ) // Clean up any leftover sessions
56+ serverUrl = await startWebServer ( { port : 8771 } )
5857 } )
5958
6059 it ( 'should serve built assets when NODE_ENV=test' , async ( ) => {
@@ -133,13 +132,31 @@ describe.serial('Web Server', () => {
133132
134133 it ( 'should return individual session' , async ( ) => {
135134 // Create a test session first
136- const session = testManager . spawn ( {
137- command : 'cat ' ,
138- args : [ ] ,
135+ const session = manager . spawn ( {
136+ command : 'sleep ' ,
137+ args : [ '1' ] ,
139138 description : 'Test session' ,
140139 parentSessionId : 'test' ,
141140 } )
142141
142+ console . log ( 'Created session:' , session )
143+ const fullSession = manager . get ( session . id )
144+ console . log ( 'Session from manager.get:' , fullSession )
145+
146+ // Wait for PTY to start
147+ await new Promise ( ( resolve ) => setTimeout ( resolve , 100 ) )
148+
149+ const response = await fetch ( `${ serverUrl } /api/sessions/${ session . id } ` )
150+ console . log ( 'Session response status:' , response . status )
151+ expect ( response . status ) . toBe ( 200 )
152+
153+ const sessionData = await response . json ( )
154+ console . log ( 'Session data:' , sessionData )
155+ expect ( sessionData . id ) . toBe ( session . id )
156+ expect ( sessionData . command ) . toBe ( 'sleep' )
157+ expect ( sessionData . args ) . toEqual ( [ '1' ] )
158+ } )
159+
143160 console . log ( 'Created session:' , session )
144161 const fullSession = testManager . get ( session . id )
145162 console . log ( 'Session from manager.get:' , fullSession )
@@ -154,8 +171,8 @@ describe.serial('Web Server', () => {
154171 const sessionData = await response . json ( )
155172 console . log ( 'Session data:' , sessionData )
156173 expect ( sessionData . id ) . toBe ( session . id )
157- expect ( sessionData . command ) . toBe ( 'cat ' )
158- expect ( sessionData . args ) . toEqual ( [ ] )
174+ expect ( sessionData . command ) . toBe ( 'sleep ' )
175+ expect ( sessionData . args ) . toEqual ( [ '1' ] )
159176 } )
160177
161178 it ( 'should return 404 for non-existent session' , async ( ) => {
@@ -168,7 +185,7 @@ describe.serial('Web Server', () => {
168185
169186 it ( 'should handle input to session' , async ( ) => {
170187 // Create a session to test input
171- const session = testManager . spawn ( {
188+ const session = manager . spawn ( {
172189 command : 'cat' ,
173190 args : [ ] ,
174191 description : 'Test session' ,
@@ -194,11 +211,11 @@ describe.serial('Web Server', () => {
194211 expect ( result ) . toHaveProperty ( 'success' , true )
195212
196213 // Clean up
197- testManager . kill ( session . id , true )
214+ manager . kill ( session . id , true )
198215 } )
199216
200217 it ( 'should handle kill session' , async ( ) => {
201- const session = testManager . spawn ( {
218+ const session = manager . spawn ( {
202219 command : 'sleep' ,
203220 args : [ '1' ] ,
204221 description : 'Test session' ,
@@ -224,8 +241,8 @@ describe.serial('Web Server', () => {
224241 it ( 'should return session output' , async ( ) => {
225242 // Create a session that produces output
226243 const session = testManager . spawn ( {
227- command : 'echo ' ,
228- args : [ 'line1\nline2\nline3 ' ] ,
244+ command : 'sh ' ,
245+ args : [ '-c' , 'echo " line1"; echo "line2"; echo "line3" '] ,
229246 description : 'Test session with output' ,
230247 parentSessionId : 'test-output' ,
231248 } )
0 commit comments