PureOne's picture
Publish Qyxo-Zhaenivoth Guard v1.0.0 research release
25c6e75 verified
Raw History Blame Contribute Delete
7.13 kB
"""Public CLI integration tests run through real subprocess entry points."""
import json
import os
from pathlib import Path
import subprocess
import sys
import tempfile
import unittest
ROOT = Path(__file__).resolve().parents[1]
def cli(*args, input=None):
return subprocess.run([sys.executable, "-m", "qzguard", *args], cwd=ROOT,
input=input, text=True, capture_output=True)
class CliTests(unittest.TestCase):
def test_help_version_demo_and_doctor(self):
help_result = cli("--help")
self.assertEqual(help_result.returncode, 0, help_result.stderr)
self.assertIn("check", help_result.stdout)
self.assertEqual(cli("--version").stdout.strip(), "1.0.0")
for command in ("demo", "doctor"):
result = cli(command, "--backend", "exact")
self.assertEqual(result.returncode, 0, result.stderr)
data = json.loads(result.stdout)
self.assertEqual(data["status"], "ok")
self.assertEqual(json.loads(cli("demo").stdout)["backend"], "exact")
def test_single_stdin_exact_and_rational_details(self):
data = {"value_lower": [0., 1.], "mass_lower": [1., 1.],
"mass_upper": [1., 1.], "threshold": .9, "relation": ">="}
result = cli("check", "--input", "-", "--witness", input=json.dumps(data))
self.assertEqual(result.returncode, 0, result.stderr)
record = json.loads(result.stdout)
self.assertEqual(record["backend"], "exact")
self.assertEqual(record["guaranteed"], [False])
self.assertEqual(record["counterexample_exists"], [True])
self.assertIn("integer-exact", record["numerical_contract"])
self.assertEqual(record["witnesses"][0]["ratio"], {"numerator": "1", "denominator": "2"})
self.assertTrue(record["exact_details_requested"])
def test_stream_results_errors_and_output(self):
with tempfile.TemporaryDirectory() as directory:
directory = Path(directory)
bank = directory / "bank.json"
updates = directory / "updates.jsonl"
output = directory / "output.jsonl"
bank.write_text(json.dumps({"value_lower": [0., 1.]}), encoding="utf-8")
updates.write_text('\n' + json.dumps({"id": "first", "mass_lower": [1., 100.],
"mass_upper": [1., 1e6], "threshold": .9, "relation": ">="}) +
'\n{"mass_lower":[0,0],"mass_upper":[1,1],"threshold":0}\n' +
json.dumps({"id": "last", "mass_lower": [1., 1.], "mass_upper": [1., 1.],
"threshold": .4}) + '\n', encoding="utf-8")
result = cli("check", "--bank", str(bank), "--updates", str(updates),
"--output", str(output))
self.assertEqual(result.returncode, 2)
self.assertEqual(result.stdout, "")
records = [json.loads(line) for line in output.read_text().splitlines()]
self.assertEqual([r["status"] for r in records], ["ok", "error", "ok"])
self.assertEqual([r["line"] for r in records], [2, 3, 4])
self.assertEqual(records[0]["guaranteed"], [True])
self.assertEqual(records[-1]["guaranteed"], [False])
self.assertIn("processed 3 updates; 1 errors", result.stderr)
self.assertNotIn("Traceback", result.stderr)
def test_stream_stdin(self):
with tempfile.TemporaryDirectory() as directory:
bank = Path(directory) / "bank.json"
bank.write_text('{"value_lower":[1]}', encoding="utf-8")
result = cli("check", "--bank", str(bank), "--updates", "-",
input='{"mass_lower":[1],"mass_upper":[2],"threshold":1}\n')
self.assertEqual(result.returncode, 0, result.stderr)
self.assertEqual(json.loads(result.stdout)["guaranteed"], [True])
def test_strict_numbers_fields_and_shape(self):
fixtures = [
'{"value_lower":[NaN],"mass_lower":[1],"mass_upper":[1],"threshold":0}',
'{"value_lower":[1e999],"mass_lower":[1],"mass_upper":[1],"threshold":0}',
'{"value_lower":[1],"value_lower":[2],"mass_lower":[1],"mass_upper":[1],"threshold":0}',
'{"value_lower":[true],"mass_lower":[1],"mass_upper":[1],"threshold":0}',
'{"value_lower":[1],"mass_lower":["1"],"mass_upper":[1],"threshold":0}',
'{"value_lower":[1],"mass_lower":[1],"mass_upper":[1],"threshold":0,"typo":0}',
'{"value_lower":[[1,2]],"mass_lower":[1],"mass_upper":[1],"threshold":[0]}',
]
for fixture in fixtures:
result = cli("check", "--input", "-", input=fixture)
self.assertEqual(result.returncode, 2, fixture)
self.assertEqual(result.stdout, "")
self.assertNotIn("Traceback", result.stderr)
def test_preflight_does_not_destroy_output(self):
with tempfile.TemporaryDirectory() as directory:
output = Path(directory) / "output.jsonl"
output.write_text("keep me", encoding="utf-8")
result = cli("check", "--bank", str(Path(directory)/"missing.json"),
"--updates", "-", "--output", str(output), input="")
self.assertEqual(result.returncode, 2)
self.assertEqual(output.read_text(), "keep me")
result = cli("check", "--input", "-", "--output", str(output), input="{}")
self.assertEqual(result.returncode, 2)
self.assertEqual(output.read_text(), "keep me")
def test_output_cannot_overwrite_input(self):
with tempfile.TemporaryDirectory() as directory:
source = Path(directory) / "input.json"
original = '{"value_lower":[1],"mass_lower":[1],"mass_upper":[1],"threshold":0}'
source.write_text(original, encoding="utf-8")
result = cli("check", "--input", str(source), "--output", str(source))
self.assertEqual(result.returncode, 2)
self.assertEqual(source.read_text(), original)
def test_output_cannot_overwrite_hardlink_alias(self):
with tempfile.TemporaryDirectory() as directory:
source = Path(directory) / "input.json"
alias = Path(directory) / "alias.json"
original = '{"value_lower":[1],"mass_lower":[1],"mass_upper":[1],"threshold":0}'
source.write_text(original, encoding="utf-8")
try:
os.link(source, alias)
except OSError as exc:
self.skipTest(f"hard links unavailable: {exc}")
result = cli("check", "--input", str(source), "--output", str(alias))
self.assertEqual(result.returncode, 2)
self.assertEqual(source.read_text(), original)
self.assertEqual(alias.read_text(), original)
def test_missing_update_option_no_traceback(self):
result = cli("check", "--bank", "anything.json")
self.assertEqual(result.returncode, 2)
self.assertIn("--updates", result.stderr)
self.assertNotIn("Traceback", result.stderr)
if __name__ == "__main__":
unittest.main()