|
| 1 | +import { describe, expect, test } from "bun:test"; |
| 2 | +import { otherAccountsAvailable } from "../otherAccountsAvailable"; |
| 3 | + |
| 4 | +type Account = Parameters<typeof otherAccountsAvailable>[0][number]; |
| 5 | + |
| 6 | +const baseAccount: Account = { |
| 7 | + id: "acc-1", |
| 8 | + paused: false, |
| 9 | + rateLimitedUntil: null, |
| 10 | +}; |
| 11 | + |
| 12 | +describe("otherAccountsAvailable", () => { |
| 13 | + test("returns true when a healthy other account exists", () => { |
| 14 | + const accounts: Account[] = [ |
| 15 | + { ...baseAccount, id: "acc-self" }, |
| 16 | + { ...baseAccount, id: "acc-other" }, |
| 17 | + ]; |
| 18 | + expect(otherAccountsAvailable(accounts, "acc-self")).toBe(true); |
| 19 | + }); |
| 20 | + |
| 21 | + test("excludes the error's own account", () => { |
| 22 | + const accounts: Account[] = [{ ...baseAccount, id: "acc-self" }]; |
| 23 | + expect(otherAccountsAvailable(accounts, "acc-self")).toBe(false); |
| 24 | + }); |
| 25 | + |
| 26 | + test("excludes paused accounts", () => { |
| 27 | + const accounts: Account[] = [ |
| 28 | + { ...baseAccount, id: "acc-self" }, |
| 29 | + { ...baseAccount, id: "acc-paused", paused: true }, |
| 30 | + ]; |
| 31 | + expect(otherAccountsAvailable(accounts, "acc-self")).toBe(false); |
| 32 | + }); |
| 33 | + |
| 34 | + test("excludes accounts whose rateLimitedUntil is in the future", () => { |
| 35 | + const future = Date.now() + 60_000; |
| 36 | + const accounts: Account[] = [ |
| 37 | + { ...baseAccount, id: "acc-self" }, |
| 38 | + { ...baseAccount, id: "acc-rl", rateLimitedUntil: future }, |
| 39 | + ]; |
| 40 | + expect(otherAccountsAvailable(accounts, "acc-self")).toBe(false); |
| 41 | + }); |
| 42 | + |
| 43 | + test("counts accounts whose rateLimitedUntil has already elapsed", () => { |
| 44 | + const past = Date.now() - 60_000; |
| 45 | + const accounts: Account[] = [ |
| 46 | + { ...baseAccount, id: "acc-self" }, |
| 47 | + { ...baseAccount, id: "acc-recovered", rateLimitedUntil: past }, |
| 48 | + ]; |
| 49 | + expect(otherAccountsAvailable(accounts, "acc-self")).toBe(true); |
| 50 | + }); |
| 51 | + |
| 52 | + test("treats null rateLimitedUntil as available", () => { |
| 53 | + const accounts: Account[] = [ |
| 54 | + { ...baseAccount, id: "acc-self" }, |
| 55 | + { ...baseAccount, id: "acc-other", rateLimitedUntil: null }, |
| 56 | + ]; |
| 57 | + expect(otherAccountsAvailable(accounts, "acc-self")).toBe(true); |
| 58 | + }); |
| 59 | + |
| 60 | + test("handles undefined accounts list as no available", () => { |
| 61 | + expect(otherAccountsAvailable(undefined, "acc-self")).toBe(false); |
| 62 | + }); |
| 63 | + |
| 64 | + test("returns false when only paused and rate-limited accounts exist", () => { |
| 65 | + const future = Date.now() + 60_000; |
| 66 | + const accounts: Account[] = [ |
| 67 | + { ...baseAccount, id: "acc-self" }, |
| 68 | + { ...baseAccount, id: "acc-paused", paused: true }, |
| 69 | + { ...baseAccount, id: "acc-rl", rateLimitedUntil: future }, |
| 70 | + ]; |
| 71 | + expect(otherAccountsAvailable(accounts, "acc-self")).toBe(false); |
| 72 | + }); |
| 73 | +}); |
0 commit comments