Add page data delivery and publication guards

This commit is contained in:
john
2026-07-08 21:10:47 +08:00
parent 8d629e7a4e
commit eea14c1855
66 changed files with 3035 additions and 413 deletions
+39 -36
View File
@@ -1,46 +1,49 @@
import test from 'node:test';
import assert from 'node:assert/strict';
import { buildPublishedHtmlViewContext, resolvePublicRequestOrigin } from './mindspace-public-page-context.mjs';
import path from 'node:path';
import {
buildMindSpacePageDataPayload,
injectMindSpacePageDataContext,
injectPublishedPageDataContext,
parseMindSpacePublishFilePath,
} from './mindspace-public-page-context.mjs';
test('resolvePublicRequestOrigin prefers https for public hosts and forwarded proto for local hosts', () => {
assert.equal(
resolvePublicRequestOrigin({ hostHeader: 'm.tkmind.cn', forwardedProto: 'http', protocol: 'http' }),
'https://m.tkmind.cn',
);
assert.equal(
resolvePublicRequestOrigin({ hostHeader: '127.0.0.1:8081', forwardedProto: 'http', protocol: 'https' }),
'http://127.0.0.1:8081',
test('parseMindSpacePublishFilePath extracts userId and relative path', () => {
const h5Root = '/srv/memind';
const filePath = path.join(
h5Root,
'MindSpace',
'1c99b83b-0454-474f-a5d2-129d34506a32',
'public',
'survey.html',
);
const parsed = parseMindSpacePublishFilePath(filePath, h5Root);
assert.deepEqual(parsed, {
userId: '1c99b83b-0454-474f-a5d2-129d34506a32',
relativePath: 'public/survey.html',
});
});
test('buildPublishedHtmlViewContext builds file and directory page urls', () => {
const direct = buildPublishedHtmlViewContext({
origin: 'https://m.tkmind.cn',
requestPath: '/MindSpace/user-1/public/report.html?x=1',
filePath: '/tmp/MindSpace/user-1/public/report.html',
test('injectMindSpacePageDataContext adds meta and script before head close', () => {
const html = '<html><head><title>demo</title></head><body>ok</body></html>';
const next = injectMindSpacePageDataContext(html, {
pageId: 'page-123',
accessMode: 'password',
});
assert.equal(direct.pageUrl, 'https://m.tkmind.cn/MindSpace/user-1/public/report.html');
assert.equal(direct.pageDirUrl, 'https://m.tkmind.cn/MindSpace/user-1/public/');
const implicit = buildPublishedHtmlViewContext({
origin: 'https://m.tkmind.cn',
requestPath: '/MindSpace/user-1/public/report',
filePath: '/tmp/MindSpace/user-1/public/index.html',
});
assert.equal(implicit.pageUrl, 'https://m.tkmind.cn/MindSpace/user-1/public/report/');
assert.equal(implicit.pageDirUrl, 'https://m.tkmind.cn/MindSpace/user-1/public/report/');
assert.match(next, /meta name="mindspace-page-data-page-id" content="page-123"/);
assert.match(next, /window\.__MINDSPACE_PAGE_DATA__=\{"pageId":"page-123","accessMode":"password"\}/);
});
test('buildPublishedHtmlViewContext adds thumbnail png fallback when svg sibling exists', () => {
const context = buildPublishedHtmlViewContext({
origin: 'https://m.tkmind.cn',
requestPath: '/MindSpace/user-1/public/report.html',
filePath: '/tmp/MindSpace/user-1/public/report.html',
fileExists: (value) => value.endsWith('.thumbnail.svg'),
thumbnailPngPathForSvg: (value) => value.replace('.svg', '.png'),
});
assert.equal(
context.fallbackImageUrl,
'https://m.tkmind.cn/MindSpace/user-1/public/report.thumbnail.png',
);
test('buildMindSpacePageDataPayload returns null without pageId', () => {
assert.equal(buildMindSpacePageDataPayload({}), null);
});
test('injectPublishedPageDataContext injects pageId from publication resolve result', () => {
const html = '<html><head></head><body><script src="/assets/page-data-client.js"></script></body></html>';
const next = injectPublishedPageDataContext(html, {
pageSource: { pageId: '2b201736-03ca-4172-affd-00f2a0f700b5' },
publication: { accessMode: 'public' },
});
assert.match(next, /meta name="mindspace-page-data-page-id" content="2b201736-03ca-4172-affd-00f2a0f700b5"/);
assert.match(next, /window\.__MINDSPACE_PAGE_DATA__=\{"pageId":"2b201736-03ca-4172-affd-00f2a0f700b5","accessMode":"public"\}/);
});