Files
Neutron/tests/test_fidelity_ledger.py
T

211 lines
10 KiB
Python

import copy
import importlib
import json
from pathlib import Path
import subprocess
import sys
import tempfile
import unittest
ROOT = Path(__file__).resolve().parents[1]
sys.path.insert(0, str(ROOT / 'tools/recon'))
TOOL = ROOT / 'tools/recon/fidelity_ledger.py'
LEDGER = ROOT / 'docs/evidence/capabilities.json'
def evidence(identifier='proof-a', **changes):
value = {
'id': identifier, 'receipt_id': identifier, 'evidence_kind': 'port_fixture',
'method': 'bounded fixture', 'revision': 'a' * 40, 'historical': True,
'content': {'runtime_sha256': '1' * 64, 'resources_sha256': '2' * 64},
'platform': {'os': 'linux', 'execution': 'native'},
'scope': ['Carl first neutral input'], 'status': 'current',
'claim': {'key': 'neutral-return', 'result': 'upright'},
'supersedes': [], 'reason': None, 'artifacts': [],
'missing_evidence': ['Original and native Windows comparison absent'],
'report_path': 'docs/gameplay/main-menu-player-lifetime.md',
}
value.update(changes)
return value
def target(record=None):
row = record or evidence()
return {key: copy.deepcopy(row[key]) for key in ('content', 'platform', 'scope')}
def capability(identifier='startup-order'):
row = evidence()
return {
'id': identifier, 'group': 'movement', 'requirement_ids': ['MOVE-04'],
'summary': 'TSK story entry preserves retained Jimmy-before-input order.',
'verified_scope': 'Historical Linux fixture; direct fresh scene remains separate.',
'differences': ['Whole-original audiovisual comparison remains absent.'],
'acceptance': {'status': 'open', 'remaining': ['Original and native Windows comparison']},
'source_links': [{'label': 'Source lifetime', 'path': row['report_path']}],
'scenario_ids': ['fresh-natural-ordinary'], 'implementation': {'revision': 'a' * 40,
'paths': ['src/GamePort/Program.cs'], 'status': 'bounded'},
'targets': [dict(id='historical-linux', **target(row))],
'evidence': [row], 'discrepancies': [{'id': 'reference-gap',
'impact': 'missing_verification', 'summary': 'Missing whole-original recording'}],
}
def ledger(*rows):
return {'schema_version': 1, 'capabilities': list(rows)}
class LedgerTests(unittest.TestCase):
def api(self):
self.assertTrue(TOOL.is_file(), 'Capability ledger CLI must exist')
return importlib.import_module('fidelity_ledger')
def test_real_startup_capability_cli_and_viewer_roundtrip(self):
self.assertTrue(TOOL.is_file(), 'Capability ledger CLI must exist')
with tempfile.TemporaryDirectory() as temporary:
exported = Path(temporary) / 'ledger.json'
result = subprocess.run([sys.executable, str(TOOL), 'export', '--ledger', str(LEDGER),
'--output', str(exported)], text=True, capture_output=True)
self.assertEqual(result.returncode, 0, result.stderr)
model = json.loads(exported.read_text())
row = next(row for row in model['capabilities'] if row['id'] == 'startup-order')
self.assertIn('already corrected', row['summary'])
self.assertEqual(row['acceptance']['status'], 'open')
self.assertTrue(row['current'])
self.assertTrue(row['history'])
from fidelity_review import export_review
reviewed = export_review(None, None, Path(temporary) / 'review', exported)
loaded = json.loads((Path(temporary) / 'review/review.json').read_text())
self.assertEqual(reviewed, loaded)
nav = next(row for row in loaded['ledger']['capabilities'] if row['id'] == 'startup-order')
self.assertEqual(nav['current'], row['current'])
self.assertEqual(nav['history'], row['history'])
self.assertTrue(all(link['validated_local'] for link in nav['source_links']))
for link in nav['source_links']:
self.assertTrue((Path(temporary) / 'review' / link['href']).is_file())
self.assertIn('native Windows', str(nav['acceptance']))
self.assertEqual(loaded['acceptance']['state'], 'open')
def test_empty_ledger_fails(self):
with self.assertRaises(ValueError): self.api().validate_ledger(ledger(), ROOT)
def test_single_capability_renders_with_open_acceptance(self):
api = self.api()
text = api.render_markdown(ledger(capability()), ROOT)
self.assertIn('TSK story entry', text)
self.assertIn('OPEN', text)
self.assertIn('History', text)
def test_duplicate_capability_conflict_rejected(self):
row = capability()
other = copy.deepcopy(row)
other['summary'] = 'contradictory'
with self.assertRaises(ValueError): self.api().validate_ledger(ledger(row, other), ROOT)
def test_equal_record_ids_merge_only_equal_records(self):
row = capability()
row['evidence'].append(copy.deepcopy(row['evidence'][0]))
model = self.api().export_ledger(ledger(row), ROOT)
self.assertEqual(len(model['capabilities'][0]['evidence']), 1)
row['evidence'][-1]['method'] = 'changed bytes'
with self.assertRaises(ValueError): self.api().validate_ledger(ledger(row), ROOT)
def test_conflicting_shared_receipt_identity_rejected(self):
row = capability()
row['evidence'].append(evidence('other-id', receipt_id='proof-a', method='different'))
with self.assertRaises(ValueError): self.api().validate_ledger(ledger(row), ROOT)
def test_equal_priority_has_stable_id_order(self):
api = self.api()
a, b = capability('zeta'), capability('alpha')
self.assertEqual([r['id'] for r in api.export_ledger(ledger(a, b), ROOT)['capabilities']],
['alpha', 'zeta'])
def test_supersession_cycle_rejected(self):
row = capability()
row['evidence'] = [evidence('a', supersedes=['b'], reason='replace'),
evidence('b', supersedes=['a'], reason='replace')]
with self.assertRaises(ValueError): self.api().validate_ledger(ledger(row), ROOT)
def test_unresolved_contradictory_current_claims_fail(self):
row = capability()
row['evidence'].append(evidence('other', claim={'key': 'neutral-return', 'result': 'tilted'}))
with self.assertRaises(ValueError): self.api().validate_ledger(ledger(row), ROOT)
def test_explicit_supersession_retains_prior_claim(self):
row = capability()
row['evidence'][0].update(status='superseded')
row['evidence'].append(evidence('new', supersedes=['proof-a'], reason='Source correction',
claim={'key': 'neutral-return', 'result': 'corrected'}))
model = self.api().export_ledger(ledger(row), ROOT)['capabilities'][0]
self.assertEqual([r['id'] for r in model['current']], ['new'])
self.assertEqual([r['id'] for r in model['history']], ['proof-a'])
def test_unsafe_dead_and_tampered_source_links_fail(self):
api = self.api()
for path in ('../Original/Neutron.exe', '/etc/passwd', 'C:/secret',
'artifacts/private.json', 'docs/nonexistent-ledger-source.md'):
with self.subTest(path=path):
row = capability()
row['source_links'][0]['path'] = path
with self.assertRaises(ValueError): api.validate_ledger(ledger(row), ROOT)
row = capability()
row['source_links'][0]['sha256'] = '0' * 64
with self.assertRaises(ValueError): api.validate_ledger(ledger(row), ROOT)
def test_docs_revision_does_not_change_content_applicability(self):
api = self.api()
row = evidence()
current = target(row)
current['source_revision'] = 'b' * 40
self.assertEqual(api.select_current_evidence([row], current), [row])
def test_changed_package_or_resources_is_not_applicable(self):
api = self.api()
for key in ('runtime_sha256', 'resources_sha256', 'package_sha256'):
row = evidence()
changed = target(row)
changed['content'][key] = '3' * 64
self.assertEqual(api.select_current_evidence([row], changed), [])
def test_mismatched_platform_or_scope_is_not_applicable(self):
api = self.api()
for key, value in (('platform', {'os': 'windows', 'execution': 'native'}),
('scope', ['whole campaign'])):
changed = target()
changed[key] = value
self.assertEqual(api.select_current_evidence([evidence()], changed), [])
def test_missing_artifact_is_explicit_and_never_accepted(self):
row = capability()
row['evidence'][0]['artifacts'] = [{'path': 'artifacts/fidelity/never-captured.webm',
'missing_reason': 'No recording was made'}]
exported = self.api().export_ledger(ledger(row), ROOT)['capabilities'][0]
self.assertEqual(exported['acceptance']['status'], 'open')
self.assertIn('No recording', str(exported['evidence']))
row['acceptance']['status'] = 'accepted'
with self.assertRaises(ValueError): self.api().validate_ledger(ledger(row), ROOT)
def test_invalid_receipt_classification_rejected(self):
row = capability()
row['evidence'][0]['evidence_kind'] = 'native_port'
row['evidence'][0]['platform'] = {'os': 'windows', 'execution': 'wine'}
with self.assertRaises(ValueError): self.api().validate_ledger(ledger(row), ROOT)
def test_nonfinite_or_oversized_input_rejected(self):
row = capability()
row['evidence'][0]['elapsed'] = float('nan')
with self.assertRaises(ValueError): self.api().validate_ledger(ledger(row), ROOT)
row = capability()
row['summary'] = 'x' * 20000
with self.assertRaises(ValueError): self.api().validate_ledger(ledger(row), ROOT)
def test_duplicate_history_is_not_silently_overwritten(self):
row = capability()
row['evidence'] = [evidence('old', status='historical'), evidence('old', status='superseded')]
with self.assertRaises(ValueError): self.api().validate_ledger(ledger(row), ROOT)
if __name__ == '__main__':
unittest.main()