Files
memind/mindspace-workspace-page-deliver-page-data.test.mjs

78 lines
2.6 KiB
JavaScript

import assert from 'node:assert/strict';
import fs from 'node:fs';
import os from 'node:os';
import path from 'node:path';
import test from 'node:test';
import { createWorkspacePageDeliverService } from './mindspace-workspace-page-deliver.mjs';
const PAGE_DATA_HTML = `<!doctype html><html><body>
<script src="/assets/page-data-client.js"></script>
<script>MindSpacePageData.createClient({ apiBase: '/api' }).insertRow('survey', { q1: 'a' });</script>
</body></html>`;
test('ensureWorkspaceHtmlPublications skips page-data html and delegates to pageDataEnsure', async () => {
const h5Root = fs.mkdtempSync(path.join(os.tmpdir(), 'deliver-h5-'));
const userId = 'user-1';
const publishDir = path.join(h5Root, 'MindSpace', userId);
fs.mkdirSync(path.join(publishDir, 'public'), { recursive: true });
fs.writeFileSync(path.join(publishDir, 'public', 'survey.html'), PAGE_DATA_HTML, 'utf8');
const published = [];
const bound = [];
const service = createWorkspacePageDeliverService({
pool: {
async query() {
return [[{
page_id: 'page-1',
title: '问卷',
current_version_id: 'ver-1',
workspace_relative_path: 'public/survey.html',
source_snapshot_json: JSON.stringify({
auto_synced: true,
relative_path: 'public/survey.html',
content_mode: 'static_html',
}),
}]];
},
},
pageService: {
async getPage() {
return { id: 'page-1', title: '问卷', currentVersionId: 'ver-1' };
},
findPageByRelativePath: async () => null,
},
publicationService: {
async getCurrent() {
return null;
},
async publish(userIdArg, pageId, input) {
published.push({ userId: userIdArg, pageId, input });
return { id: 'pub-1' };
},
},
pageSyncService: {
async syncUserGeneratedPages() {
return { created: 0, updated: 0, skipped: 0 };
},
},
pageDataEnsure: {
async ensurePageDataHtmlPagesBound(options) {
bound.push(options);
return { bound: [{ relativePath: 'public/survey.html' }], skipped: [], errors: [] };
},
},
h5Root,
storageRoot: null,
});
const result = await service.syncAndDeliver(userId, {
pageDataRelativePaths: ['public/survey.html'],
});
assert.equal(bound.length, 1);
assert.deepEqual(bound[0].onlyRelativePaths, ['public/survey.html']);
assert.equal(result.publish.published, 0);
assert.equal(result.publish.skipped, 1);
assert.equal(published.length, 0);
fs.rmSync(h5Root, { recursive: true, force: true });
});