import test from 'node:test' import assert from 'node:assert/strict' import fs from 'node:fs' import { purchaseProjectState, purchaseDraftBatchCount, purchaseBatchProjectChanges } from '../../src/utils/purchase-project.js' import { assistantApplyDraftChanges, assistantBulkChanges, assistantPurchaseProjectChanges } from '../../src/utils/assistant-draft-edit.js' import { createPurchaseDraft } from '../../src/utils/purchase-sheet-create.js' test('one batch has one common project and missing information can be distinguished from mixed history', () => { assert.deepEqual(purchaseProjectState([{projectId:1},{projectId:'1'}]), {mixed:false,count:1,missing:0,value:1}) assert.equal(purchaseProjectState([{projectId:1},{}]).missing,1) assert.equal(purchaseProjectState([{projectId:1},{projectId:2}]).mixed,true) assert.deepEqual(purchaseProjectState([{},{}]),{mixed:false,count:0,missing:2,value:null}) assert.equal(purchaseProjectState([{projectId:{keyword:'研发项目',code:'P1'}},{projectId:{code:'P1',keyword:'研发项目'}}]).mixed,false) }) test('project header changes are atomic, preserve unrelated fields, and never partially update locked history', () => { const rows=[{projectId:1,quantity:3,finalUnitPrice:12},{projectId:1,quantity:5,finalUnitPrice:10}] assert.throws(()=>purchaseBatchProjectChanges(rows,2,row=>row.quantity!==5),/不能统一更换项目/) assert.equal(rows[0].projectId,1);assert.equal(rows[1].projectId,1) assert.equal(purchaseBatchProjectChanges(rows,2).length,2) assert.equal(purchaseBatchProjectChanges(rows,1).length,0) }) test('multiple real batches are never mistaken for one common-project confirmation', () => { assert.equal(purchaseDraftBatchCount([{purchaseBatchId:1},{purchaseBatchId:'1'}]),1) assert.equal(purchaseDraftBatchCount([{purchaseBatchId:1},{purchaseBatchId:2}]),2) assert.equal(purchaseDraftBatchCount([{batchName:'第一批',supplierId:1},{batchName:'第二批',supplierId:1}]),2) }) test('AI common project edits use native safe bulk updates and preserve material, quantity, quotation and source rows', () => { const rows=[{_rowId:'1',projectId:null,materialName:'电机',quantity:2,finalUnitPrice:12,_sourceRow:7},{_rowId:'2',projectId:null,materialName:'支架',quantity:3,finalUnitPrice:0,_sourceRow:8}] const field={key:'projectId',ref:'project',type:'reference',batch:true},draft={status:'WAIT_CONFIRM'} const changes=assistantBulkChanges(rows,field,{value:17,label:'研发项目'},{action:'purchase.create',draft,overwrite:true}) const result=assistantApplyDraftChanges(rows,{changes},{action:'purchase.create',fields:[field],draft}) assert.equal(result.length,2);for(let i=0;i<2;i++){assert.equal(result[i].projectId,17);for(const key of ['materialName','quantity','finalUnitPrice','_sourceRow'])assert.equal(result[i][key],rows[i][key])} assert.equal(rows[0].projectId,null) }) test('both procurement screens expose a common project while retaining mixed-history detail and input navigation', async () => { const native=fs.readFileSync(new URL('../../src/components/PurchaseSheet/index.vue',import.meta.url),'utf8') assert.match(native,/批次所属项目/);assert.match(native,/历史多项目批次/);assert.match(native,/this\.batchProject\.mixed/);assert.match(native,/row\.projectId=this\.batchProject\.value/) assert.match(native,/createPurchaseDraft\(this,unbatched\)/) const vm={projectId:17,commonProject:99,sheet:null,drafts:[],references:async()=>{},resetRowView(){},draftPrefix(){return 'test:'},parseTime(){return '2026-10-17'},emptyRow(){return {projectId:this.commonProject}},layout(){}} await createPurchaseDraft(vm) assert.equal(vm.commonProject,17) assert.equal(vm.sheet.rows.length,5) assert.ok(vm.sheet.rows.every(row=>row.projectId===17),'Every new row inherits the common project') assert.match(native,/更正类别将在保存时同步修改物品档案/,'Category side effects remain discoverable in the column hover hint') const ai=fs.readFileSync(new URL('../../src/components/BusinessAssistant/AssistantDraft.vue',import.meta.url),'utf8') assert.match(ai,/assistant-purchase-project/);assert.match(ai,/purchaseDraftBatchCount\(props\.rows\) <= 1/);assert.match(ai,/!sharedProject\.value \|\| field\.key !== 'projectId'/) }) test('AI common project never partially changes a batch containing executed rows', () => { const rows=[{_rowId:'done',projectId:17},{_rowId:'pending',projectId:17}] const field={key:'projectId',ref:'project',batch:true},draft={status:'PARTIAL',results:[{rowId:'done',status:'SUCCEEDED'}]} assert.throws(()=>assistantPurchaseProjectChanges(rows,field,{value:18},{action:'purchase.create',draft}),/不能统一更换项目/) assert.deepEqual(rows.map(row=>row.projectId),[17,17]) assert.deepEqual(assistantPurchaseProjectChanges(rows,field,{value:17},{action:'purchase.create',draft}),[]) rows[1].projectId=null assert.deepEqual(assistantPurchaseProjectChanges(rows,field,{value:17},{action:'purchase.create',draft}).map(change=>change.rowId),['pending']) }) test('AI common project ignores unresolved selections and terminal or readonly drafts', () => { const rows=[{_rowId:'1',projectId:null}],field={key:'projectId',ref:'project',batch:true} for(const value of [null,'',{keyword:'研发项目'},'未匹配项目'])assert.deepEqual(assistantPurchaseProjectChanges(rows,field,{value},{action:'purchase.create',draft:{status:'WAIT_CONFIRM'}}),[]) for(const draft of [{status:'SUCCEEDED'},{status:'CANCELLED'},{status:'WAIT_CONFIRM',readOnly:true},{status:'WAIT_CONFIRM',permissionDenied:true}])assert.deepEqual(assistantPurchaseProjectChanges(rows,field,{value:17},{action:'purchase.create',draft}),[]) })