Skip to content

Commit 66eefe3

Browse files
committed
refactor: add onData to SpawnOptions for isolated output handling in tests
1 parent 4935b8e commit 66eefe3

4 files changed

Lines changed: 19 additions & 35 deletions

File tree

src/plugin/pty/manager.ts

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,6 @@ export function onRawOutput(callback: RawOutputCallback): void {
1919
rawOutputCallbacks.push(callback)
2020
}
2121

22-
export function clearRawOutputCallbacks(): void {
23-
rawOutputCallbacks.length = 0
24-
}
25-
2622
function notifyRawOutput(sessionId: string, rawData: string): void {
2723
for (const callback of rawOutputCallbacks) {
2824
try {
@@ -48,15 +44,16 @@ class PTYManager {
4844
console.log(`${new Date().toISOString()}: Manager spawn called with opts:`, opts)
4945
const session = this.lifecycleManager.spawn(
5046
opts,
51-
(id, data) => {
52-
console.log(
53-
`${new Date().toISOString()}: Manager onData callback for id:`,
54-
id,
55-
'data length:',
56-
data.length
57-
)
58-
notifyRawOutput(id, data)
59-
},
47+
opts.onData ||
48+
((id, data) => {
49+
console.log(
50+
`${new Date().toISOString()}: Manager onData callback for id:`,
51+
id,
52+
'data length:',
53+
data.length
54+
)
55+
notifyRawOutput(id, data)
56+
}),
6057
async (id, exitCode) => {
6158
console.log(
6259
`${new Date().toISOString()}: Manager onExit callback for id:`,

src/plugin/pty/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ export interface SpawnOptions {
4444
description?: string
4545
parentSessionId: string
4646
notifyOnExit?: boolean
47+
onData?: (sessionId: string, data: string) => void
4748
}
4849

4950
export interface ReadResult {

test/pty-echo.test.ts

Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,5 @@
11
import { describe, it, expect, beforeEach, afterEach } from 'bun:test'
2-
import {
3-
initManager,
4-
manager,
5-
onRawOutput,
6-
clearRawOutputCallbacks,
7-
} from '../src/plugin/pty/manager.ts'
2+
import { initManager, manager } from '../src/plugin/pty/manager.ts'
83

94
describe('PTY Echo Behavior', () => {
105
const fakeClient = {
@@ -17,29 +12,25 @@ describe('PTY Echo Behavior', () => {
1712

1813
beforeEach(() => {
1914
initManager(fakeClient)
20-
clearRawOutputCallbacks()
2115
})
2216

2317
afterEach(() => {
2418
// Clean up any sessions
2519
manager.clearAllSessions()
26-
clearRawOutputCallbacks()
2720
})
2821

2922
it('should echo input characters in interactive bash session', async () => {
3023
const receivedOutputs: string[] = []
3124

32-
// Subscribe to raw output events
33-
onRawOutput((_sessionId, rawData) => {
34-
receivedOutputs.push(rawData)
35-
})
36-
3725
// Spawn interactive bash session
3826
const session = manager.spawn({
3927
command: 'bash',
4028
args: ['-i'],
4129
description: 'Echo test session',
4230
parentSessionId: 'test',
31+
onData: (_sessionId, rawData) => {
32+
receivedOutputs.push(rawData)
33+
},
4334
})
4435

4536
console.log('Echo session:', session)
@@ -72,17 +63,15 @@ describe('PTY Echo Behavior', () => {
7263
it('should echo different input characters in interactive bash session', async () => {
7364
const receivedOutputs: string[] = []
7465

75-
// Subscribe to raw output events
76-
onRawOutput((_sessionId, rawData) => {
77-
receivedOutputs.push(rawData)
78-
})
79-
8066
// Spawn interactive bash session
8167
const session = manager.spawn({
8268
command: 'bash',
8369
args: ['-i'],
8470
description: 'Echo test session 2',
8571
parentSessionId: 'test2',
72+
onData: (_sessionId, rawData) => {
73+
receivedOutputs.push(rawData)
74+
},
8675
})
8776

8877
console.log('Echo session 2:', session)

test/web-server.test.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { describe, it, expect, beforeEach, afterEach } from 'bun:test'
22
import { startWebServer, stopWebServer, getServerUrl } from '../src/web/server/server.ts'
3-
import { initManager, manager, clearRawOutputCallbacks } from '../src/plugin/pty/manager.ts'
3+
import { initManager, manager } from '../src/plugin/pty/manager.ts'
44

55
describe('Web Server', () => {
66
const fakeClient = {
@@ -13,13 +13,11 @@ describe('Web Server', () => {
1313

1414
beforeEach(() => {
1515
initManager(fakeClient)
16-
clearRawOutputCallbacks()
1716
})
1817

1918
afterEach(() => {
2019
stopWebServer()
2120
manager.cleanupAll() // Ensure cleanup after each test
22-
clearRawOutputCallbacks()
2321
})
2422

2523
describe('Server Lifecycle', () => {
@@ -53,7 +51,6 @@ describe('Web Server', () => {
5351

5452
beforeEach(async () => {
5553
manager.cleanupAll() // Clean up any leftover sessions
56-
clearRawOutputCallbacks()
5754
serverUrl = await startWebServer({ port: 8771 })
5855
})
5956

0 commit comments

Comments
 (0)