test: add xUnit test coverage for API controllers and client services

Create ChatAgent.Api.Tests and ChatAgent.Client.Tests projects with xUnit
and Moq. Test HealthController (200 + valid response), ChatController
(SSE streaming with mocked upstream, error handling), and ChatApiClient
(delta parsing, error events, health endpoint). 6 tests, all passing.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
local
2026-04-04 02:21:10 +01:00
parent 00e7df2802
commit 17a5a58e73
13 changed files with 632 additions and 0 deletions

View File

@@ -0,0 +1,56 @@
## ADDED Requirements
### Requirement: API test project exists
An xUnit test project SHALL exist at `tests/ChatAgent.Api.Tests/` targeting the API project, with xUnit, Moq, and Microsoft.AspNetCore.Mvc.Testing as dependencies.
#### Scenario: API tests run
- **WHEN** `dotnet test` is run from the solution root
- **THEN** API tests are discovered and executed
### Requirement: Client test project exists
An xUnit test project SHALL exist at `tests/ChatAgent.Client.Tests/` targeting the Client services, with xUnit and Moq as dependencies.
#### Scenario: Client tests run
- **WHEN** `dotnet test` is run from the solution root
- **THEN** Client service tests are discovered and executed
### Requirement: HealthController test coverage
Tests SHALL verify that GET /api/health returns HTTP 200 with a valid HealthResponse containing a non-empty Status and a recent Timestamp.
#### Scenario: Health endpoint returns 200
- **WHEN** a GET request is sent to /api/health
- **THEN** the response status is 200 and the body contains `status: "healthy"` and a UTC timestamp
### Requirement: ChatController test coverage
Tests SHALL verify that POST /api/chat returns a streaming SSE response containing text deltas and a [DONE] terminator. Tests SHALL mock the upstream Responses API.
#### Scenario: Chat streams text deltas
- **WHEN** a POST is sent to /api/chat with a valid ChatRequest
- **THEN** the response is `text/event-stream` containing `data: {"text":"..."}` events followed by `data: [DONE]`
#### Scenario: Chat handles upstream error
- **WHEN** the Responses API is unreachable or returns an error
- **THEN** the response contains a `data: {"error":"..."}` event followed by `data: [DONE]`
### Requirement: ChatApiClient test coverage
Tests SHALL verify that SendChatStreamingAsync correctly parses SSE events from the backend into text deltas, handles [DONE], and throws on error events.
#### Scenario: Client parses text deltas
- **WHEN** the backend returns SSE events with text deltas
- **THEN** SendChatStreamingAsync yields each text fragment in order
#### Scenario: Client handles error event
- **WHEN** the backend returns an error SSE event
- **THEN** SendChatStreamingAsync throws HttpRequestException with the error message