|
| 1 | +/** |
| 2 | + * Tests for webhook backoff config — Issue #607 |
| 3 | + */ |
| 4 | + |
| 5 | +import { describe, it, expect } from 'vitest' |
| 6 | +import { |
| 7 | + validateBackoff, |
| 8 | + computeBackoffDelay, |
| 9 | + DEFAULT_BACKOFF, |
| 10 | +} from '../webhook-backoff' |
| 11 | + |
| 12 | +describe('validateBackoff (#607)', () => { |
| 13 | + it('returns defaults when input is null', () => { |
| 14 | + const result = validateBackoff(null) |
| 15 | + expect(result.ok).toBe(true) |
| 16 | + if (result.ok) expect(result.value).toEqual(DEFAULT_BACKOFF) |
| 17 | + }) |
| 18 | + |
| 19 | + it('merges partial config with defaults', () => { |
| 20 | + const result = validateBackoff({ initialMs: 500 }) |
| 21 | + expect(result.ok).toBe(true) |
| 22 | + if (result.ok) { |
| 23 | + expect(result.value.initialMs).toBe(500) |
| 24 | + expect(result.value.maxMs).toBe(DEFAULT_BACKOFF.maxMs) |
| 25 | + } |
| 26 | + }) |
| 27 | + |
| 28 | + it('rejects initialMs below 50', () => { |
| 29 | + const result = validateBackoff({ initialMs: 10 }) |
| 30 | + expect(result.ok).toBe(false) |
| 31 | + }) |
| 32 | + |
| 33 | + it('rejects initialMs above 5000', () => { |
| 34 | + const result = validateBackoff({ initialMs: 6000 }) |
| 35 | + expect(result.ok).toBe(false) |
| 36 | + }) |
| 37 | + |
| 38 | + it('rejects maxMs below initialMs', () => { |
| 39 | + const result = validateBackoff({ initialMs: 1000, maxMs: 500 }) |
| 40 | + expect(result.ok).toBe(false) |
| 41 | + }) |
| 42 | + |
| 43 | + it('rejects maxMs above 60000', () => { |
| 44 | + const result = validateBackoff({ maxMs: 70_000 }) |
| 45 | + expect(result.ok).toBe(false) |
| 46 | + }) |
| 47 | + |
| 48 | + it('rejects multiplier below 1', () => { |
| 49 | + const result = validateBackoff({ multiplier: 0.5 }) |
| 50 | + expect(result.ok).toBe(false) |
| 51 | + }) |
| 52 | + |
| 53 | + it('rejects multiplier above 5', () => { |
| 54 | + const result = validateBackoff({ multiplier: 6 }) |
| 55 | + expect(result.ok).toBe(false) |
| 56 | + }) |
| 57 | + |
| 58 | + it('rejects jitter outside [0, 1]', () => { |
| 59 | + expect(validateBackoff({ jitter: -0.1 }).ok).toBe(false) |
| 60 | + expect(validateBackoff({ jitter: 1.1 }).ok).toBe(false) |
| 61 | + }) |
| 62 | + |
| 63 | + it('accepts boundary values', () => { |
| 64 | + const result = validateBackoff({ |
| 65 | + initialMs: 50, |
| 66 | + maxMs: 60_000, |
| 67 | + multiplier: 5, |
| 68 | + jitter: 0, |
| 69 | + }) |
| 70 | + expect(result.ok).toBe(true) |
| 71 | + }) |
| 72 | +}) |
| 73 | + |
| 74 | +describe('computeBackoffDelay (#607)', () => { |
| 75 | + it('returns initialMs for attempt 0 with zero jitter', () => { |
| 76 | + const delay = computeBackoffDelay( |
| 77 | + 0, |
| 78 | + { ...DEFAULT_BACKOFF, jitter: 0 }, |
| 79 | + () => 0.5, |
| 80 | + ) |
| 81 | + expect(delay).toBe(DEFAULT_BACKOFF.initialMs) |
| 82 | + }) |
| 83 | + |
| 84 | + it('multiplies on each successive attempt', () => { |
| 85 | + const cfg = { initialMs: 100, maxMs: 10_000, multiplier: 2, jitter: 0 } |
| 86 | + expect(computeBackoffDelay(0, cfg, () => 0.5)).toBe(100) |
| 87 | + expect(computeBackoffDelay(1, cfg, () => 0.5)).toBe(200) |
| 88 | + expect(computeBackoffDelay(2, cfg, () => 0.5)).toBe(400) |
| 89 | + expect(computeBackoffDelay(3, cfg, () => 0.5)).toBe(800) |
| 90 | + }) |
| 91 | + |
| 92 | + it('caps at maxMs', () => { |
| 93 | + const cfg = { initialMs: 100, maxMs: 500, multiplier: 2, jitter: 0 } |
| 94 | + expect(computeBackoffDelay(10, cfg, () => 0.5)).toBe(500) |
| 95 | + }) |
| 96 | + |
| 97 | + it('respects jitter bounds', () => { |
| 98 | + const cfg = { initialMs: 1000, maxMs: 10_000, multiplier: 1, jitter: 0.5 } |
| 99 | + // With random()=0 → variance = 1000*0.5*-0.5 = -250 → 750 |
| 100 | + // With random()=1 → variance = 1000*0.5*0.5 = 250 → 1250 |
| 101 | + expect(computeBackoffDelay(0, cfg, () => 0)).toBe(750) |
| 102 | + expect(computeBackoffDelay(0, cfg, () => 1)).toBe(1250) |
| 103 | + }) |
| 104 | + |
| 105 | + it('never returns negative delays', () => { |
| 106 | + const cfg = { initialMs: 100, maxMs: 10_000, multiplier: 1, jitter: 1 } |
| 107 | + expect(computeBackoffDelay(0, cfg, () => -10)).toBeGreaterThanOrEqual(0) |
| 108 | + }) |
| 109 | +}) |
0 commit comments