|
| 1 | +import { describe, it, expect, vi, beforeEach } from "vitest"; |
| 2 | +import { |
| 3 | + withTokenCap, |
| 4 | + isMaxTokensUnsupported, |
| 5 | + resetTokenCapCache, |
| 6 | +} from "./token-cap.js"; |
| 7 | + |
| 8 | +/** The shape the OpenAI SDK throws for GPT-5 / o-series when sent `max_tokens`. */ |
| 9 | +function unsupportedParamError() { |
| 10 | + return Object.assign( |
| 11 | + new Error( |
| 12 | + "400 Unsupported parameter: 'max_tokens' is not supported with this model. Use 'max_completion_tokens' instead." |
| 13 | + ), |
| 14 | + { |
| 15 | + status: 400, |
| 16 | + param: "max_tokens", |
| 17 | + code: "unsupported_parameter", |
| 18 | + type: "invalid_request_error", |
| 19 | + } |
| 20 | + ); |
| 21 | +} |
| 22 | + |
| 23 | +describe("isMaxTokensUnsupported", () => { |
| 24 | + it("recognises the structured OpenAI rejection", () => { |
| 25 | + expect(isMaxTokensUnsupported(unsupportedParamError())).toBe(true); |
| 26 | + }); |
| 27 | + |
| 28 | + it("recognises a proxy that keeps the message but drops the fields", () => { |
| 29 | + const err = Object.assign(new Error("Use 'max_completion_tokens' instead."), { |
| 30 | + status: 400, |
| 31 | + }); |
| 32 | + expect(isMaxTokensUnsupported(err)).toBe(true); |
| 33 | + }); |
| 34 | + |
| 35 | + it("ignores unrelated 400s so they surface to the user", () => { |
| 36 | + const err = Object.assign(new Error("model not found"), { |
| 37 | + status: 400, |
| 38 | + code: "model_not_found", |
| 39 | + }); |
| 40 | + expect(isMaxTokensUnsupported(err)).toBe(false); |
| 41 | + }); |
| 42 | + |
| 43 | + it("ignores non-400 failures", () => { |
| 44 | + const err = Object.assign(new Error("rate limited"), { |
| 45 | + status: 429, |
| 46 | + param: "max_tokens", |
| 47 | + code: "unsupported_parameter", |
| 48 | + }); |
| 49 | + expect(isMaxTokensUnsupported(err)).toBe(false); |
| 50 | + }); |
| 51 | + |
| 52 | + it("ignores non-objects", () => { |
| 53 | + expect(isMaxTokensUnsupported("boom")).toBe(false); |
| 54 | + expect(isMaxTokensUnsupported(null)).toBe(false); |
| 55 | + }); |
| 56 | +}); |
| 57 | + |
| 58 | +describe("withTokenCap", () => { |
| 59 | + beforeEach(() => { |
| 60 | + resetTokenCapCache(); |
| 61 | + }); |
| 62 | + |
| 63 | + it("sends max_tokens and does not retry when the endpoint accepts it", async () => { |
| 64 | + const send = vi.fn().mockResolvedValue("ok"); |
| 65 | + |
| 66 | + await expect(withTokenCap("gpt-4o-mini", 2048, send)).resolves.toBe("ok"); |
| 67 | + |
| 68 | + expect(send).toHaveBeenCalledTimes(1); |
| 69 | + expect(send).toHaveBeenCalledWith({ max_tokens: 2048 }); |
| 70 | + }); |
| 71 | + |
| 72 | + it("retries with max_completion_tokens when the model rejects max_tokens", async () => { |
| 73 | + const send = vi |
| 74 | + .fn() |
| 75 | + .mockRejectedValueOnce(unsupportedParamError()) |
| 76 | + .mockResolvedValueOnce("ok"); |
| 77 | + |
| 78 | + await expect(withTokenCap("gpt-5.6", 2048, send)).resolves.toBe("ok"); |
| 79 | + |
| 80 | + expect(send).toHaveBeenCalledTimes(2); |
| 81 | + expect(send).toHaveBeenNthCalledWith(1, { max_tokens: 2048 }); |
| 82 | + expect(send).toHaveBeenNthCalledWith(2, { max_completion_tokens: 2048 }); |
| 83 | + }); |
| 84 | + |
| 85 | + it("remembers the answer, so only the first call pays the extra round-trip", async () => { |
| 86 | + const send = vi |
| 87 | + .fn() |
| 88 | + .mockRejectedValueOnce(unsupportedParamError()) |
| 89 | + .mockResolvedValue("ok"); |
| 90 | + |
| 91 | + await withTokenCap("gpt-5.6", 500, send); |
| 92 | + send.mockClear(); |
| 93 | + await withTokenCap("gpt-5.6", 500, send); |
| 94 | + |
| 95 | + expect(send).toHaveBeenCalledTimes(1); |
| 96 | + expect(send).toHaveBeenCalledWith({ max_completion_tokens: 500 }); |
| 97 | + }); |
| 98 | + |
| 99 | + it("learns per model, not globally", async () => { |
| 100 | + const rejecting = vi |
| 101 | + .fn() |
| 102 | + .mockRejectedValueOnce(unsupportedParamError()) |
| 103 | + .mockResolvedValue("ok"); |
| 104 | + await withTokenCap("gpt-5.6", 500, rejecting); |
| 105 | + |
| 106 | + const other = vi.fn().mockResolvedValue("ok"); |
| 107 | + await withTokenCap("openai/gpt-5.6-luna", 500, other); |
| 108 | + |
| 109 | + expect(other).toHaveBeenCalledTimes(1); |
| 110 | + expect(other).toHaveBeenCalledWith({ max_tokens: 500 }); |
| 111 | + }); |
| 112 | + |
| 113 | + it("propagates unrelated errors without a retry", async () => { |
| 114 | + const err = Object.assign(new Error("model not found"), { status: 400 }); |
| 115 | + const send = vi.fn().mockRejectedValue(err); |
| 116 | + |
| 117 | + await expect(withTokenCap("nope", 500, send)).rejects.toThrow("model not found"); |
| 118 | + expect(send).toHaveBeenCalledTimes(1); |
| 119 | + }); |
| 120 | + |
| 121 | + it("does not loop when the retry itself fails", async () => { |
| 122 | + const send = vi.fn().mockRejectedValue(unsupportedParamError()); |
| 123 | + |
| 124 | + await expect(withTokenCap("gpt-5.6", 500, send)).rejects.toThrow(); |
| 125 | + expect(send).toHaveBeenCalledTimes(2); |
| 126 | + }); |
| 127 | +}); |
0 commit comments