Skip to content

Graph-Native Infrastructure for Context and Accountable AI Systems

fix(cli): resolve all review findings from PR #578

KaifAhmad1af697a83d8e1
diff --git a/semantica/cli.py b/semantica/cli.py
index 39397abf..77a67bb4 100644
--- a/semantica/cli.py
+++ b/semantica/cli.py
@@ -158,7 +158,7 @@ def _setup_cli_logging(
# Do not emit a warning: completion scripts and machine-readable
# commands must keep stdout/stderr stable when the default log file is
# unavailable. Explicitly configured log-file failures still raise.
- _ = (quiet, json_output, exc)
+ del quiet, json_output, exc # suppress unused-variable lint
def _get_framework(cli_ctx: CLIContext) -> "Semantica":
@@ -448,6 +448,18 @@ def build_alias(
_run_with_error_handling(_action)
+# ─── Graph store helper ──────────────────────────────────────────────────────
+
+
+def _get_graph_store(cli_ctx: CLIContext) -> Any:
+ """Return a GraphStore instance wired from the current CLIContext config."""
+ from .graph_store import GraphStore
+ cfg = cli_ctx.config.to_dict()
+ graph_db = dict(cfg.get("graph_db", {}))
+ backend = cli_ctx.store_backend or graph_db.pop("backend", "neo4j")
+ return GraphStore(backend=backend, **graph_db)
+
+
# ─── Output helpers ──────────────────────────────────────────────────────────
@@ -832,7 +844,7 @@ def normalize(cli_ctx: CLIContext, input_text: str, mode: str, domain: str,
def _action() -> None:
src = Path(input_text)
- text = src.read_text(encoding="utf-8") if src.exists() else input_text
+ text = src.read_text(encoding="utf-8") if src.is_file() else input_text
try:
from .normalize import normalize_text, normalize_date, normalize_entity
if mode == "temporal":
@@ -910,19 +922,19 @@ def extract(
raise click.ClickException(f"Extract module not available: {exc}") from exc
json_out = _is_json(cli_ctx, local_json) or fmt == "json"
if json_out:
- payload = result if isinstance(result, dict) else {"result": str(result)}
+ text_out = json.dumps(
+ result if isinstance(result, dict) else {"result": str(result)},
+ default=str,
+ )
elif fmt == "yaml":
- payload = None
- click.echo(yaml.dump(result, default_flow_style=False))
+ text_out = yaml.dump(result, default_flow_style=False)
else:
- payload = None
- console.print(result)
- if payload is not None:
- if output:
- Path(output).write_text(json.dumps(payload, default=str), encoding="utf-8")
- _ok(cli_ctx, f"Wrote {output}")
- else:
- _jecho(payload)
+ text_out = str(result)
+ if output:
+ Path(output).write_text(text_out, encoding="utf-8")
+ _ok(cli_ctx, f"Wrote {output}")
+ else:
+ click.echo(text_out)
_run_with_error_handling(_action)
@@ -1041,14 +1053,41 @@ def embed_index(cli_ctx: CLIContext, file_path: str, store: str,
def _action() -> None:
try:
+ import numpy as np
+ import pandas as pd
from .vector_store import create_index
- result = create_index(file_path, store=store, namespace=namespace)
except ImportError as exc:
raise click.ClickException(f"Vector store module not available: {exc}") from exc
+
+ fp = Path(file_path)
+ suffix = fp.suffix.lower()
+ if suffix == ".parquet":
+ df = pd.read_parquet(fp)
+ elif suffix in (".json", ".jsonl"):
+ df = pd.read_json(fp, lines=(suffix == ".jsonl"))
+ else:
+ raise click.ClickException(
+ f"Unsupported embeddings file format '{suffix}'. Use .parquet, .json, or .jsonl"
+ )
+
+ vector_col = next(
+ (c for c in df.columns if isinstance(df[c].iloc[0] if len(df) else None, (list, np.ndarray))),
+ None,
+ ) if len(df) else None
+ if vector_col is None:
+ raise click.ClickException("No vector column found in embeddings file")
+
+ id_col = next(
+ (c for c in df.columns if c != vector_col and df[c].dtype == object), None
+ )
+ vectors = [np.array(v, dtype=np.float32) for v in df[vector_col]]
+ ids = list(df[id_col].astype(str)) if id_col else None
+
+ result = create_index(vectors, ids=ids, store=store, namespace=namespace)
if _is_json(cli_ctx, local_json):
- _jecho(result if isinstance(result, dict) else {"status": "ok"})
+ _jecho(result if isinstance(result, dict) else {"status": "ok", "indexed": len(vectors)})
else:
- _ok(cli_ctx, f"Indexed {file_path} into {store}")
+ _ok(cli_ctx, f"Indexed {len(vectors)} vectors from {file_path} into {store}")
_run_with_error_handling(_action)
@@ -1132,13 +1171,25 @@ def deduplicate(
threshold=min_similarity,
**detection_kwargs,
)
- else:
- result = detect_duplicates(
+ else: # report — pairwise pairs with similarity scores
+ pairs = detect_duplicates(
entities,
- method="group",
+ method="pairwise",
similarity_threshold=min_similarity,
**detection_kwargs,
)
+ result = {
+ "total_entities": len(entities),
+ "duplicate_pairs": len(pairs),
+ "pairs": [
+ {
+ "entity_1": getattr(p, "entity1_id", None),
+ "entity_2": getattr(p, "entity2_id", None),
+ "similarity": getattr(p, "similarity_score", None),
+ }
+ for p in pairs
+ ],
+ }
except ImportError as exc:
raise click.ClickException(f"Deduplication module not available: {exc}") from exc
if output:
@@ -1265,7 +1316,13 @@ def reason_query(cli_ctx: CLIContext, query_str: str, with_inference: bool,
def reason_list(cli_ctx: CLIContext) -> None:
"""List available reasoning engines."""
cli_ctx = _require_ctx(cli_ctx)
- engines = ["deductive", "abductive", "rete", "forward-chain", "datalog", "sparql", "graph"]
+ _builtin: List[str] = ["deductive", "abductive", "rete", "forward-chain", "datalog", "sparql", "graph"]
+ try:
+ import semantica.reasoning as _reasoning_mod
+ _fn = getattr(_reasoning_mod, "get_available_engines", None)
+ engines: List[str] = list(_fn()) if callable(_fn) else _builtin
+ except ImportError:
+ engines = _builtin
if cli_ctx.json_output:
_jecho({"engines": engines})
else:
@@ -1311,16 +1368,29 @@ def decision_record(cli_ctx: CLIContext, title: str, tags: Optional[str],
title=title, tags=tag_list)
return
try:
- from .context import record_decision
+ from .context.decision_methods import record_decision
+ graph_store = _get_graph_store(cli_ctx)
+ cross_ctx: Dict[str, Any] = {"tags": tag_list}
+ if valid_from:
+ cross_ctx["valid_from"] = valid_from
+ if valid_until:
+ cross_ctx["valid_until"] = valid_until
+ # Map CLI flags to API: title→scenario, rationale→reasoning,
+ # first tag (if any)→category, outcome and confidence use defaults.
+ category = tag_list[0] if tag_list else "general"
result = record_decision(
- title=title, tags=tag_list,
- valid_from=valid_from, valid_until=valid_until,
- rationale=rationale,
+ graph_store,
+ category=category,
+ scenario=title,
+ reasoning=rationale or "",
+ outcome="recorded",
+ confidence=1.0,
+ cross_system_context=cross_ctx,
)
except ImportError as exc:
raise click.ClickException(f"Context module not available: {exc}") from exc
if _is_json(cli_ctx, local_json):
- _jecho(result if isinstance(result, dict) else {"id": str(result)})
+ _jecho({"id": str(result)})
else:
_ok(cli_ctx, f"Decision recorded: {result}")
@@ -1338,22 +1408,29 @@ def decision_list(cli_ctx: CLIContext, limit: int, fmt: str, local_json: bool) -
def _action() -> None:
try:
- from .context import DecisionQuery
- dq = DecisionQuery(config=cli_ctx.config.to_dict())
- results = dq.list(limit=limit)
+ from .context.decision_query import DecisionQuery
+ dq = DecisionQuery(_get_graph_store(cli_ctx))
+ results_raw = dq.find_by_time_range(
+ __import__("datetime").datetime.min,
+ __import__("datetime").datetime.now(),
+ limit=limit,
+ )
+ results = [
+ {"id": d.decision_id, "title": d.scenario, "tags": [d.category]}
+ for d in (results_raw or [])
+ ]
except ImportError as exc:
raise click.ClickException(f"Context module not available: {exc}") from exc
if _is_json(cli_ctx, local_json) or fmt == "json":
- _jecho(results if isinstance(results, list) else [])
+ _jecho(results)
else:
table = Table(title="Recent Decisions")
table.add_column("ID", style="cyan")
table.add_column("Title")
- table.add_column("Tags")
- for d in (results or []):
- if isinstance(d, dict):
- table.add_row(str(d.get("id", "")), str(d.get("title", "")),
- str(d.get("tags", "")))
+ table.add_column("Category")
+ for d in results:
+ table.add_row(str(d.get("id", "")), str(d.get("title", "")),
+ str(d.get("tags", [""])[0]))
console.print(table)
_run_with_error_handling(_action)
@@ -1372,13 +1449,22 @@ def decision_query(cli_ctx: CLIContext, filter_str: Optional[str],
def _action() -> None:
try:
- from .context import DecisionQuery
- dq = DecisionQuery(config=cli_ctx.config.to_dict())
- results = dq.query(filter=filter_str, since=since)
+ from .context.decision_query import DecisionQuery
+ import datetime as _dt
+ dq = DecisionQuery(_get_graph_store(cli_ctx))
+ since_dt = _dt.datetime.fromisoformat(since) if since else _dt.datetime.min
+ raw = dq.find_by_time_range(since_dt, _dt.datetime.now(), limit=500)
+ results: List[Dict[str, Any]] = [
+ {"id": d.decision_id, "category": d.category, "scenario": d.scenario,
+ "outcome": d.outcome, "confidence": d.confidence}
+ for d in (raw or [])
+ if filter_str is None
+ or filter_str.lstrip("tag:").lower() in d.category.lower()
+ ]
except ImportError as exc:
raise click.ClickException(f"Context module not available: {exc}") from exc
if _is_json(cli_ctx, local_json) or fmt == "json":
- _jecho(results if isinstance(results, list) else [])
+ _jecho(results)
else:
console.print(results)
@@ -1402,12 +1488,16 @@ def decision_trace(cli_ctx: CLIContext, decision_id: str, fmt: str, local_json:
def _action() -> None:
try:
- from .context import get_causal_chain
- chain = get_causal_chain(decision_id, config=cli_ctx.config.to_dict())
+ from .context.decision_methods import get_causal_chain
+ chain_raw = get_causal_chain(_get_graph_store(cli_ctx), decision_id)
+ chain: Any = [
+ {"id": d.decision_id, "scenario": d.scenario, "outcome": d.outcome}
+ for d in chain_raw
+ ]
except ImportError as exc:
raise click.ClickException(f"Context module not available: {exc}") from exc
if _is_json(cli_ctx, local_json) or fmt == "json":
- _jecho(chain if isinstance(chain, dict) else {"chain": str(chain)})
+ _jecho(chain if isinstance(chain, (dict, list)) else {"chain": str(chain)})
else:
console.print(chain)
@@ -1425,13 +1515,17 @@ def decision_similar(cli_ctx: CLIContext, decision_id: str, top_k: int, local_js
def _action() -> None:
try:
- from .context import find_precedents
- results = find_precedents(decision_id, top_k=top_k,
- config=cli_ctx.config.to_dict())
+ from .context.decision_methods import find_precedents
+ raw = find_precedents(_get_graph_store(cli_ctx), decision_id, limit=top_k)
+ results: List[Dict[str, Any]] = [
+ {"id": d.decision_id, "scenario": d.scenario, "category": d.category,
+ "confidence": d.confidence}
+ for d in (raw or [])
+ ]
except ImportError as exc:
raise click.ClickException(f"Context module not available: {exc}") from exc
if _is_json(cli_ctx, local_json):
- _jecho(results if isinstance(results, list) else [])
+ _jecho(results)
else:
console.print(results)
@@ -1448,12 +1542,12 @@ def decision_impact(cli_ctx: CLIContext, decision_id: str, local_json: bool) ->
def _action() -> None:
try:
- from .context import analyze_decision_impact
- result = analyze_decision_impact(decision_id, config=cli_ctx.config.to_dict())
+ from .context.decision_methods import analyze_decision_impact
+ result = analyze_decision_impact(_get_graph_store(cli_ctx), decision_id)
except ImportError as exc:
raise click.ClickException(f"Context module not available: {exc}") from exc
if _is_json(cli_ctx, local_json):
- _jecho(result if isinstance(result, dict) else {"impact": str(result)})
+ _jecho(result)
else:
console.print(result)
@@ -1473,14 +1567,17 @@ def decision_check(cli_ctx: CLIContext, decision_id: str, rules: Optional[str],
def _action() -> None:
try:
- from .context import check_decision_compliance
+ from .context.decision_methods import check_decision_compliance
+ # The API expects a policy_id string; derive it from the rules file stem
+ # if provided, otherwise use the decision ID itself as the policy key.
+ policy_id = Path(rules).stem if rules else decision_id
result = check_decision_compliance(
- decision_id, rules_file=rules, config=cli_ctx.config.to_dict()
+ _get_graph_store(cli_ctx), decision_id, policy_id
)
except ImportError as exc:
raise click.ClickException(f"Context module not available: {exc}") from exc
if _is_json(cli_ctx, local_json):
- _jecho(result if isinstance(result, dict) else {"compliant": str(result)})
+ _jecho(result)
else:
console.print(result)
@@ -2283,12 +2380,18 @@ def _viz_command(name: str, fn_name: str, title: str) -> None:
)
except ImportError as exc:
raise click.ClickException(f"Visualization module not available: {exc}") from exc
- out_path = output or f"{name}.{fmt}"
- if isinstance(result, str):
- Path(out_path).write_text(result, encoding="utf-8")
- elif isinstance(result, bytes):
- Path(out_path).write_bytes(result)
- _ok(cli_ctx, f"Wrote {out_path}")
+ if output:
+ if isinstance(result, bytes):
+ Path(output).write_bytes(result)
+ else:
+ Path(output).write_text(str(result), encoding="utf-8")
+ _ok(cli_ctx, f"Wrote {output}")
+ else:
+ # No --output: emit to stdout so the caller can redirect or pipe.
+ if isinstance(result, bytes):
+ sys.stdout.buffer.write(result)
+ else:
+ click.echo(result)
_run_with_error_handling(_action)
@@ -2607,6 +2710,58 @@ def backup(ctx: click.Context) -> None:
click.echo(ctx.get_help())
+def _collect_backup_sources(
+ cfg: Dict[str, Any],
+ config_path: Optional[str],
+ include: str,
+ strip_config: bool,
+) -> List[Tuple[str, Path]]:
+ """Return (arcname, local_path) pairs for local files to include in a backup."""
+ sources: List[Tuple[str, Path]] = []
+
+ if not strip_config and include in ("config", "all"):
+ candidates: List[str] = [c for c in [config_path, "semantica.yaml", "semantica.yml"] if c]
+ for c in candidates:
+ p = Path(c)
+ if p.is_file():
+ sources.append(("config/semantica.yaml", p))
+ break
+
+ if include in ("ontology", "all"):
+ _ont: Any = cfg.get("ontology_path") or (cfg.get("ontology") or {}).get("path")
+ ont_path: Optional[str] = str(_ont) if _ont else None
+ if ont_path:
+ p = Path(ont_path)
+ if p.is_dir():
+ for f in p.rglob("*"):
+ if f.is_file():
+ sources.append((f"ontology/{f.relative_to(p)}", f))
+ elif p.is_file():
+ sources.append((f"ontology/{p.name}", p))
+
+ store_map = {"graph": "graph_db", "vector": "vector_store", "triplet": "triplet_store"}
+ for store_type, cfg_key in store_map.items():
+ if include not in (store_type, "all"):
+ continue
+ info = cfg.get(cfg_key)
+ if not isinstance(info, dict):
+ continue
+ # Only file-addressable backends have a local path key
+ _dp: Any = info.get("path") or info.get("data_path") or info.get("index_path")
+ data_path: Optional[str] = str(_dp) if _dp else None
+ if not data_path:
+ continue
+ p = Path(data_path)
+ if p.is_dir():
+ for f in p.rglob("*"):
+ if f.is_file():
+ sources.append((f"{store_type}/{f.relative_to(p)}", f))
+ elif p.is_file():
+ sources.append((f"{store_type}/{p.name}", p))
+
+ return sources
+
+
def _redact(uri: str) -> str:
"""Redact passwords/tokens from a connection URI."""
import re
@@ -2710,6 +2865,8 @@ def backup_create(
f"Keyfile {keyfile} is not owned by the current user."
)
passphrase = kp.read_text(encoding="utf-8").strip()
+ if not passphrase:
+ raise click.ClickException(f"Keyfile {keyfile} is empty; cannot derive encryption key.")
elif encrypt:
passphrase = click.prompt("Backup passphrase", hide_input=True,
confirmation_prompt=True)
@@ -2729,17 +2886,24 @@ def backup_create(
dest.parent.mkdir(parents=True, exist_ok=True)
import tarfile, tempfile, shutil
+ sources = _collect_backup_sources(cfg, cli_ctx.config_path, include, strip_config)
with tempfile.TemporaryDirectory() as tmp:
tmp_path = Path(tmp)
+ manifest: Dict[str, Any] = {
+ "include": include,
+ "version": str(__version__),
+ "files": [arcname for arcname, _ in sources],
+ }
(tmp_path / "backup_manifest.json").write_text(
- json.dumps({"include": include, "version": __version__}, indent=2),
- encoding="utf-8",
+ json.dumps(manifest, indent=2), encoding="utf-8",
)
# Build the tar archive in a temp file; we'll compress/encrypt on top.
raw_tar = Path(tmp) / "semantica-backup.tar"
with tarfile.open(str(raw_tar), "w") as tar:
- tar.add(tmp_path, arcname="semantica-backup",
- filter=lambda m: m if m.name != "semantica-backup.tar" else None)
+ tar.add(str(tmp_path / "backup_manifest.json"),
+ arcname="semantica-backup/backup_manifest.json")
+ for arcname, local_path in sources:
+ tar.add(str(local_path), arcname=f"semantica-backup/{arcname}")
# Compress (gzip) if requested.
if compress:
@@ -2811,17 +2975,30 @@ def backup_sync(cli_ctx: CLIContext, destination: str, include: str,
quiet = cli_ctx.quiet or local_quiet
def _action() -> None:
+ import shutil
dest = Path(destination)
+ cfg = cli_ctx.config.to_dict()
+ sources = _collect_backup_sources(cfg, cli_ctx.config_path, include, strip_config=False)
if _is_dry(cli_ctx, local_dry):
- _dry(cli_ctx, "sync backup", destination=str(dest), include=include)
+ _dry(cli_ctx, "sync backup", destination=str(dest), include=include,
+ files=[a for a, _ in sources])
return
dest.mkdir(parents=True, exist_ok=True)
try:
os.chmod(str(dest), 0o700)
except OSError:
pass
+ copied = 0
+ for arcname, src_path in sources:
+ dst_file = dest / arcname
+ dst_file.parent.mkdir(parents=True, exist_ok=True)
+ # Incremental: skip if destination is up-to-date
+ if dst_file.exists() and dst_file.stat().st_mtime >= src_path.stat().st_mtime:
+ continue
+ shutil.copy2(str(src_path), str(dst_file))
+ copied += 1
if not quiet:
- _ok(cli_ctx, f"Synced to {destination}")
+ _ok(cli_ctx, f"Synced {copied} file(s) to {destination}")
_run_with_error_handling(_action)
@@ -2840,10 +3017,67 @@ def backup_restore(cli_ctx: CLIContext, source: str, local_dry: bool) -> None:
cli_ctx = _require_ctx(cli_ctx)
def _action() -> None:
+ import shutil, tarfile as _tf
+ src = Path(source)
if _is_dry(cli_ctx, local_dry):
- _dry(cli_ctx, "restore", source=source)
+ _dry(cli_ctx, "restore", source=str(src))
return
- _ok(cli_ctx, f"Restored from {source}")
+
+ # Determine payload: decrypt .enc first, then extract archive or copy dir.
+ work_path = src
+ if src.suffix == ".enc":
+ passphrase = click.prompt("Backup passphrase", hide_input=True)
+ try:
+ from cryptography.hazmat.primitives.kdf.pbkdf2 import PBKDF2HMAC
+ from cryptography.hazmat.primitives import hashes
+ from cryptography.hazmat.primitives.ciphers.aead import AESGCM
+ except ImportError as exc:
+ raise click.ClickException(
+ "Decryption requires the 'cryptography' package: pip install cryptography"
+ ) from exc
+ raw = src.read_bytes()
+ if not raw.startswith(b"SEM1") or len(raw) < 4 + 32 + 12:
+ raise click.ClickException("Unrecognised or corrupted backup file.")
+ salt, nonce, ciphertext = raw[4:36], raw[36:48], raw[48:]
+ kdf = PBKDF2HMAC(algorithm=hashes.SHA256(), length=32, salt=salt, iterations=480_000)
+ key = kdf.derive(passphrase.encode())
+ try:
+ plaintext = AESGCM(key).decrypt(nonce, ciphertext, None)
+ except Exception as exc:
+ raise click.ClickException("Decryption failed — wrong passphrase?") from exc
+ import tempfile
+ tmp_enc = tempfile.NamedTemporaryFile(delete=False, suffix=".tar.gz")
+ tmp_enc.write(plaintext)
+ tmp_enc.close()
+ work_path = Path(tmp_enc.name)
+
+ try:
+ if _tf.is_tarfile(str(work_path)):
+ restore_root = Path.cwd()
+ with _tf.open(str(work_path), "r:*") as tar:
+ # Dry-run listing was already handled above; extract now
+ for member in tar.getmembers():
+ # Strip the leading "semantica-backup/" prefix
+ member.name = member.name.replace("semantica-backup/", "", 1)
+ if member.name:
+ tar.extract(member, path=str(restore_root))
+ console.print(f" restored: {member.name}")
+ elif src.is_dir():
+ restore_root = Path.cwd()
+ for f in src.rglob("*"):
+ if f.is_file():
+ rel = f.relative_to(src)
+ dst = restore_root / rel
+ dst.parent.mkdir(parents=True, exist_ok=True)
+ shutil.copy2(str(f), str(dst))
+ console.print(f" restored: {rel}")
+ else:
+ raise click.ClickException(f"Cannot restore from {source}: not a tar archive or directory.")
+ finally:
+ if src.suffix == ".enc":
+ work_path.unlink(missing_ok=True)
+
+ _ok(cli_ctx, f"Restore complete from {source}")
_run_with_error_handling(_action)
diff --git a/tests/test_cli_commands.py b/tests/test_cli_commands.py
index a087614f..08790222 100644
--- a/tests/test_cli_commands.py
+++ b/tests/test_cli_commands.py
@@ -24,7 +24,6 @@ import pytest
from click.testing import CliRunner
import semantica.cli as cli_module
-from semantica.cli import main
# ─── fixtures ─────────────────────────────────────────────────────────────────
@@ -81,7 +80,7 @@ class TestGlobalFlags:
captured["json"] = ctx.json_output
monkeypatch.setattr(cli_module, "_run_build", fake_run_build)
- result = runner.invoke(main, ["--json", "kg", "build", "-s", "x.txt"])
+ result = runner.invoke(cli_module.main, ["--json", "kg", "build", "-s", "x.txt"])
assert result.exit_code == 0
assert captured["json"] is True
@@ -92,7 +91,7 @@ class TestGlobalFlags:
captured["quiet"] = ctx.quiet
monkeypatch.setattr(cli_module, "_run_build", fake_run_build)
- result = runner.invoke(main, ["--quiet", "kg", "build", "-s", "x.txt"])
+ result = runner.invoke(cli_module.main, ["--quiet", "kg", "build", "-s", "x.txt"])
assert result.exit_code == 0
assert captured["quiet"] is True
@@ -103,7 +102,7 @@ class TestGlobalFlags:
captured["dry_run"] = ctx.dry_run_global
monkeypatch.setattr(cli_module, "_run_build", fake_run_build)
- result = runner.invoke(main, ["--dry-run", "kg", "build", "-s", "x.txt"])
+ result = runner.invoke(cli_module.main, ["--dry-run", "kg", "build", "-s", "x.txt"])
assert result.exit_code == 0
assert captured["dry_run"] is True
@@ -114,7 +113,7 @@ class TestGlobalFlags:
captured["store"] = ctx.store_backend
monkeypatch.setattr(cli_module, "_run_build", fake_run_build)
- result = runner.invoke(main, ["--store", "neo4j", "kg", "build", "-s", "x.txt"])
+ result = runner.invoke(cli_module.main, ["--store", "neo4j", "kg", "build", "-s", "x.txt"])
assert result.exit_code == 0
assert captured["store"] == "neo4j"
@@ -125,19 +124,19 @@ class TestGlobalFlags:
captured["vs"] = ctx.vector_store_backend
monkeypatch.setattr(cli_module, "_run_build", fake_run_build)
- result = runner.invoke(main, ["--vector-store", "qdrant", "kg", "build", "-s", "x.txt"])
+ result = runner.invoke(cli_module.main, ["--vector-store", "qdrant", "kg", "build", "-s", "x.txt"])
assert result.exit_code == 0
assert captured["vs"] == "qdrant"
def test_root_help_shows_all_global_flags(self, runner):
- result = runner.invoke(main, ["--help"])
+ result = runner.invoke(cli_module.main, ["--help"])
assert result.exit_code == 0
for flag in ["--json", "--quiet", "--dry-run", "--store", "--vector-store",
"--profile", "--no-color"]:
assert flag in result.output, f"{flag} missing from root help"
def test_root_help_shows_all_command_groups(self, runner):
- result = runner.invoke(main, ["--help"])
+ result = runner.invoke(cli_module.main, ["--help"])
assert result.exit_code == 0
for cmd in ["ingest", "parse", "split", "normalize", "extract", "embed",
"deduplicate", "reason", "decision", "temporal", "provenance",
@@ -159,7 +158,7 @@ class TestGlobalFlags:
monkeypatch.setattr(cli_module, "setup_logging", fake_setup_logging)
result = runner.invoke(
- main,
+ cli_module.main,
["--json", "backup", "schedule", "--dest", "x", "--freq", "daily"],
)
@@ -177,7 +176,7 @@ class TestKgSubcommands:
@pytest.mark.parametrize("sub", ["query", "stats", "analyze", "find-path",
"resolve", "predict", "validate"])
def test_help_exits_0(self, runner, sub):
- result = runner.invoke(main, ["kg", sub, "--help"])
+ result = runner.invoke(cli_module.main, ["kg", sub, "--help"])
_ok(result, substr=sub.replace("-", " ") if sub != "find-path" else "")
assert result.exit_code == 0
@@ -186,13 +185,13 @@ class TestKgSubcommands:
execute_query=lambda q, **kw: {"query": q, "lang": "cypher", "rows": []},
)
monkeypatch.setitem(__import__("sys").modules, "semantica.graph_store", fake_gs)
- result = runner.invoke(main, ["kg", "query", "MATCH (n) RETURN n", "--json"])
+ result = runner.invoke(cli_module.main, ["kg", "query", "MATCH (n) RETURN n", "--json"])
_ok(result)
data = _json_output(result)
assert "query" in data
def test_kg_query_fails_cleanly_without_backend(self, runner):
- result = runner.invoke(main, ["kg", "query", "MATCH (n) RETURN n"])
+ result = runner.invoke(cli_module.main, ["kg", "query", "MATCH (n) RETURN n"])
# Either exits 0 (fallback) or non-0 (clean error) — never traceback
assert "Traceback" not in result.output
@@ -203,7 +202,7 @@ class TestKgSubcommands:
),
)
monkeypatch.setitem(__import__("sys").modules, "semantica.kg", fake_kg)
- result = runner.invoke(main, ["kg", "stats", "--json"])
+ result = runner.invoke(cli_module.main, ["kg", "stats", "--json"])
_ok(result)
data = _json_output(result)
assert isinstance(data, dict)
@@ -216,13 +215,13 @@ class TestKgSubcommands:
),
)
monkeypatch.setitem(__import__("sys").modules, "semantica.kg", fake_kg)
- result = runner.invoke(main, ["kg", "analyze", "--mode", "community", "--json"])
+ result = runner.invoke(cli_module.main, ["kg", "analyze", "--mode", "community", "--json"])
_ok(result)
data = _json_output(result)
assert isinstance(data, dict)
def test_kg_find_path_requires_from_and_to(self, runner):
- result = runner.invoke(main, ["kg", "find-path"])
+ result = runner.invoke(cli_module.main, ["kg", "find-path"])
assert result.exit_code != 0
def test_kg_find_path_json_with_mock(self, runner, monkeypatch):
@@ -232,7 +231,7 @@ class TestKgSubcommands:
),
)
monkeypatch.setitem(__import__("sys").modules, "semantica.kg", fake_kg)
- result = runner.invoke(main, ["kg", "find-path",
+ result = runner.invoke(cli_module.main, ["kg", "find-path",
"--from", "Alice", "--to", "Acme", "--json"])
_ok(result)
data = _json_output(result)
@@ -243,7 +242,7 @@ class TestKgSubcommands:
EntityResolver=lambda **kw: MagicMock(resolve=lambda: {"resolved": 5}),
)
monkeypatch.setitem(__import__("sys").modules, "semantica.kg", fake_kg)
- result = runner.invoke(main, ["kg", "resolve"])
+ result = runner.invoke(cli_module.main, ["kg", "resolve"])
assert result.exit_code == 0
def test_kg_predict_exits_0_with_mock(self, runner, monkeypatch):
@@ -251,7 +250,7 @@ class TestKgSubcommands:
LinkPredictor=lambda **kw: MagicMock(predict=lambda: {"predictions": []}),
)
monkeypatch.setitem(__import__("sys").modules, "semantica.kg", fake_kg)
- result = runner.invoke(main, ["kg", "predict"])
+ result = runner.invoke(cli_module.main, ["kg", "predict"])
assert result.exit_code == 0
def test_kg_validate_exits_0_with_mock(self, runner, monkeypatch):
@@ -262,7 +261,7 @@ class TestKgSubcommands:
),
)
monkeypatch.setitem(__import__("sys").modules, "semantica.kg", fake_kg)
- result = runner.invoke(main, ["kg", "validate"])
+ result = runner.invoke(cli_module.main, ["kg", "validate"])
assert result.exit_code == 0
@@ -271,7 +270,7 @@ class TestKgSubcommands:
class TestIngest:
def test_help_shows_flags(self, runner):
- result = runner.invoke(main, ["ingest", "--help"])
+ result = runner.invoke(cli_module.main, ["ingest", "--help"])
_ok(result)
for flag in ["--type", "--format", "--recursive", "--watch",
"--batch-size", "--store", "--output", "--dry-run"]:
@@ -279,7 +278,7 @@ class TestIngest:
def test_dry_run_json_exits_0(self, runner):
# Per-command --json with --dry-run should emit JSON (fixed via json_out param)
- result = runner.invoke(main, ["ingest", "data.pdf", "--dry-run", "--json"])
+ result = runner.invoke(cli_module.main, ["ingest", "data.pdf", "--dry-run", "--json"])
_ok(result)
data = _json_output(result)
assert data["dry_run"] is True
@@ -287,13 +286,13 @@ class TestIngest:
def test_dry_run_global_json_exits_0(self, runner):
# Global --json with per-command --dry-run
- result = runner.invoke(main, ["--json", "ingest", "data.pdf", "--dry-run"])
+ result = runner.invoke(cli_module.main, ["--json", "ingest", "data.pdf", "--dry-run"])
_ok(result)
data = _json_output(result)
assert data["dry_run"] is True
def test_dry_run_text_exits_0(self, runner):
- result = runner.invoke(main, ["ingest", "data.pdf", "--dry-run"])
+ result = runner.invoke(cli_module.main, ["ingest", "data.pdf", "--dry-run"])
_ok(result, substr="Dry run")
def test_runtime_path_passes_source_positionally(self, runner, monkeypatch):
@@ -307,7 +306,7 @@ class TestIngest:
monkeypatch.setattr("semantica.ingest.methods.ingest_file", fake_ingest_file)
result = runner.invoke(
- main,
+ cli_module.main,
["ingest", "README.md", "--type", "file", "--format", "csv", "--json"],
)
@@ -329,7 +328,7 @@ class TestIngest:
monkeypatch.setattr("semantica.ingest.methods.ingest_file", fake_ingest_file)
- result = runner.invoke(main, ["ingest", "README.md", "--json"])
+ result = runner.invoke(cli_module.main, ["ingest", "README.md", "--json"])
_ok(result)
data = _json_output(result)
@@ -344,21 +343,21 @@ class TestIngest:
(_ for _ in ()).throw(ImportError(n))
if n.startswith("semantica.ingest") else original_import(n, *a, **k)
)):
- result = runner.invoke(main, ["ingest", "data.pdf"])
+ result = runner.invoke(cli_module.main, ["ingest", "data.pdf"])
assert result.exit_code != 0
assert "Traceback" not in result.output
def test_type_choice_validation(self, runner):
- result = runner.invoke(main, ["ingest", "x.pdf", "--type", "invalid_type"])
+ result = runner.invoke(cli_module.main, ["ingest", "x.pdf", "--type", "invalid_type"])
assert result.exit_code != 0
def test_recursive_flag_accepted(self, runner):
- result = runner.invoke(main, ["ingest", "./data", "--recursive", "--dry-run"])
+ result = runner.invoke(cli_module.main, ["ingest", "./data", "--recursive", "--dry-run"])
_ok(result)
def test_global_dry_run_triggers_ingest_dry(self, runner):
# Both global --dry-run and global --json
- result = runner.invoke(main, ["--dry-run", "--json", "ingest", "data.pdf"])
+ result = runner.invoke(cli_module.main, ["--dry-run", "--json", "ingest", "data.pdf"])
_ok(result)
data = _json_output(result)
assert data["dry_run"] is True
@@ -369,17 +368,17 @@ class TestIngest:
class TestParse:
def test_help_shows_flags(self, runner):
- result = runner.invoke(main, ["parse", "--help"])
+ result = runner.invoke(cli_module.main, ["parse", "--help"])
_ok(result)
for flag in ["--parser", "--format"]:
assert flag in result.output
def test_missing_file_arg_fails(self, runner):
- result = runner.invoke(main, ["parse"])
+ result = runner.invoke(cli_module.main, ["parse"])
assert result.exit_code != 0
def test_nonexistent_file_fails(self, runner):
- result = runner.invoke(main, ["parse", "no_such_file.pdf"])
+ result = runner.invoke(cli_module.main, ["parse", "no_such_file.pdf"])
assert result.exit_code != 0
def test_parse_real_file(self, runner):
@@ -390,12 +389,12 @@ class TestParse:
(_ for _ in ()).throw(ImportError(n))
if n.startswith("semantica.parse") else __import__(n, *a, **k)
)):
- result = runner.invoke(main, ["parse", "doc.txt"])
+ result = runner.invoke(cli_module.main, ["parse", "doc.txt"])
assert result.exit_code != 0
assert "Traceback" not in result.output
def test_format_choices(self, runner):
- result = runner.invoke(main, ["parse", "--help"])
+ result = runner.invoke(cli_module.main, ["parse", "--help"])
assert "json" in result.output
assert "yaml" in result.output
assert "table" in result.output
@@ -406,13 +405,13 @@ class TestParse:
class TestSplit:
def test_help_shows_strategy_choices(self, runner):
- result = runner.invoke(main, ["split", "--help"])
+ result = runner.invoke(cli_module.main, ["split", "--help"])
_ok(result)
for strategy in ["recursive", "semantic", "entity-aware", "table"]:
assert strategy in result.output
def test_missing_input_fails(self, runner):
- result = runner.invoke(main, ["split"])
+ result = runner.invoke(cli_module.main, ["split"])
assert result.exit_code != 0
def test_split_with_import_error(self, runner):
@@ -423,7 +422,7 @@ class TestSplit:
(_ for _ in ()).throw(ImportError(n))
if n.startswith("semantica.split") else __import__(n, *a, **k)
)):
- result = runner.invoke(main, ["split", "doc.txt"])
+ result = runner.invoke(cli_module.main, ["split", "doc.txt"])
assert result.exit_code != 0
assert "Traceback" not in result.output
@@ -441,7 +440,7 @@ class TestSplit:
with open("doc.txt", "w") as f:
f.write("line1\nline2")
result = runner.invoke(
- main, ["split", "doc.txt", "--output", "out.json"]
+ cli_module.main, ["split", "doc.txt", "--output", "out.json"]
)
if result.exit_code == 0:
assert os.path.exists("out.json")
@@ -452,7 +451,7 @@ class TestSplit:
class TestNormalize:
def test_help_shows_mode_and_domain(self, runner):
- result = runner.invoke(main, ["normalize", "--help"])
+ result = runner.invoke(cli_module.main, ["normalize", "--help"])
_ok(result)
assert "--mode" in result.output
assert "--domain" in result.output
@@ -466,7 +465,7 @@ class TestNormalize:
monkeypatch.setitem(
__import__("sys").modules, "semantica.normalize", fake_norm
)
- result = runner.invoke(main, ["normalize", "hello world", "--mode", "text"])
+ result = runner.invoke(cli_module.main, ["normalize", "hello world", "--mode", "text"])
assert result.exit_code == 0
assert "HELLO WORLD" in result.output
@@ -479,13 +478,13 @@ class TestNormalize:
monkeypatch.setitem(
__import__("sys").modules, "semantica.normalize", fake_norm
)
- result = runner.invoke(main, ["normalize", "text", "--json"])
+ result = runner.invoke(cli_module.main, ["normalize", "text", "--json"])
_ok(result)
data = _json_output(result)
assert "result" in data
def test_domain_choices(self, runner):
- result = runner.invoke(main, ["normalize", "--help"])
+ result = runner.invoke(cli_module.main, ["normalize", "--help"])
for d in ["healthcare", "legal", "finance", "general"]:
assert d in result.output
@@ -495,7 +494,7 @@ class TestNormalize:
class TestExtract:
def test_help_shows_mode_method_flags(self, runner):
- result = runner.invoke(main, ["extract", "--help"])
+ result = runner.invoke(cli_module.main, ["extract", "--help"])
_ok(result)
for flag in ["--mode", "--method", "--model", "--confidence",
"--temporal", "--format", "--output"]:
@@ -510,7 +509,7 @@ class TestExtract:
monkeypatch.setitem(
__import__("sys").modules, "semantica.semantic_extract", fake_ext
)
- result = runner.invoke(main, ["extract", "Alice works at Acme.", "--json"])
+ result = runner.invoke(cli_module.main, ["extract", "Alice works at Acme.", "--json"])
_ok(result)
data = _json_output(result)
assert isinstance(data, dict)
@@ -524,7 +523,7 @@ class TestExtract:
monkeypatch.setitem(
__import__("sys").modules, "semantica.semantic_extract", fake_ext
)
- result = runner.invoke(main, ["extract", "-", "--json"], input="Alice\n")
+ result = runner.invoke(cli_module.main, ["extract", "-", "--json"], input="Alice\n")
_ok(result)
def test_import_error_is_clean(self, runner):
@@ -532,12 +531,12 @@ class TestExtract:
(_ for _ in ()).throw(ImportError(n))
if n.startswith("semantica.semantic_extract") else __import__(n, *a, **k)
)):
- result = runner.invoke(main, ["extract", "text"])
+ result = runner.invoke(cli_module.main, ["extract", "text"])
assert result.exit_code != 0
assert "Traceback" not in result.output
def test_invalid_method_choice(self, runner):
- result = runner.invoke(main, ["extract", "text", "--method", "magic"])
+ result = runner.invoke(cli_module.main, ["extract", "text", "--method", "magic"])
assert result.exit_code != 0
@@ -546,24 +545,24 @@ class TestExtract:
class TestEmbed:
def test_group_help(self, runner):
- result = runner.invoke(main, ["embed", "--help"])
+ result = runner.invoke(cli_module.main, ["embed", "--help"])
_ok(result)
for sub in ["generate", "search", "index"]:
assert sub in result.output
def test_generate_help(self, runner):
- result = runner.invoke(main, ["embed", "generate", "--help"])
+ result = runner.invoke(cli_module.main, ["embed", "generate", "--help"])
_ok(result)
assert "--model" in result.output
def test_search_help(self, runner):
- result = runner.invoke(main, ["embed", "search", "--help"])
+ result = runner.invoke(cli_module.main, ["embed", "search", "--help"])
_ok(result)
assert "--top-k" in result.output
assert "--hybrid" in result.output
def test_index_help(self, runner):
- result = runner.invoke(main, ["embed", "index", "--help"])
+ result = runner.invoke(cli_module.main, ["embed", "index", "--help"])
_ok(result)
assert "--store" in result.output
@@ -572,7 +571,7 @@ class TestEmbed:
(_ for _ in ()).throw(ImportError(n))
if "embeddings" in n else __import__(n, *a, **k)
)):
- result = runner.invoke(main, ["embed", "generate", "entities.json"])
+ result = runner.invoke(cli_module.main, ["embed", "generate", "entities.json"])
assert result.exit_code != 0
assert "Traceback" not in result.output
@@ -581,12 +580,42 @@ class TestEmbed:
(_ for _ in ()).throw(ImportError(n))
if "vector_store" in n else __import__(n, *a, **k)
)):
- result = runner.invoke(main, ["embed", "search", "CEO query"])
+ result = runner.invoke(cli_module.main, ["embed", "search", "CEO query"])
assert result.exit_code != 0
assert "Traceback" not in result.output
def test_index_requires_existing_file(self, runner):
- result = runner.invoke(main, ["embed", "index", "nonexistent.parquet"])
+ result = runner.invoke(cli_module.main, ["embed", "index", "nonexistent.parquet"])
+ assert result.exit_code != 0
+
+ def test_index_loads_vectors_from_json(self, runner, monkeypatch, tmp_path):
+ """Vectors are loaded from the file; create_index receives List[np.ndarray]."""
+ import json as _json, numpy as np
+ records = [{"id": "a", "embedding": [0.1, 0.2, 0.3]},
+ {"id": "b", "embedding": [0.4, 0.5, 0.6]}]
+ json_file = tmp_path / "test.json"
+ json_file.write_text(_json.dumps(records), encoding="utf-8")
+
+ captured = {}
+
+ def fake_create_index(vectors, ids=None, **kw):
+ captured["vectors"] = vectors
+ captured["ids"] = ids
+ return {"status": "ok"}
+
+ fake_vs = _fake_module(create_index=fake_create_index)
+ monkeypatch.setitem(__import__("sys").modules, "semantica.vector_store", fake_vs)
+ result = runner.invoke(cli_module.main, ["embed", "index", str(json_file), "--json"])
+ _ok(result)
+ assert len(captured["vectors"]) == 2
+ assert isinstance(captured["vectors"][0], np.ndarray)
+
+ def test_index_rejects_unsupported_format(self, runner, monkeypatch, tmp_path):
+ txt_file = tmp_path / "embeddings.txt"
+ txt_file.write_text("not a supported format")
+ fake_vs = _fake_module(create_index=MagicMock(return_value={}))
+ monkeypatch.setitem(__import__("sys").modules, "semantica.vector_store", fake_vs)
+ result = runner.invoke(cli_module.main, ["embed", "index", str(txt_file)])
assert result.exit_code != 0
@@ -595,17 +624,17 @@ class TestEmbed:
class TestDeduplicate:
def test_help_shows_flags(self, runner):
- result = runner.invoke(main, ["deduplicate", "--help"])
+ result = runner.invoke(cli_module.main, ["deduplicate", "--help"])
_ok(result)
for flag in ["--strategy", "--min-similarity", "--action", "--dry-run"]:
assert flag in result.output
def test_dry_run_exits_0(self, runner):
- result = runner.invoke(main, ["deduplicate", "--dry-run"])
+ result = runner.invoke(cli_module.main, ["deduplicate", "--dry-run"])
_ok(result)
def test_dry_run_json(self, runner):
- result = runner.invoke(main, ["deduplicate", "--dry-run", "--json"])
+ result = runner.invoke(cli_module.main, ["deduplicate", "--dry-run", "--json"])
_ok(result)
data = _json_output(result)
assert data["dry_run"] is True
@@ -625,7 +654,7 @@ class TestDeduplicate:
monkeypatch.setattr("semantica.graph_store.methods.get_nodes", lambda **kwargs: entities)
result = runner.invoke(
- main,
+ cli_module.main,
["deduplicate", "--action", "detect", "--min-similarity", "0.1", "--json"],
)
@@ -658,7 +687,7 @@ class TestDeduplicate:
)
result = runner.invoke(
- main,
+ cli_module.main,
["deduplicate", "--action", "merge", "--json"],
)
@@ -671,7 +700,7 @@ class TestDeduplicate:
assert captured["kwargs"]["sort_by"] == "similarity_score"
def test_global_dry_run_triggers_dry(self, runner):
- result = runner.invoke(main, ["--dry-run", "--json", "deduplicate"])
+ result = runner.invoke(cli_module.main, ["--dry-run", "--json", "deduplicate"])
_ok(result)
data = _json_output(result)
assert data["dry_run"] is True
@@ -681,17 +710,17 @@ class TestDeduplicate:
(_ for _ in ()).throw(ImportError(n))
if "deduplication" in n else __import__(n, *a, **k)
)):
- result = runner.invoke(main, ["deduplicate"])
+ result = runner.invoke(cli_module.main, ["deduplicate"])
assert result.exit_code != 0
assert "Traceback" not in result.output
def test_invalid_strategy_choice(self, runner):
- result = runner.invoke(main, ["deduplicate", "--strategy", "magic"])
+ result = runner.invoke(cli_module.main, ["deduplicate", "--strategy", "magic"])
assert result.exit_code != 0
@pytest.mark.parametrize("action", ["detect", "merge", "report"])
def test_action_choices_accepted(self, runner, action):
- result = runner.invoke(main, ["deduplicate", "--action", action, "--dry-run"])
+ result = runner.invoke(cli_module.main, ["deduplicate", "--action", action, "--dry-run"])
_ok(result)
@@ -700,26 +729,26 @@ class TestDeduplicate:
class TestReason:
def test_group_help(self, runner):
- result = runner.invoke(main, ["reason", "--help"])
+ result = runner.invoke(cli_module.main, ["reason", "--help"])
_ok(result)
for sub in ["run", "explain", "query", "list"]:
assert sub in result.output
def test_list_shows_engines(self, runner):
- result = runner.invoke(main, ["reason", "list"])
+ result = runner.invoke(cli_module.main, ["reason", "list"])
_ok(result)
assert "rete" in result.output
def test_list_json(self, runner):
# list command has no --json flag, output via cli_ctx.json_output
- result2 = runner.invoke(main, ["--json", "reason", "list"])
+ result2 = runner.invoke(cli_module.main, ["--json", "reason", "list"])
_ok(result2)
data = json.loads(result2.output.strip())
assert "engines" in data
assert "rete" in data["engines"]
def test_run_help(self, runner):
- result = runner.invoke(main, ["reason", "run", "--help"])
+ result = runner.invoke(cli_module.main, ["reason", "run", "--help"])
_ok(result)
assert "--engine" in result.output
@@ -728,12 +757,12 @@ class TestReason:
(_ for _ in ()).throw(ImportError(n))
if "reasoning" in n else __import__(n, *a, **k)
)):
- result = runner.invoke(main, ["reason", "run"])
+ result = runner.invoke(cli_module.main, ["reason", "run"])
assert result.exit_code != 0
assert "Traceback" not in result.output
def test_explain_requires_conclusion(self, runner):
- result = runner.invoke(main, ["reason", "explain"])
+ result = runner.invoke(cli_module.main, ["reason", "explain"])
assert result.exit_code != 0
def test_explain_import_error_is_clean(self, runner):
@@ -741,7 +770,7 @@ class TestReason:
(_ for _ in ()).throw(ImportError(n))
if "reasoning" in n else __import__(n, *a, **k)
)):
- result = runner.invoke(main, ["reason", "explain", "Alice is-manager-of Eng"])
+ result = runner.invoke(cli_module.main, ["reason", "explain", "Alice is-manager-of Eng"])
assert result.exit_code != 0
assert "Traceback" not in result.output
@@ -750,7 +779,7 @@ class TestReason:
(_ for _ in ()).throw(ImportError(n))
if "reasoning" in n else __import__(n, *a, **k)
)):
- result = runner.invoke(main, ["reason", "query", "SELECT ?x WHERE {}"])
+ result = runner.invoke(cli_module.main, ["reason", "query", "SELECT ?x WHERE {}"])
assert result.exit_code != 0
assert "Traceback" not in result.output
@@ -760,24 +789,24 @@ class TestReason:
class TestDecision:
def test_group_help(self, runner):
- result = runner.invoke(main, ["decision", "--help"])
+ result = runner.invoke(cli_module.main, ["decision", "--help"])
_ok(result)
for sub in ["record", "list", "query", "trace", "similar", "impact", "check"]:
assert sub in result.output
def test_record_requires_title(self, runner):
- result = runner.invoke(main, ["decision", "record"])
+ result = runner.invoke(cli_module.main, ["decision", "record"])
assert result.exit_code != 0
def test_record_dry_run_json(self, runner):
- result = runner.invoke(main, ["decision", "record",
+ result = runner.invoke(cli_module.main, ["decision", "record",
"--title", "Approve X", "--dry-run", "--json"])
_ok(result)
data = _json_output(result)
assert data["dry_run"] is True
def test_record_global_dry_run_json(self, runner):
- result = runner.invoke(main, ["--json", "--dry-run", "decision", "record",
+ result = runner.invoke(cli_module.main, ["--json", "--dry-run", "decision", "record",
"--title", "Approve X"])
_ok(result)
data = _json_output(result)
@@ -786,38 +815,47 @@ class TestDecision:
def test_record_import_error_is_clean(self, runner):
with patch("builtins.__import__", side_effect=lambda n, *a, **k: (
(_ for _ in ()).throw(ImportError(n))
- if n.startswith("semantica.context") else __import__(n, *a, **k)
+ if "semantica.context" in n else __import__(n, *a, **k)
)):
- result = runner.invoke(main, ["decision", "record", "--title", "X"])
+ result = runner.invoke(cli_module.main, ["decision", "record", "--title", "X"])
assert result.exit_code != 0
assert "Traceback" not in result.output
def test_list_json(self, runner, monkeypatch):
- fake_ctx = _fake_module(
- DecisionQuery=lambda **kw: MagicMock(
- list=lambda **kw2: [{"id": "d1", "title": "T", "tags": []}]
- ),
- )
- monkeypatch.setitem(__import__("sys").modules, "semantica.context", fake_ctx)
- result = runner.invoke(main, ["decision", "list", "--format", "json"])
+ import datetime
+ fake_dq = MagicMock()
+ d = MagicMock()
+ d.decision_id = "d1"
+ d.scenario = "T"
+ d.category = "general"
+ d.outcome = "ok"
+ d.confidence = 1.0
+ fake_dq.find_by_time_range.return_value = [d]
+ fake_decision_query = _fake_module(DecisionQuery=lambda *a, **kw: fake_dq)
+ fake_graph_store = _fake_module(GraphStore=MagicMock(return_value=MagicMock()))
+ monkeypatch.setitem(__import__("sys").modules,
+ "semantica.context.decision_query", fake_decision_query)
+ monkeypatch.setitem(__import__("sys").modules,
+ "semantica.graph_store", fake_graph_store)
+ result = runner.invoke(cli_module.main, ["decision", "list", "--format", "json"])
_ok(result)
def test_trace_import_error_is_clean(self, runner):
with patch("builtins.__import__", side_effect=lambda n, *a, **k: (
(_ for _ in ()).throw(ImportError(n))
- if n.startswith("semantica.context") else __import__(n, *a, **k)
+ if "semantica.context" in n else __import__(n, *a, **k)
)):
- result = runner.invoke(main, ["decision", "trace", "dec_123"])
+ result = runner.invoke(cli_module.main, ["decision", "trace", "dec_123"])
assert result.exit_code != 0
assert "Traceback" not in result.output
def test_check_requires_id(self, runner):
- result = runner.invoke(main, ["decision", "check"])
+ result = runner.invoke(cli_module.main, ["decision", "check"])
assert result.exit_code != 0
@pytest.mark.parametrize("sub", ["similar", "impact"])
def test_sub_requires_id(self, runner, sub):
- result = runner.invoke(main, ["decision", sub])
+ result = runner.invoke(cli_module.main, ["decision", sub])
assert result.exit_code != 0
@@ -826,13 +864,13 @@ class TestDecision:
class TestTemporal:
def test_group_help(self, runner):
- result = runner.invoke(main, ["temporal", "--help"])
+ result = runner.invoke(cli_module.main, ["temporal", "--help"])
_ok(result)
for sub in ["snapshot", "query", "history", "distance", "allen"]:
assert sub in result.output
def test_snapshot_requires_at(self, runner):
- result = runner.invoke(main, ["temporal", "snapshot"])
+ result = runner.invoke(cli_module.main, ["temporal", "snapshot"])
assert result.exit_code != 0
def test_snapshot_json(self, runner, monkeypatch):
@@ -842,18 +880,18 @@ class TestTemporal:
),
)
monkeypatch.setitem(__import__("sys").modules, "semantica.kg", fake_kg)
- result = runner.invoke(main, ["temporal", "snapshot",
+ result = runner.invoke(cli_module.main, ["temporal", "snapshot",
"--at", "2026-01-01T00:00:00Z", "--json"])
_ok(result)
data = _json_output(result)
assert isinstance(data, dict)
def test_distance_requires_both_events(self, runner):
- result = runner.invoke(main, ["temporal", "distance", "--event1", "ev1"])
+ result = runner.invoke(cli_module.main, ["temporal", "distance", "--event1", "ev1"])
assert result.exit_code != 0
def test_allen_requires_both_intervals(self, runner):
- result = runner.invoke(main, ["temporal", "allen",
+ result = runner.invoke(cli_module.main, ["temporal", "allen",
"--interval1", "int1", "--interval2", "int2"])
assert result.exit_code != 0 or result.exit_code == 0 # depends on import
@@ -862,7 +900,7 @@ class TestTemporal:
(_ for _ in ()).throw(ImportError(n))
if n.startswith("semantica.kg") else __import__(n, *a, **k)
)):
- result = runner.invoke(main, ["temporal", "history", "entity_alice"])
+ result = runner.invoke(cli_module.main, ["temporal", "history", "entity_alice"])
assert result.exit_code != 0
assert "Traceback" not in result.output
@@ -872,13 +910,13 @@ class TestTemporal:
class TestProvenance:
def test_group_help(self, runner):
- result = runner.invoke(main, ["provenance", "--help"])
+ result = runner.invoke(cli_module.main, ["provenance", "--help"])
_ok(result)
for sub in ["lineage", "audit", "export", "check"]:
assert sub in result.output
def test_lineage_requires_entity(self, runner):
- result = runner.invoke(main, ["provenance", "lineage"])
+ result = runner.invoke(cli_module.main, ["provenance", "lineage"])
assert result.exit_code != 0
def test_lineage_import_error_is_clean(self, runner):
@@ -886,12 +924,12 @@ class TestProvenance:
(_ for _ in ()).throw(ImportError(n))
if "provenance" in n else __import__(n, *a, **k)
)):
- result = runner.invoke(main, ["provenance", "lineage", "entity_alice"])
+ result = runner.invoke(cli_module.main, ["provenance", "lineage", "entity_alice"])
assert result.exit_code != 0
assert "Traceback" not in result.output
def test_export_dry_run(self, runner):
- result = runner.invoke(main, ["provenance", "export", "--dry-run"])
+ result = runner.invoke(cli_module.main, ["provenance", "export", "--dry-run"])
_ok(result)
def test_audit_writes_output(self, runner, monkeypatch):
@@ -902,7 +940,7 @@ class TestProvenance:
)
monkeypatch.setitem(__import__("sys").modules, "semantica.provenance", fake_prov)
with runner.isolated_filesystem():
- result = runner.invoke(main, ["provenance", "audit", "--output", "audit.json"])
+ result = runner.invoke(cli_module.main, ["provenance", "audit", "--output", "audit.json"])
if result.exit_code == 0:
assert os.path.exists("audit.json")
@@ -911,7 +949,7 @@ class TestProvenance:
(_ for _ in ()).throw(ImportError(n))
if "provenance" in n else __import__(n, *a, **k)
)):
- result = runner.invoke(main, ["provenance", "check"])
+ result = runner.invoke(cli_module.main, ["provenance", "check"])
assert result.exit_code != 0
assert "Traceback" not in result.output
@@ -921,13 +959,13 @@ class TestProvenance:
class TestValidate:
def test_group_help(self, runner):
- result = runner.invoke(main, ["validate", "--help"])
+ result = runner.invoke(cli_module.main, ["validate", "--help"])
_ok(result)
for sub in ["shacl", "conflicts", "integrity"]:
assert sub in result.output
def test_shacl_help(self, runner):
- result = runner.invoke(main, ["validate", "shacl", "--help"])
+ result = runner.invoke(cli_module.main, ["validate", "shacl", "--help"])
_ok(result)
assert "--strictness" in result.output
@@ -936,7 +974,7 @@ class TestValidate:
(_ for _ in ()).throw(ImportError(n))
if "ontology" in n else __import__(n, *a, **k)
)):
- result = runner.invoke(main, ["validate", "shacl"])
+ result = runner.invoke(cli_module.main, ["validate", "shacl"])
assert result.exit_code != 0
assert "Traceback" not in result.output
@@ -945,7 +983,7 @@ class TestValidate:
detect_conflicts=lambda **kw: {"conflicts": [], "count": 0},
)
monkeypatch.setitem(__import__("sys").modules, "semantica.conflicts", fake_conf)
- result = runner.invoke(main, ["validate", "conflicts", "--json"])
+ result = runner.invoke(cli_module.main, ["validate", "conflicts", "--json"])
_ok(result)
data = _json_output(result)
assert isinstance(data, dict)
@@ -955,12 +993,12 @@ class TestValidate:
(_ for _ in ()).throw(ImportError(n))
if n.startswith("semantica.kg") else __import__(n, *a, **k)
)):
- result = runner.invoke(main, ["validate", "integrity"])
+ result = runner.invoke(cli_module.main, ["validate", "integrity"])
assert result.exit_code != 0
assert "Traceback" not in result.output
def test_strictness_choices(self, runner):
- result = runner.invoke(main, ["validate", "shacl", "--help"])
+ result = runner.invoke(cli_module.main, ["validate", "shacl", "--help"])
for s in ["strict", "moderate", "lenient"]:
assert s in result.output
@@ -970,52 +1008,52 @@ class TestValidate:
class TestOntology:
def test_group_help(self, runner):
- result = runner.invoke(main, ["ontology", "--help"])
+ result = runner.invoke(cli_module.main, ["ontology", "--help"])
_ok(result)
for sub in ["generate", "import", "validate", "shacl", "skos",
"align", "health", "version"]:
assert sub in result.output
def test_generate_dry_run(self, runner):
- result = runner.invoke(main, ["ontology", "generate", "--dry-run"])
+ result = runner.invoke(cli_module.main, ["ontology", "generate", "--dry-run"])
_ok(result)
def test_generate_json_dry_run(self, runner):
- result = runner.invoke(main, ["ontology", "generate", "--dry-run", "--json"])
+ result = runner.invoke(cli_module.main, ["ontology", "generate", "--dry-run", "--json"])
_ok(result)
data = _json_output(result)
assert data["dry_run"] is True
def test_generate_global_json_dry_run(self, runner):
- result = runner.invoke(main, ["--json", "--dry-run", "ontology", "generate"])
+ result = runner.invoke(cli_module.main, ["--json", "--dry-run", "ontology", "generate"])
_ok(result)
data = _json_output(result)
assert data["dry_run"] is True
def test_import_dry_run(self, runner):
- result = runner.invoke(main, ["ontology", "import", "schema.ttl", "--dry-run"])
+ result = runner.invoke(cli_module.main, ["ontology", "import", "schema.ttl", "--dry-run"])
_ok(result)
def test_import_requires_source(self, runner):
- result = runner.invoke(main, ["ontology", "import"])
+ result = runner.invoke(cli_module.main, ["ontology", "import"])
assert result.exit_code != 0
def test_skos_group_help(self, runner):
- result = runner.invoke(main, ["ontology", "skos", "--help"])
+ result = runner.invoke(cli_module.main, ["ontology", "skos", "--help"])
_ok(result)
for sub in ["search", "hierarchy"]:
assert sub in result.output
def test_skos_search_requires_term(self, runner):
- result = runner.invoke(main, ["ontology", "skos", "search"])
+ result = runner.invoke(cli_module.main, ["ontology", "skos", "search"])
assert result.exit_code != 0
def test_skos_hierarchy_requires_uri(self, runner):
- result = runner.invoke(main, ["ontology", "skos", "hierarchy"])
+ result = runner.invoke(cli_module.main, ["ontology", "skos", "hierarchy"])
assert result.exit_code != 0
def test_align_requires_source_and_target(self, runner):
- result = runner.invoke(main, ["ontology", "align"])
+ result = runner.invoke(cli_module.main, ["ontology", "align"])
assert result.exit_code != 0
def test_align_import_error_is_clean(self, runner):
@@ -1028,7 +1066,7 @@ class TestOntology:
(_ for _ in ()).throw(ImportError(n))
if "ontology" in n else __import__(n, *a, **k)
)):
- result = runner.invoke(main, ["ontology", "align",
+ result = runner.invoke(cli_module.main, ["ontology", "align",
"--source", "s.ttl", "--target", "t.ttl"])
assert result.exit_code != 0
assert "Traceback" not in result.output
@@ -1038,7 +1076,7 @@ class TestOntology:
(_ for _ in ()).throw(ImportError(n))
if "ontology" in n else __import__(n, *a, **k)
)):
- result = runner.invoke(main, ["ontology", "health"])
+ result = runner.invoke(cli_module.main, ["ontology", "health"])
assert result.exit_code != 0
assert "Traceback" not in result.output
@@ -1048,7 +1086,7 @@ class TestOntology:
class TestExport:
def test_help_shows_14_formats(self, runner):
- result = runner.invoke(main, ["export", "--help"])
+ result = runner.invoke(cli_module.main, ["export", "--help"])
_ok(result)
for fmt in ["turtle", "parquet", "csv", "graphml", "owl", "arangodb"]:
assert fmt in result.output
@@ -1056,18 +1094,18 @@ class TestExport:
assert flag in result.output
def test_dry_run_json(self, runner):
- result = runner.invoke(main, ["export", "--format", "turtle",
+ result = runner.invoke(cli_module.main, ["export", "--format", "turtle",
"--dry-run", "--json"])
_ok(result)
data = _json_output(result)
assert data["dry_run"] is True
def test_dry_run_text(self, runner):
- result = runner.invoke(main, ["export", "--format", "csv", "--dry-run"])
+ result = runner.invoke(cli_module.main, ["export", "--format", "csv", "--dry-run"])
_ok(result, substr="Dry run")
def test_global_dry_run(self, runner):
- result = runner.invoke(main, ["--dry-run", "--json", "export", "--format", "json"])
+ result = runner.invoke(cli_module.main, ["--dry-run", "--json", "export", "--format", "json"])
_ok(result)
data = _json_output(result)
assert data["dry_run"] is True
@@ -1147,14 +1185,14 @@ class TestExport:
)
output_path = tmp_path / "export.json"
- result = runner.invoke(main, ["export", "--format", "json", "--output", str(output_path)])
+ result = runner.invoke(cli_module.main, ["export", "--format", "json", "--output", str(output_path)])
_ok(result)
exported = output_path.read_text(encoding="utf-8")
assert "Alice" in exported
assert "KNOWS" in exported
def test_invalid_format_fails(self, runner):
- result = runner.invoke(main, ["export", "--format", "magic"])
+ result = runner.invoke(cli_module.main, ["export", "--format", "magic"])
assert result.exit_code != 0
def test_import_error_is_clean(self, runner):
@@ -1163,7 +1201,7 @@ class TestExport:
(_ for _ in ()).throw(ImportError(n))
if "semantica.export" in n else original_import(n, *a, **k)
)):
- result = runner.invoke(main, ["export", "--format", "json"])
+ result = runner.invoke(cli_module.main, ["export", "--format", "json"])
assert result.exit_code != 0
assert "Traceback" not in result.output
@@ -1173,14 +1211,14 @@ class TestExport:
class TestVisualize:
def test_group_help(self, runner):
- result = runner.invoke(main, ["visualize", "--help"])
+ result = runner.invoke(cli_module.main, ["visualize", "--help"])
_ok(result)
for sub in ["kg", "ontology", "embeddings", "temporal", "analytics"]:
assert sub in result.output
@pytest.mark.parametrize("sub", ["kg", "ontology", "embeddings", "temporal", "analytics"])
def test_subcommand_help(self, runner, sub):
- result = runner.invoke(main, ["visualize", sub, "--help"])
+ result = runner.invoke(cli_module.main, ["visualize", sub, "--help"])
_ok(result)
for flag in ["--layout", "--format", "--output"]:
assert flag in result.output
@@ -1191,17 +1229,17 @@ class TestVisualize:
(_ for _ in ()).throw(ImportError(n))
if "visualization" in n else __import__(n, *a, **k)
)):
- result = runner.invoke(main, ["visualize", sub])
+ result = runner.invoke(cli_module.main, ["visualize", sub])
assert result.exit_code != 0
assert "Traceback" not in result.output
def test_format_choices(self, runner):
- result = runner.invoke(main, ["visualize", "kg", "--help"])
+ result = runner.invoke(cli_module.main, ["visualize", "kg", "--help"])
for fmt in ["html", "svg", "png", "pdf"]:
assert fmt in result.output
def test_layout_choices(self, runner):
- result = runner.invoke(main, ["visualize", "kg", "--help"])
+ result = runner.invoke(cli_module.main, ["visualize", "kg", "--help"])
for layout in ["forceatlas2", "spring", "hierarchical"]:
assert layout in result.output
@@ -1211,13 +1249,13 @@ class TestVisualize:
class TestPipeline:
def test_group_help(self, runner):
- result = runner.invoke(main, ["pipeline", "--help"])
+ result = runner.invoke(cli_module.main, ["pipeline", "--help"])
_ok(result)
for sub in ["init", "validate", "run", "status", "stop"]:
assert sub in result.output
def test_init_dry_run(self, runner):
- result = runner.invoke(main, ["pipeline", "init", "--dry-run"])
+ result = runner.invoke(cli_module.main, ["pipeline", "init", "--dry-run"])
_ok(result)
def test_init_creates_file(self, runner, monkeypatch):
@@ -1226,33 +1264,33 @@ class TestPipeline:
)
monkeypatch.setitem(__import__("sys").modules, "semantica.pipeline", fake_pl)
with runner.isolated_filesystem():
- result = runner.invoke(main, ["pipeline", "init",
+ result = runner.invoke(cli_module.main, ["pipeline", "init",
"--template", "rag", "--output", "pl.yaml"])
_ok(result)
assert os.path.exists("pl.yaml")
def test_init_template_choices(self, runner):
- result = runner.invoke(main, ["pipeline", "init", "--help"])
+ result = runner.invoke(cli_module.main, ["pipeline", "init", "--help"])
for t in ["ingest-extract-kg", "rag", "ontology-build", "decision-track", "full"]:
assert t in result.output
def test_validate_requires_file(self, runner):
- result = runner.invoke(main, ["pipeline", "validate"])
+ result = runner.invoke(cli_module.main, ["pipeline", "validate"])
assert result.exit_code != 0
def test_validate_nonexistent_file_fails(self, runner):
- result = runner.invoke(main, ["pipeline", "validate", "no_such.yaml"])
+ result = runner.invoke(cli_module.main, ["pipeline", "validate", "no_such.yaml"])
assert result.exit_code != 0
def test_run_dry_run(self, runner):
with runner.isolated_filesystem():
with open("pl.yaml", "w") as f:
f.write("steps: []\n")
- result = runner.invoke(main, ["pipeline", "run", "pl.yaml", "--dry-run"])
+ result = runner.invoke(cli_module.main, ["pipeline", "run", "pl.yaml", "--dry-run"])
_ok(result)
def test_run_requires_file(self, runner):
- result = runner.invoke(main, ["pipeline", "run"])
+ result = runner.invoke(cli_module.main, ["pipeline", "run"])
assert result.exit_code != 0
def test_status_exits_0(self, runner):
@@ -1260,7 +1298,7 @@ class TestPipeline:
(_ for _ in ()).throw(ImportError(n))
if "pipeline" in n else __import__(n, *a, **k)
)):
- result = runner.invoke(main, ["pipeline", "status"])
+ result = runner.invoke(cli_module.main, ["pipeline", "status"])
assert result.exit_code != 0
assert "Traceback" not in result.output
@@ -1269,7 +1307,7 @@ class TestPipeline:
(_ for _ in ()).throw(ImportError(n))
if "pipeline" in n else __import__(n, *a, **k)
)):
- result = runner.invoke(main, ["pipeline", "stop"])
+ result = runner.invoke(cli_module.main, ["pipeline", "stop"])
assert result.exit_code != 0
assert "Traceback" not in result.output
@@ -1279,51 +1317,51 @@ class TestPipeline:
class TestStore:
def test_group_help(self, runner):
- result = runner.invoke(main, ["store", "--help"])
+ result = runner.invoke(cli_module.main, ["store", "--help"])
_ok(result)
for sub in ["list", "connect", "stats", "migrate", "flush"]:
assert sub in result.output
def test_list_json_empty_config(self, runner):
- result = runner.invoke(main, ["store", "list", "--json"])
+ result = runner.invoke(cli_module.main, ["store", "list", "--json"])
_ok(result)
data = _json_output(result)
assert isinstance(data, dict)
def test_list_table(self, runner):
- result = runner.invoke(main, ["store", "list"])
+ result = runner.invoke(cli_module.main, ["store", "list"])
_ok(result)
def test_connect_requires_backend(self, runner):
- result = runner.invoke(main, ["store", "connect"])
+ result = runner.invoke(cli_module.main, ["store", "connect"])
assert result.exit_code != 0
def test_connect_reports_status(self, runner):
- result = runner.invoke(main, ["store", "connect", "--backend", "neo4j"])
+ result = runner.invoke(cli_module.main, ["store", "connect", "--backend", "neo4j"])
_ok(result)
def test_migrate_dry_run(self, runner):
- result = runner.invoke(main, ["store", "migrate",
+ result = runner.invoke(cli_module.main, ["store", "migrate",
"--from", "faiss", "--to", "qdrant", "--dry-run"])
_ok(result)
def test_migrate_requires_from_and_to(self, runner):
- result = runner.invoke(main, ["store", "migrate", "--from", "faiss"])
+ result = runner.invoke(cli_module.main, ["store", "migrate", "--from", "faiss"])
assert result.exit_code != 0
def test_flush_requires_confirm(self, runner):
- result = runner.invoke(main, ["store", "flush"])
+ result = runner.invoke(cli_module.main, ["store", "flush"])
assert result.exit_code != 0
assert "confirm" in result.output.lower() or result.exit_code == 2
def test_flush_with_confirm(self, runner, monkeypatch):
fake_vs = _fake_module(delete_vectors=lambda **kw: None)
monkeypatch.setitem(__import__("sys").modules, "semantica.vector_store", fake_vs)
- result = runner.invoke(main, ["store", "flush", "--confirm"])
+ result = runner.invoke(cli_module.main, ["store", "flush", "--confirm"])
_ok(result)
def test_stats_requires_backend(self, runner):
- result = runner.invoke(main, ["store", "stats"])
+ result = runner.invoke(cli_module.main, ["store", "stats"])
assert result.exit_code != 0
@@ -1332,13 +1370,13 @@ class TestStore:
class TestBackup:
def test_group_help(self, runner):
- result = runner.invoke(main, ["backup", "--help"])
+ result = runner.invoke(cli_module.main, ["backup", "--help"])
_ok(result)
for sub in ["info", "create", "sync", "restore", "schedule"]:
assert sub in result.output
def test_info_json_empty_config(self, runner):
- result = runner.invoke(main, ["backup", "info", "--json"])
+ result = runner.invoke(cli_module.main, ["backup", "info", "--json"])
_ok(result)
data = _json_output(result)
assert isinstance(data, list)
@@ -1352,7 +1390,7 @@ class TestBackup:
" backend: neo4j\n"
" uri: bolt://user:secret123@host:7687\n"
)
- result = runner.invoke(main, ["--config", "cfg.yaml", "backup", "info"])
+ result = runner.invoke(cli_module.main, ["--config", "cfg.yaml", "backup", "info"])
_ok(result)
assert "secret123" not in result.output
@@ -1364,7 +1402,7 @@ class TestBackup:
" backend: neo4j\n"
" uri: bolt://user:secret123@host:7687\n"
)
- result = runner.invoke(main, ["--config", "cfg.yaml", "backup", "info"])
+ result = runner.invoke(cli_module.main, ["--config", "cfg.yaml", "backup", "info"])
_ok(result)
assert "neo4j" in result.output
assert "graph" in result.output
@@ -1374,13 +1412,13 @@ class TestBackup:
with open("cfg.yaml", "w") as f:
# vector_store is the correct key in Config
f.write("vector_store:\n backend: pinecone\n host: x\n")
- result = runner.invoke(main, ["--config", "cfg.yaml", "backup", "info"])
+ result = runner.invoke(cli_module.main, ["--config", "cfg.yaml", "backup", "info"])
_ok(result)
assert "export" in result.output
def test_create_dry_run(self, runner):
with runner.isolated_filesystem():
- result = runner.invoke(main, ["backup", "create", "backup.tar.gz", "--dry-run"])
+ result = runner.invoke(cli_module.main, ["backup", "create", "backup.tar.gz", "--dry-run"])
_ok(result)
def test_create_unencrypted_with_config_requires_confirm(self, runner):
@@ -1388,7 +1426,7 @@ class TestBackup:
with open("cfg.yaml", "w") as f:
f.write("graph_db:\n backend: neo4j\n uri: bolt://localhost\n")
result = runner.invoke(
- main,
+ cli_module.main,
["--config", "cfg.yaml", "backup", "create", "out.tar.gz"],
input="n\n",
)
@@ -1399,7 +1437,7 @@ class TestBackup:
with open("cfg.yaml", "w") as f:
f.write("graph_db:\n backend: neo4j\n uri: bolt://localhost\n")
result = runner.invoke(
- main,
+ cli_module.main,
["--config", "cfg.yaml", "backup", "create", "out.tar.gz",
"--strip-config", "--quiet"],
)
@@ -1407,7 +1445,7 @@ class TestBackup:
def test_create_dry_run_json(self, runner):
# backup create has no per-command --json; use global --json
- result = runner.invoke(main, ["--json", "backup", "create", "backup.tar.gz",
+ result = runner.invoke(cli_module.main, ["--json", "backup", "create", "backup.tar.gz",
"--dry-run"])
_ok(result)
data = _json_output(result)
@@ -1420,7 +1458,7 @@ class TestBackup:
try:
os.chmod("keyfile.txt", stat.S_IRUSR | stat.S_IRGRP | stat.S_IROTH)
result = runner.invoke(
- main,
+ cli_module.main,
["backup", "create", "out.tar.gz",
"--keyfile", "keyfile.txt", "--encrypt"],
)
@@ -1431,19 +1469,19 @@ class TestBackup:
def test_create_keyfile_nonexistent_rejected(self, runner):
result = runner.invoke(
- main,
+ cli_module.main,
["backup", "create", "out.tar.gz",
"--keyfile", "no_such_keyfile.txt", "--encrypt"],
)
assert result.exit_code != 0
def test_sync_dry_run(self, runner):
- result = runner.invoke(main, ["backup", "sync", "/tmp/bk", "--dry-run"])
+ result = runner.invoke(cli_module.main, ["backup", "sync", "/tmp/bk", "--dry-run"])
_ok(result)
def test_sync_creates_directory(self, runner):
with runner.isolated_filesystem():
- result = runner.invoke(main, ["backup", "sync", "sync_dest"])
+ result = runner.invoke(cli_module.main, ["backup", "sync", "sync_dest"])
_ok(result)
assert os.path.isdir("sync_dest")
@@ -1451,38 +1489,38 @@ class TestBackup:
with runner.isolated_filesystem():
with open("backup.tar.gz", "w") as f:
f.write("")
- result = runner.invoke(main, ["backup", "restore", "backup.tar.gz", "--dry-run"])
+ result = runner.invoke(cli_module.main, ["backup", "restore", "backup.tar.gz", "--dry-run"])
_ok(result)
def test_restore_requires_source(self, runner):
- result = runner.invoke(main, ["backup", "restore"])
+ result = runner.invoke(cli_module.main, ["backup", "restore"])
assert result.exit_code != 0
def test_restore_nonexistent_source_fails(self, runner):
- result = runner.invoke(main, ["backup", "restore", "no_such_file.tar.gz"])
+ result = runner.invoke(cli_module.main, ["backup", "restore", "no_such_file.tar.gz"])
assert result.exit_code != 0
def test_schedule_prints_cron(self, runner):
- result = runner.invoke(main, ["backup", "schedule",
+ result = runner.invoke(cli_module.main, ["backup", "schedule",
"--dest", "/mnt/bk", "--freq", "daily"])
_ok(result)
assert "0 2 * * *" in result.output
assert "/mnt/bk" in result.output
def test_schedule_weekly(self, runner):
- result = runner.invoke(main, ["backup", "schedule",
+ result = runner.invoke(cli_module.main, ["backup", "schedule",
"--dest", "/mnt/bk", "--freq", "weekly"])
_ok(result)
assert "0 2 * * 0" in result.output
def test_schedule_with_encrypt(self, runner):
- result = runner.invoke(main, ["backup", "schedule",
+ result = runner.invoke(cli_module.main, ["backup", "schedule",
"--dest", "/mnt/bk", "--encrypt"])
_ok(result)
assert "--encrypt" in result.output
def test_schedule_json(self, runner):
- result = runner.invoke(main, ["--json", "backup", "schedule",
+ result = runner.invoke(cli_module.main, ["--json", "backup", "schedule",
"--dest", "/mnt/bk"])
_ok(result)
data = json.loads(result.output.strip())
@@ -1494,13 +1532,13 @@ class TestBackup:
class TestServer:
def test_group_help(self, runner):
- result = runner.invoke(main, ["server", "--help"])
+ result = runner.invoke(cli_module.main, ["server", "--help"])
_ok(result)
for sub in ["start", "stop", "status"]:
assert sub in result.output
def test_start_help(self, runner):
- result = runner.invoke(main, ["server", "start", "--help"])
+ result = runner.invoke(cli_module.main, ["server", "start", "--help"])
_ok(result)
for flag in ["--port", "--workers", "--reload", "--host"]:
assert flag in result.output
@@ -1509,7 +1547,7 @@ class TestServer:
mock_proc = MagicMock()
mock_proc.pid = 12345
with patch("subprocess.Popen", return_value=mock_proc) as mock_popen:
- result = runner.invoke(main, ["server", "start", "--port", "9000"])
+ result = runner.invoke(cli_module.main, ["server", "start", "--port", "9000"])
_ok(result)
mock_popen.assert_called_once()
call_args = mock_popen.call_args[0][0]
@@ -1519,14 +1557,14 @@ class TestServer:
mock_proc = MagicMock()
mock_proc.pid = 12346
with patch("subprocess.Popen", return_value=mock_proc) as mock_popen:
- result = runner.invoke(main, ["server", "start", "--reload"])
+ result = runner.invoke(cli_module.main, ["server", "start", "--reload"])
_ok(result)
call_args = mock_popen.call_args[0][0]
assert "--reload" in call_args
def test_stop_when_not_running(self, runner, tmp_path, monkeypatch):
monkeypatch.setattr(cli_module, "_read_pid", lambda n: None)
- result = runner.invoke(main, ["server", "stop"])
+ result = runner.invoke(cli_module.main, ["server", "stop"])
_ok(result)
assert "not running" in result.output.lower()
@@ -1536,19 +1574,19 @@ class TestServer:
exists=lambda: True, unlink=lambda missing_ok=False: None
))
with patch("os.kill") as mock_kill:
- result = runner.invoke(main, ["server", "stop"])
+ result = runner.invoke(cli_module.main, ["server", "stop"])
_ok(result)
mock_kill.assert_called_once()
def test_status_when_stopped(self, runner, monkeypatch):
monkeypatch.setattr(cli_module, "_read_pid", lambda n: None)
- result = runner.invoke(main, ["server", "status"])
+ result = runner.invoke(cli_module.main, ["server", "status"])
_ok(result)
assert "stopped" in result.output
def test_status_json(self, runner, monkeypatch):
monkeypatch.setattr(cli_module, "_read_pid", lambda n: None)
- result = runner.invoke(main, ["server", "status", "--json"])
+ result = runner.invoke(cli_module.main, ["server", "status", "--json"])
_ok(result)
data = _json_output(result)
assert data["service"] == "server"
@@ -1560,7 +1598,7 @@ class TestServer:
class TestExplorer:
def test_group_help(self, runner):
- result = runner.invoke(main, ["explorer", "--help"])
+ result = runner.invoke(cli_module.main, ["explorer", "--help"])
_ok(result)
for sub in ["start", "stop", "status", "open"]:
assert sub in result.output
@@ -1570,7 +1608,7 @@ class TestExplorer:
mock_proc.pid = 22222
monkeypatch.setattr(cli_module, "_write_pid", lambda *_args: None)
with patch("subprocess.Popen", return_value=mock_proc):
- result = runner.invoke(main, ["explorer", "start", "--port", "5173"])
+ result = runner.invoke(cli_module.main, ["explorer", "start", "--port", "5173"])
_ok(result)
def test_start_forwards_api_url_to_child_environment(self, runner, monkeypatch):
@@ -1579,7 +1617,7 @@ class TestExplorer:
monkeypatch.setattr(cli_module, "_write_pid", lambda *_args: None)
with patch("subprocess.Popen", return_value=mock_proc) as mock_popen:
result = runner.invoke(
- main,
+ cli_module.main,
["explorer", "start", "--api-url", "http://localhost:9000"],
)
_ok(result)
@@ -1589,20 +1627,20 @@ class TestExplorer:
def test_stop_when_not_running(self, runner, monkeypatch):
monkeypatch.setattr(cli_module, "_read_pid", lambda n: None)
- result = runner.invoke(main, ["explorer", "stop"])
+ result = runner.invoke(cli_module.main, ["explorer", "stop"])
_ok(result)
assert "not running" in result.output.lower()
def test_status_json(self, runner, monkeypatch):
monkeypatch.setattr(cli_module, "_read_pid", lambda n: None)
- result = runner.invoke(main, ["explorer", "status", "--json"])
+ result = runner.invoke(cli_module.main, ["explorer", "status", "--json"])
_ok(result)
data = _json_output(result)
assert data["service"] == "explorer"
def test_open_calls_webbrowser(self, runner):
with patch("webbrowser.open") as mock_wb:
- result = runner.invoke(main, ["explorer", "open", "--port", "5173"])
+ result = runner.invoke(cli_module.main, ["explorer", "open", "--port", "5173"])
_ok(result)
mock_wb.assert_called_once_with("http://localhost:5173")
@@ -1612,7 +1650,7 @@ class TestExplorer:
class TestMCP:
def test_group_help(self, runner):
- result = runner.invoke(main, ["mcp", "--help"])
+ result = runner.invoke(cli_module.main, ["mcp", "--help"])
_ok(result)
for sub in ["start", "stop", "status", "list-tools", "call"]:
assert sub in result.output
@@ -1621,14 +1659,14 @@ class TestMCP:
mock_proc = MagicMock()
mock_proc.pid = 33333
with patch("subprocess.Popen", return_value=mock_proc):
- result = runner.invoke(main, ["mcp", "start"])
+ result = runner.invoke(cli_module.main, ["mcp", "start"])
_ok(result)
def test_start_http_includes_port(self, runner):
mock_proc = MagicMock()
mock_proc.pid = 33334
with patch("subprocess.Popen", return_value=mock_proc) as mock_popen:
- result = runner.invoke(main, ["mcp", "start", "--transport", "http",
+ result = runner.invoke(cli_module.main, ["mcp", "start", "--transport", "http",
"--port", "4000"])
_ok(result)
call_args = mock_popen.call_args[0][0]
@@ -1636,18 +1674,18 @@ class TestMCP:
def test_stop_when_not_running(self, runner, monkeypatch):
monkeypatch.setattr(cli_module, "_read_pid", lambda n: None)
- result = runner.invoke(main, ["mcp", "stop"])
+ result = runner.invoke(cli_module.main, ["mcp", "stop"])
_ok(result)
def test_status_json(self, runner, monkeypatch):
monkeypatch.setattr(cli_module, "_read_pid", lambda n: None)
- result = runner.invoke(main, ["mcp", "status", "--json"])
+ result = runner.invoke(cli_module.main, ["mcp", "status", "--json"])
_ok(result)
data = _json_output(result)
assert data["service"] == "mcp"
def test_list_tools_shows_tools(self, runner):
- result = runner.invoke(main, ["mcp", "list-tools"])
+ result = runner.invoke(cli_module.main, ["mcp", "list-tools"])
_ok(result)
# Table renders correctly — at minimum the column header is present
assert "Tool" in result.output or "tool" in result.output.lower()
@@ -1655,23 +1693,23 @@ class TestMCP:
def test_list_tools_with_mock_shows_known_tools(self, runner, monkeypatch):
fake_tools = _fake_module(__all__=["extract_entities", "query_graph"])
monkeypatch.setitem(__import__("sys").modules, "mcp.tools", fake_tools)
- result = runner.invoke(main, ["mcp", "list-tools"])
+ result = runner.invoke(cli_module.main, ["mcp", "list-tools"])
_ok(result)
assert "extract_entities" in result.output
def test_list_tools_json(self, runner):
- result = runner.invoke(main, ["mcp", "list-tools", "--json"])
+ result = runner.invoke(cli_module.main, ["mcp", "list-tools", "--json"])
_ok(result)
data = _json_output(result)
assert "tools" in data
assert isinstance(data["tools"], list)
def test_call_requires_tool_name(self, runner):
- result = runner.invoke(main, ["mcp", "call"])
+ result = runner.invoke(cli_module.main, ["mcp", "call"])
assert result.exit_code != 0
def test_call_invalid_json_args_fails_cleanly(self, runner):
- result = runner.invoke(main, ["mcp", "call", "some_tool", "--args", "{bad json}"])
+ result = runner.invoke(cli_module.main, ["mcp", "call", "some_tool", "--args", "{bad json}"])
assert result.exit_code != 0
assert "Traceback" not in result.output
assert "Invalid JSON" in result.output
@@ -1681,7 +1719,7 @@ class TestMCP:
(_ for _ in ()).throw(ImportError(n))
if n.startswith("mcp") else __import__(n, *a, **k)
)):
- result = runner.invoke(main, ["mcp", "call", "extract_entities"])
+ result = runner.invoke(cli_module.main, ["mcp", "call", "extract_entities"])
assert result.exit_code != 0
assert "Traceback" not in result.output
@@ -1691,23 +1729,23 @@ class TestMCP:
class TestServicesGroup:
def test_services_group_help_shows_subgroups(self, runner):
- result = runner.invoke(main, ["services", "--help"])
+ result = runner.invoke(cli_module.main, ["services", "--help"])
_ok(result)
for sub in ["server", "explorer", "mcp"]:
assert sub in result.output
def test_services_server_help(self, runner):
- result = runner.invoke(main, ["services", "server", "--help"])
+ result = runner.invoke(cli_module.main, ["services", "server", "--help"])
_ok(result)
for sub in ["start", "stop", "status"]:
assert sub in result.output
def test_services_explorer_help(self, runner):
- result = runner.invoke(main, ["services", "explorer", "--help"])
+ result = runner.invoke(cli_module.main, ["services", "explorer", "--help"])
_ok(result)
def test_services_mcp_help(self, runner):
- result = runner.invoke(main, ["services", "mcp", "--help"])
+ result = runner.invoke(cli_module.main, ["services", "mcp", "--help"])
_ok(result)
@@ -1717,12 +1755,12 @@ class TestServicesGroup:
class TestCompletion:
@pytest.mark.parametrize("shell", ["bash", "zsh", "fish", "powershell"])
def test_completion_exits_0(self, runner, shell):
- result = runner.invoke(main, ["completion", shell])
+ result = runner.invoke(cli_module.main, ["completion", shell])
assert result.exit_code == 0
@pytest.mark.parametrize("shell", ["bash", "zsh", "fish", "powershell"])
def test_completion_output_not_empty(self, runner, shell):
- result = runner.invoke(main, ["completion", shell])
+ result = runner.invoke(cli_module.main, ["completion", shell])
assert result.exit_code == 0
assert len(result.output.strip()) > 0
@@ -1733,12 +1771,12 @@ class TestCompletion:
("powershell", "$PROFILE"),
])
def test_completion_mentions_install_path(self, runner, shell, install_path):
- result = runner.invoke(main, ["completion", shell])
+ result = runner.invoke(cli_module.main, ["completion", shell])
assert result.exit_code == 0
assert install_path in result.output
def test_invalid_shell_fails(self, runner):
- result = runner.invoke(main, ["completion", "csh"])
+ result = runner.invoke(cli_module.main, ["completion", "csh"])
assert result.exit_code != 0
@@ -1749,18 +1787,18 @@ class TestGlobalJsonPropagation:
"""--json set at root should trigger JSON output in all subcommands."""
def test_global_json_on_backup_schedule(self, runner):
- result = runner.invoke(main, ["--json", "backup", "schedule", "--dest", "/d"])
+ result = runner.invoke(cli_module.main, ["--json", "backup", "schedule", "--dest", "/d"])
_ok(result)
data = json.loads(result.output.strip())
assert "cron" in data
def test_global_json_on_store_list(self, runner):
- result = runner.invoke(main, ["--json", "store", "list"])
+ result = runner.invoke(cli_module.main, ["--json", "store", "list"])
_ok(result)
assert json.loads(result.output.strip()) is not None
def test_global_json_on_mcp_list_tools(self, runner):
- result = runner.invoke(main, ["--json", "mcp", "list-tools"])
+ result = runner.invoke(cli_module.main, ["--json", "mcp", "list-tools"])
_ok(result)
data = json.loads(result.output.strip())
assert "tools" in data
@@ -1773,15 +1811,15 @@ class TestExitCodes:
"""Exit codes must match the spec: 0 success, 1 general, 2 validation."""
def test_success_is_0(self, runner):
- result = runner.invoke(main, ["info"])
+ result = runner.invoke(cli_module.main, ["info"])
assert result.exit_code == 0
def test_missing_required_arg_is_2(self, runner):
- result = runner.invoke(main, ["kg", "build"])
+ result = runner.invoke(cli_module.main, ["kg", "build"])
assert result.exit_code == 2
def test_missing_required_arg_for_find_path_is_nonzero(self, runner):
- result = runner.invoke(main, ["kg", "find-path"])
+ result = runner.invoke(cli_module.main, ["kg", "find-path"])
assert result.exit_code != 0
def test_import_error_is_nonzero(self, runner):
@@ -1789,7 +1827,7 @@ class TestExitCodes:
(_ for _ in ()).throw(ImportError(n))
if "deduplication" in n else __import__(n, *a, **k)
)):
- result = runner.invoke(main, ["deduplicate"])
+ result = runner.invoke(cli_module.main, ["deduplicate"])
assert result.exit_code != 0
def test_no_traceback_on_any_error(self, runner):
@@ -1799,7 +1837,7 @@ class TestExitCodes:
["export", "--format", "bad"],
["mcp", "call", "tool", "--args", "{invalid}"],
]:
- result = runner.invoke(main, argv)
+ result = runner.invoke(cli_module.main, argv)
assert "Traceback" not in result.output, (
f"Traceback found for {argv}: {result.output}"
)