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
+7 -7
View File
@@ -1,3 +1,4 @@
import {linkShared} from './helpers/shared-modules.mjs';
import {test} from 'node:test';
import assert from 'node:assert/strict';
import {readFile} from 'node:fs/promises';
@@ -6,7 +7,7 @@ import ts from 'typescript';
async function moduleUrl(file,transform=code=>code){
const source=await readFile(new URL(file,import.meta.url),'utf8');
const code=ts.transpileModule(source,{fileName:new URL(file,import.meta.url).pathname,compilerOptions:{module:ts.ModuleKind.ESNext,target:ts.ScriptTarget.ES2022,jsx:ts.JsxEmit.ReactJSX}}).outputText;
return 'data:text/javascript;base64,'+Buffer.from(transform(code)).toString('base64');
return 'data:text/javascript;base64,'+Buffer.from(linkShared(transform(code))).toString('base64');
}
const protocol=await moduleUrl('../src/lib/api/protocol.ts'),auth=await moduleUrl('../src/lib/client/auth.ts');
const apiUrl=await moduleUrl('../src/lib/client/api.ts',code=>code.replace('"../api/protocol"',JSON.stringify(protocol)).replace('"./auth"',JSON.stringify(auth)));
@@ -15,7 +16,7 @@ const startupUrl='data:text/javascript,export const calls=[];export function sta
const startup=await import(startupUrl);
const scriptUrl='data:text/javascript,export default function Script(){}';
const runtimeUrl=await moduleUrl('../src/components/site/SiteRuntime.tsx',code=>{
const imports={'react/jsx-runtime':import.meta.resolve('react/jsx-runtime'),'next/script':scriptUrl,'@/lib/client/application':startupUrl,'@/lib/client/analytics':startupUrl};
const imports={'react/jsx-runtime':import.meta.resolve('react/jsx-runtime'),'next/script':scriptUrl,'@/lib/client/application':startupUrl};
for(const [path,url] of Object.entries(imports))for(const quote of ["'",'"'])code=code.replace(quote+path+quote,JSON.stringify(url));
return code;
});
@@ -55,16 +56,15 @@ test('API business failures, invalid JSON and network failures remain rejected w
});
});
test('home, player, navigation and wallet hydration starts normally without logging server data or refetching',async()=>{
test('home, player and info hydration starts the application only, without analytics, logging or refetching',async()=>{
await browser(async()=>{
globalThis.fetch=()=>assert.fail('hydration must use existing server data');
const wallet={user:{userId:77,recharge:90,token:'session-secret'},packages:{coinList:[{id:31,name:'金币套餐',price:10000}],vipList:[]},transactions:{list:[{id:12,amount:30}],totalCount:18}};
const home={homeConfigData:[{id:31,moreUrl:'/topics/'}]},navigation=[{name:'短剧',url:'/shorts/'}];
for(const page of [{path:'/',panel:'home'},{path:'/detail/7/',player:{title:'短剧'}},{path:'/info/deposit/page/2/',panel:'info',info:{key:'deposit',wallet}}]){
for(const page of [{path:'/',panel:'home'},{path:'/detail/7/',player:{title:'短剧'}},{path:'/info/about-us/',panel:'info',info:{key:'about-us'}}]){
const start=startup.calls.length;
SiteRuntime({home,homeLoaded:true,page,navigation}).props.onReady();
assert.equal(startup.calls[start],'tracker');assert.equal(startup.calls[start+1][0],home);assert.equal(startup.calls[start+1][3],page);
assert.equal(startup.calls.length,start+1);assert.equal(startup.calls[start][0],home);assert.equal(startup.calls[start][3],page);
}
assert.equal(wallet.user.token,'session-secret');
assert.ok(!startup.calls.includes('tracker'));
});
});