|
| 1 | +import { describe, expect, it } from "vitest"; |
| 2 | +import { |
| 3 | + FeishuBackoffError, |
| 4 | + getBackoffCodeFromResponse, |
| 5 | + isFeishuBackoffError, |
| 6 | +} from "./typing-backoff.js"; |
| 7 | + |
| 8 | +describe("isFeishuBackoffError", () => { |
| 9 | + it.each([ |
| 10 | + { name: "HTTP 429 with data", error: { response: { status: 429, data: {} } }, expected: true }, |
| 11 | + { |
| 12 | + name: "Feishu quota response", |
| 13 | + error: { response: { status: 200, data: { code: 99991403 } } }, |
| 14 | + expected: true, |
| 15 | + }, |
| 16 | + { |
| 17 | + name: "Feishu rate-limit response", |
| 18 | + error: { response: { status: 200, data: { code: 99991400 } } }, |
| 19 | + expected: true, |
| 20 | + }, |
| 21 | + { name: "SDK HTTP code", error: { code: 429, message: "too many requests" }, expected: true }, |
| 22 | + { |
| 23 | + name: "SDK quota code", |
| 24 | + error: { code: 99991403, message: "quota exceeded" }, |
| 25 | + expected: true, |
| 26 | + }, |
| 27 | + { name: "other HTTP error", error: { response: { status: 500, data: {} } }, expected: false }, |
| 28 | + { |
| 29 | + name: "other Feishu response code", |
| 30 | + error: { response: { status: 200, data: { code: 99991401 } } }, |
| 31 | + expected: false, |
| 32 | + }, |
| 33 | + { name: "generic Error", error: new Error("network timeout"), expected: false }, |
| 34 | + { name: "null", error: null, expected: false }, |
| 35 | + { name: "undefined", error: undefined, expected: false }, |
| 36 | + { name: "string code", error: "429", expected: false }, |
| 37 | + { name: "HTTP 429 without data", error: { response: { status: 429 } }, expected: true }, |
| 38 | + ])("classifies $name", ({ error, expected }) => { |
| 39 | + expect(isFeishuBackoffError(error)).toBe(expected); |
| 40 | + }); |
| 41 | +}); |
| 42 | + |
| 43 | +describe("getBackoffCodeFromResponse", () => { |
| 44 | + it.each([ |
| 45 | + { |
| 46 | + name: "quota exceeded", |
| 47 | + response: { code: 99991403, msg: "quota exceeded", data: null }, |
| 48 | + expected: 99991403, |
| 49 | + }, |
| 50 | + { |
| 51 | + name: "rate limit", |
| 52 | + response: { code: 99991400, msg: "rate limit", data: null }, |
| 53 | + expected: 99991400, |
| 54 | + }, |
| 55 | + { |
| 56 | + name: "HTTP rate limit", |
| 57 | + response: { code: 429, msg: "too many requests", data: null }, |
| 58 | + expected: 429, |
| 59 | + }, |
| 60 | + { |
| 61 | + name: "success", |
| 62 | + response: { code: 0, msg: "success", data: { reaction_id: "r1" } }, |
| 63 | + expected: undefined, |
| 64 | + }, |
| 65 | + { |
| 66 | + name: "other error", |
| 67 | + response: { code: 99991401, msg: "other error", data: null }, |
| 68 | + expected: undefined, |
| 69 | + }, |
| 70 | + { name: "null", response: null, expected: undefined }, |
| 71 | + { name: "undefined", response: undefined, expected: undefined }, |
| 72 | + { name: "missing code", response: { data: { reaction_id: "r1" } }, expected: undefined }, |
| 73 | + ])("reads the backoff code from $name", ({ response, expected }) => { |
| 74 | + expect(getBackoffCodeFromResponse(response)).toBe(expected); |
| 75 | + }); |
| 76 | +}); |
| 77 | + |
| 78 | +describe("FeishuBackoffError", () => { |
| 79 | + it.each([ |
| 80 | + { code: 99991403, message: "Feishu API backoff: code 99991403" }, |
| 81 | + { code: 99991400, message: "Feishu API backoff: code 99991400" }, |
| 82 | + ])("preserves the backoff error contract for $code", ({ code, message }) => { |
| 83 | + const error = new FeishuBackoffError(code); |
| 84 | + expect(isFeishuBackoffError(error)).toBe(true); |
| 85 | + expect(error).toBeInstanceOf(Error); |
| 86 | + expect(error.name).toBe("FeishuBackoffError"); |
| 87 | + expect(error.message).toBe(message); |
| 88 | + expect(error.code).toBe(code); |
| 89 | + }); |
| 90 | +}); |
0 commit comments