Menu
Public repository / 054e5cda0171bd13facff3c16a38f2048a1052cc
AkurAI-Mail / src/lib/webmail/sync.test.ts
Latest change 22202d1bde69be4ace8a299dffd91f8ff24378df - Recover deployed webmail frontend (frontend parity with EC2 a6afffc) by Ólafur Búi Ólafsson
import { describe, expect, test } from "bun:test";
import { classifySyncError, retryDelay, syncedLabel } from "./sync";
describe("sync helpers", () => {
test("backs off exponentially up to one minute", () => {
expect([0, 1, 2, 5, 10].map(retryDelay)).toEqual([2000, 4000, 8000, 60000, 60000]);
});
test("classifies 401 as an expired session, network failures as offline, the rest as server errors", () => {
expect(classifySyncError(new Error("x"), { status: 401 }, true)).toBe("auth-expired");
expect(classifySyncError(new TypeError("fetch failed"), undefined, true)).toBe("offline");
expect(classifySyncError(new Error("x"), { status: 500 }, false)).toBe("offline");
expect(classifySyncError(new Error("x"), { status: 502 }, true)).toBe("server-error");
});
test("describes the last sync time", () => {
const now = 1_000_000_000;
expect(syncedLabel(null, now)).toBe("Never synced");
expect(syncedLabel(now - 2000, now)).toBe("Synced just now");
expect(syncedLabel(now - 30_000, now)).toBe("Synced 30s ago");
expect(syncedLabel(now - 5 * 60_000, now)).toBe("Synced 5m ago");
expect(syncedLabel(now - 3 * 3_600_000, now)).toBe("Synced 3h ago");
});
});