// Offline execution of the shipped handlers with written fixtures. No browser or ASR claim. import fs from 'node:fs'; import path from 'node:path'; import vm from 'node:vm'; import crypto from 'node:crypto'; import assert from 'node:assert/strict'; import { transcript } from './formatter.mjs'; const here = path.dirname(new URL(import.meta.url).pathname.replace(/^\/([A-Za-z]:)/, '$1')); const root = here; const source = fs.readFileSync(path.join(root, 'app.js'), 'utf8'); const fixtures = [ { id:'punctuation', description:'Curly punctuation and an emoji inside one segment', segments:['It’s ready — copy “Reel one”. 🎬'], expected:'It’s ready — copy “Reel one”. 🎬' }, { id:'segments', description:'Separate response segments', segments:['First line.', 'Second line.', 'Third line.'], expected:'First line. Second line. Third line.' }, { id:'internal-breaks', description:'Line feeds inside one returned segment', segments:['First line.\nSecond line.\n\nThird line.'], expected:'First line.\nSecond line.\n\nThird line.' }, { id:'edge-spaces', description:'Whitespace at segment edges and inside a segment', segments:[' Keep two spaces. ', '\tNext word.\n'], expected:'Keep two spaces. Next word.' } ]; let actualNetworkCalls = 0; const nativeFetch = globalThis.fetch; globalThis.fetch = async () => { actualNetworkCalls++; throw Error('NO NETWORK ALLOWED IN THIS EXPERIMENT'); }; const rows = []; try { for (const fixture of fixtures) { const parsed = transcript({lang:'en', content:fixture.segments.map(text => ({text}))}); assert.equal(parsed.text, fixture.expected); const elements = new Map(); const get = selector => { if (!elements.has(selector)) elements.set(selector, { value:'', textContent:'', innerHTML:'', hidden:false, disabled:false, dataset:{}, handlers:{}, classList:{toggle(){}}, setAttribute(){}, focus(){}, select(){}, addEventListener(type,fn){this.handlers[type]=fn;} }); return elements.get(selector); }; let copied = null, blob = null, filename = null; class TestURL extends URL { static createObjectURL(value){blob=value;return 'blob:offline-written-fixture';} static revokeObjectURL(){} } const mockCalls = []; const context = vm.createContext({ document:{querySelector:get,createElement(){return {set href(value){},set download(value){filename=value;},click(){}};}}, navigator:{clipboard:{async writeText(text){copied=text;}}}, fetch:async (url,options) => {mockCalls.push({url,method:options?.method||'GET'});return {ok:true,async json(){return url==='/api/status'?{ready:true}:parsed;}};}, URL:TestURL, Blob, AbortController, Date, Intl, Number, JSON, String, Boolean, performance, setTimeout(){return 1;}, clearTimeout(){} }); vm.runInContext(source,context,{filename:'shipped-app.js'}); get('#reel-url').value='https://www.instagram.com/reel/OFFLINE_WRITTEN_FIXTURE/'; await get('#transcript-form').handlers.submit({preventDefault(){}}); await get('#copy').handlers.click(); get('#download').handlers.click(); const downloaded = await blob.text(); const display = get('#transcript').value; assert.equal(display, fixture.expected); assert.equal(copied, fixture.expected); assert.equal(downloaded, fixture.expected+'\n'); assert.equal(filename,'instagram-transcript.txt'); const dir=path.join(here,fixture.id);fs.mkdirSync(dir,{recursive:true}); fs.writeFileSync(path.join(dir,'input.json'),JSON.stringify({kind:'written synthetic text fixture, not video or ASR output',segments:fixture.segments},null,2)); for(const [name,text] of [['displayed.txt',display],['copied-handler.txt',copied],['download.txt',downloaded]])fs.writeFileSync(path.join(dir,name),text); rows.push({id:fixture.id,description:fixture.description,input_segments:fixture.segments,backend_text:parsed.text,displayed_text:display,copied_handler_text:copied,downloaded_text:downloaded,display_utf8_bytes:Buffer.byteLength(display),download_utf8_bytes:Buffer.byteLength(downloaded),internal_linefeeds_in_display:(display.match(/\n/g)||[]).length,word_count_label:get('#word-count').textContent,display_copy_equal:display===copied,download_equals_display_plus_linefeed:downloaded===display+'\n',mock_handler_calls:mockCalls,filename}); } } finally { globalThis.fetch=nativeFetch; } assert.equal(actualNetworkCalls,0); const result={captured_utc:new Date().toISOString(),method:'Execute the actual cloud-worker transcript() formatter and shipped app.js in a Node VM with in-memory DOM, fetch, clipboard and download sinks. Written fixtures, not Instagram submissions. No browser compatibility, ASR accuracy or audio completeness was tested.',app_sha256:crypto.createHash('sha256').update(source).digest('hex'),worker_sha256:crypto.createHash('sha256').update(fs.readFileSync(path.join(root,'formatter.mjs'))).digest('hex'),real_network_requests:actualNetworkCalls,paid_requests:0,rows}; fs.writeFileSync(path.join(here,'rerun-results.json'),JSON.stringify(result,null,2),{flag:'wx'}); console.log(JSON.stringify({method:result.method,network_requests:actualNetworkCalls,rows:rows.map(({id,display_utf8_bytes,download_utf8_bytes,internal_linefeeds_in_display,word_count_label,display_copy_equal,download_equals_display_plus_linefeed})=>({id,display_utf8_bytes,download_utf8_bytes,internal_linefeeds_in_display,word_count_label,display_copy_equal,download_equals_display_plus_linefeed}))}));