jobbsok/tests/test_helpers_selfcheck.py

88 lines
3.5 KiB
Python

"""The test harness tests itself (plan Step 6).
Everything downstream of here trusts these helpers, so a golden comparator
that silently passes, or a pristine-fixture guard that never trips, would not
announce itself -- it would just make the rest of the suite quietly weaker.
The four tests below are the ones that would catch that: each asserts the
helper can FAIL, not merely that it can pass.
`assert_golden` takes its update flag as an argument rather than reading the
pytest config itself, so the rewrite path is reachable from a test without
re-running pytest inside pytest. The flag wiring is asserted separately, on
the registered option.
Style note: this file follows tests/test_jsonl.py.
"""
import os
import pytest
from helpers import golden, workspace
def test_assert_golden_passes_unchanged_and_fails_on_a_change(tmp_path):
reference = str(tmp_path / "dagens.txt")
with open(reference, "w", encoding="utf-8") as handle:
handle.write("Tromsø: 2 saker\n")
golden.assert_golden(reference, "Tromsø: 2 saker\n")
with pytest.raises(AssertionError) as caught:
golden.assert_golden(reference, "Tromsø: 3 saker\n")
# The message has to carry the diff; "golden mismatch" alone sends the
# reader back to the file to work out what moved.
assert "3 saker" in str(caught.value)
def test_update_golden_rewrites_the_file_and_the_flag_is_registered(tmp_path, request):
reference = str(tmp_path / "dagens.txt")
with open(reference, "w", encoding="utf-8") as handle:
handle.write("gammelt\n")
golden.assert_golden(reference, "nytt\n", update=True)
assert open(reference, encoding="utf-8").read() == "nytt\n"
# Regeneration is deliberate: off unless the operator asks for it.
assert request.config.getoption("--update-golden") is False
def test_make_case_produces_a_case_that_read_jsonl_can_read(empty_workspace, frozen_today):
sak_id = workspace.make_case(
empty_workspace,
arbeidsgiver="Fjordtek AS",
rolle="Rådgiver",
year_month="2026-09",
events=[workspace.event("opprettet", days_ago=3), workspace.event("soknad_sendt", days_ago=1)],
)
assert sak_id == "2026-09-fjordtek-as-radgiver"
log = os.path.join(empty_workspace, "saker", sak_id, "logg.jsonl")
records = workspace.read_jsonl(log)
assert [rec["hendelse"] for rec in records] == ["opprettet", "soknad_sendt"]
meta, _ = workspace.frontmatter(os.path.join(empty_workspace, "saker", sak_id, "sak.md"))
assert meta["sak_id"] == sak_id
assert meta["arbeidsgiver"] == "Fjordtek AS"
assert frozen_today == "2026-09-15"
def test_the_pristine_guard_trips_when_the_corpus_is_written_to(tmp_path):
corpus = tmp_path / "fixtures"
corpus.mkdir()
(corpus / "kandidat.md").write_text("opprinnelig\n", encoding="utf-8")
before = workspace.snapshot_tree(str(corpus))
workspace.assert_tree_unchanged(str(corpus), before) # nothing moved yet
(corpus / "kandidat.md").write_text("endret av en test\n", encoding="utf-8")
with pytest.raises(AssertionError) as caught:
workspace.assert_tree_unchanged(str(corpus), before)
assert "kandidat.md" in str(caught.value)
# A new file is a mutation too -- a test that drops output into the corpus
# poisons every later run just as surely as one that edits a fixture.
restored = workspace.snapshot_tree(str(corpus))
(corpus / "uventet.txt").write_text("x\n", encoding="utf-8")
with pytest.raises(AssertionError):
workspace.assert_tree_unchanged(str(corpus), restored)