Files
breeze/tests/practice_downloads_test.py
waozi 458f4fb5eb Run the test harnesses on the Ziran 0.6 toolchain
Point every harness's default toolchain at build/packages/ziran/build,
drop the removed `zi2zx bundle` form, link the generated C each harness
actually gets (whole generated folders where the test has no stubs, and
Kryon's package-prefixed frame pacing module), and stop listing the
header-only type module in the modal rules test. Twenty-two more test
targets pass from a clean export; plans/2026-11-17-test-harnesses.md
lists the 54 that still fail and why.
2026-11-17 03:12:21 -03:00

125 lines
6.6 KiB
Python

"""Exercise actual content mounting, offline cache and language downloads."""
import hashlib
import json
import os
import re
from pathlib import Path
import shutil
import subprocess
import sys
ROOT = Path(__file__).resolve().parents[1]
sys.path.insert(0, str(ROOT / 'scripts'))
from practice_content import NAMES
work = Path(os.environ.get('INBE_PRACTICE_TEST_OUTPUT', str(ROOT / 'build/practice-downloads-test')))
if work.exists():
shutil.rmtree(work)
work.mkdir(parents=True)
fixtures = work / 'fixtures'
(fixtures / 'cache/packages').mkdir(parents=True)
compiler = Path(os.environ.get('ZIRAN', str(ROOT / 'build/packages/ziran/build/bin/ziran')))
packs = []
for name in NAMES:
data = (ROOT / 'build/practice-content' / (name + '.zx')).read_bytes()
digest = hashlib.sha256(data).hexdigest()
(fixtures / (digest + '.zx')).write_bytes(data)
pack = dict(practice=name, language='', sha256=digest, size=len(data))
if name == 'taichi':
pack['product'] = 'inbe.practice.taichi'
packs.append(pack)
for code in ('en', 'es'):
assets = work / code / 'assets'
voice = assets / ('practices/taichi/voice/' + code)
voice.mkdir(parents=True)
(voice / 'test.ogg').write_bytes(b'fixture audio')
bundle = work / (code + '.zx')
subprocess.run([str(compiler), 'bundle', '--root', str(ROOT), '--entry', 'content:Main',
'--asset-dir', 'assets=' + str(assets), '-o', str(bundle),
str(ROOT / 'apps/practice/content.zi')], check=True)
data = bundle.read_bytes()
digest = hashlib.sha256(data).hexdigest()
(fixtures / (digest + '.zx')).write_bytes(data)
packs.append(dict(practice='taichi', language=code, sha256=digest, size=len(data),
product='inbe.practice.taichi'))
# The male recording is a separate English pack in its own folder.
assets = work / 'en-male' / 'assets'
voice = assets / 'practices/taichi/voice-male/en'
voice.mkdir(parents=True)
(voice / 'test.ogg').write_bytes(b'fixture male audio')
bundle = work / 'en-male.zx'
subprocess.run([str(compiler), 'bundle', '--project', '--offline', '--root', str(ROOT), '--entry', 'content:Main',
'--asset-dir', 'assets=' + str(assets), '-o', str(bundle),
str(ROOT / 'apps/practice/content.zi')], check=True)
data = bundle.read_bytes()
digest = hashlib.sha256(data).hexdigest()
(fixtures / (digest + '.zx')).write_bytes(data)
packs.append(dict(practice='taichi', language='en', voice='male', sha256=digest, size=len(data),
product='inbe.practice.taichi'))
# Lumi's guide lists its narration on its own, one free pack per language.
assets = work / 'guide-en' / 'assets'
voice = assets / 'practices/guide/voice/en'
voice.mkdir(parents=True)
(voice / 'test.ogg').write_bytes(b'fixture guide audio')
bundle = work / 'guide-en.zx'
subprocess.run([str(compiler), 'bundle', '--root', str(ROOT), '--entry', 'content:Main',
'--asset-dir', 'assets=' + str(assets), '-o', str(bundle),
str(ROOT / 'apps/practice/content.zi')], check=True)
data = bundle.read_bytes()
digest = hashlib.sha256(data).hexdigest()
(fixtures / (digest + '.zx')).write_bytes(data)
guide = [dict(language='en', sha256=digest, size=len(data))]
(fixtures / 'catalog.json').write_text(json.dumps(dict(schema=1, packs=packs, guide=guide)) + '\n')
(fixtures / 'default-whm.zx').write_bytes((ROOT / 'build/practice-content/whm.zx').read_bytes())
(fixtures / 'shipped-patterns.zx').write_bytes((ROOT / 'build/practice-content/patterns.zx').read_bytes())
# Stale content in a stable cache slot must be verified and replaced.
(fixtures / 'cache/packages/practice-whm.zx').write_bytes(b'old release')
(fixtures / 'cache/packages/practice-taichi.fr.zx').write_bytes(b'retired language')
preserved = {
'progress.json': b'preserve progress',
'practice-taichi.bad.zx': b'unrelated file',
'practice-taichix.zx': b'other namespace',
}
for name, data in preserved.items():
(fixtures / 'cache/packages' / name).write_bytes(data)
generated = work / 'generated'
arguments = [str(compiler), 'build', '--target=c', '--no-main', '--project', '--offline', '--root', str(ROOT / 'tests'),
'--define', 'PLATFORM_DESKTOP']
subprocess.run([*arguments, '-o', str(generated), str(ROOT / 'tests/practice_downloads_behavior.zi')], check=True)
monocypher = ROOT / 'build/packages/monocypher/src'
# The linker wraps these modules' procedures, so their headers declare them
# rather than giving other modules inline copies that would bypass the wraps.
download_header = next(generated.glob('*file_download.h'))
wrapped = ['-DZIRAN_' + header.stem.upper() + '_H_SOURCE'
for header in [download_header, *generated.glob('p*_sha256.h')]]
wrappers = []
for name in ('Start', 'Poll', 'Release'):
legacy = 'file_download_FileDownload' + name
symbol = re.search(r'([a-z0-9_]*file_download_FileDownload' + name + ')', download_header.read_text())[1]
wrappers.extend(('-D__wrap_' + legacy + '=__wrap_' + symbol, '-Wl,--wrap=' + symbol))
subprocess.run([os.environ.get('CC', 'cc'), '-std=c11', '-D_GNU_SOURCE', '-O1',
'-ffunction-sections', '-fdata-sections', *wrapped, *wrappers, '-I' + str(generated),
'-I' + str(ROOT / 'build/packages/ziran/include'), '-I' + str(monocypher),
'-I' + str(monocypher / 'optional'), str(ROOT / 'tests/practice_downloads_host.c'),
*map(str, generated.rglob('*.c')), str(monocypher / 'monocypher.c'),
str(monocypher / 'optional/monocypher-ed25519.c'),
os.environ.get('ZIRAN_RUNTIME', str(ROOT / 'build/packages/ziran/build/libziran.a')), '-Wl,--gc-sections',
'-Wl,--wrap=storage_db_path',
'-Wl,--wrap=Sha256Hex',
'-lm', '-pthread', '-o', str(work / 'test')], check=True)
subprocess.run([str(work / 'test'), str(fixtures)], check=True, timeout=120,
env={key: value for key, value in os.environ.items()
if key not in ('DISPLAY', 'WAYLAND_DISPLAY', 'XAUTHORITY', 'DBUS_SESSION_BUS_ADDRESS')})
cache = fixtures / 'cache/packages'
assert not list(cache.glob('practice-taichi.??.zx'))
# Removing Tai Chi forgets its removed languages; Lumi's guide keeps its own.
assert not list(cache.glob('practice-taichi.*.removed'))
assert (cache / 'practice-guide.en.removed').exists()
# Wim Hof ends installed from the app's own copy, with no removal marker.
assert not (cache / 'practice-whm.zx').exists() and not (cache / 'practice-whm.removed').exists()
for name, data in preserved.items():
assert (cache / name).read_bytes() == data
print('Practice downloads: verified content, corrupt-download retry, offline cache, narration in the app '
"language, chosen and removed languages, Lumi's guide narration, removal, shipped Wim Hof, granted "
'paid packs, and Sun Salutation downloading and updating an older copy passed')