Files
OBLITERATUS/tests/test_concepts_cli.py
T
2026-10-18 20:52:50 -04:00

281 lines
12 KiB
Python

"""Concept CLI end-to-end contracts using local data and an injected HTTPS transport."""
from io import BytesIO
import json
from pathlib import Path
import shutil
import pytest
from obliteratus.cli import main
from obliteratus.concepts.pack import (
ConceptPack, builtin_refusal_pack, cache_directory, import_pack,
)
from obliteratus.concepts.pack_io import digest, zip_bytes
from tests.test_concept_packs import FIXTURES, manifest_at, update_manifest
pytestmark = pytest.mark.cpu
@pytest.fixture(autouse=True)
def private_cache(tmp_path, monkeypatch):
monkeypatch.setenv("OBLITERATUS_CACHE_DIR", str(tmp_path / "cache"))
def test_export_help_describes_affine_replay_and_reload(capsys):
with pytest.raises(SystemExit) as error:
main(["concepts", "export", "--help"])
assert error.value.code == 0
help_text = " ".join(capsys.readouterr().out.split())
assert "affine reference means" in help_text
assert "standard HF reload" in help_text
def test_list_and_show_builtin_offline_json(capsys):
main(["concepts", "list", "--json"])
result = json.loads(capsys.readouterr().out)
assert [(row["id"], row["tier"], row["version"]) for row in result] == [
("pain", "research", "1.0.0"),
("refusal", "core", "1.0.0"),
]
assert result[1]["licence"] == ["AGPL-3.0-or-later"]
main(["concepts", "show", "refusal", "--json"])
manifest = json.loads(capsys.readouterr().out)
assert manifest == builtin_refusal_pack().manifest
@pytest.mark.parametrize("options,enabled,cap", [
([], False, 64),
(["--snapshot", "disk", "--snapshot-max-gib", "256"], True, 256),
])
def test_baseline_forwards_extraction_pin_and_snapshot_policy(monkeypatch, tmp_path, options, enabled, cap):
from unittest.mock import Mock
run = Mock(return_value=tmp_path)
monkeypatch.setattr("obliteratus.concepts.baseline.run_baseline", run)
main(["concepts", "baseline", "pain", "local-model", "--output-dir", str(tmp_path),
"--extraction-layer", "0", *options])
assert run.call_args.kwargs["extraction_layer"] == 0
assert run.call_args.kwargs["snapshot_enabled"] is enabled
assert run.call_args.kwargs["snapshot_max_bytes"] == cap * 1024**3
@pytest.mark.parametrize("command", ["obliterate", "abliterate"])
def test_extraction_pin_forwarded_to_pipeline(monkeypatch, command):
from unittest.mock import Mock
constructor = Mock()
monkeypatch.setattr("obliteratus.abliterate.AbliterationPipeline", constructor)
main([command, "offline", "--extraction-layer", "24"])
assert constructor.call_args.kwargs["extraction_layer"] == 24
@pytest.mark.parametrize("value", ["-1", "true", "1.5", "1,2"])
@pytest.mark.parametrize("command", [
["obliterate", "offline"],
["concepts", "baseline", "pain", "offline", "--output-dir", "unused"],
])
def test_extraction_pin_cli_rejects_invalid_indices(command, value):
with pytest.raises(SystemExit) as error:
main([*command, "--extraction-layer", value])
assert error.value.code == 2
def test_remote_extraction_pin_rejected_before_connection(monkeypatch):
def no_connection(*args):
pytest.fail("Concept extraction override must be rejected before connecting")
monkeypatch.setattr("obliteratus.cli._make_remote_runner", no_connection)
with pytest.raises(SystemExit) as error:
main(["obliterate", "offline", "--remote", "host", "--extraction-layer", "24"])
assert error.value.code == 2
def test_init_lint_pack_import_and_list_roundtrip(tmp_path, capsys):
source = tmp_path / "source"
main(["concepts", "init", str(source), "--id", "new-concept"])
capsys.readouterr()
main(["concepts", "lint", str(source), "--json"])
assert json.loads(capsys.readouterr().out)["valid"] is True
first, second = tmp_path / "a.cvpack", tmp_path / "b.cvpack"
main(["concepts", "pack", str(source), str(first)])
main(["concepts", "pack", str(source), str(second)])
assert first.read_bytes() == second.read_bytes()
main(["concepts", "import", str(first), "--sha256", digest(first.read_bytes())])
capsys.readouterr()
main(["concepts", "list", "--json"])
assert {row["id"] for row in json.loads(capsys.readouterr().out)} == {
"pain", "refusal", "user/new-concept",
}
main(["concepts", "show", "user/new-concept", "--json"])
assert json.loads(capsys.readouterr().out)["tier"] == "user"
@pytest.mark.parametrize("fixture,reason", [
("invalid_hash", "positive.jsonl: SHA-256 mismatch; expected " + "0" * 64),
("invalid_licence", "missing spdx"), ("invalid_pickle", "Forbidden data file"),
])
@pytest.mark.parametrize("command", ["lint", "import"])
def test_invalid_pack_commands_exit_four(fixture, reason, command, capsys):
with pytest.raises(SystemExit) as error:
main(["concepts", command, str(FIXTURES / fixture)])
assert error.value.code == 4
assert reason in capsys.readouterr().err
def test_unknown_major_requires_upgrade_and_missing_url_sha_fails_early(tmp_path, capsys):
pack = ConceptPack.load(FIXTURES / "valid_minimal")
pack.manifest["schema_version"] = "2.0"
import yaml
pack.files["manifest.yaml"] = yaml.safe_dump(pack.manifest).encode()
source = tmp_path / "unknown.cvpack"
source.write_bytes(zip_bytes(pack.files))
with pytest.raises(SystemExit) as error:
main(["concepts", "lint", str(source)])
assert error.value.code == 4
assert "upgrade" in capsys.readouterr().err
with pytest.raises(SystemExit) as error:
main(["concepts", "import", "https://example.org/test.cvpack"])
assert error.value.code == 4
assert "requires --sha256" in capsys.readouterr().err
def test_remote_lint_fetch_import_fake_transport(tmp_path, monkeypatch, capsys):
import shutil
from obliteratus.concepts import pack as pack_module
source = tmp_path / "remote"
shutil.copytree(FIXTURES / "valid_minimal", source)
payload = (source / "positive.jsonl").read_bytes()
manifest = manifest_at(source)
entry = manifest["sets"]["positive"]
del entry["path"]
entry["remote"] = {"repo": "https://github.com/example/data", "commit": "a" * 40,
"path": "data/present.jsonl"}
manifest["provenance"] = {"source_commit": "a" * 40}
update_manifest(source, manifest)
(source / "positive.jsonl").unlink()
calls = []
class Opener:
def open(self, request, timeout):
calls.append((request.full_url, timeout))
data = payload if "raw.githubusercontent.com" in request.full_url else archive
return BytesIO(data)
monkeypatch.setattr(pack_module, "build_opener", lambda *args: Opener())
main(["concepts", "lint", str(source), "--json"])
assert json.loads(capsys.readouterr().out)["valid"] is True
main(["concepts", "show", str(source), "--json"])
assert json.loads(capsys.readouterr().out)["sets"]["positive"]["remote"]
assert calls == []
main(["concepts", "fetch", str(source), "--json"])
assert json.loads(capsys.readouterr().out)["fetched"] is True
assert calls == [("https://raw.githubusercontent.com/example/data/" + "a" * 40
+ "/data/present.jsonl", 30)]
archive = zip_bytes(ConceptPack.load(source).files)
main(["concepts", "import", "https://example.org/remote.cvpack", "--sha256", digest(archive)])
assert len(calls) == 2
capsys.readouterr()
main(["concepts", "list", "--json"])
assert "user/synthetic" in {row["id"] for row in json.loads(capsys.readouterr().out)}
@pytest.mark.parametrize("command", [
["init", "existing"], ["init", "new", "--id", "../bad"],
["pack", str(FIXTURES / "valid_minimal"), "bad.zip"], ["show", "missing"],
])
def test_bad_local_operations_exit_four(tmp_path, monkeypatch, command):
monkeypatch.chdir(tmp_path)
Path("existing").mkdir()
with pytest.raises(SystemExit) as error:
main(["concepts", *command])
assert error.value.code == 4
assert not Path("bad.zip").exists()
assert not Path("new").exists()
def test_show_unknown_optional_yaml_date_is_json_serializable(tmp_path, capsys):
source = tmp_path / "source"
main(["concepts", "init", str(source)])
manifest = manifest_at(source)
from datetime import date
manifest["future_created_at"] = date(2026, 10, 5)
update_manifest(source, manifest)
capsys.readouterr()
main(["concepts", "show", str(source), "--json"])
assert json.loads(capsys.readouterr().out)["future_created_at"] == "2026-10-05"
@pytest.mark.parametrize("name", ["refusal", "core/refusal", "user/synthetic"])
def test_security_review_registered_ids_precede_local_paths(tmp_path, monkeypatch, capsys, name):
installed = import_pack(FIXTURES / "valid_minimal")
expected = installed if name == "user/synthetic" else builtin_refusal_pack()
shadow = tmp_path / name
shutil.copytree(FIXTURES / "valid_minimal", shadow)
manifest = manifest_at(shadow)
manifest["id"] = "shadow"
update_manifest(shadow, manifest)
monkeypatch.chdir(tmp_path)
main(["concepts", "lint", name, "--json"])
assert json.loads(capsys.readouterr().out)["sha256"] == expected.sha256
def test_security_review_bare_directory_requires_explicit_path(tmp_path, monkeypatch, capsys):
shutil.copytree(FIXTURES / "valid_minimal", tmp_path / "local")
monkeypatch.chdir(tmp_path)
with pytest.raises(SystemExit) as error:
main(["concepts", "show", "local", "--json"])
assert error.value.code == 4
assert "Unknown concept pack" in capsys.readouterr().err
main(["concepts", "show", "./local", "--json"])
assert json.loads(capsys.readouterr().out)["id"] == "synthetic"
def test_security_review_archive_suffix_is_an_explicit_path(tmp_path, monkeypatch, capsys):
ConceptPack.load(FIXTURES / "valid_minimal").write(tmp_path / "local.cvpack")
monkeypatch.chdir(tmp_path)
main(["concepts", "show", "local.cvpack", "--json"])
assert json.loads(capsys.readouterr().out)["id"] == "synthetic"
@pytest.mark.parametrize("command", [["list"], ["show", "refusal"], ["show", "user/synthetic"]])
def test_security_review_corrupt_cache_warns_without_blocking_other_packs(capsys, command):
installed = import_pack(FIXTURES / "valid_minimal")
bad = cache_directory() / "packs" / "bad.cvpack"
bad.write_bytes(b"not a zip")
main(["concepts", *command, "--json"])
output = capsys.readouterr()
assert "Warning" in output.err
assert str(bad) in output.err
result = json.loads(output.out)
if command == ["list"]:
assert {row["id"] for row in result} == {"pain", "refusal", "user/synthetic"}
else:
assert result == (installed if command[-1] == "user/synthetic"
else builtin_refusal_pack()).manifest
@pytest.mark.parametrize("command,target", [
(command, target) for command in ("show", "lint", "fetch", "pack")
for target in ("id", "path")
] + [("import", "path")])
def test_security_review_corrupt_cache_target_fails_naming_file(tmp_path, capsys, command, target):
import_pack(FIXTURES / "valid_minimal")
bad = cache_directory() / "packs" / "bad.cvpack"
bad.write_bytes(b"not a zip")
source = "user/bad" if target == "id" else str(bad)
arguments = ["concepts", command, source]
if command == "pack":
arguments.append(str(tmp_path / "output.cvpack"))
elif command != "import":
arguments.append("--json")
with pytest.raises(SystemExit) as error:
main(arguments)
assert error.value.code == 4
output = capsys.readouterr()
assert str(bad) in output.err
assert "executable prefix" in output.err
if command not in {"pack", "import"}:
assert output.out == ""
assert not (tmp_path / "output.cvpack").exists()