|
| 1 | +import { describe, test, expect } from 'bun:test' |
| 2 | + |
| 3 | +import { getNextInterval } from '../use-connection-status' |
| 4 | + |
| 5 | +/** |
| 6 | + * Tests for the adaptive health check interval logic. |
| 7 | + * |
| 8 | + * These tests verify the core exponential backoff algorithm that determines |
| 9 | + * how frequently health checks should run based on consecutive successful checks. |
| 10 | + */ |
| 11 | + |
| 12 | +describe('useConnectionStatus - adaptive interval logic', () => { |
| 13 | + describe('getNextInterval', () => { |
| 14 | + test('returns 10s for 0-2 consecutive successes', () => { |
| 15 | + expect(getNextInterval(0)).toBe(10_000) |
| 16 | + expect(getNextInterval(1)).toBe(10_000) |
| 17 | + expect(getNextInterval(2)).toBe(10_000) |
| 18 | + }) |
| 19 | + |
| 20 | + test('returns 30s for 3-5 consecutive successes', () => { |
| 21 | + expect(getNextInterval(3)).toBe(30_000) |
| 22 | + expect(getNextInterval(4)).toBe(30_000) |
| 23 | + expect(getNextInterval(5)).toBe(30_000) |
| 24 | + }) |
| 25 | + |
| 26 | + test('returns 60s for 6-9 consecutive successes', () => { |
| 27 | + expect(getNextInterval(6)).toBe(60_000) |
| 28 | + expect(getNextInterval(7)).toBe(60_000) |
| 29 | + expect(getNextInterval(8)).toBe(60_000) |
| 30 | + expect(getNextInterval(9)).toBe(60_000) |
| 31 | + }) |
| 32 | + |
| 33 | + test('returns 2min for 10-14 consecutive successes', () => { |
| 34 | + expect(getNextInterval(10)).toBe(120_000) |
| 35 | + expect(getNextInterval(11)).toBe(120_000) |
| 36 | + expect(getNextInterval(14)).toBe(120_000) |
| 37 | + }) |
| 38 | + |
| 39 | + test('returns 5min for 15-19 consecutive successes', () => { |
| 40 | + expect(getNextInterval(15)).toBe(300_000) |
| 41 | + expect(getNextInterval(16)).toBe(300_000) |
| 42 | + expect(getNextInterval(19)).toBe(300_000) |
| 43 | + }) |
| 44 | + |
| 45 | + test('returns 10min (max) for 20+ consecutive successes', () => { |
| 46 | + expect(getNextInterval(20)).toBe(600_000) |
| 47 | + expect(getNextInterval(25)).toBe(600_000) |
| 48 | + expect(getNextInterval(100)).toBe(600_000) |
| 49 | + expect(getNextInterval(1000)).toBe(600_000) |
| 50 | + }) |
| 51 | + }) |
| 52 | + |
| 53 | + describe('interval progression', () => { |
| 54 | + test('progresses through all thresholds in order', () => { |
| 55 | + const progression = [ |
| 56 | + { successes: 0, expectedInterval: 10_000 }, |
| 57 | + { successes: 3, expectedInterval: 30_000 }, |
| 58 | + { successes: 6, expectedInterval: 60_000 }, |
| 59 | + { successes: 10, expectedInterval: 120_000 }, |
| 60 | + { successes: 15, expectedInterval: 300_000 }, |
| 61 | + { successes: 20, expectedInterval: 600_000 }, |
| 62 | + ] |
| 63 | + |
| 64 | + for (const { successes, expectedInterval } of progression) { |
| 65 | + expect(getNextInterval(successes)).toBe(expectedInterval) |
| 66 | + } |
| 67 | + }) |
| 68 | + |
| 69 | + test('intervals increase monotonically', () => { |
| 70 | + let previousInterval = 0 |
| 71 | + // Test first occurrence of each threshold |
| 72 | + const testPoints = [0, 3, 6, 10, 15, 20, 50] |
| 73 | + |
| 74 | + for (const successes of testPoints) { |
| 75 | + const interval = getNextInterval(successes) |
| 76 | + expect(interval).toBeGreaterThanOrEqual(previousInterval) |
| 77 | + previousInterval = interval |
| 78 | + } |
| 79 | + }) |
| 80 | + }) |
| 81 | + |
| 82 | + describe('edge cases', () => { |
| 83 | + test('handles negative values (treated as 0)', () => { |
| 84 | + // In practice, consecutive successes should never be negative, |
| 85 | + // but the function should handle it gracefully |
| 86 | + expect(getNextInterval(-1)).toBe(10_000) |
| 87 | + expect(getNextInterval(-100)).toBe(10_000) |
| 88 | + }) |
| 89 | + |
| 90 | + test('handles very large values', () => { |
| 91 | + expect(getNextInterval(Number.MAX_SAFE_INTEGER)).toBe(600_000) |
| 92 | + }) |
| 93 | + }) |
| 94 | + |
| 95 | + describe('boundary conditions', () => { |
| 96 | + test('uses correct interval at exact threshold boundaries', () => { |
| 97 | + // At threshold - 1: should use previous interval |
| 98 | + expect(getNextInterval(2)).toBe(10_000) // Just before 3 |
| 99 | + expect(getNextInterval(5)).toBe(30_000) // Just before 6 |
| 100 | + expect(getNextInterval(9)).toBe(60_000) // Just before 10 |
| 101 | + expect(getNextInterval(14)).toBe(120_000) // Just before 15 |
| 102 | + expect(getNextInterval(19)).toBe(300_000) // Just before 20 |
| 103 | + |
| 104 | + // At threshold: should use new interval |
| 105 | + expect(getNextInterval(3)).toBe(30_000) |
| 106 | + expect(getNextInterval(6)).toBe(60_000) |
| 107 | + expect(getNextInterval(10)).toBe(120_000) |
| 108 | + expect(getNextInterval(15)).toBe(300_000) |
| 109 | + expect(getNextInterval(20)).toBe(600_000) |
| 110 | + }) |
| 111 | + }) |
| 112 | +}) |
0 commit comments