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
+9 -9
View File
@@ -1,3 +1,4 @@
import {linkShared} from './helpers/shared-modules.mjs';
import {routingModuleUrl} from './helpers/routing.mjs';
import {test} from 'node:test';
import assert from 'node:assert/strict';
@@ -7,7 +8,7 @@ import ts from 'typescript';
async function moduleUrl(path, transform=code=>code){
const source=await readFile(new URL(path,import.meta.url),'utf8');
const {outputText}=ts.transpileModule(source,{compilerOptions:{target:ts.ScriptTarget.ES2022,module:ts.ModuleKind.ESNext}});
return 'data:text/javascript;base64,'+Buffer.from(transform(outputText).replace(/(['"])\.\.\/routing\1/g,JSON.stringify(routingModuleUrl))).toString('base64');
return 'data:text/javascript;base64,'+Buffer.from(linkShared(transform(outputText)).replace(/(['"])\.\.\/routing\1/g,JSON.stringify(routingModuleUrl))).toString('base64');
}
const routingUrl=await moduleUrl('../src/lib/routing.ts');
const {parseSitePath,routeFromSegments,navigationSection,internalPagePath,getRankPage,RANK_PAGES,samePageAddress}=await import(routingUrl);
@@ -32,11 +33,9 @@ test('clean paths select the matching page, IDs, and navigation section',()=>{
assert.deepEqual(parseSitePath('/shorts/'),{group:'page',key:'shorts'});
assert.deepEqual(parseSitePath('/topics/31/'),{group:'page',key:'topics',value:'31'});
assert.deepEqual(parseSitePath('/play/23456/90001/'),{group:'play',key:'23456',value:'90001'});
assert.deepEqual(parseSitePath('/info/deposit/vip/'),{group:'info',key:'deposit',value:'vip'});
assert.deepEqual(parseSitePath('/member/history/'),{group:'member',key:'history'});
assert.deepEqual(parseSitePath('/member/likes/'),{group:'member',key:'likes'});
assert.deepEqual(parseSitePath('/member/likes/page/2/'),{group:'member',key:'likes',page:2});
for(const [path,section] of [['/','home'],['/shorts/','shorts'],['/topics/31/','topics'],['/detail/7/','shorts'],['/member/center/','']]){
assert.deepEqual(parseSitePath('/info/about-us/'),{group:'info',key:'about-us'});
for(const path of ['/info/deposit/vip/','/info/deposit/','/info/feedback/','/info/user-center/','/info/settings/','/member/history/','/member/likes/','/member/likes/page/2/','/member/center/'])assert.equal(parseSitePath(path),null,path);
for(const [path,section] of [['/','home'],['/shorts/','shorts'],['/topics/31/','topics'],['/detail/7/','shorts']]){
assert.equal(navigationSection(parseSitePath(path)),section,path);
}
});
@@ -86,7 +85,8 @@ test('hash page links are not converted or treated as page destinations',()=>{
test('only recognized internal page destinations are handled by the application',()=>{
const base='https://drama.example/detail/7';
assert.equal(internalPagePath('/shorts/',base),'/shorts/');
assert.equal(internalPagePath('https://drama.example/member/history/',base),'/member/history/');
assert.equal(internalPagePath('https://drama.example/info/about-us/',base),'/info/about-us/');
assert.equal(internalPagePath('https://drama.example/member/history/',base),null);
assert.equal(internalPagePath('/search/'+encodeURIComponent('爱情 / 逆袭')+'/',base),'/search/'+encodeURIComponent('爱情 / 逆袭')+'/');
for(const href of ['https://external.example/shorts','javascript:alert(1)','/api/app/playlet/home','/assets/poster.webp','#comments','/unknown','/play/7/','/shorts/extra/','/member/unknown/','/detail/0/']){
assert.equal(internalPagePath(href,base),null,href);
@@ -99,8 +99,8 @@ test('page navigation requests a complete server document while leaving hashes,
try{
for(const path of ['#comments','/#page/shorts','https://external.example/shorts','/series/'])navigateTo(path);
assert.deepEqual(calls,[]);
for(const path of ['/play/23456/90001/','/member/history/','/rank/new/','/shorts/page/2/?sort=2'])navigateTo(path);
assert.deepEqual(calls,['/play/23456/90001/','/member/history/','/rank/new/','/shorts/page/2/?sort=2']);
for(const path of ['/play/23456/90001/','/info/about-us/','/rank/new/','/shorts/page/2/?sort=2'])navigateTo(path);
assert.deepEqual(calls,['/play/23456/90001/','/info/about-us/','/rank/new/','/shorts/page/2/?sort=2']);
assert.equal(globalThis.window.location.href,'https://drama.example/shorts/','the old document stays visible until the new response arrives');
}finally{globalThis.window=previous;}
});