Files
shanxia2.0/web-admin/scripts/quota-settlement-submission.test.ts
T

40 lines
2.6 KiB
TypeScript

import assert from 'node:assert/strict'
import { readFileSync } from 'node:fs'
import test from 'node:test'
import { parse } from '@vue/compiler-sfc'
import ts from 'typescript'
function harness() {
const script = parse(readFileSync(new URL('../src/components/business/pl-quota-settlement-dialog/index.vue', import.meta.url), 'utf8')).descriptor.scriptSetup!.content
const ast = ts.createSourceFile('settlement.ts', script, ts.ScriptTarget.Latest, true)
const methods = ast.statements.filter(n => ts.isFunctionDeclaration(n) && ['accept','chooseSupplement'].includes(n.name?.text || '')).map(n=>n.getText(ast)).join('\n')
const results: any[] = [], warnings: string[] = []
let finish!: (v:any)=>void
const state:any = {
allowSupplement:{value:true}, choosing:{value:false}, supplementConfiguration:{value:undefined},
targetPlanId:{value:'a'}, rows:{value:[]}, adjustment:{value:false}, reason:{value:'超量'},
sourceNote:{value:'磅单'}, evidenceUrls:{value:''}, attachments:{value:undefined}, visible:{value:true},
supplementDialog:{value:{open:()=>new Promise(r=>{finish=r})}},
ElMessage:{warning:(m:string)=>warnings.push(m)}
}
const js=ts.transpileModule(`let epoch=0; let resolve=(v)=>results.push(v);${methods};return {accept,choose:chooseSupplement,invalidate:()=>epoch++}`,{compilerOptions:{target:ts.ScriptTarget.ES2022}}).outputText
const actions=new Function(...Object.keys(state),'results',js)(...Object.values(state),results)
return {...actions,state,results,warnings,finish:(v:any)=>finish(v)}
}
test('supplement requires candidates and provenance before forwarding explicit intent',()=>{
const h=harness();h.accept();assert.equal(h.results.length,0)
h.state.supplementConfiguration.value={purchaseCertIds:['g'],transportCertIds:['y'],conditions:[{certId:'g'}]}
h.state.sourceNote.value='';h.accept();assert.equal(h.results.length,0)
h.state.sourceNote.value='磅单';h.accept()
assert.equal(h.results[0].quotaAllowSupplement,true)
assert.deepEqual(h.results[0].quotaConfiguration,h.state.supplementConfiguration.value)
})
test('late supplementary selection cannot update another settlement session',async()=>{
const h=harness(),task=h.choose();h.invalidate();h.finish({purchaseCertIds:['old']});await task
assert.equal(h.state.supplementConfiguration.value,undefined)
})
test('turning off supplement does not forward stale extra candidates',()=>{
const h=harness();h.state.allowSupplement.value=false;h.state.supplementConfiguration.value={purchaseCertIds:['old']};h.accept()
assert.equal(h.results[0].quotaAllowSupplement,false)
assert.equal(h.results[0].quotaConfiguration,undefined)
})