2026-10-19 16:11:59 +08:00
// Execute the analysis page's actual lifecycle with synthetic status responses; no network/model access.
const fs = require ( 'node:fs' ), path = require ( 'node:path' ), vm = require ( 'node:vm' ), assert = require ( 'node:assert/strict' );
const ts = require ( 'D:/Develop/HBuilderX/plugins/uniapp-uts-v1/node_modules/@dcloudio/uni-uts-v1/lib/typescript/lib/typescript.js' );
const repo = path . resolve ( __dirname , '..' );
const compile = source => ts . transpileModule ( source ,{ compilerOptions : { module : ts . ModuleKind . CommonJS , target : ts . ScriptTarget . ES2020 }}). outputText ;
const routeExports = {}; vm . runInNewContext ( compile ( fs . readFileSync ( path . join ( repo , 'services/report-route.uts' ), 'utf8' )),{ exports : routeExports });
2026-10-22 11:24:15 +08:00
const matchRoutes = {}; vm . runInNewContext ( compile ( fs . readFileSync ( path . join ( repo , 'services/match-route.uts' ), 'utf8' )),{ exports : matchRoutes });
2026-10-19 16:11:59 +08:00
const script = fs . readFileSync ( path . join ( repo , 'packages/finance/analyzing/analyzing.uvue' ), 'utf8' ). match ( /<script setup lang="uts">([\s\S]*?)<\/script>/ )[ 1 ];
function status ( overrides = {}){ return { id : '42' , revisionId : '9' , publishedRevisionId : '9' , status : 'succeeded' , statusLabel : '分析完成' , message : '' , hasReport : true , stage : 'completed' , canRetry : false , taskId : 'task9' ,... overrides };}
function page ( reply ){
let token = 'synthetic-owner' , response = reply , onLoad , onShow , onHide , onUnload ; const navigations = [], timers = [];
const session = ()=> token == null ? null : { token , getString : key => key === 'token' ? token : '' };
const context = { exports : {}, Error , Date , Math , Promise , ref : value =>({ value }),
onLoad : fn =>{ onLoad = fn }, onShow : fn =>{ onShow = fn }, onHide : fn =>{ onHide = fn }, onUnload : fn =>{ onUnload = fn },
setTimeout : fn =>{ timers . push ( fn ); return timers . length }, clearTimeout : ()=>{}, setInterval : ()=> 1 , clearInterval : ()=>{},
uni : { redirectTo : ({ url })=>{ navigations . push ({ kind : 'redirect' , url })}, navigateTo : ({ url })=>{ navigations . push ({ kind : 'navigate' , url })}},
2026-10-22 11:24:15 +08:00
require : module => module . endsWith ( 'layout.uts' ) ? { usePageLayout : ()=>({})} : module . endsWith ( 'api.uts' ) ? { getSession : session } : module . endsWith ( 'records.uts' ) ? { fetchAssessmentStatus : async ()=> typeof response === 'function' ? response () : response } : module . endsWith ( 'report-route.uts' ) ? routeExports : module . endsWith ( 'match-route.uts' ) ? matchRoutes : (()=>{ throw Error ( 'Unexpected dependency ' + module )})()};
2026-10-19 16:11:59 +08:00
vm . runInNewContext ( compile ( script + '\nexports.refreshStatus=refreshStatus; exports.report=report; exports.statusMessage=()=>message.value;' ), context );
onLoad ({ assessmentId : '42' });
return { navigations , timers , show : ()=> onShow (), hide : ()=> onHide (), setReply : r =>{ response = r }, setToken : t =>{ token = t }, refresh : ()=> context . exports . refreshStatus (), manualReport : ()=> context . exports . report (), message : ()=> context . exports . statusMessage ()};
}
const flush = async ()=>{ await Promise . resolve (); await Promise . resolve (); await Promise . resolve ()};
let checks = 0 ; const check = ( ok , label )=>{ assert . ok ( ok , label ); checks ++ };
( async ()=>{
let p = page ( status ()); p . show (); await flush ();
check ( p . navigations . length === 1 , 'successful analysis automatically opens its result' );
2026-10-22 11:24:15 +08:00
assert . deepEqual ( p . navigations [ 0 ],{ kind : 'redirect' , url : '/packages/finance/matches/matches?assessmentId=42&productIds=&revisionId=9&group=all' }); checks ++ ;
p . manualReport (); assert . deepEqual ( p . navigations [ 1 ], p . navigations [ 0 ]); checks ++ ;
2026-10-19 16:11:59 +08:00
check ( p . timers . length === 0 , 'successful result does not keep polling' );
p = page ( status ({ status : 'processing' , stage : 'queued' , revisionId : '10' , publishedRevisionId : '9' })); p . show (); await flush ();
check ( p . navigations . length === 0 && p . timers . length === 1 , 'processing revision never opens the older published report' );
p . setReply ( status ({ revisionId : '10' , publishedRevisionId : '10' })); p . timers [ 0 ](); await flush ();
2026-10-22 11:24:15 +08:00
check ( p . navigations . length === 1 && p . navigations [ 0 ]. url === '/packages/finance/matches/matches?assessmentId=42&productIds=&revisionId=10&group=all' , 'poll completion opens the newly published match results' );
2026-10-19 16:11:59 +08:00
p = page ( status ({ status : 'failed' , canRetry : true })); p . show (); await flush (); check ( p . navigations . length === 0 , 'failed assessment stays on recovery page' );
check ( p . message (). includes ( '分析未完成' ) &&! p . message (). includes ( '可查看本次报告' ), 'failed analysis without server message does not announce completed report' );
p = page ( status ({ hasReport : false , publishedRevisionId : '' })); p . show (); await flush (); check ( p . navigations . length === 0 , 'missing report never navigates to an unfrozen result' );
p = page ( status ({ publishedRevisionId : '8' })); p . show (); await flush (); check ( p . navigations . length === 0 , 'inconsistent published revision is not opened as the new result' );
let resolve ; const pending = new Promise ( r =>{ resolve = r }); p = page (()=> pending ); p . show (); p . hide (); resolve ( status ()); await flush (); check ( p . navigations . length === 0 , 'late response after leaving cannot navigate' );
let switched ; const delayed = new Promise ( r =>{ switched = r }); p = page (()=> delayed ); p . show (); p . setToken ( 'other-account' ); switched ( status ()); await flush (); check ( p . navigations . length === 0 , 'account switch rejects former account result' );
p = page ( status ()); p . setToken ( null ); p . show (); await flush (); check ( p . navigations [ 0 ]. url === '/packages/account/login/login' , 'expired session requires login' );
console . log ( 'PASS: ' + checks + ' analysis result route checks; no network/model access' );
})(). catch ( e =>{ console . error ( e ); process . exitCode = 1 });