cmvr-wms/ruoyi-ui-vue3/tests/unit/purchase-live-data.test.mjs
lixiaolong 771574fef7 feat: 完善采购协同、到货照片及验收发票流程
优化采购实时编辑与权限,支持先到货后补报价、按批次补录照片和批量附件上传。更新验收报告模板及明细选择,增加验收发票独立管理和付款前置校验。完善 BOM 数据权限及 App 更新兼容性。
2026-11-23 14:41:42 +08:00

45 lines
5.4 KiB
JavaScript

import test from 'node:test'
import assert from 'node:assert/strict'
import {createLiveAttempt,liveAttemptSheet,rebaseLiveSheet,createLiveEventParser,cloneLive,validateLiveReceipt} from '../../src/utils/purchase-live-data.js'
const fixture=()=>({version:'v1',batch:{id:1,batchName:'测试'},deletedIds:[],rows:[{id:1,_key:'saved-1',materialName:'物品一',quantity:1,remark:'原备注'},{id:2,_key:'saved-2',materialName:'物品二',quantity:2,remark:''}]})
test('legacy row acknowledgements converge without recreating the same new row on every autosave',()=>{
const base=fixture(),sent=cloneLive(base),key='new-1720000-0.12345';sent.rows.push({_key:key,id:null,materialName:'新物品',quantity:1})
const server=cloneLive(base);server.rows.push({id:3,materialName:'新物品',quantity:1});server.clientRowIds={[key]:3};server.version='v2'
validateLiveReceipt(sent,server);const merged=rebaseLiveSheet(sent,cloneLive(sent),server,createLiveAttempt(base,sent,'once'))
assert.equal(merged.proposed.rows.length,3);assert.equal(merged.proposed.rows[2].id,3)
for(let i=0;i<10;i++){validateLiveReceipt(merged.proposed,server);assert.equal(rebaseLiveSheet(merged.latest,merged.proposed,server).proposed.rows.length,3)}
delete server.clientRowIds;assert.throws(()=>validateLiveReceipt(sent,server),/暂停自动保存/)
})
test('new-row acknowledgements reject missing, reused and duplicate database identifiers',()=>{
const sent={rows:[{id:1,_key:'saved-1'},{id:null,_key:'new-a'},{id:null,_key:'new-b'}]},server={rows:[{id:1},{id:2},{id:3}],clientRowIds:{'new-a':2,'new-b':3}}
validateLiveReceipt(sent,server)
for(const mapping of [{},{'new-a':1,'new-b':3},{'new-a':2,'new-b':2},{'new-a':2,'new-b':99}])assert.throws(()=>validateLiveReceipt(sent,{...server,clientRowIds:mapping}))
})
test('an immutable attempt retries original cells even after subsequent typing',()=>{
const base=fixture(),local=cloneLive(base);local.rows[0].remark='本次';const attempt=createLiveAttempt(base,local,'test-key');local.rows[0].remark='后续输入';const sent=liveAttemptSheet(base,attempt)
assert.equal(sent.rows[0].remark,'本次');assert.equal(sent.rows[0].clientRowKey,'saved-1');assert.equal(sent.submissionKey,'test-key');assert.equal(attempt.rows.length,1);assert.throws(()=>liveAttemptSheet({...base,version:'new'},attempt),/快照不匹配/)
})
test('remote rows and fields become visible without overwriting current local edits',()=>{
const base=fixture(),local=cloneLive(base),server=cloneLive(base);local.rows[0].remark='我的';server.version='v2';server.rows[1].quantity=7;server.rows.push({id:3,materialName:'新增',quantity:9})
const result=rebaseLiveSheet(base,local,server);assert.equal(result.conflicts.length,0);assert.equal(result.proposed.rows[0].remark,'我的');assert.equal(result.latest.rows[0].remark,'原备注');assert.equal(result.proposed.rows[1].quantity,7);assert.equal(result.proposed.rows[2]._key,'saved-3')
})
test('the same field and related identity fields produce an explicit conflict',()=>{
const base=fixture(),local=cloneLive(base),server=cloneLive(base);local.rows[0].remark='我的';server.rows[0].remark='他人';assert.equal(rebaseLiveSheet(base,local,server).conflicts[0].label,'备注')
local.rows[0].remark='原备注';local.rows[0].materialParams='规格A';server.rows[0].remark='原备注';server.rows[0].materialName='其他物品';assert.equal(rebaseLiveSheet(base,local,server).conflicts[0].fields.includes('materialParams'),true)
})
test('self acknowledgements retain subsequent typing and stable keys for assigned new row IDs',()=>{
const base=fixture(),sent=cloneLive(base);sent.rows.push({id:null,_key:'new-local',materialName:'新增',productId:null,quantity:1});const attempt=createLiveAttempt(base,sent,'test-key'),local=cloneLive(sent),server=cloneLive(sent)
local.rows[2].materialName='继续输入';server.rows[2]={id:3,materialName:'新增',productId:8,quantity:1};server.clientRowIds={'new-local':3};server.version='v2'
const result=rebaseLiveSheet(sent,local,server,attempt);assert.equal(result.conflicts.length,0);assert.equal(result.proposed.rows[2]._key,'new-local');assert.equal(result.proposed.rows[2].id,3);assert.equal(result.proposed.rows[2].materialName,'继续输入')
})
test('deleting a new row while its creation is in flight schedules deletion of its assigned ID',()=>{
const base=fixture(),sent=cloneLive(base);sent.rows.push({id:null,_key:'new-local',materialName:'新增',quantity:1});const local=cloneLive(base),server=cloneLive(base);server.rows.push({id:3,materialName:'新增',quantity:1});server.clientRowIds={'new-local':3}
const result=rebaseLiveSheet(sent,local,server,createLiveAttempt(base,sent,'key'));assert.deepEqual(result.proposed.deletedIds,[3]);assert.equal(result.proposed.rows.length,2)
})
test('a concurrent deletion cannot silently discard local changes',()=>{
const base=fixture(),local=cloneLive(base),server=cloneLive(base);local.rows[0].remark='不能丢失';server.rows.shift();assert.equal(rebaseLiveSheet(base,local,server).conflicts[0].kind,'removed')
})
test('SSE framing supports fragmented input, CRLF and comments without processing incomplete data',()=>{
const events=[],feed=createLiveEventParser((type,data)=>events.push([type,data]));feed(':ping\r\nevent: member\r\ndata: {"name":"张');assert.equal(events.length,0);feed('三"}\r\n\r\nevent: saved\ndata: {"batchId":1}\n\n');assert.deepEqual(events,[['member',{name:'张三'}],['saved',{batchId:1}]])
})