"""End-to-end checks of the source CLI and isolated standalone zipapp.""" from __future__ import annotations import base64 import hashlib import json import os from pathlib import Path import subprocess import sys import tempfile import unittest ROOT = Path(__file__).resolve().parents[1] class CliTests(unittest.TestCase): def setUp(self): self.temporary = tempfile.TemporaryDirectory(prefix='nyarion-cli-') self.work = Path(self.temporary.name) self.image = self.work / 'test.img' self.cli('init', self.image, '--pool-blocks', '1024') def tearDown(self): self.temporary.cleanup() def cli(self, *args, input_data=None, expected=0, launcher=None, cwd=None, env=None): result = subprocess.run([*(launcher or [sys.executable, '-m', 'nyarion']), *(str(arg) for arg in args)], cwd=cwd or ROOT, env=env, input=input_data, stdout=subprocess.PIPE, stderr=subprocess.PIPE) self.assertEqual(result.returncode, expected, result.stderr.decode('utf8', 'replace')) return result def image_hash(self): return hashlib.sha256(self.image.read_bytes()).hexdigest() def test_binary_value_roundtrip_and_new_output_file(self): value = bytes(range(256)) * 8 source, output = self.work / 'input.bin', self.work / 'output.bin' source.write_bytes(value) self.cli('put', self.image, '/binary', '--file', source, '--token', 'binary-1') self.assertEqual(self.cli('get', self.image, '/binary').stdout, value) self.cli('get', self.image, '/binary', '--output', output) self.assertEqual(output.read_bytes(), value) self.cli('get', self.image, '/binary', '--output', output, expected=2) self.assertEqual(output.read_bytes(), value) def test_retry_conflict_and_receipt_persistence(self): first = self.cli('put', self.image, '/hello', '--text', 'hello', '--token', 'same') again = self.cli('put', self.image, '/hello', '--text', 'hello', '--token', 'same') self.assertEqual(json.loads(first.stdout), json.loads(again.stdout)) before = self.image_hash() self.cli('put', self.image, '/hello', '--text', 'changed', '--token', 'same', expected=2) self.assertEqual(self.image_hash(), before) receipt = json.loads(self.cli('query', self.image, 'same').stdout) self.assertEqual(receipt['epoch'], 1) self.assertEqual(self.cli('get', self.image, '/hello').stdout, b'hello') def test_multi_key_transaction_and_delete_are_atomic_requests(self): self.cli('put', self.image, '/old', '--text', 'old', '--token', 'old') transaction = {'token': 'multi', 'changes': {'/old': None, '/a': {'base64': base64.b64encode(b'a').decode()}, '/b': {'base64': base64.b64encode(b'\0\xff').decode()}}} raw = json.dumps(transaction).encode() self.cli('transact', self.image, '-', input_data=raw) self.assertEqual(json.loads(self.cli('ls', self.image).stdout), ['/a', '/b']) self.assertEqual(self.cli('get', self.image, '/b').stdout, b'\0\xff') self.cli('delete', self.image, '/a', '--token', 'delete-a') self.assertEqual(json.loads(self.cli('list', self.image).stdout), ['/b']) def test_invalid_transaction_preserves_whole_image(self): before = self.image_hash() invalid = b'{"token":"duplicate","changes":{"/a":{"base64":"YQ=="},"/a":null}}' self.cli('transact', self.image, '-', input_data=invalid, expected=2) invalid = b'{"token":"bad","changes":{"/a":{"base64":"YQ=="},"relative":null}}' self.cli('transact', self.image, '-', input_data=invalid, expected=2) self.assertEqual(self.image_hash(), before) self.assertEqual(json.loads(self.cli('ls', self.image).stdout), []) def test_init_refuses_existing_image(self): before = self.image_hash() self.cli('init', self.image, expected=2) self.assertEqual(self.image_hash(), before) def test_export_digest_and_readonly_verification(self): self.cli('put', self.image, '/text', '--text', 'zażółć', '--token', 'text-1') before = self.image_hash() verified = json.loads(self.cli('verify', self.image).stdout) snapshot = json.loads(self.cli('export', self.image).stdout) snapshot_digest = snapshot.pop('snapshot_sha256') canonical = json.dumps(snapshot, sort_keys=True, separators=(',', ':'), ensure_ascii=True).encode('ascii') self.assertEqual(hashlib.sha256(canonical).hexdigest(), snapshot_digest) self.assertEqual(verified['snapshot_sha256'], snapshot_digest) self.assertEqual(base64.b64decode(snapshot['files'][0]['base64']), 'zażółć'.encode()) self.assertEqual(snapshot['receipts'][0]['epoch'], 1) self.assertEqual(self.image_hash(), before) def test_checkpoint_and_empty_value(self): self.cli('put', self.image, '/empty', '--file', '-', '--token', 'empty', input_data=b'') checkpoint = json.loads(self.cli('checkpoint', self.image).stdout) self.assertEqual(checkpoint['checkpoint_epoch'], 1) self.assertEqual(self.cli('get', self.image, '/empty').stdout, b'') inspected = json.loads(self.cli('inspect', self.image).stdout) self.assertEqual(inspected['tokens'], 1) self.assertEqual(inspected['epoch'], 1) def test_plan_is_readonly_and_reports_force_boundaries(self): self.cli('put', self.image, '/planned', '--text', 'planned', '--token', 'planned') before = self.image_hash() plan = json.loads(self.cli('plan', self.image).stdout) self.assertEqual(plan['epoch'], 1) self.assertEqual(plan['force_boundaries'], [plan['physical_blocks'], plan['physical_blocks'] + 2]) self.assertEqual(plan['bytes_written'], plan['physical_blocks'] * 4096 + 1024) self.assertEqual(self.image_hash(), before) def test_missing_get_does_not_create_output(self): output = self.work / 'missing-output.bin' self.cli('get', self.image, '/missing', '--output', output, expected=2) self.assertFalse(output.exists()) def test_zipapp_runs_outside_source_tree_without_pythonpath(self): artifacts = self.work / 'artifacts' built = subprocess.run([sys.executable, str(ROOT / 'build_release.py'), '--output-dir', str(artifacts)], cwd=self.work, stdout=subprocess.PIPE, stderr=subprocess.PIPE) self.assertEqual(built.returncode, 0, built.stderr.decode()) artifact = artifacts / 'nyarion.pyz' environment = dict(os.environ) environment.pop('PYTHONPATH', None) launcher = [sys.executable, '-I', str(artifact)] isolated = self.work / 'isolated.img' self.cli('init', isolated, '--pool-blocks', '512', launcher=launcher, cwd=self.work, env=environment) self.cli('put', isolated, '/standalone', '--text', 'works', '--token', 'zipapp-1', launcher=launcher, cwd=self.work, env=environment) self.assertEqual(self.cli('get', isolated, '/standalone', launcher=launcher, cwd=self.work, env=environment).stdout, b'works') self.assertEqual(json.loads(self.cli('verify', isolated, launcher=launcher, cwd=self.work, env=environment).stdout)['receipts'], 1) if __name__ == '__main__': unittest.main()