test-bx-invoice-thumb.mjs 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. import assert from 'node:assert/strict'
  2. import {
  3. isImageFileType,
  4. fileExtLabel,
  5. formatInvoiceDateYmd,
  6. buildAttachItemsFromInvoiceRows,
  7. mergeInvoicePicksByDraftKeys
  8. } from '../utils/bxInvoiceThumb.js'
  9. assert.equal(isImageFileType('image/jpeg'), true)
  10. assert.equal(isImageFileType('application/pdf'), false)
  11. assert.equal(fileExtLabel('a.PDF'), 'PDF')
  12. assert.equal(formatInvoiceDateYmd('2026-09-16T12:00:00'), '2026-09-16')
  13. assert.equal(formatInvoiceDateYmd(''), '—')
  14. const rows = [
  15. {
  16. id: 'inv1',
  17. amount: 100,
  18. createTime: '2026-09-01T10:00:00',
  19. attachments: [
  20. { id: 'a1', fileName: 'x.jpg', fileType: 'image/jpeg' },
  21. { id: 'a2', fileName: 'y.pdf', fileType: 'application/pdf' }
  22. ]
  23. }
  24. ]
  25. const items = buildAttachItemsFromInvoiceRows(rows)
  26. assert.equal(items.length, 2)
  27. assert.equal(items[0].key, 'inv1:a1')
  28. assert.equal(items[1].fileName, 'y.pdf')
  29. const prev = [
  30. {
  31. key: 'inv9:a9',
  32. invoiceId: 'inv9',
  33. attachmentId: 'a9',
  34. amount: 9,
  35. createTime: '2026-08-01',
  36. fileName: 'old.jpg',
  37. fileType: 'image/jpeg',
  38. previewUrl: 'http://old'
  39. }
  40. ]
  41. const merged = mergeInvoicePicksByDraftKeys(
  42. ['inv9:a9', 'inv1:a1'],
  43. items,
  44. prev
  45. )
  46. assert.equal(merged.length, 2)
  47. assert.ok(merged.some((p) => p.key === 'inv9:a9' && p.previewUrl === 'http://old'))
  48. assert.ok(merged.some((p) => p.key === 'inv1:a1'))
  49. console.log('bxInvoiceThumb ok')