testsAndMisc/python_pkg/session_autopsy/tests/test_store.py
Krzysztof kuhy Rudnicki a83b5abc27
Some checks are pending
Pre-commit checks / pre-commit (push) Waiting to run
Python tests / test (push) Waiting to run
Shell tests / shell-tests (push) Waiting to run
Shell tests / arch-tests (push) Waiting to run
python_pkg: track session_autopsy, finishing its interrupted refactor
The transcript analyzer behind /compile-candidate lived only in the working
tree. Untracked meant unlinted — pre-commit only inspects files handed to it —
so nothing had ever checked it, and it had been abandoned mid-refactor with
every parse raising AttributeError: '_Accumulator' object has no attribute
'first_timestamp'.

The accumulator had been moved onto the grouped records dataclasses, but three
increment sites and parse_session still read flat attributes that no longer
existed. parse_session now hands over acc.meta and acc.counts directly; only
subagent_count comes from elsewhere, since no transcript line carries it.

Two modules broke the 500-line limit, so they were split along seams that were
already there. signatures.py takes the text-signature helpers — traces.py was
importing them from parse.py, making parse an accidental hub, and now neither
imports the other. stats.py takes the pure number and time helpers. parse.py
521 -> 458, detectors.py 559 -> 467.

The structural lint findings were refactored, not configured away.
_counter_candidates took eight arguments; the five naming which counter field to
mine and at what thresholds became a frozen _CounterSpec, so both call sites are
one line. _extract_skill carried in_span and a line budget as loop variables
across thirteen branches; a _SpanBuffer owns that state now, leaving the walk at
four. record() took twelve flat kwargs and now takes one override per record
block, with session_meta() so a partial meta override keeps the standard
timestamp — a bare SessionMeta(slug=...) would have nulled started_at and
quietly changed the per-week arithmetic. All 36 call sites rewritten by an AST
pass. tests/factories.py moved into conftest.py, where every other package here
keeps shared builders and where name-tests-test does not object.

965 tests pass at 100% statement and branch coverage, every hook is green, and
no suppression was added anywhere. `candidates` and `traces skill-finish` both
run against the real corpus.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-07-25 16:56:43 +02:00

66 lines
2.0 KiB
Python

"""Tests for the flock-guarded sessions.jsonl store."""
from __future__ import annotations
import json
from typing import TYPE_CHECKING
from python_pkg.session_autopsy.records import ActivityCounts
from python_pkg.session_autopsy.store import (
SESSIONS_FILE,
StoreLock,
load_file_index,
load_records,
upsert_records,
)
from python_pkg.session_autopsy.tests.conftest import (
FileFacts,
record,
)
if TYPE_CHECKING:
from pathlib import Path
def test_lock_exit_without_enter(tmp_path: Path) -> None:
"""__exit__ with no held handle is a safe no-op."""
StoreLock(tmp_path).__exit__(None, None, None)
def test_load_records_missing_store(tmp_path: Path) -> None:
"""An absent sessions.jsonl reads as empty."""
assert load_records(tmp_path / "home") == []
def test_iter_skips_bad_lines(tmp_path: Path) -> None:
"""Blank, malformed, and non-dict store lines are skipped."""
home = tmp_path
good = record("keep")
(home / SESSIONS_FILE).write_text(
"\n".join(["", "{bad json", json.dumps([1, 2]), json.dumps(good.to_dict())])
+ "\n",
encoding="utf-8",
)
records = load_records(home)
assert [rec.session_id for rec in records] == ["keep"]
def test_upsert_inserts_and_replaces(tmp_path: Path) -> None:
"""Upsert keys by session id: new ids append, same ids replace."""
home = tmp_path / "home"
assert upsert_records(home, [record("a"), record("b")]) == 2
replacement = record("a", counts=ActivityCounts(assistant_msgs=99))
assert upsert_records(home, [replacement]) == 2
by_id = {rec.session_id: rec for rec in load_records(home)}
assert by_id["a"].counts.assistant_msgs == 99
assert by_id["b"].counts.assistant_msgs == 10
def test_load_file_index(tmp_path: Path) -> None:
"""The index maps transcript paths to (size, mtime)."""
home = tmp_path / "home"
upsert_records(
home, [record("a", file=FileFacts(path="/t/a.jsonl", size=5, mtime=2.5))]
)
assert load_file_index(home) == {"/t/a.jsonl": (5, 2.5)}