Files
memind/server/portal-publication-shell.test.mjs
T

97 lines
2.1 KiB
JavaScript

import assert from 'node:assert/strict';
import test from 'node:test';
import {
appendQueryParam,
detectPublishedPageTitle,
extractOgImageUrl,
extractShareMetaFromPageHtml,
publishedPageShellHtml,
removeQueryParam,
resolveRequestOrigin,
} from './portal-publication-shell.mjs';
test('publication shell helpers preserve URL and metadata behavior', () => {
const html = `<!doctype html>
<html><head>
<title> Demo Page </title>
<meta property="og:image" content="https://cdn.example/cover.png">
<meta name="mindspace-cover" content='{"subtitle":"Demo subtitle"}'>
</head></html>`;
assert.equal(
detectPublishedPageTitle(html),
'Demo Page',
);
assert.equal(
extractOgImageUrl(html),
'https://cdn.example/cover.png',
);
assert.deepEqual(
extractShareMetaFromPageHtml(html),
{
title: 'Demo Page',
subtitle: 'Demo subtitle',
},
);
assert.equal(
appendQueryParam('/page?a=1', 'view', 'raw'),
'/page?a=1&view=raw',
);
assert.equal(
removeQueryParam(
'/page?a=1&view=raw#section',
'view',
),
'/page?a=1#section',
);
});
test('resolveRequestOrigin preserves forwarded host and protocol handling', () => {
assert.equal(
resolveRequestOrigin({
headers: {
'x-forwarded-host':
'public.example',
'x-forwarded-proto': 'https',
},
protocol: 'http',
}),
'https://public.example',
);
});
test('publishedPageShellHtml renders a functional escaped share shell', () => {
const html = publishedPageShellHtml({
iframeUrl:
'/u/alice/page?view=raw&x=<unsafe>',
shareUrl:
'https://example.test/u/alice/page?a=1&b=2',
title: '<Demo & Page>',
longImageUrl:
'/u/alice/page?download=long-image',
});
assert.match(
html,
/class="publication-frame"/,
);
assert.match(
html,
/class="publication-share-fab"/,
);
assert.match(
html,
/&lt;Demo &amp; Page&gt;/,
);
assert.match(
html,
/view=raw&amp;x=&lt;unsafe&gt;/,
);
assert.match(
html,
/download=long-image/,
);
assert.doesNotMatch(
html,
/<title><Demo/,
);
});