# Testing Patterns

## Test Coverage Goal

Tests should cover all paths in the corresponding `docs/commands/*.md`:

- **Process Flow**: Each branch in the mermaid flowchart = one test case
- **Error Scenarios**: Each error code listed = one test case
- **Idempotent paths**: If flowchart shows "Already exists? → Return existing"

## Mock Database

```typescript
const { db, spies } = createMockDb<DB>();

// Single return
spies.select.mockReturnValue(entity);

// Sequential returns (in query execution order)
spies.select.mockReturnValueOnce(first).mockReturnValueOnce(second);
```

## Fixtures (`testing/fixtures.ts`)

- Import `Schema` from `lib/types` (not `Namespace` from generated code)
- Pattern: `export const baseEntity = { ... } as const satisfies Entity<Schema>`
- Fixed IDs for traceability: `"entity-1"`
- Consistent timestamp: `new Date("2024-01-01T00:00:00.000Z")`
- `updatedAt: null` for base fixtures
