|
| 1 | +import { describe, expect, it } from "vitest"; |
| 2 | + |
| 3 | +import { createOpenCodeEventNormalizer } from "../src/opencode-events.js"; |
| 4 | +import type { NormalizedRuntimeEvent } from "../src/codex-events.js"; |
| 5 | +import type { RuntimeProcessCompletion } from "../src/process-runner.js"; |
| 6 | + |
| 7 | +/* |
| 8 | + * 2026-10-12, 15:04Z: what OpenCode 1.18.27 wrote for `run --format json -m opencode/space-bunny-free`, while every |
| 9 | + * one of its free models answered so, for everyone (anomalyco/opencode#54833) -- trimmed only of the response |
| 10 | + * headers. Its server path hands the same record on (opencode-serve-run.ts, session.error). The card said "Its |
| 11 | + * sign-in may have expired -- `opencode auth login` in a terminal" to people who never signed in to anything. |
| 12 | + */ |
| 13 | +const MEASURED = JSON.stringify({ |
| 14 | + type: "error", |
| 15 | + timestamp: 1791817472158, |
| 16 | + sessionID: "ses_ecf5c461affe7hz3Cz49cHA0hJ", |
| 17 | + error: { |
| 18 | + name: "APIError", |
| 19 | + data: { |
| 20 | + message: "Upstream request failed: Invalid credential", |
| 21 | + statusCode: 401, |
| 22 | + isRetryable: false, |
| 23 | + responseBody: JSON.stringify({ error: { type: "server_error", message: "Upstream request failed: Invalid credential" } }), |
| 24 | + metadata: { url: "https://opencode.ai/inference/v1/chat/completions" }, |
| 25 | + }, |
| 26 | + }, |
| 27 | +}); |
| 28 | + |
| 29 | +const NOW = "2026-10-12T15:04:32.000Z"; |
| 30 | +const completion: RuntimeProcessCompletion = { |
| 31 | + exitCode: 1, |
| 32 | + signal: null, |
| 33 | + stderr: "", |
| 34 | + stderrTruncated: false, |
| 35 | + recordCount: 1, |
| 36 | + cancelled: false, |
| 37 | + forcedTerminationAttempted: false, |
| 38 | + terminationUnconfirmed: false, |
| 39 | + inputDeliveryFailed: false, |
| 40 | + outputLimitExceeded: false, |
| 41 | + startedAt: NOW, |
| 42 | + finishedAt: NOW, |
| 43 | +}; |
| 44 | + |
| 45 | +function said(model: string): { readonly diagnostic: string | undefined; readonly failed: string | undefined } { |
| 46 | + const opencode = createOpenCodeEventNormalizer({ runId: "run_1", model, now: () => new Date(NOW) }); |
| 47 | + const events: NormalizedRuntimeEvent[] = [...opencode.accept({ sequence: 1, raw: MEASURED }), ...opencode.finish(completion)]; |
| 48 | + const message = (type: string): string | undefined => { |
| 49 | + const event = events.find((one) => one.type === type); |
| 50 | + const payload = event === undefined ? undefined : (event.payload as { readonly message?: unknown }); |
| 51 | + return typeof payload?.message === "string" ? payload.message : undefined; |
| 52 | + }; |
| 53 | + return { diagnostic: message("adapter.diagnostic"), failed: message("run.failed") }; |
| 54 | +} |
| 55 | + |
| 56 | +describe("a free model OpenCode's service refused", () => { |
| 57 | + it("says so, as OpenCode's side, while it happens and when the run ends -- never a sign-in", () => { |
| 58 | + const { diagnostic, failed } = said("opencode/space-bunny-free"); |
| 59 | + const sentence = "OpenCode's service refused the free model, so OpenCode stopped: Upstream request failed: Invalid credential. Free models need no sign-in, so this is on OpenCode's side: pick another model, or try this one again later."; |
| 60 | + expect(diagnostic).toBe(sentence); |
| 61 | + expect(failed).toBe(sentence); |
| 62 | + }); |
| 63 | + |
| 64 | + it("while a model of the person's own, refused the same way, is still pointed at its sign-in", () => { |
| 65 | + const { failed } = said("anthropic/claude-sonnet-5-5"); |
| 66 | + expect(failed).toMatch(/Its sign-in may have expired -- `opencode auth login` in a terminal\.$/); |
| 67 | + }); |
| 68 | +}); |
0 commit comments