---
phase: 11-architect-agent
plan: 07
type: execute
wave: 4
depends_on: ["11-01", "11-02", "11-03"]
files_modified:
  - src/agent/mock-manager.test.ts
  - src/db/repositories/initiative-repository.test.ts
  - src/db/repositories/phase-repository.test.ts
autonomous: true
---

<objective>
Add unit tests for mode schemas, MockAgentManager modes, and new repositories.

Purpose: Ensure mode-specific functionality works correctly before E2E testing.

Output: Test coverage for agent modes, initiative repository, and phase repository.
</objective>

<execution_context>
@~/.claude/get-shit-done/workflows/execute-plan.md
@~/.claude/get-shit-done/templates/summary.md
</execution_context>

<context>
@.planning/PROJECT.md
@.planning/ROADMAP.md
@.planning/STATE.md

# Test patterns
@src/agent/mock-manager.test.ts
@src/db/repositories/drizzle-task-repository.test.ts
</context>

<tasks>

<task type="auto">
  <name>Task 1: Add MockAgentManager mode tests</name>
  <files>src/agent/mock-manager.test.ts</files>
  <action>
Add test suite for mode functionality:

```typescript
describe('agent modes', () => {
  it('should spawn agent with default execute mode', async () => {
    const agent = await manager.spawn({
      name: 'exec-agent',
      taskId: 't1',
      prompt: 'test',
    });
    expect(agent.mode).toBe('execute');
  });

  it('should spawn agent in discuss mode', async () => {
    manager.setScenario('discuss-agent', {
      status: 'context_complete',
      delay: 0,
      decisions: [{ topic: 'Auth', decision: 'JWT', reason: 'Standard' }],
      summary: 'Auth discussion complete',
    });

    const agent = await manager.spawn({
      name: 'discuss-agent',
      taskId: 't1',
      prompt: 'discuss auth',
      mode: 'discuss',
    });

    expect(agent.mode).toBe('discuss');
  });

  it('should spawn agent in breakdown mode', async () => {
    manager.setScenario('breakdown-agent', {
      status: 'breakdown_complete',
      delay: 0,
      phases: [
        { number: 1, name: 'Foundation', description: 'Core setup' },
        { number: 2, name: 'Features', description: 'Main features' },
      ],
    });

    const agent = await manager.spawn({
      name: 'breakdown-agent',
      taskId: 't1',
      prompt: 'breakdown work',
      mode: 'breakdown',
    });

    expect(agent.mode).toBe('breakdown');
  });

  it('should emit stopped event with context_complete reason', async () => {
    manager.setScenario('discuss-done', {
      status: 'context_complete',
      delay: 0,
      decisions: [],
      summary: 'Done',
    });

    await manager.spawn({
      name: 'discuss-done',
      taskId: 't1',
      prompt: 'test',
      mode: 'discuss',
    });
    await vi.runAllTimersAsync();

    const stopped = eventBus.emittedEvents.find(e => e.type === 'agent:stopped');
    expect(stopped?.payload.reason).toBe('context_complete');
  });

  it('should emit stopped event with breakdown_complete reason', async () => {
    manager.setScenario('breakdown-done', {
      status: 'breakdown_complete',
      delay: 0,
      phases: [],
    });

    await manager.spawn({
      name: 'breakdown-done',
      taskId: 't1',
      prompt: 'test',
      mode: 'breakdown',
    });
    await vi.runAllTimersAsync();

    const stopped = eventBus.emittedEvents.find(e => e.type === 'agent:stopped');
    expect(stopped?.payload.reason).toBe('breakdown_complete');
  });
});
```
  </action>
  <verify>npm test src/agent/mock-manager.test.ts passes</verify>
  <done>Mode tests added for MockAgentManager</done>
</task>

<task type="auto">
  <name>Task 2: Add InitiativeRepository tests</name>
  <files>src/db/repositories/initiative-repository.test.ts</files>
  <action>
Create test file following DrizzleTaskRepository test patterns:

```typescript
import { describe, it, expect, beforeEach, afterEach } from 'vitest';
import { DrizzleInitiativeRepository } from './drizzle-initiative-repository.js';
import { createTestDatabase, cleanupTestDatabase } from '../test-utils.js';

describe('DrizzleInitiativeRepository', () => {
  let repository: DrizzleInitiativeRepository;
  let cleanup: () => Promise<void>;

  beforeEach(async () => {
    const { db, cleanup: c } = await createTestDatabase();
    cleanup = c;
    repository = new DrizzleInitiativeRepository(db);
  });

  afterEach(async () => {
    await cleanup();
  });

  describe('create', () => {
    it('should create initiative with generated id', async () => {
      const initiative = await repository.create({
        name: 'Test Initiative',
        description: 'Test description',
        status: 'active',
      });

      expect(initiative.id).toBeDefined();
      expect(initiative.name).toBe('Test Initiative');
      expect(initiative.status).toBe('active');
    });

    it('should set timestamps on create', async () => {
      const initiative = await repository.create({
        name: 'Test',
        status: 'active',
      });

      expect(initiative.createdAt).toBeInstanceOf(Date);
      expect(initiative.updatedAt).toBeInstanceOf(Date);
    });
  });

  describe('findById', () => {
    it('should find existing initiative', async () => {
      const created = await repository.create({ name: 'Find Me', status: 'active' });
      const found = await repository.findById(created.id);
      expect(found).not.toBeNull();
      expect(found?.name).toBe('Find Me');
    });

    it('should return null for non-existent id', async () => {
      const found = await repository.findById('non-existent');
      expect(found).toBeNull();
    });
  });

  describe('findByStatus', () => {
    it('should filter by status', async () => {
      await repository.create({ name: 'Active 1', status: 'active' });
      await repository.create({ name: 'Active 2', status: 'active' });
      await repository.create({ name: 'Completed', status: 'completed' });

      const active = await repository.findByStatus('active');
      expect(active).toHaveLength(2);

      const completed = await repository.findByStatus('completed');
      expect(completed).toHaveLength(1);
    });
  });

  describe('update', () => {
    it('should update initiative fields', async () => {
      const created = await repository.create({ name: 'Original', status: 'active' });
      const updated = await repository.update(created.id, { name: 'Updated' });

      expect(updated.name).toBe('Updated');
      expect(updated.updatedAt.getTime()).toBeGreaterThan(created.updatedAt.getTime());
    });
  });
});
```

Note: If createTestDatabase doesn't exist, create a simple test utility or use in-memory SQLite.
  </action>
  <verify>npm test src/db/repositories/initiative-repository.test.ts passes</verify>
  <done>Initiative repository tests added</done>
</task>

<task type="auto">
  <name>Task 3: Add PhaseRepository tests</name>
  <files>src/db/repositories/phase-repository.test.ts</files>
  <action>
Create test file:

```typescript
import { describe, it, expect, beforeEach, afterEach } from 'vitest';
import { DrizzlePhaseRepository } from './drizzle-phase-repository.js';
import { DrizzleInitiativeRepository } from './drizzle-initiative-repository.js';
import { createTestDatabase, cleanupTestDatabase } from '../test-utils.js';

describe('DrizzlePhaseRepository', () => {
  let phaseRepo: DrizzlePhaseRepository;
  let initRepo: DrizzleInitiativeRepository;
  let initiativeId: string;
  let cleanup: () => Promise<void>;

  beforeEach(async () => {
    const { db, cleanup: c } = await createTestDatabase();
    cleanup = c;
    phaseRepo = new DrizzlePhaseRepository(db);
    initRepo = new DrizzleInitiativeRepository(db);

    // Create parent initiative
    const init = await initRepo.create({ name: 'Test Initiative', status: 'active' });
    initiativeId = init.id;
  });

  afterEach(async () => {
    await cleanup();
  });

  describe('create', () => {
    it('should create phase with generated id', async () => {
      const phase = await phaseRepo.create({
        initiativeId,
        number: 1,
        name: 'Phase 1',
        description: 'First phase',
        status: 'pending',
      });

      expect(phase.id).toBeDefined();
      expect(phase.number).toBe(1);
      expect(phase.name).toBe('Phase 1');
    });
  });

  describe('findByInitiativeId', () => {
    it('should return phases ordered by number', async () => {
      await phaseRepo.create({ initiativeId, number: 3, name: 'Phase 3', status: 'pending' });
      await phaseRepo.create({ initiativeId, number: 1, name: 'Phase 1', status: 'pending' });
      await phaseRepo.create({ initiativeId, number: 2, name: 'Phase 2', status: 'pending' });

      const phases = await phaseRepo.findByInitiativeId(initiativeId);

      expect(phases).toHaveLength(3);
      expect(phases[0].number).toBe(1);
      expect(phases[1].number).toBe(2);
      expect(phases[2].number).toBe(3);
    });
  });

  describe('getNextNumber', () => {
    it('should return 1 for empty initiative', async () => {
      const next = await phaseRepo.getNextNumber(initiativeId);
      expect(next).toBe(1);
    });

    it('should return max + 1', async () => {
      await phaseRepo.create({ initiativeId, number: 1, name: 'P1', status: 'pending' });
      await phaseRepo.create({ initiativeId, number: 2, name: 'P2', status: 'pending' });

      const next = await phaseRepo.getNextNumber(initiativeId);
      expect(next).toBe(3);
    });
  });

  describe('findByNumber', () => {
    it('should find phase by initiative and number', async () => {
      await phaseRepo.create({ initiativeId, number: 1, name: 'Phase 1', status: 'pending' });

      const found = await phaseRepo.findByNumber(initiativeId, 1);
      expect(found).not.toBeNull();
      expect(found?.name).toBe('Phase 1');
    });
  });
});
```
  </action>
  <verify>npm test src/db/repositories/phase-repository.test.ts passes</verify>
  <done>Phase repository tests added</done>
</task>

</tasks>

<verification>
Before declaring plan complete:
- [ ] npm test passes for all new test files
- [ ] Mode tests cover execute, discuss, breakdown
- [ ] Repository tests cover CRUD operations
- [ ] All existing tests still pass
</verification>

<success_criteria>

- All tasks completed
- MockAgentManager modes tested
- Initiative repository CRUD tested
- Phase repository CRUD tested
- getNextNumber edge cases tested
</success_criteria>

<output>
After completion, create `.planning/phases/11-architect-agent/11-07-SUMMARY.md`
</output>
