import { test } from 'node:test'; import assert from 'node:assert/strict'; import { extractSharePreviewMeta, injectOgTags, injectWechatShareBridge, PLATFORM_SITE_NAME, renderWechatSharePreviewHtml, } from './mindspace-og-tags.mjs'; const ctx = { origin: 'https://m.tkmind.cn', pageUrl: 'https://m.tkmind.cn/MindSpace/john/public/space.html', pageDirUrl: 'https://m.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, //); assert.match(out, //); assert.match(out, //); 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://m.tkmind.cn/MindSpace/john/public/space.thumbnail.png', }); assert.match(out, //); assert.match(out, //); assert.match(out, //); }); test('prefers the page cover over the thumbnail fallback when the cover file exists', () => { const html = `X`; const out = injectOgTags(html, { ...ctx, fallbackImageUrl: 'https://m.tkmind.cn/x.thumbnail.png', htmlFilePath: '/tmp/public/page.html', fileExists: (target) => String(target).endsWith('assets/hero.jpg'), }); assert.match(out, /og:image" content="https:\/\/m\.tkmind\.cn\/MindSpace\/john\/public\/assets\/hero\.jpg"/); assert.doesNotMatch(out, /thumbnail\.png/); }); test('falls back to thumbnail png when the declared cover file is missing on disk', () => { const html = `X`; const out = injectOgTags(html, { ...ctx, fallbackImageUrl: 'https://m.tkmind.cn/x.thumbnail.png', htmlFilePath: '/tmp/public/page.html', fileExists: () => false, }); assert.match(out, //); assert.doesNotMatch(out, / { const html = `T`; const out = injectOgTags(html, ctx); assert.equal((out.match(/og:image/g) ?? []).length, 1); assert.match(out, //); }); 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, //); 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, //); }); test('uses meta override for shell pages that only expose a title', () => { const shell = `连云港三天两夜攻略 🌊`; const out = injectOgTags(shell, { ...ctx, fallbackImageUrl: 'https://m.tkmind.cn/MindSpace/john/public/demo.thumbnail.png', meta: { subtitle: '三天两夜·沙滩酒店·必吃美食' }, }); assert.match(out, //); assert.match(out, //); }); test('extractSharePreviewMeta returns the card fields used by preview tooling', () => { const html = injectOgTags( `标题`, ctx, ); const preview = extractSharePreviewMeta(html, ctx); assert.equal(preview.title, '标题'); assert.equal(preview.description, '描述'); assert.equal(preview.siteName, PLATFORM_SITE_NAME); assert.match(preview.iconUrl, /\/brand\/tkmind-icon\.png$/); }); test('renderWechatSharePreviewHtml includes title, description and site footer', () => { const html = renderWechatSharePreviewHtml({ title: '连云港三天两夜攻略 🌊', description: '三天两夜·沙滩酒店·必吃美食', siteName: PLATFORM_SITE_NAME, imageUrl: 'https://example.com/cover.png', iconUrl: '/brand/tkmind-icon.png', pageUrl: ctx.pageUrl, }); assert.match(html, /连云港三天两夜攻略/); assert.match(html, /三天两夜·沙滩酒店·必吃美食/); assert.match(html, /TKMind 智趣/); assert.match(html, /example\.com\/cover\.png/); }); test('injectWechatShareBridge adds WeChat share bootstrap with og-derived metadata', () => { const html = `页面标题` + `` + `` + `` + ``; const out = injectWechatShareBridge(html, { pageUrl: 'https://m.tkmind.cn/u/john/pages/demo', }); assert.match(out, /data-tkmind-wechat-share="1"/); assert.match(out, /fetch\(endpoint \+ '\?url='/); assert.match(out, /updateAppMessageShareData/); assert.match(out, /https:\/\/g2\.tkmind\.cn\/hero\.png/); assert.match(out, /分享标题/); assert.match(out, /分享描述/); }); test('injectWechatShareBridge falls back description when og tags are missing', () => { const html = `页面标题`; const out = injectWechatShareBridge(html, { pageUrl: 'https://m.tkmind.cn/u/john/pages/demo' }); assert.match(out, /TKMind 智趣 · 精选作品/); }); test('injectWechatShareBridge is idempotent', () => { const html = `X`; const first = injectWechatShareBridge(html, { pageUrl: 'https://m.tkmind.cn/u/john/pages/demo' }); const second = injectWechatShareBridge(first, { pageUrl: 'https://m.tkmind.cn/u/john/pages/demo' }); assert.equal((second.match(/data-tkmind-wechat-share="1"/g) ?? []).length, 1); });