availability-dashboard.test.tsx 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. /**
  2. * @vitest-environment happy-dom
  3. */
  4. import { describe, expect, test } from "vitest";
  5. // Test the time range calculation logic from AvailabilityDashboard
  6. describe("AvailabilityDashboard - time range calculations", () => {
  7. type TimeRangeOption = "15min" | "1h" | "6h" | "24h" | "7d";
  8. const TIME_RANGE_MAP: Record<TimeRangeOption, number> = {
  9. "15min": 15 * 60 * 1000,
  10. "1h": 60 * 60 * 1000,
  11. "6h": 6 * 60 * 60 * 1000,
  12. "24h": 24 * 60 * 60 * 1000,
  13. "7d": 7 * 24 * 60 * 60 * 1000,
  14. };
  15. const TARGET_BUCKETS = 60;
  16. function calculateBucketSize(timeRangeMs: number): number {
  17. const bucketSizeMs = timeRangeMs / TARGET_BUCKETS;
  18. const bucketSizeMinutes = bucketSizeMs / (60 * 1000);
  19. return Math.max(0.25, Math.round(bucketSizeMinutes * 4) / 4);
  20. }
  21. describe("TIME_RANGE_MAP values", () => {
  22. test("15min should be 15 minutes in milliseconds", () => {
  23. expect(TIME_RANGE_MAP["15min"]).toBe(15 * 60 * 1000);
  24. expect(TIME_RANGE_MAP["15min"]).toBe(900000);
  25. });
  26. test("1h should be 1 hour in milliseconds", () => {
  27. expect(TIME_RANGE_MAP["1h"]).toBe(60 * 60 * 1000);
  28. expect(TIME_RANGE_MAP["1h"]).toBe(3600000);
  29. });
  30. test("6h should be 6 hours in milliseconds", () => {
  31. expect(TIME_RANGE_MAP["6h"]).toBe(6 * 60 * 60 * 1000);
  32. expect(TIME_RANGE_MAP["6h"]).toBe(21600000);
  33. });
  34. test("24h should be 24 hours in milliseconds", () => {
  35. expect(TIME_RANGE_MAP["24h"]).toBe(24 * 60 * 60 * 1000);
  36. expect(TIME_RANGE_MAP["24h"]).toBe(86400000);
  37. });
  38. test("7d should be 7 days in milliseconds", () => {
  39. expect(TIME_RANGE_MAP["7d"]).toBe(7 * 24 * 60 * 60 * 1000);
  40. expect(TIME_RANGE_MAP["7d"]).toBe(604800000);
  41. });
  42. });
  43. describe("calculateBucketSize", () => {
  44. test("should calculate bucket size for 15min range", () => {
  45. const bucketSize = calculateBucketSize(TIME_RANGE_MAP["15min"]);
  46. // 15min / 60 buckets = 0.25 minutes per bucket
  47. expect(bucketSize).toBe(0.25);
  48. });
  49. test("should calculate bucket size for 1h range", () => {
  50. const bucketSize = calculateBucketSize(TIME_RANGE_MAP["1h"]);
  51. // 60min / 60 buckets = 1 minute per bucket
  52. expect(bucketSize).toBe(1);
  53. });
  54. test("should calculate bucket size for 6h range", () => {
  55. const bucketSize = calculateBucketSize(TIME_RANGE_MAP["6h"]);
  56. // 360min / 60 buckets = 6 minutes per bucket
  57. expect(bucketSize).toBe(6);
  58. });
  59. test("should calculate bucket size for 24h range", () => {
  60. const bucketSize = calculateBucketSize(TIME_RANGE_MAP["24h"]);
  61. // 1440min / 60 buckets = 24 minutes per bucket
  62. expect(bucketSize).toBe(24);
  63. });
  64. test("should calculate bucket size for 7d range", () => {
  65. const bucketSize = calculateBucketSize(TIME_RANGE_MAP["7d"]);
  66. // 10080min / 60 buckets = 168 minutes per bucket
  67. expect(bucketSize).toBe(168);
  68. });
  69. test("should enforce minimum bucket size of 0.25 minutes", () => {
  70. // Very small time range
  71. const bucketSize = calculateBucketSize(1000); // 1 second
  72. expect(bucketSize).toBe(0.25);
  73. });
  74. test("should round to nearest 0.25 minutes", () => {
  75. // Test rounding behavior
  76. const testCases = [
  77. { input: 60 * 60 * 1000 * 1.1, expected: 1 }, // ~1.1 min -> 1
  78. { input: 60 * 60 * 1000 * 1.3, expected: 1.25 }, // ~1.3 min -> 1.25
  79. { input: 60 * 60 * 1000 * 1.6, expected: 1.5 }, // ~1.6 min -> 1.5
  80. { input: 60 * 60 * 1000 * 1.9, expected: 2 }, // ~1.9 min -> 2
  81. ];
  82. for (const { input, expected } of testCases) {
  83. const result = calculateBucketSize(input);
  84. expect(result).toBeCloseTo(expected, 1);
  85. }
  86. });
  87. });
  88. describe("time range date calculations", () => {
  89. test("should calculate correct start time for each range", () => {
  90. const now = new Date("2024-01-15T12:00:00Z");
  91. for (const [range, ms] of Object.entries(TIME_RANGE_MAP)) {
  92. const startTime = new Date(now.getTime() - ms);
  93. const diff = now.getTime() - startTime.getTime();
  94. expect(diff).toBe(ms);
  95. }
  96. });
  97. test("15min range should go back 15 minutes", () => {
  98. const now = new Date("2024-01-15T12:00:00Z");
  99. const startTime = new Date(now.getTime() - TIME_RANGE_MAP["15min"]);
  100. expect(startTime.toISOString()).toBe("2024-01-15T11:45:00.000Z");
  101. });
  102. test("24h range should go back 24 hours", () => {
  103. const now = new Date("2024-01-15T12:00:00Z");
  104. const startTime = new Date(now.getTime() - TIME_RANGE_MAP["24h"]);
  105. expect(startTime.toISOString()).toBe("2024-01-14T12:00:00.000Z");
  106. });
  107. test("7d range should go back 7 days", () => {
  108. const now = new Date("2024-01-15T12:00:00Z");
  109. const startTime = new Date(now.getTime() - TIME_RANGE_MAP["7d"]);
  110. expect(startTime.toISOString()).toBe("2024-01-08T12:00:00.000Z");
  111. });
  112. });
  113. });
  114. describe("AvailabilityDashboard - overview metrics calculations", () => {
  115. interface TimeBucket {
  116. avgLatencyMs: number;
  117. redCount: number;
  118. totalRequests: number;
  119. }
  120. interface Provider {
  121. timeBuckets: TimeBucket[];
  122. totalRequests: number;
  123. currentStatus: "green" | "yellow" | "red" | "unknown";
  124. }
  125. function calculateAvgLatency(providers: Provider[]): number {
  126. if (providers.length === 0) return 0;
  127. const providersWithLatency = providers.filter((p) =>
  128. p.timeBuckets.some((b) => b.avgLatencyMs > 0)
  129. );
  130. if (providersWithLatency.length === 0) return 0;
  131. const totalLatency = providersWithLatency.reduce((sum, p) => {
  132. const latencies = p.timeBuckets.filter((b) => b.avgLatencyMs > 0).map((b) => b.avgLatencyMs);
  133. if (latencies.length === 0) return sum;
  134. return sum + latencies.reduce((a, b) => a + b, 0) / latencies.length;
  135. }, 0);
  136. return totalLatency / providersWithLatency.length;
  137. }
  138. function calculateErrorRate(providers: Provider[]): number {
  139. if (providers.length === 0) return 0;
  140. const totalErrorRate = providers.reduce((sum, p) => {
  141. const total = p.totalRequests;
  142. const errors = p.timeBuckets.reduce((s, b) => s + b.redCount, 0);
  143. return sum + (total > 0 ? errors / total : 0);
  144. }, 0);
  145. return totalErrorRate / providers.length;
  146. }
  147. function countByStatus(providers: Provider[], status: string): number {
  148. return providers.filter((p) => p.currentStatus === status).length;
  149. }
  150. describe("calculateAvgLatency", () => {
  151. test("should return 0 for empty providers", () => {
  152. expect(calculateAvgLatency([])).toBe(0);
  153. });
  154. test("should calculate average latency across providers", () => {
  155. const providers: Provider[] = [
  156. {
  157. timeBuckets: [{ avgLatencyMs: 100, redCount: 0, totalRequests: 10 }],
  158. totalRequests: 10,
  159. currentStatus: "green",
  160. },
  161. {
  162. timeBuckets: [{ avgLatencyMs: 200, redCount: 0, totalRequests: 10 }],
  163. totalRequests: 10,
  164. currentStatus: "green",
  165. },
  166. ];
  167. expect(calculateAvgLatency(providers)).toBe(150);
  168. });
  169. test("should ignore providers with no latency data", () => {
  170. const providers: Provider[] = [
  171. {
  172. timeBuckets: [{ avgLatencyMs: 100, redCount: 0, totalRequests: 10 }],
  173. totalRequests: 10,
  174. currentStatus: "green",
  175. },
  176. {
  177. timeBuckets: [{ avgLatencyMs: 0, redCount: 0, totalRequests: 0 }],
  178. totalRequests: 0,
  179. currentStatus: "unknown",
  180. },
  181. ];
  182. expect(calculateAvgLatency(providers)).toBe(100);
  183. });
  184. test("should average multiple buckets within a provider", () => {
  185. const providers: Provider[] = [
  186. {
  187. timeBuckets: [
  188. { avgLatencyMs: 100, redCount: 0, totalRequests: 10 },
  189. { avgLatencyMs: 200, redCount: 0, totalRequests: 10 },
  190. { avgLatencyMs: 300, redCount: 0, totalRequests: 10 },
  191. ],
  192. totalRequests: 30,
  193. currentStatus: "green",
  194. },
  195. ];
  196. expect(calculateAvgLatency(providers)).toBe(200);
  197. });
  198. });
  199. describe("calculateErrorRate", () => {
  200. test("should return 0 for empty providers", () => {
  201. expect(calculateErrorRate([])).toBe(0);
  202. });
  203. test("should calculate error rate correctly", () => {
  204. const providers: Provider[] = [
  205. {
  206. timeBuckets: [{ avgLatencyMs: 100, redCount: 10, totalRequests: 100 }],
  207. totalRequests: 100,
  208. currentStatus: "green",
  209. },
  210. ];
  211. expect(calculateErrorRate(providers)).toBe(0.1); // 10%
  212. });
  213. test("should average error rates across providers", () => {
  214. const providers: Provider[] = [
  215. {
  216. timeBuckets: [{ avgLatencyMs: 100, redCount: 10, totalRequests: 100 }],
  217. totalRequests: 100,
  218. currentStatus: "green",
  219. },
  220. {
  221. timeBuckets: [{ avgLatencyMs: 100, redCount: 20, totalRequests: 100 }],
  222. totalRequests: 100,
  223. currentStatus: "yellow",
  224. },
  225. ];
  226. expect(calculateErrorRate(providers)).toBeCloseTo(0.15, 10); // (10% + 20%) / 2
  227. });
  228. test("should handle providers with zero requests", () => {
  229. const providers: Provider[] = [
  230. {
  231. timeBuckets: [{ avgLatencyMs: 0, redCount: 0, totalRequests: 0 }],
  232. totalRequests: 0,
  233. currentStatus: "unknown",
  234. },
  235. ];
  236. expect(calculateErrorRate(providers)).toBe(0);
  237. });
  238. });
  239. describe("countByStatus", () => {
  240. const providers: Provider[] = [
  241. { timeBuckets: [], totalRequests: 100, currentStatus: "green" },
  242. { timeBuckets: [], totalRequests: 100, currentStatus: "green" },
  243. { timeBuckets: [], totalRequests: 50, currentStatus: "yellow" },
  244. { timeBuckets: [], totalRequests: 10, currentStatus: "red" },
  245. { timeBuckets: [], totalRequests: 0, currentStatus: "unknown" },
  246. ];
  247. test("should count green providers", () => {
  248. expect(countByStatus(providers, "green")).toBe(2);
  249. });
  250. test("should count yellow providers", () => {
  251. expect(countByStatus(providers, "yellow")).toBe(1);
  252. });
  253. test("should count red providers", () => {
  254. expect(countByStatus(providers, "red")).toBe(1);
  255. });
  256. test("should count unknown providers", () => {
  257. expect(countByStatus(providers, "unknown")).toBe(1);
  258. });
  259. test("should return 0 for non-existent status", () => {
  260. expect(countByStatus(providers, "nonexistent")).toBe(0);
  261. });
  262. });
  263. });
  264. describe("AvailabilityDashboard - auto-refresh intervals", () => {
  265. test("provider tab should use 30 second interval", () => {
  266. const activeTab = "provider";
  267. const interval = activeTab === "provider" ? 30000 : 10000;
  268. expect(interval).toBe(30000);
  269. });
  270. test("endpoint tab should use 10 second interval", () => {
  271. const activeTab = "endpoint";
  272. const interval = activeTab === "provider" ? 30000 : 10000;
  273. expect(interval).toBe(10000);
  274. });
  275. });