Files
baiyue-firstleg/output/local-schema-repair/weight-flow.mjs
T
2026-10-20 11:33:20 +08:00

33 lines
3.4 KiB
JavaScript

import fs from 'node:fs';
import {randomUUID} from 'node:crypto';
const base=process.env.ADMIN_TEST_GATEWAY;
if(!base||!['localhost','127.0.0.1'].includes(new URL(base).hostname))throw new Error('仅允许本机');
const checks=[];let admin='',merchant='';
async function api(token,method,path,body,headers={}){
const r=await fetch(base+path,{method,headers:{...(token?{Authorization:'Bearer '+token}:{}),...(body?{'Content-Type':'application/json'}:{}),...headers},body:body?JSON.stringify(body):undefined,signal:AbortSignal.timeout(45000)});
const v=await r.json();checks.push({method,path,http:r.status,code:v.code,message:String(v.code)==='200'?undefined:v.message,passed:r.status===200&&String(v.code)==='200'});
fs.writeFileSync(new URL('weight-flow.json',import.meta.url),JSON.stringify(checks,null,2));
if(!checks.at(-1).passed)throw new Error(`${path}: ${r.status}/${v.code} ${v.message}`);return v.data;
}
admin=(await api('','POST','/api/auth/login',{loginId:process.env.ADMIN_TEST_USERNAME,password:process.env.ADMIN_TEST_PASSWORD})).accessToken;
const customer=await api(admin,'GET','/api/platform/v1/iam/tenants/2085308606829346818');
if(!JSON.stringify(customer).includes('测试'))throw new Error('目标并非既有测试客户,停止业务写入');
const before=await api(admin,'GET','/api/platform/v1/orders?tenantId=2085308606829346818&mainStatus=WAIT_WEIGHT_CONFIRM&page=1&size=5');
const target=before.records?.find(x=>x.inboundId);
if(!target)throw new Error('测试客户没有可确认来源');
const ticket=await api(admin,'POST','/api/platform/v1/iam/tenants/2085308606829346818/impersonation',{reason:'本地新增订单页签验收:独立确认测试客户计费重并核对队列流转'});
merchant=(await api('','POST','/api/auth/impersonations/exchange',{code:ticket.code})).accessToken;
const weights=await api(merchant,'GET',`/api/merchant/v1/first-leg/orders/${target.orderId}/chargeable-weights`);
const weight=weights.find(x=>String(x.inboundId)===String(target.inboundId)&&!x.confirmed);
if(!weight)throw new Error('测试来源没有当前待确认计费重');
const key=randomUUID();
const result=await api(merchant,'POST',`/api/merchant/v1/first-leg/chargeable-weights/${weight.inboundId}/confirm`,{expectedFingerprint:weight.fingerprint},{'Idempotency-Key':key});
if(!result.confirmed)throw new Error('未读回已确认事实');
await api(merchant,'POST',`/api/merchant/v1/first-leg/chargeable-weights/${weight.inboundId}/confirm`,{expectedFingerprint:weight.fingerprint},{'Idempotency-Key':key});
const confirmed=await api(admin,'GET',`/api/platform/v1/orders?tenantId=2085308606829346818&mainStatus=WEIGHT_CONFIRMED&inboundCode=${encodeURIComponent(target.inboundCode)}&page=1&size=5`);
const pending=await api(admin,'GET',`/api/platform/v1/orders?tenantId=2085308606829346818&mainStatus=WAIT_WEIGHT_CONFIRM&inboundCode=${encodeURIComponent(target.inboundCode)}&page=1&size=5`);
if(!confirmed.records.some(x=>String(x.orderId)===String(target.orderId))||pending.records.some(x=>String(x.orderId)===String(target.orderId)))throw new Error('确认后页签流转不一致');
checks.push({label:'同一测试订单确认后进入已确认、离开待确认;同幂等命令重放成功',passed:true,orderId:target.orderId,inboundId:weight.inboundId});
fs.writeFileSync(new URL('weight-flow.json',import.meta.url),JSON.stringify(checks,null,2));console.log(JSON.stringify(checks,null,2));