test_event_edges.py¶
tests/test_event_edges.py
Author-declared @emits/@handles event edges (task #47).
test_a_declared_event_vocabulary_reaches_the_importer
None test_event_edges.test_a_declared_event_vocabulary_reaches_the_importer(Path tmp_path)
THE DEFECT ITSELF (task #317). `import_event_edges` has taken an `event_tags` override since it was written and `cli.py` never passed one, so the 17 built-in English verbs were the entire vocabulary with no route to change them: a repo whose bus is documented `@broadcasts`/`@reacts` got silence, and no unit test could see it because every unit test calls the importer directly and can hand it whatever it likes.
Checks
assert edges == [("emit_foo", "on_foo", "EVENT:FOO")]
Result: passed
Verifies
Source · 21 lines
def test_a_declared_event_vocabulary_reaches_the_importer(tmp_path: Path) -> None:
"""THE DEFECT ITSELF (task #317). `import_event_edges` has taken an
`event_tags` override since it was written and `cli.py` never passed one, so
the 17 built-in English verbs were the entire vocabulary with no route to
change them: a repo whose bus is documented `@broadcasts`/`@reacts` got
silence, and no unit test could see it because every unit test calls the
importer directly and can hand it whatever it likes.
So this drives the REAL pipeline. Deleting the argument at the call site
turns it red again, which is the only check that the parameter is wired
rather than merely present."""
root = tmp_path / "declared"
doxyfile = _write_event_repo(root, "broadcasts", "reacts")
(root / ".clew.yaml").write_text(
"event_tags:\n broadcasts: producer\n reacts: consumer\n", encoding="utf-8"
)
edges = _built_event_edges(root, doxyfile, tmp_path / "declared.db")
assert edges == [("emit_foo", "on_foo", "EVENT:FOO")]
Activity · 5 steps
test_a_declared_vocabulary_replaces_the_defaults_rather_than_merging
None test_event_edges.test_a_declared_vocabulary_replaces_the_defaults_rather_than_merging(Path tmp_path)
REPLACE, not merge — and this is the test that would catch merge semantics, because every other one here declares verbs the defaults do not have and passes either way.
Checks
assert keys == {"bcast": PRODUCER}assert "evt_emits" not in keys, "a declared vocabulary must REPLACE the defaults"assert "exceptions" not in keys, "an exception alias must stop being read as an emit"
Result: passed
Verifies
| requirement | linked | path to the implementer |
|---|---|---|
| REQ-DDB-CONFIG-007 | measured | test_a_declared_vocabulary_replaces_the_defaults_rather_than_merging → _declared_event_keys → classify_aliases |
Source · 26 lines
def test_a_declared_vocabulary_replaces_the_defaults_rather_than_merging(
tmp_path: Path,
) -> None:
"""REPLACE, not merge — and this is the test that would catch merge semantics,
because every other one here declares verbs the defaults do not have and
passes either way.
It is load-bearing rather than a style choice. `raises` is a built-in PRODUCER
verb and an EXCEPTION verb in most codebases, so a repo aliasing
`raises=@xrefitem exceptions ...` has its exception documentation mined as
event production. Replacing is that repo's only fix; merging would leave it
with no way to say "not that word"."""
doxyfile = tmp_path / "Doxyfile"
doxyfile.write_text(
'ALIASES = "broadcasts=@xrefitem bcast \\"B\\" \\"B\\"" \\\n'
' "emits=@xrefitem evt_emits \\"E\\" \\"E\\"" \\\n'
' "raises=@xrefitem exceptions \\"Throws\\" \\"Exceptions\\""\n',
encoding="utf-8",
)
keys = _declared_event_keys(doxyfile, {"broadcasts": PRODUCER})
assert keys == {"bcast": PRODUCER}
assert "evt_emits" not in keys, "a declared vocabulary must REPLACE the defaults"
assert "exceptions" not in keys, "an exception alias must stop being read as an emit"
Activity · 6 steps
test_a_fully_recognised_alias_set_logs_no_hint
None test_event_edges.test_a_fully_recognised_alias_set_logs_no_hint(Path tmp_path, pytest.LogCaptureFixture caplog)
THE CONTROL. A diagnostic that fires on a repo whose vocabulary is already understood is noise, and noise is how a real hint stops being read — so the quiet case has to be pinned as hard as the loud one.
Checks
assert keys == {"evt_emits": PRODUCER, "evt_handles": CONSUMER}assert "not recognised event verbs" not in caplog.text
Result: passed
Verifies
| requirement | linked | path to the implementer |
|---|---|---|
| REQ-DDB-CONFIG-007 | measured | test_a_fully_recognised_alias_set_logs_no_hint → _declared_event_keys → classify_aliases |
Source · 20 lines
def test_a_fully_recognised_alias_set_logs_no_hint(
tmp_path: Path, caplog: pytest.LogCaptureFixture
) -> None:
"""THE CONTROL. A diagnostic that fires on a repo whose vocabulary is already
understood is noise, and noise is how a real hint stops being read — so the
quiet case has to be pinned as hard as the loud one."""
doxyfile = tmp_path / "Doxyfile"
doxyfile.write_text(
'ALIASES = "emits=@xrefitem evt_emits \\"E\\" \\"E\\"" \\\n'
' "handles=@xrefitem evt_handles \\"H\\" \\"H\\""\n',
encoding="utf-8",
)
with caplog.at_level(logging.INFO, logger="clew.event_edges"):
keys = _declared_event_keys(doxyfile, None)
assert keys == {"evt_emits": PRODUCER, "evt_handles": CONSUMER}
assert "not recognised event verbs" not in caplog.text
@_NEEDS_DOXYGENtest_an_undeclared_repo_vocabulary_is_silent_without_the_declaration
None test_event_edges.test_an_undeclared_repo_vocabulary_is_silent_without_the_declaration(Path tmp_path)
The negative control for the test above, and the reason the diagnostic exists: the SAME repo with the SAME aliases and no declaration produces no event edges, because `broadcasts`/`reacts` are not built-in verbs. Without this, the declared-vocabulary test would pass just as happily against an implementation that recognised every alias it found.
Checks
assert _built_event_edges(root, doxyfile, tmp_path / "undeclared.db") == []
Result: passed
Verifies
Source · 12 lines
def test_an_undeclared_repo_vocabulary_is_silent_without_the_declaration(
tmp_path: Path,
) -> None:
"""The negative control for the test above, and the reason the diagnostic
exists: the SAME repo with the SAME aliases and no declaration produces no
event edges, because `broadcasts`/`reacts` are not built-in verbs. Without
this, the declared-vocabulary test would pass just as happily against an
implementation that recognised every alias it found."""
root = tmp_path / "undeclared"
doxyfile = _write_event_repo(root, "broadcasts", "reacts")
assert _built_event_edges(root, doxyfile, tmp_path / "undeclared.db") == []1 more lines, not shown
Activity · 3 steps
test_an_unrecognised_alias_is_named_in_the_log
None test_event_edges.test_an_unrecognised_alias_is_named_in_the_log(Path tmp_path, pytest.LogCaptureFixture caplog)
THE DIAGNOSTIC. Before it, a repo using `@broadcasts` got zero rows and total silence — indistinguishable from a repo with no event bus at all, which is the same shape as the #29 undeclared-accessor case: the layer was empty because the detector had no way to look, not because there was nothing there.
Checks
assert _declared_event_keys(doxyfile, None) == {}assert "broadcasts" in caplog.text and "reacts" in caplog.textassert "event_tags" in caplog.text, "the hint must name the declaration that claims them"
Result: passed
Verifies
| requirement | linked | path to the implementer |
|---|---|---|
| REQ-DDB-CONFIG-007 | measured | test_an_unrecognised_alias_is_named_in_the_log → _declared_event_keys → classify_aliases |
Source · 21 lines
def test_an_unrecognised_alias_is_named_in_the_log(
tmp_path: Path, caplog: pytest.LogCaptureFixture
) -> None:
"""THE DIAGNOSTIC. Before it, a repo using `@broadcasts` got zero rows and
total silence — indistinguishable from a repo with no event bus at all, which
is the same shape as the #29 undeclared-accessor case: the layer was empty
because the detector had no way to look, not because there was nothing there.
"""
doxyfile = tmp_path / "Doxyfile"
doxyfile.write_text(
'ALIASES = "broadcasts=@xrefitem bcast \\"B\\" \\"B\\"" \\\n'
' "reacts=@xrefitem rct \\"R\\" \\"R\\""\n',
encoding="utf-8",
)
with caplog.at_level(logging.INFO, logger="clew.event_edges"):
assert _declared_event_keys(doxyfile, None) == {}
assert "broadcasts" in caplog.text and "reacts" in caplog.text
assert "event_tags" in caplog.text, "the hint must name the declaration that claims them"
Activity · 6 steps
test_decl_def_duplicate_collapses_to_the_definition
None test_event_edges.test_decl_def_duplicate_collapses_to_the_definition(Path tmp_path)
Doxygen documents one function twice when it is declared in a header and defined in a .cpp; both rows carry the tag. Collapse to the definition so a single logical emitter does not double its edges.
Checks
assert topics["EVENT:FOO"][PRODUCER] == {11}assert _edges_for(topics) == [(11, 20, "EVENT:FOO")]
Result: passed
Verifies
| requirement | linked | path to the implementer |
|---|---|---|
| REQ-DDB-SCHEMA-009 | measured | test_decl_def_duplicate_collapses_to_the_definition → _topic_roles |
Source · 14 lines
def test_decl_def_duplicate_collapses_to_the_definition(tmp_path: Path) -> None:
"""Doxygen documents one function twice when it is declared in a header and
defined in a .cpp; both rows carry the tag. Collapse to the definition so a
single logical emitter does not double its edges."""
tagged = [
("EVENT:FOO", PRODUCER, "emit_it", 10, False), # header declaration
("EVENT:FOO", PRODUCER, "emit_it", 11, True), # definition
("EVENT:FOO", CONSUMER, "on_foo", 20, True),
]
topics = _topic_roles(tagged)
assert topics["EVENT:FOO"][PRODUCER] == {11}
assert _edges_for(topics) == [(11, 20, "EVENT:FOO")]
Activity · 4 steps
test_declared_event_keys_reads_the_repos_own_aliases
None test_event_edges.test_declared_event_keys_reads_the_repos_own_aliases(Path tmp_path)
The xrefitem KEY is repo-chosen, so it is read from the target's ALIASES rather than assumed. Only the author-facing TAG NAME is interpreted.
Checks
assert keys == {"evt_emits": PRODUCER, "evt_handles": CONSUMER}assert "req_trace" not in keys
Result: passed
Verifies
| requirement | linked | path to the implementer |
|---|---|---|
| REQ-DDB-CONFIG-007 | measured | test_declared_event_keys_reads_the_repos_own_aliases → _declared_event_keys → classify_aliases |
Source · 11 lines
def test_declared_event_keys_reads_the_repos_own_aliases(tmp_path: Path) -> None:
"""The xrefitem KEY is repo-chosen, so it is read from the target's ALIASES
rather than assumed. Only the author-facing TAG NAME is interpreted."""
doxyfile = tmp_path / "Doxyfile"
doxyfile.write_text(_XREFITEM_ALIASES, encoding="utf-8")
keys = _declared_event_keys(doxyfile, None)
assert keys == {"evt_emits": PRODUCER, "evt_handles": CONSUMER}
# req_trace is an alias too, but `req` is not an event verb — correctly ignored.
assert "req_trace" not in keys
Activity · 5 steps
test_default_vocabulary_still_produces_event_edges_end_to_end
None test_event_edges.test_default_vocabulary_still_produces_event_edges_end_to_end(Path tmp_path)
THE SUCCESS-PATH HALF. A target that declares NOTHING must keep getting DEFAULT_EVENT_TAGS and the same edges it got before the declaration section existed.
Checks
assert not (root / ".clew.yaml").exists(), "the control declares nothing"assert edges == [("emit_foo", "on_foo", "EVENT:FOO")]
Result: passed
Verifies
Source · 19 lines
def test_default_vocabulary_still_produces_event_edges_end_to_end(tmp_path: Path) -> None:
"""THE SUCCESS-PATH HALF. A target that declares NOTHING must keep getting
DEFAULT_EVENT_TAGS and the same edges it got before the declaration section
existed.
Written because a check with a test for its failure path and none for its
success path is this project's standing way of shipping something completely
broken while the suite stays green — and because the declared-vocabulary test
below is worthless without a control proving the pipeline reaches this
importer at all."""
root = tmp_path / "defaults"
doxyfile = _write_event_repo(root, "emits", "handles")
assert not (root / ".clew.yaml").exists(), "the control declares nothing"
edges = _built_event_edges(root, doxyfile, tmp_path / "defaults.db")
assert edges == [("emit_foo", "on_foo", "EVENT:FOO")]
@_NEEDS_DOXYGENtest_edges_only_attach_to_rowids_that_carried_the_tag
None test_event_edges.test_edges_only_attach_to_rowids_that_carried_the_tag(Path tmp_path)
REGRESSION for the bug this importer was first written with.
Checks
assert rows == [(1, 2, "EVENT:FOO", "event", 1, "keyed", 1)], ("untagged same-named functions must not inherit the edge" )
Result: passed
Verifies
| requirement | linked | path to the implementer |
|---|---|---|
| REQ-DDB-CONFIG-007 | measured | test_edges_only_attach_to_rowids_that_carried_the_tag → import_event_edges → _declared_event_keys → classify_aliases |
| REQ-DDB-SCHEMA-009 | measured | test_edges_only_attach_to_rowids_that_carried_the_tag → import_event_edges |
Source · 34 lines
def test_edges_only_attach_to_rowids_that_carried_the_tag(tmp_path: Path) -> None:
"""REGRESSION for the bug this importer was first written with.
`memberdef.name` is UNQUALIFIED: on a C++ codebase the name `react` maps to 60
memberdefs, one per FSM class, of which exactly ONE declares `@handles`.
The first implementation harvested tag-carrying NAMES and then re-resolved
each name through a name index, fabricating an edge for the other 59 — 5294
edges where 549 are real. An edge must only ever be attributed to a
memberdef that actually carried the tag."""
db = tmp_path / "evt.db"
_make_db(
db,
[
(1, "emit_it", 1, _EMITS.format(topic="EVENT:FOO")),
(2, "react", 1, _HANDLES.format(topic="EVENT:FOO")), # the ONE tagged react
(3, "react", 1, "<para>a different class's react, no tag</para>"),
(4, "react", 1, ""), # and another
],
)
doxyfile = tmp_path / "Doxyfile"
doxyfile.write_text(_XREFITEM_ALIASES, encoding="utf-8")
import_event_edges(db, doxyfile)
conn = sqlite3.connect(db)
rows = conn.execute(
"SELECT writer_rowid, reader_rowid, key_name, edge_kind, declared, "
"dispatch_mode, edge_triggered FROM shared_key_edges"
).fetchall()
conn.close()
assert rows == [(1, 2, "EVENT:FOO", "event", 1, "keyed", 1)], (
"untagged same-named functions must not inherit the edge"
)
Activity · 9 steps
test_event_tag_vocabulary_is_overridable
None test_event_edges.test_event_tag_vocabulary_is_overridable(Path tmp_path)
A repo using its own verbs supplies a mapping instead of the defaults — the built-in vocabulary is a default, not a hardcoded assumption.
Checks
assert _declared_event_keys(doxyfile, None) == {} # not in the default vocabularyassert keys == {"bcast": PRODUCER, "rct": CONSUMER}
Result: passed
Verifies
| requirement | linked | path to the implementer |
|---|---|---|
| REQ-DDB-CONFIG-007 | measured | test_event_tag_vocabulary_is_overridable → _declared_event_keys → classify_aliases |
Source · 14 lines
def test_event_tag_vocabulary_is_overridable(tmp_path: Path) -> None:
"""A repo using its own verbs supplies a mapping instead of the defaults —
the built-in vocabulary is a default, not a hardcoded assumption."""
doxyfile = tmp_path / "Doxyfile"
doxyfile.write_text(
'ALIASES = "broadcasts=@xrefitem bcast \\"B\\" \\"B\\"" \\\n'
' "reacts=@xrefitem rct \\"R\\" \\"R\\""\n',
encoding="utf-8",
)
assert _declared_event_keys(doxyfile, None) == {} # not in the default vocabulary
keys = _declared_event_keys(doxyfile, {"broadcasts": PRODUCER, "reacts": CONSUMER})
assert keys == {"bcast": PRODUCER, "rct": CONSUMER}
Activity · 5 steps
test_one_sided_topics_yield_no_edges
None test_event_edges.test_one_sided_topics_yield_no_edges(Path tmp_path)
A topic emitted but never handled (or vice versa) produces no edge — it is reported as a diagnostic instead, since it usually means a real defect.
Checks
assert _edges_for(topics) == []
Result: passed
Verifies
| requirement | linked | path to the implementer |
|---|---|---|
| REQ-DDB-SCHEMA-009 | measured | test_one_sided_topics_yield_no_edges → _topic_roles |
Source · 12 lines
def test_one_sided_topics_yield_no_edges(tmp_path: Path) -> None:
"""A topic emitted but never handled (or vice versa) produces no edge — it
is reported as a diagnostic instead, since it usually means a real defect."""
topics = _topic_roles(
[
("EVENT:ORPHAN", PRODUCER, "emit_it", 1, True),
("EVENT:UNEMITTED", CONSUMER, "on_it", 2, True),
]
)
assert _edges_for(topics) == []
Activity · 2 steps
test_repo_without_event_aliases_writes_nothing
None test_event_edges.test_repo_without_event_aliases_writes_nothing(Path tmp_path)
demobot and every non-event repo: a correct negative, not an error.
Checks
assert conn.execute("SELECT COUNT(*) FROM shared_key_edges").fetchone()[0] == 0
Result: passed
Verifies
| requirement | linked | path to the implementer |
|---|---|---|
| REQ-DDB-CONFIG-007 | measured | test_repo_without_event_aliases_writes_nothing → import_event_edges → _declared_event_keys → classify_aliases |
| REQ-DDB-SCHEMA-009 | measured | test_repo_without_event_aliases_writes_nothing → import_event_edges |
Source · 16 lines
def test_repo_without_event_aliases_writes_nothing(tmp_path: Path) -> None:
"""demobot and every non-event repo: a correct negative, not an error."""
db = tmp_path / "evt.db"
_make_db(db, [(1, "f", 1, _EMITS.format(topic="EVENT:FOO"))])
doxyfile = tmp_path / "Doxyfile"
doxyfile.write_text("PROJECT_NAME = synth\nINPUT =\n", encoding="utf-8")
import_event_edges(db, doxyfile)
conn = sqlite3.connect(db)
assert conn.execute("SELECT COUNT(*) FROM shared_key_edges").fetchone()[0] == 0
conn.close()
# ─── the DECLARATION route, driven through the real pipeline ─────────────────
Activity · 8 steps
test_unknown_tag_vocabulary_and_missing_doxyfile_are_correct_negatives
None test_event_edges.test_unknown_tag_vocabulary_and_missing_doxyfile_are_correct_negatives(Path tmp_path)
A repo with no event alias, and an unreadable/synthesized Doxyfile, both yield zero keys — empty, never an error.
Checks
assert _declared_event_keys(plain, None) == {}assert _declared_event_keys(tmp_path / "absent", None) == {}
Result: passed
Verifies
| requirement | linked | path to the implementer |
|---|---|---|
| REQ-DDB-CONFIG-007 | measured | test_unknown_tag_vocabulary_and_missing_doxyfile_are_correct_negatives → _declared_event_keys → classify_aliases |
Source · 11 lines
def test_unknown_tag_vocabulary_and_missing_doxyfile_are_correct_negatives(
tmp_path: Path,
) -> None:
"""A repo with no event alias, and an unreadable/synthesized Doxyfile, both
yield zero keys — empty, never an error."""
plain = tmp_path / "Doxyfile"
plain.write_text('ALIASES = "note=@xrefitem notes \\"Note\\" \\"Notes\\""\n', encoding="utf-8")
assert _declared_event_keys(plain, None) == {}
assert _declared_event_keys(tmp_path / "absent", None) == {}