cmvr-wms/scripts/projectdev/history-entry-ui-fixture.cjs

36 lines
4.5 KiB
JavaScript
Raw Normal View History

// Isolated browser QA. /dev-api is completely mocked; this server never contacts
// the real backend, database, or source data, and never forwards a business write.
const http=require('node:http'),fs=require('node:fs'),path=require('node:path'),assert=require('node:assert/strict')
const output=path.resolve(__dirname,'../../.codex-tmp/history-entry-ui');fs.mkdirSync(output,{recursive:true})
const definitions=[{key:'title',label:'模块标题',type:'varchar',length:50},{key:'oper_name',label:'原操作者',type:'varchar',length:50},{key:'oper_time',label:'原操作时间',type:'datetime',length:null},{key:'status',label:'原状态',type:'int',length:null},{key:'oper_param',label:'请求参数',type:'varchar',length:2000},{key:'error_msg',label:'错误信息',type:'varchar',length:2000}]
const fields={title:'补录界面模拟测试',oper_name:'历史测试用户',oper_time:'2025-05-06 07:08:09.0',status:0,oper_param:'{"message":"原始测试内容,不含真实数据"}',error_msg:null}
const histories=[{sourceId:101,title:fields.title,operator:fields.oper_name,time:fields.oper_time,saved:false},{sourceId:102,title:'补录界面第二条测试',operator:fields.oper_name,time:fields.oper_time,saved:false}]
const operations=[],state={saveAttempts:0,saved:0,realBusinessWrites:0,failNext:true}
function record(){fs.writeFileSync(path.join(output,'results.json'),JSON.stringify({...state,operations},null,2))}
const server=http.createServer(async(req,res)=>{
const url=new URL(req.url,'http://history-ui-test.localhost:8099'),ok=data=>{res.setHeader('content-type','application/json;charset=utf-8');res.end(JSON.stringify(data))}
if(url.pathname.startsWith('/dev-api/')){
let text='';for await(const chunk of req)text+=chunk;const body=text?JSON.parse(text):{}
if(url.pathname==='/dev-api/captchaImage')return ok({code:200,captchaEnabled:false})
if(url.pathname==='/dev-api/login')return ok({code:200,token:'isolated-ui-fixture-not-a-real-token'})
if(url.pathname==='/dev-api/getInfo')return ok({code:200,user:{userId:1,userName:'fixture',nickName:'界面模拟测试',avatar:''},roles:['admin'],permissions:['*:*:*']})
if(url.pathname==='/dev-api/getRouters')return ok({code:200,data:[]})
if(url.pathname==='/dev-api/system/history-entry/types')return ok({code:200,data:[{kind:'operation',label:'操作记录',total:2,saved:histories.filter(r=>r.saved).length},{kind:'login',label:'登录记录',total:0,saved:0}]})
if(url.pathname==='/dev-api/system/history-entry/list'){const pending=url.searchParams.get('pending')==='true',kind=url.searchParams.get('kind');const rows=kind==='operation'?histories.filter(r=>!pending||!r.saved):[];return ok({code:200,data:{rows,total:rows.length}})}
if(/^\/dev-api\/system\/history-entry\/operation\/\d+$/.test(url.pathname)){const id=Number(url.pathname.split('/').pop());return ok({code:200,data:{kind:'operation',sourceId:id,sourceHash:'fixture-hash-'+id,fields:{...fields,title:histories.find(r=>r.sourceId===id).title},definitions,references:[]}})}
if(url.pathname==='/dev-api/system/history-entry'&&req.method==='POST'){
state.saveAttempts++;operations.push({kind:body.kind,sourceId:body.sourceId,fields:body.fields,references:body.references});record()
const expected={...fields,title:histories.find(r=>r.sourceId===body.sourceId)?.title}
try{assert.deepEqual(body.fields,expected)}catch{return ok({code:500,msg:'字段与原记录不一致,未保存'})}
if(state.failNext){state.failNext=false;record();return ok({code:500,msg:'模拟保存失败,原内容应保留'})}
const row=histories.find(r=>r.sourceId===body.sourceId);const alreadySaved=row.saved;if(!alreadySaved){row.saved=true;state.saved++}record();return ok({code:200,data:{targetKey:String(500+body.sourceId),alreadySaved}})
}
if(req.method!=='GET')return ok({code:500,msg:'测试环境禁止其他写入'})
return ok({code:200,data:[],rows:[],total:0})
}
if(!['GET','HEAD'].includes(req.method)){res.writeHead(405);return res.end('Writes cannot be proxied')}
const proxy=http.request({hostname:'127.0.0.1',port:8088,path:req.url,method:req.method,headers:{...req.headers,host:'localhost:8088'}},upstream=>{res.writeHead(upstream.statusCode,upstream.headers);upstream.pipe(res)})
proxy.on('error',()=>{res.writeHead(502);res.end('Frontend unavailable')});req.pipe(proxy)
})
record();server.listen(8099,'127.0.0.1',()=>console.log('History UI fixture http://history-ui-test.localhost:8099/system/history-entry; all API operations mocked'))