---
phase: 08-e2e-scenario-tests
plan: 02
type: execute
wave: 1
depends_on: []
files_modified: [src/test/e2e/edge-cases.test.ts]
autonomous: true
---

<objective>
E2E tests proving edge case scenarios work: agent crashes, waiting for input, merge conflicts, and task blocking.

Purpose: Validate error handling and edge cases in dispatch/coordination flow work correctly with proper event emission and state management.
Output: Comprehensive test file covering all edge case scenarios.
</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
@.planning/phases/07-mock-agent-test-harness/07-01-SUMMARY.md
@.planning/phases/07-mock-agent-test-harness/07-02-SUMMARY.md

# Test infrastructure from Phase 7:
@src/test/harness.ts
@src/test/fixtures.ts
@src/agent/mock-manager.ts

# Types for understanding dispatch/coordination:
@src/dispatch/types.ts
@src/coordination/types.ts
</context>

<tasks>

<task type="auto">
  <name>Task 1: Create E2E edge case test file with crash and waiting scenarios</name>
  <files>src/test/e2e/edge-cases.test.ts</files>
  <action>
Create E2E tests for edge case scenarios using test harness.

Test scenarios to implement:

1. **Agent crash during task**:
   - Seed fixture, queue task
   - Set crash scenario for agent: `harness.setAgentScenario(agentName, { outcome: 'crash', message: 'Token limit exceeded' })`
   - Dispatch task, wait for completion
   - Verify: agent:spawned then agent:crashed events
   - Verify: task status should NOT be 'completed' (still in_progress or blocked)
   - Verify: error message captured

2. **Agent waiting for input and resume**:
   - Seed fixture, queue task
   - Set waiting scenario: `harness.setAgentScenario(agentName, { outcome: 'waiting_for_input', question: 'Which database?' })`
   - Dispatch task
   - Verify: agent:waiting event with question
   - Resume agent: `harness.agentManager.resume(agentId, 'PostgreSQL')`
   - Wait for completion
   - Verify: agent:resumed then agent:stopped events
   - Verify: task can now be completed

3. **Task blocking**:
   - Seed fixture, queue task
   - Call dispatchManager.blockTask(taskId, 'Waiting for user decision')
   - Verify: task appears in blocked list from getQueueState()
   - Verify: getNextDispatchable() does not return blocked task

Agent name pattern from dispatch: `agent-${taskId.slice(0, 6)}`
Use setAgentScenario before dispatch to configure behavior.

Pattern:
```typescript
vi.useFakeTimers();
const seeded = await harness.seedFixture(SIMPLE_FIXTURE);
const taskAId = seeded.tasks.get('Task A')!;

// Pre-seed required idle agent
await harness.agentManager.spawn({ name: 'pool-agent', taskId: 'placeholder', prompt: 'placeholder' });
await vi.runAllTimersAsync();

// Set scenario BEFORE dispatch
harness.setAgentScenario(`agent-${taskAId.slice(0, 6)}`, { outcome: 'crash' });

await harness.dispatchManager.queue(taskAId);
harness.clearEvents();
await harness.dispatchManager.dispatchNext();
await vi.runAllTimersAsync();

// Verify crash events
```
  </action>
  <verify>npm run test -- src/test/e2e/edge-cases.test.ts passes all tests</verify>
  <done>Crash, waiting-for-input, and blocking tests pass with proper event verification</done>
</task>

<task type="auto">
  <name>Task 2: Add merge conflict scenario test</name>
  <files>src/test/e2e/edge-cases.test.ts</files>
  <action>
Add test for merge conflict handling:

**Test: Merge conflict triggers handleConflict**
- Seed fixture, dispatch task, complete task
- Set up worktree for task
- Set conflict merge result: `harness.worktreeManager.setMergeResult(worktreeId, { success: false, conflicts: ['src/shared.ts', 'src/types.ts'], message: 'Merge conflict' })`
- Queue merge and process
- Verify: merge conflict detected
- Call handleConflict
- Verify: merge:conflict event emitted
- Verify: conflict appears in queue state

**Test: Successful merge after conflict resolution**
- After conflict, clear the merge result override
- Re-process merges
- Verify: merge succeeds

MockWorktreeManager methods:
- `setMergeResult(worktreeId, result)` - configure specific merge behavior
- Default behavior returns success

CoordinationManager methods:
- `queueMerge(taskId)` - queue completed task for merge
- `processMerges(targetBranch)` - process all ready merges
- `handleConflict(taskId, conflicts)` - handle merge conflict
- `getQueueState()` - check conflicted tasks

Note: Need to track worktreeId from agent spawn. MockAgentManager creates worktreeId during spawn - access via `agentInfo.worktreeId`. The CoordinationManager uses this when queueing for merge.
  </action>
  <verify>npm run test -- src/test/e2e/edge-cases.test.ts passes (including conflict tests)</verify>
  <done>Merge conflict and resolution tests pass, proves conflict handling works</done>
</task>

<task type="auto">
  <name>Task 3: Add test module index export</name>
  <files>src/test/e2e/index.ts</files>
  <action>
Create index file for E2E test module that exports test utilities if any were created.

If no shared utilities were created (tests are self-contained), create minimal index:

```typescript
/**
 * E2E Tests for Dispatch/Coordination Flows
 *
 * Test files:
 * - happy-path.test.ts: Normal operation scenarios
 * - edge-cases.test.ts: Error handling and edge cases
 *
 * Uses TestHarness from src/test/ for system wiring.
 */

// No exports needed - tests are self-contained
export {};
```

This documents the test module structure for future reference.
  </action>
  <verify>File exists and TypeScript compiles</verify>
  <done>E2E test module properly organized with index</done>
</task>

</tasks>

<verification>
Before declaring plan complete:
- [ ] `npm run test -- src/test/e2e/` passes all tests
- [ ] Agent crash test exists and passes
- [ ] Agent waiting for input test exists and passes
- [ ] Task blocking test exists and passes
- [ ] Merge conflict test exists and passes
- [ ] Conflict resolution test exists and passes
- [ ] No flaky tests (run twice to confirm)
</verification>

<success_criteria>

- All edge case E2E tests pass
- Tests use TestHarness and MockAgentManager scenarios
- Event verification confirms correct error handling
- Conflict handling proven working
- Agent recovery (waiting → resume) proven working
</success_criteria>

<output>
After completion, create `.planning/phases/08-e2e-scenario-tests/08-02-SUMMARY.md`
</output>
