-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsession-runner.test.js
More file actions
192 lines (157 loc) · 6.29 KB
/
Copy pathsession-runner.test.js
File metadata and controls
192 lines (157 loc) · 6.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
const { describe, it } = require('node:test');
const assert = require('node:assert');
const { EventEmitter } = require('events');
const { createSessionRunner } = require('./session-runner');
function createMockChild() {
const stdout = new EventEmitter();
stdout.removeAllListeners = function(...args) {
EventEmitter.prototype.removeAllListeners.apply(this, args);
};
const child = new EventEmitter();
child.stdout = stdout;
child.kill = () => {};
return child;
}
describe('session-runner boundary', () => {
it('resolves with sessionId from step_start event', async () => {
let emittedChild = null;
const mockSpawn = () => {
emittedChild = createMockChild();
return emittedChild;
};
const runner = createSessionRunner({ spawn: mockSpawn });
const promise = runner.createSession({ prompt: 'Hello world' });
// Emit a step_start event with session_id
emittedChild.stdout.emit('data', JSON.stringify({ type: 'step_start', session_id: 'ses_abc123' }) + '\n');
const result = await promise;
assert.strictEqual(result.sessionId, 'ses_abc123');
assert.strictEqual(result.processing, true);
});
it('resolves with sessionId from camelCase sessionId field', async () => {
let emittedChild = null;
const mockSpawn = () => {
emittedChild = createMockChild();
return emittedChild;
};
const runner = createSessionRunner({ spawn: mockSpawn });
const promise = runner.createSession({ prompt: 'Test prompt' });
emittedChild.stdout.emit('data', JSON.stringify({ type: 'step_start', sessionId: 'ses_xyz789' }) + '\n');
const result = await promise;
assert.strictEqual(result.sessionId, 'ses_xyz789');
});
it('ignores non-step_start events until step_start appears', async () => {
let emittedChild = null;
const mockSpawn = () => {
emittedChild = createMockChild();
return emittedChild;
};
const runner = createSessionRunner({ spawn: mockSpawn });
const promise = runner.createSession({ prompt: 'Hello' });
emittedChild.stdout.emit('data', JSON.stringify({ type: 'log', message: 'starting' }) + '\n');
emittedChild.stdout.emit('data', JSON.stringify({ type: 'step_start', session_id: 'ses_delayed' }) + '\n');
const result = await promise;
assert.strictEqual(result.sessionId, 'ses_delayed');
});
it('ignores non-JSON lines', async () => {
let emittedChild = null;
const mockSpawn = () => {
emittedChild = createMockChild();
return emittedChild;
};
const runner = createSessionRunner({ spawn: mockSpawn });
const promise = runner.createSession({ prompt: 'Hello' });
emittedChild.stdout.emit('data', 'some log output\n');
emittedChild.stdout.emit('data', JSON.stringify({ type: 'step_start', session_id: 'ses_after_log' }) + '\n');
const result = await promise;
assert.strictEqual(result.sessionId, 'ses_after_log');
});
it('handles split NDJSON lines across data chunks', async () => {
let emittedChild = null;
const mockSpawn = () => {
emittedChild = createMockChild();
return emittedChild;
};
const runner = createSessionRunner({ spawn: mockSpawn });
const promise = runner.createSession({ prompt: 'Hello' });
const event = JSON.stringify({ type: 'step_start', session_id: 'ses_split' });
emittedChild.stdout.emit('data', event.substring(0, 10));
emittedChild.stdout.emit('data', event.substring(10) + '\n');
const result = await promise;
assert.strictEqual(result.sessionId, 'ses_split');
});
it('rejects when prompt is missing', async () => {
const runner = createSessionRunner({ spawn: () => createMockChild() });
await assert.rejects(
() => runner.createSession({}),
/Prompt is required/
);
});
it('rejects when prompt is empty string', async () => {
const runner = createSessionRunner({ spawn: () => createMockChild() });
await assert.rejects(
() => runner.createSession({ prompt: ' ' }),
/Prompt is required/
);
});
it('rejects when spawn errors', async () => {
const mockSpawn = () => {
const child = createMockChild();
setTimeout(() => child.emit('error', new Error('spawn opencode ENOENT')), 0);
return child;
};
const runner = createSessionRunner({ spawn: mockSpawn });
await assert.rejects(
() => runner.createSession({ prompt: 'Hello' }),
/spawn opencode ENOENT/
);
});
it('rejects when process exits without session ID', async () => {
const mockSpawn = () => {
const child = createMockChild();
setTimeout(() => child.emit('close', 1), 0);
return child;
};
const runner = createSessionRunner({ spawn: mockSpawn });
await assert.rejects(
() => runner.createSession({ prompt: 'Hello' }),
/Process exited with code 1 without producing a session ID/
);
});
it('rejects on timeout when no session ID is produced', async () => {
const mockSpawn = () => createMockChild();
const runner = createSessionRunner({ spawn: mockSpawn, timeoutMs: 50 });
await assert.rejects(
() => runner.createSession({ prompt: 'Hello' }),
/Timeout waiting for session ID/
);
});
it('passes directory to spawn args when provided', async () => {
let capturedArgs = null;
const mockSpawn = (_cmd, args) => {
capturedArgs = args;
const child = createMockChild();
setTimeout(() => {
child.stdout.emit('data', JSON.stringify({ type: 'step_start', session_id: 'ses_dir' }) + '\n');
}, 0);
return child;
};
const runner = createSessionRunner({ spawn: mockSpawn });
await runner.createSession({ prompt: 'Hello', directory: '/home/dsp/project' });
assert.ok(capturedArgs.includes('--dir'));
assert.ok(capturedArgs.includes('/home/dsp/project'));
});
it('does not pass directory arg when not provided', async () => {
let capturedArgs = null;
const mockSpawn = (_cmd, args) => {
capturedArgs = args;
const child = createMockChild();
setTimeout(() => {
child.stdout.emit('data', JSON.stringify({ type: 'step_start', session_id: 'ses_nodir' }) + '\n');
}, 0);
return child;
};
const runner = createSessionRunner({ spawn: mockSpawn });
await runner.createSession({ prompt: 'Hello' });
assert.ok(!capturedArgs.includes('--dir'));
});
});