monitor/api/tests/Unit/Repository/SystemRepositoryTest.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

81 lines
2.6 KiB
PHP

<?php
declare(strict_types=1);
namespace Monitor\Api\Tests\Repository;
use InvalidArgumentException;
use Monitor\Api\Db\Database;
use Monitor\Api\Repository\SystemRepository;
use PHPUnit\Framework\TestCase;
final class SystemRepositoryTest extends TestCase
{
private SystemRepository $repository;
protected function setUp(): void
{
$this->repository = new SystemRepository(Database::inMemory());
}
public function testReportStatusInsertsSystem(): void
{
$system = $this->repository->reportStatus('srv1', ['status' => 'ok', 'name' => 'Server 1']);
self::assertSame('ok', $system['status']);
self::assertSame('api', $system['source']);
}
public function testReportStatusRejectsInvalidStatus(): void
{
$this->expectException(InvalidArgumentException::class);
$this->repository->reportStatus('srv1', ['status' => 'unknown']);
}
public function testReportStatusUpdatesExistingSystem(): void
{
$this->repository->reportStatus('srv1', ['status' => 'ok']);
$updated = $this->repository->reportStatus('srv1', ['status' => 'error', 'detail' => 'Timeout']);
self::assertSame('error', $updated['status']);
self::assertSame('Timeout', $updated['detail']);
}
public function testImportLegacyRowsDoesNotOverwriteApiSourcedSystem(): void
{
$this->repository->reportStatus('srv1', ['status' => 'ok']);
$this->repository->importLegacyRows([
['id' => 'srv1', 'name' => 'srv1', 'status' => 'error', 'detail' => '1.2.3.4', 'last_check_at' => '2026-01-01 00:00'],
]);
$system = $this->repository->find('srv1');
self::assertSame('ok', $system['status']);
self::assertSame('api', $system['source']);
}
public function testImportLegacyRowsCreatesNewLegacySystem(): void
{
$this->repository->importLegacyRows([
['id' => 'srv2', 'name' => 'srv2', 'status' => 'ok', 'detail' => '1.2.3.5', 'last_check_at' => '2026-01-01 00:00'],
]);
$system = $this->repository->find('srv2');
self::assertNotNull($system);
self::assertSame('legacy', $system['source']);
}
public function testAllOrdersErrorsFirstAndLimitsToNineTiles(): void
{
for ($i = 0; $i < 12; $i++) {
$this->repository->reportStatus('srv' . $i, ['status' => 'ok']);
}
$this->repository->reportStatus('broken', ['status' => 'error']);
$all = $this->repository->all();
self::assertCount(SystemRepository::MAX_TILES, $all);
self::assertSame('error', $all[0]['status']);
}
}