alarmReviewModel.test.mjs 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. import { test } from 'node:test'
  2. import assert from 'node:assert/strict'
  3. import { valueText, thresholdText, alarmDescription, imageAttachments, hasActualValue } from './alarmReviewModel.js'
  4. test('zero is a value, not an empty threshold', () => {
  5. assert.equal(valueText(0,'MPa'),'0 MPa')
  6. assert.equal(thresholdText({minValue:0,maxValue:10,warningUnit:'MPa'}),'0 ~ 10 MPa')
  7. assert.equal(thresholdText({maxValue:0}),'上限 0')
  8. assert.equal(valueText(null),'—')
  9. })
  10. test('snapshot visibility hides empty actual values but preserves numeric zero', () => {
  11. for (const actualValue of [null, undefined, '']) assert.equal(hasActualValue({ actualValue, maxValue: 0 }), false)
  12. assert.equal(hasActualValue({}), false)
  13. assert.equal(hasActualValue({ actualValue: 0 }), true)
  14. assert.equal(hasActualValue({ actualValue: '0' }), true)
  15. assert.equal(hasActualValue({ actualValue: 336 }), true)
  16. })
  17. test('single-sided limits and equality follow collection semantics', () => {
  18. assert.equal(thresholdText({minValue:1,warningUnit:'m'}),'下限 1 m')
  19. assert.equal(alarmDescription({warningType:'压力',actualValue:1,minValue:1}),'压力达到或低于下限')
  20. assert.equal(alarmDescription({warningType:'压力',actualValue:10,maxValue:10}),'压力达到或超过上限')
  21. assert.equal(alarmDescription({warningCode:'WARN',actualValue:null}),'WARN报警')
  22. })
  23. test('attachments reject executable or protocol-relative links', () => {
  24. assert.deepEqual(imageAttachments('javascript:alert(1),//evil.test/a,data:text/html;base64,eA=='),[])
  25. assert.deepEqual(imageAttachments('/profile/a.png','/dev-api'),[{url:'/dev-api/profile/a.png',name:'a.png'}])
  26. assert.equal(imageAttachments('https://files.example/a.png')[0].url,'https://files.example/a.png')
  27. })
  28. test('image preview groups preserve image order and exclude PDF documents', () => {
  29. const images = imageAttachments('/profile/first.jpeg,/profile/report.pdf,/profile/second.PNG?version=1', '/dev-api')
  30. assert.deepEqual(images.map(file => file.url), ['/dev-api/profile/first.jpeg', '/dev-api/profile/second.PNG?version=1'])
  31. })
  32. test('image grouping handles absolute URLs, fragments and invalid attachments', () => {
  33. const images = imageAttachments('https://files.example/a.JPG?version=2#view,/profile/b.png#image,javascript:bad,//evil.test/a.jpg', '/dev-api')
  34. assert.equal(images.length, 2)
  35. assert.equal(images[0].url, 'https://files.example/a.JPG?version=2#view')
  36. assert.equal(images[1].name, 'b.png')
  37. assert.deepEqual(imageAttachments(null), [])
  38. assert.deepEqual(imageAttachments('/profile/report.pdf'), [])
  39. })