Graph-Native Infrastructure for Context and Accountable AI Systems
Python agent-memoryaiai-governanceai-infrastructureartificial-intelligencecontext-engineeringcontext-graphsdata-engineeringdecision-intelligencedeveloper-toolsexplainable-aigenerative-aigraph-ragknowledge-graphllmontologyprovenancepythonreasoningsemantic-search
test(cli): exercise every extract --mode choice, incl. the default `all` (#1850) (#1851)
Kevina0dfbb897912
diff --git a/tests/test_cli_commands.py b/tests/test_cli_commands.pyindex d8ff55c9..0deef438 100644--- a/tests/test_cli_commands.py+++ b/tests/test_cli_commands.py@@ -673,6 +673,68 @@ class TestExtract:data = _json_output(result)assert set(data) == {"ner", "relations", "triplets", "events"}+ def test_mode_choices_include_all_and_coreference(self):+ """#1850 — every advertised mode must be a real `--mode` choice.++ `all` is the *default* mode, yet the smoke loop in+ ``tests/verify_rich_cli.py`` used to hardcode a list that silently+ omitted it — which is why #1789 shipped unnoticed. Pin the full+ ``choices`` set (and the default) so that loop stays aligned with the+ command contract.+ """+ mode_param = next(+ p for p in cli_module.extract.params if p.name == "mode"+ )+ choices = list(mode_param.type.choices)+ assert mode_param.default == "all"+ assert "all" in choices, f"'all' missing from choices: {choices}"+ assert set(choices) == {+ "ner", "relations", "triplets", "events", "coreference", "all",+ }++ def test_explicit_all_mode_returns_every_stage(self, runner, monkeypatch):+ """#1850 — an explicit `--mode all` (not just the default) runs all stages."""+ _ner_result = [MagicMock(text="Alice", label="PER", confidence=0.9,+ start_char=0, end_char=5, metadata={})]+ fake_ext = _fake_module(+ NERExtractor=lambda **kw: MagicMock(extract=lambda text, **kw2: _ner_result),+ RelationExtractor=lambda **kw: MagicMock(extract=lambda text, **kw2: []),+ TripletExtractor=lambda **kw: MagicMock(extract=lambda text, **kw2: []),+ EventDetector=lambda **kw: MagicMock(extract=lambda text, **kw2: []),+ )+ monkeypatch.setitem(+ __import__("sys").modules, "semantica.semantic_extract", fake_ext+ )+ result = runner.invoke(+ cli_module.main,+ ["extract", "Alice works at Acme.", "--mode", "all", "--json"],+ )+ _ok(result)+ assert set(_json_output(result)) == {"ner", "relations", "triplets", "events"}++ def test_coreference_mode_is_a_clean_placeholder_error(self, runner, monkeypatch):+ """#1850 — `coreference` is a listed choice but has no extractor yet.++ Until it is wired it must fail with a clean "not yet wired" error (and+ no traceback), rather than silently succeeding or crashing.+ """+ fake_ext = _fake_module(+ NERExtractor=lambda **kw: MagicMock(extract=lambda text, **kw2: []),+ RelationExtractor=lambda **kw: MagicMock(extract=lambda text, **kw2: []),+ TripletExtractor=lambda **kw: MagicMock(extract=lambda text, **kw2: []),+ EventDetector=lambda **kw: MagicMock(extract=lambda text, **kw2: []),+ )+ monkeypatch.setitem(+ __import__("sys").modules, "semantica.semantic_extract", fake_ext+ )+ result = runner.invoke(+ cli_module.main,+ ["extract", "Alice works at Acme.", "--mode", "coreference"],+ )+ assert result.exit_code != 0+ assert "Traceback" not in result.output+ assert "not yet wired" in _flatten(result.output)+def test_all_mode_reuses_ner_and_relations(self, runner, monkeypatch):"""#1789 (Qodo) — 'all' must not recompute NER/relations per stage."""calls = {"ner": 0, "relations": 0, "triplets": 0}diff --git a/tests/verify_rich_cli.py b/tests/verify_rich_cli.pyindex fc7e720f..ade59fd2 100644--- a/tests/verify_rich_cli.py+++ b/tests/verify_rich_cli.py@@ -154,10 +154,55 @@ else:# ─────────────────────────────────────────────────────────────────────────────print("GROUP: extract")help_ok("extract")-for mode in ["ner", "relations", "triplets", "events"]:++# Derive the mode list straight from the Click command's `choices` so this loop+# can never drift from the real set again. A hardcoded list that silently+# omitted `all` — the *default* mode — is exactly what let #1789 ship broken+# and go unnoticed (#1850).+_extract_mode_opt = next(+ p for p in cli_mod.extract.params if getattr(p, "name", None) == "mode"+)+EXTRACT_MODES = list(_extract_mode_opt.type.choices)+assert "all" in EXTRACT_MODES, (+ f"'all' (the default mode) missing from extract --mode choices: {EXTRACT_MODES}"+)+for mode in EXTRACT_MODES:code, _, _ = run(["extract", "--mode", mode, "--help"])- assert code == 0-ok("extract: all modes in --help")+ assert code == 0, f"extract --mode {mode} --help exited {code}"+ok(f"extract: every --mode choice in --help ({', '.join(EXTRACT_MODES)})")++# Exercise the default mode for real, not just at the --help level: #1789 broke+# `--mode all` on every bare invocation, so assert exit 0 and that all four+# stages land in the JSON payload. `--method pattern` keeps this hermetic+# (no model load / network) and fast.+code, _, cli = run(+ ["extract", "Alice works at Acme Corp.", "--mode", "all", "--json",+ "--method", "pattern"]+)+assert code == 0, f"extract --mode all --json exited {code}: {cli[:200]}"+# Progress log lines are interleaved into stdout (pre-existing), so pick the+# JSON object out rather than assuming the whole stream is JSON.+_json_lines = [ln for ln in cli.splitlines() if ln.lstrip().startswith("{")]+assert _json_lines, f"extract --mode all --json produced no JSON line: {cli[:200]}"+_payload = json.loads(_json_lines[-1])+assert set(_payload) == {"ner", "relations", "triplets", "events"}, (+ f"extract --mode all missing stages: {sorted(_payload)}"+)+ok("extract --mode all --json: exit 0 with ner/relations/triplets/events")++# `coreference` is listed in --mode's choices but has no runtime extractor yet,+# so it must fail loudly with a clean error instead of pretending to succeed+# (#1850). Drop this check once coreference is actually wired.+code, rich, _ = run(+ ["extract", "Alice works at Acme Corp.", "--mode", "coreference",+ "--method", "pattern"]+)+assert code != 0 and "not yet wired" in rich.lower(), (+ f"extract --mode coreference should be a clean placeholder error, "+ f"got exit {code}: {rich[:200]}"+)+ok("extract --mode coreference: clean 'not yet wired' placeholder error")+# NOTE: logging output pollutes --json stdout — pre-existing issueskip("extract --json ner", "pre-existing: log messages pollute JSON stdout")