const assert = require('node:assert/strict'); const { readFileSync } = require('node:fs'); const { test } = require('node:test'); const ts = require('typescript'); const source = ts.createSourceFile( 'WorksGallery.tsx', readFileSync(`${__dirname}/../src/components/creator/WorksGallery.tsx`, 'utf8'), ts.ScriptTarget.Latest, true, ts.ScriptKind.TSX, ); test('作品卡片封面读本地缓存 /works/:id/cover(签名 URL 会过期)', () => { assert.match(sourceText(), /cover`/); assert.match(sourceText(), /fetchCreatorBinary/); assert.doesNotMatch(sourceText(), /cover_url/); }); test('观看/点赞/评论/分享四项指标全宽 GRID 贴封面下方', () => { const grid = /function WorkStatsGrid/.exec(sourceText()); assert.ok(grid, '应有 WorkStatsGrid 组件'); const body = sourceText().slice(grid.index, grid.index + 1300); for (const key of ['play_count', 'likes', 'comments_count', 'shares']) { assert.match(body, new RegExp(key), `GRID 应包含指标 ${key}`); } // 四列均分占满整行:每列 flex:1,容器自带上下边框贴封面。 assert.match(body, /flex: 1/); assert.match(body, /borderTop/); assert.match(body, /borderBottom/); }); test('无标题作品在卡片和详情中均不显示占位标题', () => { const text = sourceText(); assert.match(text, /\{work\.title \? \(/); assert.match(text, /\{detailWork\.title \? \(/); assert.doesNotMatch(text, /work\.title \|\| '无标题'|detailWork\.title \|\| '无标题'/); }); test('卡片展示作品描述(body 省略两行)', () => { const card = /work\.body \? \(/.exec(sourceText()); assert.ok(card, '卡片应有 body 描述分支'); const body = sourceText().slice(card.index - 120, card.index + 200); assert.match(body, /ellipsis=\{\{ rows: 2, tooltip: true \}\}/); }); function sourceText() { return source.getFullText(); } for (const [name, pagePath] of [ ['我的账号详情', 'accounts/$id'], ['监控账号详情', 'accounts/monitoring/$id'], ['竞品分析', 'creator/competitors'], ]) { test(`${name}使用共用作品列表,列数与封面比例保持一致`, () => { const page = readFileSync(`${__dirname}/../src/pages/${pagePath}/index.tsx`, 'utf8'); assert.match(page, /import WorksGallery from '@\/components\/creator\/WorksGallery'/); assert.match(page, / { assert.match(competitorsPageText, /import WorksGallery from '@\/components\/creator\/WorksGallery'/); assert.doesNotMatch(competitorsPageText, /\bWorkCover\b|