-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathheadless.test.ts
More file actions
90 lines (83 loc) · 3.2 KB
/
Copy pathheadless.test.ts
File metadata and controls
90 lines (83 loc) · 3.2 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
// Tests for headless one-shot mode.
//
// These tests stub the DeepSeek API by injecting a fake provider via the
// underlying agent. Since the agent loop is in @deepcode/core, and runHeadless
// constructs its own DeepSeekProvider, we can't easily mock the provider
// without dependency injection. Instead these tests focus on:
// 1. Wiring — runHeadless can be imported, exit code path is correct on
// common error conditions (no creds).
// 2. Output formatter helpers — exposed indirectly via integration through
// a fake event stream (testing the format selection logic only).
//
// Full end-to-end is exercised by docs/m1-validation.md (real API live tests).
import { mkdtemp, rm } from 'node:fs/promises';
import { tmpdir } from 'node:os';
import { join } from 'node:path';
import { PassThrough } from 'node:stream';
import { afterEach, beforeEach, describe, expect, it } from 'vitest';
import { runHeadless } from './headless.js';
function streamToString(s: PassThrough): Promise<string> {
return new Promise((resolve) => {
const chunks: Buffer[] = [];
s.on('data', (c) => chunks.push(c as Buffer));
s.on('end', () => resolve(Buffer.concat(chunks).toString('utf8')));
});
}
describe('runHeadless — early-exit paths (no API call)', () => {
let home: string;
let cwd: string;
let savedKey: string | undefined;
let savedToken: string | undefined;
let savedHelper: string | undefined;
beforeEach(async () => {
home = await mkdtemp(join(tmpdir(), 'dc-headless-home-'));
cwd = await mkdtemp(join(tmpdir(), 'dc-headless-cwd-'));
savedKey = process.env['DEEPSEEK_API_KEY'];
savedToken = process.env['DEEPSEEK_AUTH_TOKEN'];
savedHelper = process.env['DEEPCODE_API_KEY_HELPER'];
delete process.env['DEEPSEEK_API_KEY'];
delete process.env['DEEPSEEK_AUTH_TOKEN'];
delete process.env['DEEPCODE_API_KEY_HELPER'];
});
afterEach(async () => {
await rm(home, { recursive: true, force: true });
await rm(cwd, { recursive: true, force: true });
if (savedKey !== undefined) process.env['DEEPSEEK_API_KEY'] = savedKey;
if (savedToken !== undefined) process.env['DEEPSEEK_AUTH_TOKEN'] = savedToken;
if (savedHelper !== undefined) process.env['DEEPCODE_API_KEY_HELPER'] = savedHelper;
});
it('exits 3 when no credentials are present', async () => {
const out = new PassThrough();
const err = new PassThrough();
const code = await runHeadless({
output: out,
errOutput: err,
cwd,
home,
prompt: 'hello',
outputFormat: 'text',
});
out.end();
err.end();
expect(code).toBe(3);
const errStr = await streamToString(err);
expect(errStr).toMatch(/no DeepSeek credentials/i);
});
it('json format emits a JSON error object when creds missing', async () => {
// Even on creds-missing, this returns 3 with an err message to stderr —
// stdout stays empty because we exit before runAgent. Verifying error path.
const out = new PassThrough();
const err = new PassThrough();
const code = await runHeadless({
output: out,
errOutput: err,
cwd,
home,
prompt: 'hi',
outputFormat: 'json',
});
out.end();
err.end();
expect(code).toBe(3);
});
});