waterDeviceExport.test.mjs 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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, /deleteWaterDevices/u)
  27. assert.match(apiSource, /export const importDeviceLedger = \(file, updateSupport = false\)/u)
  28. assert.match(apiSource, /url: '\/waterSupply\/device\/ledger\/import'/u)
  29. assert.match(apiSource, /export const getDeviceLedgerStatistics = \(\) => get\('\/waterSupply\/device\/ledger\/statistics'\)/u)
  30. assert.match(pageSource, /importDeviceLedger/u)
  31. assert.match(pageSource, /供水设备批量导入/u)
  32. assert.match(pageSource, /importUpdateSupport/u)
  33. assert.match(pageSource, /openEdit\(row\)/u)
  34. assert.match(pageSource, /updateWaterDevice\(editForm\.value\)/u)
  35. assert.match(pageSource, /row\.equipmentImageUrl/u)
  36. assert.match(pageSource, /detail\.equipmentImageUrl/u)
  37. assert.match(pageSource, /openCreate/u)
  38. assert.match(pageSource, /ledgerStatsCards/u)
  39. assert.match(pageSource, /设备数量/u)
  40. assert.match(pageSource, /在线率/u)
  41. assert.match(pageSource, /removeSelectedDevices/u)
  42. assert.match(pageSource, /handleDeviceSelectionChange/u)
  43. assert.match(pageSource, /ImageUpload v-model="editForm.equipmentImage"/u)
  44. assert.match(pageSource, /facility-row-action is-detail/u)
  45. assert.match(pageSource, /facility-row-action is-edit/u)
  46. assert.match(pageSource, /type="primary" plain size="small" :icon="Plus"/u)
  47. assert.match(pageSource, /type="danger" plain size="small" :icon="Delete"/u)
  48. })
  49. test('recognizes JSON export errors when the MIME includes a charset', async () => {
  50. const message = await readMonitoringExportError(
  51. new Blob(['{"msg":"导出失败"}'], { type: 'application/json;charset=UTF-8' })
  52. )
  53. assert.equal(message, '导出失败')
  54. })
  55. test('recognizes JSON export errors returned through an HTTP error response', async () => {
  56. const message = await readMonitoringExportError({
  57. type: 'application/json;charset=UTF-8',
  58. text: async () => '{"message":"没有导出权限"}'
  59. })
  60. assert.equal(message, '没有导出权限')
  61. })