import test from 'node:test'; import assert from 'node:assert/strict'; import { allowPlazaEmbedFrame, appendPlazaEmbedQuery, injectPlazaEmbedBootstrap, isPlazaEmbedRequest, preparePublicationHtmlForEmbed, publishedPageCspForEmbed, stripPublicationHtmlCspMeta, } from './plaza-embed.mjs'; test('isPlazaEmbedRequest detects embed=plaza', () => { assert.equal(isPlazaEmbedRequest({ embed: 'plaza' }), true); assert.equal(isPlazaEmbedRequest({ embed: 'other' }), false); assert.equal(isPlazaEmbedRequest({}), false); }); test('appendPlazaEmbedQuery appends query once', () => { assert.equal( appendPlazaEmbedQuery('/u/john/pages/demo'), '/u/john/pages/demo?embed=plaza', ); assert.equal( appendPlazaEmbedQuery('/u/john/pages/demo?embed=plaza'), '/u/john/pages/demo?embed=plaza', ); }); test('injectPlazaEmbedBootstrap adds script before closing body', () => { const html = '

Hi

'; const out = injectPlazaEmbedBootstrap(html); assert.match(out, /plaza-embed-bootstrap/); assert.match(out, /<\/body>/); }); test('publishedPageCspForEmbed allows inline script', () => { assert.match(publishedPageCspForEmbed(true), /script-src 'unsafe-inline'/); assert.match(publishedPageCspForEmbed(true), /img-src 'self' data: https: http:/); }); test('allowPlazaEmbedFrame removes legacy X-Frame-Options header', () => { let removed = null; allowPlazaEmbedFrame({ removeHeader(name) { removed = name; }, }); assert.equal(removed, 'X-Frame-Options'); }); test('stripPublicationHtmlCspMeta removes inline CSP meta tags', () => { const html = ''; const out = stripPublicationHtmlCspMeta(html); assert.doesNotMatch(out, /Content-Security-Policy/); }); test('preparePublicationHtmlForEmbed strips CSP and injects bootstrap', () => { const html = `
Hi
`; const out = preparePublicationHtmlForEmbed(html); assert.doesNotMatch(out, /http-equiv="Content-Security-Policy"/); assert.match(out, /plaza-embed-bootstrap/); assert.match(out, /plaza-embed-overrides/); assert.match(out, /\.hero\{height:auto!important;min-height:0!important/); }); test('embed bootstrap measures content blocks without document scrollHeight feedback', () => { const html = '
Tall
'; const out = preparePublicationHtmlForEmbed(html); assert.doesNotMatch(out, /root\?root\.scrollHeight/); assert.doesNotMatch(out, /body\?body\.scrollHeight/); assert.match(out, /isDecorative/); assert.match(out, /bg-particles/); assert.match(out, /blocks\(\)\.forEach\(function\(el\)\{if\(el\)ro\.observe\(el\)/); });