import test from 'node:test' import assert from 'node:assert/strict' import { createRowIdentityEditor, pasteRectangle } from '../../src/utils/purchase-sheet.js' const item = { id: 9, productCode: 'WP9', productName: '电机', brand: 'A', spec: 'M4', unit: '个', category: '机械件' } function fixture(items = []) { const pending = [], rows = new Set() const editor = createRowIdentityEditor({ getItems: () => items, loadName: name => new Promise(resolve => pending.push({ name, resolve })), isCurrentRow: row => rows.has(row), onDetach: row => { row.loanId = null } }) return { editor, pending, rows, items } } function draft() { return { materialName: '新物品', materialParams: '已填型号', materialBrand: '', quantity: 3, unit: '件', category: '设备', finalUnitPrice: '12.345678', purchaseLink: 'https://example.com/item', projectId: 1, expenseCategoryId: 2, remark: '保留说明' } } test('entering or clearing a new item brand preserves the specification, quote and all other draft fields', () => { const { editor } = fixture(), row = draft(), before = { ...row } row.materialBrand = '新品牌' editor.brandChanged(row) assert.deepEqual(row, { ...before, materialBrand: '新品牌', productId: null, productCode: null, loanId: null }) row.materialBrand = '' editor.brandChanged(row) assert.equal(row.materialParams, before.materialParams) assert.equal(row.finalUnitPrice, before.finalUnitPrice) editor.specChanged(row, '新型号') assert.equal(row.materialBrand, '') assert.equal(row.purchaseLink, before.purchaseLink) assert.equal(row.unit, before.unit) assert.equal(row.category, before.category) }) test('free text only links a complete exact identity; a sole existing variant cannot replace a typed model', () => { const { editor } = fixture([item]), row = { ...draft(), materialName: '电机', materialBrand: 'A' } editor.brandChanged(row) assert.equal(row.productId, null) assert.equal(row.materialParams, '已填型号') editor.specChanged(row, 'M4') assert.equal(row.productId, null) assert.equal(row.finalUnitPrice, '12.345678') assert.equal(row.unit, '件') assert.equal(row.category, '设备') row.unit = '个' row.category = '机械件' editor.brandChanged(row) assert.equal(row.productId, item.id) }) test('an explicit archive option replaces archive fields while keeping the entered quote and purchase details', async () => { const { editor } = fixture([item]), row = draft() await editor.nameChanged(row, '@item:9') assert.equal(row.productId, 9) assert.equal(row.materialParams, 'M4') assert.equal(row.unit, '个') assert.equal(row.finalUnitPrice, '12.345678') assert.equal(row.projectId, 1) assert.equal(row.purchaseLink, 'https://example.com/item') assert.equal(row.quantity, 3) }) test('a delayed name match cannot overwrite a brand, specification or quote entered while loading', async () => { const f = fixture(), row = draft() f.rows.add(row) const response = f.editor.nameChanged(row, '电机') row.materialBrand = '自定义品牌' f.editor.brandChanged(row) f.editor.specChanged(row, '自定义型号') row.finalUnitPrice = 0 f.items.push(item) f.pending[0].resolve() await response assert.equal(row.materialBrand, '自定义品牌') assert.equal(row.materialParams, '自定义型号') assert.equal(row.finalUnitPrice, 0) assert.equal(row.productId, null) }) test('out of order A to B to A name responses and a removed row never apply stale matches', async () => { const f = fixture(), row = { materialBrand: 'A', materialParams: 'M4' } f.rows.add(row) const first = f.editor.nameChanged(row, '电机') const second = f.editor.nameChanged(row, '控制器') const latest = f.editor.nameChanged(row, '电机') f.items.push(item) f.pending[0].resolve() await first assert.equal(row.productId, null) f.pending[1].resolve() await second assert.equal(row.productId, null) f.rows.delete(row) f.pending[2].resolve() await latest assert.equal(row.productId, null) }) test('an unchanged delayed exact match fills only missing archive metadata and preserves zero quote', async () => { const f = fixture(), row = { materialBrand: 'A', materialParams: 'M4', finalUnitPrice: 0, quantity: 2 } f.rows.add(row) const result = f.editor.nameChanged(row, '电机') f.items.push(item) f.pending[0].resolve() await result assert.equal(row.productId, 9) assert.equal(row.unit, '个') assert.equal(row.category, '机械件') assert.equal(row.finalUnitPrice, 0) assert.equal(row.quantity, 2) }) test('pasting an identity into a draft preserves its quote and purchase link', () => { const row = { ...draft(), _key: 'one' } const result = pasteRectangle([row], '品牌A\t型号B', 0, 0, ['materialBrand', 'materialParams'], { readonly: () => false, items: [], units: ['件'] }) assert.equal(result[0].materialParams, '型号B') assert.equal(result[0].finalUnitPrice, row.finalUnitPrice) assert.equal(result[0].purchaseLink, row.purchaseLink) assert.equal(row.materialBrand, '') })