import { test } from 'node:test'; import assert from 'node:assert/strict'; import { injectOgTags } from './mindspace-og-tags.mjs'; const ctx = { origin: 'https://g2.tkmind.cn', pageUrl: 'https://g2.tkmind.cn/MindSpace/john/public/space.html', pageDirUrl: 'https://g2.tkmind.cn/MindSpace/john/public/', }; test('injects og/twitter tags with absolute image from a relative cover', () => { const html = `🚀 太空小勇士 - 闯关大冒险` + `` + ``; const out = injectOgTags(html, ctx); assert.match(out, //); assert.match(out, //); assert.match(out, //); assert.match(out, //); // injected before assert.ok(out.indexOf('og:image') < out.indexOf('')); }); test('falls back to the thumbnail png when the page has no cover of its own', () => { const html = `纯文字页面`; const out = injectOgTags(html, { ...ctx, fallbackImageUrl: 'https://g2.tkmind.cn/MindSpace/john/public/space.thumbnail.png', }); assert.match(out, //); assert.match(out, //); }); test('prefers the page cover over the thumbnail fallback', () => { const html = `X`; const out = injectOgTags(html, { ...ctx, fallbackImageUrl: 'https://g2.tkmind.cn/x.thumbnail.png' }); assert.match(out, /og:image" content="https:\/\/g2\.tkmind\.cn\/MindSpace\/john\/public\/assets\/hero\.jpg"/); assert.doesNotMatch(out, /thumbnail\.png/); }); test('keeps an author-provided og:image and does not duplicate', () => { const html = `T`; const out = injectOgTags(html, ctx); assert.equal((out.match(/og:image/g) ?? []).length, 1); }); test('skips og:image for svg-only cover, falls back to summary card', () => { const html = `仅SVG`; const out = injectOgTags(html, ctx); assert.doesNotMatch(out, /og:image/); assert.match(out, //); }); test('passes through a full https cover url unchanged', () => { const html = `X`; const out = injectOgTags(html, ctx); assert.match(out, //); }); test('resolves a root-absolute cover against the origin', () => { const html = `X`; const out = injectOgTags(html, ctx); assert.match(out, //); }); test('escapes quotes and ampersands in title to keep the meta tag well-formed', () => { const html = `say "hi" & bye`; const out = injectOgTags(html, ctx); assert.match(out, //); });