149 lines
5.1 KiB
JavaScript
149 lines
5.1 KiB
JavaScript
import test from 'node:test';
|
|
import assert from 'node:assert/strict';
|
|
import {
|
|
collectInlineScriptHashes,
|
|
decorateMindSpacePublishedHtml,
|
|
handleMindSpaceLongImageDownload,
|
|
} from './mindspace-public-delivery.mjs';
|
|
|
|
test('collectInlineScriptHashes returns sha256 hashes for inline scripts only', () => {
|
|
const html = `<html><body>
|
|
<script>console.log('page')</script>
|
|
<script src="https://cdn.example/app.js"></script>
|
|
<script>console.log('share')</script>
|
|
</body></html>`;
|
|
const hashes = collectInlineScriptHashes(html);
|
|
assert.equal(hashes.length, 2);
|
|
assert.match(hashes[0], /^[A-Za-z0-9+/]+=*$/);
|
|
assert.notEqual(hashes[0], hashes[1]);
|
|
});
|
|
|
|
test('handleMindSpaceLongImageDownload renders and downloads when requested', async () => {
|
|
const calls = [];
|
|
const res = {
|
|
headersSent: false,
|
|
set(key, value) {
|
|
calls.push(['set', key, value]);
|
|
return this;
|
|
},
|
|
download(filePath, filename, cb) {
|
|
calls.push(['download', filePath, filename]);
|
|
cb?.();
|
|
},
|
|
status(code) {
|
|
calls.push(['status', code]);
|
|
return this;
|
|
},
|
|
type(value) {
|
|
calls.push(['type', value]);
|
|
return this;
|
|
},
|
|
send(value) {
|
|
calls.push(['send', value]);
|
|
return this;
|
|
},
|
|
json(value) {
|
|
calls.push(['json', value]);
|
|
return this;
|
|
},
|
|
};
|
|
|
|
const handled = await handleMindSpaceLongImageDownload({
|
|
query: { download: 'long-image' },
|
|
filePath: '/tmp/report.html',
|
|
pageUrl: 'https://m.tkmind.cn/MindSpace/u/public/report.html',
|
|
res,
|
|
isLongImageDownloadRequest: () => true,
|
|
longImagePathForHtml: () => '/tmp/report.long.png',
|
|
renderLongImage: async (...args) => {
|
|
calls.push(['render', ...args]);
|
|
},
|
|
});
|
|
|
|
assert.equal(handled, true);
|
|
assert.deepEqual(calls[0], ['render', {
|
|
url: 'https://m.tkmind.cn/MindSpace/u/public/report.html',
|
|
outputPath: '/tmp/report.long.png',
|
|
}]);
|
|
assert.deepEqual(calls[1], ['set', 'Cache-Control', 'no-store']);
|
|
assert.deepEqual(calls[2], ['download', '/tmp/report.long.png', 'report.long.png']);
|
|
});
|
|
|
|
test('handleMindSpaceLongImageDownload falls back to local html path without page url', async () => {
|
|
const calls = [];
|
|
const res = {
|
|
headersSent: false,
|
|
set() { return this; },
|
|
download() {},
|
|
status() { return this; },
|
|
type() { return this; },
|
|
send() { return this; },
|
|
};
|
|
|
|
await handleMindSpaceLongImageDownload({
|
|
query: { download: 'long-image' },
|
|
filePath: '/tmp/report.html',
|
|
res,
|
|
isLongImageDownloadRequest: () => true,
|
|
longImagePathForHtml: () => '/tmp/report.long.png',
|
|
renderLongImage: async (...args) => {
|
|
calls.push(['render', ...args]);
|
|
},
|
|
});
|
|
|
|
assert.deepEqual(calls[0], ['render', { htmlPath: '/tmp/report.html', outputPath: '/tmp/report.long.png' }]);
|
|
});
|
|
|
|
test('decorateMindSpacePublishedHtml returns decorated html and csp', () => {
|
|
const pageScript = "document.body.dataset.ready='1'";
|
|
const shareScript = "document.body.dataset.share='1'";
|
|
const result = decorateMindSpacePublishedHtml({
|
|
html: `<html><body><script>${pageScript}</script>demo</body></html>`,
|
|
embed: false,
|
|
context: {
|
|
origin: 'https://m.tkmind.cn',
|
|
pageUrl: 'https://m.tkmind.cn/MindSpace/u/public/report.html',
|
|
pageDirUrl: 'https://m.tkmind.cn/MindSpace/u/public/',
|
|
fallbackImageUrl: 'https://m.tkmind.cn/MindSpace/u/public/report.thumbnail.png',
|
|
},
|
|
userAgent: 'MicroMessenger',
|
|
preparePublicationHtmlForEmbed: (value) => value,
|
|
injectOgTags: (value, meta) => `${value}<!--og:${meta.pageUrl}-->`,
|
|
injectWechatShareBridge: (value, meta) => `${value}<!--wechat:${meta.pageUrl}-->`,
|
|
injectPublicFileShareButton: (value) => ({
|
|
html: value.replace('</body>', `<script>${shareScript}</script></body>`),
|
|
scriptHashes: ['ignored'],
|
|
}),
|
|
publishedPageCsp: (_value, options) => JSON.stringify(options),
|
|
isWechatUserAgent: () => true,
|
|
});
|
|
|
|
assert.match(result.html, /og:/);
|
|
assert.match(result.html, /wechat:/);
|
|
assert.match(result.html, /dataset\.share/);
|
|
assert.equal(result.allowEmbedFrame, false);
|
|
const options = JSON.parse(result.csp);
|
|
assert.equal(options.embed, false);
|
|
assert.equal(options.wechatShare, true);
|
|
assert.deepEqual(options.scriptHashes, collectInlineScriptHashes(result.html));
|
|
assert.equal(options.scriptHashes.length, 2);
|
|
});
|
|
|
|
test('decorateMindSpacePublishedHtml supports embed mode without share injection', () => {
|
|
const result = decorateMindSpacePublishedHtml({
|
|
html: '<html></html>',
|
|
embed: true,
|
|
context: { origin: '', pageUrl: '', pageDirUrl: '', fallbackImageUrl: '' },
|
|
preparePublicationHtmlForEmbed: (value) => `${value}<!--embed-->`,
|
|
injectOgTags: (value) => value,
|
|
injectWechatShareBridge: (value) => value,
|
|
injectPublicFileShareButton: (value) => ({ html: value, scriptHashes: [] }),
|
|
publishedPageCsp: (_value, options) => JSON.stringify(options),
|
|
isWechatUserAgent: () => false,
|
|
});
|
|
|
|
assert.match(result.html, /embed/);
|
|
assert.equal(result.allowEmbedFrame, true);
|
|
assert.equal(result.csp, JSON.stringify({ embed: true, wechatShare: false, scriptHashes: [] }));
|
|
});
|