pl-child-identity.test.ts 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. import assert from 'node:assert/strict'
  2. import test from 'node:test'
  3. import { readFileSync } from 'node:fs'
  4. import * as persistence from '../src/views/m/pl/plan/mobilePlanPersistence'
  5. import {
  6. buildStopProductPayload,
  7. syncDeliveriesProductsFromLoad,
  8. createEmptyDeliveryForm,
  9. mergePlanProductsFromLoadMirror
  10. } from '../src/utils/pl-plan-stop-form'
  11. const product = {
  12. id: 'load-product',
  13. planProductId: 'plan-product',
  14. productId: 'sku',
  15. productNo: 'SKU',
  16. productName: '产品',
  17. unit: '吨',
  18. planQty: 10
  19. }
  20. test('editing a stop submits its existing product identity', () => {
  21. assert.equal(buildStopProductPayload(product).id, 'load-product')
  22. })
  23. test('copying load products into a new delivery never copies the load identity', () => {
  24. const result = syncDeliveriesProductsFromLoad([createEmptyDeliveryForm(1)], [product])
  25. assert.equal(result.deliveries[0].products[0].id, undefined)
  26. })
  27. test('refreshing delivery products retains that delivery identity', () => {
  28. const delivery = createEmptyDeliveryForm(1)
  29. delivery.products = [{ ...product, id: 'delivery-product' }]
  30. const result = syncDeliveriesProductsFromLoad([delivery], [product])
  31. assert.equal(result.deliveries[0].products[0].id, 'delivery-product')
  32. })
  33. test('desktop mirror consumes previous product identities once for repeated SKUs', () => {
  34. const before = [
  35. { ...product, id: 'first', quantity: 1 },
  36. { ...product, id: 'second', quantity: 1 }
  37. ]
  38. const result = mergePlanProductsFromLoadMirror(
  39. [{ ...product, id: undefined }, { ...product, id: undefined }] as any,
  40. before as any
  41. )
  42. assert.deepEqual(result.map((row) => row.id), ['first', 'second'])
  43. })
  44. test('desktop splits a historical collapsed identity across different packages', () => {
  45. const first = { ...product, id: 'pp', packageSpec: '桶' }
  46. const second = { ...product, id: 'pp', packageSpec: '袋' }
  47. const result = mergePlanProductsFromLoadMirror([first, second] as any, [first] as any)
  48. assert.deepEqual(result.map((row) => row.id), ['pp', undefined])
  49. const links = persistence.buildDesktopProductLinks([first, second], [], result)
  50. assert.notEqual(links!.load[0].planProductClientKey, links!.load[1].planProductClientKey)
  51. assert.equal(links!.load[1].planProductId, undefined)
  52. })
  53. test('normal edit payload does not reset archived actual quantity', () => {
  54. const source = readFileSync(new URL('../src/components/business/pl-plan-form-dialog/index.vue', import.meta.url), 'utf8')
  55. const start = source.indexOf('function mapProductsToSubmitPayload()')
  56. const end = source.indexOf('\n function ', start + 10)
  57. const build = new Function('formData', 'prepareLineAliasAndBrandForSubmit', `${source.slice(start, end)}; return mapProductsToSubmitPayload()`)
  58. const rows = build({ products: [{ ...product, id: 'pp', actualQty: 8 }] }, () => {})
  59. assert.equal(rows[0].actualQty, undefined)
  60. })
  61. test('desktop new repeated SKU rows use request keys without opting into mobile metadata', () => {
  62. const loads = [{ ...product, id: undefined, planProductId: undefined, packageSpec: 'a' }, { ...product, id: undefined, planProductId: undefined, packageSpec: 'b' }]
  63. const links = persistence.buildDesktopProductLinks(loads, [{ products: [loads[1]] }], loads as any)
  64. assert.equal(links!.deliveries[0][0].planProductClientKey, links!.products[1].clientKey)
  65. assert.equal('mobileProductMeta' in links!.load[0], false)
  66. })