import assert from 'node:assert/strict'; import { existsSync, readFileSync } from 'node:fs'; import { Buffer } from 'node:buffer'; import ts from 'typescript'; const moduleUrl = new URL('../server/src/utils/platformWorkCover.ts', import.meta.url); assert.ok( existsSync(moduleUrl), 'server/src/utils/platformWorkCover.ts should export platform work cover helpers', ); const source = readFileSync(moduleUrl, 'utf8'); const { outputText } = ts.transpileModule(source, { compilerOptions: { module: ts.ModuleKind.ES2022, target: ts.ScriptTarget.ES2022, }, }); const dataUrl = `data:text/javascript;base64,${Buffer.from(outputText, 'utf8').toString('base64')}`; const { extractPlatformWorkCoverUrl, normalizePlatformImageUrl } = await import(dataUrl); assert.equal(normalizePlatformImageUrl('//example.com/a.jpg'), 'https://example.com/a.jpg'); assert.equal(normalizePlatformImageUrl(' http://example.com/a.jpg?x=1 '), 'http://example.com/a.jpg?x=1'); assert.equal(normalizePlatformImageUrl('not-a-url'), ''); assert.equal( extractPlatformWorkCoverUrl({ cover_images: '[{"src":"//bjh.example.com/cover-a.jpg"},{"src":"//bjh.example.com/cover-b.jpg"}]', }), 'https://bjh.example.com/cover-a.jpg', ); assert.equal( extractPlatformWorkCoverUrl({ coverImage: { imageUrl: 'https://bjh.example.com/cover-c.jpg' }, }), 'https://bjh.example.com/cover-c.jpg', ); assert.equal( extractPlatformWorkCoverUrl({ objectDesc: { mediaList: [ { coverUrl: 'https://wx.example.com/post-cover.jpg', }, ], }, }), 'https://wx.example.com/post-cover.jpg', ); assert.equal( extractPlatformWorkCoverUrl({ mediaSpec: { coverUrl: '//wx.example.com/media-cover.jpg', }, }), 'https://wx.example.com/media-cover.jpg', ); assert.equal( extractPlatformWorkCoverUrl({ desc: { media: [ { thumbUrl: 'https://wx.example.com/desc-media-thumb.jpg', coverUrl: 'https://wx.example.com/desc-media-cover.jpg', }, ], }, }), 'https://wx.example.com/desc-media-cover.jpg', ); assert.equal( extractPlatformWorkCoverUrl({ title: 'no image here', author: { avatarUrl: 'https://example.com/avatar.jpg' }, }), '', ); console.log('platform work cover extraction checks passed');