2026-10-14 09:11:33 +08:00
const fs = require ( 'node:fs' ) , path = require ( 'node:path' ) , assert = require ( 'node:assert/strict' )
const { chromium , frontend , backend , username , password } = require ( './vue3-test-runtime.cjs' )
2026-10-15 11:30:55 +08:00
const checkControlCells = require ( './vue3-cell-overflow-check.cjs' )
const { checkForms } = require ( './vue3-form-display-check.cjs' )
2026-10-14 09:11:33 +08:00
; ( async ( ) => {
const login = await ( await fetch ( backend + '/login' , { method : 'POST' , headers : { 'Content-Type' : 'application/json' } , body : JSON . stringify ( { username , password } ) } ) ) . json ( ) ; assert . equal ( login . code , 200 )
const menus = await ( await fetch ( backend + '/getRouters' , { headers : { Authorization : 'Bearer ' + login . token } } ) ) . json ( ) , routes = [ ]
function collect ( nodes , parent = '' ) { for ( const node of nodes || [ ] ) { const url = node . path . startsWith ( '/' ) ? node . path : ( parent + '/' + node . path ) . replace ( /\/+/g , '/' ) ; if ( node . children ? . length ) collect ( node . children , url ) ; else if ( node . component && ! node . hidden && ! /^https?:/ . test ( url ) ) routes . push ( { url , title : node . meta ? . title || url , source : node . component } ) } }
collect ( menus . data )
const extras = [ [ 'profile' , 'system/user/profile/index.vue' , '个人中心' ] , [ 'cache-list' , 'monitor/cache/list.vue' , '缓存列表' ] , [ 'cache' , 'monitor/cache/index.vue' , '缓存监控' ] , [ 'server' , 'monitor/server/index.vue' , '服务监控' ] , [ 'online' , 'monitor/online/index.vue' , '在线用户' ] , [ 'logininfor' , 'monitor/logininfor/index.vue' , '登录日志' ] , [ 'job' , 'monitor/job/index.vue' , '定时任务' ] , [ 'post' , 'system/post/index.vue' , '岗位' ] , [ 'dept' , 'system/dept/index.vue' , '部门' ] , [ 'config' , 'system/config/index.vue' , '参数设置' ] , [ 'notice' , 'system/notice/index.vue' , '通知公告' ] , [ 'dict' , 'system/dict/index.vue' , '系统字典' ] , [ 'gen' , 'tool/gen/index.vue' , '代码生成' ] , [ 'gen-edit/:tableId' , 'tool/gen/editTable.vue' , '生成配置' ] ]
const browser = await chromium . launch ( ) , out = path . resolve ( '.codex-tmp/vue3-all-pages-audit' ) ; fs . mkdirSync ( out , { recursive : true } )
const results = [ ] , errors = [ ] , warnings = [ ]
try {
const c = await browser . newContext ( { viewport : { width : 1366 , height : 768 } } ) ; await c . addCookies ( [ { name : 'Admin-Token' , value : login . token , url : frontend } ] ) ; const page = await c . newPage ( ) ; page . setDefaultTimeout ( 12000 )
page . on ( 'pageerror' , e => errors . push ( { url : page . url ( ) , message : e . message } ) ) ; page . on ( 'console' , m => { if ( m . type ( ) === 'warning' ) warnings . push ( { url : page . url ( ) , message : m . text ( ) . slice ( 0 , 600 ) } ) } )
const pending = new Set ( ) ; let lastActivity = Date . now ( )
page . on ( 'request' , r => { if ( r . url ( ) . includes ( '/dev-api/' ) ) { pending . add ( r ) ; lastActivity = Date . now ( ) } } )
const finish = r => { pending . delete ( r ) ; lastActivity = Date . now ( ) } ; page . on ( 'requestfinished' , finish ) ; page . on ( 'requestfailed' , finish )
async function settled ( ) { await page . waitForTimeout ( 100 ) ; const start = Date . now ( ) ; while ( pending . size || Date . now ( ) - lastActivity < 200 ) { if ( Date . now ( ) - start > 12000 ) throw Error ( 'API requests did not settle' ) ; await page . waitForTimeout ( 50 ) } await page . waitForFunction ( ( ) => ! [ ... document . querySelectorAll ( '.el-loading-mask' ) ] . some ( e => e . getClientRects ( ) . length && Number ( getComputedStyle ( e ) . opacity ) > . 1 ) ) ; await page . waitForTimeout ( 250 ) }
await page . route ( '**/dev-api/**' , async r => {
if ( ! [ 'GET' , 'HEAD' ] . includes ( r . request ( ) . method ( ) ) ) return r . abort ( 'blockedbyclient' )
const p = new URL ( r . request ( ) . url ( ) ) . pathname
// Cache contents and generation fixtures are synthetic; no real cache values are inspected.
if ( p === '/dev-api/monitor/cache/getNames' ) return r . fulfill ( { json : { code : 200 , data : Array . from ( { length : 30 } , ( _ , i ) => ( { cacheName : 'audit_' + i + ':' , remark : '只读测试缓存' } ) ) } } )
if ( p . includes ( '/monitor/cache/getKeys/' ) ) return r . fulfill ( { json : { code : 200 , data : Array . from ( { length : 40 } , ( _ , i ) => '测试键' + i ) } } )
if ( p . includes ( '/monitor/cache/getValue/' ) ) return r . fulfill ( { json : { code : 200 , data : { cacheName : '测试' , cacheKey : '测试键' , cacheValue : '模拟内容' } } } )
if ( p === '/dev-api/tool/gen/990001' ) return r . fulfill ( { json : { code : 200 , data : { info : { tableId : 990001 , tableName : 'audit_table' , tableComment : '效果验证表' , className : 'AuditTable' , functionAuthor : '测试' , tplCategory : 'crud' , packageName : 'com.audit' , moduleName : 'audit' , businessName : 'audit' , functionName : '测试' , genType : '0' } , tables : [ ] , rows : Array . from ( { length : 40 } , ( _ , i ) => ( { columnId : 990000 + i , columnName : 'column_' + i , columnComment : '测试字段 ' + i , columnType : 'varchar(100)' , javaType : 'String' , javaField : 'field' + i , isInsert : '1' , isEdit : '1' , isList : '1' , isQuery : '1' , queryType : 'EQ' , htmlType : 'input' , sort : i + 1 } ) ) } } } )
return r . continue ( )
} )
await page . goto ( frontend + '/index' ) ; await page . waitForLoadState ( 'networkidle' )
await page . evaluate ( async extras => { const router = document . querySelector ( '#app' ) . _ _vue _app _ _ . config . globalProperties . $router , { default : Layout } = await import ( '/src/layout/index.vue' ) ; for ( const [ slug , source , title ] of extras ) { const module = await import ( '/src/views/' + source ) ; router . addRoute ( { path : '/_audit' , component : Layout , children : [ { path : slug , name : 'Audit-' + slug , component : module . default , meta : { title , noCache : true } } ] } ) } } , extras )
const seen = new Set ( routes . map ( r => r . source ) ) ; for ( const [ slug , source , title ] of extras ) if ( ! seen . has ( source . replace ( /\.vue$/ , '' ) ) ) routes . push ( { url : '/_audit/' + slug . replace ( ':tableId' , '990001' ) , title , source } )
routes . push ( { url : '/system/user-auth/role/1' , title : '分配角色' } , { url : '/system/role-auth/user/1' , title : '分配用户' } , { url : '/system/dict-data/index/1' , title : '字典数据' } , { url : '/monitor/job-log/index/1' , title : '任务日志' } , { url : '/warehouse/batch-import' , title : '智能批量导入' } )
for ( const route of routes ) {
try {
await page . evaluate ( async url => { await document . querySelector ( '#app' ) . _ _vue _app _ _ . config . globalProperties . $router . push ( url ) } , route . url ) ; assert . equal ( new URL ( page . url ( ) ) . pathname , route . url , 'actual router must display the audited route' ) ; await settled ( )
const checks = [ ]
for ( const viewport of [ { width : 1366 , height : 768 } , { width : 1024 , height : 600 } ] ) {
2026-10-28 17:30:35 +08:00
await page . setViewportSize ( viewport ) ; await settled ( )
// ResizeObserver and table layout finish asynchronously, especially during parallel builds.
await page . waitForFunction ( ( ) => [ ... document . querySelectorAll ( '.business-list-page__table > .el-table' ) ] . filter ( e => e . getClientRects ( ) . length ) . every ( e => Math . abs ( e . getBoundingClientRect ( ) . height - e . parentElement . clientHeight ) <= 2 ) )
2026-10-27 21:17:27 +08:00
checks . push ( await page . evaluate ( ( ) => { const stage = document . querySelector ( '.desktop-viewport' ) , visible = e => e . getClientRects ( ) . length && getComputedStyle ( e ) . visibility !== 'hidden' , box = e => { const r = e . getBoundingClientRect ( ) ; return { x : r . x , y : r . y , width : r . width , height : r . height , bottom : r . bottom , right : r . right } } ; return { viewport : [ innerWidth , innerHeight ] , document : [ document . documentElement . scrollWidth , document . documentElement . scrollHeight ] , root : stage ? . firstElementChild ? . className , rootScroll : stage ? . firstElementChild ? { height : stage . firstElementChild . clientHeight , scroll : stage . firstElementChild . scrollHeight } : null , tables : [ ... ( stage ? . querySelectorAll ( 'div.el-table' ) || [ ] ) ] . filter ( e => visible ( e ) && e . querySelector ( '.el-table__body-wrapper' ) ) . map ( e => { const b = box ( e ) ; let lastRowReachable = b . bottom <= innerHeight + 1 ; if ( ! lastRowReachable ) { let parent = e . parentElement ; while ( parent && ! ( parent . scrollHeight > parent . clientHeight + 1 && /auto|scroll/ . test ( getComputedStyle ( parent ) . overflowY ) ) ) parent = parent . parentElement ; if ( parent && box ( parent ) . bottom <= innerHeight + 1 ) { const before = parent . scrollTop ; parent . scrollTop = parent . scrollHeight ; const last = e . querySelector ( '.el-table__body tbody tr:last-child' ) || e ; lastRowReachable = box ( last ) . bottom <= Math . min ( innerHeight , box ( parent ) . bottom ) + 1 ; parent . scrollTop = before } } return { ... b , lastRowReachable , columns : [ ... e . querySelectorAll ( '.el-table__header th' ) ] . map ( c => ( { text : c . innerText , width : c . getBoundingClientRect ( ) . width } ) ) } } ) , headers : [ ... ( stage ? . querySelectorAll ( '.query-toolbar,.business-list-page__header' ) || [ ] ) ] . filter ( visible ) . map ( e => ( { ... box ( e ) , overflow : e . scrollWidth - e . clientWidth } ) ) , loading : [ ... document . querySelectorAll ( '.el-loading-mask' ) ] . filter ( e => visible ( e ) && Number ( getComputedStyle ( e ) . opacity ) > . 1 ) . length } } ) )
2026-10-15 11:30:55 +08:00
await checkControlCells ( page , route . title + ' ' + viewport . width )
await checkForms ( page , route . title + ' ' + viewport . width , '.desktop-viewport' )
2026-10-14 09:11:33 +08:00
if ( viewport . width === 1366 ) await page . screenshot ( { path : path . join ( out , route . url . replace ( /[^a-zA-Z0-9]/g , '_' ) + '.png' ) , animations : 'disabled' } )
}
2026-10-27 21:17:27 +08:00
results . push ( { ... route , checks } ) ; const issues = checks . flatMap ( v => [ ... v . tables . filter ( t => ! t . lastRowReachable || t . height < 80 ) . map ( t => 'table ' + Math . round ( t . height ) + 'px bottom ' + Math . round ( t . bottom ) ) , ... v . headers . filter ( h => h . overflow > 2 ) . map ( ( ) => 'header overflow' ) , ... ( v . loading ? [ 'loading remains' ] : [ ] ) ] ) ; console . log ( 'CHECK' , route . title , JSON . stringify ( issues ) )
2026-10-14 09:11:33 +08:00
} catch ( e ) { results . push ( { ... route , error : e . message } ) ; console . log ( 'FAIL' , route . title , e . message . slice ( 0 , 120 ) ) }
}
fs . writeFileSync ( path . join ( out , 'result.json' ) , JSON . stringify ( { results , errors , warnings } , null , 2 ) )
fs . writeFileSync ( path . join ( out , 'index.html' ) , '<!doctype html><meta charset="utf-8"><title>全页面复核</title><style>body{font:14px system-ui;background:#f3f5f7;margin:24px;color:#303133}section{padding:16px;margin:20px 0;background:white}h2{font-size:16px}img{width:100%}</style><h1>全页面复核</h1><p>业务写入已拦截;缓存内容和生成配置使用模拟数据。包含菜单页面及隐藏的维护页面。</p>' + results . map ( r => '<section><h2>' + r . title + ' · ' + r . url + '</h2>' + ( r . error ? '<p>检查异常</p>' : '<img loading="lazy" src="' + r . url . replace ( /[^a-zA-Z0-9]/g , '_' ) + '.png">' ) + '</section>' ) . join ( '' ) )
console . log ( 'DONE' , JSON . stringify ( { pages : results . length , failures : results . filter ( r => r . error ) . length , errors , warnings } ) )
assert . equal ( results . filter ( r => r . error ) . length , 0 ) ; assert . deepEqual ( errors , [ ] ) ; assert . deepEqual ( warnings , [ ] )
2026-10-27 21:17:27 +08:00
for ( const r of results ) for ( const check of r . checks ) { if ( ! [ '/monitor/druid' , '/tool/swagger' ] . includes ( r . url ) ) assert . ok ( check . root , r . url + ' must mount a visible page' ) ; assert . ok ( ! check . loading , r . url + ' leaves a loading mask' ) ; check . tables . forEach ( t => assert . ok ( t . height >= 80 && t . lastRowReachable , r . url + ' table does not fit' ) ) ; check . headers . forEach ( h => assert . ok ( h . overflow <= 2 , r . url + ' header overflows' ) ) }
2026-10-14 09:11:33 +08:00
} finally { await browser . close ( ) }
} ) ( ) . catch ( e => { console . error ( e ) ; process . exitCode = 1 } )