confidence-badge.test.tsx 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /**
  2. * @vitest-environment happy-dom
  3. */
  4. import { describe, expect, test } from "vitest";
  5. // Test the pure functions extracted from ConfidenceBadge component
  6. // These determine confidence levels based on request counts
  7. describe("ConfidenceBadge - getConfidenceLevel logic", () => {
  8. type ConfidenceLevel = "low" | "medium" | "high";
  9. function getConfidenceLevel(
  10. count: number,
  11. thresholds: { low: number; medium: number; high: number }
  12. ): ConfidenceLevel {
  13. if (count >= thresholds.high) return "high";
  14. if (count >= thresholds.medium) return "medium";
  15. return "low";
  16. }
  17. describe("with default thresholds (low: 10, medium: 50, high: 200)", () => {
  18. const thresholds = { low: 10, medium: 50, high: 200 };
  19. test("should return low confidence for counts < medium threshold", () => {
  20. expect(getConfidenceLevel(0, thresholds)).toBe("low");
  21. expect(getConfidenceLevel(5, thresholds)).toBe("low");
  22. expect(getConfidenceLevel(10, thresholds)).toBe("low");
  23. expect(getConfidenceLevel(49, thresholds)).toBe("low");
  24. });
  25. test("should return medium confidence for counts >= medium and < high", () => {
  26. expect(getConfidenceLevel(50, thresholds)).toBe("medium");
  27. expect(getConfidenceLevel(100, thresholds)).toBe("medium");
  28. expect(getConfidenceLevel(199, thresholds)).toBe("medium");
  29. });
  30. test("should return high confidence for counts >= high threshold", () => {
  31. expect(getConfidenceLevel(200, thresholds)).toBe("high");
  32. expect(getConfidenceLevel(500, thresholds)).toBe("high");
  33. expect(getConfidenceLevel(1000, thresholds)).toBe("high");
  34. });
  35. });
  36. describe("with custom thresholds", () => {
  37. const customThresholds = { low: 5, medium: 20, high: 100 };
  38. test("should respect custom thresholds", () => {
  39. expect(getConfidenceLevel(4, customThresholds)).toBe("low");
  40. expect(getConfidenceLevel(5, customThresholds)).toBe("low");
  41. expect(getConfidenceLevel(19, customThresholds)).toBe("low");
  42. expect(getConfidenceLevel(20, customThresholds)).toBe("medium");
  43. expect(getConfidenceLevel(99, customThresholds)).toBe("medium");
  44. expect(getConfidenceLevel(100, customThresholds)).toBe("high");
  45. });
  46. });
  47. describe("edge cases", () => {
  48. const thresholds = { low: 10, medium: 50, high: 200 };
  49. test("should handle zero requests", () => {
  50. expect(getConfidenceLevel(0, thresholds)).toBe("low");
  51. });
  52. test("should handle negative values (treat as low)", () => {
  53. expect(getConfidenceLevel(-1, thresholds)).toBe("low");
  54. expect(getConfidenceLevel(-100, thresholds)).toBe("low");
  55. });
  56. test("should handle very large values", () => {
  57. expect(getConfidenceLevel(1000000, thresholds)).toBe("high");
  58. });
  59. test("should handle exact threshold boundaries", () => {
  60. // At exactly medium threshold
  61. expect(getConfidenceLevel(50, thresholds)).toBe("medium");
  62. // At exactly high threshold
  63. expect(getConfidenceLevel(200, thresholds)).toBe("high");
  64. });
  65. });
  66. });
  67. describe("ConfidenceBadge - visual configuration", () => {
  68. const confidenceConfig = {
  69. low: {
  70. bars: 1,
  71. color: "bg-slate-400",
  72. bgColor: "bg-slate-400/10",
  73. borderStyle: "border-dashed border-slate-400/50",
  74. },
  75. medium: {
  76. bars: 2,
  77. color: "bg-amber-500",
  78. bgColor: "bg-amber-500/10",
  79. borderStyle: "border-solid border-amber-500/50",
  80. },
  81. high: {
  82. bars: 3,
  83. color: "bg-emerald-500",
  84. bgColor: "bg-emerald-500/10",
  85. borderStyle: "border-solid border-emerald-500/50",
  86. },
  87. };
  88. test("low confidence should show 1 bar with dashed border", () => {
  89. expect(confidenceConfig.low.bars).toBe(1);
  90. expect(confidenceConfig.low.borderStyle).toContain("border-dashed");
  91. });
  92. test("medium confidence should show 2 bars with solid border", () => {
  93. expect(confidenceConfig.medium.bars).toBe(2);
  94. expect(confidenceConfig.medium.borderStyle).toContain("border-solid");
  95. });
  96. test("high confidence should show 3 bars with solid border", () => {
  97. expect(confidenceConfig.high.bars).toBe(3);
  98. expect(confidenceConfig.high.borderStyle).toContain("border-solid");
  99. });
  100. test("each level should have distinct colors", () => {
  101. expect(confidenceConfig.low.color).not.toBe(confidenceConfig.medium.color);
  102. expect(confidenceConfig.medium.color).not.toBe(confidenceConfig.high.color);
  103. expect(confidenceConfig.low.color).not.toBe(confidenceConfig.high.color);
  104. });
  105. test("bar heights should increase progressively", () => {
  106. // Bar heights are calculated as bar * 4px
  107. const lowMaxHeight = confidenceConfig.low.bars * 4;
  108. const mediumMaxHeight = confidenceConfig.medium.bars * 4;
  109. const highMaxHeight = confidenceConfig.high.bars * 4;
  110. expect(lowMaxHeight).toBe(4);
  111. expect(mediumMaxHeight).toBe(8);
  112. expect(highMaxHeight).toBe(12);
  113. });
  114. });