|
| 1 | +import test from 'node:test'; |
| 2 | +import assert from 'node:assert/strict'; |
| 3 | +import { |
| 4 | + handleSharePage, |
| 5 | + injectMetadata, |
| 6 | + renderCard, |
| 7 | + safeHTTPSURL, |
| 8 | + socialPayload, |
| 9 | +} from '../worker.js'; |
| 10 | + |
| 11 | +const script = { |
| 12 | + title: '天气卡片 <测试>', |
| 13 | + summary: '展示未来天气', |
| 14 | + ai_summary: '一个简洁的天气卡片作品', |
| 15 | + category: 'appui', |
| 16 | + file_type: 'py', |
| 17 | + author_name: '社区作者', |
| 18 | + content: 'from appui import App\n\napp = App()\napp.run()', |
| 19 | + like_count: 12, |
| 20 | + run_count: 34, |
| 21 | +}; |
| 22 | + |
| 23 | +test('builds canonical per-script social metadata', () => { |
| 24 | + const meta = socialPayload(script, 'scr_123', 'https://link.pythonide.xin'); |
| 25 | + assert.equal(meta.title, '天气卡片 <测试>'); |
| 26 | + assert.equal(meta.description, '一个简洁的天气卡片作品'); |
| 27 | + assert.equal(meta.url, 'https://link.pythonide.xin/s/scr_123'); |
| 28 | + assert.equal(meta.image, 'https://link.pythonide.xin/og/script/scr_123.png'); |
| 29 | + assert.equal(meta.isGeneratedImage, true); |
| 30 | +}); |
| 31 | + |
| 32 | +test('uses an existing HTTPS cover as the share thumbnail', () => { |
| 33 | + const meta = socialPayload({ ...script, cover_image_url: 'https://cdn.example.com/cover.png' }, 'scr_123'); |
| 34 | + assert.equal(meta.image, 'https://cdn.example.com/cover.png'); |
| 35 | + assert.equal(meta.isGeneratedImage, false); |
| 36 | + assert.equal(safeHTTPSURL('http://cdn.example.com/cover.png'), ''); |
| 37 | +}); |
| 38 | + |
| 39 | +test('injects escaped metadata into the static share page', () => { |
| 40 | + const html = '<html><head><!-- edge:meta-start --><meta name="description" content="old"><!-- edge:meta-end --><title>Old</title></head></html>'; |
| 41 | + const output = injectMetadata(html, socialPayload(script, 'scr_123')); |
| 42 | + assert.match(output, /天气卡片 <测试>/); |
| 43 | + assert.match(output, /og:image/); |
| 44 | + assert.match(output, /application\/ld\+json/); |
| 45 | + assert.doesNotMatch(output, /<title>Old<\/title>/); |
| 46 | +}); |
| 47 | + |
| 48 | +test('renders a valid 1200 by 630 PNG social card', async () => { |
| 49 | + const bytes = await renderCard(script, 'scr_123'); |
| 50 | + assert.deepEqual(Array.from(bytes.slice(0, 8)), [137, 80, 78, 71, 13, 10, 26, 10]); |
| 51 | + const view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength); |
| 52 | + assert.equal(view.getUint32(16), 1200); |
| 53 | + assert.equal(view.getUint32(20), 630); |
| 54 | + assert.ok(bytes.byteLength > 10000); |
| 55 | +}); |
| 56 | + |
| 57 | +test('server-renders metadata before a social crawler receives the page', async () => { |
| 58 | + const staticHTML = '<html><head><!-- edge:meta-start --><meta name="description" content="old"><!-- edge:meta-end --><title>Old</title></head><body>Share page</body></html>'; |
| 59 | + const fetcher = async (url) => { |
| 60 | + if (String(url).includes('/v1/scripts/')) { |
| 61 | + return new Response(JSON.stringify({ script }), { status: 200, headers: { 'Content-Type': 'application/json' } }); |
| 62 | + } |
| 63 | + return new Response(staticHTML, { status: 200, headers: { 'Content-Type': 'text/html' } }); |
| 64 | + }; |
| 65 | + const response = await handleSharePage( |
| 66 | + new Request('https://link.pythonide.xin/s/scr_123'), |
| 67 | + 'scr_123', |
| 68 | + { STATIC_INDEX_URL: 'https://static.example/index.html' }, |
| 69 | + fetcher, |
| 70 | + ); |
| 71 | + const html = await response.text(); |
| 72 | + assert.equal(response.status, 200); |
| 73 | + assert.match(response.headers.get('content-type'), /text\/html/); |
| 74 | + assert.match(html, /天气卡片 <测试>/); |
| 75 | + assert.match(html, /https:\/\/link\.pythonide\.xin\/og\/script\/scr_123\.png/); |
| 76 | +}); |
0 commit comments