# Tasks

## Task 1: Extend Seed Script with New Test Data

- [x] 1.1 Add deterministic UUID constants for watchlists, approvals, lockouts, notifications, ingestion runs, saved queries, and daily risk snapshots
- [x] 1.2 Add `_seed_watchlists` function to insert 2 watchlists with 3 watchlist members
- [x] 1.3 Add `_seed_operator_approvals` function to insert 1 pending and 1 approved approval record
- [x] 1.4 Add `_seed_symbol_lockouts` function to insert 1 active and 1 expired lockout
- [x] 1.5 Add `_seed_notifications` function to insert 1 delivered notification record
- [x] 1.6 Add `_seed_ingestion_runs` function to insert 2 ingestion runs (completed + failed)
- [x] 1.7 Add `_seed_saved_queries` function to insert 1 saved query record
- [x] 1.8 Add `_seed_daily_risk_snapshots` function to insert 1 daily risk snapshot
- [x] 1.9 Export new seed ID lookup dictionaries (SEED_WATCHLIST_IDS, SEED_APPROVAL_IDS, SEED_LOCKOUT_IDS, etc.)
- [x] 1.10 Call all new seed functions from the main `seed()` function
- [x] 1.11 Update conftest.py to import and expose new seed IDs in the `seed_ids` fixture

## Task 2: Query API Extended Tests — Documents and Evidence

- [x] 2.1 Create `tests/integration/test_query_api_extended.py` with test class for document filtering (by ticker, by doc_type)
- [x] 2.2 Add test for document detail with non-existent UUID returning 404
- [x] 2.3 Add test for recommendation evidence drill-down endpoint (`/api/recommendations/{id}/evidence`)
- [x] 2.4 Add test for trend evidence drill-down endpoint (`/api/trends/{id}/evidence`)
- [x] 2.5 Add test for trend projection endpoint (`/api/trends/{id}/projection`)
- [x] 2.6 Add test for trend projection with no projection returning appropriate response

## Task 3: Query API Extended Tests — Macro and Competitive Layer

- [x] 3.1 Add test for macro status endpoint (`/api/admin/macro/status`)
- [x] 3.2 Add test for macro events list endpoint (`/api/macro/events`) verifying seeded events
- [x] 3.3 Add test for macro event detail endpoint (`/api/macro/events/{id}`) with impacts
- [x] 3.4 Add test for macro impacts by ticker endpoint (`/api/macro/impacts/AAPL`)
- [x] 3.5 Add test for competitive status endpoint (`/api/admin/competitive/status`)
- [x] 3.6 Add test for competitive signals endpoint (`/api/patterns/{ticker}/competitive-signals`)
- [x] 3.7 Add test for patterns endpoint (`/api/patterns/{ticker}`)

## Task 4: Query API Extended Tests — Operational and Admin Endpoints

- [x] 4.1 Add test for pipeline health endpoint verifying all required fields
- [x] 4.2 Add test for ingestion summary endpoint verifying `total_runs` and `by_source_type`
- [x] 4.3 Add test for coverage gaps endpoint verifying `missing_source_types` and `stale_sources`
- [x] 4.4 Add test for source toggle endpoint (`PUT /api/admin/sources/{id}/toggle`)
- [x] 4.5 Add test for trading config endpoint (`GET /api/admin/trading/config`)
- [x] 4.6 Add test for pending approvals endpoint (`GET /api/admin/trading/approvals`)
- [x] 4.7 Add test for active lockouts endpoint (`GET /api/admin/trading/lockouts`)
- [x] 4.8 Add test for lockout create and delete lifecycle (`POST` then `DELETE /api/admin/trading/lockouts`)

## Task 5: Query API Extended Tests — Agents and Analytics

- [x] 5.1 Add test for agent detail endpoint (`GET /api/agents/{id}`) verifying `system_prompt`, `temperature`, `max_tokens`
- [x] 5.2 Add test for agent create endpoint (`POST /api/agents`) verifying 201 and slug generation
- [x] 5.3 Add test for agent update endpoint (`PUT /api/agents/{id}`)
- [x] 5.4 Add test for variant create endpoint (`POST /api/agents/{id}/variants`) verifying 201
- [x] 5.5 Add test for variant activate endpoint (`POST /api/agents/{id}/variants/{vid}/activate`)
- [x] 5.6 Add test for agent performance endpoint (`GET /api/agents/{id}/performance`)
- [x] 5.7 Add test for variant performance endpoint (`GET /api/agents/{id}/variants/{vid}/performance`)
- [x] 5.8 Add test for PostgreSQL schema endpoint (`GET /api/analytics/pg-schema`)
- [x] 5.9 Add test for saved queries list endpoint (`GET /api/analytics/saved-queries`)
- [x] 5.10 Add test for saved query create and delete lifecycle
- [x] 5.11 Add test for pg-query endpoint with a valid read-only SQL query

## Task 6: Symbol Registry Write Path and Edge Case Tests

- [x] 6.1 Create `tests/integration/test_registry_write_paths.py` with test for duplicate company creation returning 409
- [x] 6.2 Add test for company not found returning 404
- [x] 6.3 Add test for alias creation (`POST /companies/{id}/aliases`) returning 201
- [x] 6.4 Add test for source creation (`POST /companies/{id}/sources`) returning 201
- [x] 6.5 Add test for source creation with invalid source_type returning 422
- [x] 6.6 Add test for watchlist creation (`POST /watchlists`) returning 201
- [x] 6.7 Add test for watchlist member addition (`POST /watchlists/{id}/members/{company_id}`) returning 201
- [x] 6.8 Add test for watchlist members list (`GET /watchlists/{id}/members`) with ticker and legal_name
- [x] 6.9 Add test for duplicate watchlist creation returning 409

## Task 7: Symbol Registry — Competitor and Exposure Write Paths

- [x] 7.1 Add test for competitor creation (`POST /companies/{id}/competitors`) returning 201
- [x] 7.2 Add test for self-referencing competitor returning 400
- [x] 7.3 Add test for competitor update (`PUT /companies/{id}/competitors/{rel_id}`)
- [x] 7.4 Add test for competitor soft-delete (`DELETE /companies/{id}/competitors/{rel_id}`) returning 200
- [x] 7.5 Add test for exposure profile upsert (`PUT /companies/{id}/exposure`) with version increment
- [x] 7.6 Add test for exposure history endpoint (`GET /companies/{id}/exposure/history`)
- [x] 7.7 Add test for exposure profile not found returning 404

## Task 8: Risk Engine — Evaluation Edge Cases and Approval Lifecycle

- [x] 8.1 Create `tests/integration/test_risk_approval_lifecycle.py` with test for minimal order evaluation
- [x] 8.2 Add test for order exceeding position cap returning `eligible: false`
- [x] 8.3 Add test for evaluation with custom config override
- [x] 8.4 Add test for pending approvals list from seeded data
- [x] 8.5 Add test for approval detail endpoint (`GET /approvals/{id}`) with seeded pending approval
- [x] 8.6 Add test for approval review (`POST /approvals/{id}/review` with `approved: true`)
- [x] 8.7 Add test for review of non-existent approval returning 404
- [x] 8.8 Add test for approval expiry endpoint (`POST /approvals/expire`)

## Task 9: Trading Engine — Configuration Round-Trips and Extended Tests

- [x] 9.1 Create `tests/integration/test_trading_extended.py` with config round-trip test (PUT config → GET status)
- [x] 9.2 Add test for pause/resume round-trip (POST pause → GET status → POST resume → GET status)
- [x] 9.3 Add test for metrics consistency (`total_portfolio_value ≈ active_pool + reserve_pool + unrealized_pnl`)
- [x] 9.4 Add test for metrics history returning portfolio snapshots
- [x] 9.5 Add test for notification config update round-trip (PUT → GET)
- [x] 9.6 Add test for notification history endpoint
- [x] 9.7 Add test for decisions filtering by ticker
- [x] 9.8 Add test for decisions filtering by limit
- [x] 9.9 Add test for decisions filtering by decision type
- [x] 9.10 Add test for override order with invalid ticker returning 422
- [x] 9.11 Add test for override order with zero/negative quantity returning 422
- [x] 9.12 Add test for valid override order returning 202 or structured error

## Task 10: Cross-Service Round-Trip Tests

- [x] 10.1 Create `tests/integration/test_cross_service_roundtrip.py` with test creating company via registry then reading via query API
- [x] 10.2 Add test for exposure profile round-trip (PUT via registry → GET via registry)
- [x] 10.3 Add test for competitor relationship bidirectional visibility
- [x] 10.4 Add test for risk evaluation schema matching what query API returns for recommendations

## Task 11: Error Handling and Empty-State Tests

- [x] 11.1 Create `tests/integration/test_error_handling.py` with test for empty list endpoints returning 200 with `[]`
- [x] 11.2 Add test for non-existent UUID detail endpoints returning 404 with JSON body
- [x] 11.3 Add test for invalid JSON body returning 422 with validation details
- [x] 11.4 Add test for trend projection with no projection returning appropriate response (not 500)
- [x] 11.5 Add test for macro impacts with no data returning 200 with empty impacts list
- [x] 11.6 Add test verifying all four service health endpoints return `{"status": "ok"}`
- [x] 11.7 Add test for override order structured error when Redis unavailable (503 with JSON, not unhandled exception)
