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 = `
Demo Page
`;
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=',
shareUrl:
'https://example.test/u/alice/page?a=1&b=2',
title: '',
longImageUrl:
'/u/alice/page?download=long-image',
});
assert.match(
html,
/class="publication-frame"/,
);
assert.match(
html,
/class="publication-share-fab"/,
);
assert.match(
html,
/<Demo & Page>/,
);
assert.match(
html,
/view=raw&x=<unsafe>/,
);
assert.match(
html,
/download=long-image/,
);
assert.doesNotMatch(
html,
/