import test from 'node:test' import assert from 'node:assert/strict' import fs from 'node:fs' import {purchaseDocumentKinds,purchaseAttachmentError,purchaseDocumentPath,purchaseDocumentId,purchaseUploadId,purchaseNumberDirty,mergePurchaseDocuments} from '../../src/utils/purchase-documents.js' test('six optional batch groups and business numbers are presented in business order',()=>{ assert.deepEqual(purchaseDocumentKinds.map(g=>g.kind),['REQUEST','QUOTE','CONTRACT','ACCEPTANCE','PAYMENT','OTHER']) assert.equal(purchaseDocumentKinds[0].numberLabel,'随行编号') assert.equal(purchaseDocumentKinds[2].numberLabel,'合同编号') }) test('workflow groups accept PDF and other/detail attachments allow ordinary office formats',()=>{ assert.equal(purchaseAttachmentError({name:'合同.pdf',size:1024},'CONTRACT'),'') assert.match(purchaseAttachmentError({name:'合同.png',size:1024},'CONTRACT'),/PDF/) for(const ext of ['pdf','png','jpg','jpeg','doc','docx','xls','xlsx','txt','zip']) assert.equal(purchaseAttachmentError({name:`备注.${ext}`,size:1024},'OTHER'),'') for(const ext of ['html','exe','svg','js'])assert.match(purchaseAttachmentError({name:`危险.${ext}`,size:1024},'OTHER'),/支持/) for(const size of [0,-1,NaN,Infinity,10485761])assert.match(purchaseAttachmentError({name:'a.pdf',size},'OTHER'),/10 MB/) }) test('private URLs cannot be built from unsaved ids, traversal, or another target kind',()=>{ assert.equal(purchaseDocumentPath('batch',12),'/warehouse/purchase/documents/batch/12') assert.equal(purchaseDocumentPath('detail',12),'/warehouse/purchase/documents/detail/12') for(const id of [null,undefined,0,-1,'../12','http://elsewhere',1.5,'1e3'])assert.throws(()=>purchaseDocumentPath('batch',id)) assert.throws(()=>purchaseDocumentPath('unknown',1)) for(const id of ['../secret','abc','https://other.test/1'])assert.throws(()=>purchaseDocumentId(id)) }) test('UUID generation works on production HTTP origins without crypto.randomUUID',()=>{ const id=purchaseUploadId({getRandomValues: bytes=>{bytes.fill(21);return bytes}}) assert.match(id,/^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/) assert.equal(purchaseDocumentId(id),id) assert.throws(()=>purchaseUploadId({})) }) test('attachment reload preserves unsaved numbers and their version and reports a concurrent edit',()=>{ const old=[{kind:'REQUEST',number:'OLD',draft:'MY-NEW',version:2,files:[]}] const fresh=[{kind:'REQUEST',number:'OTHER-EDITOR',version:3,files:[{id:'a'}]}] const merged=mergePurchaseDocuments(fresh,old) assert.equal(merged[0].draft,'MY-NEW');assert.equal(merged[0].version,2);assert.equal(merged[0].conflict,true) assert.equal(merged[0].serverNumber,'OTHER-EDITOR');assert.equal(merged[0].serverVersion,3);assert.equal(merged[0].files.length,1) assert.equal(purchaseNumberDirty(merged[0]),true) assert.equal(mergePurchaseDocuments(fresh)[0].draft,'OTHER-EDITOR') assert.equal(purchaseNumberDirty(undefined),false) }) test('purchase sheet exposes batch buttons and saved-line attachment entry without gating business actions',()=>{ const sheet=fs.readFileSync(new URL('../../src/components/PurchaseSheet/index.vue',import.meta.url),'utf8') assert.match(sheet,/v-for="document in documentKinds"/) assert.match(sheet,/documents\.open\('detail',s\.row\.id/) assert.match(sheet,/保存批次后可维护附件及编号/) const drawer=fs.readFileSync(new URL('../../src/components/PurchaseDocuments/index.vue',import.meta.url),'utf8') assert.doesNotMatch(drawer,/