|
| 1 | +import { describe, expect, test } from "bun:test" |
| 2 | +import { APICallError } from "ai" |
| 3 | +import { SessionV1 } from "@opencode-ai/core/v1/session" |
| 4 | +import { ProviderV2 } from "@opencode-ai/core/provider" |
| 5 | +import { MessageV2 } from "../../src/session/message-v2" |
| 6 | +import { ProviderError } from "../../src/provider/error" |
| 7 | + |
| 8 | +// Fork contract tests for task 2.2 (upstream anomalyco/opencode#53107): |
| 9 | +// an HTTP 413 payload rejection MUST classify as context overflow so the |
| 10 | +// run loop takes the overflow-compaction recovery path instead of bricking |
| 11 | +// the session. Live-gateway loop verification is deferred to task 7.1. |
| 12 | + |
| 13 | +const providerID = ProviderV2.ID.make("test") |
| 14 | + |
| 15 | +function apiCallError(statusCode: number, message: string) { |
| 16 | + return new APICallError({ |
| 17 | + message, |
| 18 | + url: "https://example.com", |
| 19 | + requestBodyValues: {}, |
| 20 | + statusCode, |
| 21 | + responseHeaders: { "content-type": "application/json" }, |
| 22 | + isRetryable: false, |
| 23 | + }) |
| 24 | +} |
| 25 | + |
| 26 | +describe("payload rejection recovery contract", () => { |
| 27 | + test("413 with a non-overflow message still classifies as context overflow", () => { |
| 28 | + // "Payload Too Large" matches none of the message-text patterns: only the |
| 29 | + // statusCode === 413 branch can classify this. |
| 30 | + const parsed = ProviderError.parseAPICallError({ |
| 31 | + providerID, |
| 32 | + error: apiCallError(413, "Payload Too Large"), |
| 33 | + }) |
| 34 | + expect(parsed.type).toBe("context_overflow") |
| 35 | + }) |
| 36 | + |
| 37 | + test("413 surfaces as ContextOverflowError at the session boundary", () => { |
| 38 | + const result = MessageV2.fromError(apiCallError(413, "Request Entity Too Large"), { providerID }) |
| 39 | + expect(SessionV1.ContextOverflowError.isInstance(result)).toBe(true) |
| 40 | + }) |
| 41 | + |
| 42 | + test("same shape without 413 is not context overflow", () => { |
| 43 | + const result = MessageV2.fromError(apiCallError(500, "Internal Server Error"), { providerID }) |
| 44 | + expect(SessionV1.ContextOverflowError.isInstance(result)).toBe(false) |
| 45 | + expect(SessionV1.APIError.isInstance(result)).toBe(true) |
| 46 | + }) |
| 47 | +}) |
0 commit comments