Retrofit for the conventions merged in #4 (issue #3): - OpLog: JSON-lines emitter with the fixed envelope (ts, agent, event, task_id, cause); task_id set by the orchestrator at unit-of-work start - StateStore owns emission: transition, intent begin/commit/abort, and schema migrations each emit their event right after the durable write commits; callers pass the cause - reconcile_on_boot emits boot.reconcile with action and verdicts, and resolves intents with reconciliation-verdict causes - Failed resolutions emit nothing (a line exists iff the write committed) Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
121 lines
4.0 KiB
Python
121 lines
4.0 KiB
Python
import sqlite3
|
|
from pathlib import Path
|
|
|
|
import pytest
|
|
|
|
from nisse_dev.state import StateSchemaError, StateStore
|
|
|
|
|
|
@pytest.fixture
|
|
def db(tmp_path: Path) -> Path:
|
|
return tmp_path / "state.db"
|
|
|
|
|
|
def test_wal_mode_and_full_sync(db: Path) -> None:
|
|
with StateStore(db) as store:
|
|
journal = store._conn.execute("PRAGMA journal_mode").fetchone()[0]
|
|
sync = store._conn.execute("PRAGMA synchronous").fetchone()[0]
|
|
assert journal == "wal"
|
|
assert sync == 2 # FULL
|
|
|
|
|
|
def test_fresh_store_stamps_schema_version(db: Path) -> None:
|
|
with StateStore(db, schema_version=1) as store:
|
|
assert store.schema_version == 1
|
|
|
|
|
|
def test_transition_persists_across_reopen(db: Path) -> None:
|
|
with StateStore(db) as store:
|
|
store.transition(
|
|
"EXECUTING", cause="test", project="demo", data={"milestone": 2}
|
|
)
|
|
with StateStore(db) as store:
|
|
state = store.get_state()
|
|
assert state is not None
|
|
assert state.state == "EXECUTING"
|
|
assert state.project == "demo"
|
|
assert state.data == {"milestone": 2}
|
|
|
|
|
|
def test_get_state_on_fresh_store_is_none(db: Path) -> None:
|
|
with StateStore(db) as store:
|
|
assert store.get_state() is None
|
|
|
|
|
|
def test_intent_lifecycle(db: Path) -> None:
|
|
with StateStore(db) as store:
|
|
intent_id = store.begin_intent("push_branch", {"branch": "m2"}, cause="test")
|
|
[pending] = store.pending_intents()
|
|
assert pending.id == intent_id
|
|
assert pending.kind == "push_branch"
|
|
assert pending.payload == {"branch": "m2"}
|
|
store.commit_intent(intent_id, cause="test")
|
|
assert store.pending_intents() == []
|
|
|
|
|
|
def test_pending_intent_survives_reopen(db: Path) -> None:
|
|
with StateStore(db) as store:
|
|
store.begin_intent("open_pr", {"title": "hello"}, cause="test")
|
|
with StateStore(db) as store:
|
|
[pending] = store.pending_intents()
|
|
assert pending.kind == "open_pr"
|
|
|
|
|
|
def test_aborted_intent_is_not_pending(db: Path) -> None:
|
|
with StateStore(db) as store:
|
|
intent_id = store.begin_intent("push_branch", {}, cause="test")
|
|
store.abort_intent(intent_id, cause="test")
|
|
assert store.pending_intents() == []
|
|
|
|
|
|
def test_resolving_nonpending_intent_raises(db: Path) -> None:
|
|
with StateStore(db) as store:
|
|
intent_id = store.begin_intent("push_branch", {}, cause="test")
|
|
store.commit_intent(intent_id, cause="test")
|
|
with pytest.raises(KeyError):
|
|
store.commit_intent(intent_id, cause="test")
|
|
with pytest.raises(KeyError):
|
|
store.abort_intent(9999, cause="test")
|
|
|
|
|
|
def test_migration_runs_and_bumps_version(db: Path) -> None:
|
|
with StateStore(db, schema_version=1):
|
|
pass
|
|
|
|
def add_notes_table(conn: sqlite3.Connection) -> None:
|
|
conn.execute("CREATE TABLE notes (id INTEGER PRIMARY KEY, body TEXT)")
|
|
|
|
with StateStore(db, schema_version=2, migrations={1: add_notes_table}) as store:
|
|
assert store.schema_version == 2
|
|
store._conn.execute("SELECT * FROM notes")
|
|
|
|
|
|
def test_missing_migration_raises(db: Path) -> None:
|
|
with StateStore(db, schema_version=1):
|
|
pass
|
|
with pytest.raises(StateSchemaError, match="no migration"):
|
|
StateStore(db, schema_version=2, migrations={})
|
|
|
|
|
|
def test_failed_migration_leaves_version_unchanged(db: Path) -> None:
|
|
with StateStore(db, schema_version=1):
|
|
pass
|
|
|
|
def broken(conn: sqlite3.Connection) -> None:
|
|
conn.execute("CREATE TABLE notes (id INTEGER PRIMARY KEY)")
|
|
raise RuntimeError("boom")
|
|
|
|
with pytest.raises(RuntimeError, match="boom"):
|
|
StateStore(db, schema_version=2, migrations={1: broken})
|
|
with StateStore(db, schema_version=1) as store:
|
|
assert store.schema_version == 1
|
|
with pytest.raises(sqlite3.OperationalError):
|
|
store._conn.execute("SELECT * FROM notes")
|
|
|
|
|
|
def test_newer_schema_is_refused(db: Path) -> None:
|
|
with StateStore(db, schema_version=3):
|
|
pass
|
|
with pytest.raises(StateSchemaError, match="newer"):
|
|
StateStore(db, schema_version=1)
|