앞선 감사 네 번은 "이 코드가 제대로 도는가"를 물었다. 이번엔 작업자·노조·안전관리자·
공장장·설치기사 5인 관점으로 3단논법을 세우고 전제를 코드로 확인한 뒤 반문으로 깼다.
5인 중 2인의 논증이 완전히, 2인이 부분적으로 무너졌다.
읽기 감사 — 이 시스템은 쓰기를 전부 감사했다. 임계값 한 번 바꾼 것까지 해시 체인에
들어간다. 그런데 작업자 사진을 열어보는 것은 하나도 남지 않았다. 감시 시스템과 안전
시스템을 가르는 선이 정확히 거기라서, 나머지 프라이버시 설계(RAM 링버퍼 7.5초·전송 전
얼굴 블러·라이브뷰 부재)가 전부 이 한 줄이 없어서 검증 불가능한 주장에 머물러 있었다.
거부된 열람은 기록하지 않는다 — 그러면 감사 로그가 "본 사람"이 아니라 "요청한 사람"
목록이 되어 정작 누가 봤는지를 묻는 데 못 쓴다.
크론 생존 — 죽은 노드는 스스로 신고할 수 없어서 크론이 하트비트의 부재로 판정한다.
그런데 그 크론이 멈추면 아무도 판정하지 않는다. 통보·5분 확대·장치 이상 발행이 전부 그
한 프로세스에 걸려 있어서, 멈춘 순간부터 화면은 조용해지고 그 침묵은 "사고 없음"과
구별되지 않는다. 감시자의 생존은 감시자가 아닌 쪽이 읽어야 한다.
증거 보존 — 스냅샷 삭제가 나이 하나만 봤다. 기본 7일인데 중대재해 조사는 몇 주에서 몇
달이라, 우리가 내건 목적("사고 후 원인 분석")이 8일째에 스스로 사라졌다. 확인 처리로
자동 연장하지 않는 이유는 자동이면 사실상 전량 보존이 되어 7일 약속이 무너지기 때문이다.
증거 정책 — 프레임이 ergo_report 하나만 빼고 모든 이벤트에 붙었다. 체류 상한 초과도
포함이라, 밀폐공간 프리셋 기본값(30분) 기준으로 정상 작업자가 규정대로 일하는 동안
주기적으로 본인 사진이 서버로 갔다. 위험해서가 아니라 오래 있어서. 기준을 명문화했다:
시간 경과 자체가 조건인 이벤트에는 사진을 붙이지 않는다.
마이그레이션 2026-09-25-evidence-hold-and-view-audit.sql (README-UPLOAD 10번).
검증: phpunit 302 passed(신규 18, 스킵 0) · pytest(신규 18)
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01EBjuR7DW6PNNMows4KnbYU
167 lines
7.3 KiB
PHP
167 lines
7.3 KiB
PHP
<?php
|
|
declare(strict_types=1);
|
|
namespace App\Tests;
|
|
|
|
use App\Auth\ArraySessionStore;
|
|
use App\Dashboard\{DeviceStatusController, EventDetailController};
|
|
use App\Http\Request;
|
|
use App\Repository\AppStateRepository;
|
|
use App\Security\Csrf;
|
|
use App\Tests\Support\{InMemoryDeviceRepository, InMemoryEventRepository,
|
|
InMemorySnapshotRepository, InMemoryAuditRepository};
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
/**
|
|
* 감시자의 생존, 그리고 증거 보존 보류의 권한·기록.
|
|
*
|
|
* 죽은 노드는 스스로 신고할 수 없어서 크론이 하트비트의 부재로 판정한다. 그런데 그 크론이
|
|
* 멈추면 아무도 판정하지 않는다 — 통보·5분 미확인 확대·장치 이상 발행이 전부 그 한
|
|
* 프로세스에 걸려 있어서, 멈춘 순간부터 화면은 조용해지고 그 침묵은 "사고 없음"과 똑같이
|
|
* 보인다. 그래서 크론이 자기 실행 시각을 남기고, 크론이 아닌 쪽(대시보드)이 그 나이를 읽는다.
|
|
*/
|
|
final class DispatcherLivenessTest extends TestCase
|
|
{
|
|
private function state(?int $lastRun): AppStateRepository
|
|
{
|
|
return new class($lastRun) implements AppStateRepository {
|
|
public function __construct(private ?int $lastRun) {}
|
|
public function get(string $key): ?array
|
|
{
|
|
if ($key !== 'dispatch_alerts.last_run' || $this->lastRun === null) return null;
|
|
return ['value' => (string)$this->lastRun, 'updated_at' => $this->lastRun];
|
|
}
|
|
public function set(string $key, string $value, int $now): void {}
|
|
};
|
|
}
|
|
|
|
private function dispatcherBlock(?AppStateRepository $state): array
|
|
{
|
|
$sess = new ArraySessionStore();
|
|
$sess->set('user_id', 1);
|
|
$sess->set('site_scope', null);
|
|
$ctl = new DeviceStatusController(new InMemoryDeviceRepository(), $sess, null, null, $state);
|
|
$res = $ctl->json(new Request('GET', '/api/devices', [], '', []), 10_000);
|
|
return json_decode($res->body(), true)['dispatcher'];
|
|
}
|
|
|
|
public function test_a_recent_run_reads_as_alive(): void
|
|
{
|
|
$d = $this->dispatcherBlock($this->state(10_000 - 30));
|
|
$this->assertFalse($d['stale']);
|
|
$this->assertSame(30, $d['seconds_since']);
|
|
}
|
|
|
|
public function test_a_stopped_cron_is_visible(): void
|
|
{
|
|
// 크론이 5분 넘게 안 돌았다 = 통보가 통째로 멈춘 상태. 화면이 조용한 것이
|
|
// "사고 없음"이라는 뜻이 아니게 되는 지점이고, 그걸 사람이 알아야 한다.
|
|
$d = $this->dispatcherBlock($this->state(10_000 - 600));
|
|
$this->assertTrue($d['stale']);
|
|
$this->assertSame(600, $d['seconds_since']);
|
|
}
|
|
|
|
public function test_never_run_is_distinguishable_from_stopped(): void
|
|
{
|
|
// 조치가 다르다 — 전자는 크론을 등록하는 것이고 후자는 왜 멈췄는지 보는 것이다.
|
|
$d = $this->dispatcherBlock($this->state(null));
|
|
$this->assertTrue($d['stale']);
|
|
$this->assertNull($d['last_run_at']);
|
|
$this->assertNull($d['seconds_since']);
|
|
}
|
|
|
|
public function test_a_missing_state_table_does_not_break_the_status_panel(): void
|
|
{
|
|
// 마이그레이션 전 배포에서 상태 패널 전체가 죽으면, 이 기능을 넣은 대가로 기존
|
|
// 장치 감시를 잃는다. 그 경우 "한 번도 안 돌았다"로 보이고, 그게 실제로 맞다.
|
|
$throwing = new class implements AppStateRepository {
|
|
public function get(string $key): ?array { throw new \RuntimeException('no such table'); }
|
|
public function set(string $key, string $value, int $now): void {}
|
|
};
|
|
$d = $this->dispatcherBlock($throwing);
|
|
$this->assertTrue($d['stale']);
|
|
$this->assertNull($d['last_run_at']);
|
|
}
|
|
|
|
public function test_the_panel_still_works_without_the_repository(): void
|
|
{
|
|
$d = $this->dispatcherBlock(null);
|
|
$this->assertTrue($d['stale']);
|
|
}
|
|
|
|
// --- 증거 보존 보류 --------------------------------------------------------------
|
|
|
|
/** @return array{0:EventDetailController,1:InMemoryEventRepository,2:InMemoryAuditRepository,3:ArraySessionStore,4:int} */
|
|
private function detail(string $role): array
|
|
{
|
|
$events = new InMemoryEventRepository();
|
|
$id = $events->insert(['device_id' => 'pi-01', 'camera_id' => 'cam-01',
|
|
'event_type' => 'fall', 'severity' => 'high', 'occurred_at' => 1000.0, 'site' => null]);
|
|
$audit = new InMemoryAuditRepository();
|
|
$sess = new ArraySessionStore();
|
|
$sess->set('user_id', 1); $sess->set('username', '관리자');
|
|
$sess->set('role', $role); $sess->set('site_scope', null);
|
|
$sess->set('csrf', 'tok');
|
|
return [new EventDetailController($events, new InMemorySnapshotRepository(), $audit, $sess),
|
|
$events, $audit, $sess, $id];
|
|
}
|
|
|
|
private function holdReq(mixed $days): Request
|
|
{
|
|
return new Request('POST', '/events/1/hold', ['X-Csrf-Token' => 'tok'],
|
|
json_encode(['days' => $days]), []);
|
|
}
|
|
|
|
public function test_an_admin_can_hold_evidence_and_it_is_recorded(): void
|
|
{
|
|
[$ctl, $events, $audit, , $id] = $this->detail('admin');
|
|
|
|
$res = $ctl->hold($this->holdReq(90), ['id' => $id]);
|
|
|
|
$this->assertSame(200, $res->status());
|
|
$this->assertNotNull($events->rows[$id]['hold_until']);
|
|
$this->assertSame('evidence_hold', $audit->log[0]['act'],
|
|
'보존기간 연장이 기록되지 않으면, 노조에 "사고 건만 기록과 함께 남긴다"고 말할 근거가 없습니다.');
|
|
}
|
|
|
|
public function test_releasing_a_hold_is_recorded_separately(): void
|
|
{
|
|
[$ctl, $events, $audit, , $id] = $this->detail('admin');
|
|
$ctl->hold($this->holdReq(90), ['id' => $id]);
|
|
$res = $ctl->hold($this->holdReq(0), ['id' => $id]);
|
|
|
|
$this->assertSame(200, $res->status());
|
|
$this->assertNull($events->rows[$id]['hold_until']);
|
|
$this->assertSame('evidence_hold_release', $audit->log[1]['act']);
|
|
}
|
|
|
|
public function test_an_operator_cannot_extend_retention(): void
|
|
{
|
|
// 보존기간 연장은 운영 편의가 아니라 규정 판단이다.
|
|
[$ctl, $events, $audit, , $id] = $this->detail('operator');
|
|
|
|
$this->assertSame(403, $ctl->hold($this->holdReq(90), ['id' => $id])->status());
|
|
$this->assertNull($events->rows[$id]['hold_until'] ?? null);
|
|
$this->assertSame([], $audit->log);
|
|
}
|
|
|
|
public function test_an_unbounded_hold_is_refused(): void
|
|
{
|
|
// 무기한 보류는 프라이버시 약속의 예외가 아니라 폐기다.
|
|
[$ctl, $events, , , $id] = $this->detail('admin');
|
|
|
|
foreach ([400, -1, 'forever', null] as $bad) {
|
|
$this->assertSame(400, $ctl->hold($this->holdReq($bad), ['id' => $id])->status(),
|
|
'days=' . var_export($bad, true) . ' 를 통과시켰습니다');
|
|
}
|
|
$this->assertNull($events->rows[$id]['hold_until'] ?? null);
|
|
}
|
|
|
|
public function test_a_hold_without_csrf_is_refused(): void
|
|
{
|
|
[$ctl, , $audit, , $id] = $this->detail('admin');
|
|
$req = new Request('POST', '/events/1/hold', [], json_encode(['days' => 90]), []);
|
|
|
|
$this->assertSame(403, $ctl->hold($req, ['id' => $id])->status());
|
|
$this->assertSame([], $audit->log);
|
|
}
|
|
}
|