manual-dispatch-calendar.test.ts 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. import assert from 'node:assert/strict'
  2. import test from 'node:test'
  3. import {
  4. loadCalendarPages,
  5. manualCalendarDays,
  6. manualCopyText,
  7. validateManualDraft
  8. } from '../src/views/pl/dispatch-calendar/manual-model'
  9. const row = (id: string, date = '2026-10-26', vehicleId = 'v1') => ({
  10. id,
  11. dispatchDate: date,
  12. vehicleId,
  13. licensePlate: '皖H12345',
  14. productId: 'p1',
  15. productName: '液碱',
  16. origin: '山峡',
  17. destination: '云栋',
  18. quantity: '',
  19. calendarKind: 'acid' as const,
  20. remark: '',
  21. createTime: '',
  22. updateTime: ''
  23. })
  24. test('同车同日同路线保留独立车次,车辆筛选与汇总一致', () => {
  25. const days = manualCalendarDays(
  26. [row('1'), row('2'), row('3', '2026-10-26', 'v2'), row('4', '2026-11-01')],
  27. '2026-10',
  28. 'v1'
  29. )
  30. const day = days.find((d) => d.date === '2026-10-26')!
  31. assert.equal(day.rows.length, 2)
  32. assert.equal(day.vehicles.length, 1)
  33. assert.equal(day.products[0].count, 2)
  34. assert.ok(days.some((d) => d.date === '2026-10-01' && d.rows.length === 0))
  35. })
  36. test('完整分页加载,不悄悄展示截断数据', async () => {
  37. const calls: number[] = []
  38. const rows = await loadCalendarPages(async ({ page }) => {
  39. calls.push(page)
  40. return { list: [row(String(page))], total: 3 }
  41. })
  42. assert.deepEqual(calls, [1, 2, 3])
  43. assert.equal(rows?.length, 3)
  44. await assert.rejects(
  45. loadCalendarPages(async () => ({ list: [], total: 1 })),
  46. /不完整/
  47. )
  48. })
  49. test('过期请求不继续加载,不返回旧月份数据', async () => {
  50. let current = true
  51. assert.equal(
  52. await loadCalendarPages(
  53. async () => {
  54. current = false
  55. return { list: [row('1')], total: 3 }
  56. },
  57. () => current
  58. ),
  59. null
  60. )
  61. })
  62. test('复制保留重复车次,文本不混入实际运输计划', () => {
  63. const text = manualCopyText('2026-10-26', [row('1'), row('2')])
  64. assert.match(text, /2026-10-26 派车计划/)
  65. assert.equal(text.split('山峡 → 云栋').length - 1, 2)
  66. })
  67. test('同一车辆按创建时间正序,复制时排在一起且每条换行', () => {
  68. const later = {
  69. ...row('b', '2026-10-28', 'qa'),
  70. licensePlate: '皖QA1024',
  71. productName: '测试',
  72. origin: '2',
  73. destination: '3',
  74. remark: '23',
  75. createTime: '2026-10-28 10:00:00'
  76. }
  77. const earlier = {
  78. ...row('a', '2026-10-28', 'qa'),
  79. licensePlate: '皖QA1024',
  80. productName: '测试',
  81. origin: '1',
  82. destination: '2',
  83. remark: '12',
  84. createTime: '2026-10-28 09:00:00'
  85. }
  86. const other = {
  87. ...row('c', '2026-10-28', 'other'),
  88. licensePlate: '皖H00001',
  89. createTime: '2026-10-28 09:30:00'
  90. }
  91. const day = manualCalendarDays([later, other, earlier], '2026-10').find(
  92. (item) => item.date === '2026-10-28'
  93. )!
  94. assert.deepEqual(
  95. day.vehicles.find((vehicle) => vehicle.id === 'qa')!.rows.map((item) => item.id),
  96. ['a', 'b']
  97. )
  98. const text = manualCopyText('2026-10-28', [later, other, earlier])
  99. assert.deepEqual(text.split('\n'), [
  100. '2026-10-28 派车计划',
  101. ' ·皖QA1024',
  102. ' 测试 1 → 2(12)',
  103. ' 测试 2 → 3(23)',
  104. ' ·皖H00001',
  105. ' 液碱 山峡 → 云栋'
  106. ])
  107. assert.equal(text.startsWith('2026-10-28'), true)
  108. })
  109. test('表单拒绝空白、无效日期,保留手写起止地', () => {
  110. assert.equal(validateManualDraft({ ...row('1'), origin: ' ' }), '请填写始发地')
  111. assert.equal(
  112. validateManualDraft({ ...row('1'), dispatchDate: '2026-02-30' }),
  113. '请选择有效的派车日期'
  114. )
  115. assert.equal(validateManualDraft(row('1')), '')
  116. })
  117. test('数量是可空文本,复制逐条展示,日汇总仍只计车次', () => {
  118. const tons = { ...row('1'), quantity: '32.50' }
  119. const blank = { ...row('2'), quantity: ' ' }
  120. const day = manualCalendarDays([tons, blank], '2026-10').find((item) => item.date === '2026-10-26')!
  121. assert.deepEqual(day.products[0], { id: 'p1', name: '液碱', count: 2 })
  122. const text = manualCopyText('2026-10-26', [tons, blank])
  123. assert.match(text, /液碱 32\.50吨 山峡 → 云栋/)
  124. assert.equal(text.split('山峡 → 云栋').length - 1, 2)
  125. assert.equal((text.match(/吨/g) || []).length, 1)
  126. assert.equal(validateManualDraft({ ...row('1'), quantity: '' }), '')
  127. assert.equal(validateManualDraft({ ...row('1'), quantity: ' 12.5 ' }), '')
  128. assert.equal(
  129. validateManualDraft({ ...row('1'), quantity: '12.345' }),
  130. '数量须为数字,最多两位小数'
  131. )
  132. assert.equal(
  133. validateManualDraft({ ...row('1'), quantity: '1吨' }),
  134. '数量须为数字,最多两位小数'
  135. )
  136. })