ProjectUsage.metrics.test.ts 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. import { describe, expect, it } from 'vitest'
  2. import {
  3. computeSuccessAndNonSuccessRates,
  4. sumErrors,
  5. sumTotal,
  6. sumWarnings,
  7. toLogsBarChartData,
  8. } from './ProjectUsage.metrics'
  9. describe('ProjectUsage.metrics', () => {
  10. const rows = [
  11. { timestamp: '2025-10-22T13:00:00Z', ok_count: 90, warning_count: 5, error_count: 5 },
  12. { timestamp: '2025-10-22T13:01:00Z', ok_count: 50, warning_count: 10, error_count: 0 },
  13. ]
  14. it('toLogsBarChartData maps and coerces fields correctly', () => {
  15. const data = toLogsBarChartData(rows)
  16. expect(data).toHaveLength(2)
  17. expect(data[0]).toEqual({
  18. timestamp: '2025-10-22T13:00:00Z',
  19. ok_count: 90,
  20. warning_count: 5,
  21. error_count: 5,
  22. })
  23. })
  24. it('sum helpers compute totals correctly', () => {
  25. const data = toLogsBarChartData(rows)
  26. expect(sumTotal(data)).toBe(160)
  27. expect(sumWarnings(data)).toBe(15)
  28. expect(sumErrors(data)).toBe(5)
  29. })
  30. it('computeSuccessAndNonSuccessRates returns expected percentages', () => {
  31. const data = toLogsBarChartData(rows)
  32. const total = sumTotal(data)
  33. const warns = sumWarnings(data)
  34. const errs = sumErrors(data)
  35. const { successRate, nonSuccessRate } = computeSuccessAndNonSuccessRates(total, warns, errs)
  36. // success = 160 - (15 + 5) = 140 → 87.5%
  37. expect(successRate).toBeCloseTo(87.5)
  38. expect(nonSuccessRate).toBeCloseTo(12.5)
  39. })
  40. })