This commit is contained in:
谢宇宁
2026-11-06 00:13:27 +07:00
parent f65d10e998
commit 37411fcada
98 changed files with 958 additions and 4598 deletions
+27 -55
View File
@@ -1,4 +1,5 @@
import {routingModuleUrl} from './helpers/routing.mjs';
import {linkShared,sharedModule} from './helpers/shared-modules.mjs';
import {test} from 'node:test';
import assert from 'node:assert/strict';
import {readFile} from 'node:fs/promises';
@@ -18,7 +19,8 @@ const renderUrl='data:text/javascript;base64,'+Buffer.from(renderCode.replace("'
const searchCode=ts.transpileModule(await readFile(new URL('../src/lib/client/search.ts',import.meta.url),'utf8'),{compilerOptions:{module:ts.ModuleKind.ESNext,target:ts.ScriptTarget.ES2022}}).outputText;
const search=await import('data:text/javascript;base64,'+Buffer.from(searchCode.replace("'../routing'",JSON.stringify(routingModuleUrl)).replace("'../api/protocol'",JSON.stringify(protocolUrl)).replace("'./render'",JSON.stringify(renderUrl))).toString('base64'));
const infoCode=ts.transpileModule(await readFile(new URL('../src/lib/info-pages.ts',import.meta.url),'utf8'),{compilerOptions:{module:ts.ModuleKind.ESNext,target:ts.ScriptTarget.ES2022}}).outputText;
const infoPages=await import('data:text/javascript;base64,'+Buffer.from(infoCode).toString('base64'));
const infoPages=await import('data:text/javascript;base64,'+Buffer.from(linkShared(infoCode)).toString('base64'));
const brandModule=await sharedModule('brand');
const siteNavigationCode=ts.transpileModule(await readFile(new URL('../src/lib/site-navigation.ts',import.meta.url),'utf8'),{compilerOptions:{module:ts.ModuleKind.CommonJS,target:ts.ScriptTarget.ES2022}}).outputText;
const applicationFile=new URL('../src/lib/client/application.ts',import.meta.url);
const applicationCode=ts.transpileModule(await readFile(applicationFile,'utf8'),{compilerOptions:{module:ts.ModuleKind.CommonJS,target:ts.ScriptTarget.ES2022}}).outputText;
@@ -38,6 +40,7 @@ function fixture(load=async()=>({list:[{id:1}],totalCount:72,name:'接口通用
const exports={},channelExports={},siteNavigationExports={};
const require=specifier=>{
if(specifier==='../site-navigation')return siteNavigationExports;
if(specifier==='../brand')return brandModule;
if(specifier==='./routing')return routing;
if(specifier==='./api/protocol')return protocol;
if(specifier==='./channel')return channelExports;
@@ -51,8 +54,6 @@ function fixture(load=async()=>({list:[{id:1}],totalCount:72,name:'接口通用
if(specifier==='./config')return {configureSite:async()=>{}};
if(specifier==='./hero-swipe')return {bindHeroSwipe(){}};
if(specifier==='./promo-marquee')return {bindPromotionMarquee:root=>{calls.push({path:'bindPromotionMarquee',root});return ()=>calls.push({path:'disposePromotionMarquee',root});}};
if(specifier==='./account')return {renderMember:async(...args)=>calls.push({path:'renderMember',args}),renderWallet:async(...args)=>calls.push({path:'renderWallet',args})};
if(specifier==='./feedback')return {renderFeedbackPage:(...args)=>calls.push({path:'renderFeedbackPage',args})};
if(specifier==='./player')return {renderPlayer:async(_app,id,chapterId,isVideo,options)=>{calls.push({path:'renderPlayer',id,chapterId,isVideo,...options});return ()=>{};}};
if(specifier==='./render')return {escape:String,empty:message=>`<p>${message}</p>`,loading:()=>'<p>加载中</p>',activeBanners:()=>[],topicCard:()=>'<a href="/topics/1/">专题</a>',renderHome:()=>'<div data-home-loaded>接口首页</div>',card:(item,video,options)=>{cardCalls.push({item,video,options});return '<a href="/detail/1/">剧集</a>';},pagination:(_,page)=>`<span>第 ${page} 页</span>`};
return {};
@@ -64,7 +65,7 @@ function fixture(load=async()=>({list:[{id:1}],totalCount:72,name:'接口通用
}
test('all native pages bind every square ad row and clean up old loops before rebinding',async()=>{
for(const pathname of ['/','/shorts/','/detail/7/','/play/7/8/','/member/favorites/','/info/deposit/']){
for(const pathname of ['/','/shorts/','/detail/7/','/play/7/8/','/info/about-us/']){
const rows=[{name:'first row'},{name:'second row'}],f=fixture(undefined,{promotionRows:rows});
f.location.pathname=pathname;await f.app.route();
assert.deepEqual(f.calls.filter(call=>call.path==='bindPromotionMarquee').map(call=>call.root),rows,pathname);
@@ -74,38 +75,29 @@ test('all native pages bind every square ad row and clean up old loops before re
}
});
test('about uses business Telegram and the feedback group while retaining configured service hours',()=>{
test('about shows only the single business Telegram contact, ignoring backend mail, feedback and service channels',()=>{
const page=infoPages.getInfoPage('about-us',{config:{telegram:'https://t.me/business_team',tgGroup:'https://t.me/feedback_group',gmailAddr:'ignored@example.com',customerServiceHost:'https://ignored.example.com'}},[
{name:'商务合作',value:'business@example.com'},{name:'用户反馈',value:'mailto:support@example.com'},
{name:'服务时间',value:'周一至周五 09:00–18:00'},{name:'邮箱',value:'ignored@example.com'},
]);
assert.ok(page.html.includes('商务合作:<a href="https://t.me/business_team"'));
assert.ok(page.html.includes('用户反馈:<a href="https://t.me/feedback_group"'));
assert.ok(page.html.includes('<p>服务时间:周一至周五 09:00–18:00</p>'));
assert.ok(!page.html.includes('mailto:'));assert.ok(!page.html.includes('@example.com'));assert.ok(!page.html.includes('ignored.example.com'));
assert.ok(page.html.includes('商务合作:<a href="https://t.me/jbd125"'));assert.ok(page.html.includes('Telegram @jbd125'));
for(const text of ['用户反馈','feedback_group','business_team','mailto:','@example.com','ignored.example.com','客服'])assert.ok(!page.html.includes(text),text);
});
test('Telegram contacts prefer site configuration, fall back to address configuration and hide absent channels',()=>{
const html=infoPages.infoContactSection({config:{telegram:'',tgGroup:''},newAddressInfo:{telegram:'@business_team',tgGroup:'https://t.me/feedback_group'}},null);
assert.ok(html.includes('商务合作:<a href="https://t.me/business_team"'));assert.ok(html.includes('用户反馈:<a href="https://t.me/feedback_group"'));
assert.ok(!html.includes('服务时间'));
const preferred=infoPages.infoContactSection({config:{telegram:'https://t.me/primary'},newAddressInfo:{telegram:'https://t.me/fallback'}});
assert.ok(preferred.includes('t.me/primary'));assert.ok(!preferred.includes('t.me/fallback'));assert.ok(!preferred.includes('用户反馈'));
assert.ok(infoPages.infoContactSection({config:{gmailAddr:'help@example.com',customerServiceHost:'https://ignored.example.com'}}).includes(' hidden'));
assert.ok(infoPages.infoContactSection({config:{telegramAdd:'https://t.me/legacy'}}).includes('t.me/legacy'));
test('the contact section is identical whatever the backend configures',()=>{
const expected=infoPages.infoContactSection();
for(const config of [{config:{telegram:'',tgGroup:''},newAddressInfo:{telegram:'@business_team',tgGroup:'https://t.me/feedback_group'}},{config:{telegram:'https://t.me/primary'}},{config:{gmailAddr:'help@example.com'}},{config:{telegramAdd:'https://t.me/legacy'}}])
assert.equal(infoPages.infoContactSection(config),expected);
assert.ok(expected.includes('href="https://t.me/jbd125"'));assert.ok(!expected.includes(' hidden'));
});
test('configured contacts escape text and reject executable or malformed addresses',()=>{
test('configured contact rows never reach the page, so executable addresses cannot be injected',()=>{
const html=infoPages.infoContactSection({},[
{name:'<img onerror=alert(1)>',value:'<script>alert(1)</script>'},
{name:'注入',value:'javascript:alert(1)'},
{name:'数据链接',value:'data:text/html,<script>alert(1)</script>'},
{name:'正常链接',value:'https://example.com/?a=1&b=2'},null,
{name:'数据链接',value:'data:text/html,<script>alert(1)</script>'},null,
]);
assert.ok(html.includes('&lt;script&gt;'));assert.ok(html.includes('&lt;img onerror=alert(1)&gt;'));
assert.ok(!html.includes('<script'));assert.ok(!html.includes('javascript:'));assert.ok(!html.includes('data:text'));
assert.ok(html.includes('href="https://example.com/?a=1&amp;b=2" target="_blank" rel="sponsored nofollow noopener noreferrer"'));
assert.ok(infoPages.infoContactSection({config:{gmailAddr:'javascript:bad@example.com'}}).includes(' hidden'));
for(const text of ['<script','<img','javascript:','data:text'])assert.ok(!html.includes(text),text);
});
test('ranking data and native page links derive from the server route and preserve global positions',()=>{
@@ -132,32 +124,12 @@ test('initial server channel and search content is retained without page request
}
});
test('server player data binds once with its comment route and never refetches the initial content',async()=>{
const player={detail:{id:23456,title:'剧集'},chapters:[],chapter:null,comments:{list:[],totalCount:0}};
const path='/play/23456/90001/page/2/',f=fixture(()=>assert.fail('no page fetch'),{page:{path:path+'?commentSort=1',panel:'player',title:'剧集',player}});
f.location.pathname=path;f.location.search='?commentSort=1';await f.app.route();
assert.equal(f.calls.length,1);assert.equal(f.calls[0].path,'renderPlayer');assert.equal(f.calls[0].initial,player);assert.equal(f.calls[0].preserveMarkup,true);assert.equal(f.calls[0].page,2);assert.equal(f.calls[0].commentSort,1);
assert.equal(f.calls[0].pageHref(3,1),'/play/23456/90001/page/3/?commentSort=1');await f.app.route();assert.equal(f.calls.length,1);assert.equal(f.reloads.length,1);
});
test('private and feedback pages only bind interactions to their prepared snapshots',async()=>{
for(const [path,info,handler] of [['/member/settings/',{key:'settings',member:{user:{userId:7}}},'renderMember'],['/info/deposit/',{key:'deposit',wallet:{user:{userId:7},packages:{coinList:[]},transactions:{list:[]}}},'renderWallet'],['/info/feedback/',{key:'feedback',feedback:true},'renderFeedbackPage']]){
const f=fixture(()=>assert.fail('no page fetch'),{page:{path,panel:'info',title:'完整页面',info}});f.location.pathname=path;await f.app.route();
assert.equal(f.calls.length,1);assert.equal(f.calls[0].path,handler);assert.equal(f.calls[0].args.at(-1),true);
}
});
test('cold unavailable member and wallet pages client-render after guest bootstrap',async()=>{
for(const [path,handler,tab] of [['/member/center/','renderMember','center'],['/member/history/','renderMember','history'],['/info/deposit/','renderWallet',undefined],['/info/deposit/vip/','renderWallet',undefined],['/member/settings/','renderMember','settings']]){
const f=fixture(()=>assert.fail('no page fetch'),{page:{path,panel:'info',title:'完整页面',info:{key:path.includes('deposit')?'deposit':tab},session:{user:null,state:'unavailable'}}});
f.location.pathname=path;await f.app.route();
assert.equal(f.calls.length,1,path);assert.equal(f.calls[0].path,handler,path);
if(handler==='renderMember')assert.equal(f.calls[0].args[1],tab);
if(path==='/info/deposit/vip/')assert.equal(f.calls[0].args[1],true);
if(path==='/info/deposit/')assert.equal(f.calls[0].args[1],false);
}
const signedOut=fixture(()=>assert.fail('login-required must keep SSR'),{page:{path:'/member/center/',panel:'info',title:'完整页面',info:{key:'center'},session:{user:null,state:'login-required'}}});
signedOut.location.pathname='/member/center/';await signedOut.app.route();assert.equal(signedOut.calls.length,0);
test('server player data binds once and never refetches the initial content',async()=>{
const player={detail:{id:23456,title:'剧集'},chapters:[],chapter:null};
const path='/play/23456/90001/',f=fixture(()=>assert.fail('no page fetch'),{page:{path,panel:'player',title:'剧集',player}});
f.location.pathname=path;await f.app.route();
assert.equal(f.calls.length,1);assert.equal(f.calls[0].path,'renderPlayer');assert.equal(f.calls[0].initial,player);assert.equal(f.calls[0].preserveMarkup,true);
await f.app.route();assert.equal(f.calls.length,1);assert.equal(f.reloads.length,1);
});
test('missing or mismatched snapshots leave server error HTML visible without a client retry',async()=>{
@@ -169,18 +141,18 @@ test('client configuration preserves the server content metadata and canonical',
const path='/topics/7/',environment=seo.resolveSeoEnvironment({config:{foreverUrlAddr:'https://canonical.drama.test'}}),content={title:'服务端专题',description:'服务端简介',totalCount:72,pageSize:24};
const f=fixture(()=>assert.fail('no page fetch'),{page:{path,panel:'channel',title:'服务端专题'},seo:{environment,content,path}});f.location.pathname=path;await f.app.route();
await f.app.applyConfig({config:{foreverUrlAddr:'https://other.drama.test'}});
assert.equal(f.document.title,'服务端专题 - 禁果短剧');assert.equal(f.document.value('link[rel="canonical"]'),'https://canonical.drama.test/topics/7/');
assert.equal(f.document.title,'服务端专题 - 黄果短剧');assert.equal(f.document.value('link[rel="canonical"]'),'https://canonical.drama.test/topics/7/');
});
test('shared server configuration initializes downloads and contacts without refetching page configuration or footer',async()=>{
const shared={config:{config:{officialAppAddr:'https://app.test/download'}},footer:{footerInfo:{contactList:[{name:'服务时间',value:'每天'}]}},trackerConfigured:false};
const shared={config:{config:{officialAppAddr:'https://app.test/download'}},footer:{footerInfo:{contactList:[{name:'服务时间',value:'每天'}]}}};
const f=fixture(()=>assert.fail('public settings already arrived in HTML'),{shared});
await f.app.initializeShared();assert.equal(f.app.config.config.officialAppAddr,'https://app.test/download');assert.equal(f.calls.length,0);
});
test('public back/forward documents keep working without reload; interactive private pages revalidate as fresh documents',async()=>{
test('public back/forward documents keep working without reload',async()=>{
const f=fixture();f.location.pathname='/shorts/';f.app.hidePage(true);assert.equal(f.app.signal.aborted,false);
f.app.restorePage();assert.equal(f.reloads.length,0);assert.deepEqual(f.calls.map(item=>item.path),['restore-session']);
const privatePage=fixture();privatePage.location.pathname='/info/deposit/';privatePage.app.hidePage(true);assert.equal(privatePage.app.signal.aborted,true);
privatePage.app.restorePage();assert.equal(privatePage.reloads.length,1);
const removed=fixture();removed.location.pathname='/info/about-us/';removed.app.hidePage(true);assert.equal(removed.app.signal.aborted,false);
removed.app.restorePage();assert.equal(removed.reloads.length,0);
});