claude-flow 3.63.1 → 3.63.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,293 +0,0 @@
1
- /**
2
- * Event Store Tests
3
- *
4
- * Comprehensive tests for the event sourcing implementation.
5
- * Tests event storage, retrieval, filtering, snapshots, and projections.
6
- */
7
- import { describe, it, expect, beforeEach, afterEach } from 'vitest';
8
- import { EventStore } from './event-store.js';
9
- import { AgentStateProjection, TaskHistoryProjection, MemoryIndexProjection } from './projections.js';
10
- import { createAgentSpawnedEvent, createAgentStartedEvent, createAgentTaskAssignedEvent, createAgentTaskCompletedEvent, createTaskCreatedEvent, createTaskStartedEvent, createTaskCompletedEvent, createMemoryStoredEvent, createMemoryRetrievedEvent, createMemoryDeletedEvent, } from './domain-events.js';
11
- describe('EventStore', () => {
12
- let eventStore;
13
- beforeEach(async () => {
14
- eventStore = new EventStore({ databasePath: ':memory:', verbose: false });
15
- await eventStore.initialize();
16
- });
17
- afterEach(async () => {
18
- await eventStore.shutdown();
19
- });
20
- describe('Event Appending', () => {
21
- it('should append events to the store', async () => {
22
- const event = createAgentSpawnedEvent('agent-1', 'coder', 'core', ['coding', 'testing']);
23
- await eventStore.append(event);
24
- const events = await eventStore.getEvents('agent-1');
25
- expect(events).toHaveLength(1);
26
- expect(events[0].type).toBe('agent:spawned');
27
- expect(events[0].version).toBe(1);
28
- });
29
- it('should increment version for each event', async () => {
30
- const event1 = createAgentSpawnedEvent('agent-1', 'coder', 'core', []);
31
- const event2 = createAgentStartedEvent('agent-1');
32
- await eventStore.append(event1);
33
- await eventStore.append(event2);
34
- const events = await eventStore.getEvents('agent-1');
35
- expect(events[0].version).toBe(1);
36
- expect(events[1].version).toBe(2);
37
- });
38
- it('should maintain version per aggregate', async () => {
39
- const agent1Event = createAgentSpawnedEvent('agent-1', 'coder', 'core', []);
40
- const agent2Event = createAgentSpawnedEvent('agent-2', 'tester', 'quality', []);
41
- await eventStore.append(agent1Event);
42
- await eventStore.append(agent2Event);
43
- const agent1Events = await eventStore.getEvents('agent-1');
44
- const agent2Events = await eventStore.getEvents('agent-2');
45
- expect(agent1Events[0].version).toBe(1);
46
- expect(agent2Events[0].version).toBe(1);
47
- });
48
- });
49
- describe('Event Retrieval', () => {
50
- beforeEach(async () => {
51
- // Setup: Create multiple events
52
- await eventStore.append(createAgentSpawnedEvent('agent-1', 'coder', 'core', []));
53
- await eventStore.append(createAgentStartedEvent('agent-1'));
54
- await eventStore.append(createTaskCreatedEvent('task-1', 'implementation', 'Build feature', 'Description', 'high', []));
55
- await eventStore.append(createTaskStartedEvent('task-1', 'agent-1'));
56
- });
57
- it('should retrieve events by aggregate ID', async () => {
58
- const events = await eventStore.getEvents('agent-1');
59
- expect(events).toHaveLength(2);
60
- expect(events[0].type).toBe('agent:spawned');
61
- expect(events[1].type).toBe('agent:started');
62
- });
63
- it('should retrieve events from specific version', async () => {
64
- const events = await eventStore.getEvents('agent-1', 2);
65
- expect(events).toHaveLength(1);
66
- expect(events[0].type).toBe('agent:started');
67
- });
68
- it('should retrieve events by type', async () => {
69
- const events = await eventStore.getEventsByType('task:created');
70
- expect(events).toHaveLength(1);
71
- expect(events[0].aggregateId).toBe('task-1');
72
- });
73
- });
74
- describe('Event Filtering', () => {
75
- beforeEach(async () => {
76
- await eventStore.append(createAgentSpawnedEvent('agent-1', 'coder', 'core', []));
77
- await eventStore.append(createAgentSpawnedEvent('agent-2', 'tester', 'quality', []));
78
- await eventStore.append(createTaskCreatedEvent('task-1', 'implementation', 'Task 1', 'Desc', 'high', []));
79
- await eventStore.append(createMemoryStoredEvent('mem-1', 'default', 'key1', 'semantic', 1024));
80
- });
81
- it('should filter by aggregate types', async () => {
82
- const events = await eventStore.query({ aggregateTypes: ['agent'] });
83
- expect(events).toHaveLength(2);
84
- expect(events.every((e) => e.aggregateType === 'agent')).toBe(true);
85
- });
86
- it('should filter by event types', async () => {
87
- const events = await eventStore.query({ eventTypes: ['agent:spawned'] });
88
- expect(events).toHaveLength(2);
89
- expect(events.every((e) => e.type === 'agent:spawned')).toBe(true);
90
- });
91
- it('should filter by aggregate IDs', async () => {
92
- const events = await eventStore.query({ aggregateIds: ['agent-1', 'task-1'] });
93
- expect(events).toHaveLength(2);
94
- });
95
- it('should support pagination', async () => {
96
- const page1 = await eventStore.query({ limit: 2, offset: 0 });
97
- const page2 = await eventStore.query({ limit: 2, offset: 2 });
98
- expect(page1).toHaveLength(2);
99
- expect(page2).toHaveLength(2);
100
- expect(page1[0].id).not.toBe(page2[0].id);
101
- });
102
- });
103
- describe('Event Replay', () => {
104
- it('should replay all events', async () => {
105
- await eventStore.append(createAgentSpawnedEvent('agent-1', 'coder', 'core', []));
106
- await eventStore.append(createAgentStartedEvent('agent-1'));
107
- await eventStore.append(createTaskCreatedEvent('task-1', 'implementation', 'Task', 'Desc', 'high', []));
108
- const events = [];
109
- for await (const event of eventStore.replay()) {
110
- events.push(event);
111
- }
112
- expect(events).toHaveLength(3);
113
- });
114
- it('should replay from specific version', async () => {
115
- await eventStore.append(createAgentSpawnedEvent('agent-1', 'coder', 'core', []));
116
- await eventStore.append(createAgentStartedEvent('agent-1'));
117
- await eventStore.append(createTaskCreatedEvent('task-1', 'implementation', 'Task', 'Desc', 'high', []));
118
- const events = [];
119
- for await (const event of eventStore.replay(2)) {
120
- events.push(event);
121
- }
122
- expect(events.length).toBeGreaterThanOrEqual(2);
123
- });
124
- });
125
- describe('Snapshots', () => {
126
- it('should save snapshots', async () => {
127
- const snapshot = {
128
- aggregateId: 'agent-1',
129
- aggregateType: 'agent',
130
- version: 5,
131
- state: { status: 'active', tasks: ['task-1'] },
132
- timestamp: Date.now(),
133
- };
134
- await eventStore.saveSnapshot(snapshot);
135
- const retrieved = await eventStore.getSnapshot('agent-1');
136
- expect(retrieved).not.toBeNull();
137
- expect(retrieved?.version).toBe(5);
138
- expect(retrieved?.state).toEqual({ status: 'active', tasks: ['task-1'] });
139
- });
140
- it('should return latest snapshot', async () => {
141
- const snapshot1 = {
142
- aggregateId: 'agent-1',
143
- aggregateType: 'agent',
144
- version: 5,
145
- state: { status: 'active' },
146
- timestamp: Date.now(),
147
- };
148
- const snapshot2 = {
149
- aggregateId: 'agent-1',
150
- aggregateType: 'agent',
151
- version: 10,
152
- state: { status: 'completed' },
153
- timestamp: Date.now(),
154
- };
155
- await eventStore.saveSnapshot(snapshot1);
156
- await eventStore.saveSnapshot(snapshot2);
157
- const retrieved = await eventStore.getSnapshot('agent-1');
158
- expect(retrieved?.version).toBe(10);
159
- });
160
- });
161
- describe('Statistics', () => {
162
- it('should provide event store statistics', async () => {
163
- await eventStore.append(createAgentSpawnedEvent('agent-1', 'coder', 'core', []));
164
- await eventStore.append(createAgentSpawnedEvent('agent-2', 'tester', 'quality', []));
165
- await eventStore.append(createTaskCreatedEvent('task-1', 'implementation', 'Task', 'Desc', 'high', []));
166
- const stats = await eventStore.getStats();
167
- expect(stats.totalEvents).toBe(3);
168
- expect(stats.eventsByType['agent:spawned']).toBe(2);
169
- expect(stats.eventsByType['task:created']).toBe(1);
170
- expect(stats.eventsByAggregate['agent-1']).toBe(1);
171
- expect(stats.eventsByAggregate['agent-2']).toBe(1);
172
- });
173
- });
174
- });
175
- describe('AgentStateProjection', () => {
176
- let eventStore;
177
- let projection;
178
- beforeEach(async () => {
179
- eventStore = new EventStore({ databasePath: ':memory:' });
180
- await eventStore.initialize();
181
- projection = new AgentStateProjection(eventStore);
182
- });
183
- afterEach(async () => {
184
- await eventStore.shutdown();
185
- });
186
- it('should build agent state from events', async () => {
187
- await eventStore.append(createAgentSpawnedEvent('agent-1', 'coder', 'core', ['coding']));
188
- await eventStore.append(createAgentStartedEvent('agent-1'));
189
- await projection.initialize();
190
- const agent = projection.getAgent('agent-1');
191
- expect(agent).not.toBeNull();
192
- expect(agent?.role).toBe('coder');
193
- expect(agent?.status).toBe('active');
194
- });
195
- it('should track task assignments', async () => {
196
- await eventStore.append(createAgentSpawnedEvent('agent-1', 'coder', 'core', []));
197
- await eventStore.append(createAgentTaskAssignedEvent('agent-1', 'task-1', Date.now()));
198
- await projection.initialize();
199
- const agent = projection.getAgent('agent-1');
200
- expect(agent?.currentTask).toBe('task-1');
201
- });
202
- it('should track completed tasks', async () => {
203
- await eventStore.append(createAgentSpawnedEvent('agent-1', 'coder', 'core', []));
204
- await eventStore.append(createAgentTaskCompletedEvent('agent-1', 'task-1', { success: true }, Date.now(), 5000));
205
- await projection.initialize();
206
- const agent = projection.getAgent('agent-1');
207
- expect(agent?.completedTasks).toContain('task-1');
208
- expect(agent?.taskCount).toBe(1);
209
- });
210
- it('should filter agents by status', async () => {
211
- await eventStore.append(createAgentSpawnedEvent('agent-1', 'coder', 'core', []));
212
- await eventStore.append(createAgentSpawnedEvent('agent-2', 'tester', 'quality', []));
213
- await eventStore.append(createAgentStartedEvent('agent-1'));
214
- await projection.initialize();
215
- const activeAgents = projection.getAgentsByStatus('active');
216
- const idleAgents = projection.getAgentsByStatus('idle');
217
- expect(activeAgents).toHaveLength(1);
218
- expect(idleAgents).toHaveLength(1);
219
- });
220
- });
221
- describe('TaskHistoryProjection', () => {
222
- let eventStore;
223
- let projection;
224
- beforeEach(async () => {
225
- eventStore = new EventStore({ databasePath: ':memory:' });
226
- await eventStore.initialize();
227
- projection = new TaskHistoryProjection(eventStore);
228
- });
229
- afterEach(async () => {
230
- await eventStore.shutdown();
231
- });
232
- it('should build task history from events', async () => {
233
- await eventStore.append(createTaskCreatedEvent('task-1', 'implementation', 'Build Feature', 'Description', 'high', []));
234
- await eventStore.append(createTaskStartedEvent('task-1', 'agent-1'));
235
- await eventStore.append(createTaskCompletedEvent('task-1', { success: true }, 5000));
236
- await projection.initialize();
237
- const task = projection.getTask('task-1');
238
- expect(task).not.toBeNull();
239
- expect(task?.title).toBe('Build Feature');
240
- expect(task?.status).toBe('completed');
241
- expect(task?.assignedAgent).toBe('agent-1');
242
- });
243
- it('should calculate average task duration', async () => {
244
- await eventStore.append(createTaskCreatedEvent('task-1', 'implementation', 'Task 1', 'Desc', 'high', []));
245
- await eventStore.append(createTaskCompletedEvent('task-1', { success: true }, 5000));
246
- await eventStore.append(createTaskCreatedEvent('task-2', 'implementation', 'Task 2', 'Desc', 'high', []));
247
- await eventStore.append(createTaskCompletedEvent('task-2', { success: true }, 3000));
248
- await projection.initialize();
249
- const avgDuration = projection.getAverageTaskDuration();
250
- expect(avgDuration).toBe(4000);
251
- });
252
- });
253
- describe('MemoryIndexProjection', () => {
254
- let eventStore;
255
- let projection;
256
- beforeEach(async () => {
257
- eventStore = new EventStore({ databasePath: ':memory:' });
258
- await eventStore.initialize();
259
- projection = new MemoryIndexProjection(eventStore);
260
- });
261
- afterEach(async () => {
262
- await eventStore.shutdown();
263
- });
264
- it('should build memory index from events', async () => {
265
- await eventStore.append(createMemoryStoredEvent('mem-1', 'default', 'key1', 'semantic', 1024));
266
- await eventStore.append(createMemoryRetrievedEvent('mem-1', 'default', 'key1', 1));
267
- await projection.initialize();
268
- const memory = projection.getMemory('mem-1');
269
- expect(memory).not.toBeNull();
270
- expect(memory?.namespace).toBe('default');
271
- expect(memory?.accessCount).toBe(1);
272
- });
273
- it('should track memory deletions', async () => {
274
- await eventStore.append(createMemoryStoredEvent('mem-1', 'default', 'key1', 'semantic', 1024));
275
- await eventStore.append(createMemoryDeletedEvent('mem-1', 'default', 'key1'));
276
- await projection.initialize();
277
- const memory = projection.getMemory('mem-1');
278
- expect(memory?.isDeleted).toBe(true);
279
- const activeMemories = projection.getActiveMemories();
280
- expect(activeMemories).toHaveLength(0);
281
- });
282
- it('should calculate total size by namespace', async () => {
283
- await eventStore.append(createMemoryStoredEvent('mem-1', 'ns1', 'key1', 'semantic', 1024));
284
- await eventStore.append(createMemoryStoredEvent('mem-2', 'ns1', 'key2', 'semantic', 2048));
285
- await eventStore.append(createMemoryStoredEvent('mem-3', 'ns2', 'key3', 'semantic', 512));
286
- await projection.initialize();
287
- const ns1Size = projection.getTotalSizeByNamespace('ns1');
288
- const ns2Size = projection.getTotalSizeByNamespace('ns2');
289
- expect(ns1Size).toBe(3072);
290
- expect(ns2Size).toBe(512);
291
- });
292
- });
293
- //# sourceMappingURL=event-store.test.js.map
@@ -1,9 +0,0 @@
1
- /**
2
- * V3 Hooks System - Tests
3
- *
4
- * Comprehensive tests for hook registry and executor.
5
- *
6
- * @module v3/shared/hooks/hooks.test
7
- */
8
- export {};
9
- //# sourceMappingURL=hooks.test.d.ts.map
@@ -1,322 +0,0 @@
1
- /**
2
- * V3 Hooks System - Tests
3
- *
4
- * Comprehensive tests for hook registry and executor.
5
- *
6
- * @module v3/shared/hooks/hooks.test
7
- */
8
- import { describe, it, expect, beforeEach, vi } from 'vitest';
9
- import { createHookRegistry } from './registry.js';
10
- import { createHookExecutor } from './executor.js';
11
- import { HookEvent, HookPriority } from './types.js';
12
- import { createEventBus } from '../core/event-bus.js';
13
- describe('HookRegistry', () => {
14
- let registry;
15
- beforeEach(() => {
16
- registry = createHookRegistry();
17
- });
18
- it('should register a hook', () => {
19
- const handler = vi.fn();
20
- const id = registry.register(HookEvent.PreToolUse, handler);
21
- expect(id).toBeDefined();
22
- expect(registry.has(id)).toBe(true);
23
- expect(registry.count()).toBe(1);
24
- });
25
- it('should unregister a hook', () => {
26
- const handler = vi.fn();
27
- const id = registry.register(HookEvent.PreToolUse, handler);
28
- const result = registry.unregister(id);
29
- expect(result).toBe(true);
30
- expect(registry.has(id)).toBe(false);
31
- expect(registry.count()).toBe(0);
32
- });
33
- it('should return false when unregistering non-existent hook', () => {
34
- const result = registry.unregister('non-existent');
35
- expect(result).toBe(false);
36
- });
37
- it('should get handlers sorted by priority', () => {
38
- const handler1 = vi.fn();
39
- const handler2 = vi.fn();
40
- const handler3 = vi.fn();
41
- registry.register(HookEvent.PreToolUse, handler1, HookPriority.Normal);
42
- registry.register(HookEvent.PreToolUse, handler2, HookPriority.High);
43
- registry.register(HookEvent.PreToolUse, handler3, HookPriority.Low);
44
- const handlers = registry.getHandlers(HookEvent.PreToolUse);
45
- expect(handlers).toHaveLength(3);
46
- expect(handlers[0].handler).toBe(handler2); // High priority first
47
- expect(handlers[1].handler).toBe(handler1); // Normal priority second
48
- expect(handlers[2].handler).toBe(handler3); // Low priority last
49
- });
50
- it('should filter disabled hooks', () => {
51
- const handler = vi.fn();
52
- const id = registry.register(HookEvent.PreToolUse, handler, HookPriority.Normal, {
53
- enabled: false,
54
- });
55
- const handlers = registry.getHandlers(HookEvent.PreToolUse);
56
- const allHandlers = registry.getHandlers(HookEvent.PreToolUse, true);
57
- expect(handlers).toHaveLength(0);
58
- expect(allHandlers).toHaveLength(1);
59
- });
60
- it('should enable and disable hooks', () => {
61
- const handler = vi.fn();
62
- const id = registry.register(HookEvent.PreToolUse, handler);
63
- registry.disable(id);
64
- expect(registry.getHandlers(HookEvent.PreToolUse)).toHaveLength(0);
65
- registry.enable(id);
66
- expect(registry.getHandlers(HookEvent.PreToolUse)).toHaveLength(1);
67
- });
68
- it('should list hooks with filters', () => {
69
- registry.register(HookEvent.PreToolUse, vi.fn(), HookPriority.High);
70
- registry.register(HookEvent.PostToolUse, vi.fn(), HookPriority.Normal);
71
- registry.register(HookEvent.PreEdit, vi.fn(), HookPriority.Low);
72
- const allHooks = registry.listHooks();
73
- expect(allHooks).toHaveLength(3);
74
- const preToolHooks = registry.listHooks({ event: HookEvent.PreToolUse });
75
- expect(preToolHooks).toHaveLength(1);
76
- const highPriorityHooks = registry.listHooks({ minPriority: HookPriority.Normal });
77
- expect(highPriorityHooks).toHaveLength(2);
78
- });
79
- it('should get event types', () => {
80
- registry.register(HookEvent.PreToolUse, vi.fn());
81
- registry.register(HookEvent.PostToolUse, vi.fn());
82
- registry.register(HookEvent.PreEdit, vi.fn());
83
- const eventTypes = registry.getEventTypes();
84
- expect(eventTypes).toContain(HookEvent.PreToolUse);
85
- expect(eventTypes).toContain(HookEvent.PostToolUse);
86
- expect(eventTypes).toContain(HookEvent.PreEdit);
87
- });
88
- it('should track statistics', () => {
89
- registry.register(HookEvent.PreToolUse, vi.fn());
90
- registry.register(HookEvent.PostToolUse, vi.fn());
91
- registry.recordExecution(true, 10);
92
- registry.recordExecution(true, 20);
93
- registry.recordExecution(false, 5);
94
- const stats = registry.getStats();
95
- expect(stats.totalHooks).toBe(2);
96
- expect(stats.totalExecutions).toBe(3);
97
- expect(stats.totalFailures).toBe(1);
98
- expect(stats.avgExecutionTime).toBe((10 + 20 + 5) / 3);
99
- });
100
- it('should reset statistics', () => {
101
- registry.recordExecution(true, 10);
102
- registry.resetStats();
103
- const stats = registry.getStats();
104
- expect(stats.totalExecutions).toBe(0);
105
- expect(stats.totalFailures).toBe(0);
106
- });
107
- it('should clear all hooks', () => {
108
- registry.register(HookEvent.PreToolUse, vi.fn());
109
- registry.register(HookEvent.PostToolUse, vi.fn());
110
- registry.clear();
111
- expect(registry.count()).toBe(0);
112
- expect(registry.getEventTypes()).toHaveLength(0);
113
- });
114
- });
115
- describe('HookExecutor', () => {
116
- let registry;
117
- let executor;
118
- let eventBus;
119
- beforeEach(() => {
120
- registry = createHookRegistry();
121
- eventBus = createEventBus();
122
- executor = createHookExecutor(registry, eventBus);
123
- });
124
- it('should execute single hook successfully', async () => {
125
- const handler = vi.fn(async () => ({ success: true }));
126
- registry.register(HookEvent.PreToolUse, handler);
127
- const context = {
128
- event: HookEvent.PreToolUse,
129
- timestamp: new Date(),
130
- tool: { name: 'Read', parameters: { path: 'file.ts' } },
131
- };
132
- const result = await executor.execute(HookEvent.PreToolUse, context);
133
- expect(result.success).toBe(true);
134
- expect(result.hooksExecuted).toBe(1);
135
- expect(result.hooksFailed).toBe(0);
136
- expect(handler).toHaveBeenCalledWith(context);
137
- });
138
- it('should execute multiple hooks in priority order', async () => {
139
- const executionOrder = [];
140
- const handler1 = vi.fn(async () => {
141
- executionOrder.push(1);
142
- return { success: true };
143
- });
144
- const handler2 = vi.fn(async () => {
145
- executionOrder.push(2);
146
- return { success: true };
147
- });
148
- const handler3 = vi.fn(async () => {
149
- executionOrder.push(3);
150
- return { success: true };
151
- });
152
- registry.register(HookEvent.PreToolUse, handler1, HookPriority.Normal);
153
- registry.register(HookEvent.PreToolUse, handler2, HookPriority.High);
154
- registry.register(HookEvent.PreToolUse, handler3, HookPriority.Low);
155
- const context = {
156
- event: HookEvent.PreToolUse,
157
- timestamp: new Date(),
158
- };
159
- await executor.execute(HookEvent.PreToolUse, context);
160
- expect(executionOrder).toEqual([2, 1, 3]); // High, Normal, Low
161
- });
162
- it('should handle hook errors gracefully', async () => {
163
- const handler1 = vi.fn(async () => ({ success: true }));
164
- const handler2 = vi.fn(async () => {
165
- throw new Error('Hook failed');
166
- });
167
- const handler3 = vi.fn(async () => ({ success: true }));
168
- registry.register(HookEvent.PreToolUse, handler1, HookPriority.High);
169
- registry.register(HookEvent.PreToolUse, handler2, HookPriority.Normal);
170
- registry.register(HookEvent.PreToolUse, handler3, HookPriority.Low);
171
- const context = {
172
- event: HookEvent.PreToolUse,
173
- timestamp: new Date(),
174
- };
175
- const result = await executor.execute(HookEvent.PreToolUse, context, {
176
- continueOnError: true,
177
- });
178
- expect(result.hooksExecuted).toBe(3);
179
- expect(result.hooksFailed).toBe(1);
180
- expect(handler3).toHaveBeenCalled(); // Should continue despite error
181
- });
182
- it('should abort on error when continueOnError is false', async () => {
183
- const handler1 = vi.fn(async () => ({ success: true }));
184
- const handler2 = vi.fn(async () => {
185
- throw new Error('Hook failed');
186
- });
187
- const handler3 = vi.fn(async () => ({ success: true }));
188
- registry.register(HookEvent.PreToolUse, handler1, HookPriority.High);
189
- registry.register(HookEvent.PreToolUse, handler2, HookPriority.Normal);
190
- registry.register(HookEvent.PreToolUse, handler3, HookPriority.Low);
191
- const context = {
192
- event: HookEvent.PreToolUse,
193
- timestamp: new Date(),
194
- };
195
- const result = await executor.execute(HookEvent.PreToolUse, context);
196
- expect(result.aborted).toBe(true);
197
- expect(result.hooksExecuted).toBe(2);
198
- expect(handler3).not.toHaveBeenCalled(); // Should not execute after error
199
- });
200
- it('should abort when hook returns abort flag', async () => {
201
- const handler1 = vi.fn(async () => ({ success: true }));
202
- const handler2 = vi.fn(async () => ({ success: true, abort: true }));
203
- const handler3 = vi.fn(async () => ({ success: true }));
204
- registry.register(HookEvent.PreToolUse, handler1, HookPriority.High);
205
- registry.register(HookEvent.PreToolUse, handler2, HookPriority.Normal);
206
- registry.register(HookEvent.PreToolUse, handler3, HookPriority.Low);
207
- const context = {
208
- event: HookEvent.PreToolUse,
209
- timestamp: new Date(),
210
- };
211
- const result = await executor.execute(HookEvent.PreToolUse, context);
212
- expect(result.aborted).toBe(true);
213
- expect(result.hooksExecuted).toBe(2);
214
- expect(handler3).not.toHaveBeenCalled();
215
- });
216
- it('should merge context modifications', async () => {
217
- const handler1 = vi.fn(async () => ({
218
- success: true,
219
- data: { metadata: { modified: true } },
220
- }));
221
- const handler2 = vi.fn(async (context) => {
222
- expect(context.metadata?.modified).toBe(true);
223
- return { success: true };
224
- });
225
- registry.register(HookEvent.PreToolUse, handler1, HookPriority.High);
226
- registry.register(HookEvent.PreToolUse, handler2, HookPriority.Normal);
227
- const context = {
228
- event: HookEvent.PreToolUse,
229
- timestamp: new Date(),
230
- };
231
- const result = await executor.execute(HookEvent.PreToolUse, context);
232
- expect(result.finalContext?.metadata).toEqual({ modified: true });
233
- });
234
- it('should handle timeout', async () => {
235
- const handler = vi.fn(async () => {
236
- await new Promise(resolve => setTimeout(resolve, 200));
237
- return { success: true };
238
- });
239
- registry.register(HookEvent.PreToolUse, handler);
240
- const context = {
241
- event: HookEvent.PreToolUse,
242
- timestamp: new Date(),
243
- };
244
- const result = await executor.executeWithTimeout(HookEvent.PreToolUse, context, 100);
245
- expect(result.success).toBe(false);
246
- expect(result.hooksFailed).toBe(1);
247
- });
248
- it('should execute hooks in parallel', async () => {
249
- const handler1 = vi.fn(async () => ({ success: true }));
250
- const handler2 = vi.fn(async () => ({ success: true }));
251
- registry.register(HookEvent.PreToolUse, handler1);
252
- registry.register(HookEvent.PostToolUse, handler2);
253
- const contexts = [
254
- { event: HookEvent.PreToolUse, timestamp: new Date() },
255
- { event: HookEvent.PostToolUse, timestamp: new Date() },
256
- ];
257
- const results = await executor.executeParallel([HookEvent.PreToolUse, HookEvent.PostToolUse], contexts);
258
- expect(results).toHaveLength(2);
259
- expect(results[0].success).toBe(true);
260
- expect(results[1].success).toBe(true);
261
- });
262
- it('should execute hooks sequentially with context chaining', async () => {
263
- const handler1 = vi.fn(async () => ({
264
- success: true,
265
- data: { metadata: { step: 1 } },
266
- }));
267
- const handler2 = vi.fn(async () => ({
268
- success: true,
269
- data: { metadata: { step: 2 } },
270
- }));
271
- registry.register(HookEvent.PreToolUse, handler1);
272
- registry.register(HookEvent.PostToolUse, handler2);
273
- const initialContext = {
274
- event: HookEvent.PreToolUse,
275
- timestamp: new Date(),
276
- };
277
- const result = await executor.executeSequential([HookEvent.PreToolUse, HookEvent.PostToolUse], initialContext);
278
- expect(result.success).toBe(true);
279
- expect(result.hooksExecuted).toBe(2);
280
- });
281
- it('should emit events to event bus', async () => {
282
- const preExecuteHandler = vi.fn();
283
- const postExecuteHandler = vi.fn();
284
- eventBus.on('hooks:pre-execute', preExecuteHandler);
285
- eventBus.on('hooks:post-execute', postExecuteHandler);
286
- const handler = vi.fn(async () => ({ success: true }));
287
- registry.register(HookEvent.PreToolUse, handler);
288
- const context = {
289
- event: HookEvent.PreToolUse,
290
- timestamp: new Date(),
291
- };
292
- await executor.execute(HookEvent.PreToolUse, context);
293
- expect(preExecuteHandler).toHaveBeenCalled();
294
- expect(postExecuteHandler).toHaveBeenCalled();
295
- });
296
- it('should skip disabled hooks', async () => {
297
- const handler = vi.fn(async () => ({ success: true }));
298
- const id = registry.register(HookEvent.PreToolUse, handler, HookPriority.Normal, {
299
- enabled: false,
300
- });
301
- const context = {
302
- event: HookEvent.PreToolUse,
303
- timestamp: new Date(),
304
- };
305
- const result = await executor.execute(HookEvent.PreToolUse, context);
306
- expect(result.hooksExecuted).toBe(0);
307
- expect(handler).not.toHaveBeenCalled();
308
- });
309
- it('should record execution statistics', async () => {
310
- const handler = vi.fn(async () => ({ success: true }));
311
- registry.register(HookEvent.PreToolUse, handler);
312
- const context = {
313
- event: HookEvent.PreToolUse,
314
- timestamp: new Date(),
315
- };
316
- await executor.execute(HookEvent.PreToolUse, context);
317
- const stats = registry.getStats();
318
- expect(stats.totalExecutions).toBe(1);
319
- expect(stats.totalFailures).toBe(0);
320
- });
321
- });
322
- //# sourceMappingURL=hooks.test.js.map
@@ -1,9 +0,0 @@
1
- /**
2
- * V3 Hooks System - Export Verification
3
- *
4
- * Verifies that all exports are accessible through the public API.
5
- *
6
- * @module v3/shared/hooks/verify-exports.test
7
- */
8
- export {};
9
- //# sourceMappingURL=verify-exports.test.d.ts.map