@sparkleideas/testing 3.0.0-alpha.7

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.
Files changed (42) hide show
  1. package/README.md +547 -0
  2. package/__tests__/framework.test.ts +21 -0
  3. package/package.json +61 -0
  4. package/src/fixtures/agent-fixtures.ts +793 -0
  5. package/src/fixtures/agents.ts +212 -0
  6. package/src/fixtures/configurations.ts +491 -0
  7. package/src/fixtures/index.ts +21 -0
  8. package/src/fixtures/mcp-fixtures.ts +1030 -0
  9. package/src/fixtures/memory-entries.ts +328 -0
  10. package/src/fixtures/memory-fixtures.ts +750 -0
  11. package/src/fixtures/swarm-fixtures.ts +837 -0
  12. package/src/fixtures/tasks.ts +309 -0
  13. package/src/helpers/assertion-helpers.ts +616 -0
  14. package/src/helpers/assertions.ts +286 -0
  15. package/src/helpers/create-mock.ts +200 -0
  16. package/src/helpers/index.ts +182 -0
  17. package/src/helpers/mock-factory.ts +711 -0
  18. package/src/helpers/setup-teardown.ts +678 -0
  19. package/src/helpers/swarm-instance.ts +326 -0
  20. package/src/helpers/test-application.ts +310 -0
  21. package/src/helpers/test-utils.ts +670 -0
  22. package/src/index.ts +232 -0
  23. package/src/mocks/index.ts +29 -0
  24. package/src/mocks/mock-mcp-client.ts +723 -0
  25. package/src/mocks/mock-services.ts +793 -0
  26. package/src/regression/api-contract.ts +473 -0
  27. package/src/regression/index.ts +46 -0
  28. package/src/regression/integration-regression.ts +416 -0
  29. package/src/regression/performance-baseline.ts +356 -0
  30. package/src/regression/regression-runner.ts +339 -0
  31. package/src/regression/security-regression.ts +331 -0
  32. package/src/setup.ts +127 -0
  33. package/src/v2-compat/api-compat.test.ts +590 -0
  34. package/src/v2-compat/cli-compat.test.ts +484 -0
  35. package/src/v2-compat/compatibility-validator.ts +1072 -0
  36. package/src/v2-compat/hooks-compat.test.ts +602 -0
  37. package/src/v2-compat/index.ts +58 -0
  38. package/src/v2-compat/mcp-compat.test.ts +557 -0
  39. package/src/v2-compat/report-generator.ts +441 -0
  40. package/tmp.json +0 -0
  41. package/tsconfig.json +20 -0
  42. package/vitest.config.ts +12 -0
package/README.md ADDED
@@ -0,0 +1,547 @@
1
+ # @claude-flow/testing
2
+
3
+ [![npm version](https://img.shields.io/npm/v/@claude-flow/testing.svg)](https://www.npmjs.com/package/@claude-flow/testing)
4
+ [![npm downloads](https://img.shields.io/npm/dm/@claude-flow/testing.svg)](https://www.npmjs.com/package/@claude-flow/testing)
5
+ [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
6
+ [![TypeScript](https://img.shields.io/badge/TypeScript-5.0+-blue.svg)](https://www.typescriptlang.org/)
7
+ [![TDD](https://img.shields.io/badge/TDD-London%20School-purple.svg)](https://github.com/ruvnet/claude-flow)
8
+ [![ADR-008](https://img.shields.io/badge/ADR--008-Vitest-green.svg)](https://vitest.dev/)
9
+
10
+ > Comprehensive testing framework for V3 Claude-Flow modules. Implements London School TDD patterns with behavior verification, shared fixtures, and mock services.
11
+
12
+ Based on ADR-008 (Vitest over Jest).
13
+
14
+ ## Installation
15
+
16
+ ```bash
17
+ npm install @claude-flow/testing vitest --save-dev
18
+ ```
19
+
20
+ ## Quick Start
21
+
22
+ ```typescript
23
+ import {
24
+ setupV3Tests,
25
+ createMockApplication,
26
+ agentConfigs,
27
+ swarmConfigs,
28
+ waitFor,
29
+ } from '@claude-flow/testing';
30
+
31
+ // Configure test environment
32
+ setupV3Tests();
33
+
34
+ describe('MyModule', () => {
35
+ const app = createMockApplication();
36
+
37
+ beforeEach(() => {
38
+ // Mocks are automatically reset
39
+ });
40
+
41
+ it('should spawn an agent', async () => {
42
+ const result = await app.agentLifecycle.spawn(agentConfigs.queenCoordinator);
43
+
44
+ expect(result.success).toBe(true);
45
+ expect(result.agent.type).toBe('queen-coordinator');
46
+ });
47
+ });
48
+ ```
49
+
50
+ ## Directory Structure
51
+
52
+ ```
53
+ src/
54
+ ├── fixtures/ # Pre-defined test data
55
+ │ ├── agent-fixtures.ts # Mock agents, configs
56
+ │ ├── memory-fixtures.ts # Memory entries, backends
57
+ │ ├── swarm-fixtures.ts # Swarm configs, topologies
58
+ │ └── mcp-fixtures.ts # MCP tools, contexts
59
+ ├── helpers/ # Test utilities
60
+ │ ├── test-utils.ts # waitFor, retry, timeout
61
+ │ ├── mock-factory.ts # Factory functions for mocks
62
+ │ ├── assertion-helpers.ts # Custom Vitest matchers
63
+ │ └── setup-teardown.ts # Global setup/teardown
64
+ ├── mocks/ # Mock service implementations
65
+ │ ├── mock-services.ts # AgentDB, SwarmCoordinator, etc.
66
+ │ └── mock-mcp-client.ts # MCP client for CLI testing
67
+ ├── setup.ts # Global test configuration
68
+ └── index.ts # Main exports
69
+ ```
70
+
71
+ ## Fixtures
72
+
73
+ ### Agent Fixtures
74
+
75
+ ```typescript
76
+ import {
77
+ agentConfigs,
78
+ agentInstances,
79
+ createAgentConfig,
80
+ createAgentInstance,
81
+ createV3SwarmAgentConfigs,
82
+ createMockAgent,
83
+ createMockV3Swarm,
84
+ } from '@claude-flow/testing';
85
+
86
+ // Pre-defined configs
87
+ const queen = agentConfigs.queenCoordinator;
88
+ const coder = agentConfigs.coder;
89
+
90
+ // Create with overrides
91
+ const customAgent = createAgentConfig('coder', {
92
+ name: 'Custom Coder',
93
+ priority: 90,
94
+ });
95
+
96
+ // Full V3 15-agent swarm
97
+ const swarmConfigs = createV3SwarmAgentConfigs();
98
+
99
+ // Mock agents with vitest mocks
100
+ const mockAgent = createMockAgent('security-architect');
101
+ mockAgent.execute.mockResolvedValue({ success: true });
102
+ ```
103
+
104
+ ### Memory Fixtures
105
+
106
+ ```typescript
107
+ import {
108
+ memoryEntries,
109
+ searchResults,
110
+ learnedPatterns,
111
+ hnswConfigs,
112
+ memoryBackendConfigs,
113
+ createMemoryEntry,
114
+ createVectorQuery,
115
+ generateMockEmbedding,
116
+ createMemoryBatch,
117
+ } from '@claude-flow/testing';
118
+
119
+ // Pre-defined entries
120
+ const pattern = memoryEntries.agentPattern;
121
+ const securityRule = memoryEntries.securityRule;
122
+
123
+ // Create with overrides
124
+ const entry = createMemoryEntry('agentPattern', {
125
+ key: 'custom:pattern:001',
126
+ });
127
+
128
+ // Generate embeddings
129
+ const embedding = generateMockEmbedding(384, 'my-seed');
130
+
131
+ // Create batch for performance testing
132
+ const batch = createMemoryBatch(10000, 'semantic');
133
+ ```
134
+
135
+ ### Swarm Fixtures
136
+
137
+ ```typescript
138
+ import {
139
+ swarmConfigs,
140
+ swarmStates,
141
+ swarmTasks,
142
+ swarmMessages,
143
+ coordinationResults,
144
+ createSwarmConfig,
145
+ createSwarmTask,
146
+ createSwarmMessage,
147
+ createConsensusRequest,
148
+ createMockSwarmCoordinator,
149
+ } from '@claude-flow/testing';
150
+
151
+ // Pre-defined configs
152
+ const v3Config = swarmConfigs.v3Default;
153
+ const minimalConfig = swarmConfigs.minimal;
154
+
155
+ // Create with overrides
156
+ const customConfig = createSwarmConfig('v3Default', {
157
+ maxAgents: 20,
158
+ coordination: {
159
+ consensusProtocol: 'pbft',
160
+ heartbeatInterval: 500,
161
+ electionTimeout: 3000,
162
+ },
163
+ });
164
+
165
+ // Mock coordinator
166
+ const coordinator = createMockSwarmCoordinator();
167
+ await coordinator.initialize(v3Config);
168
+ ```
169
+
170
+ ### MCP Fixtures
171
+
172
+ ```typescript
173
+ import {
174
+ mcpTools,
175
+ mcpResources,
176
+ mcpPrompts,
177
+ mcpServerConfigs,
178
+ mcpToolResults,
179
+ mcpErrors,
180
+ createMCPTool,
181
+ createMCPRequest,
182
+ createMCPResponse,
183
+ createMockMCPClient,
184
+ } from '@claude-flow/testing';
185
+
186
+ // Pre-defined tools
187
+ const swarmInit = mcpTools.swarmInit;
188
+ const agentSpawn = mcpTools.agentSpawn;
189
+
190
+ // Mock client
191
+ const client = createMockMCPClient();
192
+ await client.connect();
193
+ const result = await client.callTool('swarm_init', { topology: 'mesh' });
194
+ ```
195
+
196
+ ## Test Utilities
197
+
198
+ ### Async Utilities
199
+
200
+ ```typescript
201
+ import {
202
+ waitFor,
203
+ waitUntilChanged,
204
+ retry,
205
+ withTimeout,
206
+ sleep,
207
+ parallelLimit,
208
+ } from '@claude-flow/testing';
209
+
210
+ // Wait for condition
211
+ await waitFor(() => element.isVisible(), { timeout: 5000 });
212
+
213
+ // Wait for value to change
214
+ await waitUntilChanged(() => counter.value, { from: 0 });
215
+
216
+ // Retry with exponential backoff
217
+ const result = await retry(
218
+ async () => await fetchData(),
219
+ { maxAttempts: 3, backoff: 100 }
220
+ );
221
+
222
+ // Timeout wrapper
223
+ await withTimeout(async () => await longOp(), 5000);
224
+
225
+ // Parallel with concurrency limit
226
+ const results = await parallelLimit(
227
+ items.map(item => () => processItem(item)),
228
+ 5 // max 5 concurrent
229
+ );
230
+ ```
231
+
232
+ ### Time Control
233
+
234
+ ```typescript
235
+ import { createMockClock, measureTime } from '@claude-flow/testing';
236
+
237
+ // Mock clock for time-dependent tests
238
+ const clock = createMockClock();
239
+ clock.install();
240
+ clock.tick(1000); // Advance by 1 second
241
+ clock.uninstall();
242
+
243
+ // Measure execution time
244
+ const { result, duration } = await measureTime(async () => {
245
+ return await expensiveOperation();
246
+ });
247
+ ```
248
+
249
+ ### Event Emitter
250
+
251
+ ```typescript
252
+ import { createTestEmitter } from '@claude-flow/testing';
253
+
254
+ const emitter = createTestEmitter<{ message: string; count: number }>();
255
+
256
+ const handler = vi.fn();
257
+ emitter.on('message', handler);
258
+ emitter.emit('message', 'hello');
259
+
260
+ expect(handler).toHaveBeenCalledWith('hello');
261
+ ```
262
+
263
+ ## Mock Factory
264
+
265
+ ### Application Mocks
266
+
267
+ ```typescript
268
+ import {
269
+ createMockApplication,
270
+ createMockEventBus,
271
+ createMockTaskManager,
272
+ createMockAgentLifecycle,
273
+ createMockMemoryService,
274
+ createMockSecurityService,
275
+ createMockSwarmCoordinator,
276
+ createMockLogger,
277
+ } from '@claude-flow/testing';
278
+
279
+ // Full application with all mocks
280
+ const app = createMockApplication();
281
+
282
+ // Individual service mocks
283
+ const eventBus = createMockEventBus();
284
+ const taskManager = createMockTaskManager();
285
+ const security = createMockSecurityService();
286
+
287
+ // Use in tests
288
+ await app.taskManager.create({ name: 'Test', type: 'coding', payload: {} });
289
+ expect(app.taskManager.create).toHaveBeenCalled();
290
+
291
+ // Access tracked state
292
+ expect(app.eventBus.publishedEvents).toHaveLength(1);
293
+ expect(app.taskManager.tasks.size).toBe(1);
294
+ ```
295
+
296
+ ## Mock Services
297
+
298
+ ### MockAgentDB
299
+
300
+ ```typescript
301
+ import { MockAgentDB } from '@claude-flow/testing';
302
+
303
+ const db = new MockAgentDB();
304
+
305
+ // Insert vectors
306
+ await db.insert('vec-1', embedding, { type: 'pattern' });
307
+
308
+ // Search
309
+ const results = await db.search(queryEmbedding, 10, 0.7);
310
+
311
+ // Verify calls
312
+ expect(db.insert).toHaveBeenCalledWith('vec-1', expect.any(Array), expect.any(Object));
313
+ ```
314
+
315
+ ### MockSwarmCoordinator
316
+
317
+ ```typescript
318
+ import { MockSwarmCoordinator } from '@claude-flow/testing';
319
+
320
+ const coordinator = new MockSwarmCoordinator();
321
+
322
+ await coordinator.initialize({ topology: 'hierarchical-mesh' });
323
+ await coordinator.addAgent({ type: 'coder', name: 'Coder-1' });
324
+ const result = await coordinator.coordinate({ id: 'task-1', type: 'coding', payload: {} });
325
+
326
+ expect(coordinator.getState().agentCount).toBe(1);
327
+ expect(result.success).toBe(true);
328
+ ```
329
+
330
+ ### MockMCPClient
331
+
332
+ ```typescript
333
+ import { MockMCPClient, createStandardMockMCPClient } from '@claude-flow/testing';
334
+
335
+ // Standard client with common tools
336
+ const client = createStandardMockMCPClient();
337
+ await client.connect();
338
+
339
+ // Custom tool handlers
340
+ client.setToolHandler('swarm_init', async (params) => ({
341
+ content: [{ type: 'text', text: JSON.stringify({ swarmId: 'test' }) }],
342
+ }));
343
+
344
+ const result = await client.callTool('swarm_init', { topology: 'mesh' });
345
+
346
+ // Verify request history
347
+ expect(client.getRequestHistory()).toHaveLength(1);
348
+ expect(client.getLastRequest()?.method).toBe('tools/call');
349
+ ```
350
+
351
+ ## Assertions
352
+
353
+ ### Standard Assertions
354
+
355
+ ```typescript
356
+ import {
357
+ assertEventPublished,
358
+ assertEventOrder,
359
+ assertMocksCalledInOrder,
360
+ assertV3PerformanceTargets,
361
+ assertValidStateTransition,
362
+ assertNoSensitiveData,
363
+ } from '@claude-flow/testing';
364
+
365
+ // Event assertions
366
+ assertEventPublished(mockEventBus, 'UserCreated', { userId: '123' });
367
+ assertEventOrder(mockEventBus.publish, ['UserCreated', 'EmailSent']);
368
+
369
+ // Mock order
370
+ assertMocksCalledInOrder([mockValidate, mockSave, mockNotify]);
371
+
372
+ // Performance targets
373
+ assertV3PerformanceTargets({
374
+ searchSpeedup: 160,
375
+ flashAttentionSpeedup: 3.5,
376
+ memoryReduction: 0.55,
377
+ });
378
+
379
+ // State transitions
380
+ assertValidStateTransition('pending', 'running', {
381
+ pending: ['running', 'cancelled'],
382
+ running: ['completed', 'failed'],
383
+ });
384
+
385
+ // Security
386
+ assertNoSensitiveData(mockLogger.logs, ['password', 'token', 'secret']);
387
+ ```
388
+
389
+ ### Custom Vitest Matchers
390
+
391
+ ```typescript
392
+ import { registerCustomMatchers } from '@claude-flow/testing';
393
+
394
+ // Register in setup
395
+ registerCustomMatchers();
396
+
397
+ // Use in tests
398
+ expect(mockFn).toHaveBeenCalledWithPattern({ userId: expect.any(String) });
399
+ expect(event).toHaveEventType('UserCreated');
400
+ expect(metrics).toMeetV3PerformanceTargets();
401
+ ```
402
+
403
+ ## Setup & Teardown
404
+
405
+ ### Global Test Setup
406
+
407
+ ```typescript
408
+ import { setupV3Tests, configureTestEnvironment } from '@claude-flow/testing';
409
+
410
+ // Simple setup
411
+ setupV3Tests();
412
+
413
+ // Custom configuration
414
+ configureTestEnvironment({
415
+ resetMocks: true,
416
+ fakeTimers: true,
417
+ suppressConsole: ['log', 'warn'],
418
+ env: {
419
+ NODE_ENV: 'test',
420
+ DEBUG: 'false',
421
+ },
422
+ });
423
+ ```
424
+
425
+ ### Test Context
426
+
427
+ ```typescript
428
+ import { createSetupContext, createTestScope } from '@claude-flow/testing';
429
+
430
+ // Setup context with cleanup
431
+ const ctx = createSetupContext();
432
+ ctx.addCleanup(() => server.close());
433
+ ctx.registerResource(database);
434
+ // ... run tests
435
+ await ctx.runCleanup();
436
+
437
+ // Isolated test scope
438
+ const scope = createTestScope();
439
+ scope.addMock(mockService);
440
+ await scope.run(async () => {
441
+ // test code - mocks auto-cleared after
442
+ });
443
+ ```
444
+
445
+ ### Performance Testing
446
+
447
+ ```typescript
448
+ import { createPerformanceTestHelper } from '@claude-flow/testing';
449
+
450
+ const perf = createPerformanceTestHelper();
451
+
452
+ perf.startMeasurement('search');
453
+ await search(query);
454
+ const duration = perf.endMeasurement('search');
455
+
456
+ // Get statistics
457
+ const stats = perf.getStats('search');
458
+ console.log(`Avg: ${stats.avg}ms, P95: ${stats.p95}ms`);
459
+ ```
460
+
461
+ ## Performance Targets
462
+
463
+ The testing framework includes assertions for V3 performance targets:
464
+
465
+ | Metric | Target |
466
+ |--------|--------|
467
+ | Search Speedup | 150x - 12,500x |
468
+ | Flash Attention Speedup | 2.49x - 7.47x |
469
+ | Memory Reduction | >= 50% |
470
+ | Startup Time | < 500ms |
471
+ | Response Time | < 100ms |
472
+
473
+ ```typescript
474
+ import { assertV3PerformanceTargets, TEST_CONFIG } from '@claude-flow/testing';
475
+
476
+ // Assert targets
477
+ assertV3PerformanceTargets({
478
+ searchSpeedup: 160,
479
+ flashAttentionSpeedup: 3.5,
480
+ memoryReduction: 0.55,
481
+ startupTimeMs: 450,
482
+ responseTimeMs: 80,
483
+ });
484
+
485
+ // Access constants
486
+ console.log(TEST_CONFIG.FLASH_ATTENTION_SPEEDUP_MIN); // 2.49
487
+ console.log(TEST_CONFIG.AGENTDB_SEARCH_IMPROVEMENT_MAX); // 12500
488
+ ```
489
+
490
+ ## Best Practices
491
+
492
+ ### 1. Use London School TDD
493
+
494
+ ```typescript
495
+ // Arrange mocks before acting
496
+ const mockRepo = createMock<UserRepository>();
497
+ mockRepo.findById.mockResolvedValue(user);
498
+
499
+ // Act
500
+ await service.processUser('123');
501
+
502
+ // Assert behavior (not implementation)
503
+ expect(mockRepo.findById).toHaveBeenCalledWith('123');
504
+ expect(mockNotifier.notify).toHaveBeenCalledBefore(mockRepo.save);
505
+ ```
506
+
507
+ ### 2. Use Fixtures Over Inline Data
508
+
509
+ ```typescript
510
+ // Good - use fixtures
511
+ const agent = agentConfigs.queenCoordinator;
512
+ const task = createSwarmTask('securityScan');
513
+
514
+ // Avoid - inline data
515
+ const agent = { type: 'queen-coordinator', name: 'Test', capabilities: [] };
516
+ ```
517
+
518
+ ### 3. Isolate Tests
519
+
520
+ ```typescript
521
+ // Use fresh mocks per test
522
+ beforeEach(() => {
523
+ vi.clearAllMocks();
524
+ });
525
+
526
+ // Or use test scope
527
+ const scope = createTestScope();
528
+ await scope.run(async () => {
529
+ // isolated test
530
+ });
531
+ ```
532
+
533
+ ### 4. Test Behavior, Not Implementation
534
+
535
+ ```typescript
536
+ // Good - behavior verification
537
+ expect(mockEventBus.publish).toHaveBeenCalledWith(
538
+ expect.objectContaining({ type: 'UserCreated' })
539
+ );
540
+
541
+ // Avoid - implementation details
542
+ expect(service._internalQueue.length).toBe(1);
543
+ ```
544
+
545
+ ## License
546
+
547
+ MIT
@@ -0,0 +1,21 @@
1
+ import { describe, it, expect } from 'vitest';
2
+
3
+ describe('@sparkleideas/testing', () => {
4
+ it('should export test utilities', () => {
5
+ // Testing module provides test utilities
6
+ expect(true).toBe(true);
7
+ });
8
+
9
+ it('should support London School TDD patterns', () => {
10
+ // Mock-first testing approach
11
+ const mockService = { execute: () => 'mocked' };
12
+ expect(mockService.execute()).toBe('mocked');
13
+ });
14
+
15
+ it('should support test fixtures', () => {
16
+ // Fixture-based testing
17
+ const fixture = { id: 'test-1', data: { value: 42 } };
18
+ expect(fixture.id).toBe('test-1');
19
+ expect(fixture.data.value).toBe(42);
20
+ });
21
+ });
package/package.json ADDED
@@ -0,0 +1,61 @@
1
+ {
2
+ "name": "@sparkleideas/testing",
3
+ "version": "3.0.0-alpha.7",
4
+ "type": "module",
5
+ "description": "Testing module - TDD London School framework, test utilities, fixtures, and mock services for V3 Claude-Flow",
6
+ "main": "dist/index.js",
7
+ "types": "dist/index.d.ts",
8
+ "exports": {
9
+ ".": {
10
+ "types": "./dist/index.d.js",
11
+ "import": "./dist/index.js"
12
+ },
13
+ "./helpers": {
14
+ "types": "./dist/helpers/index.d.js",
15
+ "import": "./dist/helpers/index.js"
16
+ },
17
+ "./fixtures": {
18
+ "types": "./dist/fixtures/index.d.js",
19
+ "import": "./dist/fixtures/index.js"
20
+ },
21
+ "./mocks": {
22
+ "types": "./dist/mocks/index.d.js",
23
+ "import": "./dist/mocks/index.js"
24
+ },
25
+ "./setup": {
26
+ "types": "./dist/setup.d.js",
27
+ "import": "./dist/setup.js"
28
+ }
29
+ },
30
+ "scripts": {
31
+ "build": "tsc",
32
+ "test": "vitest run",
33
+ "typecheck": "tsc --noEmit"
34
+ },
35
+ "keywords": [
36
+ "claude-flow",
37
+ "testing",
38
+ "vitest",
39
+ "tdd",
40
+ "london-school",
41
+ "mocks",
42
+ "fixtures"
43
+ ],
44
+ "peerDependencies": {
45
+ "vitest": ">=1.0.0",
46
+ "@sparkleideas/memory": "^3.0.0-alpha.2",
47
+ "@sparkleideas/shared": "^3.0.0-alpha.1",
48
+ "@sparkleideas/swarm": "^3.0.0-alpha.1"
49
+ },
50
+ "devDependencies": {
51
+ "@claude-flow/memory": "^3.0.0-alpha.2",
52
+ "@claude-flow/shared": "^3.0.0-alpha.1",
53
+ "@claude-flow/swarm": "^3.0.0-alpha.1",
54
+ "typescript": "^5.3.0",
55
+ "vitest": "^4.0.16"
56
+ },
57
+ "publishConfig": {
58
+ "access": "public",
59
+ "tag": "v3alpha"
60
+ }
61
+ }