Files
oh-my-opencode/src/shared/model-error-classifier.test.ts
T
YeonGyu-Kim c52abe88f1 fix(tests): fix test isolation for cache-dependent tests
- Mock getOmoOpenCodeCacheDir to use temp directories

- Clear real cache files in beforeEach to prevent pollution

- Add top-level beforeEach/afterEach in model-availability.test.ts

- Use mock.module for proper test isolation

- Fixes model-error-classifier, model-availability, connected-providers-cache
2026-03-11 19:42:46 +09:00

86 lines
2.4 KiB
TypeScript

declare const require: (name: string) => any
const { describe, expect, test, beforeEach, mock } = require("bun:test")
const readConnectedProvidersCacheMock = mock(() => null)
mock.module("./connected-providers-cache", () => ({
readConnectedProvidersCache: readConnectedProvidersCacheMock,
}))
import { shouldRetryError, selectFallbackProvider } from "./model-error-classifier"
describe("model-error-classifier", () => {
beforeEach(() => {
readConnectedProvidersCacheMock.mockReturnValue(null)
readConnectedProvidersCacheMock.mockClear()
})
test("treats overloaded retry messages as retryable", () => {
//#given
const error = { message: "Provider is overloaded" }
//#when
const result = shouldRetryError(error)
//#then
expect(result).toBe(true)
})
test("treats cooling-down auto-retry messages as retryable", () => {
//#given
const error = {
message:
"All credentials for model claude-opus-4-6-thinking are cooling down [retrying in ~5 days attempt #1]",
}
//#when
const result = shouldRetryError(error)
//#then
expect(result).toBe(true)
})
test("selectFallbackProvider prefers first connected provider in preference order", () => {
//#given
readConnectedProvidersCacheMock.mockReturnValue(["anthropic", "nvidia"])
//#when
const provider = selectFallbackProvider(["anthropic", "nvidia"], "nvidia")
//#then
expect(provider).toBe("anthropic")
})
test("selectFallbackProvider falls back to next connected provider when first is disconnected", () => {
//#given
readConnectedProvidersCacheMock.mockReturnValue(["nvidia"])
//#when
const provider = selectFallbackProvider(["anthropic", "nvidia"])
//#then
expect(provider).toBe("nvidia")
})
test("selectFallbackProvider uses provider preference order when cache is missing", () => {
//#given - no cache file
//#when
const provider = selectFallbackProvider(["anthropic", "nvidia"], "nvidia")
//#then
expect(provider).toBe("anthropic")
})
test("selectFallbackProvider uses connected preferred provider when fallback providers are unavailable", () => {
//#given
readConnectedProvidersCacheMock.mockReturnValue(["provider-x"])
//#when
const provider = selectFallbackProvider(["provider-y"], "provider-x")
//#then
expect(provider).toBe("provider-x")
})
})