test_wire.py¶
tests/test_wire.py
Tests for the shared R2→JSON serializer.
test_envelope_keys_all_survive
None test_wire.test_envelope_keys_all_survive()
The envelope keeps every key, even the empty ones.
Checks
assert out is not Noneassert set(out) == {"subject", "brief", "threads", "callers"}assert out["brief"] is Noneassert out["threads"] == []assert out["callers"] == []
Result: passed
Verifies
| requirement | linked | path to the implementer |
|---|---|---|
| REQ-DDB-QUERY-001 | measured | test_envelope_keys_all_survive → one |
Source · 12 lines
def test_envelope_keys_all_survive() -> None:
"""A consumer reads the envelope key by key, so `threads: []` must be PRESENT to
be read as "none". This is the distinction that broke when elision was applied
universally, and the viewer/MCP field-parity test caught it."""
out = wire.one(_Envelope())
assert out is not None
assert set(out) == {"subject", "brief", "threads", "callers"}
assert out["brief"] is None
assert out["threads"] == []
assert out["callers"] == []
Activity · 6 steps
test_false_and_zero_are_measurements_not_absences
None test_wire.test_false_and_zero_are_measurements_not_absences()
A false or zero value is kept, not treated as absent.
Checks
assert out is not Noneassert row["crosses_thread"] is Falseassert row["line_start"] == 0assert "strength" not in row
Result: passed
Verifies
| requirement | linked | path to the implementer |
|---|---|---|
| REQ-DDB-QUERY-001 | measured | test_false_and_zero_are_measurements_not_absences → one |
Source · 12 lines
def test_false_and_zero_are_measurements_not_absences() -> None:
"""`crosses_thread: false` says "this edge stays on one thread", which is a
different claim from the field being missing ("nobody looked"). Eliding falsy
values instead of absent ones would silently turn the former into the latter."""
out = wire.one(_Envelope(callers=[_Row(crosses_thread=False, line_start=0)]))
assert out is not None
row = out["callers"][0]
assert row["crosses_thread"] is False
assert row["line_start"] == 0
assert "strength" not in row
Activity · 6 steps
test_internal_row_ids_are_stripped_from_envelope_and_rows
None test_wire.test_internal_row_ids_are_stripped_from_envelope_and_rows()
Internal database row ids must never reach a served payload.
Checks
assert out is not Noneassert "rowid" not in out, "an internal row id must not be served on the envelope"assert "rowid" not in out["callers"][0], "nor inside a neighbour row"assert out["subject"] == "thing", "stripping must not remove real envelope fields"assert out["callers"][0]["name"] == "callee", "nor real row fields"assert out["callers"][0]["line_start"] == 7, "a genuine int field must survive"
Result: passed
Verifies
| requirement | linked | path to the implementer |
|---|---|---|
| REQ-DDB-QUERY-001 | measured | test_internal_row_ids_are_stripped_from_envelope_and_rows → one |
Source · 48 lines
def test_internal_row_ids_are_stripped_from_envelope_and_rows() -> None:
"""A GRADED ANSWER PUBLISHED ONE AS A SOURCE LOCATION. An index-arm run wrote
`programs/ssl/ssl_pthread_server.c:2244` as a citation; the file is 484 lines and 2244 was a
`rowid`. The answer even hedged "per index rowid", so the model half-knew and wrote it anyway —
which makes this a payload-shape defect rather than a model slip.
THE SHAPE IS WHY. `rowid` sat beside `line_start` and `line_end`, which ARE locations, and
repeated inside every `callers`/`callees` row — 15 occurrences in one measured reply, each an
integer next to a symbol name with no line number of its own. That is precisely the slot a
reader fills with "line". Nothing on the tool surface accepts a rowid as input, so it bought
nothing in exchange.
BOTH LEVELS, which is what distinguishes this from elision. Elision is row-scoped because an
envelope key must be present to read as "none"; a `rowid` misleads equally in either place.
@brief `rowid` is absent from both the envelope and its rows.
@return None.
@version 1
"""
## THE FIXTURE MUST ACTUALLY CARRY A rowid, and the first version of this test did not — the
## shared `_Row` had been renamed to `line_start`, so `"rowid" not in row` was trivially true
## and emptying `_INTERNAL_ONLY` left the test GREEN. The mutation control caught it. A fixture
## that matches the detector rather than the world is this repo's standing failure.
@dataclass(frozen=True)
class _IdBearing:
"""@brief A row and envelope that really do carry an internal id. @version 1"""
rowid: int = 99
name: str = "callee"
line_start: int = 7
@dataclass(frozen=True)
class _IdEnvelope:
"""@brief Envelope carrying an id of its own plus id-bearing rows. @version 1"""
rowid: int = 42
subject: str = "thing"
callers: tuple[_IdBearing, ...] = (_IdBearing(),)
out = wire.one(_IdEnvelope())
assert out is not None
assert "rowid" not in out, "an internal row id must not be served on the envelope"
assert "rowid" not in out["callers"][0], "nor inside a neighbour row"
## A strip that emptied everything would satisfy both assertions above.
assert out["subject"] == "thing", "stripping must not remove real envelope fields"
assert out["callers"][0]["name"] == "callee", "nor real row fields"
assert out["callers"][0]["line_start"] == 7, "a genuine int field must survive"1 more lines, not shown
Activity · 9 steps
test_none_serializes_to_none
None test_wire.test_none_serializes_to_none()
None in, None out.
Checks
assert wire.one(None) is None
Result: passed
Verifies
| requirement | linked | path to the implementer |
|---|---|---|
| REQ-DDB-QUERY-001 | measured | test_none_serializes_to_none → one |
Source · 6 lines
def test_none_serializes_to_none() -> None:
"""Every MCP wrapper over an optional R2 result depends on this: a missing symbol
is a null payload, not an exception and not an empty dict."""
assert wire.one(None) is None
Activity · 1 steps
test_rows_helper_treats_its_items_as_rows
None test_wire.test_rows_helper_treats_its_items_as_rows()
rows prunes each element; one per element would not.
Checks
assert wire.rows([_Row()]) == [{"name": "callee", "line_start": 7}]assert wire.one(_Row()) == {
Result: passed
Verifies
| requirement | linked | path to the implementer |
|---|---|---|
| REQ-DDB-QUERY-001 | measured | test_rows_helper_treats_its_items_as_rows → rows |
Source · 14 lines
def test_rows_helper_treats_its_items_as_rows() -> None:
"""The two entry points differ ONLY in whether the top level is a row, and calling
the wrong one is a real mistake that the MCP parity test caught: `one` per element
treats each as an envelope and prunes nothing."""
assert wire.rows([_Row()]) == [{"name": "callee", "line_start": 7}]
assert wire.one(_Row()) == {
"name": "callee",
"line_start": 7,
"strength": None,
"crosses_thread": None,
"implementors": [],
}
Activity · 2 steps
test_rows_inside_an_envelope_are_pruned
None test_wire.test_rows_inside_an_envelope_are_pruned()
A row inside a list drops the fields carrying nothing.
Checks
assert out is not Noneassert out["callers"] == [{"name": "callee", "line_start": 7}]
Result: passed
Verifies
| requirement | linked | path to the implementer |
|---|---|---|
| REQ-DDB-QUERY-001 | measured | test_rows_inside_an_envelope_are_pruned → one |
Source · 7 lines
def test_rows_inside_an_envelope_are_pruned() -> None:
"""The measured saving: 54% of a real call row's fields were null by kind."""
out = wire.one(_Envelope(callers=[_Row()]))
assert out is not None
assert out["callers"] == [{"name": "callee", "line_start": 7}]
Activity · 3 steps
test_tuples_become_lists
None test_wire.test_tuples_become_lists()
A tuple field becomes a JSON array so the round trip holds.
Checks
assert out is not Noneassert out["callers"][0]["implementors"] == ["Derived::run", "Other::run"]assert json.loads(json.dumps(out)) == out
Result: passed
Verifies
| requirement | linked | path to the implementer |
|---|---|---|
| REQ-DDB-QUERY-001 | measured | test_tuples_become_lists → one |
Source · 9 lines
def test_tuples_become_lists() -> None:
"""R2's dataclasses are frozen, so a multi-valued field is a tuple; `json.loads`
returns a list, so a tuple on the wire fails equality with its own round trip."""
out = wire.one(_Envelope(callers=[_Row(implementors=("Derived::run", "Other::run"))]))
assert out is not None
assert out["callers"][0]["implementors"] == ["Derived::run", "Other::run"]
assert json.loads(json.dumps(out)) == out
Activity · 4 steps
test_unknown_enums_are_elided_but_only_the_named_ones
None test_wire.test_unknown_enums_are_elided_but_only_the_named_ones()
An undetermined enum is absent, and a determined one is present.
Checks
assert out is not None # envelope path still intactassert "dispatch_mode" not in pruned, "an undetermined dispatch_mode must not ship"assert "edge_kind" not in pruned, "an undetermined edge_kind must not ship"assert pruned["lock_kind"] == "unknown", ("lock_kind='unknown' is a measurement — a lock of undetermined type is not no lock" )assert pruned["crosses_thread"] is False, "a measured non-crossing must still ship"assert determined["dispatch_mode"] == "keyed"
Result: passed
Verifies
| requirement | linked | path to the implementer |
|---|---|---|
| REQ-DDB-QUERY-001 | measured | test_unknown_enums_are_elided_but_only_the_named_ones → rows |
Source · 38 lines
def test_unknown_enums_are_elided_but_only_the_named_ones() -> None:
"""A REGRESSION TEST for an inversion this module itself introduced.
`dispatch_mode` and `edge_kind` are `TEXT NOT NULL DEFAULT 'unknown'`, so unlike a
nullable column they cannot BE null — the pipeline records "undetermined" as a
literal string. Row elision therefore kept those two while dropping
`crosses_thread = NULL`, which is informative when known. The result was exactly
backwards: the two fields that say nothing were presented as measurements, and the
one that would matter was presented as not-applicable, which the shared `rows`
description tells a model to read as "the question does not apply".
`lock_kind` is the control. It also has an `'unknown'` member and there it IS
informative — "a lock whose type was not determined" is a different claim from "no
lock" — so the rule is keyed on FIELD NAME, and matching the string alone would
erase that."""
row = {
"name": "publish",
"dispatch_mode": "unknown",
"edge_kind": "unknown",
"lock_kind": "unknown",
"crosses_thread": False,
}
out = wire.one(_Envelope(callers=[]))
assert out is not None # envelope path still intact
pruned = wire.rows([_KeyRow(**row)])[0]
assert "dispatch_mode" not in pruned, "an undetermined dispatch_mode must not ship"
assert "edge_kind" not in pruned, "an undetermined edge_kind must not ship"
assert pruned["lock_kind"] == "unknown", (
"lock_kind='unknown' is a measurement — a lock of undetermined type is not no lock"
)
assert pruned["crosses_thread"] is False, "a measured non-crossing must still ship"
## And a DETERMINED value of the same field must survive.
determined = wire.rows([_KeyRow(**{**row, "dispatch_mode": "keyed"})])[0]
assert determined["dispatch_mode"] == "keyed"