|
| 1 | +import { describe, expect, it } from "bun:test"; |
| 2 | + |
| 3 | +import { nextPollStep, slowDownIncrementMs } from "./pollState.js"; |
| 4 | +import type { DeviceTokens, PollState } from "./types.js"; |
| 5 | + |
| 6 | +const state: PollState = { intervalMs: 5_000, deadlineMs: 300_000 }; |
| 7 | + |
| 8 | +const tokens: DeviceTokens = { |
| 9 | + accessToken: "access", |
| 10 | + refreshToken: "refresh", |
| 11 | + expiresAt: undefined, |
| 12 | + organizationId: undefined, |
| 13 | +}; |
| 14 | + |
| 15 | +describe("nextPollStep", () => { |
| 16 | + it("finishes when the response carries tokens", () => { |
| 17 | + const step = nextPollStep(state, { kind: "tokens", tokens }, 0); |
| 18 | + |
| 19 | + expect(step).toEqual({ action: "done", tokens }); |
| 20 | + }); |
| 21 | + |
| 22 | + it("finishes on tokens even after the deadline has passed", () => { |
| 23 | + const step = nextPollStep(state, { kind: "tokens", tokens }, 300_001); |
| 24 | + |
| 25 | + expect(step).toEqual({ action: "done", tokens }); |
| 26 | + }); |
| 27 | + |
| 28 | + it("polls again at the current interval while authorization is pending", () => { |
| 29 | + const step = nextPollStep(state, { kind: "pending" }, 0); |
| 30 | + |
| 31 | + expect(step).toEqual({ action: "poll", delayMs: 5_000, state }); |
| 32 | + }); |
| 33 | + |
| 34 | + it("raises the interval by five seconds on slow-down", () => { |
| 35 | + const step = nextPollStep(state, { kind: "slow-down" }, 0); |
| 36 | + |
| 37 | + expect(step).toEqual({ |
| 38 | + action: "poll", |
| 39 | + delayMs: 5_000 + slowDownIncrementMs, |
| 40 | + state: { intervalMs: 5_000 + slowDownIncrementMs, deadlineMs: 300_000 }, |
| 41 | + }); |
| 42 | + }); |
| 43 | + |
| 44 | + it("keeps the raised interval for later pending responses", () => { |
| 45 | + const slowed = nextPollStep(state, { kind: "slow-down" }, 0); |
| 46 | + if (slowed.action !== "poll") throw Error("expected to keep polling"); |
| 47 | + |
| 48 | + const step = nextPollStep(slowed.state, { kind: "pending" }, 0); |
| 49 | + |
| 50 | + expect(step).toEqual({ |
| 51 | + action: "poll", |
| 52 | + delayMs: 10_000, |
| 53 | + state: slowed.state, |
| 54 | + }); |
| 55 | + }); |
| 56 | + |
| 57 | + it("fails when the person rejects the request", () => { |
| 58 | + const step = nextPollStep(state, { kind: "denied" }, 0); |
| 59 | + |
| 60 | + expect(step).toEqual({ |
| 61 | + action: "fail", |
| 62 | + reason: "access-denied", |
| 63 | + detail: undefined, |
| 64 | + }); |
| 65 | + }); |
| 66 | + |
| 67 | + it("fails when the device code expires", () => { |
| 68 | + const step = nextPollStep(state, { kind: "expired" }, 0); |
| 69 | + |
| 70 | + expect(step).toEqual({ |
| 71 | + action: "fail", |
| 72 | + reason: "expired", |
| 73 | + detail: undefined, |
| 74 | + }); |
| 75 | + }); |
| 76 | + |
| 77 | + it("times out once the deadline passes, however long the server stalls", () => { |
| 78 | + const step = nextPollStep(state, { kind: "pending" }, 300_001); |
| 79 | + |
| 80 | + expect(step).toEqual({ |
| 81 | + action: "fail", |
| 82 | + reason: "timeout", |
| 83 | + detail: undefined, |
| 84 | + }); |
| 85 | + }); |
| 86 | + |
| 87 | + it("keeps polling at the deadline itself", () => { |
| 88 | + const step = nextPollStep(state, { kind: "pending" }, 300_000); |
| 89 | + |
| 90 | + expect(step).toEqual({ action: "poll", delayMs: 5_000, state }); |
| 91 | + }); |
| 92 | + |
| 93 | + it("retries a server it could not reach, at double the interval", () => { |
| 94 | + const step = nextPollStep(state, { kind: "unreachable", detail: "x" }, 0); |
| 95 | + |
| 96 | + expect(step).toEqual({ |
| 97 | + action: "poll", |
| 98 | + delayMs: 10_000, |
| 99 | + state: { intervalMs: 10_000, deadlineMs: 300_000 }, |
| 100 | + }); |
| 101 | + }); |
| 102 | + |
| 103 | + it("doubles again while it still cannot reach the server", () => { |
| 104 | + const first = nextPollStep(state, { kind: "unreachable", detail: "x" }, 0); |
| 105 | + if (first.action !== "poll") throw Error("expected to keep polling"); |
| 106 | + |
| 107 | + const second = nextPollStep( |
| 108 | + first.state, |
| 109 | + { kind: "unreachable", detail: "x" }, |
| 110 | + 0, |
| 111 | + ); |
| 112 | + |
| 113 | + expect(second).toEqual({ |
| 114 | + action: "poll", |
| 115 | + delayMs: 20_000, |
| 116 | + state: { intervalMs: 20_000, deadlineMs: 300_000 }, |
| 117 | + }); |
| 118 | + }); |
| 119 | + |
| 120 | + it("keeps the backed-off interval once the server answers again", () => { |
| 121 | + const backedOff = nextPollStep( |
| 122 | + state, |
| 123 | + { kind: "unreachable", detail: "x" }, |
| 124 | + 0, |
| 125 | + ); |
| 126 | + if (backedOff.action !== "poll") throw Error("expected to keep polling"); |
| 127 | + |
| 128 | + const step = nextPollStep(backedOff.state, { kind: "pending" }, 0); |
| 129 | + |
| 130 | + expect(step).toEqual({ |
| 131 | + action: "poll", |
| 132 | + delayMs: 10_000, |
| 133 | + state: backedOff.state, |
| 134 | + }); |
| 135 | + }); |
| 136 | + |
| 137 | + it("stops retrying an unreachable server once the deadline passes", () => { |
| 138 | + const step = nextPollStep( |
| 139 | + state, |
| 140 | + { kind: "unreachable", detail: "x" }, |
| 141 | + 300_001, |
| 142 | + ); |
| 143 | + |
| 144 | + expect(step).toEqual({ |
| 145 | + action: "fail", |
| 146 | + reason: "timeout", |
| 147 | + detail: undefined, |
| 148 | + }); |
| 149 | + }); |
| 150 | + |
| 151 | + it("fails with the detail from an unexpected error", () => { |
| 152 | + const step = nextPollStep(state, { kind: "error", detail: "boom" }, 0); |
| 153 | + |
| 154 | + expect(step).toEqual({ |
| 155 | + action: "fail", |
| 156 | + reason: "network", |
| 157 | + detail: "boom", |
| 158 | + }); |
| 159 | + }); |
| 160 | +}); |
0 commit comments