115 lines
3.7 KiB
JavaScript
115 lines
3.7 KiB
JavaScript
import test from 'node:test';
|
|
import assert from 'node:assert/strict';
|
|
import path from 'node:path';
|
|
import {
|
|
buildMindSpacePageDataPayload,
|
|
injectMindSpacePageDataContext,
|
|
injectPublishedPageDataContext,
|
|
parseMindSpacePublishFilePath,
|
|
resolveMindSpacePageDataContext,
|
|
} from './mindspace-public-page-context.mjs';
|
|
|
|
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('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.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('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"\}/);
|
|
});
|
|
|
|
test('resolveMindSpacePageDataContext prefers the Portal policy index in split-service mode', async () => {
|
|
const pool = {
|
|
async query() {
|
|
return [[{
|
|
page_id: 'local-page',
|
|
owner_user_id: 'user-1',
|
|
access_mode: 'public',
|
|
dataset_count: 1,
|
|
scope_hash: 'scope',
|
|
updated_at: 123,
|
|
current_publish_id: 'local-publish',
|
|
}]];
|
|
},
|
|
};
|
|
let remoteCalls = 0;
|
|
const result = await resolveMindSpacePageDataContext({
|
|
pool,
|
|
pageService: {
|
|
async findPageByRelativePath() {
|
|
remoteCalls += 1;
|
|
return { id: 'remote-page', publicationAccessMode: 'password' };
|
|
},
|
|
},
|
|
userId: 'USER-1',
|
|
relativePath: 'public/storefront.html',
|
|
});
|
|
assert.deepEqual(result, {
|
|
pageId: 'local-page',
|
|
accessMode: 'public',
|
|
publicationId: 'local-publish',
|
|
});
|
|
assert.equal(remoteCalls, 0);
|
|
});
|
|
|
|
test('resolveMindSpacePageDataContext falls back to the MindSpace page service', async () => {
|
|
const result = await resolveMindSpacePageDataContext({
|
|
pool: {
|
|
async query() {
|
|
return [[]];
|
|
},
|
|
},
|
|
pageService: {
|
|
async findPageByRelativePath(userId, relativePath) {
|
|
assert.equal(userId, 'user-1');
|
|
assert.equal(relativePath, 'public/plain.html');
|
|
return {
|
|
id: 'remote-page',
|
|
publicationAccessMode: 'internal',
|
|
currentPublishId: 'remote-publish',
|
|
createdAt: '2026-07-20T12:34:56.000Z',
|
|
};
|
|
},
|
|
},
|
|
userId: 'USER-1',
|
|
relativePath: 'public/plain.html',
|
|
});
|
|
assert.deepEqual(result, {
|
|
pageId: 'remote-page',
|
|
accessMode: 'internal',
|
|
publicationId: 'remote-publish',
|
|
generatedAt: '2026-07-20T12:34:56.000Z',
|
|
});
|
|
});
|