Files
kryon/tests/document_storage_test.py
T
waozi 0678895719 Run ZX apps on Ziran's one executable format
Ziran dropped the semantic bundle VM: every bundle is revision-28 executable
ZX on the instruction engine (or its Wasm tier). Players and hosted apps
call InstantiateBundle on every target, including the browser; the --vm
option of the zx player goes, and its tests run without it. The web,
Raylib and Android runtimes build libziran in normal mode so they carry the
engine (BOOTSTRAP=1 builds have none), and their rebuild rules watch the
engine's Ziran sources.
2026-11-14 16:27:22 -03:00

73 lines
4.8 KiB
Python

#!/usr/bin/env python3
"""Real native and IDBFS barriers, including portable AppLocal guests."""
from toolchain import JS_RUNTIME
import argparse
import os
from pathlib import Path
import subprocess
import shutil
from toolchain import ZIRAN, ZIRAN_ROOT
ROOT = Path(__file__).resolve().parents[1]
WORK = ROOT / 'work/document-storage'
ENV = dict(os.environ, EMCC_CORES='1', CMAKE_BUILD_PARALLEL_LEVEL='1', ZIRAN_PAR_THREADS='1', MAKEFLAGS='-j1', YUE_DESKTOP_RECOVERY='0')
for key in ('DISPLAY','WAYLAND_DISPLAY','XAUTHORITY','DBUS_SESSION_BUS_ADDRESS','SESSION_MANAGER','DESKTOP_SESSION','XDG_SESSION_ID'):
ENV.pop(key, None)
def run(args):
subprocess.run(list(map(str,args)),cwd=ROOT,env=ENV,check=True)
def emscripten_compiler():
selected = os.environ.get('EMCC') or shutil.which('emcc')
if not selected:
selected = str(Path.home()/'emsdk/upstream/emscripten/emcc')
found = shutil.which(selected)
if not found:
raise SystemExit('Browser document storage tests require Emscripten; set EMCC or install emcc on PATH')
return str(Path(found).absolute())
def main():
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument('--browser', action='store_true', help='also run real browser IDBFS barriers')
args = parser.parse_args()
run(['git','check-ignore','work/document-storage/root.txt'])
WORK.mkdir(parents=True, exist_ok=True)
(WORK/'data').mkdir(exist_ok=True)
(WORK/'root.txt').write_text(str(WORK/'data'))
emcc = emscripten_compiler() if args.browser else None
if args.browser:
ENV['EM_CACHE']=str(ROOT/'build/emscripten-cache')
run(['make','-j1','-C',ZIRAN_ROOT,'BUILD_DIR='+str(WORK/'runtime'),'CC='+emcc,'AR='+str(Path(emcc).with_name('emar')),'OBJCOPY=true','CFLAGS=-O2',WORK/'runtime/libziran.a'])
common=['--project','--root',ROOT/'tests','--module-path',ROOT/'tests']
guest=ROOT/'tests/document_storage_guest.zi'
source=ROOT/'tests/document_storage_test.zi'
run([ZIRAN,'ir',*common,'-o',WORK/'guest-ir',guest])
for form in ('source','saved'):
paths=common if form=='source' else ['--root',WORK/'guest-ir']
file=guest if form=='source' else WORK/'guest-ir/document_storage_guest.zir'
run([ZIRAN,'bundle',*paths,'--entry','document_storage_guest:main','-o',WORK/'guest.zx',file])
for web in ((False,True) if args.browser else (False,)):
gen=WORK/'c'
shutil.rmtree(gen,ignore_errors=True)
define=['--define','PLATFORM_WEB'] if web else []
target=['--target-os','web','--target-cpu','wasm32'] if web else []
run([ZIRAN,'ir',*common,*(['--target=c',*target] if web else []),*define,'-o',WORK/'ir',source])
paths=common if form=='source' else ['--root',WORK/'ir']
file=source if form=='source' else WORK/'ir/document_storage_test.zir'
run([ZIRAN,'build','--target=c',*target,*paths,*define,'--entry','document_storage_test:main','-o',gen,file])
flags=['-O1','-ffunction-sections','-fdata-sections','-I'+str(gen),'-I'+str(ZIRAN_ROOT/'include'),*sorted(gen.glob('*.c'))]
if not web:
run([os.environ.get('CC','cc'),*flags,ZIRAN_ROOT/'build/libziran.a','-Wl,--gc-sections','-lm','-lpthread','-o',WORK/'test'])
run([WORK/'test'])
else:
ENV['EM_CACHE']=str(ROOT/'build/emscripten-cache')
shell=WORK/'shell.html'
shell.write_text('''<!doctype html><meta charset="utf-8"><pre id="result">pending</pre><script>
var storageTestMode=Number(new URLSearchParams(location.search).get('reload')||0);
var Module={preRun:[()=>{Date.now=()=>1700000000000; if(storageTestMode===2) indexedDB.open=()=>{throw new Error('initial IDB unavailable')};const sync=FS.syncfs; FS.syncfs=(p,cb)=>sync(p,e=>setTimeout(()=>cb(e),40));}],onExit:c=>document.getElementById('result').textContent=c?'FAIL '+c:'PASS',onAbort:e=>document.getElementById('result').textContent='ABORT '+e};
function storageTestFail(){FS.filesystems.IDBFS.dbs['/kryon-apps'].close()}
</script>{{{ SCRIPT }}}''')
run([emcc,*flags,WORK/'runtime/libziran.a','--js-library',ZIRAN_ROOT/'web/ziran_web.js','-lidbfs.js','-sASYNCIFY','-sSTACK_SIZE=16777216','-sASYNCIFY_STACK_SIZE=1048576','-g2','-sASSERTIONS=1','-sALLOW_MEMORY_GROWTH=1','-sINITIAL_MEMORY=134217728','-sSINGLE_FILE=1','-sEXIT_RUNTIME=1','-sEXPORTED_RUNTIME_METHODS=FS','-sENVIRONMENT=web','--embed-file',str(WORK/'guest.zx')+'@work/document-storage/guest.zx','--shell-file',shell,'-o',WORK/'test.html'])
shutil.rmtree(WORK/'browser-profile',ignore_errors=True)
run([JS_RUNTIME,ROOT/'tests/document_storage_test.mjs',(WORK/'test.html').as_uri(),WORK/'browser-profile'])
print(f'document storage {form} {"browser IDBFS + hosted VM" if web else "native + hosted VM"}: passed',flush=True)
if __name__=='__main__': main()