import { describe, expect, it } from 'vitest'; import { Backoff } from '../../src/background/transport/backoff.js'; describe('Backoff', () => { it('grows geometrically and caps, with no jitter', () => { const b = new Backoff({ baseMs: 500, factor: 2, maxMs: 8000, jitter: 0 }); expect([b.next(), b.next(), b.next(), b.next(), b.next(), b.next()]).toEqual([ 500, 1000, 2000, 4000, 8000, 8000, ]); }); it('reset() returns to the base delay', () => { const b = new Backoff({ baseMs: 100, factor: 3, jitter: 0 }); b.next(); b.next(); expect(b.attempts).toBe(2); b.reset(); expect(b.attempts).toBe(0); expect(b.next()).toBe(100); }); it('keeps jittered delays within +/- the jitter fraction', () => { const rand = [0, 0.5, 1]; let i = 0; const b = new Backoff({ baseMs: 1000, factor: 1, maxMs: 1000, jitter: 0.2, random: () => rand[i++]! }); expect(b.next()).toBe(800); // random 0 -> raw - spread expect(b.next()).toBe(1000); // random 0.5 -> raw expect(b.next()).toBe(1200); // random 1 -> raw + spread }); });