import assert from 'node:assert/strict' import { readFileSync } from 'node:fs' import test from 'node:test' import vm from 'node:vm' import { buildHtmlPreview } from './htmlPreview.ts' // The preview injects two top-level script blocks: the storage shim and the // wkui bridge. Evaluate both joined, so each fake frame gets the full stack. const bootstrap = [...buildHtmlPreview('').matchAll(/'], ['\uFEFF\n\n\n\n
', ''], ['', ''], ['', ''], ] for (const [prefix, rest] of cases) { const source = prefix + rest const rendered = buildHtmlPreview(source) assert.equal(rendered, prefix + buildHtmlPreview('') + rest) assert.equal(rendered.replace(buildHtmlPreview(''), ''), source) } }) test('only executable artifact previews use the compatibility copy; source and sandbox stay intact', () => { const source = readFileSync(new URL('../components/document-preview.vue', import.meta.url), 'utf8') const htmlCase = source.slice(source.indexOf("case 'html':"), source.indexOf("case 'docx':")) assert.match(htmlCase, /if \(allowsHtmlScriptPreview\(\)\) \{\s*const previewHtml = buildHtmlPreview\(await blob\.text\(\)\)/) assert.match(htmlCase, /new Blob\(\[previewHtml\], \{ type: 'text\/html;charset=utf-8' \}\)/) assert.match(htmlCase, /await renderText\(blob, ft \|\| 'html'\)/) assert.match(source, /sandbox="allow-scripts"/) assert.doesNotMatch(source, /allow-same-origin/) })