Files
Sanctification/tools/card-production/tests/test_asset_archive.py
Daniel 57202b90b1 Move card binaries to Git LFS and preserve archived revisions
Keep permanent image assets and accepted exports in Git LFS at their existing paths. Stop tracking temporary card review renders and caches while preserving local files.

Archive source inputs, exact exports and text evidence by SHA-256 before compacting superseded revisions, with integrity verification and restoration support. Keep existing Git history and pending card/harness work separate.

Validation: 35 production tests passed against an isolated checkout of the staged migration; local image checksums and LFS pointers were verified.
2026-10-04 17:46:31 -07:00

117 lines
5.2 KiB
Python

"""Archives preserve originals before destructive compaction and refuse corrupt data."""
import json
from pathlib import Path
import sys
import tempfile
import unittest
sys.path.insert(0, str(Path(__file__).resolve().parents[1]))
import asset_archive as archive
class AssetArchiveTests(unittest.TestCase):
def setUp(self):
self.temp = tempfile.TemporaryDirectory()
self.repo = Path(self.temp.name)
self.package = self.repo / 'artifacts/cards/BP-999-test'
self.package.mkdir(parents=True)
self.write('manifest.json', json.dumps({
'cardId': 'BP-999', 'revision': 'v01', 'stage': 'approved',
'approval': {'by': 'user', 'note': 'Fixture approval'},
}).encode())
self.write('source/art-native.png', b'irreplaceable native fixture')
self.write('source/generation-prompt.txt', b'exact prompt')
self.write('high/normal/card.png', b'approved printing fixture')
self.write('review/build-validation.json', b'{"status":"passed"}')
self.write('review/crop.png', b'temporary review crop')
self.write('review/font-cache/cache.bin', b'cache fixture')
def tearDown(self):
self.temp.cleanup()
def write(self, name, data):
path = self.package / name
path.parent.mkdir(parents=True, exist_ok=True)
path.write_bytes(data)
return path
def snapshot(self):
record = archive.archive_revision(self.package, self.repo)
return self.repo / record['manifest']
def test_restores_native_art_exact_exports_and_text_evidence(self):
manifest_before = (self.package / 'manifest.json').read_bytes()
snapshot = self.snapshot()
data, _ = archive.verify_snapshot(snapshot)
self.assertNotIn('review/crop.png', data['files'])
self.assertNotIn('review/font-cache/cache.bin', data['files'])
output = archive.restore_snapshot(snapshot, self.repo / 'recovered')
for name in ['manifest.json', 'source/art-native.png', 'source/generation-prompt.txt',
'high/normal/card.png', 'review/build-validation.json']:
self.assertEqual((output / name).read_bytes(), (self.package / name).read_bytes())
self.assertEqual((self.package / 'manifest.json').read_bytes(), manifest_before)
def test_deduplicates_identical_bytes_and_repeated_snapshots(self):
self.write('source/art-master.png', b'irreplaceable native fixture')
first = self.snapshot()
data, store = archive.verify_snapshot(first)
count = len(list((store / 'objects').glob('*/*')))
self.assertEqual(self.snapshot(), first)
self.assertEqual(len(list((store / 'objects').glob('*/*'))), count)
self.assertEqual(data['files']['source/art-native.png']['sha256'],
data['files']['source/art-master.png']['sha256'])
def test_retains_linked_input_bytes_even_after_original_is_removed(self):
external = self.repo / 'working-art.png'
external.write_bytes(b'linked original fixture')
link = self.package / 'source/linked-art.png'
link.symlink_to(external)
snapshot = self.snapshot()
external.unlink()
restored = archive.restore_snapshot(snapshot, self.repo / 'recovered')
self.assertEqual((restored / 'source/linked-art.png').read_bytes(), b'linked original fixture')
self.assertFalse((restored / 'source/linked-art.png').is_symlink())
def test_corrupt_object_blocks_archive_and_restore(self):
snapshot = self.snapshot()
data, store = archive.verify_snapshot(snapshot)
source = archive.object_path(store, data['files']['source/art-native.png']['sha256'])
source.write_bytes(b'corrupt fixture')
with self.assertRaises(ValueError):
archive.archive_revision(self.package, self.repo)
with self.assertRaises(ValueError):
archive.restore_snapshot(snapshot, self.repo / 'recovered')
self.assertFalse((self.repo / 'recovered').exists())
def test_changed_input_creates_another_immutable_snapshot(self):
first = self.snapshot()
before = first.read_bytes()
self.write('source/art-native.png', b'new native fixture')
second = self.snapshot()
self.assertNotEqual(first, second)
self.assertEqual(first.read_bytes(), before)
archive.verify_snapshot(first)
archive.verify_snapshot(second)
def test_broken_link_blocks_archival(self):
(self.package / 'source/missing.png').symlink_to('no-such-input.png')
with self.assertRaisesRegex(ValueError, 'broken input'):
self.snapshot()
def test_restore_rejects_overwriting_existing_work(self):
snapshot = self.snapshot()
with self.assertRaisesRegex(ValueError, 'empty directory'):
archive.restore_snapshot(snapshot, self.package)
def test_tampered_manifest_is_rejected(self):
snapshot = self.snapshot()
data = json.loads(snapshot.read_text())
data['files']['../escape.png'] = data['files']['source/art-native.png']
snapshot.write_text(json.dumps(data))
with self.assertRaisesRegex(ValueError, 'snapshot hash'):
archive.verify_snapshot(snapshot)
if __name__ == '__main__':
unittest.main()