87 lines
3.3 KiB
Python
87 lines
3.3 KiB
Python
import hashlib
|
|
import json
|
|
import sqlite3
|
|
|
|
import pytest
|
|
|
|
from dx_productivity.dsh.scheduling import ScheduleStore
|
|
|
|
|
|
def populate(root, count=6):
|
|
store = ScheduleStore(root)
|
|
with store._connect() as db:
|
|
for index in range(count):
|
|
store._event(db, 'a' * 32, '2026-10-31T12:00:00Z', 'result', 'b' * 32,
|
|
{'status': 'completed', 'index': index})
|
|
return store
|
|
|
|
|
|
def test_archive_bounds_hot_events_without_losing_original_records(tmp_path):
|
|
store = populate(tmp_path)
|
|
before = store.events()
|
|
first = store.archive_events(keep=2, batch=2)
|
|
assert first['archived'] == 2
|
|
assert len(store.events()) == 4
|
|
store.archive_events(keep=2, batch=2)
|
|
summary = ScheduleStore(tmp_path).audit_status(keep=2)
|
|
assert summary['live_events'] == 2 and summary['archived_events'] == 4
|
|
archived = []
|
|
for path in sorted((tmp_path / 'dsh/schedule-audit').glob('*.jsonl')):
|
|
raw = path.read_bytes()
|
|
assert path.stem.endswith(hashlib.sha256(raw).hexdigest())
|
|
archived.extend(json.loads(line) for line in raw.splitlines())
|
|
assert sorted(archived + store.events(), key=lambda row: row['sequence']) == list(reversed(before))
|
|
assert store.archive_events(keep=2)['archived'] == 0
|
|
|
|
|
|
def test_failed_archive_export_never_deletes_audit_events(tmp_path, monkeypatch):
|
|
from dx_productivity.dsh import schedule_audit
|
|
store = populate(tmp_path)
|
|
before = store.events()
|
|
def unavailable(*args):
|
|
raise OSError('controlled export failure')
|
|
monkeypatch.setattr(schedule_audit, '_write_archive', unavailable)
|
|
with pytest.raises(OSError):
|
|
store.archive_events(keep=2)
|
|
assert store.events() == before
|
|
|
|
|
|
def test_database_failure_after_archive_is_safe_and_retryable(tmp_path):
|
|
store = populate(tmp_path)
|
|
before = store.events()
|
|
with sqlite3.connect(store.path) as db:
|
|
db.execute("CREATE TRIGGER reject_trim BEFORE DELETE ON schedule_events BEGIN SELECT RAISE(ABORT, 'controlled fault'); END")
|
|
with pytest.raises(sqlite3.IntegrityError):
|
|
store.archive_events(keep=2)
|
|
assert store.events() == before
|
|
files = list((tmp_path / 'dsh/schedule-audit').glob('*.jsonl'))
|
|
assert len(files) == 1
|
|
raw = files[0].read_bytes()
|
|
with sqlite3.connect(store.path) as db:
|
|
db.execute('DROP TRIGGER reject_trim')
|
|
store.archive_events(keep=2)
|
|
assert files[0].read_bytes() == raw
|
|
assert store.audit_status(keep=2)['archived_events'] == 4
|
|
|
|
|
|
@pytest.mark.parametrize('damage', ['missing', 'changed'])
|
|
def test_missing_or_changed_committed_archive_is_never_reported_healthy(tmp_path, damage):
|
|
store = populate(tmp_path)
|
|
store.audit_status(keep=2)
|
|
archive = next((tmp_path / 'dsh/schedule-audit').glob('*.jsonl'))
|
|
if damage == 'missing':
|
|
archive.unlink()
|
|
else:
|
|
archive.write_bytes(b'corrupted evidence\n')
|
|
with pytest.raises(OSError):
|
|
store.audit_status(keep=2)
|
|
|
|
|
|
def test_automatic_rotation_batches_overflow_instead_of_creating_one_file_per_event(tmp_path):
|
|
store = populate(tmp_path, count=3)
|
|
assert store.audit_status(keep=2)['archived_events'] == 0
|
|
with store._connect() as db:
|
|
store._event(db, 'a' * 32, '2026-10-31T12:00:00Z', 'result', None, {'status': 'completed'})
|
|
status = store.audit_status(keep=2)
|
|
assert status['live_events'] == 2 and status['archived_events'] == 2
|