feat(pl): 实现合同产品用证与额度结转以支持购买证和运输证全流程管理。

This commit is contained in:
wuzilong committed 2026-11-01 18:56:05 +08:00
1 parent cb0344498a
commit fb866c7fa7
104 files changed
+10187 -3833

No files matched your search

@@ -72,7 +72,6 @@ function harness(page: string, overrides: Record<string, any> = {}, initialProps
writes.push(args)
return { data: { id: args[0], products: [] } }
},
setHazardCertReady: async (...args: any[]) => writes.push(args),
updateProductCerts: async (...args: any[]) => writes.push(args),
...overrides.planApi
}
@@ -262,18 +261,6 @@ test('arrange opt-in old internal info cannot publish after reset', async () =>
assert.equal(a.driverUrl.value, '')
assert.deepEqual(a.siteRecords.value, [])
})
test('cert parent delayed confirmation invalidated synchronously', async () => {
const confirm = deferred()
const h = harness('plan-cert/detail', { showCrmConfirmDialog: () => confirm.promise })
await h.init()
const pending = h.setReady({ id: 'A' }, true)
await flush()
confirm.resolve()
h.route.params.id = 'B'
await pending
assert.deepEqual(h.writes, [])
h.unmount()
})
test('cert upload old successful save cannot close or notify new record', async () => {
const saved = deferred()
const h = harness('plan-cert/CertUpload', {
@@ -491,94 +478,6 @@ test('arrange QR nextTick cannot copy newly bound dialog after reset', async ()
assert.equal(copies, 0)
assert.equal(a.qrcodeVisible.value, false)
})
for (const stage of ['get', 'confirm', 'write'])
for (const reject of [false, true])
test(`cert ready ${stage}: switched/ABA late ${reject ? 'error' : 'success'} ignored`, async () => {
const old = deferred(),
writes: any[] = []
const h = harness('plan-cert/detail', {
showCrmConfirmDialog: stage === 'confirm' ? () => old.promise : async () => {},
planApi: {
...(stage === 'get' ? { get: () => old.promise } : {}),
setHazardCertReady: (...args: any[]) => {
writes.push(args)
return stage === 'write' ? old.promise : Promise.resolve()
}
}
})
await h.init()
const pending = h.setReady({ id: 'A' }, true)
await flush()
h.route.params.id = 'B'
h.route.params.id = 'A'
h.acting.value = true
if (reject) old.reject(new Error('old'))
else old.resolve({ data: { id: 'A', products: [{ isToxic: 1 }] } })
await pending
assert.equal(writes.length, stage === 'write' ? 1 : 0)
assert.deepEqual(h.messages, [])
assert.equal(h.acting.value, true)
h.unmount()
})
for (const bad of [null, {}, { id: 'B', products: [{}] }])
test(`cert ready invalid read ${JSON.stringify(bad)}`, async () => {
const h = harness('plan-cert/detail', { planApi: { get: async () => ({ data: bad }) } })
await h.init()
await h.setReady({ id: 'A' }, true)
assert.deepEqual(h.writes, [])
assert.equal(h.acting.value, false)
h.unmount()
})
test('cert parent ready idempotence, mismatched event, saved identity and query gate', async () => {
let permissionReads = 0,
reloads = 0,
opens = 0
const h = harness('plan-cert/detail', {
fetchGetMenuCodes: async () => {
permissionReads++
return { all: true }
}
})
h.detailRef.value = {
reload: () => {
reloads++
},
openContract: () => {
opens++
},
invalidateRecord() {}
}
h.route.query.contractId = 'contract'
await flush()
assert.equal(opens, 0)
await h.init()
await h.init()
assert.equal(permissionReads, 1)
await h.setReady({ id: 'OTHER' }, true)
assert.deepEqual(h.calls, [])
await h.saved('OTHER')
assert.equal(reloads, 0)
await h.saved('A')
assert.equal(reloads, 1)
h.unmount()
})
for (const reject of [false, true])
test(`cert permissions retry supersedes old ${reject ? 'failure' : 'success'}`, async () => {
const old = deferred()
let n = 0
const h = harness('plan-cert/detail', {
fetchGetMenuCodes: () => (++n === 1 ? old.promise : Promise.resolve({ all: false }))
})
const pending = h.init()
await h.loadPermissions()
if (reject) old.reject(new Error('old'))
else old.resolve({ all: true })
await pending
assert.equal(h.canReady.value, false)
assert.equal(h.permissionError.value, false)
assert.equal(h.permissionsLoaded.value, true)
h.unmount()
})
for (const transition of ['B', 'ABA', 'close-reopen', 'unmount', 'invalidate', 'permission'])
for (const reject of [false, true])
test(`upload ${transition}: stale save ${reject ? 'error' : 'success'}`, async () => {
@@ -688,90 +587,6 @@ for (const reject of [false, true])
assert.equal(h.plan.value.id, 'B')
h.unmount()
})
function recordChildKey(parent: any, component: string) {
const template = parse(
readFileSync(new URL('../src/views/m/pl/plan-cert/detail.vue', import.meta.url), 'utf8')
).descriptor.template!.content
const key = template.match(new RegExp(`<${component}\\b[^>]*:key="([^"]+)"`))?.[1]
assert.ok(key, `${component} must have a record-instance key`)
return new Function('state', `with (state) { return (${key}) }`)(vue.proxyRefs(parent))
}
for (const change of ['record', 'ABA', 'query'])
test(`cert upload real parent renderer ${change} before child props flush`, async () => {
const saved = deferred()
let child: any
const Child = vue.defineComponent({
props: ['modelValue', 'planId', 'permitted'],
emits: ['update:modelValue', 'success'],
setup(props, context) {
child = harness('plan-cert/CertUpload', {
defineProps: () => props,
defineEmits: () => context.emit,
defineExpose: context.expose,
onBeforeUnmount: vue.onBeforeUnmount,
planApi: { updateProductCerts: () => saved.promise }
})
return () => vue.h('upload')
}
})
const renderer = vue.createRenderer({
insert(el: any, parent: any) {
el.parent = parent
},
remove() {},
createElement: () => ({}),
createText: () => ({}),
createComment: () => ({}),
setText() {},
setElementText() {},
parentNode: (n: any) => n.parent,
nextSibling: () => null,
patchProp() {}
})
const template = parse(
readFileSync(new URL('../src/views/m/pl/plan-cert/detail.vue', import.meta.url), 'utf8')
).descriptor.template!.content
assert.match(template, /<CertUpload\b[^>]*ref="uploadRef"/)
const parent = harness('plan-cert/detail')
await parent.init()
parent.uploadOpen.value = true
let successes = 0
const App = vue.defineComponent({
setup: () => () =>
vue.h(Child, {
key: recordChildKey(parent, 'CertUpload'),
ref: parent.uploadRef,
planId: parent.id.value,
modelValue: parent.uploadOpen.value,
permitted: parent.canUpload.value,
'data-tab': parent.route.query.tab,
'onUpdate:modelValue': (open: boolean) => {
parent.uploadOpen.value = open
},
onSuccess: (id: string) => {
successes++
void parent.saved(id)
}
})
})
const app = renderer.createApp(App)
app.mount({})
await flush()
const oldChild = child,
pending = child.save()
saved.resolve()
if (change !== 'query') {
parent.route.params.id = 'B'
if (change === 'ABA') parent.route.params.id = 'A'
} else parent.route.query.tab = 'notes'
assert.equal(oldChild.props.planId, 'A', 'Vue child props have not flushed')
await pending
await flush()
assert.equal(successes, change !== 'query' ? 0 : 1)
assert.equal(oldChild.messages.length, change !== 'query' ? 0 : 1)
app.unmount()
parent.unmount()
})
for (const reject of [false, true])
test(`cert detail same-record retry isolates old loader ${reject ? 'error' : 'success/finally'}`, async () => {
const old = deferred(),
@@ -831,84 +646,6 @@ test('transport local tabs retain single-line 44px targets and bordered content
)
assert.match(style, /\.mp-wv-section\s*\{[^}]*min-width:\s*0[^}]*box-sizing:\s*border-box/)
})
for (const component of ['CertDetail', 'CertUpload'])
test(`${component}: real parent same-tick ABA remounts, unrelated query preserves instance`, async () => {
let child: any,
mounts = 0,
reads = 0
const Child = vue.defineComponent({
props: ['planId', 'modelValue', 'permitted'],
setup(props, context) {
mounts++
const read = async (id: string) => {
reads++
return { id, products: [{ id: 'p', isToxic: 1 }] }
}
child = harness(`plan-cert/${component}`, {
defineProps: () => props,
defineExpose: context.expose,
onBeforeUnmount: vue.onBeforeUnmount,
usePlPlanViewDetail: detailLoader(read, true),
planApi: { get: read }
})
return () => vue.h('child')
}
})
const renderer = vue.createRenderer({
insert(el: any, parent: any) {
el.parent = parent
},
remove() {},
createElement: () => ({}),
createText: () => ({}),
createComment: () => ({}),
setText() {},
setElementText() {},
parentNode: (n: any) => n.parent,
nextSibling: () => null,
patchProp() {}
})
const parent = harness('plan-cert/detail')
await parent.init()
parent.uploadOpen.value = true
const App = vue.defineComponent({
setup: () => () =>
vue.h(Child, {
key: recordChildKey(parent, component),
ref: component === 'CertDetail' ? parent.detailRef : parent.uploadRef,
planId: parent.id.value,
modelValue: parent.uploadOpen.value,
permitted: parent.canUpload.value,
'data-tab': parent.route.query.tab
})
})
const app = renderer.createApp(App)
app.mount({})
await flush()
assert.equal(child.plan.value.id, 'A')
const first = child
if (component === 'CertUpload') child.products.value[0].purchaseCertUrl = 'preserved'
parent.route.query.tab = 'info'
await flush()
assert.equal(child, first)
assert.equal(reads, 1)
assert.equal(mounts, 1)
if (component === 'CertUpload')
assert.equal(child.products.value[0].purchaseCertUrl, 'preserved')
parent.route.params.id = 'B'
parent.route.params.id = 'A'
await flush()
assert.equal(mounts, 2, 'an invalidated child must never be reused for same-tick ABA')
if (component === 'CertUpload') {
parent.openUpload({ id: 'A' })
await flush()
}
assert.equal(reads, 2)
assert.equal(child.plan.value.id, 'A')
assert.equal(child.loading.value, false)
app.unmount()
parent.unmount()
})
for (const reject of [false, true])
test(`cert detail reload resets copy busy while obsolete ${reject ? 'failure' : 'success'} preserves new copy`, async () => {
const old = deferred(),
@@ -967,59 +704,6 @@ test('plan detail identity switch invalidates old reads and closes every record
h.unmount()
})
for (const change of ['token', 'user', 'logout'])
test(`cert parent ${change} change invalidates children and pending ready confirmation`, async () => {
const user = vue.reactive({ accessToken: 'old', info: { id: 'old-user' } })
const confirm = deferred()
let permissions = 0,
invalidations = 0
const h = harness('plan-cert/detail', {
useUserStore: () => user,
showCrmConfirmDialog: () => confirm.promise,
fetchGetMenuCodes: async () => (++permissions === 1 ? { all: true } : { menuCodes: [] })
})
await h.init()
h.detailRef.value = h.uploadRef.value = { invalidateRecord: () => invalidations++ }
h.uploadOpen.value = true
const pending = h.setReady({ id: 'A' }, true)
await flush()
const epoch = h.recordEpoch.value
if (change === 'token') user.accessToken = 'new'
else if (change === 'user') user.info = { id: 'new-user' }
else user.accessToken = ''
assert.equal(h.uploadOpen.value, false)
assert.equal(h.canReady.value, false)
assert.ok(h.recordEpoch.value > epoch)
assert.ok(invalidations >= 2)
confirm.resolve()
await pending
await flush()
assert.deepEqual(h.writes, [])
assert.equal(h.canUpload.value, false)
assert.equal(permissions, change === 'logout' ? 1 : 2)
h.unmount()
})
test('cert parent old permission read cannot grant access after identity changes', async () => {
const user = vue.reactive({ accessToken: 'old', info: { id: 'user' } })
const old = deferred()
let reads = 0
const h = harness('plan-cert/detail', {
useUserStore: () => user,
fetchGetMenuCodes: () => (++reads === 1 ? old.promise : Promise.resolve({ menuCodes: [] }))
})
const init = h.init()
user.accessToken = 'new'
await flush()
old.resolve({ all: true })
await init
assert.equal(h.canUpload.value, false)
assert.equal(h.canReady.value, false)
assert.equal(h.canPrice.value, false)
assert.equal(reads, 2)
h.unmount()
})
test('cert detail requests mobile metadata once and passes that snapshot to the shared read loader', async () => {
const snapshot = { id: 'A', products: [], deliveries: [] }
const reads: any[] = [],