-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Expand file tree
/
Copy pathconfig-tool.test.ts
More file actions
179 lines (154 loc) · 5.6 KB
/
config-tool.test.ts
File metadata and controls
179 lines (154 loc) · 5.6 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
import { describe, it, expect, beforeEach, vi } from 'vitest';
import { ConfigTool } from './config-tool.js';
import type { Config } from '../config/config.js';
function makeConfig(currentModel = 'qwen-coder-plus') {
let model = currentModel;
return {
getModel: vi.fn(() => model),
setModel: vi.fn(async (newModel: string) => {
model = newModel;
}),
getAvailableModels: vi.fn(() => [
{ id: 'qwen-coder-plus', label: 'Qwen Coder Plus', authType: 'api-key' },
{ id: 'qwen3-coder', label: 'Qwen3 Coder', authType: 'api-key' },
]),
} as unknown as Config;
}
describe('ConfigTool', () => {
let config: ReturnType<typeof makeConfig>;
let tool: ConfigTool;
beforeEach(() => {
config = makeConfig();
tool = new ConfigTool(config);
});
it('has the correct name and display name', () => {
expect(tool.name).toBe('config');
expect(tool.displayName).toBe('Config');
});
describe('validation', () => {
it('rejects unknown setting', () => {
expect(() =>
tool.build({ action: 'get', setting: 'nonexistent' }),
).toThrow(/Unknown setting.*nonexistent/);
});
it('rejects SET without value', () => {
expect(() => tool.build({ action: 'set', setting: 'model' })).toThrow(
/Value is required/,
);
});
it('rejects SET with empty string value', () => {
expect(() =>
tool.build({ action: 'set', setting: 'model', value: '' }),
).toThrow(/Value is required/);
});
it('rejects SET with whitespace-only value', () => {
expect(() =>
tool.build({ action: 'set', setting: 'model', value: ' ' }),
).toThrow(/Value is required/);
});
it('rejects prototype chain keys like toString', () => {
expect(() => tool.build({ action: 'get', setting: 'toString' })).toThrow(
/Unknown setting.*toString/,
);
});
it('rejects __proto__ as setting name', () => {
expect(() => tool.build({ action: 'get', setting: '__proto__' })).toThrow(
/Unknown setting/,
);
});
it('accepts valid GET params', () => {
expect(() =>
tool.build({ action: 'get', setting: 'model' }),
).not.toThrow();
});
it('accepts valid SET params', () => {
expect(() =>
tool.build({ action: 'set', setting: 'model', value: 'qwen3-coder' }),
).not.toThrow();
});
});
describe('GET', () => {
it('returns current model value and available models', async () => {
const invocation = tool.build({ action: 'get', setting: 'model' });
const result = await invocation.execute(new AbortController().signal);
expect(result.llmContent).toContain('model = qwen-coder-plus');
expect(result.llmContent).toContain('Available models:');
expect(result.llmContent).toContain('qwen-coder-plus');
expect(result.llmContent).toContain('qwen3-coder');
});
it('permission is allow for GET', async () => {
const invocation = tool.build({ action: 'get', setting: 'model' });
const permission = await invocation.getDefaultPermission();
expect(permission).toBe('allow');
});
});
describe('SET', () => {
it('permission is ask for SET', async () => {
const invocation = tool.build({
action: 'set',
setting: 'model',
value: 'qwen3-coder',
});
const permission = await invocation.getDefaultPermission();
expect(permission).toBe('ask');
});
it('changes model on success', async () => {
const invocation = tool.build({
action: 'set',
setting: 'model',
value: 'qwen3-coder',
});
const result = await invocation.execute(new AbortController().signal);
expect(result.llmContent).toContain("changed from 'qwen-coder-plus'");
expect(result.llmContent).toContain("to 'qwen3-coder'");
// eslint-disable-next-line @typescript-eslint/no-explicit-any
expect((config as any).setModel).toHaveBeenCalledWith('qwen3-coder', {
reason: 'agent-config-tool',
context: 'ConfigTool SET',
});
});
it('returns error when setModel throws', async () => {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(config as any).setModel = vi.fn(async () => {
throw new Error('Invalid model ID');
});
tool = new ConfigTool(config);
const invocation = tool.build({
action: 'set',
setting: 'model',
value: 'nonexistent-model',
});
const result = await invocation.execute(new AbortController().signal);
expect(result.llmContent).toContain('Failed to set model');
expect(result.llmContent).toContain('Invalid model ID');
});
});
describe('confirmation details', () => {
it('shows from/to for SET', async () => {
const invocation = tool.build({
action: 'set',
setting: 'model',
value: 'qwen3-coder',
});
const details = await invocation.getConfirmationDetails(
new AbortController().signal,
);
expect(details.type).toBe('info');
if (details.type === 'info') {
expect(details.prompt).toContain('qwen-coder-plus');
expect(details.prompt).toContain('qwen3-coder');
expect(details.hideAlwaysAllow).toBe(true);
}
});
it('shows read description for GET', async () => {
const invocation = tool.build({ action: 'get', setting: 'model' });
const details = await invocation.getConfirmationDetails(
new AbortController().signal,
);
expect(details.type).toBe('info');
if (details.type === 'info') {
expect(details.prompt).toContain('Read model');
}
});
});
});