|
| 1 | +import { describe, expect, it } from "vitest"; |
| 2 | +import { isFeishuBackoffError, getBackoffCodeFromResponse, FeishuBackoffError } from "./typing.js"; |
| 3 | + |
| 4 | +describe("isFeishuBackoffError", () => { |
| 5 | + it("returns true for HTTP 429 (AxiosError shape)", () => { |
| 6 | + const err = { response: { status: 429, data: {} } }; |
| 7 | + expect(isFeishuBackoffError(err)).toBe(true); |
| 8 | + }); |
| 9 | + |
| 10 | + it("returns true for Feishu quota exceeded code 99991403", () => { |
| 11 | + const err = { response: { status: 200, data: { code: 99991403 } } }; |
| 12 | + expect(isFeishuBackoffError(err)).toBe(true); |
| 13 | + }); |
| 14 | + |
| 15 | + it("returns true for Feishu rate limit code 99991400", () => { |
| 16 | + const err = { response: { status: 200, data: { code: 99991400 } } }; |
| 17 | + expect(isFeishuBackoffError(err)).toBe(true); |
| 18 | + }); |
| 19 | + |
| 20 | + it("returns true for SDK error with code 429", () => { |
| 21 | + const err = { code: 429, message: "too many requests" }; |
| 22 | + expect(isFeishuBackoffError(err)).toBe(true); |
| 23 | + }); |
| 24 | + |
| 25 | + it("returns true for SDK error with top-level code 99991403", () => { |
| 26 | + const err = { code: 99991403, message: "quota exceeded" }; |
| 27 | + expect(isFeishuBackoffError(err)).toBe(true); |
| 28 | + }); |
| 29 | + |
| 30 | + it("returns false for other HTTP errors (e.g. 500)", () => { |
| 31 | + const err = { response: { status: 500, data: {} } }; |
| 32 | + expect(isFeishuBackoffError(err)).toBe(false); |
| 33 | + }); |
| 34 | + |
| 35 | + it("returns false for non-rate-limit Feishu codes", () => { |
| 36 | + const err = { response: { status: 200, data: { code: 99991401 } } }; |
| 37 | + expect(isFeishuBackoffError(err)).toBe(false); |
| 38 | + }); |
| 39 | + |
| 40 | + it("returns false for generic Error", () => { |
| 41 | + expect(isFeishuBackoffError(new Error("network timeout"))).toBe(false); |
| 42 | + }); |
| 43 | + |
| 44 | + it("returns false for null", () => { |
| 45 | + expect(isFeishuBackoffError(null)).toBe(false); |
| 46 | + }); |
| 47 | + |
| 48 | + it("returns false for undefined", () => { |
| 49 | + expect(isFeishuBackoffError(undefined)).toBe(false); |
| 50 | + }); |
| 51 | + |
| 52 | + it("returns false for string", () => { |
| 53 | + expect(isFeishuBackoffError("429")).toBe(false); |
| 54 | + }); |
| 55 | + |
| 56 | + it("returns true for 429 even without data", () => { |
| 57 | + const err = { response: { status: 429 } }; |
| 58 | + expect(isFeishuBackoffError(err)).toBe(true); |
| 59 | + }); |
| 60 | +}); |
| 61 | + |
| 62 | +describe("getBackoffCodeFromResponse", () => { |
| 63 | + it("returns backoff code for response with quota exceeded code", () => { |
| 64 | + const response = { code: 99991403, msg: "quota exceeded", data: null }; |
| 65 | + expect(getBackoffCodeFromResponse(response)).toBe(response.code); |
| 66 | + }); |
| 67 | + |
| 68 | + it("returns backoff code for response with rate limit code", () => { |
| 69 | + const response = { code: 99991400, msg: "rate limit", data: null }; |
| 70 | + expect(getBackoffCodeFromResponse(response)).toBe(response.code); |
| 71 | + }); |
| 72 | + |
| 73 | + it("returns backoff code for response with code 429", () => { |
| 74 | + const response = { code: 429, msg: "too many requests", data: null }; |
| 75 | + expect(getBackoffCodeFromResponse(response)).toBe(response.code); |
| 76 | + }); |
| 77 | + |
| 78 | + it("returns undefined for successful response (code 0)", () => { |
| 79 | + const response = { code: 0, msg: "success", data: { reaction_id: "r1" } }; |
| 80 | + expect(getBackoffCodeFromResponse(response)).toBeUndefined(); |
| 81 | + }); |
| 82 | + |
| 83 | + it("returns undefined for other error codes", () => { |
| 84 | + const response = { code: 99991401, msg: "other error", data: null }; |
| 85 | + expect(getBackoffCodeFromResponse(response)).toBeUndefined(); |
| 86 | + }); |
| 87 | + |
| 88 | + it("returns undefined for null", () => { |
| 89 | + expect(getBackoffCodeFromResponse(null)).toBeUndefined(); |
| 90 | + }); |
| 91 | + |
| 92 | + it("returns undefined for undefined", () => { |
| 93 | + expect(getBackoffCodeFromResponse(undefined)).toBeUndefined(); |
| 94 | + }); |
| 95 | + |
| 96 | + it("returns undefined for response without code field", () => { |
| 97 | + const response = { data: { reaction_id: "r1" } }; |
| 98 | + expect(getBackoffCodeFromResponse(response)).toBeUndefined(); |
| 99 | + }); |
| 100 | +}); |
| 101 | + |
| 102 | +describe("FeishuBackoffError", () => { |
| 103 | + it("is detected by isFeishuBackoffError via .code property", () => { |
| 104 | + const err = new FeishuBackoffError(99991403); |
| 105 | + expect(isFeishuBackoffError(err)).toBe(true); |
| 106 | + }); |
| 107 | + |
| 108 | + it("is detected for rate limit code 99991400", () => { |
| 109 | + const err = new FeishuBackoffError(99991400); |
| 110 | + expect(isFeishuBackoffError(err)).toBe(true); |
| 111 | + }); |
| 112 | + |
| 113 | + it("has correct name and message", () => { |
| 114 | + const err = new FeishuBackoffError(99991403); |
| 115 | + expect(err.name).toBe("FeishuBackoffError"); |
| 116 | + expect(err.message).toBe("Feishu API backoff: code 99991403"); |
| 117 | + expect(err.code).toBe(99991403); |
| 118 | + }); |
| 119 | + |
| 120 | + it("is an instance of Error", () => { |
| 121 | + const err = new FeishuBackoffError(99991403); |
| 122 | + expect(err instanceof Error).toBe(true); |
| 123 | + }); |
| 124 | + |
| 125 | + it("survives catch-and-rethrow pattern", () => { |
| 126 | + // Simulates the exact pattern in addTypingIndicator/removeTypingIndicator: |
| 127 | + // thrown inside try, caught by catch, isFeishuBackoffError must match |
| 128 | + let caught: unknown; |
| 129 | + try { |
| 130 | + try { |
| 131 | + throw new FeishuBackoffError(99991403); |
| 132 | + } catch (err) { |
| 133 | + if (isFeishuBackoffError(err)) { |
| 134 | + throw err; // re-thrown — this is the fix |
| 135 | + } |
| 136 | + // would be silently swallowed with plain Error |
| 137 | + caught = "swallowed"; |
| 138 | + } |
| 139 | + } catch (err) { |
| 140 | + caught = err; |
| 141 | + } |
| 142 | + expect(caught).toBeInstanceOf(FeishuBackoffError); |
| 143 | + }); |
| 144 | +}); |
0 commit comments