import assert from 'node:assert/strict' import test from 'node:test' import { readFileSync } from 'node:fs' import * as persistence from '../src/views/m/pl/plan/mobilePlanPersistence' import { buildStopProductPayload, syncDeliveriesProductsFromLoad, createEmptyDeliveryForm, mergePlanProductsFromLoadMirror } from '../src/utils/pl-plan-stop-form' const product = { id: 'load-product', planProductId: 'plan-product', productId: 'sku', productNo: 'SKU', productName: '产品', unit: '吨', planQty: 10 } test('editing a stop submits its existing product identity', () => { assert.equal(buildStopProductPayload(product).id, 'load-product') }) test('copying load products into a new delivery never copies the load identity', () => { const result = syncDeliveriesProductsFromLoad([createEmptyDeliveryForm(1)], [product]) assert.equal(result.deliveries[0].products[0].id, undefined) }) test('refreshing delivery products retains that delivery identity', () => { const delivery = createEmptyDeliveryForm(1) delivery.products = [{ ...product, id: 'delivery-product' }] const result = syncDeliveriesProductsFromLoad([delivery], [product]) assert.equal(result.deliveries[0].products[0].id, 'delivery-product') }) test('desktop mirror consumes previous product identities once for repeated SKUs', () => { const before = [ { ...product, id: 'first', quantity: 1 }, { ...product, id: 'second', quantity: 1 } ] const result = mergePlanProductsFromLoadMirror( [{ ...product, id: undefined }, { ...product, id: undefined }] as any, before as any ) assert.deepEqual(result.map((row) => row.id), ['first', 'second']) }) test('desktop splits a historical collapsed identity across different packages', () => { const first = { ...product, id: 'pp', packageSpec: '桶' } const second = { ...product, id: 'pp', packageSpec: '袋' } const result = mergePlanProductsFromLoadMirror([first, second] as any, [first] as any) assert.deepEqual(result.map((row) => row.id), ['pp', undefined]) const links = persistence.buildDesktopProductLinks([first, second], [], result) assert.notEqual(links!.load[0].planProductClientKey, links!.load[1].planProductClientKey) assert.equal(links!.load[1].planProductId, undefined) }) test('normal edit payload does not reset archived actual quantity', () => { const source = readFileSync(new URL('../src/components/business/pl-plan-form-dialog/index.vue', import.meta.url), 'utf8') const start = source.indexOf('function mapProductsToSubmitPayload()') const end = source.indexOf('\n function ', start + 10) const build = new Function('formData', 'prepareLineAliasAndBrandForSubmit', `${source.slice(start, end)}; return mapProductsToSubmitPayload()`) const rows = build({ products: [{ ...product, id: 'pp', actualQty: 8 }] }, () => {}) assert.equal(rows[0].actualQty, undefined) }) test('desktop new repeated SKU rows use request keys without opting into mobile metadata', () => { const loads = [{ ...product, id: undefined, planProductId: undefined, packageSpec: 'a' }, { ...product, id: undefined, planProductId: undefined, packageSpec: 'b' }] const links = persistence.buildDesktopProductLinks(loads, [{ products: [loads[1]] }], loads as any) assert.equal(links!.deliveries[0][0].planProductClientKey, links!.products[1].clientKey) assert.equal('mobileProductMeta' in links!.load[0], false) })