vomebook commited on
Commit ·
a2b3337
1
Parent(s): 9fa3d76
Expand frontend reader and download regression coverage
Browse filesFiles changed (5)
77 unmodified lines78798081828384858687888990918 unmodified lines10010110210310410577 unmodified linesconst before = messages.length;await send(null); await send({type:"cancel"});assert.equal(messages.length, before);await send({type:"page", id:++id, session:null, offset:"bad"});assert.match(latest().error, /索引无效/);await send({type:"unknown", id:++id});assert.match(latest().error, /索引无效/);for (const index of [{...config.index, url:"https://evil.example/api/reader-content"},{...config.index, bytes:0}, {...config.index, bytes:64*1024*1024+1},{...config.index, sha256:"bad"},{...config.index, url:proxyOrigin+"/api/reader-bucket-resource?path=../bad"}]) {await send({type:"init", configuration:{...config,index}});8 unmodified linesassert.match((await search()).error, expected, mode);}assert.equal(streamCancelled, true);// Abort while the response stream is blocked in read(): the Worker must cancel// the underlying stream and a subsequent fetch must still be possible.responseMode = "held-stream"; streamCancelled = false;77 unmodified lines78798081828384858687888990919293949596978 unmodified lines10610710810911011111211311411511611777 unmodified linesconst before = messages.length;await send(null); await send({type:"cancel"});assert.equal(messages.length, before);const invalidPagingRequests = requests.length;for (const page of [{session:null, offset:"bad"}, {session:1, offset:"0"}]) {const invalidId = ++id;await send({type:"page", id:invalidId, ...page});assert.equal(latest().id, invalidId);assert.match(latest().error, /索引无效/);}assert.equal(requests.length, invalidPagingRequests);await send({type:"unknown", id:++id});assert.match(latest().error, /索引无效/);for (const index of [{...config.index, url:"https://evil.example/api/reader-content"},{...config.index, bytes:0}, {...config.index, bytes:64*1024*1024+1},{...config.index, sha256:"bad"}, {...config.index, text_profile:42},{...config.index, url:proxyOrigin+"/api/reader-bucket-resource?path=../bad"}]) {await send({type:"init", configuration:{...config,index}});8 unmodified linesassert.match((await search()).error, expected, mode);}assert.equal(streamCancelled, true);responseMode = "valid";// A response of exactly the expected length must still match the SHA-256.const actualHash = config.index.sha256;const otherHash = (actualHash[0] === "0" ? "1" : "0") + actualHash.slice(1);await send({type:"init", configuration:{...config,index:{...config.index,sha256:otherHash}}});assert.match((await search()).error, /校验失败/);// Abort while the response stream is blocked in read(): the Worker must cancel// the underlying stream and a subsequent fetch must still be possible.responseMode = "held-stream"; streamCancelled = false;
36 unmodified lines3738394036 unmodified linesconst body=new ReadableStream({pull(){ready();return new Promise(()=>{});},cancel(){throw new Error('cancel failed');}});const owner=new AbortController();const pending=readPdfSearchText(body,owner.signal,10);await waiting;owner.abort();await assert.rejects(pending,{name:'AbortError'});assert.equal(body.locked,false);});36 unmodified lines37383940414243444546474836 unmodified linesconst body=new ReadableStream({pull(){ready();return new Promise(()=>{});},cancel(){throw new Error('cancel failed');}});const owner=new AbortController();const pending=readPdfSearchText(body,owner.signal,10);await waiting;owner.abort();await assert.rejects(pending,{name:'AbortError'});assert.equal(body.locked,false);// A size-limit failure also owns stream cleanup; a failed cancellation must// not mask the size error or leave the body locked.let cancellations=0;const oversized=new ReadableStream({start(controller){controller.enqueue(Uint8Array.of(1,2));},cancel(){cancellations++;throw new Error('cancel failed after limit');}});await assert.rejects(readPdfSearchBytes(oversized,signal(),1,1),/READER_RESOURCE_LIMIT/);assert.equal(cancellations,1);assert.equal(oversized.locked,false);});
50 unmodified lines51525354555651 unmodified lines10810911011111211346 unmodified lines16016116216316416550 unmodified linesassert.equal(detect(Uint8Array.of(0xff),'Русский'),'windows-1251');assert.equal(detect(Uint8Array.of(0xc4,0xe3,0xba,0xc3),'中文'),'gb18030');assert.equal(detect(Uint8Array.of(0xa4,0xa4,0xa4,0xe5),'中文'),'big5');// Compare the original two-filter heuristic on random bytes, BOMs and// threshold edges. Only the zero scan is replaced; decoder scoring stays.const oldHeuristic=bytes=>{51 unmodified linesawait f.context.renderPlainText(responseChunks([]));assert.deepEqual(samples,[[]]);assert.equal(f.context.content.textContent,'');assert.deepEqual(f.events.filter(event=>event==='track'||event==='untrack'),['track','untrack']);} finally {f.restore();}});46 unmodified linesconst last=block.textContent.charCodeAt(block.textContent.length-1);assert.ok(!(last>=0xd800&&last<=0xdbff));assert.notEqual(last,13);}} finally {f.restore();}});50 unmodified lines515253545556575851 unmodified lines11011111211311411511611711811912012112212312412512612712812946 unmodified lines17617717817918018118218318418518618718818950 unmodified linesassert.equal(detect(Uint8Array.of(0xff),'Русский'),'windows-1251');assert.equal(detect(Uint8Array.of(0xc4,0xe3,0xba,0xc3),'中文'),'gb18030');assert.equal(detect(Uint8Array.of(0xa4,0xa4,0xa4,0xe5),'中文'),'big5');assert.equal(detect(Uint8Array.of(0xcf,0xf0,0xe8,0xe2,0xe5,0xf2,0x21)),'windows-1251','legacy Cyrillic text can be detected without a language hint in the title');// Compare the original two-filter heuristic on random bytes, BOMs and// threshold edges. Only the zero scan is replaced; decoder scoring stays.const oldHeuristic=bytes=>{51 unmodified linesawait f.context.renderPlainText(responseChunks([]));assert.deepEqual(samples,[[]]);assert.equal(f.context.content.textContent,'');assert.deepEqual(f.events.filter(event=>event==='track'||event==='untrack'),['track','untrack']);for (const [encoding, bom, a, b] of [['utf-16le', [0xff,0xfe], [0x41,0], [0x42,0]],['utf-16be', [0xfe,0xff], [0,0x41], [0,0x42]]]) {f.context.content.children.length=0;f.events.length=0;samples.length=0;const response=responseChunks([Uint8Array.of(bom[0]),Uint8Array.of(),Uint8Array.of(bom[1]),Uint8Array.of(a[0]),Uint8Array.of(a[1],b[0]),Uint8Array.of(b[1])]);await f.context.renderPlainText(response);assert.equal(f.context.content.textContent,'AB',encoding);assert.deepEqual(samples,[bom],`${encoding} decoder selected at the split BOM`);assert.equal(response.body.locked,false);assert.deepEqual(f.events.filter(event=>event==='track'||event==='untrack'),['track','untrack']);}} finally {f.restore();}});46 unmodified linesconst last=block.textContent.charCodeAt(block.textContent.length-1);assert.ok(!(last>=0xd800&&last<=0xdbff));assert.notEqual(last,13);}f.context.content.children.length=0;const before='x'.repeat(32767), after='\r\n'+'y'.repeat(32763)+'\nend';const encode=text=>new TextEncoder().encode(text);await f.context.renderPlainText(responseChunks([encode('\ufeff'+before),encode(after)]));assert.equal(f.context.content.textContent,before+after);const streamed=f.context.content.children[0].children.map(block=>block.textContent);assert.deepEqual(streamed,[before,after.slice(0,-3),'end']);} finally {f.restore();}});
5 unmodified lines67891011125 unmodified linestest('reader errors preserve the original reason and release the reader lock', async () => {const old={document:globalThis.document,security:globalThis.VoiceOfMLReaderSecurity};const failure=new Error('source failed'), events=[], reader={read:async()=>{throw failure;},cancel:async reason=>events.push(['cancel',reason]),releaseLock:()=>events.push('release')};globalThis.document={createElement:()=>({className:'',appendChild(){}})};globalThis.VoiceOfMLReaderSecurity={LIMITS:{documentBytes:32},assertResponseSize(){}};const context={content:{appendChild(){}},documentState:{title:''},assertReaderActive(){},5 unmodified lines6789101112135 unmodified linestest('reader errors preserve the original reason and release the reader lock', async () => {const old={document:globalThis.document,security:globalThis.VoiceOfMLReaderSecurity};const failure=new Error('source failed'), events=[], reader={read:async()=>{throw failure;},cancel:async reason=>{events.push(['cancel',reason]);throw new Error('cancel also failed');},releaseLock:()=>events.push('release')};globalThis.document={createElement:()=>({className:'',appendChild(){}})};globalThis.VoiceOfMLReaderSecurity={LIMITS:{documentBytes:32},assertResponseSize(){}};const context={content:{appendChild(){}},documentState:{title:''},assertReaderActive(){},
84 unmodified lines8586878889909192934 unmodified lines989910010110210384 unmodified lines});test("two active checks bound the queue and abandoned work never starts", async () => {const {controller: c, requests} = fixture();const thirdOwner = new AbortController();const first = c.checkDownload("a"), second = c.checkDownload("b");const third = c.checkDownload("c", false, thirdOwner.signal);assert.equal(c.activeDownloadChecks, 2);assert.equal(c.downloadCheckQueue.length, 1);4 unmodified linesassert.equal(c.downloadChecks.has("c"), false);requests[0].resolve(response({ok: true}));assert.equal(await first, true);assert.equal(requests.length, 2);assert.equal(c.downloadCheckQueue.length, 0);requests[1].resolve(response({ok: true}));84 unmodified lines8586878889909192939495969798991001011021031041051061071081091104 unmodified lines11511611711811912012184 unmodified lines});test("two active checks bound the queue and abandoned work never starts", async () => {let allowSpeculation = false, batchActive = false;const guarded = fixture({isSpeculativeAllowed: () => allowSpeculation, isBatchActive: () => batchActive,});assert.equal(await guarded.controller.checkDownload("opted-out", true), false);allowSpeculation = true;batchActive = true;assert.equal(await guarded.controller.checkDownload("batch-active", true), false);const aborted = new AbortController();aborted.abort();assert.equal(await guarded.controller.checkDownload("already-aborted", false, aborted.signal), false);assert.equal(guarded.requests.length, 0);assert.equal(guarded.timers.size, 0);assert.equal(guarded.controller.downloadChecks.size, 0);assert.equal(guarded.controller.downloadCheckQueue.length, 0);const {controller: c, requests} = fixture();const thirdOwner = new AbortController();const first = c.checkDownload("a"), second = c.checkDownload("b");const shared = c.checkDownload("a", true); // Join an existing check despite busy slots.const third = c.checkDownload("c", false, thirdOwner.signal);assert.equal(c.activeDownloadChecks, 2);assert.equal(c.downloadCheckQueue.length, 1);4 unmodified linesassert.equal(c.downloadChecks.has("c"), false);requests[0].resolve(response({ok: true}));assert.equal(await first, true);assert.equal(await shared, true);assert.equal(requests.length, 2);assert.equal(c.downloadCheckQueue.length, 0);requests[1].resolve(response({ok: true}));
tests/reader/chapter-search/test_reader_chapter_worker.mjs+15-3
tests/reader/pdf-search/test_pdf_search_stream.mjs+8
tests/reader/text/test_reader_text_baseline.mjs+24
tests/reader/text/test_reader_text_faults.mjs+2-1
tests/search/download/test_download_controller_contract.js+18