test_declaration.py¶
tests/test_declaration.py
A target repo's `.clew.yaml` declaration (task #51).
test_a_declared_doxyfile_is_reachable_and_a_missing_one_refuses
None test_declaration.test_a_declared_doxyfile_is_reachable_and_a_missing_one_refuses(Path tmp_path)
A declared Doxyfile is used; a declared path that is not there REFUSES.
Checks
assert SECTION_DOXYFILE in KNOWN_SECTIONS, "declarable, or the document-level gate refuses it"assert applied == [SECTION_DOXYFILE]assert Path(args.doxyfile) == doxydir / "mbedtls.doxyfile"assert "typo.doxyfile" in str(refusal.value)assert apply_options(ok, {"requirements": "nope.yaml"}, tmp_path) == ["requirements"]
Result: passed
Verifies
| requirement | linked | path to the implementer |
|---|---|---|
| REQ-DDB-CONFIG-001 | catalog |
Source · 48 lines
def test_a_declared_doxyfile_is_reachable_and_a_missing_one_refuses(tmp_path: Path) -> None:
"""gh#420. `discover_doxyfile` matches the NAME `Doxyfile` in the repo root, `docs/` and
`doc/` and refuses to guess further, because it was once caught selecting a test FIXTURE's
Doxyfile to index a whole project. Mbed-TLS ships `doxygen/mbedtls.doxyfile` — wrong
directory AND wrong filename — so it was unreachable from discovery, and until this section
existed there was no way to state it either. `--doxyfile` could name it on one command line,
which is the shape the mandate calls not-a-declaration: unrecorded, unreplayed, and
unreachable from the MCP surface.
BOTH HALVES, and the second is the fail-closed one. A stated path that is not a file must
refuse rather than fall through to whole-repo synthesis, because synthesis is a LEGITIMATE
build — so the result would be a well-formed index of a different thing, reporting success.
That is why `doxyfile` is in `PATH_OPTIONS_MUST_EXIST` while `requirements` is not: a missing
catalog degrades to empty metadata over the same rows, a missing Doxyfile changes which
source is compiled in.
"""
from types import SimpleNamespace
from clew.buildoptions import BuildOptionError, apply_options
from clew.declaration import SECTION_DOXYFILE
assert SECTION_DOXYFILE in KNOWN_SECTIONS, "declarable, or the document-level gate refuses it"
doxydir = tmp_path / "doxygen"
doxydir.mkdir()
(doxydir / "mbedtls.doxyfile").write_text(
"INPUT = ../include\nFILE_PATTERNS = *.h\n", encoding="utf-8"
)
## RESOLVED AGAINST THE REPO ROOT, not the process's cwd — an MCP server's cwd is not the
## target repo, and the declaration states the path an author would write in-tree.
args = SimpleNamespace()
applied = apply_options(args, {SECTION_DOXYFILE: "doxygen/mbedtls.doxyfile"}, tmp_path)
assert applied == [SECTION_DOXYFILE]
assert Path(args.doxyfile) == doxydir / "mbedtls.doxyfile"
## AND THE REFUSAL NAMES THE PATH. A typo'd declaration silently indexing the whole repo is
## the "wrong answer beats no answer" failure this whole section is shaped around.
with pytest.raises(BuildOptionError) as refusal:
apply_options(SimpleNamespace(), {SECTION_DOXYFILE: "doxygen/typo.doxyfile"}, tmp_path)
assert "typo.doxyfile" in str(refusal.value)
## THE CONTRAST THAT JUSTIFIES THE SET: a missing `requirements` catalog is NOT refused here,
## because each loader reports its own absent document with the context to say what it was for.
ok = SimpleNamespace()
assert apply_options(ok, {"requirements": "nope.yaml"}, tmp_path) == ["requirements"]
Activity · 16 steps
test_a_declared_event_tag_vocabulary_is_read_and_normalised
None test_declaration.test_a_declared_event_tag_vocabulary_is_read_and_normalised(Path tmp_path)
The accessor's happy path, and the reason the section exists: a repo whose bus is documented `@broadcasts`/`@reacts` had no way to say so, because `import_event_edges` took the vocabulary as a parameter no caller passed.
Checks
assert declared_event_tags(decl) == {"broadcasts": PRODUCER, "reacts": CONSUMER}assert declared_event_tags({}) is Noneassert declared_event_tags({"event_tags": {}}) is Noneassert declared_event_tags({"event_tags": []}) is None
Result: passed
Verifies
| requirement | linked | path to the implementer |
|---|---|---|
| REQ-DDB-CONFIG-001 | measured | test_a_declared_event_tag_vocabulary_is_read_and_normalised → declared_event_tags |
Source · 17 lines
def test_a_declared_event_tag_vocabulary_is_read_and_normalised(tmp_path: Path) -> None:
"""The accessor's happy path, and the reason the section exists: a repo whose
bus is documented `@broadcasts`/`@reacts` had no way to say so, because
`import_event_edges` took the vocabulary as a parameter no caller passed."""
from clew.declaration import declared_event_tags
from clew.event_edges import CONSUMER, PRODUCER
decl = {"event_tags": {"Broadcasts": "Producer", "reacts": "consumer"}}
assert declared_event_tags(decl) == {"broadcasts": PRODUCER, "reacts": CONSUMER}
# Undeclared is None, NOT {} — the importer's parameter distinguishes them, and
# only None means "keep the built-in verbs".
assert declared_event_tags({}) is None
assert declared_event_tags({"event_tags": {}}) is None
assert declared_event_tags({"event_tags": []}) is None
Activity · 7 steps
test_absent_declaration_is_not_an_error
None test_declaration.test_absent_declaration_is_not_an_error(Path tmp_path)
Most repos declare nothing and run entirely on built-in defaults, so an absent file is the norm rather than a failure.
Checks
assert load_declaration(tmp_path) == {}assert load_declaration(None) == {}
Result: passed
Verifies
| requirement | linked | path to the implementer |
|---|---|---|
| REQ-DDB-CONFIG-001 | measured | test_absent_declaration_is_not_an_error → load_declaration |
| REQ-DDB-GUARD-012 | inferred | test_absent_declaration_is_not_an_error → load_declaration → load_declaration_located → _checked_sections → _checked_section → _checked_requirements → _validate_node |
| REQ-DDB-WIKI-054 | inferred | test_absent_declaration_is_not_an_error → load_declaration → load_declaration_located → _checked_sections → _checked_section → _checked_list_section → all |
Source · 7 lines
def test_absent_declaration_is_not_an_error(tmp_path: Path) -> None:
"""Most repos declare nothing and run entirely on built-in defaults, so an
absent file is the norm rather than a failure."""
assert load_declaration(tmp_path) == {}
assert load_declaration(None) == {}
Activity · 2 steps
test_an_experimental_section_is_tolerated_and_ignored
test_declaration.TestForwardCompatibility.test_an_experimental_section_is_tolerated_and_ignored(self, tmp_path)
An `x-` section neither refuses nor reaches the build.
Checks
assert "thread_patterns" in data, "the known sections must still be read"assert not [key for key in data if key.startswith("x-")], (f"an experimental section reached the build, which is worse than refusing it: {data}" )
Result: passed
Verifies
| requirement | linked | path to the implementer |
|---|---|---|
| REQ-DDB-CONFIG-001 | measured | test_an_experimental_section_is_tolerated_and_ignored → load_declaration |
| REQ-DDB-GUARD-012 | inferred | test_an_experimental_section_is_tolerated_and_ignored → load_declaration → load_declaration_located → _checked_sections → _checked_section → _checked_requirements → _validate_node |
| REQ-DDB-WIKI-054 | inferred | test_an_experimental_section_is_tolerated_and_ignored → load_declaration → load_declaration_located → _checked_sections → _checked_section → _checked_list_section → all |
Source · 15 lines
def test_an_experimental_section_is_tolerated_and_ignored(self, tmp_path):
"""@brief An `x-` section neither refuses nor reaches the build. @version 2"""
## FIXTURE CHANGED (2.0.0, R5-CFG F1): `thread_patterns: [worker]` was a list, which no
## reader ever read and the loader now refuses; the property is the `x-` tolerance.
(tmp_path / ".clew.yaml").write_text(
"x-future-thing:\n whatever: 1\nthread_patterns:\n spawns:\n - {name: worker}\n"
)
data = load_declaration(tmp_path)
assert "thread_patterns" in data, "the known sections must still be read"
assert not [key for key in data if key.startswith("x-")], (
f"an experimental section reached the build, which is worse than refusing it: {data}"
)
Activity · 4 steps
test_an_unknown_event_role_is_refused
None test_declaration.test_an_unknown_event_role_is_refused(Path tmp_path)
FAIL CLOSED, one level below the section allow-list. Degrading a misspelled `producers:` to a default would file an emitter as a handler, and a REVERSED edge is worse than a missing one: these rows carry `declared=1` and `confidence='high'` because an author asserted them, so `chain_trace` would report causality flowing the wrong way with the strongest grading the pipeline can give.
Checks
assert "broadcasts" in message, "the refusal must name the offending tag"assert "producer" in message and "consumer" in message, "and the allowed roles"
Result: passed
Verifies
| requirement | linked | path to the implementer |
|---|---|---|
| REQ-DDB-CONFIG-001 | measured | test_an_unknown_event_role_is_refused → declared_event_tags |
Source · 18 lines
def test_an_unknown_event_role_is_refused(tmp_path: Path) -> None:
"""FAIL CLOSED, one level below the section allow-list. Degrading a misspelled
`producers:` to a default would file an emitter as a handler, and a REVERSED
edge is worse than a missing one: these rows carry `declared=1` and
`confidence='high'` because an author asserted them, so `chain_trace` would
report causality flowing the wrong way with the strongest grading the pipeline
can give."""
from clew.declaration import declared_event_tags
from clew.vocabulary import DeclarationError
with pytest.raises(DeclarationError) as exc:
declared_event_tags({"event_tags": {"broadcasts": "producers"}})
message = str(exc.value)
assert "broadcasts" in message, "the refusal must name the offending tag"
assert "producer" in message and "consumer" in message, "and the allowed roles"
Activity · 7 steps
test_an_unknown_section_beside_the_guard_section_is_still_refused
None test_declaration.test_an_unknown_section_beside_the_guard_section_is_still_refused(Path tmp_path)
Sharing the file must not loosen the allow-list: a misspelled section next to a valid `guard:` parses, nothing reads it, and the build would report the declaration honoured.
Result: passed
Verifies
| requirement | linked | path to the implementer |
|---|---|---|
| REQ-DDB-CONFIG-001 | measured | test_an_unknown_section_beside_the_guard_section_is_still_refused → load_declaration |
| REQ-DDB-GUARD-012 | inferred | test_an_unknown_section_beside_the_guard_section_is_still_refused → load_declaration → load_declaration_located → _checked_sections → _checked_section → _checked_requirements → _validate_node |
| REQ-DDB-WIKI-054 | inferred | test_an_unknown_section_beside_the_guard_section_is_still_refused → load_declaration → load_declaration_located → _checked_sections → _checked_section → _checked_list_section → all |
Source · 12 lines
def test_an_unknown_section_beside_the_guard_section_is_still_refused(tmp_path: Path) -> None:
"""Sharing the file must not loosen the allow-list: a misspelled section next to a valid
`guard:` parses, nothing reads it, and the build would report the declaration honoured."""
(tmp_path / ".clew.yaml").write_text(
"guard:\n output_dir: o/\nthread_pattern:\n spawns: []\n", encoding="utf-8"
)
from clew.vocabulary import DeclarationError
with pytest.raises(DeclarationError, match="unknown section"):
load_declaration(tmp_path)
Activity · 4 steps
test_an_unknown_section_is_refused
None test_declaration.test_an_unknown_section_is_refused(Path tmp_path)
The no-hardcoding mandate's worst case, closed at document level.
Checks
assert "shared_key_pattern" in message and "thread_pattern" in messageassert "shared_key_patterns" in message, "the refusal must name the ALLOWED spellings"
Result: passed
Verifies
| requirement | linked | path to the implementer |
|---|---|---|
| REQ-DDB-CONFIG-001 | measured | test_an_unknown_section_is_refused → load_declaration |
| REQ-DDB-GUARD-012 | inferred | test_an_unknown_section_is_refused → load_declaration → load_declaration_located → _checked_sections → _checked_section → _checked_requirements → _validate_node |
| REQ-DDB-WIKI-054 | inferred | test_an_unknown_section_is_refused → load_declaration → load_declaration_located → _checked_sections → _checked_section → _checked_list_section → all |
Source · 28 lines
def test_an_unknown_section_is_refused(tmp_path: Path) -> None:
"""The no-hardcoding mandate's worst case, closed at document level.
A singular/plural slip parses to valid YAML that NO consumer reads, so the
build ran entirely on built-in defaults — while `load_declaration` logged
"declares shared_key_pattern" and told the owner the file was honoured.
Substituting our assumptions for a declaration the author did write, and
reporting success, is precisely what the mandate forbids.
`dispatch.py::_reject_unknown` closed this one level down; this lifts it to the
document, which is where the quietest slips live."""
import pytest
from clew.declaration import load_declaration
from clew.vocabulary import DeclarationError
(tmp_path / ".clew.yaml").write_text(
"shared_key_pattern:\n - prefix: Store_Set\nthread_pattern:\n - name: spawn\n",
encoding="utf-8",
)
with pytest.raises(DeclarationError) as exc:
load_declaration(tmp_path)
message = str(exc.value)
assert "shared_key_pattern" in message and "thread_pattern" in message
assert "shared_key_patterns" in message, "the refusal must name the ALLOWED spellings"
Activity · 9 steps
test_an_x_prefix_does_not_excuse_a_misspelling
test_declaration.TestForwardCompatibility.test_an_x_prefix_does_not_excuse_a_misspelling(self, tmp_path, caplog)
A tolerated section is logged, not silent.
Checks
assert data == {}assert any("x-thread_patterns" in record.getMessage() for record in caplog.records), ("a tolerated section must still be named, or a typo with an `x-` prefix is silent" )
Result: passed
Verifies
| requirement | linked | path to the implementer |
|---|---|---|
| REQ-DDB-CONFIG-001 | measured | test_an_x_prefix_does_not_excuse_a_misspelling → load_declaration |
| REQ-DDB-GUARD-012 | inferred | test_an_x_prefix_does_not_excuse_a_misspelling → load_declaration → load_declaration_located → _checked_sections → _checked_section → _checked_requirements → _validate_node |
| REQ-DDB-WIKI-054 | inferred | test_an_x_prefix_does_not_excuse_a_misspelling → load_declaration → load_declaration_located → _checked_sections → _checked_section → _checked_list_section → all |
Source · 17 lines
def test_an_x_prefix_does_not_excuse_a_misspelling(self, tmp_path, caplog):
"""THE LIMIT. `x-thread_patterns` is not a tolerated experiment, it is a typo with a prefix —
but there is no way to tell those apart, and tolerating it silently is exactly the
failure mode the refusal exists to prevent. This asserts the rule stays narrow: the
prefix buys tolerance, and tolerance means the section is reported as ignored.
@brief A tolerated section is logged, not silent. @version 1
"""
(tmp_path / ".clew.yaml").write_text("x-thread_patterns:\n - worker\n")
with caplog.at_level(logging.WARNING, logger="clew.declaration"):
data = load_declaration(tmp_path)
assert data == {}
assert any("x-thread_patterns" in record.getMessage() for record in caplog.records), (
"a tolerated section must still be named, or a typo with an `x-` prefix is silent"
)1 more lines, not shown
Activity · 5 steps
test_build_meta_records_the_stated_declaration
None test_declaration.test_build_meta_records_the_stated_declaration(Path tmp_path)
The build records which document was stated, and records nothing when none was.
Checks
assert held["declaration.stated_sha256"] == "ab" * 32assert held["declaration.stated_sections"] == "locks"assert not [k for k in held2 if k.startswith("declaration.")], ("a build with no stated document must write NO declaration row — the harness gate reads " "absence as 'not stated', so a placeholder row would certify a policy nobody stated" )
Result: passed
Verifies
| requirement | linked | path to the implementer |
|---|---|---|
| REQ-DDB-CONFIG-001 | catalog | |
| REQ-DDB-CONFIG-006 | measured | test_build_meta_records_the_stated_declaration → write_build_signature |
| REQ-DDB-CONFIG-007 | measured | test_build_meta_records_the_stated_declaration → write_build_signature |
| REQ-DDB-PIPE-005 | measured | test_build_meta_records_the_stated_declaration → write_build_signature |
Source · 28 lines
def test_build_meta_records_the_stated_declaration(tmp_path: Path) -> None:
"""Both halves. A recorded document is what makes the harness gate possible; an ABSENT
section is what makes "no document was stated" honestly readable, rather than presenting as
a statement that was made and was blank.
"""
import sqlite3
from clew.signature import write_build_signature
db = tmp_path / "clew.db"
write_build_signature(db, declaration={"stated_sha256": "ab" * 32, "stated_sections": "locks"})
conn = sqlite3.connect(str(db))
held = dict(conn.execute("SELECT key, value FROM build_meta"))
conn.close()
assert held["declaration.stated_sha256"] == "ab" * 32
assert held["declaration.stated_sections"] == "locks"
db2 = tmp_path / "plain.db"
write_build_signature(db2)
conn = sqlite3.connect(str(db2))
held2 = dict(conn.execute("SELECT key, value FROM build_meta"))
conn.close()
assert not [k for k in held2 if k.startswith("declaration.")], (
"a build with no stated document must write NO declaration row — the harness gate reads "
"absence as 'not stated', so a placeholder row would certify a policy nobody stated"
)
Activity · 15 steps
test_declared_path_section_resolves_relative_to_the_repo
None test_declaration.test_declared_path_section_resolves_relative_to_the_repo(Path tmp_path)
Formats that are not YAML (the ingot data-model TOML) are NAMED by the section rather than inlined; the path stays repo-relative so the declaration is portable.
Checks
assert declared_path(decl, SECTION_DATA_MODEL, tmp_path) == (tmp_path / "model.toml").resolve()assert declared_path({SECTION_DATA_MODEL: "gone.toml"}, SECTION_DATA_MODEL, tmp_path) is Noneassert declared_path({}, SECTION_DATA_MODEL, tmp_path) is None
Result: passed
Verifies
| requirement | linked | path to the implementer |
|---|---|---|
| REQ-DDB-CONFIG-001 | measured | test_declared_path_section_resolves_relative_to_the_repo → declared_path |
Source · 13 lines
def test_declared_path_section_resolves_relative_to_the_repo(tmp_path: Path) -> None:
"""Formats that are not YAML (the ingot data-model TOML) are NAMED by the
section rather than inlined; the path stays repo-relative so the
declaration is portable."""
(tmp_path / "model.toml").write_text("", encoding="utf-8")
decl = {SECTION_DATA_MODEL: "model.toml"}
assert declared_path(decl, SECTION_DATA_MODEL, tmp_path) == (tmp_path / "model.toml").resolve()
# Declared but missing warns and yields None rather than failing the build.
assert declared_path({SECTION_DATA_MODEL: "gone.toml"}, SECTION_DATA_MODEL, tmp_path) is None
assert declared_path({}, SECTION_DATA_MODEL, tmp_path) is None
Activity · 5 steps
test_entry_patterns_declaration_replaces_the_guesses_not_the_facts
None test_declaration.test_entry_patterns_declaration_replaces_the_guesses_not_the_facts()
Checks
assert set(ENTRY_PATTERN_FACTS) <= set(declared.values), "facts must survive"assert "%trampoline%" in declared.values and "dispatch_%" in declared.valuesassert "%init%" not in declared.values, "a declaration replaces the guesses"assert list(_entry_patterns(args, {}).values) == DEFAULT_ENTRY_PATTERNSassert list(_entry_patterns(explicit, decl).values) == [*ENTRY_PATTERN_FACTS, "only_this"]
Result: passed
Verifies
| requirement | linked | path to the implementer |
|---|---|---|
| REQ-DDB-CONFIG-001 | measured | test_entry_patterns_declaration_replaces_the_guesses_not_the_facts → _entry_patterns → string_list |
| REQ-DDB-CONFIG-006 | measured | test_entry_patterns_declaration_replaces_the_guesses_not_the_facts → _entry_patterns → resolve_layered |
Source · 40 lines
def test_entry_patterns_declaration_replaces_the_guesses_not_the_facts() -> None:
"""#34: a repo whose handlers are reached only through indirect dispatch has
no static caller for them, so liveness marks them orphan. Naming that
convention reclaims them — measured on a C++ codebase, declaring its dispatch naming
takes orphans from 61 to 17, and the survivors (now_ms, parse_bool,
read_file, slurp) are genuine utility leaves.
REWRITTEN FOR gh#319, and the old contract is what the rewrite is about. This
test used to assert that a declaration EXTENDS the whole default set while an
explicit flag REPLACES it — two different meanings of "supersede" for one
setting, so moving a value from `.clew.yaml` onto the command line
silently dropped `main`, `app_main` and every guess with them. Its final line
(`_entry_patterns(explicit, decl) == ["only_this"]`) PINNED that collapse as
intended behaviour.
Both stated tiers now displace only the tier-5 name-shape guesses, and the
tier-3 language/platform entry points accumulate beneath whatever is stated.
The full rule is exercised in tests/test_tiers.py; this keeps the
declaration-shaped case where the declaration format is tested."""
import argparse
from clew.cli import _entry_patterns
from clew.reachability import DEFAULT_ENTRY_PATTERNS, ENTRY_PATTERN_FACTS
args = argparse.Namespace(entry_patterns=None)
decl = {SECTION_ENTRY_PATTERNS: ["%trampoline%", "dispatch_%"]}
declared = _entry_patterns(args, decl)
assert set(ENTRY_PATTERN_FACTS) <= set(declared.values), "facts must survive"
assert "%trampoline%" in declared.values and "dispatch_%" in declared.values
assert "%init%" not in declared.values, "a declaration replaces the guesses"
# No declaration: exactly the defaults, unchanged.
assert list(_entry_patterns(args, {}).values) == DEFAULT_ENTRY_PATTERNS
# An explicit flag beats the declaration — and still cannot drop a fact.
explicit = argparse.Namespace(entry_patterns=["only_this"])
assert list(_entry_patterns(explicit, decl).values) == [*ENTRY_PATTERN_FACTS, "only_this"]
Activity · 12 steps
test_every_known_section_is_accepted
None test_declaration.test_every_known_section_is_accepted(Path tmp_path)
The negative control. Without it the refusal above would pass just as happily against a loader that rejected everything, and a declaration format nobody can use is worse than one that mis-reads a typo.
Checks
assert set(load_declaration(tmp_path)) == set(KNOWN_SECTIONS)
Result: passed
Verifies
| requirement | linked | path to the implementer |
|---|---|---|
| REQ-DDB-CONFIG-001 | measured | test_every_known_section_is_accepted → load_declaration |
| REQ-DDB-GUARD-012 | measured | test_every_known_section_is_accepted → load_declaration → load_declaration_located → _checked_sections → _checked_section → _checked_requirements → _validate_node |
| REQ-DDB-WIKI-054 | inferred | test_every_known_section_is_accepted → load_declaration → load_declaration_located → _checked_sections → _checked_section → _checked_list_section → all |
Source · 23 lines
def test_every_known_section_is_accepted(tmp_path: Path) -> None:
"""The negative control. Without it the refusal above would pass just as
happily against a loader that rejected everything, and a declaration format
nobody can use is worse than one that mis-reads a typo."""
from clew.declaration import KNOWN_SECTIONS, LIST_SECTIONS, PATH_SECTIONS, load_declaration
## FIXTURE CHANGED (2.0.0, R5-CFG F1): every section used to be written `[]`, a value the loader
## now refuses for a mapping or path section (a wrong SHAPE is refused, not dropped). The
## property under test — every known NAME is accepted — is unchanged, so each section gets an
## empty value of its own shape.
def empty(section: str) -> str:
return "[]" if section in LIST_SECTIONS else ("x" if section in PATH_SECTIONS else "{}")
body = "".join(f"{section}: {empty(section)}\n" for section in sorted(KNOWN_SECTIONS))
(tmp_path / ".clew.yaml").write_text(body + "guard: {}\n", encoding="utf-8")
# `guard:` is accepted and then dropped: it is the gate's section, not a declaration.
assert set(load_declaration(tmp_path)) == set(KNOWN_SECTIONS)
# ─── event_tags: a mis-typed ROLE is refused, not defaulted ──────────────────
Activity · 5 steps
test_explicit_flag_wins_over_the_declaration
None test_declaration.test_explicit_flag_wins_over_the_declaration(Path tmp_path)
The file is the repo's standing statement; a flag is a deliberate one-off override, so the flag takes precedence.
Checks
assert _declared_or_flag(str(explicit), decl, SECTION_SHARED_KEY) == explicit.resolve()assert _declared_or_flag(None, decl, SECTION_SHARED_KEY) == decl[SECTION_SHARED_KEY]assert _declared_or_flag(None, {}, SECTION_SHARED_KEY) is None
Result: passed
Verifies
| requirement | linked | path to the implementer |
|---|---|---|
| REQ-DDB-CONFIG-001 | measured |
Source · 14 lines
def test_explicit_flag_wins_over_the_declaration(tmp_path: Path) -> None:
"""The file is the repo's standing statement; a flag is a deliberate
one-off override, so the flag takes precedence."""
from clew.cli import _declared_or_flag
decl = {SECTION_SHARED_KEY: {"writers": [{"name_prefix": "FROM_DECL_"}]}}
explicit = tmp_path / "patterns.yaml"
explicit.write_text("writers: []\n", encoding="utf-8")
assert _declared_or_flag(str(explicit), decl, SECTION_SHARED_KEY) == explicit.resolve()
assert _declared_or_flag(None, decl, SECTION_SHARED_KEY) == decl[SECTION_SHARED_KEY]
assert _declared_or_flag(None, {}, SECTION_SHARED_KEY) is None
Activity · 7 steps
test_inline_manifest_still_busts_the_stage_cache
None test_declaration.test_inline_manifest_still_busts_the_stage_cache(Path tmp_path)
manifest_key folds a manifest's CONTENT into the affected stage's cache key. A declaration delivered as a mapping must hash the same way a file does, or editing `.clew.yaml` would silently reuse stale extractions.
Checks
assert manifest_key(a) == manifest_key(dict(a)), "hash must be content-stable"assert manifest_key(a) != manifest_key(b), "a changed declaration must invalidate"assert manifest_key(None) == ""
Result: passed
Verifies
| requirement | linked | path to the implementer |
|---|---|---|
| REQ-DDB-CONFIG-001 | catalog | |
| REQ-DDB-INDEX-002 | inferred | test_inline_manifest_still_busts_the_stage_cache → manifest_key → hash_file |
| REQ-DDB-PIPE-003 | measured | test_inline_manifest_still_busts_the_stage_cache → manifest_key |
Source · 11 lines
def test_inline_manifest_still_busts_the_stage_cache(tmp_path: Path) -> None:
"""manifest_key folds a manifest's CONTENT into the affected stage's cache
key. A declaration delivered as a mapping must hash the same way a file
does, or editing `.clew.yaml` would silently reuse stale extractions."""
a = {"writers": [{"name_prefix": "A_"}]}
b = {"writers": [{"name_prefix": "B_"}]}
assert manifest_key(a) == manifest_key(dict(a)), "hash must be content-stable"
assert manifest_key(a) != manifest_key(b), "a changed declaration must invalidate"
assert manifest_key(None) == ""
Activity · 5 steps
test_malformed_declaration_degrades_to_defaults
None test_declaration.test_malformed_declaration_degrades_to_defaults(Path tmp_path)
A typo must not fail a build that would otherwise succeed — it warns and falls back, rather than taking the repo's index down with it.
Checks
assert load_declaration(tmp_path) == {}assert load_declaration(tmp_path) == {}
Result: passed
Verifies
| requirement | linked | path to the implementer |
|---|---|---|
| REQ-DDB-CONFIG-001 | measured | test_malformed_declaration_degrades_to_defaults → load_declaration |
| REQ-DDB-GUARD-012 | inferred | test_malformed_declaration_degrades_to_defaults → load_declaration → load_declaration_located → _checked_sections → _checked_section → _checked_requirements → _validate_node |
| REQ-DDB-WIKI-054 | inferred | test_malformed_declaration_degrades_to_defaults → load_declaration → load_declaration_located → _checked_sections → _checked_section → _checked_list_section → all |
Source · 10 lines
def test_malformed_declaration_degrades_to_defaults(tmp_path: Path) -> None:
"""A typo must not fail a build that would otherwise succeed — it warns and
falls back, rather than taking the repo's index down with it."""
(tmp_path / ".clew.yaml").write_text("shared_key_patterns: [oops\n", encoding="utf-8")
assert load_declaration(tmp_path) == {}
(tmp_path / ".clew.yaml").write_text("- not\n- a\n- mapping\n", encoding="utf-8")
assert load_declaration(tmp_path) == {}
Activity · 4 steps
test_sections_mirror_the_standalone_manifest_formats
None test_declaration.test_sections_mirror_the_standalone_manifest_formats(Path tmp_path)
A section holds EXACTLY what the corresponding standalone file holds, so a repo can move an existing manifest's contents in verbatim and the same parser handles both delivery routes.
Called by none
Calls load_declaration, load_shared_key_patterns, load_thread_patterns
Checks
assert len(writers) == 1assert len(readers) == 1assert "osThreadNew_wrapper" in namesassert {p.name for p in DEFAULT_SPAWN_PATTERNS} <= names
Result: passed
Verifies
| requirement | linked | path to the implementer |
|---|---|---|
| REQ-DDB-CONFIG-001 | measured | test_sections_mirror_the_standalone_manifest_formats → load_declaration |
| REQ-DDB-GUARD-012 | inferred | test_sections_mirror_the_standalone_manifest_formats → load_declaration → load_declaration_located → _checked_sections → _checked_section → _checked_requirements → _validate_node |
| REQ-DDB-SCHEMA-001 | measured | test_sections_mirror_the_standalone_manifest_formats → load_thread_patterns |
| REQ-DDB-SCHEMA-005 | measured | test_sections_mirror_the_standalone_manifest_formats → load_shared_key_patterns |
| REQ-DDB-SCHEMA-012 | measured | test_sections_mirror_the_standalone_manifest_formats → load_shared_key_patterns → declaration_origin |
| REQ-DDB-WIKI-054 | inferred | test_sections_mirror_the_standalone_manifest_formats → load_declaration → load_declaration_located → _checked_sections → _checked_section → _checked_list_section → all |
Source · 29 lines
def test_sections_mirror_the_standalone_manifest_formats(tmp_path: Path) -> None:
"""A section holds EXACTLY what the corresponding standalone file holds, so
a repo can move an existing manifest's contents in verbatim and the same
parser handles both delivery routes."""
(tmp_path / ".clew.yaml").write_text(
"shared_key_patterns:\n"
" writers:\n"
' - name_prefix: "Store_Set_"\n'
" readers:\n"
' - name_prefix: "Store_Get_"\n'
"thread_patterns:\n"
" spawns:\n"
' - name: "osThreadNew_wrapper"\n'
" entry_arg_index: 1\n",
encoding="utf-8",
)
decl = load_declaration(tmp_path)
writers, readers = load_shared_key_patterns(section(decl, SECTION_SHARED_KEY))
assert len(writers) == 1
assert len(readers) == 1
spawns = load_thread_patterns(section(decl, SECTION_THREADS))
names = {p.name for p in spawns}
# The declared wrapper is MERGED over the defaults, never replacing them.
assert "osThreadNew_wrapper" in names
assert {p.name for p in DEFAULT_SPAWN_PATTERNS} <= names
Activity · 9 steps
test_stated_document_meta_identifies_the_document_by_content
None test_declaration.test_stated_document_meta_identifies_the_document_by_content(Path tmp_path)
A stated declaration document identifies itself in build_meta by content.
Checks
assert meta["stated_sections"] == "locks, vendored", "sections must be SORTED, not file order"assert len(meta["stated_sha256"]) == 64assert other["stated_sha256"] != meta["stated_sha256"]assert other["stated_sections"] == meta["stated_sections"], ("the SECTION LIST is unchanged by a whitespace edit, which is why the sha is the " "load-bearing half rather than the section names" )assert not any("/" in value for value in meta.values()), f"a path leaked into {meta}"
Result: passed
Verifies
| requirement | linked | path to the implementer |
|---|---|---|
| REQ-DDB-CONFIG-001 | measured | test_stated_document_meta_identifies_the_document_by_content → stated_document_meta |
Source · 30 lines
def test_stated_document_meta_identifies_the_document_by_content(tmp_path: Path) -> None:
"""WHY THIS EXISTS AT ALL, because the obvious alternative was tried and shipped broken.
`options.*` records which TIER won per option, and it CANNOT answer "was this document
applied". Measured on mbedtls 2026-08-14: one document declaring `locks`, `vendored` and
`preprocessor` produced an index reading `options.locks.tier=explicit` (replayed from an
older build), `options.predefined.tier=heuristic`, and no vendored row at all. Every reader
reported health and a graded run was invalid.
"""
from clew.declaration import stated_document_meta
raw = b"vendored:\n - 3rdparty\nlocks:\n locks: []\n"
meta = stated_document_meta(raw, {"vendored": ["3rdparty"], "locks": {}})
assert meta["stated_sections"] == "locks, vendored", "sections must be SORTED, not file order"
assert len(meta["stated_sha256"]) == 64
## CONTENT-ADDRESSED: one byte's difference must produce a different identity, or a document
## edited after the build that stated it would still certify as applied — which is the
## quieter half of the defect above.
other = stated_document_meta(raw + b"\n", {"vendored": ["3rdparty"], "locks": {}})
assert other["stated_sha256"] != meta["stated_sha256"]
assert other["stated_sections"] == meta["stated_sections"], (
"the SECTION LIST is unchanged by a whitespace edit, which is why the sha is the "
"load-bearing half rather than the section names"
)
## NO PATH ANYWHERE IN IT. A stated document lives in the consumer's tree, so a path here
## publishes the builder's machine layout through every MCP reply.
assert not any("/" in value for value in meta.values()), f"a path leaked into {meta}"
Activity · 9 steps
test_string_list_section_ignores_a_malformed_value
None test_declaration.test_string_list_section_ignores_a_malformed_value(Path tmp_path)
A non-list value degrades to 'declared nothing' with a warning rather than being coerced — seeding something unintended into reachability would mark genuinely dead code live, which is worse than a false orphan.
Checks
assert string_list({SECTION_ENTRY_PATTERNS: ["a", "b"]}, SECTION_ENTRY_PATTERNS) == ["a", "b"]assert string_list({SECTION_ENTRY_PATTERNS: "not a list"}, SECTION_ENTRY_PATTERNS) == []assert string_list({SECTION_ENTRY_PATTERNS: {"k": "v"}}, SECTION_ENTRY_PATTERNS) == []assert string_list({}, SECTION_ENTRY_PATTERNS) == []
Result: passed
Verifies
| requirement | linked | path to the implementer |
|---|---|---|
| REQ-DDB-CONFIG-001 | measured | test_string_list_section_ignores_a_malformed_value → string_list |
Source · 15 lines
def test_string_list_section_ignores_a_malformed_value(tmp_path: Path) -> None:
"""A non-list value degrades to 'declared nothing' with a warning rather
than being coerced — seeding something unintended into reachability would
mark genuinely dead code live, which is worse than a false orphan."""
from clew.declaration import string_list
assert string_list({SECTION_ENTRY_PATTERNS: ["a", "b"]}, SECTION_ENTRY_PATTERNS) == ["a", "b"]
assert string_list({SECTION_ENTRY_PATTERNS: "not a list"}, SECTION_ENTRY_PATTERNS) == []
assert string_list({SECTION_ENTRY_PATTERNS: {"k": "v"}}, SECTION_ENTRY_PATTERNS) == []
assert string_list({}, SECTION_ENTRY_PATTERNS) == []
# ─── a misspelled section is refused, not silently defaulted ─────────────────
Activity · 5 steps
test_the_index_scope_spelling_cannot_drift
None test_declaration.test_the_index_scope_spelling_cannot_drift()
`index_scope` is owned by `scope.py` but repeated as a literal in `declaration.py`'s allow-list, because neither module imports the other.
Checks
assert INDEX_SCOPE_SECTION in KNOWN_SECTIONS
Result: passed
Verifies
| requirement | linked | path to the implementer |
|---|---|---|
| REQ-DDB-CONFIG-001 | catalog |
Source · 14 lines
def test_the_index_scope_spelling_cannot_drift() -> None:
"""`index_scope` is owned by `scope.py` but repeated as a literal in
`declaration.py`'s allow-list, because neither module imports the other.
Duplicated spellings drift silently — that is the whole lesson of
`vocabulary.py`, which centralised 26 restated CHECK constraints. This pins the
two together so a rename in one place fails here instead of making every
`index_scope:` declaration a refused unknown section."""
from clew.declaration import KNOWN_SECTIONS
from clew.scope import INDEX_SCOPE_SECTION
assert INDEX_SCOPE_SECTION in KNOWN_SECTIONS
Activity · 3 steps
test_the_old_guard_config_passthrough_is_not_read
None test_declaration.test_the_old_guard_config_passthrough_is_not_read(Path tmp_path)
HARD BREAK: `.doxygen-guard.yaml` and its `x-clew` passthrough are gone. A repo that has not moved its declaration gets the built-in defaults, not a silent fallback.
Checks
assert load_declaration(tmp_path) == {}
Result: passed
Verifies
| requirement | linked | path to the implementer |
|---|---|---|
| REQ-DDB-CONFIG-001 | measured | test_the_old_guard_config_passthrough_is_not_read → load_declaration |
| REQ-DDB-GUARD-012 | inferred | test_the_old_guard_config_passthrough_is_not_read → load_declaration → load_declaration_located → _checked_sections → _checked_section → _checked_requirements → _validate_node |
| REQ-DDB-WIKI-054 | inferred | test_the_old_guard_config_passthrough_is_not_read → load_declaration → load_declaration_located → _checked_sections → _checked_section → _checked_list_section → all |
Source · 9 lines
def test_the_old_guard_config_passthrough_is_not_read(tmp_path: Path) -> None:
"""HARD BREAK: `.doxygen-guard.yaml` and its `x-clew` passthrough are gone. A repo that
has not moved its declaration gets the built-in defaults, not a silent fallback."""
(tmp_path / ".doxygen-guard.yaml").write_text(
"x-clew:\n index_scope:\n include: ['^from-guard/']\n", encoding="utf-8"
)
assert load_declaration(tmp_path) == {}
Activity · 2 steps
test_the_refusal_names_the_probable_cause
test_declaration.TestForwardCompatibility.test_the_refusal_names_the_probable_cause(self, tmp_path)
An unknown section points at an upgrade.
Checks
assert "clew-trace" in message, (f"the refusal never mentions upgrading, which is the usual cause: {message}" )assert "guardd" in message and "allowed" in message, "the mechanical fix must survive"
Result: passed
Verifies
| requirement | linked | path to the implementer |
|---|---|---|
| REQ-DDB-CONFIG-001 | measured | test_the_refusal_names_the_probable_cause → load_declaration |
| REQ-DDB-GUARD-012 | inferred | test_the_refusal_names_the_probable_cause → load_declaration → load_declaration_located → _checked_sections → _checked_section → _checked_requirements → _validate_node |
| REQ-DDB-WIKI-054 | inferred | test_the_refusal_names_the_probable_cause → load_declaration → load_declaration_located → _checked_sections → _checked_section → _checked_list_section → all |
Source · 15 lines
def test_the_refusal_names_the_probable_cause(self, tmp_path):
"""@brief An unknown section points at an upgrade. @version 1"""
(tmp_path / ".clew.yaml").write_text("guardd:\n version_gate: strict\n")
from clew.vocabulary import DeclarationError
with pytest.raises(DeclarationError) as excinfo:
load_declaration(tmp_path)
message = str(excinfo.value)
assert "clew-trace" in message, (
f"the refusal never mentions upgrading, which is the usual cause: {message}"
)
assert "guardd" in message and "allowed" in message, "the mechanical fix must survive"