-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathkilo-cli-config.test.ts
More file actions
266 lines (218 loc) · 9.75 KB
/
kilo-cli-config.test.ts
File metadata and controls
266 lines (218 loc) · 9.75 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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
import { describe, it, expect, vi } from 'vitest';
import { writeKiloCliConfig, toKiloModelId, type KiloCliConfigDeps } from './kilo-cli-config';
function fakeDeps(existingConfig?: string) {
const written: { path: string; data: string; mode: number }[] = [];
const dirs: string[] = [];
const deps: KiloCliConfigDeps = {
mkdirSync: vi.fn((dir: string, _opts: { recursive: boolean }) => {
dirs.push(dir);
}),
writeFileSync: vi.fn((filePath: string, data: string, opts: { mode: number }) => {
written.push({ path: filePath, data, mode: opts.mode });
}),
readFileSync: vi.fn((_path: string) => {
if (existingConfig !== undefined) return existingConfig;
throw new Error('ENOENT');
}),
existsSync: vi.fn((filePath: string) => {
if (filePath.endsWith('opencode.json')) return existingConfig !== undefined;
return false;
}),
};
return { deps, written, dirs };
}
function baseEnv(overrides: Record<string, string> = {}): Record<string, string | undefined> {
return {
KILOCLAW_KILO_CLI: 'true',
KILOCODE_API_KEY: 'test-jwt-token',
KILOCLAW_FRESH_INSTALL: 'true',
...overrides,
};
}
describe('toKiloModelId', () => {
it('replaces kilocode/ prefix with kilo/', () => {
expect(toKiloModelId('kilocode/anthropic/claude-opus-4.6')).toBe(
'kilo/anthropic/claude-opus-4.6'
);
expect(toKiloModelId('kilocode/openai/gpt-5')).toBe('kilo/openai/gpt-5');
});
it('passes through values without kilocode/ prefix', () => {
expect(toKiloModelId('kilo/anthropic/claude-opus-4.6')).toBe('kilo/anthropic/claude-opus-4.6');
expect(toKiloModelId('other/model')).toBe('other/model');
});
});
describe('writeKiloCliConfig', () => {
it('returns false when feature flag is disabled', () => {
const { deps, written } = fakeDeps();
const result = writeKiloCliConfig({ KILOCLAW_KILO_CLI: 'false' }, '/tmp/kilo', deps);
expect(result).toBe(false);
expect(written).toHaveLength(0);
});
it('returns false when feature flag is not set', () => {
const { deps, written } = fakeDeps();
const result = writeKiloCliConfig({}, '/tmp/kilo', deps);
expect(result).toBe(false);
expect(written).toHaveLength(0);
});
it('returns false when KILOCODE_API_KEY is missing', () => {
const { deps, written } = fakeDeps();
const result = writeKiloCliConfig({ KILOCLAW_KILO_CLI: 'true' }, '/tmp/kilo', deps);
expect(result).toBe(false);
expect(written).toHaveLength(0);
});
it('seeds config on fresh install with no existing config', () => {
const { deps, written, dirs } = fakeDeps();
const result = writeKiloCliConfig(baseEnv(), '/tmp/kilo', deps);
expect(result).toBe(true);
expect(dirs).toContain('/tmp/kilo');
expect(deps.mkdirSync).toHaveBeenCalledWith('/tmp/kilo', { recursive: true });
expect(written.length).toBeGreaterThanOrEqual(1);
const seedConfig = JSON.parse(written[0].data);
expect(seedConfig.$schema).toBe('https://app.kilo.ai/config.json');
// No provider block — KiloAuthPlugin auto-registers via KILO_API_KEY env var
expect(seedConfig.provider).toBeUndefined();
// No model when KILOCODE_DEFAULT_MODEL is not set
expect(seedConfig.model).toBeUndefined();
expect(seedConfig.permission.edit).toBe('allow');
expect(seedConfig.permission.bash).toBe('allow');
expect(written[0].mode).toBe(0o600);
});
it('includes model in seed config when KILOCODE_DEFAULT_MODEL is set', () => {
const { deps, written } = fakeDeps();
const env = baseEnv({ KILOCODE_DEFAULT_MODEL: 'kilocode/anthropic/claude-opus-4.6' });
const result = writeKiloCliConfig(env, '/tmp/kilo', deps);
expect(result).toBe(true);
expect(written.length).toBeGreaterThanOrEqual(1);
const seedConfig = JSON.parse(written[0].data);
expect(seedConfig.model).toBe('kilo/anthropic/claude-opus-4.6');
expect(seedConfig.permission.edit).toBe('allow');
});
it('does not seed config on fresh install when config already exists', () => {
const existing = JSON.stringify({ permission: { edit: 'allow', bash: 'allow' } });
const { deps, written } = fakeDeps(existing);
const result = writeKiloCliConfig(baseEnv(), '/tmp/kilo', deps);
expect(result).toBe(true);
// No seed (file exists), no patch (no KILOCODE_API_BASE_URL)
expect(written).toHaveLength(0);
});
it('does not seed config on non-fresh boot', () => {
const { deps, written } = fakeDeps();
const env = baseEnv({ KILOCLAW_FRESH_INSTALL: 'false' });
const result = writeKiloCliConfig(env, '/tmp/kilo', deps);
expect(result).toBe(true);
// No config exists, not fresh → no seed, no patch (nothing to patch)
expect(written).toHaveLength(0);
});
it('patches base URL on existing config using provider.kilo, stripping path to origin', () => {
const existing = JSON.stringify({ permission: { edit: 'allow', bash: 'allow' } });
const { deps, written } = fakeDeps(existing);
const env = baseEnv({
KILOCLAW_FRESH_INSTALL: 'false',
KILOCODE_API_BASE_URL: 'https://tunnel.example.com/api/gateway',
});
writeKiloCliConfig(env, '/tmp/kilo', deps);
expect(written).toHaveLength(1);
const config = JSON.parse(written[0].data);
expect(config.provider.kilo.options.baseURL).toBe('https://tunnel.example.com');
});
it('patches model from KILOCODE_DEFAULT_MODEL on existing config', () => {
const existing = JSON.stringify({ permission: { edit: 'allow', bash: 'allow' } });
const { deps, written } = fakeDeps(existing);
const env = baseEnv({
KILOCLAW_FRESH_INSTALL: 'false',
KILOCODE_DEFAULT_MODEL: 'kilocode/openai/gpt-5',
});
writeKiloCliConfig(env, '/tmp/kilo', deps);
expect(written).toHaveLength(1);
const config = JSON.parse(written[0].data);
expect(config.model).toBe('kilo/openai/gpt-5');
});
it('patches both model and base URL when both are set', () => {
const existing = JSON.stringify({ permission: { edit: 'allow', bash: 'allow' } });
const { deps, written } = fakeDeps(existing);
const env = baseEnv({
KILOCLAW_FRESH_INSTALL: 'false',
KILOCODE_DEFAULT_MODEL: 'kilocode/openai/gpt-5',
KILOCODE_API_BASE_URL: 'https://tunnel.example.com/api/gateway',
});
writeKiloCliConfig(env, '/tmp/kilo', deps);
expect(written).toHaveLength(1);
const config = JSON.parse(written[0].data);
expect(config.model).toBe('kilo/openai/gpt-5');
});
it('does not set model when KILOCODE_DEFAULT_MODEL is absent', () => {
const existing = JSON.stringify({ permission: { edit: 'allow', bash: 'allow' } });
const { deps, written } = fakeDeps(existing);
const env = baseEnv({
KILOCLAW_FRESH_INSTALL: 'false',
KILOCODE_API_BASE_URL: 'https://tunnel.example.com/api/gateway',
});
writeKiloCliConfig(env, '/tmp/kilo', deps);
expect(written).toHaveLength(1);
const config = JSON.parse(written[0].data);
expect(config.model).toBeUndefined();
});
it('creates provider structure when patching base URL on minimal config', () => {
const existing = JSON.stringify({});
const { deps, written } = fakeDeps(existing);
const env = baseEnv({
KILOCLAW_FRESH_INSTALL: 'false',
KILOCODE_API_BASE_URL: 'https://tunnel.example.com/api/gateway',
});
writeKiloCliConfig(env, '/tmp/kilo', deps);
const config = JSON.parse(written[0].data);
expect(config.provider.kilo.options.baseURL).toBe('https://tunnel.example.com');
});
it('does not write when no env overrides set', () => {
const existing = JSON.stringify({ permission: { edit: 'allow' } });
const { deps, written } = fakeDeps(existing);
const env = baseEnv({ KILOCLAW_FRESH_INSTALL: 'false' });
writeKiloCliConfig(env, '/tmp/kilo', deps);
// No KILOCODE_API_BASE_URL → no patch needed, no write
expect(written).toHaveLength(0);
});
it('skips patch gracefully when config file contains corrupt JSON', () => {
const { deps, written } = fakeDeps('not valid json {{{');
const env = baseEnv({
KILOCLAW_FRESH_INSTALL: 'false',
KILOCODE_API_BASE_URL: 'https://tunnel.example.com/api/gateway',
});
const consoleSpy = vi.spyOn(console, 'error').mockImplementation(() => {});
const result = writeKiloCliConfig(env, '/tmp/kilo', deps);
expect(result).toBe(true);
expect(written).toHaveLength(0); // no write on corrupt JSON
expect(consoleSpy).toHaveBeenCalledWith(
expect.stringContaining('[kilo-cli] Failed to patch config'),
expect.any(Error)
);
consoleSpy.mockRestore();
});
it('seeds config and then patches base URL on fresh install', () => {
const { deps, written } = fakeDeps();
let seeded = false;
(deps.existsSync as ReturnType<typeof vi.fn>).mockImplementation((filePath: string) => {
if (filePath.endsWith('opencode.json')) return seeded;
return false;
});
(deps.writeFileSync as ReturnType<typeof vi.fn>).mockImplementation(
(filePath: string, data: string, opts: { mode: number }) => {
written.push({ path: filePath, data, mode: opts.mode });
if (filePath.endsWith('opencode.json')) seeded = true;
}
);
(deps.readFileSync as ReturnType<typeof vi.fn>).mockImplementation(() => {
if (seeded) return written[written.length - 1].data;
throw new Error('ENOENT');
});
const env = baseEnv({
KILOCODE_API_BASE_URL: 'https://tunnel.example.com/api/gateway',
});
const result = writeKiloCliConfig(env, '/tmp/kilo', deps);
expect(result).toBe(true);
expect(written).toHaveLength(2); // seed + patch
const finalConfig = JSON.parse(written[1].data);
expect(finalConfig.$schema).toBe('https://app.kilo.ai/config.json');
expect(finalConfig.provider.kilo.options.baseURL).toBe('https://tunnel.example.com');
expect(finalConfig.model).toBeUndefined();
});
});