# Async Detection & SSE Streaming Tests

**Task**: AZ-142_test_async_sse
**Name**: Async Detection & SSE Streaming Tests
**Description**: Implement E2E tests verifying async media detection initiation, SSE event streaming, and duplicate media_id rejection
**Complexity**: 3 points
**Dependencies**: AZ-138_test_infrastructure
**Component**: Integration Tests
**Jira**: AZ-142
**Epic**: AZ-137

## Problem

Async media detection via POST /detect/{media_id} must return immediately with "started" status while processing continues in background. SSE streaming must deliver real-time detection events to connected clients. Duplicate media_id submissions must be rejected with 409.

## Outcome

- Async detection returns immediately without waiting for processing
- SSE connection receives detection events during processing
- Final SSE event signals completion with mediaStatus "AIProcessed"
- Duplicate media_id correctly rejected with 409 Conflict

## Scope

### Included
- FT-P-08: Async media detection returns "started" immediately
- FT-P-09: SSE streaming delivers detection events during async processing
- FT-N-04: Duplicate media_id returns 409

### Excluded
- Video frame sampling details (covered in video tests)
- SSE queue overflow behavior (covered in resource limit tests)
- Annotations service interaction (covered in resilience tests)

## Acceptance Criteria

**AC-1: Immediate async response**
Given an initialized engine
When POST /detect/{media_id} is called with config and auth headers
Then response arrives within 1s with {"status": "started"}

**AC-2: SSE event delivery**
Given an SSE client connected to GET /detect/stream
When async detection is triggered via POST /detect/{media_id}
Then SSE events are received with detection data and mediaStatus "AIProcessing"
And a final event with mediaStatus "AIProcessed" and percent 100 arrives

**AC-3: Duplicate media_id rejection**
Given an async detection is already in progress for a media_id
When POST /detect/{media_id} is called again with the same ID
Then response is 409 Conflict

## Non-Functional Requirements

**Performance**
- Async initiation response within 1s
- SSE events delivered within 120s total processing time

## Integration Tests

| AC Ref | Initial Data/Conditions | What to Test | Expected Behavior | NFR References |
|--------|------------------------|-------------|-------------------|----------------|
| AC-1 | Engine warm, test-video, JWT token | POST /detect/{media_id} | Response < 1s, status "started" | Max 2s |
| AC-2 | Engine warm, SSE connected, test-video | Listen SSE during async detection | Events received, final AIProcessed | Max 120s |
| AC-3 | Active detection in progress | Second POST with same media_id | 409 Conflict | Max 5s |

## Constraints

- SSE client must connect before triggering async detection
- JWT token required for async detection endpoint
- Test video must be accessible via configured paths

## Risks & Mitigation

**Risk 1: SSE connection timing**
- *Risk*: SSE connection may not be established before detection starts
- *Mitigation*: Add small delay between SSE connect and detection trigger; verify connection established
