import assert from 'node:assert/strict'; import test from 'node:test'; import { articleImageSource, ARTICLE_IMAGE_LIMIT, } from '../../src/components/common/ui/richTextImages.js'; import { articleErrors, emptyInsights, newArticle, } from '../../src/pages/WebsiteSettings/insightsModel.js'; import { emptySettings, serializeSettings, } from '../../src/pages/WebsiteSettings/model.js'; const png = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mP8/x8AAwMCAO+jRZkAAAAASUVORK5CYII='; test('pasted images survive article validation and final JSON serialization without a cover field', () => { const article = { ...newArticle('company'), id: 'pasted', title: '图文粘贴', summary: '摘要', body: `

图文标题

正文

`, }; const insights = { ...emptyInsights(), articles: [article] }; assert.deepEqual(articleErrors(article, insights), {}); const restored = JSON.parse( serializeSettings({ ...emptySettings(), insights }), ); assert.equal(restored.insights.articles[0].body, article.body); assert.equal('cover' in restored.insights.articles[0], false); }); test('image sources accept public URLs and bounded raster data, rejecting unsafe or temporary sources', () => { assert.equal(articleImageSource(png), png); assert.equal( articleImageSource('https://cdn.example.com/a.png'), 'https://cdn.example.com/a.png', ); for (const source of [ 'javascript:alert(1)', 'blob:https://example.com/id', '/private/a.png', 'file:///tmp/a.png', 'https://user:pass@example.com/a.png', 'data:image/svg+xml;base64,PHN2Zz4=', 'data:text/html;base64,AAAA', 'data:image/png;base64,notbase64!', ]) assert.equal(articleImageSource(source), '', source); const maximum = 'A'.repeat(Math.ceil(ARTICLE_IMAGE_LIMIT / 3) * 4 - 1) + '='; assert.ok(articleImageSource(`data:image/png;base64,${maximum}`)); assert.equal( articleImageSource( `data:image/png;base64,${'A'.repeat(maximum.length + 4)}`, ), '', ); });