Files
PacketStation/tests/server/test_cli.py
T
spencermandClaude Fable 5.1 0b663bb2b2 Phase 0: repo skeleton, image, compose stacks, doctor, migration runner, frontend shell
New repo for the Packet Station rebuild (Python 3.12, FastAPI, PostgreSQL 16,
React, VisualKit, MFilesKit, Docker Compose on docker-1). Nothing is migrated
from the M: install.

- CLAUDE.md with the safety rules, README, CHANGELOG, docs (BRIEF, PLAN,
  SECURITY rewritten for the server with the requirement ID table, DEPLOY,
  stubs for FEED, POLICY, SCHEMA, GO-LIVE, PAPER-FALLBACK).
- packetstation CLI: app, worker (heartbeat stub), migrate, doctor, version,
  healthcheck. Settings from the environment with secret files. Stack guard
  (test never points at VISUAL production). Security headers and CSP.
  Trusted proxy resolved by name. JSON logs with redaction.
- Migration runner and 0001_base: settings, append-only audit_events with
  hash chain, ps_notify, grants for ps_app.
- Multi-stage Dockerfile (ODBC Driver 18, PESCO libraries from the Gitea
  index only), compose base, proxy overlay for npm_default, CI overlay,
  postgres initdb roles, env examples, NPM proxy host notes, Gitea Actions.
- Frontend: Vite, React, TypeScript, the PESCO dark theme ported with
  self-hosted fonts, placeholder sign-in and home.
- Verified on docker-1: image builds, CI stack comes up healthy, doctor
  database checks pass, 77 server tests pass against PostgreSQL.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
2026-10-15 23:08:47 -06:00

54 lines
1.7 KiB
Python

from __future__ import annotations
import os
import threading
import time
from pathlib import Path
import pytest
from packetstation import cli, worker
def test_version(capsys: pytest.CaptureFixture[str], monkeypatch: pytest.MonkeyPatch) -> None:
monkeypatch.setenv("PS_BUILD", "abc123")
assert cli.main(["version"]) == 0
assert "build abc123" in capsys.readouterr().out
def test_worker_healthcheck(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
beat = tmp_path / "beat"
monkeypatch.setenv("WORKER_BEAT_FILE", str(beat))
assert cli.main(["healthcheck", "worker"]) == 1
worker.touch(beat)
assert cli.main(["healthcheck", "worker"]) == 0
old = time.time() - 120
os.utime(beat, (old, old))
assert cli.main(["healthcheck", "worker"]) == 1
def test_app_healthcheck_down() -> None:
# Nothing listens on 8000 in tests (or something unrelated does): either way not a crash
assert cli.main(["healthcheck", "app"]) in (0, 1)
def test_worker_loop_touches_and_stops(tmp_path: Path) -> None:
beat = tmp_path / "sub" / "beat"
stop = threading.Event()
t = threading.Thread(target=worker.run, args=(beat, "t"), kwargs={"stop": stop, "interval": 0.05})
t.start()
deadline = time.time() + 5
while not beat.exists() and time.time() < deadline:
time.sleep(0.01)
stop.set()
t.join(5)
assert beat.exists() and not t.is_alive()
def test_migrate_reports_bad_secret(
tmp_path: Path, monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str]
) -> None:
monkeypatch.setenv("PG_MIGRATE_PASSWORD_FILE", str(tmp_path / "missing"))
assert cli.main(["migrate"]) == 2
assert "PG_MIGRATE_PASSWORD_FILE" in capsys.readouterr().err