import { test } from "node:test"; import assert from "node:assert/strict"; import { ContextTooLargeError, isContextLengthError, normFinish, } from "./askProvider"; test("normFinish maps each provider's raw finish string to a normalized reason", () => { // length (truncation): Anthropic max_tokens, OpenAI length, Gemini MAX_TOKENS assert.equal(normFinish("max_tokens"), "length"); assert.equal(normFinish("length"), "length"); assert.equal(normFinish("MAX_TOKENS"), "length"); // safety / blocked assert.equal(normFinish("content_filter"), "safety"); assert.equal(normFinish("SAFETY"), "safety"); assert.equal(normFinish("RECITATION"), "safety"); // normal completion assert.equal(normFinish("end_turn"), "stop"); assert.equal(normFinish("stop"), "stop"); assert.equal(normFinish("STOP"), "stop"); assert.equal(normFinish("tool_use"), "stop"); // unknown assert.equal(normFinish("something_else"), "other"); }); test("isContextLengthError detects provider context-length phrasings", () => { // Anthropic assert.equal( isContextLengthError( '{"type":"error","error":{"type":"invalid_request_error","message":"prompt is too long: 250000 tokens > 200000 maximum"}}', ), true, ); // OpenAI assert.equal( isContextLengthError( '{"error":{"message":"This model\'s maximum context length is 128000 tokens.","code":"context_length_exceeded"}}', ), true, ); // Gemini assert.equal( isContextLengthError( '{"error":{"code":400,"message":"The input token count (1200000) exceeds the maximum number of tokens allowed"}}', ), true, ); }); test("isContextLengthError ignores unrelated 400s", () => { assert.equal( isContextLengthError('{"error":{"message":"invalid api key"}}'), false, ); assert.equal( isContextLengthError('{"error":{"message":"tools are not supported by this model"}}'), false, ); assert.equal(isContextLengthError(""), false); }); test("ContextTooLargeError carries an actionable message and stable name", () => { const e = new ContextTooLargeError("anthropic request failed (400)."); assert.equal(e.name, "ContextTooLargeError"); assert.ok(e instanceof Error); assert.match(e.message, /too large/i); assert.match(e.message, /new chat/i); });