-
Notifications
You must be signed in to change notification settings - Fork 3k
Expand file tree
/
Copy pathconstants.test.ts
More file actions
386 lines (328 loc) · 14.6 KB
/
Copy pathconstants.test.ts
File metadata and controls
386 lines (328 loc) · 14.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
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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
/**
* @license
* Copyright 2026 Qwen Team
* SPDX-License-Identifier: Apache-2.0
*/
import { describe, it, expect, vi, beforeEach } from 'vitest';
import {
HookEventName,
HooksConfigSource,
hookEventSupportsMatcher,
} from '@qwen-code/qwen-code-core';
vi.mock('../../../i18n/index.js', () => ({
t: vi.fn((key: string) => key),
}));
import {
getHookExitCodes,
getHookShortDescription,
getHookDescription,
getTranslatedSourceDisplayMap,
createEmptyHookEventInfo,
DISPLAY_HOOK_EVENTS,
supportsMatchers,
} from './constants.js';
describe('hooks constants', () => {
beforeEach(() => {
vi.clearAllMocks();
});
describe('getHookExitCodes', () => {
it('should return exit codes for Stop event', () => {
const exitCodes = getHookExitCodes(HookEventName.Stop);
expect(exitCodes).toHaveLength(3);
expect(exitCodes[0]).toEqual({
code: 0,
description: expect.any(String),
});
expect(exitCodes[1]).toEqual({
code: 2,
description: expect.any(String),
});
expect(exitCodes[2]).toEqual({
code: 'Other',
description: expect.any(String),
});
});
it('should return exit codes for PreToolUse event', () => {
const exitCodes = getHookExitCodes(HookEventName.PreToolUse);
expect(exitCodes).toHaveLength(3);
expect(exitCodes[0].code).toBe(0);
expect(exitCodes[1].code).toBe(2);
expect(exitCodes[2].code).toBe('Other');
});
it('should return exit codes for PostToolUse event', () => {
const exitCodes = getHookExitCodes(HookEventName.PostToolUse);
expect(exitCodes).toHaveLength(3);
});
it('should return exit codes for UserPromptSubmit event', () => {
const exitCodes = getHookExitCodes(HookEventName.UserPromptSubmit);
expect(exitCodes).toHaveLength(3);
});
it('should return exit codes for Notification event', () => {
const exitCodes = getHookExitCodes(HookEventName.Notification);
expect(exitCodes).toHaveLength(2);
});
it('should return exit codes for PermissionDenied event', () => {
const exitCodes = getHookExitCodes(HookEventName.PermissionDenied);
expect(exitCodes).toHaveLength(2);
expect(exitCodes[0].code).toBe(0);
expect(exitCodes[1].code).toBe('Other');
});
it('should return exit codes for SessionStart event', () => {
const exitCodes = getHookExitCodes(HookEventName.SessionStart);
expect(exitCodes).toHaveLength(2);
});
it('should return exit codes for SessionEnd event', () => {
const exitCodes = getHookExitCodes(HookEventName.SessionEnd);
expect(exitCodes).toHaveLength(2);
});
it('should return exit codes for PreCompact event', () => {
const exitCodes = getHookExitCodes(HookEventName.PreCompact);
expect(exitCodes).toHaveLength(3);
});
it('should return exit codes for InstructionsLoaded event', () => {
const exitCodes = getHookExitCodes(HookEventName.InstructionsLoaded);
expect(exitCodes).toHaveLength(2);
});
it('should return exit codes for PostCompact event', () => {
const exitCodes = getHookExitCodes(HookEventName.PostCompact);
expect(exitCodes).toHaveLength(2);
expect(exitCodes[0].code).toBe(0);
expect(exitCodes[1].code).toBe('Other');
});
it('should return exit codes for StopFailure event', () => {
// Fire-and-forget per hookAggregator — both rows are documented as ignored.
const exitCodes = getHookExitCodes(HookEventName.StopFailure);
expect(exitCodes).toHaveLength(2);
expect(exitCodes[0].code).toBe(0);
expect(exitCodes[1].code).toBe('Other');
for (const row of exitCodes) {
expect(row.description).toContain('fire-and-forget');
}
});
it('should return empty array for unknown event', () => {
const exitCodes = getHookExitCodes('unknown_event' as HookEventName);
expect(exitCodes).toEqual([]);
});
});
describe('getHookShortDescription', () => {
it('should return description for PreToolUse', () => {
const desc = getHookShortDescription(HookEventName.PreToolUse);
expect(desc).toBe('Before tool execution');
});
it('should return description for PostToolUse', () => {
const desc = getHookShortDescription(HookEventName.PostToolUse);
expect(desc).toBe('After tool execution');
});
it('should return description for UserPromptSubmit', () => {
const desc = getHookShortDescription(HookEventName.UserPromptSubmit);
expect(desc).toBe('When the user submits a prompt');
});
it('should return description for SessionStart', () => {
const desc = getHookShortDescription(HookEventName.SessionStart);
expect(desc).toBe('When a new session is started');
});
it('should return description for InstructionsLoaded', () => {
const desc = getHookShortDescription(HookEventName.InstructionsLoaded);
expect(desc).toBe('When instruction files are loaded');
});
it('should return description for PostCompact', () => {
const desc = getHookShortDescription(HookEventName.PostCompact);
expect(desc).toBe('After conversation compaction');
});
it('should return description for StopFailure', () => {
const desc = getHookShortDescription(HookEventName.StopFailure);
expect(desc).toContain('API error');
expect(desc).toContain('Stop');
});
it('should return description for PermissionDenied', () => {
const desc = getHookShortDescription(HookEventName.PermissionDenied);
expect(desc).toBe(
'When a tool call is denied before a permission dialog is displayed',
);
});
it('should return empty string for unknown event', () => {
const desc = getHookShortDescription('unknown_event' as HookEventName);
expect(desc).toBe('');
});
});
describe('getHookDescription', () => {
it('should return description for PreToolUse', () => {
const desc = getHookDescription(HookEventName.PreToolUse);
expect(desc).toBe('Input to command is JSON of tool call arguments.');
});
it('should return description for PostToolUse', () => {
const desc = getHookDescription(HookEventName.PostToolUse);
expect(desc).toContain('inputs');
expect(desc).toContain('response');
});
it('should return description for PermissionDenied', () => {
const desc = getHookDescription(HookEventName.PermissionDenied);
expect(desc).toContain('tool_name');
expect(desc).toContain('reason');
});
it('should return empty string for Stop event', () => {
const desc = getHookDescription(HookEventName.Stop);
expect(desc).toBe('');
});
it('should return description for InstructionsLoaded', () => {
const desc = getHookDescription(HookEventName.InstructionsLoaded);
expect(desc).toContain('file_path');
expect(desc).toContain('memory_type');
expect(desc).toContain('load_reason');
});
it('should return description for PostCompact', () => {
const desc = getHookDescription(HookEventName.PostCompact);
expect(desc).toContain('trigger');
expect(desc).toContain('compact_summary');
});
it('should return description for StopFailure', () => {
const desc = getHookDescription(HookEventName.StopFailure);
expect(desc).toContain('error');
expect(desc).toContain('rate_limit');
expect(desc).toContain('Fire-and-forget');
});
it('should return empty string for unknown event', () => {
const desc = getHookDescription('unknown_event' as HookEventName);
expect(desc).toBe('');
});
});
describe('getTranslatedSourceDisplayMap', () => {
it('should return mapping for all sources', () => {
const map = getTranslatedSourceDisplayMap();
expect(map[HooksConfigSource.Project]).toBe('Local Settings');
expect(map[HooksConfigSource.User]).toBe('User Settings');
expect(map[HooksConfigSource.System]).toBe('System Settings');
expect(map[HooksConfigSource.Extensions]).toBe('Extensions');
});
it('should return translated strings', () => {
const map = getTranslatedSourceDisplayMap();
Object.values(map).forEach((value) => {
expect(typeof value).toBe('string');
expect(value.length).toBeGreaterThan(0);
});
});
});
describe('DISPLAY_HOOK_EVENTS', () => {
it('should contain all expected hook events', () => {
expect(DISPLAY_HOOK_EVENTS).toContain(HookEventName.Stop);
expect(DISPLAY_HOOK_EVENTS).toContain(HookEventName.StopFailure);
expect(DISPLAY_HOOK_EVENTS).toContain(HookEventName.PreToolUse);
expect(DISPLAY_HOOK_EVENTS).toContain(HookEventName.PostToolUse);
expect(DISPLAY_HOOK_EVENTS).toContain(HookEventName.PostToolUseFailure);
expect(DISPLAY_HOOK_EVENTS).toContain(HookEventName.PostToolBatch);
expect(DISPLAY_HOOK_EVENTS).toContain(HookEventName.Notification);
expect(DISPLAY_HOOK_EVENTS).toContain(HookEventName.UserPromptSubmit);
expect(DISPLAY_HOOK_EVENTS).toContain(HookEventName.SessionStart);
expect(DISPLAY_HOOK_EVENTS).toContain(HookEventName.SessionEnd);
expect(DISPLAY_HOOK_EVENTS).toContain(HookEventName.SubagentStart);
expect(DISPLAY_HOOK_EVENTS).toContain(HookEventName.SubagentStop);
expect(DISPLAY_HOOK_EVENTS).toContain(HookEventName.PreCompact);
expect(DISPLAY_HOOK_EVENTS).toContain(HookEventName.PostCompact);
expect(DISPLAY_HOOK_EVENTS).toContain(HookEventName.PermissionRequest);
expect(DISPLAY_HOOK_EVENTS).toContain(HookEventName.PermissionDenied);
expect(DISPLAY_HOOK_EVENTS).toContain(HookEventName.TodoCreated);
expect(DISPLAY_HOOK_EVENTS).toContain(HookEventName.TodoCompleted);
expect(DISPLAY_HOOK_EVENTS).toContain(HookEventName.InstructionsLoaded);
});
it('should have 19 events', () => {
expect(DISPLAY_HOOK_EVENTS).toHaveLength(19);
});
});
describe('supportsMatchers', () => {
it('returns true for events with meaningful matchers', () => {
expect(supportsMatchers(HookEventName.PreToolUse)).toBe(true);
expect(supportsMatchers(HookEventName.PostToolUse)).toBe(true);
expect(supportsMatchers(HookEventName.PostToolUseFailure)).toBe(true);
expect(supportsMatchers(HookEventName.PermissionRequest)).toBe(true);
expect(supportsMatchers(HookEventName.Notification)).toBe(true);
expect(supportsMatchers(HookEventName.SessionStart)).toBe(true);
expect(supportsMatchers(HookEventName.SessionEnd)).toBe(true);
expect(supportsMatchers(HookEventName.SubagentStart)).toBe(true);
expect(supportsMatchers(HookEventName.SubagentStop)).toBe(true);
expect(supportsMatchers(HookEventName.PreCompact)).toBe(true);
expect(supportsMatchers(HookEventName.PostCompact)).toBe(true);
expect(supportsMatchers(HookEventName.StopFailure)).toBe(true);
});
it('returns false for events without matchers', () => {
expect(supportsMatchers(HookEventName.Stop)).toBe(false);
expect(supportsMatchers(HookEventName.PostToolBatch)).toBe(false);
expect(supportsMatchers(HookEventName.UserPromptSubmit)).toBe(false);
expect(supportsMatchers(HookEventName.TodoCreated)).toBe(false);
expect(supportsMatchers(HookEventName.TodoCompleted)).toBe(false);
});
it('returns false for unknown events', () => {
expect(supportsMatchers('unknown_event' as HookEventName)).toBe(false);
});
it('covers every HookEventName value and matches core dispatch', () => {
for (const event of Object.values(HookEventName)) {
expect(supportsMatchers(event)).toBe(hookEventSupportsMatcher(event));
}
});
});
describe('createEmptyHookEventInfo', () => {
it('should create empty info for PreToolUse', () => {
const info = createEmptyHookEventInfo(HookEventName.PreToolUse);
expect(info.event).toBe(HookEventName.PreToolUse);
expect(info.shortDescription).toBe('Before tool execution');
expect(info.description).toBe(
'Input to command is JSON of tool call arguments.',
);
expect(info.exitCodes).toHaveLength(3);
expect(info.matcherGroups).toEqual([]);
});
it('should create empty info for Stop', () => {
const info = createEmptyHookEventInfo(HookEventName.Stop);
expect(info.event).toBe(HookEventName.Stop);
expect(info.shortDescription).toBe(
'Right before Qwen Code concludes its response',
);
expect(info.description).toBe('');
expect(info.exitCodes).toHaveLength(3);
expect(info.matcherGroups).toEqual([]);
});
it('should create empty info for unknown event', () => {
const info = createEmptyHookEventInfo('unknown_event' as HookEventName);
expect(info.event).toBe('unknown_event');
expect(info.shortDescription).toBe('');
expect(info.description).toBe('');
expect(info.exitCodes).toEqual([]);
expect(info.matcherGroups).toEqual([]);
});
it('should create empty info for PermissionDenied', () => {
const info = createEmptyHookEventInfo(HookEventName.PermissionDenied);
expect(info.event).toBe(HookEventName.PermissionDenied);
expect(info.shortDescription).toBe(
'When a tool call is denied before a permission dialog is displayed',
);
expect(info.description).toContain('tool_use_id');
expect(info.exitCodes).toHaveLength(2);
expect(info.matcherGroups).toEqual([]);
});
it('should create empty info for TodoCreated', () => {
const info = createEmptyHookEventInfo(HookEventName.TodoCreated);
expect(info.event).toBe(HookEventName.TodoCreated);
expect(info.shortDescription).toBe('When a new todo item is created');
expect(info.description).toContain('todo_id');
expect(info.exitCodes).toHaveLength(3);
expect(info.matcherGroups).toEqual([]);
});
it('should create empty info for TodoCompleted', () => {
const info = createEmptyHookEventInfo(HookEventName.TodoCompleted);
expect(info.event).toBe(HookEventName.TodoCompleted);
expect(info.shortDescription).toBe(
'When a todo item is marked as completed',
);
expect(info.description).toContain('previous_status');
expect(info.exitCodes).toHaveLength(3);
expect(info.matcherGroups).toEqual([]);
});
it('should create empty info for InstructionsLoaded', () => {
const info = createEmptyHookEventInfo(HookEventName.InstructionsLoaded);
expect(info.event).toBe(HookEventName.InstructionsLoaded);
expect(info.shortDescription).toBe('When instruction files are loaded');
expect(info.description).toContain('file_path');
expect(info.exitCodes).toHaveLength(2);
expect(info.matcherGroups).toEqual([]);
});
});
});