Files
memind/mindspace-visual-editor.test.mjs
2026-07-02 21:03:14 +08:00

53 lines
2.1 KiB
JavaScript

import assert from 'node:assert/strict';
import test from 'node:test';
import {
MINDSPACE_PAGE_CONTENT_MESSAGE,
buildEditablePreviewDocument,
} from './src/utils/mindspaceVisualEditor.ts';
test('buildEditablePreviewDocument injects visual editor into body', () => {
const html = '<!doctype html><html><head></head><body><p>Hi</p></body></html>';
const result = buildEditablePreviewDocument(html);
assert.match(result, /mindspace-visual-editor-style/);
assert.match(result, /mindspace-visual-editor-script/);
assert.match(result, /data-mindspace-editing/);
assert.match(result, /<p>Hi<\/p>/);
assert.match(result, new RegExp(MINDSPACE_PAGE_CONTENT_MESSAGE.replace(':', '\\:')));
assert.match(result, /data-collapsed/);
assert.match(result, /5000/);
assert.match(result, /点击展开/);
assert.match(result, /isProtectedNode/);
});
test('buildEditablePreviewDocument normalizes platform brand line', () => {
const html = '<!doctype html><html><body><p>📧 contact@tkmind.ai</p></body></html>';
const result = buildEditablePreviewDocument(html);
assert.match(result, /TKMind · 智趣/);
assert.match(result, /data-mindspace-page-tag="platform-brand"/);
assert.doesNotMatch(result, /contact@tkmind\.cn/i);
assert.doesNotMatch(result, /tkmind\.ai/i);
});
test('buildEditablePreviewDocument wraps fragment html', () => {
const result = buildEditablePreviewDocument('<section>块</section>');
assert.match(result, /<section>块<\/section>/);
assert.match(result, /<\/body>/);
});
test('buildEditablePreviewDocument provides blank fallback', () => {
const result = buildEditablePreviewDocument('');
assert.match(result, /空白页面/);
});
test('buildEditablePreviewDocument strips CSP meta so editor script can run', () => {
const html = `<!DOCTYPE html><html><head>
<meta http-equiv="Content-Security-Policy" content="default-src 'none'; script-src 'none'">
</head><body><h1>探索世界</h1></body></html>`;
const result = buildEditablePreviewDocument(html);
assert.doesNotMatch(result, /Content-Security-Policy/);
assert.match(result, /mindspace-visual-editor-script/);
assert.match(result, /<h1>探索世界<\/h1>/);
});