waterDeviceExport.test.mjs 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. import assert from 'node:assert/strict'
  2. import { readFile } from 'node:fs/promises'
  3. import test from 'node:test'
  4. import { readMonitoringExportError } from '../src/utils/waterSupplyModel.js'
  5. test('water device exports download Excel blobs and surface backend JSON errors', async () => {
  6. const apiSource = await readFile('src/api/pipeNetwork/waterSupply.js', 'utf8')
  7. const pageSource = await readFile(
  8. 'src/views/subSystem/waterSupply/components/WaterDevicePage.vue',
  9. 'utf8'
  10. )
  11. const ledgerExport = apiSource.match(/export const exportDeviceLedger = .*$/mu)?.[0] || ''
  12. const abnormalExport = apiSource.match(/export const exportAbnormalDevices = .*$/mu)?.[0] || ''
  13. assert.match(ledgerExport, /responseType: 'blob'/u)
  14. assert.match(abnormalExport, /responseType: 'blob'/u)
  15. assert.match(pageSource, /readMonitoringExportError\(response\)/u)
  16. assert.match(pageSource, /saveAs\(blob/u)
  17. })
  18. test('water device ledger import and image display use the public contract', async () => {
  19. const apiSource = await readFile('src/api/pipeNetwork/waterSupply.js', 'utf8')
  20. const pageSource = await readFile(
  21. 'src/views/subSystem/waterSupply/components/WaterDevicePage.vue',
  22. 'utf8'
  23. )
  24. assert.match(apiSource, /export const importDeviceLedger = \(file, updateSupport = false\)/u)
  25. assert.match(apiSource, /url: '\/waterSupply\/device\/ledger\/import'/u)
  26. assert.match(pageSource, /importDeviceLedger/u)
  27. assert.match(pageSource, /供水设备批量导入/u)
  28. assert.match(pageSource, /importUpdateSupport/u)
  29. assert.match(pageSource, /row\.equipmentImage/u)
  30. assert.match(pageSource, /detail\.equipmentImage/u)
  31. })
  32. test('recognizes JSON export errors when the MIME includes a charset', async () => {
  33. const message = await readMonitoringExportError(
  34. new Blob(['{"msg":"导出失败"}'], { type: 'application/json;charset=UTF-8' })
  35. )
  36. assert.equal(message, '导出失败')
  37. })
  38. test('recognizes JSON export errors returned through an HTTP error response', async () => {
  39. const message = await readMonitoringExportError({
  40. type: 'application/json;charset=UTF-8',
  41. text: async () => '{"message":"没有导出权限"}'
  42. })
  43. assert.equal(message, '没有导出权限')
  44. })