Files
memind/mindspace-public-delivery.test.mjs
T

124 lines
4.2 KiB
JavaScript

import test from 'node:test';
import assert from 'node:assert/strict';
import { decorateMindSpacePublishedHtml, handleMindSpaceLongImageDownload } from './mindspace-public-delivery.mjs';
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://mm.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://mm.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 result = decorateMindSpacePublishedHtml({
html: '<html><body>demo</body></html>',
embed: false,
context: {
origin: 'https://mm.tkmind.cn',
pageUrl: 'https://mm.tkmind.cn/MindSpace/u/public/report.html',
pageDirUrl: 'https://mm.tkmind.cn/MindSpace/u/public/',
fallbackImageUrl: 'https://mm.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}<!--share-->`, scriptHashes: ['abc'] }),
publishedPageCsp: (_value, options) => JSON.stringify(options),
isWechatUserAgent: () => true,
});
assert.match(result.html, /og:/);
assert.match(result.html, /wechat:/);
assert.match(result.html, /share/);
assert.equal(result.allowEmbedFrame, false);
assert.equal(result.csp, JSON.stringify({ embed: false, wechatShare: true, scriptHashes: ['abc'] }));
});
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: [] }));
});