83 lines
2.4 KiB
Python
83 lines
2.4 KiB
Python
import json
|
|
from datetime import datetime
|
|
|
|
import pytest
|
|
|
|
from scripts import fleet_milestones as fm
|
|
|
|
|
|
class FixedDateTime:
|
|
@classmethod
|
|
def utcnow(cls):
|
|
return datetime(2026, 4, 15, 1, 2, 3)
|
|
|
|
|
|
def test_trigger_persists_state_and_log(tmp_path, monkeypatch, capsys):
|
|
state_file = tmp_path / "milestones.json"
|
|
log_file = tmp_path / "fleet_milestones.log"
|
|
|
|
monkeypatch.setattr(fm, "STATE_FILE", state_file)
|
|
monkeypatch.setattr(fm, "LOG_FILE", log_file)
|
|
monkeypatch.setattr(fm, "datetime", FixedDateTime)
|
|
|
|
fm.trigger("health_check_first_run")
|
|
|
|
saved = json.loads(state_file.read_text())
|
|
assert saved["health_check_first_run"] == {
|
|
"triggered_at": "2026-04-15T01:02:03Z",
|
|
"phase": 1,
|
|
}
|
|
|
|
log_lines = log_file.read_text().strip().splitlines()
|
|
assert len(log_lines) == 1
|
|
assert "First automated health check ran" in log_lines[0]
|
|
|
|
captured = capsys.readouterr()
|
|
assert "MILESTONE" in captured.out
|
|
|
|
|
|
def test_trigger_dry_run_logs_without_persisting_state(tmp_path, monkeypatch):
|
|
state_file = tmp_path / "milestones.json"
|
|
log_file = tmp_path / "fleet_milestones.log"
|
|
|
|
monkeypatch.setattr(fm, "STATE_FILE", state_file)
|
|
monkeypatch.setattr(fm, "LOG_FILE", log_file)
|
|
monkeypatch.setattr(fm, "datetime", FixedDateTime)
|
|
|
|
fm.trigger("backup_first_success", dry_run=True)
|
|
|
|
assert not state_file.exists()
|
|
assert "First automated backup completed" in log_file.read_text()
|
|
|
|
|
|
def test_trigger_unknown_key_exits(monkeypatch):
|
|
monkeypatch.setattr(fm, "datetime", FixedDateTime)
|
|
with pytest.raises(SystemExit) as exc:
|
|
fm.trigger("not-a-real-milestone")
|
|
assert exc.value.code == 1
|
|
|
|
|
|
def test_trigger_is_idempotent_once_recorded(tmp_path, monkeypatch, capsys):
|
|
state_file = tmp_path / "milestones.json"
|
|
log_file = tmp_path / "fleet_milestones.log"
|
|
state_file.write_text(
|
|
json.dumps(
|
|
{
|
|
"health_check_first_run": {
|
|
"triggered_at": "2026-04-01T00:00:00Z",
|
|
"phase": 1,
|
|
}
|
|
}
|
|
)
|
|
)
|
|
|
|
monkeypatch.setattr(fm, "STATE_FILE", state_file)
|
|
monkeypatch.setattr(fm, "LOG_FILE", log_file)
|
|
monkeypatch.setattr(fm, "datetime", FixedDateTime)
|
|
|
|
fm.trigger("health_check_first_run")
|
|
|
|
assert not log_file.exists()
|
|
captured = capsys.readouterr()
|
|
assert "already triggered" in captured.out
|