feat(consume): read a concept, with adjudication absence as its own state

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
Kjell Tore Guttormsen 2026-09-07 09:08:41 +02:00
commit 1ec3f5289c
2 changed files with 205 additions and 0 deletions

View file

@ -21,15 +21,20 @@ house pattern rather than new inventions:
from __future__ import annotations
import hashlib
import os
import sys
from pathlib import Path
import pytest
PROJECT_ROOT = Path(__file__).resolve().parents[1]
sys.path.insert(0, str(PROJECT_ROOT / "tools"))
import okf_consume # noqa: E402
from llm_ingestion_okf.materialize import parse_frontmatter # noqa: E402
GOLDEN = PROJECT_ROOT / "examples" / "ingest-golden-segmented-okf-v0-2" / "expected-bundle"
@ -108,3 +113,81 @@ def test_the_ref_covers_the_indexes_too_since_the_walk_reads_them(tmp_path: Path
nested = copy / "krav" / "1-1" / "index.md"
nested.write_text(nested.read_text(encoding="utf-8") + "\nfritekst\n", encoding="utf-8")
assert okf_consume.bundle_ref(copy) != before
# --- Step 2: one concept, read into a record ----------------------------------
PROPOSED_CONCEPT = "krav/1-1/foerste-krav"
ROOT_BUNDLE_ID = "b-golden-segmented-okf-v0-2"
def _read(concept_id: str, root: Path = GOLDEN) -> okf_consume.Concept:
return okf_consume.read_concept(
root / f"{concept_id}.md", bundle_root=root, root_bundle_id=ROOT_BUNDLE_ID
)
def test_a_concept_carrying_adjudication_reads_that_value() -> None:
assert _read(PROPOSED_CONCEPT).adjudication == "proposed"
def test_a_concept_carrying_no_adjudication_key_reads_as_unknown(tmp_path: Path) -> None:
# SS 6.1: `unknown` is written EXPLICITLY. "Not judged" and "we cannot tell
# whether it was judged" are different facts, and only one is about the
# concept.
root = tmp_path / "bundle"
_copy_bundle(GOLDEN, root)
target = root / f"{PROPOSED_CONCEPT}.md"
target.write_text(
target.read_text(encoding="utf-8").replace("adjudication: proposed\n", ""),
encoding="utf-8",
)
concept = _read(PROPOSED_CONCEPT, root)
assert concept.adjudication == "unknown"
assert concept.adjudication_present is False
def test_an_adjudication_value_outside_the_wire_set_is_refused_by_name(tmp_path: Path) -> None:
# Mapping an unrecognised value to `unknown` would report "we cannot tell"
# where the truth is "the bundle said something this consumer does not
# understand" -- a defect laundered into a state.
root = tmp_path / "bundle"
_copy_bundle(GOLDEN, root)
target = root / f"{PROPOSED_CONCEPT}.md"
target.write_text(
target.read_text(encoding="utf-8").replace("adjudication: proposed", "adjudication: seen"),
encoding="utf-8",
)
with pytest.raises(okf_consume.ConsumeError) as raised:
_read(PROPOSED_CONCEPT, root)
assert raised.value.code == "adjudication_unknown_value"
def test_the_digest_is_of_the_concept_file_and_is_not_the_source_sha256() -> None:
concept = _read(PROPOSED_CONCEPT)
on_disk = hashlib.sha256((GOLDEN / f"{PROPOSED_CONCEPT}.md").read_bytes()).hexdigest()
assert concept.sha256 == on_disk
assert len(concept.sha256) == 64
frontmatter = parse_frontmatter(GOLDEN / f"{PROPOSED_CONCEPT}.md")
assert frontmatter["source_sha256"] != concept.sha256
def test_the_concept_id_keeps_its_slashes_where_import_slug_would_flatten_them() -> None:
# `importer.import_slug` flattens one line below the rule this id follows.
# A flattened id fails a document-prefix match in a way that looks like a
# ranking miss rather than an id-format bug.
assert _read(PROPOSED_CONCEPT).concept_id == "krav/1-1/foerste-krav"
def test_bundle_id_falls_back_to_the_root_index_and_says_that_it_did(tmp_path: Path) -> None:
root = tmp_path / "bundle"
_copy_bundle(GOLDEN, root)
target = root / f"{PROPOSED_CONCEPT}.md"
target.write_text(
target.read_text(encoding="utf-8").replace(f"bundle_id: {ROOT_BUNDLE_ID}\n", ""),
encoding="utf-8",
)
concept = _read(PROPOSED_CONCEPT, root)
assert concept.bundle_id == ROOT_BUNDLE_ID
assert concept.bundle_id_inherited is True
assert _read(PROPOSED_CONCEPT).bundle_id_inherited is False