Files
web_hgcg_h5/tests/page-layouts.test.mjs
T
2026-10-27 17:39:27 +07:00

28 lines
2.0 KiB
JavaScript

import {routingModuleUrl} from './helpers/routing.mjs';
import {test} from 'node:test';
import assert from 'node:assert/strict';
import {readFile} from 'node:fs/promises';
import ts from 'typescript';
async function moduleUrl(file,replace=code=>code){
const filename=new URL(file,import.meta.url);
const {outputText}=ts.transpileModule(await readFile(filename,'utf8'),{compilerOptions:{target:ts.ScriptTarget.ES2022,module:ts.ModuleKind.ESNext}});
return 'data:text/javascript;base64,'+Buffer.from(replace(outputText).replace(/(['"])\.\.\/routing\1/g,JSON.stringify(routingModuleUrl))+'\n//# sourceURL='+filename.pathname).toString('base64');
}
const protocolUrl=await moduleUrl('../src/lib/api/protocol.ts');
const renderUrl=await moduleUrl('../src/lib/client/render.ts',code=>code.replace('"../api/protocol"',JSON.stringify(protocolUrl)));
const {card}=await import(renderUrl);
test('poster cards keep ranking numerals, descriptive link titles and alt text, and search highlights only escaped titles',()=>{
const drama={id:7,title:'测试 <短剧>',cover:'https://cdn.example/cover.webp',totalEpisode:10};
const ranked=card(drama,false,{rank:25});
assert.ok(ranked.includes('<article class="hg-card hg-card--ranked">'));assert.ok(ranked.includes('data-rank="25"'));assert.ok(ranked.includes('第 25 名'));assert.ok(ranked.includes('>25</span>'));
assert.ok(ranked.includes('href="/detail/7/"'));assert.ok(ranked.includes('data-src="https://cdn.example/cover.webp"'));
assert.ok(card(drama,false,{rank:1}).includes('>01</span>'));
assert.ok(!card(drama,false,{rank:-1}).includes('hg-card-rank'));
assert.ok(ranked.includes('title="《测试 &lt;短剧&gt;》全10集 在线观看"'));assert.ok(ranked.includes('alt="《测试 &lt;短剧&gt;》海报"'));assert.ok(ranked.includes('<span class="hg-card-eps">全10集</span>'));
const search=card(drama,false,{keyword:'<短剧>'});
assert.ok(search.includes('<span class="search-keyword">&lt;短剧&gt;</span>'));assert.ok(!search.includes('<短剧>'));
assert.ok(search.includes('<h3 class="hg-card-title">'));
});