import { test } from 'node:test' import assert from 'node:assert/strict' import { valueText, thresholdText, alarmDescription, imageAttachments, hasActualValue } from './alarmReviewModel.js' test('zero is a value, not an empty threshold', () => { assert.equal(valueText(0,'MPa'),'0 MPa') assert.equal(thresholdText({minValue:0,maxValue:10,warningUnit:'MPa'}),'0 ~ 10 MPa') assert.equal(thresholdText({maxValue:0}),'上限 0') assert.equal(valueText(null),'—') }) test('snapshot visibility hides empty actual values but preserves numeric zero', () => { for (const actualValue of [null, undefined, '']) assert.equal(hasActualValue({ actualValue, maxValue: 0 }), false) assert.equal(hasActualValue({}), false) assert.equal(hasActualValue({ actualValue: 0 }), true) assert.equal(hasActualValue({ actualValue: '0' }), true) assert.equal(hasActualValue({ actualValue: 336 }), true) }) test('single-sided limits and equality follow collection semantics', () => { assert.equal(thresholdText({minValue:1,warningUnit:'m'}),'下限 1 m') assert.equal(alarmDescription({warningType:'压力',actualValue:1,minValue:1}),'压力达到或低于下限') assert.equal(alarmDescription({warningType:'压力',actualValue:10,maxValue:10}),'压力达到或超过上限') assert.equal(alarmDescription({warningCode:'WARN',actualValue:null}),'WARN报警') }) test('attachments reject executable or protocol-relative links', () => { assert.deepEqual(imageAttachments('javascript:alert(1),//evil.test/a,data:text/html;base64,eA=='),[]) assert.deepEqual(imageAttachments('/profile/a.png','/dev-api'),[{url:'/dev-api/profile/a.png',name:'a.png'}]) assert.equal(imageAttachments('https://files.example/a.png')[0].url,'https://files.example/a.png') }) test('image preview groups preserve image order and exclude PDF documents', () => { const images = imageAttachments('/profile/first.jpeg,/profile/report.pdf,/profile/second.PNG?version=1', '/dev-api') assert.deepEqual(images.map(file => file.url), ['/dev-api/profile/first.jpeg', '/dev-api/profile/second.PNG?version=1']) }) test('image grouping handles absolute URLs, fragments and invalid attachments', () => { const images = imageAttachments('https://files.example/a.JPG?version=2#view,/profile/b.png#image,javascript:bad,//evil.test/a.jpg', '/dev-api') assert.equal(images.length, 2) assert.equal(images[0].url, 'https://files.example/a.JPG?version=2#view') assert.equal(images[1].name, 'b.png') assert.deepEqual(imageAttachments(null), []) assert.deepEqual(imageAttachments('/profile/report.pdf'), []) })