import { describe, expect, it } from "vitest"; import { DISPLAY_RETRY_BASE_MS, DISPLAY_RETRY_MAX_MS, displayRetryDelayMs, } from "../../src/browser/backoff.js"; describe("display retry backoff", () => { it("doubles from one second and stops at one minute", () => { const longest = (attempt: number) => displayRetryDelayMs(attempt, () => 1); expect([0, 1, 2, 3, 4, 5, 6, 7, 50].map(longest)).toEqual([ 1_000, 2_000, 4_000, 8_000, 16_000, 32_000, 60_000, 60_000, 60_000, ]); }); it("jitters between half and all of each step, and never waits zero", () => { for (const attempt of [0, 3, 6, 40]) { const step = Math.min(DISPLAY_RETRY_MAX_MS, DISPLAY_RETRY_BASE_MS * 2 ** attempt); expect(displayRetryDelayMs(attempt, () => 0)).toBe(step / 2); expect(displayRetryDelayMs(attempt, () => 1)).toBe(step); for (let index = 0; index < 50; index += 1) { const delay = displayRetryDelayMs(attempt); expect(delay).toBeGreaterThanOrEqual(step / 2); expect(delay).toBeLessThanOrEqual(step); } } }); it("treats a nonsensical attempt or random value as the nearest sensible one", () => { expect(displayRetryDelayMs(-3, () => 1)).toBe(1_000); expect(displayRetryDelayMs(2, () => 7)).toBe(4_000); expect(displayRetryDelayMs(2, () => -1)).toBe(2_000); }); });