38 lines
3.5 KiB
JavaScript
38 lines
3.5 KiB
JavaScript
'use strict';
|
|
const {vm,app,read,assert,functionSource}=require('./helpers/app-source');
|
|
|
|
const checkpointSource=functionSource('runMirrorCheckpoint'),benchmark=read('test/browser-performance-benchmark.js');
|
|
assert(checkpointSource.includes("build.stage==='serialize'")&&checkpointSource.includes('writeMirrorBuildChunk(build)'),'Idle mirror checkpoint does not serialize and write bounded chunks');
|
|
assert(!checkpointSource.includes('writeCompactMirror(')&&!checkpointSource.includes('JSON.stringify(snapshot)'),'Idle mirror checkpoint still performs a full synchronous mirror write');
|
|
assert(app.includes("MIRROR_CHUNK_BYTES=64*1024")&&app.includes("MIRROR_CHUNK_FORMAT='bend-field-chunked-v1'"),'Chunked mirror format or size bound is missing');
|
|
assert(!benchmark.includes('33*multiplier')&&!benchmark.includes('20*multiplier')&&!benchmark.includes('40*multiplier'),'CPU-throttled benchmark thresholds are still multiplied beyond the documented targets');
|
|
assert(benchmark.includes("name:'complex'")&&benchmark.includes("chunks:10")&&benchmark.includes("name:'long-line'"),'Browser benchmark does not cover a ten-chunk board and a long connected line');
|
|
|
|
const serializerContext={JSON,Number,Object,Array};
|
|
vm.createContext(serializerContext);
|
|
vm.runInContext(`${functionSource('jsonValueFragments')}\nthis.jsonValueFragments=jsonValueFragments;`,serializerContext);
|
|
const sample={schema:30,title:'曲線\\"test',enabled:true,missing:null,metas:{A:{id:'A',chunks:[[0,0],[1,0]],puzzle:{valid:[[0,0],[0,1]]}}},states:{A:{paths:[{cells:[[0,0],[0,1]]}]}}};
|
|
const serialized=[...serializerContext.jsonValueFragments(sample)].join('');
|
|
assert(serialized===JSON.stringify(sample)&&JSON.parse(serialized).metas.A.chunks.length===2,'Incremental JSON serializer changed the mirror payload');
|
|
|
|
const storage=new Map(),manifest={format:'bend-field-chunked-v1',generation:'g1',chunks:3,bytes:11,updatedAt:12};
|
|
storage.set('save',JSON.stringify(manifest));storage.set('chunk:g1:0','{"a":');storage.set('chunk:g1:1','[1,2]');storage.set('chunk:g1:2','}');
|
|
const readContext={
|
|
storageKey:'save',mirrorChunkPrefix:'chunk:',MIRROR_CHUNK_FORMAT:'bend-field-chunked-v1',LOCAL_MIRROR_MAX_BYTES:1024,MIRROR_CHUNK_BYTES:4,
|
|
safeLocalGet:key=>storage.has(key)?storage.get(key):null,JSON,Number,Math
|
|
};
|
|
vm.createContext(readContext);
|
|
vm.runInContext(`${functionSource('mirrorChunkKey')}\n${functionSource('parseMirrorManifest')}\n${functionSource('readCompactMirrorRaw')}\nthis.readCompactMirrorRaw=readCompactMirrorRaw;`,readContext);
|
|
assert(readContext.readCompactMirrorRaw()==='{"a":[1,2]}','Chunked mirror manifest was not reconstructed correctly');
|
|
storage.delete('chunk:g1:1');assert(readContext.readCompactMirrorRaw()===null,'Incomplete chunked mirror was accepted');
|
|
|
|
const writes=[],chunkContext={
|
|
mirrorChunkKey:(generation,index)=>`chunk:${generation}:${index}`,safeLocalSet:(key,value)=>{writes.push([key,value]);return true},
|
|
perfStart:()=>0,perfNow:()=>3,perfGauge:()=>{},perfEnd:()=>{},perfCount:()=>{}
|
|
};
|
|
vm.createContext(chunkContext);
|
|
vm.runInContext(`${functionSource('writeMirrorBuildChunk')}\nthis.writeMirrorBuildChunk=writeMirrorBuildChunk;`,chunkContext);
|
|
const build={generation:'g2',chunkIndex:0,pendingChunk:'x'.repeat(65536),bytes:0};
|
|
assert(chunkContext.writeMirrorBuildChunk(build)===true&&writes.length===1&&writes[0][1].length===65536&&build.pendingChunk===''&&build.bytes===65536,'Mirror chunk writer did not perform exactly one bounded write');
|
|
|
|
console.log('Chunked recovery mirror and strict performance acceptance regression passed');
|