Der OPs-Agent war seit seiner Registrierung am 31.08. nie praesent: seine monitor-api.conf stammt von vor Issue #75 und kennt MONITOR_AGENT_ID nicht, report_activity() in check_fds.sh meldete deshalb stillschweigend nichts - waehrend die Systemmeldungen mit demselben Key weiter ankamen. - API: jeder mit X-Api-Key authentifizierte Request frischt last_seen_at auf (AgentRepository::touch(), Zustand/Ticket bleiben). Greift auch, wenn auf dem Ping-Rechner noch ein aelteres check_fds.sh liegt. - API: neuer Endpunkt GET /api/v1/agents/me liefert den Agenten zum Key. - check_fds.sh: fehlt MONITOR_AGENT_ID, wird sie ueber /agents/me ermittelt und in monitor-api.conf nachgetragen (ohne Admin-Key). Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01F5rnDxZCwLYiEVyfrAvsAA
177 lines
6 KiB
PHP
177 lines
6 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Monitor\Api\Tests\Repository;
|
|
|
|
use InvalidArgumentException;
|
|
use Monitor\Api\Db\Database;
|
|
use Monitor\Api\Http\NotFoundException;
|
|
use Monitor\Api\Repository\AgentRepository;
|
|
use Monitor\Api\Repository\TicketRepository;
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
final class AgentRepositoryTest extends TestCase
|
|
{
|
|
private AgentRepository $repository;
|
|
private TicketRepository $tickets;
|
|
private Database $db;
|
|
|
|
protected function setUp(): void
|
|
{
|
|
$this->db = Database::inMemory();
|
|
$this->repository = new AgentRepository($this->db, deskCount: 2);
|
|
$this->tickets = new TicketRepository($this->db);
|
|
}
|
|
|
|
public function testRegisterAssignsLowestFreeDesk(): void
|
|
{
|
|
$first = $this->repository->register('Alice', 'entwickler');
|
|
$second = $this->repository->register('Bob', 'qa');
|
|
|
|
self::assertSame(0, $first['agent']['desk_index']);
|
|
self::assertSame(1, $second['agent']['desk_index']);
|
|
self::assertNotSame($first['api_key'], $second['api_key']);
|
|
self::assertArrayNotHasKey('api_key_hash', $first['agent']);
|
|
}
|
|
|
|
public function testUserRoleNeverGetsADesk(): void
|
|
{
|
|
$result = $this->repository->register('Carol', 'user');
|
|
|
|
self::assertNull($result['agent']['desk_index']);
|
|
}
|
|
|
|
/**
|
|
* Issue #66: "ops" ist die Rolle fuer den Betrieb (z.B. der Ping-Check) und sitzt -
|
|
* anders als "user" - sichtbar an einem Schreibtisch. Der Test deckt zugleich den
|
|
* erweiterten CHECK-Constraint aus 004_agent_role_ops.sql ab: ohne ihn wuerde das
|
|
* INSERT scheitern, nicht erst die Zuweisung.
|
|
*/
|
|
public function testOpsRoleGetsADesk(): void
|
|
{
|
|
$result = $this->repository->register('OPs', 'ops');
|
|
|
|
self::assertSame('ops', $result['agent']['role']);
|
|
self::assertSame(0, $result['agent']['desk_index']);
|
|
}
|
|
|
|
public function testOverflowAgentGetsNoDeskWhenAllOccupied(): void
|
|
{
|
|
$this->repository->register('Alice', 'entwickler');
|
|
$this->repository->register('Bob', 'qa');
|
|
$third = $this->repository->register('Dan', 'product_owner');
|
|
|
|
self::assertNull($third['agent']['desk_index']);
|
|
}
|
|
|
|
public function testRegisterRejectsUnknownRole(): void
|
|
{
|
|
$this->expectException(InvalidArgumentException::class);
|
|
|
|
$this->repository->register('Eve', 'ceo');
|
|
}
|
|
|
|
public function testFindByApiKeyReturnsMatchingAgent(): void
|
|
{
|
|
$result = $this->repository->register('Alice', 'entwickler');
|
|
|
|
$found = $this->repository->findByApiKey($result['api_key']);
|
|
|
|
self::assertNotNull($found);
|
|
self::assertSame($result['agent']['id'], $found['id']);
|
|
}
|
|
|
|
public function testFindByApiKeyReturnsNullForUnknownKey(): void
|
|
{
|
|
self::assertNull($this->repository->findByApiKey('does-not-exist'));
|
|
}
|
|
|
|
public function testUpdateActivityChangesStateAndTicket(): void
|
|
{
|
|
$result = $this->repository->register('Alice', 'entwickler');
|
|
$id = (int) $result['agent']['id'];
|
|
$ticketId = (int) $this->tickets->create(['title' => 'Testticket'])['id'];
|
|
|
|
$updated = $this->repository->updateActivity($id, 'working', $ticketId);
|
|
|
|
self::assertSame('working', $updated['state']);
|
|
self::assertSame($ticketId, $updated['current_ticket_id']);
|
|
}
|
|
|
|
public function testUpdateActivityRejectsUnknownTicket(): void
|
|
{
|
|
$result = $this->repository->register('Alice', 'entwickler');
|
|
|
|
$this->expectException(InvalidArgumentException::class);
|
|
|
|
$this->repository->updateActivity((int) $result['agent']['id'], 'working', 999);
|
|
}
|
|
|
|
public function testUpdateActivityThrowsForUnknownAgent(): void
|
|
{
|
|
$this->expectException(NotFoundException::class);
|
|
|
|
$this->repository->updateActivity(999, 'working', null);
|
|
}
|
|
|
|
public function testAllListsAgentsWithoutApiKeyHash(): void
|
|
{
|
|
$this->repository->register('Alice', 'entwickler');
|
|
$this->repository->register('Bob', 'qa');
|
|
|
|
$all = $this->repository->all();
|
|
|
|
self::assertCount(2, $all);
|
|
foreach ($all as $agent) {
|
|
self::assertArrayNotHasKey('api_key_hash', $agent);
|
|
}
|
|
}
|
|
|
|
public function testFingerprintChangesWhenAnAgentIsRegistered(): void
|
|
{
|
|
$before = $this->repository->fingerprint();
|
|
$this->repository->register('Alice', 'entwickler');
|
|
|
|
self::assertNotSame($before, $this->repository->fingerprint());
|
|
}
|
|
|
|
public function testRegisterAndUpdateActivityReportFreshAgentAsPresent(): void
|
|
{
|
|
$result = $this->repository->register('Alice', 'entwickler');
|
|
self::assertTrue($result['agent']['is_present']);
|
|
|
|
$updated = $this->repository->updateActivity((int) $result['agent']['id'], 'working', null);
|
|
self::assertTrue($updated['is_present']);
|
|
}
|
|
|
|
public function testFindByIdReportsAgentAsAbsentAfterPresenceTimeout(): void
|
|
{
|
|
$result = $this->repository->register('Alice', 'entwickler');
|
|
$id = (int) $result['agent']['id'];
|
|
|
|
$staleTimestamp = gmdate('Y-m-d\TH:i:s\Z', time() - 601);
|
|
$this->db->pdo()->prepare('UPDATE agents SET last_seen_at = :ts WHERE id = :id')
|
|
->execute(['ts' => $staleTimestamp, 'id' => $id]);
|
|
|
|
$agent = $this->repository->findById($id);
|
|
self::assertFalse($agent['is_present']);
|
|
}
|
|
|
|
public function testTouchRefreshesPresenceWithoutChangingState(): void
|
|
{
|
|
$result = $this->repository->register('OPs', 'ops');
|
|
$id = (int) $result['agent']['id'];
|
|
$this->repository->updateActivity($id, 'auth_expiring', null);
|
|
|
|
$staleTimestamp = gmdate('Y-m-d\TH:i:s\Z', time() - 601);
|
|
$this->db->pdo()->prepare('UPDATE agents SET last_seen_at = :ts WHERE id = :id')
|
|
->execute(['ts' => $staleTimestamp, 'id' => $id]);
|
|
|
|
$this->repository->touch($id);
|
|
|
|
$agent = $this->repository->findById($id);
|
|
self::assertTrue($agent['is_present']);
|
|
self::assertSame('auth_expiring', $agent['state']);
|
|
}
|
|
}
|