trusty-mpm 1.0.2

trusty-mpm: unified multi-agent orchestration platform (core, daemon, CLI, TUI, Telegram)
Documentation
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
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
# How TDD Prevents Testing Anti-Patterns

Test-Driven Development (TDD) is not just a testing methodology - it's a design methodology that naturally prevents all five testing anti-patterns. This guide explains the deep connection between TDD and anti-pattern prevention.

## Overview

**Core insight:** If you follow strict TDD, testing anti-patterns become nearly impossible to create.

**Why:** TDD forces you to think about real behavior before implementation, making mock-testing and test-only methods unnatural.

## TDD Fundamentals

### The RED/GREEN/REFACTOR Cycle

```
1. RED: Write failing test
   - Test doesn't compile or fails
   - Verify failure reason is correct
   - No implementation exists yet

2. GREEN: Write minimal code to pass
   - Simplest implementation
   - Just enough to pass test
   - No gold plating

3. REFACTOR: Improve code
   - Clean up duplication
   - Improve names
   - Tests still pass

4. REPEAT: Next test
```

**Critical rule:** Never write implementation without failing test first.

See the Test-Driven Development skill for complete workflow (available in the skill library for comprehensive TDD guidance).

## How TDD Prevents Each Anti-Pattern

### Anti-Pattern 1: Testing Mock Behavior

**How TDD prevents it:**

```
TDD Step 1: Write test for real behavior
  ↓
Test fails because feature doesn't exist
  ↓
TDD Step 2: Implement minimal code
  ↓
Test passes - you tested real code, not mock

If you tested mock:
  ↓
Test would pass immediately (mock exists)
  ↓
Violation of TDD - didn't see failure
```

**Example workflow:**

```typescript
// TDD Step 1: Write failing test
test('renders sidebar with navigation', () => {
  render(<Page />);
  expect(screen.getByRole('navigation')).toBeInTheDocument();
});
// ❌ Fails - Page doesn't render sidebar yet

// TDD Step 2: Implement
function Page() {
  return <div><Sidebar role="navigation" /></div>;
}
// ✅ Passes - tested real component

// WRONG way (not TDD):
test('renders sidebar mock', () => {
  render(<Page />);
  expect(screen.getByTestId('sidebar-mock')).toBeInTheDocument();
});
// ✅ Passes immediately - you never saw it fail!
// This violates TDD - test must fail first
```

**TDD guarantee:** If test passes immediately, you're testing mock behavior.

### Anti-Pattern 2: Test-Only Methods in Production

**How TDD prevents it:**

```
TDD Step 1: Write test for needed behavior
  ↓
Test describes what production needs
  ↓
TDD Step 2: Minimal implementation
  ↓
Only code test needs is written

If method only for tests:
  ↓
No production test would need it
  ↓
TDD wouldn't add it
```

**Example workflow:**

```typescript
// TDD Step 1: Write test for production behavior
test('session expires after timeout', async () => {
  const session = new Session();
  await advanceTime(SESSION_TIMEOUT);
  expect(session.isActive()).toBe(false);
});
// ❌ Fails - Session doesn't handle expiry

// TDD Step 2: Implement expiry (production feature)
class Session {
  isActive() {
    return Date.now() < this.expiresAt;
  }
}
// ✅ Passes - implemented real behavior

// WRONG way (not TDD):
// "I need to cleanup in tests"
class Session {
  destroy() { /* for tests */ }  // ❌ Not driven by test!
}
```

**TDD guarantee:** Minimal implementation doesn't add untested code.

**Test cleanup belongs in test utilities:**
```typescript
// test-utils/session.ts
export async function cleanupSession(session: Session) {
  // Use public API for cleanup
  session.logout();
  // Clear any test-specific state
}
```

### Anti-Pattern 3: Mocking Without Understanding

**How TDD prevents it:**

```
TDD Step 1: Write test with real implementation
  ↓
Test runs - you see what it does
  ↓
Identify what's slow/external
  ↓
TDD Step 2: Mock at correct level
  ↓
Mock only slow part, preserve behavior

If you mock first:
  ↓
Never saw real behavior
  ↓
Don't understand dependencies
  ↓
Mock breaks test logic
```

**Example workflow:**

```typescript
// TDD Step 1: Write test with real implementation
test('detects duplicate server', async () => {
  await addServer(config);
  await expect(addServer(config))
    .rejects.toThrow('already exists');
});
// Run test - takes 2 seconds (server startup slow)
// Observe: Config written, duplicate detected
// Understand: Test depends on config persistence

// TDD Step 2: Mock at correct level
test('detects duplicate server', async () => {
  vi.mock('MCPServerManager');  // Mock slow startup only
  // Config writing preserved - test logic intact

  await addServer(config);
  await expect(addServer(config))
    .rejects.toThrow('already exists');
});
// ✅ Fast test, correct behavior

// WRONG way (not TDD):
// "Let me mock this upfront"
vi.mock('ToolCatalog', () => ({
  discoverAndCacheTools: vi.fn()  // Breaks test logic!
}));
// Never saw real behavior - don't know what test needs
```

**TDD guarantee:** You understand dependencies before mocking them.

### Anti-Pattern 4: Incomplete Mocks

**How TDD prevents it:**

```
TDD Step 1: Write test with real API
  ↓
See complete response structure
  ↓
TDD Step 2: Mock with complete structure
  ↓
Test still passes - mock is complete

If mock incomplete:
  ↓
Test fails - mock missing fields
  ↓
TDD forces you to complete mock
```

**Example workflow:**

```typescript
// TDD Step 1: Test with real API (or type)
test('displays user profile', async () => {
  const user = await api.getUser('123');
  // TypeScript shows complete User type
  render(<Profile user={user} />);
  expect(screen.getByText(user.name)).toBeInTheDocument();
  expect(screen.getByText(user.profile.timezone)).toBeInTheDocument();
});
// Observe complete structure

// TDD Step 2: Mock with complete structure
test('displays user profile', async () => {
  const mockUser: User = {  // TypeScript enforces completeness
    id: '123',
    name: 'Alice',
    email: 'alice@example.com',
    profile: {
      timezone: 'UTC',
      locale: 'en-US',
      avatar: null
    },
    metadata: { /* complete */ }
  };

  render(<Profile user={mockUser} />);
  expect(screen.getByText('Alice')).toBeInTheDocument();
});
// ✅ Complete mock, test passes

// WRONG way (not TDD):
const mockUser = { id: '123', name: 'Alice' };  // Incomplete
// Later breaks when profile.timezone accessed
```

**TDD guarantee:** Test with real data first reveals complete structure.

**Use TypeScript to enforce completeness:**
```typescript
// Define complete type from API
interface User {
  id: string;
  name: string;
  email: string;
  profile: UserProfile;
  metadata: UserMetadata;
}

// TypeScript errors if incomplete
const mock: User = {
  id: '123',
  name: 'Alice'
  // ❌ TypeScript error - missing fields
};
```

### Anti-Pattern 5: Tests as Afterthought

**How TDD prevents it:**

```
TDD Step 1: MUST write test first
  ↓
No implementation without test
  ↓
Test IS implementation process

If implementation first:
  ↓
Violated TDD - broke the cycle
```

**TDD makes tests as afterthought impossible:**

```typescript
// TDD Way - tests ARE implementation
// 1. RED
test('user registration validates email', async () => {
  await expect(registerUser('invalid'))
    .rejects.toThrow('Invalid email');
});
// ❌ Fails - registerUser doesn't exist

// 2. GREEN
async function registerUser(email: string) {
  if (!isValidEmail(email)) {
    throw new Error('Invalid email');
  }
  // ... registration
}
// ✅ Passes

// 3. REFACTOR
// Improve validation logic, tests still pass

// NOT TDD (anti-pattern):
// 1. Implement registerUser
// 2. "Feature complete, need to add tests"  ← Afterthought
```

**TDD workflow:**
- Tests first = tests are implementation
- No "testing phase" - tests are development
- Feature complete = tests passing
- Can't forget tests - can't write code without them

## TDD Workflow Prevents Anti-Patterns

### Complete TDD Cycle

```typescript
// Feature: User registration with email validation

// ========================================
// CYCLE 1: Email validation
// ========================================

// RED: Write failing test
test('rejects invalid email', async () => {
  await expect(registerUser('invalid'))
    .rejects.toThrow('Invalid email');
});
// ❌ Fails - registerUser doesn't exist

// GREEN: Minimal implementation
async function registerUser(email: string) {
  if (!email.includes('@')) {
    throw new Error('Invalid email');
  }
}
// ✅ Passes

// REFACTOR: Improve validation
async function registerUser(email: string) {
  if (!isValidEmail(email)) {  // Better validator
    throw new Error('Invalid email');
  }
}
// ✅ Still passes

// ========================================
// CYCLE 2: Database integration
// ========================================

// RED: Write failing test
test('saves user to database', async () => {
  const user = await registerUser('alice@example.com');
  const saved = await db.users.findById(user.id);
  expect(saved.email).toBe('alice@example.com');
});
// ❌ Fails - no database integration

// GREEN: Add database
async function registerUser(email: string) {
  if (!isValidEmail(email)) {
    throw new Error('Invalid email');
  }
  const user = await db.users.create({ email });  // Added
  return user;
}
// ✅ Passes

// REFACTOR: Extract repository pattern
// Tests still pass

// ========================================
// CYCLE 3: Duplicate prevention
// ========================================

// RED: Write failing test
test('prevents duplicate email', async () => {
  await registerUser('alice@example.com');
  await expect(registerUser('alice@example.com'))
    .rejects.toThrow('Email already exists');
});
// ❌ Fails - no duplicate check

// GREEN: Add check
async function registerUser(email: string) {
  if (!isValidEmail(email)) {
    throw new Error('Invalid email');
  }
  const existing = await db.users.findByEmail(email);
  if (existing) {
    throw new Error('Email already exists');  // Added
  }
  return await db.users.create({ email });
}
// ✅ Passes

// REFACTOR: Improve error handling
// Tests still pass
```

**Notice:**
- ✅ No mock behavior testing - real code tested
- ✅ No test-only methods - only what tests needed
- ✅ No uninformed mocking - saw real behavior first
- ✅ No incomplete mocks - used real structures
- ✅ No afterthought tests - tests first always

## TDD Mindset: Test-First Thinking

### Mental Model

**NOT TDD thinking:**
```
"How do I implement this feature?"
  ↓
Implement code
  ↓
"How do I test this?"
  ↓
Write tests
  ↓
Anti-patterns emerge
```

**TDD thinking:**
```
"How will I verify this works?"
  ↓
Write test
  ↓
"What's minimal code to pass?"
  ↓
Implement
  ↓
Anti-patterns prevented
```

### Test-First Benefits

**Design benefits:**
- API designed from consumer perspective
- Testable code (loose coupling)
- Clear interfaces
- Minimal implementation

**Quality benefits:**
- Verified behavior
- Edge cases caught early
- Regression prevention
- Living documentation

**Anti-pattern prevention:**
- Can't test mocks (test fails first against real code)
- No test-only methods (minimal implementation)
- Must understand dependencies (see real behavior)
- Complete structures (test with real data first)
- Tests ARE implementation (not afterthought)

## When TDD is Violated

**Signs you're not following TDD:**

```
🚩 Writing implementation before test
🚩 Test passes immediately
🚩 Skipping "watch it fail" step
🚩 Mocking before running test
🚩 "Implementation done, adding tests"
```

**Consequences:**
- All five anti-patterns become possible
- Test quality degrades
- False confidence
- Design issues

**Recovery:**
1. Stop adding features
2. Return to TDD workflow
3. Write test first for next feature
4. Watch it fail
5. Minimal implementation
6. Maintain discipline

## TDD and Mocking Strategy

### TDD-Compliant Mocking

**The right way:**

```typescript
// 1. Write test with real implementation
test('user registration sends welcome email', async () => {
  await registerUser('alice@example.com');
  // Observe: Email sending is slow (2 seconds)
});

// 2. Identify slow operation
// emailService.send() is slow (network call)

// 3. Mock at correct level
test('user registration sends welcome email', async () => {
  vi.spyOn(emailService, 'send').mockResolvedValue();

  await registerUser('alice@example.com');

  expect(emailService.send).toHaveBeenCalledWith({
    to: 'alice@example.com',
    subject: 'Welcome'
  });
});
// Test runs fast, verifies email logic
```

**TDD mocking principles:**

1. **Run with real implementation first** - See actual behavior
2. **Measure performance** - Mock only if slow
3. **Mock at boundaries** - I/O, network, filesystem
4. **Preserve business logic** - Don't mock what you're testing
5. **Verify behavior, not mocks** - Test results, not mock calls

### Mocking Levels in TDD

```
Layer 1: Business Logic
  → NEVER mock (this is what you're testing)

Layer 2: Application Services
  → RARELY mock (needed for assertions)

Layer 3: Adapters
  → SOMETIMES mock (if slow)

Layer 4: External I/O
  → USUALLY mock (network, DB, filesystem)

Layer 5: Infrastructure
  → ALWAYS mock in unit tests (servers, APIs)
```

## Common TDD Mistakes

### Mistake 1: Not Watching Test Fail

```typescript
// ❌ WRONG
test('feature works', () => {
  expect(true).toBe(true);  // Passes immediately
});
// Didn't verify test tests right thing

// ✅ RIGHT
test('validates email', async () => {
  await expect(register('invalid'))
    .rejects.toThrow('Invalid email');
});
// ❌ Fails first - register doesn't exist
// Then implement
// ✅ Passes - verified test works
```

### Mistake 2: Overly Complex Implementation

```typescript
// ❌ WRONG (not minimal)
test('adds numbers', () => {
  expect(add(2, 3)).toBe(5);
});

function add(a: number, b: number): number {
  // Not minimal - over-engineered
  return Array(b).fill(a).reduce((sum, n) => sum + n, 0);
}

// ✅ RIGHT (minimal)
function add(a: number, b: number): number {
  return a + b;  // Simplest solution
}
```

### Mistake 3: Testing Implementation Details

```typescript
// ❌ WRONG
test('uses cache', () => {
  const cache = new Cache();
  service.setCache(cache);  // Testing how it works
  expect(service.cache).toBe(cache);
});

// ✅ RIGHT
test('returns cached result', () => {
  service.get('key');
  service.get('key');
  expect(slowOperation).toHaveBeenCalledOnce();  // Testing behavior
});
```

## TDD Discipline

### Maintaining TDD Practice

**Commit to:**
- Test first, always
- Watch it fail, every time
- Minimal implementation, no gold plating
- Refactor with confidence

**When tempted to skip TDD:**
- "Just a small change" → Write test first
- "I know it works" → Write test to prove it
- "This is hard to test" → Design issue, fix it
- "Tests can wait" → No, they can't

**TDD is not optional:**
- Not a suggestion
- Not for later
- Not just for new features
- Not "when I have time"

**TDD is how you write code.**

## Integration with Other Skills

**Prerequisite skills:**
- **test-driven-development**: Complete TDD workflow (see skill library for full details)

**Complementary skills:**
- **verification-before-completion**: Definition of "done" (available in skill library)

**Anti-pattern prevention:**
- This skill - What TDD prevents

## Summary: TDD as Anti-Pattern Prevention

| Anti-Pattern | TDD Prevention Mechanism |
|--------------|---------------------------|
| Testing Mock Behavior | Must watch test fail against real code first |
| Test-Only Methods | Minimal implementation doesn't add them |
| Mocking Without Understanding | Run with real impl first, understand dependencies |
| Incomplete Mocks | Test with real data first, see complete structure |
| Tests as Afterthought | Tests first is the workflow, impossible to skip |

**The bottom line:** Strict TDD adherence makes testing anti-patterns nearly impossible to create.

If you find yourself with these anti-patterns, you've violated TDD. Return to the RED/GREEN/REFACTOR cycle.

## Quick Reference

**TDD Cycle:**
```
RED → Write failing test
GREEN → Minimal implementation
REFACTOR → Improve code
REPEAT → Next test
```

**Anti-pattern prevention:**
```
Test first → Prevents afterthought
Watch fail → Prevents mock testing
Minimal code → Prevents test-only methods
Real impl first → Prevents uninformed mocking
Real data first → Prevents incomplete mocks
```

**TDD discipline:**
```
Always test first
Always watch fail
Always minimal implementation
Always refactor
Never skip the cycle
```

See the Test-Driven Development skill for complete TDD workflow and examples (available in the skill library).