codex-python / tests /test_topology_and_cli.py
tailot's picture
Upload folder using huggingface_hub
e1de134 verified
Raw History Blame Contribute Delete
5.41 kB
"""Tests for the external topology, node subprocesses, CLI, and gate script."""
from __future__ import annotations
import json
import subprocess
import sys
from pathlib import Path
from lingologos_codex.agents import ROLES
from lingologos_codex.decision import dossier_from_dict
from lingologos_codex.envelope import ENVELOPE_KIND
from lingologos_python import cli
from lingologos_python.session import PythonWorkspaceSession, SessionResult
PYTHON_ROOT = Path(__file__).resolve().parent.parent
def test_external_topology_json_and_node_folders_are_valid() -> None:
topo = json.loads((PYTHON_ROOT / "topology.json").read_text(encoding="utf-8"))
assert topo["sessionId"] == "lingologos-python-01"
assert topo["terminationCondition"] == "FIXED_PIPELINE"
assert sorted(inst["id"] for inst in topo["instances"]) == sorted(ROLES)
for role in ROLES:
node_dir = PYTHON_ROOT / "nodes" / role
assert (node_dir / "grammar.gf").is_file()
assert (node_dir / "ontology.smt2").is_file()
seed = json.loads((node_dir / "seed_payload.json").read_text(encoding="utf-8"))
assert seed["kind"] == ENVELOPE_KIND
assert seed["sender"] == role
dossier_from_dict(json.loads(seed["payload"]["abstractSyntaxTree"]))
def test_six_isolated_subprocess_nodes_verify_workspace(sample_pkg: Path) -> None:
session = PythonWorkspaceSession(sample_pkg, use_subprocesses=True)
res = session.verify("src/mathops.py")
assert res.verdict == "VERIFIED"
assert res.assurance == 3
assert [env["sender"] for env in res.trace] == ["arbiter", *ROLES]
def test_python_agent_stdin_stdout_roundtrip(sample_pkg: Path) -> None:
seed = (PYTHON_ROOT / "nodes" / "arbiter" / "seed_payload.json").read_text(encoding="utf-8")
proc = subprocess.run(
[
sys.executable,
"-B",
str(PYTHON_ROOT / "agents" / "python_agent.py"),
"--role",
"planner",
"--node-id",
"planner",
],
input=seed,
capture_output=True,
text=True,
encoding="utf-8",
check=True,
)
out = json.loads(proc.stdout)
assert out["sender"] == "planner"
assert out["turn"] == 1
dossier = json.loads(out["payload"]["abstractSyntaxTree"])
assert dossier["plan"]["order"][0] == "planner"
def test_cli_subcommands(sample_pkg: Path, capsys: object) -> None:
assert cli.main(["check-adapter", "--workspace", str(sample_pkg)]) == 0
assert cli.main(["symbols", "--workspace", str(sample_pkg), "--json"]) == 0
assert (
cli.main(
[
"select-tests",
"--workspace",
str(sample_pkg),
"--files",
"src/mathops.py",
"--json",
]
)
== 0
)
assert cli.main(["verify", "--workspace", str(sample_pkg), "--file", "src/mathops.py"]) == 0
assert (
cli.main(
[
"edit",
"--workspace",
str(sample_pkg),
"--file",
"src/mathops.py",
"--search",
"return left + right",
"--replace",
"return int(left + right)",
]
)
== 0
)
def test_the_cli_exits_non_zero_when_a_verified_write_never_landed(
sample_pkg: Path, monkeypatch
) -> None:
"""The exit code reports the write, not the verdict.
The session can verify a change in staging and then lose the commit to the
disk. A command line that exits 0 on that run promises a file it did not
write, so the process reports failure whenever committed is false outside a
dry run.
"""
def fake_create(self, rel_path, content, **kwargs) -> SessionResult:
return SessionResult(
case_id="python-create",
verdict="VERIFIED",
assurance=3,
reason="OK_DUAL_CONFIRMATION",
detail="the commit failed, the workspace was restored from the in-memory originals",
committed=False,
)
def fake_apply(self, pattern_id, **kwargs) -> SessionResult:
return SessionResult(
case_id="python-pattern",
verdict="VERIFIED",
assurance=3,
reason="OK_DUAL_CONFIRMATION",
detail="the text on disk after the commit is not the verified text",
committed=False,
)
content_file = sample_pkg / "newcomer.txt"
content_file.write_text("VALUE = 1\n", encoding="utf-8")
monkeypatch.setattr(PythonWorkspaceSession, "create_file", fake_create)
assert (
cli.main(
[
"create",
"--workspace",
str(sample_pkg),
"--file",
"src/newcomer.py",
"--content-file",
str(content_file),
]
)
== 1
)
monkeypatch.setattr(PythonWorkspaceSession, "apply_pattern", fake_apply)
assert (
cli.main(
[
"patterns",
"apply",
"--workspace",
str(sample_pkg),
"--pattern",
"chain-to-registry",
"--target",
"src/mathops.py::double_sum",
]
)
== 1
)