32 lines
2 KiB
HTML
32 lines
2 KiB
HTML
<!doctype html>
|
|
<meta charset="utf-8">
|
|
<title>Field persistence browser test</title>
|
|
<body data-ready="running"></body>
|
|
<script src="../field-persistence.js"></script>
|
|
<script>
|
|
(async()=>{
|
|
const api=globalThis.BendFieldPersistence,assert=(condition,message)=>{if(!condition)throw new Error(message)};
|
|
const chunks=[];let closed=false;
|
|
const writable=new WritableStream({write(chunk){chunks.push(new Uint8Array(chunk).slice())},close(){closed=true}});
|
|
const boards=[
|
|
{id:'B0',meta:{id:'B0',seed:1},state:{solved:true}},
|
|
{id:'B2',meta:{id:'B2',seed:2},state:{solved:false}}
|
|
];
|
|
const written=await api.writeArchive({
|
|
writable,
|
|
manifest:{saveSchema:31,gameplayVersion:3,worldGeneration:'test-world',appVersion:'test',generatorVersion:1,exportedAt:'2026-01-01T00:00:00.000Z',boardCount:boards.length,estimatedRawBytes:512,encoding:'ndjson',compression:'identity'},
|
|
globalState:{bonusEvents:{}},
|
|
boards,
|
|
onProgress(){}
|
|
});
|
|
assert(closed,'archive output did not close');assert(written.boardCount===2,'archive board count is wrong');
|
|
const file=new File(chunks,'roundtrip.bfsave',{type:'application/x-bend-field-save'}),seen=[];
|
|
const summary=await api.inspectArchive(file);assert(summary.boardCount===2&&summary.compression==='identity','archive inspection failed');
|
|
const read=await api.readArchive(file,{onBoard(record){seen.push(record.id)}});
|
|
assert(read.boardCount===2&&seen.join(',')==='B0,B2','archive round trip failed');
|
|
const bytes=new Uint8Array(await file.arrayBuffer()),text=new TextDecoder().decode(bytes),tampered=text.replace('"crc32":"','"crc32":"f');
|
|
let corruptRejected=false;try{await api.readArchive(new File([tampered],'corrupt.bfsave'))}catch(error){corruptRejected=error.code==='CHECKSUM_MISMATCH'}
|
|
assert(corruptRejected,'corrupt archive was accepted');
|
|
document.body.dataset.ready='pass';document.body.textContent=JSON.stringify({written,seen});
|
|
})().catch(error=>{document.body.dataset.ready='fail';document.body.textContent=error.stack||String(error)});
|
|
</script>
|