waterDeviceExport.test.mjs 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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 getWaterDeviceById = \(id\) => get\(`\/EquipmentBase\/getById\/\$\{id\}`\)/u)
  25. assert.match(apiSource, /export const updateWaterDevice = \(data\) => request\(\{ url: '\/EquipmentBase\/updateById', method: 'put', data \}/u)
  26. assert.match(apiSource, /export const importDeviceLedger = \(file, updateSupport = false\)/u)
  27. assert.match(apiSource, /url: '\/waterSupply\/device\/ledger\/import'/u)
  28. assert.match(pageSource, /importDeviceLedger/u)
  29. assert.match(pageSource, /供水设备批量导入/u)
  30. assert.match(pageSource, /importUpdateSupport/u)
  31. assert.match(pageSource, /openEdit\(row\)/u)
  32. assert.match(pageSource, /updateWaterDevice\(editForm\.value\)/u)
  33. assert.match(pageSource, /row\.equipmentImage/u)
  34. assert.match(pageSource, /detail\.equipmentImage/u)
  35. })
  36. test('recognizes JSON export errors when the MIME includes a charset', async () => {
  37. const message = await readMonitoringExportError(
  38. new Blob(['{"msg":"导出失败"}'], { type: 'application/json;charset=UTF-8' })
  39. )
  40. assert.equal(message, '导出失败')
  41. })
  42. test('recognizes JSON export errors returned through an HTTP error response', async () => {
  43. const message = await readMonitoringExportError({
  44. type: 'application/json;charset=UTF-8',
  45. text: async () => '{"message":"没有导出权限"}'
  46. })
  47. assert.equal(message, '没有导出权限')
  48. })