ut-evaluation / testing /nano_simulation_agents /tests /test_verification_simulation.py
Codex
Replace the dataset with the current testing tree and the three UT docs.
d26f22a
Raw History Blame Contribute Delete
5.98 kB
from __future__ import annotations
import hashlib
import json
import os
import subprocess
import sys
from pathlib import Path
from nano_python_agents.todo import DEFAULT_ITEMS
from verification_simulation_agents.opencode_runtime import _provider_error
ROOT = Path(__file__).resolve().parents[1]
def _sha(path: Path) -> str:
return hashlib.sha256(path.read_bytes()).hexdigest()
def _target(tmp_path: Path) -> Path:
target = tmp_path / "target-skill"
(target / "reference").mkdir(parents=True)
(target / "scripts").mkdir()
todo = "\n".join(DEFAULT_ITEMS)
(target / "SKILL.md").write_text(
"---\nname: target-skill\ndescription: Test target.\n---\n\n"
f"# Target\n\n{todo}\n\n[Reference](reference/contract.md)\n",
encoding="utf-8",
)
(target / "reference" / "contract.md").write_text("# Contract\n", encoding="utf-8")
(target / "scripts" / "test_validation.py").write_text(
"def test_target_contract():\n assert True\n",
encoding="utf-8",
)
return target
def _request(tmp_path: Path, target: Path, runtime: str) -> Path:
fixture = tmp_path / "fixture.md"
fixture.write_text("# Fixture\n\ncommon credit and reserved credit\n", encoding="utf-8")
request = {
"schema_version": "verification-simulation/request-v1",
"case_id": "TC-900",
"case_name": "fixture",
"target_skill_path": str(target.resolve()),
"fixture_path": str(fixture.resolve()),
"runtime": runtime,
"prompt": "Run the target Skill.",
}
path = tmp_path / f"{runtime}-request.json"
path.write_text(json.dumps(request), encoding="utf-8")
return path
def _run(tmp_path: Path, request: Path, name: str, config: Path | None = None) -> subprocess.CompletedProcess[str]:
environment = os.environ.copy()
environment["PYTHONPATH"] = str(ROOT / "src")
return subprocess.run(
[
sys.executable,
"-m",
"verification_simulation_agents.cli",
"run",
"--config",
str(config or ROOT / "nano_agent.toml"),
"--request",
str(request),
"--output",
str(tmp_path / name),
],
cwd=ROOT,
env=environment,
capture_output=True,
text=True,
check=False,
)
def test_offline_runtime_completes_native_todo_and_preserves_sources(tmp_path: Path) -> None:
target = _target(tmp_path)
skill = target / "SKILL.md"
before = _sha(skill)
completed = _run(tmp_path, _request(tmp_path, target, "offline"), "offline-output")
assert completed.returncode == 0, completed.stderr
result = json.loads((tmp_path / "offline-output" / "simulation-result.json").read_text(encoding="utf-8"))
assert result["status"] == "SUCCEEDED"
assert result["evidence_strength"] == "deterministic-structural-supervision"
ledger = json.loads((tmp_path / "offline-output" / "workspace" / ".nano_agent" / "todo.json").read_text(encoding="utf-8"))
assert all(item["status"] == "completed" for item in ledger["todos"])
assert len(ledger["todos"]) == 14
assert before == _sha(skill)
def test_nano_runtime_reports_missing_credential_as_blocked(tmp_path: Path) -> None:
target = _target(tmp_path)
environment_name = "NANO_API_KEY"
previous = os.environ.pop(environment_name, None)
try:
completed = _run(tmp_path, _request(tmp_path, target, "nano"), "nano-output")
finally:
if previous is not None:
os.environ[environment_name] = previous
assert completed.returncode == 2
result = json.loads((tmp_path / "nano-output" / "simulation-result.json").read_text(encoding="utf-8"))
assert result["status"] == "BLOCKED"
assert result["observations"][0]["credential_present"] is False
def test_opencode_api_error_is_classified_as_provider_blocked() -> None:
events = [
{
"type": "error",
"error": {
"name": "APIError",
"data": {"message": "account disabled", "statusCode": 403, "isRetryable": False},
},
}
]
result = _provider_error(events, "")
assert result == {
"name": "APIError",
"status_code": 403,
"message": "account disabled",
"retryable": False,
}
def test_opencode_preflight_blocks_before_full_runtime(tmp_path: Path) -> None:
target = _target(tmp_path)
executable = tmp_path / "fake-opencode"
executable.write_text(
"#!/bin/sh\n"
"printf '%s\\n' '{\"type\":\"error\",\"error\":{\"name\":\"APIError\",\"data\":{\"message\":\"disabled\",\"statusCode\":403,\"isRetryable\":false}}}'\n"
"exit 1\n",
encoding="utf-8",
)
executable.chmod(0o755)
config = tmp_path / "nano_agent.toml"
config.write_text(
"[provider]\n"
'type = "openai"\nbase_url = "https://example.invalid/v1"\nmodel = "test"\n'
'api_key_env = "NANO_API_KEY"\nmax_tokens = 100\ntemperature = 0.0\n\n'
"[agent]\nmax_turns = 1\nmax_tool_output_chars = 1000\n\n"
"[workspace]\ntest_timeout_seconds = 10\n\n"
'[prompts]\nsystem = ["system"]\n\n[skills]\n\n'
"[verification]\n"
'default_runtime = "offline"\nallowed_runtimes = ["offline", "opencode", "nano"]\n'
f'timeout_seconds = 10\nmax_output_chars = 1000\nopencode_executable = "{executable}"\n'
'opencode_model = "test/model"\nopencode_auto_approve = true\n',
encoding="utf-8",
)
completed = _run(tmp_path, _request(tmp_path, target, "opencode"), "opencode-output", config)
assert completed.returncode == 2
result = json.loads((tmp_path / "opencode-output" / "simulation-result.json").read_text(encoding="utf-8"))
assert result["status"] == "BLOCKED"
assert result["provider_error"]["status_code"] == 403
assert not (tmp_path / "opencode-output" / "logs" / "opencode.jsonl").exists()