|
1 | | -import { describe, it, expect } from 'bun:test' |
| 1 | +import { describe, it, expect, beforeEach, afterEach } from 'bun:test' |
| 2 | +import * as fs from 'fs' |
| 3 | +import * as os from 'os' |
| 4 | +import * as path from 'path' |
2 | 5 |
|
3 | 6 | import { runHeadless } from '../run-headless' |
| 7 | +import { loadHeadlessRunState, saveHeadlessRunState } from '../session-store' |
| 8 | +import { setProjectRoot } from '../../project-files' |
4 | 9 |
|
5 | 10 | import type { PrintModeEvent } from '@levelcode/common/types/print-mode' |
6 | | -import type { LevelCodeClient } from '@levelcode/sdk' |
| 11 | +import type { LevelCodeClient, RunState } from '@levelcode/sdk' |
7 | 12 |
|
8 | 13 | type Sink = { stdout: string[]; stderr: string[] } |
9 | 14 |
|
@@ -162,6 +167,108 @@ describe('runHeadless', () => { |
162 | 167 | }) |
163 | 168 | }) |
164 | 169 |
|
| 170 | +describe('runHeadless session resume', () => { |
| 171 | + let tmpDir: string |
| 172 | + beforeEach(() => { |
| 173 | + tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'hl-resume-')) |
| 174 | + setProjectRoot(tmpDir) |
| 175 | + }) |
| 176 | + afterEach(() => { |
| 177 | + fs.rmSync(tmpDir, { recursive: true, force: true }) |
| 178 | + }) |
| 179 | + |
| 180 | + it('errors with exit code 2 when --continue finds no session', async () => { |
| 181 | + const { capture } = makeSink() |
| 182 | + const { exitCode, result } = await runHeadless({ |
| 183 | + prompt: 'next turn', |
| 184 | + outputFormat: 'json', |
| 185 | + agentOverride: null, |
| 186 | + continueChat: true, |
| 187 | + client: fakeClient([]), |
| 188 | + sink: capture, |
| 189 | + }) |
| 190 | + |
| 191 | + expect(exitCode).toBe(2) |
| 192 | + expect(result.is_error).toBe(true) |
| 193 | + expect(String(result.message)).toContain('No resumable session') |
| 194 | + }) |
| 195 | + |
| 196 | + it('passes the loaded RunState as previousRun to the client', async () => { |
| 197 | + const state = { sessionState: { mainAgentState: { messageHistory: [] } } } as unknown as RunState |
| 198 | + const chatId = saveHeadlessRunState(state) |
| 199 | + expect(chatId).toBeTruthy() |
| 200 | + |
| 201 | + let sawPreviousRun: unknown |
| 202 | + const client = { |
| 203 | + run: async (opts: any) => { |
| 204 | + sawPreviousRun = opts.previousRun |
| 205 | + await opts.handleEvent?.({ type: 'text', text: 'resumed' }) |
| 206 | + return state |
| 207 | + }, |
| 208 | + } as unknown as LevelCodeClient |
| 209 | + |
| 210 | + const { exitCode, result } = await runHeadless({ |
| 211 | + prompt: 'next turn', |
| 212 | + outputFormat: 'json', |
| 213 | + agentOverride: null, |
| 214 | + continueChat: true, |
| 215 | + continueId: chatId!, |
| 216 | + client, |
| 217 | + sink: makeSink().capture, |
| 218 | + }) |
| 219 | + |
| 220 | + expect(exitCode).toBe(0) |
| 221 | + // Saved state round-trips through JSON, so compare by structure. |
| 222 | + expect(sawPreviousRun).toEqual(state) |
| 223 | + expect(result.result).toBe('resumed') |
| 224 | + }) |
| 225 | + |
| 226 | + it('saves the finished run and reports session_id on success', async () => { |
| 227 | + const state = { sessionState: { mainAgentState: { messageHistory: [] } } } as unknown as RunState |
| 228 | + const client = { |
| 229 | + run: async ({ handleEvent }: any) => { |
| 230 | + await handleEvent?.({ type: 'text', text: 'done' }) |
| 231 | + return state |
| 232 | + }, |
| 233 | + } as unknown as LevelCodeClient |
| 234 | + |
| 235 | + const { capture } = makeSink() |
| 236 | + const { exitCode, result } = await runHeadless({ |
| 237 | + prompt: 'first turn', |
| 238 | + outputFormat: 'json', |
| 239 | + agentOverride: null, |
| 240 | + client, |
| 241 | + sink: capture, |
| 242 | + }) |
| 243 | + |
| 244 | + expect(exitCode).toBe(0) |
| 245 | + expect(result.session_id).toMatch(/^[0-9a-f-]{36}$/) |
| 246 | + |
| 247 | + // The saved session is resumable |
| 248 | + const loaded = loadHeadlessRunState(result.session_id as string) |
| 249 | + expect(loaded).not.toBeNull() |
| 250 | + }) |
| 251 | + |
| 252 | + it('does not save a session after a failed run', async () => { |
| 253 | + const client = { |
| 254 | + run: async () => { |
| 255 | + throw new Error('boom') |
| 256 | + }, |
| 257 | + } as unknown as LevelCodeClient |
| 258 | + |
| 259 | + const { result } = await runHeadless({ |
| 260 | + prompt: 'failing', |
| 261 | + outputFormat: 'json', |
| 262 | + agentOverride: null, |
| 263 | + client, |
| 264 | + sink: makeSink().capture, |
| 265 | + }) |
| 266 | + |
| 267 | + expect(result.is_error).toBe(true) |
| 268 | + expect(result.session_id).toBeUndefined() |
| 269 | + }) |
| 270 | +}) |
| 271 | + |
165 | 272 | describe('runHeadless failure contract', () => { |
166 | 273 | it('reports failure when the run finishes without any output or finish event', async () => { |
167 | 274 | const { sink, capture } = makeSink() |
|
0 commit comments