"""Setop tests: detection, compute, and the machine's guard — no LLM, no net. Covers the two holes found in Emili's 2026-10-18 run: A) the set gate: mention rows must not answer an enumeration B) the setop engine: counts over list rows are computed, never routed Run: uv run python tests/setops.py """ from __future__ import annotations import sys from pathlib import Path sys.path.insert(0, str(Path(__file__).resolve().parent.parent)) # code/ from floop.setops import COUNT_RE, detect_setop, compute_setop, setop_row from floop.state import AnswerType, EvidenceRow, EvidenceTable def _list_row(eid_hint, items, qid="Q1"): t = EvidenceTable() row = EvidenceRow( qid=qid, answer_type=AnswerType.LIST, payload={"value": str(len(items)), "list_items": items, "list_count": len(items)}, engine="knowledge", trace=[], provenance={}, verified=False) return t.append(row) def test_detection(): eu = _list_row("e", ["Germany", "France", "Italy"]) world = _list_row("w", ["Germany", "France", "Italy", "Norway", "Switzerland", "United Kingdom", "Turkey"]) table = EvidenceTable() table.rows().extend # noop, just to silence linters for r in (eu, world): table.append(r) # count question + two list rows + negation -> difference spec = detect_setop("How many countries in the European continent are not members of European Union?", table) assert spec is not None and spec["op"] == "difference", spec # no negation -> intersection spec = detect_setop("How many countries are members of both?", table) assert spec is not None and spec["op"] == "intersection", spec # one list row -> plain count one = EvidenceTable() one.append(eu) spec = detect_setop("How many countries are in the list?", one) assert spec is not None and spec["op"] == "count", spec # not a count question -> None (falls through to the router) assert detect_setop("Which countries are not members?", table) is None # count question but no list rows -> None empty = EvidenceTable() assert detect_setop("How many countries?", empty) is None print("ok test_detection") def test_compute_difference(): eu = _list_row("e", ["Germany", "France", "Italy", "Austria*"]) all_europe = _list_row("w", ["Germany", "France", "Italy", "Austria", "Norway", "Switzerland", "United Kingdom", "Turkey"]) # table markers ('Austria*') must not split the element from 'Austria': # norm_name strips them, so 'Austria*' is IN the EU set -> difference # excludes it. Expected: Norway, Switzerland, UK, Turkey = 4. spec = {"op": "difference", "a": all_europe, "b": eu} out = compute_setop(spec) expected = {"norway", "switzerland", "united kingdom", "turkey"} assert set(out["list_items"]) == expected, out assert out["value"] == "4" and out["list_count"] == 4 print("ok test_compute_difference") def test_setop_row_verified(): eu = _list_row("e", ["Germany", "France"]) all_e = _list_row("w", ["Germany", "France", "Norway"]) spec = {"op": "difference", "a": all_e, "b": eu} row = setop_row(spec, "Q3", AnswerType.CONSTANT) assert row.verified is True, "the computation IS the verification" assert row.engine == "logic:setop" assert row.payload["value"] == "1" assert "setop difference" in row.trace[0] assert row.provenance["sources"] == ["E1", "E2"] or len(row.provenance["sources"]) == 2 print("ok test_setop_row_verified") if __name__ == "__main__": test_detection() test_compute_difference() test_setop_row_verified() print("\n3 setop tests passed")