indicator-compare.test.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. /**
  2. * utils/indicator-compare 多指标同图归一化纯函数单测
  3. * 覆盖:PRD §4.3 P2-4 / design §1.2.1 第二段 / design §1092
  4. */
  5. import * as ic from '@/utils/indicator-compare'
  6. describe('indicator-compare 多指标同图归一化', () => {
  7. describe('MAX_COMPARE_INDICATORS', () => {
  8. test('上限为 3', () => {
  9. expect(ic.MAX_COMPARE_INDICATORS).toBe(3)
  10. })
  11. })
  12. describe('pointNumeric', () => {
  13. test('numericValue 优先', () => {
  14. expect(ic.pointNumeric({ value: 'abc', numericValue: 2.5 })).toBe(2.5)
  15. })
  16. test('回退 value', () => {
  17. expect(ic.pointNumeric({ value: '3.62' })).toBe(3.62)
  18. })
  19. test('非数字返回 null', () => {
  20. expect(ic.pointNumeric({ value: 'N/A' })).toBe(null)
  21. })
  22. test('null/undefined 输入返回 null', () => {
  23. expect(ic.pointNumeric(null)).toBe(null)
  24. expect(ic.pointNumeric(undefined)).toBe(null)
  25. expect(ic.pointNumeric({})).toBe(null)
  26. })
  27. test('numericValue 为 0 正确返回 0', () => {
  28. expect(ic.pointNumeric({ value: '0', numericValue: 0 })).toBe(0)
  29. })
  30. })
  31. describe('normalizeValue', () => {
  32. test('正常线性映射', () => {
  33. expect(ic.normalizeValue(5, 0, 10)).toBeCloseTo(0.5)
  34. expect(ic.normalizeValue(0, 0, 10)).toBeCloseTo(0)
  35. expect(ic.normalizeValue(10, 0, 10)).toBeCloseTo(1)
  36. })
  37. test('min === max 恒定值 → 0.5', () => {
  38. expect(ic.normalizeValue(3.62, 3.62, 3.62)).toBe(0.5)
  39. })
  40. test('越界值夹到 [0,1]', () => {
  41. expect(ic.normalizeValue(-1, 0, 10)).toBe(0)
  42. expect(ic.normalizeValue(15, 0, 10)).toBe(1)
  43. })
  44. test('null/min/max 安全返回 null', () => {
  45. expect(ic.normalizeValue(null, 0, 10)).toBe(null)
  46. expect(ic.normalizeValue(5, null, 10)).toBe(null)
  47. expect(ic.normalizeValue(5, 0, null)).toBe(null)
  48. })
  49. test('NaN 输入返回 null', () => {
  50. expect(ic.normalizeValue(NaN, 0, 10)).toBe(null)
  51. })
  52. })
  53. describe('computeRealRange', () => {
  54. test('空 points → count=0', () => {
  55. var r = ic.computeRealRange({ points: [] })
  56. expect(r.count).toBe(0)
  57. expect(r.min).toBe(null)
  58. expect(r.max).toBe(null)
  59. })
  60. test('undefined item → count=0', () => {
  61. expect(ic.computeRealRange(null).count).toBe(0)
  62. expect(ic.computeRealRange(undefined).count).toBe(0)
  63. })
  64. test('单点 → count=1,无波动', () => {
  65. var r = ic.computeRealRange({ points: [{ value: '3.62', numericValue: 3.62 }] })
  66. expect(r.count).toBe(1)
  67. expect(r.min).toBeCloseTo(3.62)
  68. expect(r.max).toBeCloseTo(3.62)
  69. expect(r.hasVariance).toBe(false)
  70. })
  71. test('恒定值 ≥2 点 → count≥2 但 hasVariance=false', () => {
  72. var r = ic.computeRealRange({
  73. points: [{ value: '3.62', numericValue: 3.62 }, { value: '3.62', numericValue: 3.62 }]
  74. })
  75. expect(r.count).toBe(2)
  76. expect(r.min).toBeCloseTo(3.62)
  77. expect(r.max).toBeCloseTo(3.62)
  78. expect(r.hasVariance).toBe(false)
  79. })
  80. test('正常变化 → hasVariance=true', () => {
  81. var r = ic.computeRealRange({
  82. points: [
  83. { value: '3.62', numericValue: 3.62 },
  84. { value: '4.10', numericValue: 4.10 }
  85. ]
  86. })
  87. expect(r.count).toBe(2)
  88. expect(r.min).toBeCloseTo(3.62)
  89. expect(r.max).toBeCloseTo(4.10)
  90. expect(r.hasVariance).toBe(true)
  91. })
  92. test('有效数值过滤 NaN / 单位不同时 numericValue 仍可用', () => {
  93. var r = ic.computeRealRange({
  94. points: [
  95. { value: '10%', numericValue: 10 },
  96. { value: 'abc' } // numericValue 缺失且 value 不可转数字
  97. ]
  98. })
  99. expect(r.count).toBe(1)
  100. expect(r.min).toBeCloseTo(10)
  101. expect(r.max).toBeCloseTo(10)
  102. })
  103. })
  104. describe('toggleCompareSelection', () => {
  105. test('第 1 个添加成功', () => {
  106. var res = ic.toggleCompareSelection([], 101)
  107. expect(res.changed).toBe(true)
  108. expect(res.rejected).toBe(false)
  109. expect(res.ids).toEqual([101])
  110. })
  111. test('添加第 2、3 个均成功', () => {
  112. var res = ic.toggleCompareSelection([101], 102)
  113. expect(res.ids).toEqual([101, 102])
  114. res = ic.toggleCompareSelection(res.ids, 103)
  115. expect(res.ids).toEqual([101, 102, 103])
  116. })
  117. test('第 4 个被拒(上限 3)', () => {
  118. var res = ic.toggleCompareSelection([101, 102, 103], 104)
  119. expect(res.changed).toBe(false)
  120. expect(res.rejected).toBe(true)
  121. expect(res.ids).toEqual([101, 102, 103])
  122. })
  123. test('取消勾选移除', () => {
  124. var res = ic.toggleCompareSelection([101, 102], 102)
  125. expect(res.ids).toEqual([101])
  126. expect(res.changed).toBe(true)
  127. })
  128. test('重复添加忽略', () => {
  129. var res = ic.toggleCompareSelection([101], 101)
  130. expect(res.ids).toEqual([])
  131. })
  132. test('传 null/undefined definitionId 不改变 ids', () => {
  133. var res = ic.toggleCompareSelection([101], null)
  134. expect(res.changed).toBe(false)
  135. expect(res.ids).toEqual([101])
  136. res = ic.toggleCompareSelection([101], undefined)
  137. expect(res.changed).toBe(false)
  138. })
  139. test('自定义 max 生效', () => {
  140. var res = ic.toggleCompareSelection([101, 102], 103, 2)
  141. expect(res.rejected).toBe(true)
  142. })
  143. })
  144. describe('buildCompareSeries', () => {
  145. var baseItem = function(defId, name, unit, pts) {
  146. return { definitionId: defId, name: name, unit: unit, points: pts }
  147. }
  148. test('无有效数值 → 不纳入 series', () => {
  149. var items = [baseItem(1, 'A', '%', [{ reportDate: '2026-01-01', value: 'N/A' }])]
  150. var s = ic.buildCompareSeries(items)
  151. expect(s.length).toBe(0)
  152. })
  153. test('constant 系列 hasVariance=false,drawLine=true(≥1 个点始终画点)', () => {
  154. var items = [baseItem(1, 'A', '%', [{ reportDate: '2026-01-01', value: '5', numericValue: 5 }])]
  155. var s = ic.buildCompareSeries(items)
  156. expect(s.length).toBe(1)
  157. expect(s[0].realMin).toBeCloseTo(5)
  158. expect(s[0].realMax).toBeCloseTo(5)
  159. expect(s[0].realCount).toBe(1)
  160. expect(s[0].hasVariance).toBe(false)
  161. // 单点:n<3 → drawLine=false(红线:n<3 不连假线)
  162. expect(s[0].drawLine).toBe(false)
  163. expect(s[0].points.length).toBe(1)
  164. expect(s[0].points[0].value).toBeCloseTo(0.5) // min==max → 0.5
  165. })
  166. test('两期(n=2)→ drawLine=false(红线),数据仍在 series 中用于展示双点', () => {
  167. var items = [baseItem(1, 'LDL-C', 'mmol/L', [
  168. { reportDate: '2026-01-01', value: '3.62', numericValue: 3.62 },
  169. { reportDate: '2026-06-01', value: '4.10', numericValue: 4.10 }
  170. ])]
  171. var s = ic.buildCompareSeries(items)
  172. expect(s.length).toBe(1)
  173. expect(s[0].drawLine).toBe(false)
  174. expect(s[0].realCount).toBe(2)
  175. })
  176. test('三期(n=3)→ drawLine=true', () => {
  177. var items = [baseItem(1, 'X', 'mmol/L', [
  178. { reportDate: '2026-01', value: '1', numericValue: 1 },
  179. { reportDate: '2026-02', value: '2', numericValue: 2 },
  180. { reportDate: '2026-03', value: '3', numericValue: 3 }
  181. ])]
  182. var s = ic.buildCompareSeries(items)
  183. expect(s[0].drawLine).toBe(true)
  184. })
  185. test('混合单位(mmol/L 与 %)各自归一化独立', () => {
  186. var items = [
  187. baseItem(1, 'A', 'mmol/L', [
  188. { reportDate: '2026-01-01', value: '3.62', numericValue: 3.62 },
  189. { reportDate: '2026-06-01', value: '4.10', numericValue: 4.10 }
  190. ]),
  191. baseItem(2, 'B', '%', [
  192. { reportDate: '2026-01-01', value: '50', numericValue: 50 },
  193. { reportDate: '2026-06-01', value: '80', numericValue: 80 }
  194. ])
  195. ]
  196. var s = ic.buildCompareSeries(items)
  197. expect(s.length).toBe(2)
  198. // A 映射 [3.62, 4.10] → [0, 1]
  199. expect(s[0].points[0].value).toBeCloseTo(0)
  200. expect(s[0].points[1].value).toBeCloseTo(1)
  201. // B 映射 [50, 80] → [0, 1]
  202. expect(s[1].points[0].value).toBeCloseTo(0)
  203. expect(s[1].points[1].value).toBeCloseTo(1)
  204. // 真实范围保留
  205. expect(s[0].realMin).toBeCloseTo(3.62)
  206. expect(s[0].realMax).toBeCloseTo(4.10)
  207. expect(s[1].realMin).toBeCloseTo(50)
  208. expect(s[1].realMax).toBeCloseTo(80)
  209. // 颜色轮转
  210. expect(s[0].color).toBe('#F97316')
  211. expect(s[1].color).toBe('#0EA5E9')
  212. })
  213. test('legendText 格式:有波动 → "name(unit) · 真实范围 min~max"', () => {
  214. var items = [baseItem(1, 'LDL-C', 'mmol/L', [
  215. { reportDate: '2026-01-01', value: '3.62', numericValue: 3.62 },
  216. { reportDate: '2026-06-01', value: '4.10', numericValue: 4.10 }
  217. ])]
  218. var s = ic.buildCompareSeries(items)
  219. expect(s[0].legendText).toMatch(/真实范围/)
  220. expect(s[0].legendText).toContain('LDL-C')
  221. })
  222. test('legendText 格式:仅 1 期 → "仅 1 期 x(无趋势)"', () => {
  223. var items = [baseItem(1, 'A', '%', [{ reportDate: '2026-01-01', value: '5', numericValue: 5 }])]
  224. var s = ic.buildCompareSeries(items)
  225. expect(s[0].legendText).toMatch(/仅 1 期/)
  226. })
  227. test('legendText 格式:无波动 → "无波动 恒定 x"', () => {
  228. var items = [baseItem(1, 'A', '%', [
  229. { reportDate: '2026-01-01', value: '5', numericValue: 5 },
  230. { reportDate: '2026-06-01', value: '5', numericValue: 5 }
  231. ])]
  232. var s = ic.buildCompareSeries(items)
  233. expect(s[0].legendText).toMatch(/无波动/)
  234. })
  235. test('无有效数值的条目跳过,不占 color 位置', () => {
  236. var items = [
  237. baseItem(1, 'A', '%', [{ reportDate: '2026-01-01', value: 'N/A' }]),
  238. baseItem(2, 'B', '%', [{ reportDate: '2026-01-01', value: '5', numericValue: 5 }])
  239. ]
  240. var s = ic.buildCompareSeries(items)
  241. expect(s.length).toBe(1)
  242. expect(s[0].color).toBe('#F97316') // 仍然用 palette[0](首个有效)
  243. })
  244. })
  245. describe('buildCompareAllDates', () => {
  246. test('空数组 → []', () => {
  247. expect(ic.buildCompareAllDates([])).toEqual([])
  248. })
  249. test('只含 points → 升序并集', () => {
  250. var items = [
  251. { points: [{ reportDate: '2026-03-01' }, { reportDate: '2026-05-01' }] },
  252. { points: [{ reportDate: '2026-01-01' }, { reportDate: '2026-05-01' }] }
  253. ]
  254. var dates = ic.buildCompareAllDates(items)
  255. expect(dates).toEqual(['2026-01-01', '2026-03-01', '2026-05-01'])
  256. })
  257. test('缺失期(missingPeriods)也并入 X 轴', () => {
  258. var items = [{ points: [{ reportDate: '2026-01-01' }, { reportDate: '2026-03-01' }], missingPeriods: [{ reportDate: '2026-02-01' }] }]
  259. var dates = ic.buildCompareAllDates(items)
  260. expect(dates).toEqual(['2026-01-01', '2026-02-01', '2026-03-01'])
  261. })
  262. test('日期截断到 yyyy-MM-dd', () => {
  263. var items = [{ points: [{ reportDate: '2026-01-15T14:30:00' }] }]
  264. var dates = ic.buildCompareAllDates(items)
  265. expect(dates).toEqual(['2026-01-15'])
  266. })
  267. })
  268. describe('buildComparePeriodRows', () => {
  269. test('缺失期 → 该期未检测', () => {
  270. var items = [
  271. { definitionId: 1, name: 'A', unit: 'mmol/L', points: [{ reportDate: '2026-01-01', value: '3.62', numericValue: 3.62 }, { reportDate: '2026-03-01', value: '4.10', numericValue: 4.10 }], missingPeriods: [{ reportDate: '2026-02-01' }] }
  272. ]
  273. var rows = ic.buildComparePeriodRows(items)
  274. expect(rows.length).toBe(3)
  275. // 按日期倒序(新→旧)
  276. expect(rows[0].dateText).toContain('03-01')
  277. expect(rows[1].dateText).toContain('02-01')
  278. expect(rows[2].dateText).toContain('01-01')
  279. // 缺失期单元格
  280. expect(rows[1].cells[0].valueText).toBe('该期未检测')
  281. expect(rows[1].cells[0].isMissing).toBe(true)
  282. })
  283. test('真实值读回带单位', () => {
  284. var items = [{ definitionId: 1, name: 'LDL-C', unit: 'mmol/L', points: [{ reportDate: '2026-01-01', value: '3.62', numericValue: 3.62 }] }]
  285. var rows = ic.buildComparePeriodRows(items)
  286. expect(rows[0].cells[0].valueText).toContain('3.62')
  287. expect(rows[0].cells[0].valueText).toContain('mmol/L')
  288. })
  289. test('多指标列对齐', () => {
  290. var items = [
  291. { definitionId: 1, name: 'A', unit: 'mmol/L', points: [{ reportDate: '2026-01-01', value: '1.0', numericValue: 1 }] },
  292. { definitionId: 2, name: 'B', unit: '%', points: [{ reportDate: '2026-01-01', value: '50', numericValue: 50 }] }
  293. ]
  294. var rows = ic.buildComparePeriodRows(items)
  295. expect(rows[0].cells.length).toBe(2)
  296. expect(rows[0].cells[0].name).toBe('A')
  297. expect(rows[0].cells[1].name).toBe('B')
  298. })
  299. })
  300. describe('buildLegendText', () => {
  301. test('count=0 → 暂无数据', () => {
  302. expect(ic.buildLegendText({ points: [] })).toBe(' · 暂无数据')
  303. })
  304. test('count=1 → 仅 1 期 无趋势', () => {
  305. var t = ic.buildLegendText({ name: 'A', unit: '%', points: [{ numericValue: 5 }] })
  306. expect(t).toContain('仅 1 期')
  307. expect(t).toContain('无趋势')
  308. })
  309. test('count≥2 且 min===max → 无波动 恒定', () => {
  310. var t = ic.buildLegendText({ name: 'A', unit: '%', points: [{ numericValue: 5 }, { numericValue: 5 }] })
  311. expect(t).toContain('无波动')
  312. expect(t).toContain('恒定')
  313. expect(t).toContain('5.00')
  314. })
  315. test('count=2 有波动 → 追加(仅 2 期,未连线)', () => {
  316. var t = ic.buildLegendText({ name: 'LDL-C', unit: 'mmol/L', points: [{ numericValue: 3.62 }, { numericValue: 4.10 }] })
  317. expect(t).toContain('仅 2 期,未连线')
  318. expect(t).toContain('真实范围')
  319. })
  320. test('count≥3 有波动 → 仅真实范围', () => {
  321. var t = ic.buildLegendText({ name: 'X', unit: 'mg/dL', points: [{ numericValue: 1 }, { numericValue: 2 }, { numericValue: 3 }] })
  322. expect(t).toContain('真实范围')
  323. expect(t).not.toContain('仅 2 期')
  324. })
  325. })
  326. })