"""Prove the original no-A/no-B component branch in private Unicorn memory."""
import importlib.util
import itertools
import json
import sys
from pathlib import Path

sys.path[:0] = ['build/world-context-python']
spec = importlib.util.spec_from_file_location(
    'tracking_oracle',
    'src/loader/runtime/profile-tools/devdata/creative-mode-146/verify-current-change-tracking.py')
t = importlib.util.module_from_spec(spec)
spec.loader.exec_module(t)

cases = []
for mode, active_a, direct_b in itertools.product((0, 1, 2), (0, 1), (False, True)):
    t.setup(a=False, b=False, capacity_a=0, capacity_b=0, metadata_direct=direct_b)
    t.write(t.manager+8, '<B', mode)
    t.write(t.manager+0xebc2d0, '<H', active_a)
    t.write(t.storage+0x8d, '<B', 0x10 if direct_b else 8)
    t.write(t.storage+0x90, '<I', 123)
    t.write(t.storage+0x94, '<I', 0x8000007b)
    t.write(t.context+0x284, '<I', 1)
    before_storage = bytes(t.u.mem_read(t.storage, 0x1000))
    before_context = bytes(t.u.mem_read(t.context, 0x300))
    r = t.execute()
    assert bytes(t.u.mem_read(t.storage, 0x1000)) == before_storage
    assert bytes(t.u.mem_read(t.context, 0x300)) == before_context
    assert r['countA'] == r['countB'] == 0
    cases.append(dict(name='untracked-component-preserves-original-state',
        managerMode=mode, activeAState=active_a, directB=direct_b,
        aliasFlag=not direct_b, currentAStamp=123, negativeBStamp=0x8000007b,
        queuesHaveNoCapacity=True, storageAndContextByteExact=True, result=r))

out = Path('src/loader/runtime/profile-tools/devdata/creative-mode-146/current-untracked-components-oracle-20261016.json')
out.write_text(json.dumps(dict(executable=t.pe.identity(), processOpened=False,
    processWrites=False, nativeGameCalls=False, cases=cases,
    contract=dict(entryRva=0x9c5960, registryAOffset=0x190, registryBOffset=0x230,
        aBitReadRva=0x9c59bc, aBitTestRva=0x9c59c7, bBitReadRva=0x9c5a41,
        bBitTestRva=0x9c5a4c, noSelectedCategoriesChangesNoTrackingState=True),
    limits=['The caller still requires original lifetime and Scene execution exclusion',
            'This validates tracking state, not a gameplay modification',
            'Categories must be read for the actual live registration index']), indent=2))
print(json.dumps(dict(cases=len(cases), passed=True, path=str(out))))
