monitor/api/tests/Unit/Controller/AgentControllerTest.php
fds2610-bot 857fc88834 Phase 1 der Neuausrichtung: REST-API fuer Tickets/Events/Systeme/KPIs/Agenten
Setzt Phase 1 aus Visualization.md um: Vanilla-PHP-REST-API mit SQLite-Persistenz,
statischen API-Keys pro Agent, Codeberg-Sync fuer Backlog/Done, PHPUnit-Tests und
vollstaendiger API.md-Dokumentation - Vorgaben aus Detailantworten.md (F1-F16)
umgesetzt. Entwicklung/Build/Test laufen komplett in Docker-Compose-Containern
(PHP 8.2 + SQLite + Composer + PHPUnit/pcov), analog zum NC-Gantt-Projekt ist auf
dem Host kein PHP/Composer noetig.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-07-27 19:38:52 +02:00

101 lines
3.3 KiB
PHP

<?php
declare(strict_types=1);
namespace Monitor\Api\Tests\Controller;
use Monitor\Api\Controller\AgentController;
use Monitor\Api\Db\Database;
use Monitor\Api\Http\Request;
use Monitor\Api\Repository\AgentRepository;
use PHPUnit\Framework\TestCase;
final class AgentControllerTest extends TestCase
{
private AgentController $controller;
private AgentRepository $repository;
protected function setUp(): void
{
$this->repository = new AgentRepository(Database::inMemory());
$this->controller = new AgentController($this->repository);
}
public function testRegisterRejectsMissingDisplayName(): void
{
$response = $this->controller->register(Request::create('POST', '/api/v1/agents', body: ['role' => 'entwickler']));
self::assertSame(422, $response->status);
}
public function testRegisterRejectsUnknownRole(): void
{
$response = $this->controller->register(Request::create('POST', '/api/v1/agents', body: [
'display_name' => 'Alice', 'role' => 'ceo',
]));
self::assertSame(422, $response->status);
}
public function testRegisterReturns201WithApiKey(): void
{
$response = $this->controller->register(Request::create('POST', '/api/v1/agents', body: [
'display_name' => 'Alice', 'role' => 'entwickler',
]));
self::assertSame(201, $response->status);
self::assertArrayHasKey('api_key', $response->body());
self::assertArrayNotHasKey('api_key_hash', $response->body()['agent']);
}
public function testActivityRejectsMismatchedAgentId(): void
{
$registration = $this->repository->register('Alice', 'entwickler');
$other = $this->repository->register('Bob', 'qa');
$response = $this->controller->activity(
Request::create('POST', '/x', body: ['state' => 'working']),
['id' => (string) $other['agent']['id']],
$registration['agent']
);
self::assertSame(403, $response->status);
}
public function testActivityRejectsMissingState(): void
{
$registration = $this->repository->register('Alice', 'entwickler');
$response = $this->controller->activity(
Request::create('POST', '/x', body: []),
['id' => (string) $registration['agent']['id']],
$registration['agent']
);
self::assertSame(422, $response->status);
}
public function testActivityUpdatesOwnState(): void
{
$registration = $this->repository->register('Alice', 'entwickler');
$response = $this->controller->activity(
Request::create('POST', '/x', body: ['state' => 'working', 'current_ticket' => 7]),
['id' => (string) $registration['agent']['id']],
$registration['agent']
);
self::assertSame(200, $response->status);
self::assertSame('working', $response->body()['agent']['state']);
self::assertSame(7, $response->body()['agent']['current_ticket_id']);
}
public function testIndexListsAgents(): void
{
$this->repository->register('Alice', 'entwickler');
$response = $this->controller->index(Request::create('GET', '/api/v1/agents'));
self::assertCount(1, $response->body()['agents']);
}
}