In Produktion lag eine Sprachnachricht unbearbeitet in der Warteschlange des
OPs-Agenten: aufgenommen wurde ueber seine Figur, abgeholt wird aber nur von
der Maschine, auf der whisper.cpp laeuft. Die Agent-Zuweisung hat eine
Zustaendigkeit vorgetaeuscht, die es nicht gibt - welches Ticket in welchem
Repo entsteht, entscheidet der abholende Bot anhand des Transkripts.
- Migration 007: voice_recordings ohne agent_id. Bestehende Aufnahmen behalten
ihren Status, die haengengebliebene wandert damit in die globale Warteschlange.
- Migration 008: neue Agenten-Rolle "voice" fuer den Voice-Bot.
- Endpunkte ohne Agent im Pfad: POST/GET /api/v1/recordings,
GET /api/v1/recordings/{id}/audio, PATCH /api/v1/recordings/{id} - und ohne
Ownership-Check, an dem die Abholung mit fremdem Key bisher scheiterte.
- GET /api/v1/agents traegt pending_recordings jetzt neben der Liste statt an
jedem Agenten.
- Frontend: nur noch die Figur des Voice-Bots verlinkt auf record.html (ohne
?agent=), Zaehler und Mikrofon-Deko sitzen an seinem Schreibtisch. Die
Aufnahmeseite nennt den abholenden Bot und bittet darum, das Projekt am
Anfang der Aufnahme zu nennen.
Die Bot-Seite (bin/voice-recording-agent.sh) folgt in fds2610/Agent.
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
159 lines
5.7 KiB
PHP
159 lines
5.7 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 Monitor\Api\Repository\TicketRepository;
|
|
use Monitor\Api\Repository\VoiceRecordingRepository;
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
final class AgentControllerTest extends TestCase
|
|
{
|
|
private AgentController $controller;
|
|
private AgentRepository $repository;
|
|
private TicketRepository $tickets;
|
|
private VoiceRecordingRepository $recordings;
|
|
private string $recordingsStorageDir;
|
|
|
|
protected function setUp(): void
|
|
{
|
|
$db = Database::inMemory();
|
|
$this->repository = new AgentRepository($db);
|
|
$this->tickets = new TicketRepository($db);
|
|
$this->recordingsStorageDir = sys_get_temp_dir() . '/monitor-agent-controller-recordings-' . uniqid();
|
|
$this->recordings = new VoiceRecordingRepository($db, $this->recordingsStorageDir);
|
|
$this->controller = new AgentController($this->repository, $this->recordings);
|
|
}
|
|
|
|
protected function tearDown(): void
|
|
{
|
|
if (is_dir($this->recordingsStorageDir)) {
|
|
foreach (glob($this->recordingsStorageDir . '/*') ?: [] as $file) {
|
|
unlink($file);
|
|
}
|
|
rmdir($this->recordingsStorageDir);
|
|
}
|
|
}
|
|
|
|
public function testMeReturnsAuthenticatedAgentWithoutApiKeyHash(): void
|
|
{
|
|
$this->repository->register('Alice', 'entwickler');
|
|
$ops = $this->repository->register('OPs', 'ops');
|
|
|
|
$response = $this->controller->me(Request::create('GET', '/api/v1/agents/me'), $ops['agent']);
|
|
|
|
self::assertSame(200, $response->status);
|
|
self::assertSame($ops['agent']['id'], $response->body()['agent']['id']);
|
|
self::assertArrayNotHasKey('api_key_hash', $response->body()['agent']);
|
|
}
|
|
|
|
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');
|
|
$ticketId = (int) $this->tickets->create(['title' => 'Testticket'])['id'];
|
|
|
|
$response = $this->controller->activity(
|
|
Request::create('POST', '/x', body: ['state' => 'working', 'current_ticket' => $ticketId]),
|
|
['id' => (string) $registration['agent']['id']],
|
|
$registration['agent']
|
|
);
|
|
|
|
self::assertSame(200, $response->status);
|
|
self::assertSame('working', $response->body()['agent']['state']);
|
|
self::assertSame($ticketId, $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']);
|
|
}
|
|
|
|
/**
|
|
* Der Zaehler auf dem Schreibtisch des Voice-Bots haengt an diesem Feld - es steht neben
|
|
* der Agentenliste, nicht an jedem Agenten: die Warteschlange ist global.
|
|
*/
|
|
public function testIndexIncludesPendingRecordingsCount(): void
|
|
{
|
|
$this->repository->register('Vox', 'voice');
|
|
$done = $this->recordings->store('a', 'audio/webm');
|
|
$this->recordings->store('b', 'audio/webm');
|
|
$this->recordings->markDone((int) $done['id']);
|
|
|
|
$response = $this->controller->index(Request::create('GET', '/api/v1/agents'));
|
|
|
|
self::assertSame(1, $response->body()['pending_recordings']);
|
|
self::assertArrayNotHasKey('pending_recordings', $response->body()['agents'][0]);
|
|
}
|
|
|
|
public function testIndexDefaultsPendingRecordingsToZero(): void
|
|
{
|
|
$this->repository->register('Alice', 'entwickler');
|
|
|
|
$response = $this->controller->index(Request::create('GET', '/api/v1/agents'));
|
|
|
|
self::assertSame(0, $response->body()['pending_recordings']);
|
|
}
|
|
}
|