|
| 1 | +import { describe, it, expect, beforeEach, vi } from "vitest"; |
| 2 | +import { |
| 3 | + RateLimitCircuitBreaker, |
| 4 | + isRateLimitErrorMessage, |
| 5 | + isTransientErrorMessage, |
| 6 | +} from "./circuit-breaker.js"; |
| 7 | + |
| 8 | +describe("isRateLimitErrorMessage", () => { |
| 9 | + it("matches the standard OpenClaw rate limit message", () => { |
| 10 | + expect(isRateLimitErrorMessage("API rate limit reached. Please try again later.")).toBe(true); |
| 11 | + }); |
| 12 | + |
| 13 | + it("matches messages with emoji prefix", () => { |
| 14 | + expect(isRateLimitErrorMessage("\u26a0\ufe0f API rate limit reached. Please try again later.")).toBe(true); |
| 15 | + }); |
| 16 | + |
| 17 | + it("matches 429 status code references", () => { |
| 18 | + expect(isRateLimitErrorMessage("HTTP 429 Too Many Requests")).toBe(true); |
| 19 | + }); |
| 20 | + |
| 21 | + it("matches rate_limit_exceeded error type", () => { |
| 22 | + expect(isRateLimitErrorMessage('{"type":"rate_limit_exceeded"}')).toBe(true); |
| 23 | + }); |
| 24 | + |
| 25 | + it("matches overloaded messages", () => { |
| 26 | + expect(isRateLimitErrorMessage("The AI service is temporarily overloaded. Please try again in a moment.")).toBe(true); |
| 27 | + }); |
| 28 | + |
| 29 | + it("does not match normal messages", () => { |
| 30 | + expect(isRateLimitErrorMessage("Hello, how are you?")).toBe(false); |
| 31 | + expect(isRateLimitErrorMessage("The rate of improvement is excellent")).toBe(false); |
| 32 | + }); |
| 33 | +}); |
| 34 | + |
| 35 | +describe("isTransientErrorMessage", () => { |
| 36 | + it("matches timeout messages", () => { |
| 37 | + expect(isTransientErrorMessage("LLM request timed out.")).toBe(true); |
| 38 | + }); |
| 39 | + |
| 40 | + it("matches rate limit messages (superset)", () => { |
| 41 | + expect(isTransientErrorMessage("API rate limit reached. Please try again later.")).toBe(true); |
| 42 | + }); |
| 43 | + |
| 44 | + it("does not match normal messages", () => { |
| 45 | + expect(isTransientErrorMessage("Here is your report.")).toBe(false); |
| 46 | + }); |
| 47 | +}); |
| 48 | + |
| 49 | +describe("RateLimitCircuitBreaker", () => { |
| 50 | + let breaker: RateLimitCircuitBreaker; |
| 51 | + const channel = "matrix"; |
| 52 | + const room = "!room123:server.org"; |
| 53 | + const errorMsg = "\u26a0\ufe0f API rate limit reached. Please try again later."; |
| 54 | + const normalMsg = "Here is the analysis you requested."; |
| 55 | + |
| 56 | + const warnSpy = vi.fn(); |
| 57 | + const debugSpy = vi.fn(); |
| 58 | + |
| 59 | + beforeEach(() => { |
| 60 | + warnSpy.mockReset(); |
| 61 | + debugSpy.mockReset(); |
| 62 | + breaker = new RateLimitCircuitBreaker( |
| 63 | + { maxConsecutiveErrors: 3, baseCooldownMs: 1000, maxCooldownMs: 8000 }, |
| 64 | + { warn: warnSpy, debug: debugSpy }, |
| 65 | + ); |
| 66 | + }); |
| 67 | + |
| 68 | + it("allows normal messages through", () => { |
| 69 | + expect(breaker.shouldSuppress(channel, room, normalMsg)).toBe(false); |
| 70 | + }); |
| 71 | + |
| 72 | + it("allows first N-1 rate limit errors through", () => { |
| 73 | + expect(breaker.shouldSuppress(channel, room, errorMsg)).toBe(false); // 1/3 |
| 74 | + expect(breaker.shouldSuppress(channel, room, errorMsg)).toBe(false); // 2/3 |
| 75 | + }); |
| 76 | + |
| 77 | + it("suppresses the Nth consecutive error (trips the circuit)", () => { |
| 78 | + breaker.shouldSuppress(channel, room, errorMsg); // 1 |
| 79 | + breaker.shouldSuppress(channel, room, errorMsg); // 2 |
| 80 | + expect(breaker.shouldSuppress(channel, room, errorMsg)).toBe(true); // 3 -> OPEN |
| 81 | + expect(warnSpy).toHaveBeenCalledWith(expect.stringContaining("closed -> open")); |
| 82 | + }); |
| 83 | + |
| 84 | + it("suppresses subsequent errors while circuit is open", () => { |
| 85 | + for (let i = 0; i < 3; i++) breaker.shouldSuppress(channel, room, errorMsg); |
| 86 | + expect(breaker.shouldSuppress(channel, room, errorMsg)).toBe(true); |
| 87 | + expect(breaker.shouldSuppress(channel, room, errorMsg)).toBe(true); |
| 88 | + }); |
| 89 | + |
| 90 | + it("allows normal messages while circuit is open (does not suppress non-error)", () => { |
| 91 | + for (let i = 0; i < 3; i++) breaker.shouldSuppress(channel, room, errorMsg); |
| 92 | + // Non-error messages always go through |
| 93 | + expect(breaker.shouldSuppress(channel, room, normalMsg)).toBe(false); |
| 94 | + }); |
| 95 | + |
| 96 | + it("transitions to half_open after cooldown and allows one retry", () => { |
| 97 | + for (let i = 0; i < 3; i++) breaker.shouldSuppress(channel, room, errorMsg); |
| 98 | + const state = breaker.getState(channel, room)!; |
| 99 | + // Simulate cooldown expiration by backdating openedAt |
| 100 | + state.openedAt = Date.now() - 2000; // 2s > 1s cooldown |
| 101 | + // Next error triggers half_open transition + is allowed through |
| 102 | + expect(breaker.shouldSuppress(channel, room, errorMsg)).toBe(false); |
| 103 | + expect(breaker.getState(channel, room)!.state).toBe("half_open"); |
| 104 | + }); |
| 105 | + |
| 106 | + it("re-opens with doubled cooldown if retry fails in half_open", () => { |
| 107 | + for (let i = 0; i < 3; i++) breaker.shouldSuppress(channel, room, errorMsg); |
| 108 | + const state = breaker.getState(channel, room)!; |
| 109 | + state.openedAt = Date.now() - 2000; |
| 110 | + breaker.shouldSuppress(channel, room, errorMsg); // -> half_open, allowed |
| 111 | + // Another error in half_open |
| 112 | + expect(breaker.shouldSuppress(channel, room, errorMsg)).toBe(true); |
| 113 | + const updatedState = breaker.getState(channel, room)!; |
| 114 | + expect(updatedState.state).toBe("open"); |
| 115 | + expect(updatedState.cooldownMs).toBe(2000); // doubled from 1000 |
| 116 | + expect(warnSpy).toHaveBeenCalledWith(expect.stringContaining("half_open -> open")); |
| 117 | + }); |
| 118 | + |
| 119 | + it("resets to closed on success in half_open state", () => { |
| 120 | + for (let i = 0; i < 3; i++) breaker.shouldSuppress(channel, room, errorMsg); |
| 121 | + const state = breaker.getState(channel, room)!; |
| 122 | + state.openedAt = Date.now() - 2000; |
| 123 | + breaker.shouldSuppress(channel, room, errorMsg); // -> half_open |
| 124 | + // Normal message = success |
| 125 | + expect(breaker.shouldSuppress(channel, room, normalMsg)).toBe(false); |
| 126 | + const updatedState = breaker.getState(channel, room)!; |
| 127 | + expect(updatedState.state).toBe("closed"); |
| 128 | + expect(updatedState.consecutiveErrors).toBe(0); |
| 129 | + expect(updatedState.tripCount).toBe(0); |
| 130 | + }); |
| 131 | + |
| 132 | + it("resets error count on normal message in closed state", () => { |
| 133 | + breaker.shouldSuppress(channel, room, errorMsg); // 1 |
| 134 | + breaker.shouldSuppress(channel, room, errorMsg); // 2 |
| 135 | + breaker.shouldSuppress(channel, room, normalMsg); // reset |
| 136 | + breaker.shouldSuppress(channel, room, errorMsg); // 1 again |
| 137 | + breaker.shouldSuppress(channel, room, errorMsg); // 2 again |
| 138 | + expect(breaker.shouldSuppress(channel, room, errorMsg)).toBe(true); // 3 -> trips |
| 139 | + }); |
| 140 | + |
| 141 | + it("tracks rooms independently", () => { |
| 142 | + const room2 = "!room456:server.org"; |
| 143 | + for (let i = 0; i < 3; i++) breaker.shouldSuppress(channel, room, errorMsg); |
| 144 | + // room1 is open |
| 145 | + expect(breaker.shouldSuppress(channel, room, errorMsg)).toBe(true); |
| 146 | + // room2 is still closed |
| 147 | + expect(breaker.shouldSuppress(channel, room2, errorMsg)).toBe(false); |
| 148 | + }); |
| 149 | + |
| 150 | + it("caps cooldown at maxCooldownMs", () => { |
| 151 | + // Trip multiple times |
| 152 | + for (let trip = 0; trip < 10; trip++) { |
| 153 | + // Fill up consecutive errors |
| 154 | + for (let i = 0; i < 3; i++) breaker.shouldSuppress(channel, room, errorMsg); |
| 155 | + // Now it's open. Expire the cooldown. |
| 156 | + const state = breaker.getState(channel, room)!; |
| 157 | + state.openedAt = Date.now() - state.cooldownMs - 100; |
| 158 | + breaker.shouldSuppress(channel, room, errorMsg); // -> half_open, allowed |
| 159 | + // Fail the retry |
| 160 | + breaker.shouldSuppress(channel, room, errorMsg); // -> open with doubled cooldown |
| 161 | + } |
| 162 | + const finalState = breaker.getState(channel, room)!; |
| 163 | + expect(finalState.cooldownMs).toBeLessThanOrEqual(8000); |
| 164 | + }); |
| 165 | + |
| 166 | + it("recordSuccess resets the circuit fully", () => { |
| 167 | + for (let i = 0; i < 3; i++) breaker.shouldSuppress(channel, room, errorMsg); |
| 168 | + expect(breaker.getState(channel, room)!.state).toBe("open"); |
| 169 | + breaker.recordSuccess(channel, room); |
| 170 | + expect(breaker.getState(channel, room)!.state).toBe("closed"); |
| 171 | + expect(breaker.getState(channel, room)!.consecutiveErrors).toBe(0); |
| 172 | + }); |
| 173 | + |
| 174 | + it("cleanup removes stale entries", () => { |
| 175 | + for (let i = 0; i < 3; i++) breaker.shouldSuppress(channel, room, errorMsg); |
| 176 | + const state = breaker.getState(channel, room)!; |
| 177 | + state.openedAt = Date.now() - 7_200_000; // 2 hours ago |
| 178 | + breaker.cleanup(3_600_000); // 1 hour max age |
| 179 | + expect(breaker.getState(channel, room)).toBeUndefined(); |
| 180 | + }); |
| 181 | +}); |
0 commit comments