73 lines
3.7 KiB
JavaScript
73 lines
3.7 KiB
JavaScript
import assert from 'node:assert/strict';
|
|
import test from 'node:test';
|
|
import fs from 'node:fs/promises';
|
|
import os from 'node:os';
|
|
import path from 'node:path';
|
|
import {
|
|
ensureWorkspaceHtmlThumbnail,
|
|
workspaceThumbnailRelativePath,
|
|
} from './mindspace-workspace-thumbnails.mjs';
|
|
import { isModernFeedThumbnail } from './mindspace-thumbnails.mjs';
|
|
|
|
test('workspaceThumbnailRelativePath maps html to sidecar svg', () => {
|
|
assert.equal(workspaceThumbnailRelativePath('pilates.html'), 'pilates.thumbnail.svg');
|
|
assert.equal(workspaceThumbnailRelativePath('public/pilates.html'), 'public/pilates.thumbnail.svg');
|
|
});
|
|
|
|
test('ensureWorkspaceHtmlThumbnail writes sidecar on html save', async () => {
|
|
const root = await fs.mkdtemp(path.join(os.tmpdir(), 'workspace-thumb-'));
|
|
const html = `<!doctype html><html><head>
|
|
<title>普拉提618</title>
|
|
<meta name="mindspace-cover" content='{"tag":"运动","accent":"#eeb04e","accent2":"#1a0a2e","subtitle":"限时活动"}' />
|
|
<style>body { background:#0a0a0a; color:#eeb04e; }</style>
|
|
</head><body></body></html>`;
|
|
await fs.writeFile(path.join(root, 'pilates.html'), html, 'utf8');
|
|
const svg = await ensureWorkspaceHtmlThumbnail(root, 'pilates.html', html, { title: '普拉提618' });
|
|
assert.equal(isModernFeedThumbnail(svg), true);
|
|
assert.match(svg, /运动|普拉提618/i);
|
|
await fs.access(path.join(root, 'pilates.thumbnail.svg'));
|
|
});
|
|
|
|
test('ensureWorkspaceHtmlThumbnail schedules raster png sidecar', async () => {
|
|
const root = await fs.mkdtemp(path.join(os.tmpdir(), 'workspace-thumb-png-'));
|
|
const html = `<!doctype html><html><head>
|
|
<title>PNG sidecar</title>
|
|
<meta name="mindspace-cover" content='{"tag":"测试","accent":"#eeb04e","accent2":"#1a0a2e","subtitle":"副标题"}' />
|
|
</head><body></body></html>`;
|
|
await fs.writeFile(path.join(root, 'demo.html'), html, 'utf8');
|
|
await ensureWorkspaceHtmlThumbnail(root, 'demo.html', html, { title: 'PNG sidecar' });
|
|
await new Promise((resolve) => queueMicrotask(resolve));
|
|
await new Promise((resolve) => setImmediate(resolve));
|
|
await fs.access(path.join(root, 'demo.thumbnail.png'));
|
|
});
|
|
|
|
test('ensureWorkspaceHtmlThumbnail refreshes sidecars when the same html filename is overwritten', async () => {
|
|
const root = await fs.mkdtemp(path.join(os.tmpdir(), 'workspace-thumb-refresh-'));
|
|
const htmlPath = path.join(root, 'same-page.html');
|
|
const originalHtml = `<!doctype html><html><head>
|
|
<title>登鹳雀楼 · 王之涣</title>
|
|
<meta name="mindspace-cover" content='{"tag":"文学","subtitle":"白日依山尽"}' />
|
|
</head><body><h1>登鹳雀楼</h1></body></html>`;
|
|
await fs.writeFile(htmlPath, originalHtml, 'utf8');
|
|
const originalSvg = await ensureWorkspaceHtmlThumbnail(root, 'same-page.html', originalHtml);
|
|
assert.match(originalSvg, /登鹳雀楼/);
|
|
await new Promise((resolve) => queueMicrotask(resolve));
|
|
await new Promise((resolve) => setImmediate(resolve));
|
|
const originalPng = await fs.readFile(path.join(root, 'same-page.thumbnail.png'));
|
|
|
|
const updatedHtml = `<!doctype html><html><head>
|
|
<title>长安月 · 唐风诗韵</title>
|
|
<meta name="mindspace-cover" content='{"tag":"文化","subtitle":"原创唐诗 · 长安月色"}' />
|
|
</head><body><h1>长安月</h1></body></html>`;
|
|
await fs.writeFile(htmlPath, updatedHtml, 'utf8');
|
|
const updatedSvg = await ensureWorkspaceHtmlThumbnail(root, 'same-page.html', updatedHtml);
|
|
assert.match(updatedSvg, /长安月/);
|
|
assert.doesNotMatch(updatedSvg, /登鹳雀楼/);
|
|
assert.notEqual(updatedSvg, originalSvg);
|
|
|
|
await new Promise((resolve) => queueMicrotask(resolve));
|
|
await new Promise((resolve) => setImmediate(resolve));
|
|
const updatedPng = await fs.readFile(path.join(root, 'same-page.thumbnail.png'));
|
|
assert.notDeepEqual(updatedPng, originalPng);
|
|
});
|