Files
WoW-Hybrid-gateway/tests/octo_source_resolution_test.py
praxisandClaude Fable 5.2 7f2139d8d0 Offline test fixtures
883 files, from working commit b1a079a.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
2026-11-19 01:32:48 +02:00

118 lines
7.2 KiB
Python

#!/usr/bin/env python3
"""Pure Octo provenance resolution: no archive reader, client, DB or output files."""
from copy import deepcopy
import json
from pathlib import Path
import sys
from unittest.mock import patch
sys.path.insert(0,str(Path(__file__).resolve().parents[1]/'ops'))
import octo_client_data as octo
A='a'*64;B='b'*64;C='c'*64
def manifest():
return dict(source_installation='/local/Octowow',reader_source_sha256=C,
effective_patch_order_verified=False,
archives=[dict(file='patch-Z.mpq',bytes=1000,open_result=0,sha256=A),
dict(file='patch-A.mpq',bytes=2000,open_result=0,sha256=B)],
candidates=[dict(table='Spell',archive='patch-Z.mpq',sha256=A,rows=2,fields=173,bytes=1500),
dict(table='Spell',archive='patch-A.mpq',sha256=B,rows=2,fields=173,bytes=1500),
dict(table='Talent',archive='patch-Z.mpq',sha256=C,rows=2,fields=21,bytes=200),
dict(table='Talent',archive='patch-A.mpq',sha256=C,rows=2,fields=21,bytes=200)])
def evidence(scan):
return dict(schema_version=1,verified=True,source_installation=scan['source_installation'],
scan_sha256=octo.canonical_sha256(scan),locale='enUS',
verification=dict(method='runtime-file-load-trace',artifact='runtime/source-load-trace.json',sha256=C),
selections={'Spell':'patch-Z.mpq'})
def table(report,name):return next(row for row in report['tables'] if row['table']==name)
def rejects(call):
try:call()
except (ValueError,TypeError):return
raise AssertionError('Unsafe or contradictory provenance accepted')
with patch.object(octo,'build_reader',side_effect=AssertionError('Resolver must not build reader')), \
patch.object(octo,'scan',side_effect=AssertionError('Resolver must not rescan archives')), \
patch.object(Path,'read_bytes',side_effect=AssertionError('Pure resolver must not read files')):
scan=manifest();report=octo.resolve_candidates(scan,expected_client='/local/Octowow')
assert table(report,'Spell')['status']=='blocked-ambiguous-candidates'
assert table(report,'Spell')['selected'] is None # Alphabetically later Z is not priority.
assert table(report,'Talent')['status']=='agreement-within-scanned-set'
assert table(report,'Item')['status']=='blocked-missing-candidate'
assert report['client_identity']==dict(client_sha256=None,client_build=None,locale=None)
assert report['validation_scope']=='manifest-only' and not report['files_rehashed']
assert report['staging_revalidation_required'] and not report['verification_artifact_rehashed']
assert all(not r['runtime_effective_independently_verified'] for r in report['tables'])
assert octo.resolve_candidates(deepcopy(scan))==report
reordered=json.loads(json.dumps(scan,sort_keys=True))
assert octo.resolve_candidates(reordered)==report # Input formatting/key order is irrelevant.
reversed_inventory=deepcopy(scan);reversed_inventory['candidates'].reverse()
assert octo.resolve_candidates(reversed_inventory)['tables']==report['tables']
pinned=evidence(scan);resolved=octo.resolve_candidates(scan,pinned)
assert table(resolved,'Spell')['status']=='selected-by-pinned-evidence'
assert table(resolved,'Spell')['selected']['archive']=='patch-Z.mpq'
assert resolved['client_identity']['locale'] is None
assert resolved['evidence_declared_identity']['locale']=='enUS'
assert scan==manifest() and pinned==evidence(scan) # No input mutation.
ordered=evidence(scan);del ordered['selections']
ordered.update(archive_load_order=['patch-Z.mpq','patch-A.mpq'],winner='last')
assert table(octo.resolve_candidates(scan,ordered),'Spell')['selected']['archive']=='patch-A.mpq'
ordered['winner']='first'
assert table(octo.resolve_candidates(scan,ordered),'Spell')['selected']['archive']=='patch-Z.mpq'
for field in ('archive','candidate','schema'):
drift=deepcopy(scan)
if field=='archive':drift['archives'][0]['sha256']='d'*64
elif field=='candidate':drift['candidates'][0]['sha256']='d'*64
else:drift['candidates'][0]['fields']=172
assert table(octo.resolve_candidates(drift,pinned),'Spell')['status']=='blocked-evidence-drift'
wrong=deepcopy(pinned);wrong['source_installation']='/other/Octowow'
assert table(octo.resolve_candidates(scan,wrong),'Spell')['status']=='blocked-evidence-drift'
wrong=deepcopy(pinned);wrong['selections']['Spell']='missing.mpq'
assert table(octo.resolve_candidates(scan,wrong),'Spell')['status']=='blocked-selected-candidate-missing'
missing=deepcopy(scan);missing['archives'].pop(0)
assert table(octo.resolve_candidates(missing),'Spell')['status']=='blocked-archive-provenance'
unhashed=deepcopy(scan);del unhashed['archives'][0]['sha256']
assert table(octo.resolve_candidates(unhashed),'Spell')['status']=='blocked-archive-provenance'
failed=deepcopy(scan);failed['archives'][0]['open_result']=-1
assert table(octo.resolve_candidates(failed),'Spell')['status']=='blocked-archive-provenance'
unique=deepcopy(scan);unique['candidates']=unique['candidates'][:1]
assert table(octo.resolve_candidates(unique),'Spell')['status']=='single-candidate-unverified'
locale=deepcopy(scan);locale['locale']='deDE';wrong=evidence(locale)
assert table(octo.resolve_candidates(locale,wrong),'Spell')['status']=='blocked-evidence-drift'
incomplete=deepcopy(ordered);incomplete['archive_load_order'].pop()
assert table(octo.resolve_candidates(scan,incomplete),'Spell')['status']=='blocked-evidence-drift'
for mutate in (
lambda x:x['archives'].append(deepcopy(x['archives'][0])),
lambda x:x['candidates'].append(deepcopy(x['candidates'][0])),
lambda x:x['candidates'][0].update(archive='../patch-Z.mpq'),
lambda x:x['candidates'][0].update(table='../Spell'),
lambda x:x['candidates'][0].update(path='../Spell.dbc'),
lambda x:x['candidates'][0].update(sha256='not-a-hash'),
lambda x:x['candidates'][0].update(rows=True),
lambda x:x['candidates'][0].update(fields=513),
lambda x:x['candidates'][0].update(bytes=20),
lambda x:x['candidates'][0].update(sha256=C),
lambda x:x.update(source_installation='/local/../Octowow'),
):
bad=deepcopy(scan);mutate(bad);rejects(lambda:octo.resolve_candidates(bad))
for mutate in (
lambda x:x.update(verified=False),
lambda x:x.update(schema_version=True),
lambda x:x['verification'].update(artifact='../outside.json'),
lambda x:x['verification'].update(sha256='not-a-hash'),
lambda x:x.update(locale='unknown'),
lambda x:x.update(archive_load_order=['patch-Z.mpq']),
):
bad=deepcopy(pinned);mutate(bad);rejects(lambda:octo.resolve_candidates(scan,bad))
rejects(lambda:octo.resolve_candidates(scan,expected_client='/other/Octowow'))
ambiguous=deepcopy(ordered);ambiguous['archive_load_order']=['patch-Z.mpq','patch-Z.mpq']
rejects(lambda:octo.resolve_candidates(scan,ambiguous))
print('PASS: deterministic manifest-only source resolution, no alphabetical precedence, '
'scanned-set agreement, pinned selections/order, drift/missing/schema/path/duplicate guards. '
'No archive extraction, file rehash, client or DB access.')