Files
oh-my-opencode/src/cli/model-fallback.test.ts
T
YeonGyu-Kim f146aeff0f refactor: major codebase cleanup - BDD comments, file splitting, bug fixes (#1350)
* style(tests): normalize BDD comments from '// #given' to '// given'

- Replace 4,668 Python-style BDD comments across 107 test files
- Patterns changed: // #given -> // given, // #when -> // when, // #then -> // then
- Also handles no-space variants: //#given -> // given

* fix(rules-injector): prefer output.metadata.filePath over output.title

- Extract file path resolution to dedicated output-path.ts module
- Prefer metadata.filePath which contains actual file path
- Fall back to output.title only when metadata unavailable
- Fixes issue where rules weren't injected when tool output title was a label

* feat(slashcommand): add optional user_message parameter

- Add user_message optional parameter for command arguments
- Model can now call: command='publish' user_message='patch'
- Improves error messages with clearer format guidance
- Helps LLMs understand correct parameter usage

* feat(hooks): restore compaction-context-injector hook

- Restore hook deleted in cbbc7bd0 for session compaction context
- Injects 7 mandatory sections: User Requests, Final Goal, Work Completed,
  Remaining Tasks, Active Working Context, MUST NOT Do, Agent Verification State
- Re-register in hooks/index.ts and main plugin entry

* refactor(background-agent): split manager.ts into focused modules

- Extract constants.ts for TTL values and internal types (52 lines)
- Extract state.ts for TaskStateManager class (204 lines)
- Extract spawner.ts for task creation logic (244 lines)
- Extract result-handler.ts for completion handling (265 lines)
- Reduce manager.ts from 1377 to 755 lines (45% reduction)
- Maintain backward compatible exports

* refactor(agents): split prometheus-prompt.ts into subdirectory

- Move 1196-line prometheus-prompt.ts to prometheus/ subdirectory
- Organize prompt sections into separate files for maintainability
- Update agents/index.ts exports

* refactor(delegate-task): split tools.ts into focused modules

- Extract categories.ts for category definitions and routing
- Extract executor.ts for task execution logic
- Extract helpers.ts for utility functions
- Extract prompt-builder.ts for prompt construction
- Reduce tools.ts complexity with cleaner separation of concerns

* refactor(builtin-skills): split skills.ts into individual skill files

- Move each skill to dedicated file in skills/ subdirectory
- Create barrel export for backward compatibility
- Improve maintainability with focused skill modules

* chore: update import paths and lockfile

- Update prometheus import path after refactor
- Update bun.lock

* fix(tests): complete BDD comment normalization

- Fix remaining #when/#then patterns missed by initial sed
- Affected: state.test.ts, events.test.ts

---------

Co-authored-by: justsisyphus <justsisyphus@users.noreply.github.com>
2026-02-01 16:47:50 +09:00

436 lines
14 KiB
TypeScript

import { describe, expect, test } from "bun:test"
import { generateModelConfig } from "./model-fallback"
import type { InstallConfig } from "./types"
function createConfig(overrides: Partial<InstallConfig> = {}): InstallConfig {
return {
hasClaude: false,
isMax20: false,
hasOpenAI: false,
hasGemini: false,
hasCopilot: false,
hasOpencodeZen: false,
hasZaiCodingPlan: false,
hasKimiForCoding: false,
...overrides,
}
}
describe("generateModelConfig", () => {
describe("no providers available", () => {
test("returns ULTIMATE_FALLBACK for all agents and categories when no providers", () => {
// given no providers are available
const config = createConfig()
// when generateModelConfig is called
const result = generateModelConfig(config)
// then should use ULTIMATE_FALLBACK for everything
expect(result).toMatchSnapshot()
})
})
describe("single native provider", () => {
test("uses Claude models when only Claude is available", () => {
// given only Claude is available
const config = createConfig({ hasClaude: true })
// when generateModelConfig is called
const result = generateModelConfig(config)
// then should use Claude models per NATIVE_FALLBACK_CHAINS
expect(result).toMatchSnapshot()
})
test("uses Claude models with isMax20 flag", () => {
// given Claude is available with Max 20 plan
const config = createConfig({ hasClaude: true, isMax20: true })
// when generateModelConfig is called
const result = generateModelConfig(config)
// then should use higher capability models for Sisyphus
expect(result).toMatchSnapshot()
})
test("uses OpenAI models when only OpenAI is available", () => {
// given only OpenAI is available
const config = createConfig({ hasOpenAI: true })
// when generateModelConfig is called
const result = generateModelConfig(config)
// then should use OpenAI models
expect(result).toMatchSnapshot()
})
test("uses OpenAI models with isMax20 flag", () => {
// given OpenAI is available with Max 20 plan
const config = createConfig({ hasOpenAI: true, isMax20: true })
// when generateModelConfig is called
const result = generateModelConfig(config)
// then should use higher capability models
expect(result).toMatchSnapshot()
})
test("uses Gemini models when only Gemini is available", () => {
// given only Gemini is available
const config = createConfig({ hasGemini: true })
// when generateModelConfig is called
const result = generateModelConfig(config)
// then should use Gemini models
expect(result).toMatchSnapshot()
})
test("uses Gemini models with isMax20 flag", () => {
// given Gemini is available with Max 20 plan
const config = createConfig({ hasGemini: true, isMax20: true })
// when generateModelConfig is called
const result = generateModelConfig(config)
// then should use higher capability models
expect(result).toMatchSnapshot()
})
})
describe("all native providers", () => {
test("uses preferred models from fallback chains when all natives available", () => {
// given all native providers are available
const config = createConfig({
hasClaude: true,
hasOpenAI: true,
hasGemini: true,
})
// when generateModelConfig is called
const result = generateModelConfig(config)
// then should use first provider in each fallback chain
expect(result).toMatchSnapshot()
})
test("uses preferred models with isMax20 flag when all natives available", () => {
// given all native providers are available with Max 20 plan
const config = createConfig({
hasClaude: true,
hasOpenAI: true,
hasGemini: true,
isMax20: true,
})
// when generateModelConfig is called
const result = generateModelConfig(config)
// then should use higher capability models
expect(result).toMatchSnapshot()
})
})
describe("fallback providers", () => {
test("uses OpenCode Zen models when only OpenCode Zen is available", () => {
// given only OpenCode Zen is available
const config = createConfig({ hasOpencodeZen: true })
// when generateModelConfig is called
const result = generateModelConfig(config)
// then should use OPENCODE_ZEN_MODELS
expect(result).toMatchSnapshot()
})
test("uses OpenCode Zen models with isMax20 flag", () => {
// given OpenCode Zen is available with Max 20 plan
const config = createConfig({ hasOpencodeZen: true, isMax20: true })
// when generateModelConfig is called
const result = generateModelConfig(config)
// then should use higher capability models
expect(result).toMatchSnapshot()
})
test("uses GitHub Copilot models when only Copilot is available", () => {
// given only GitHub Copilot is available
const config = createConfig({ hasCopilot: true })
// when generateModelConfig is called
const result = generateModelConfig(config)
// then should use GITHUB_COPILOT_MODELS
expect(result).toMatchSnapshot()
})
test("uses GitHub Copilot models with isMax20 flag", () => {
// given GitHub Copilot is available with Max 20 plan
const config = createConfig({ hasCopilot: true, isMax20: true })
// when generateModelConfig is called
const result = generateModelConfig(config)
// then should use higher capability models
expect(result).toMatchSnapshot()
})
test("uses ZAI model for librarian when only ZAI is available", () => {
// given only ZAI is available
const config = createConfig({ hasZaiCodingPlan: true })
// when generateModelConfig is called
const result = generateModelConfig(config)
// then should use ZAI_MODEL for librarian
expect(result).toMatchSnapshot()
})
test("uses ZAI model for librarian with isMax20 flag", () => {
// given ZAI is available with Max 20 plan
const config = createConfig({ hasZaiCodingPlan: true, isMax20: true })
// when generateModelConfig is called
const result = generateModelConfig(config)
// then should use ZAI_MODEL for librarian
expect(result).toMatchSnapshot()
})
})
describe("mixed provider scenarios", () => {
test("uses Claude + OpenCode Zen combination", () => {
// given Claude and OpenCode Zen are available
const config = createConfig({
hasClaude: true,
hasOpencodeZen: true,
})
// when generateModelConfig is called
const result = generateModelConfig(config)
// then should prefer Claude (native) over OpenCode Zen
expect(result).toMatchSnapshot()
})
test("uses OpenAI + Copilot combination", () => {
// given OpenAI and Copilot are available
const config = createConfig({
hasOpenAI: true,
hasCopilot: true,
})
// when generateModelConfig is called
const result = generateModelConfig(config)
// then should prefer OpenAI (native) over Copilot
expect(result).toMatchSnapshot()
})
test("uses Claude + ZAI combination (librarian uses ZAI)", () => {
// given Claude and ZAI are available
const config = createConfig({
hasClaude: true,
hasZaiCodingPlan: true,
})
// when generateModelConfig is called
const result = generateModelConfig(config)
// then librarian should use ZAI, others use Claude
expect(result).toMatchSnapshot()
})
test("uses Gemini + Claude combination (explore uses Gemini)", () => {
// given Gemini and Claude are available
const config = createConfig({
hasGemini: true,
hasClaude: true,
})
// when generateModelConfig is called
const result = generateModelConfig(config)
// then explore should use Gemini flash
expect(result).toMatchSnapshot()
})
test("uses all fallback providers together", () => {
// given all fallback providers are available
const config = createConfig({
hasOpencodeZen: true,
hasCopilot: true,
hasZaiCodingPlan: true,
})
// when generateModelConfig is called
const result = generateModelConfig(config)
// then should prefer OpenCode Zen, but librarian uses ZAI
expect(result).toMatchSnapshot()
})
test("uses all providers together", () => {
// given all providers are available
const config = createConfig({
hasClaude: true,
hasOpenAI: true,
hasGemini: true,
hasOpencodeZen: true,
hasCopilot: true,
hasZaiCodingPlan: true,
})
// when generateModelConfig is called
const result = generateModelConfig(config)
// then should prefer native providers, librarian uses ZAI
expect(result).toMatchSnapshot()
})
test("uses all providers with isMax20 flag", () => {
// given all providers are available with Max 20 plan
const config = createConfig({
hasClaude: true,
hasOpenAI: true,
hasGemini: true,
hasOpencodeZen: true,
hasCopilot: true,
hasZaiCodingPlan: true,
isMax20: true,
})
// when generateModelConfig is called
const result = generateModelConfig(config)
// then should use higher capability models
expect(result).toMatchSnapshot()
})
})
describe("explore agent special cases", () => {
test("explore uses gpt-5-nano when only Gemini available (no Claude)", () => {
// given only Gemini is available (no Claude)
const config = createConfig({ hasGemini: true })
// when generateModelConfig is called
const result = generateModelConfig(config)
// then explore should use gpt-5-nano (Claude haiku not available)
expect(result.agents?.explore?.model).toBe("opencode/gpt-5-nano")
})
test("explore uses Claude haiku when Claude available", () => {
// given Claude is available
const config = createConfig({ hasClaude: true, isMax20: true })
// when generateModelConfig is called
const result = generateModelConfig(config)
// then explore should use claude-haiku-4-5
expect(result.agents?.explore?.model).toBe("anthropic/claude-haiku-4-5")
})
test("explore uses Claude haiku regardless of isMax20 flag", () => {
// given Claude is available without Max 20 plan
const config = createConfig({ hasClaude: true, isMax20: false })
// when generateModelConfig is called
const result = generateModelConfig(config)
// then explore should use claude-haiku-4-5 (isMax20 doesn't affect explore)
expect(result.agents?.explore?.model).toBe("anthropic/claude-haiku-4-5")
})
test("explore uses gpt-5-nano when only OpenAI available", () => {
// given only OpenAI is available
const config = createConfig({ hasOpenAI: true })
// when generateModelConfig is called
const result = generateModelConfig(config)
// then explore should use gpt-5-nano (fallback)
expect(result.agents?.explore?.model).toBe("opencode/gpt-5-nano")
})
test("explore uses gpt-5-mini when only Copilot available", () => {
// given only Copilot is available
const config = createConfig({ hasCopilot: true })
// when generateModelConfig is called
const result = generateModelConfig(config)
// then explore should use gpt-5-mini (Copilot fallback)
expect(result.agents?.explore?.model).toBe("github-copilot/gpt-5-mini")
})
})
describe("Sisyphus agent special cases", () => {
test("Sisyphus uses sisyphus-high capability when isMax20 is true", () => {
// given Claude is available with Max 20 plan
const config = createConfig({ hasClaude: true, isMax20: true })
// when generateModelConfig is called
const result = generateModelConfig(config)
// then Sisyphus should use opus (sisyphus-high)
expect(result.agents?.sisyphus?.model).toBe("anthropic/claude-opus-4-5")
})
test("Sisyphus uses sisyphus-low capability when isMax20 is false", () => {
// given Claude is available without Max 20 plan
const config = createConfig({ hasClaude: true, isMax20: false })
// when generateModelConfig is called
const result = generateModelConfig(config)
// then Sisyphus should use sonnet (sisyphus-low)
expect(result.agents?.sisyphus?.model).toBe("anthropic/claude-sonnet-4-5")
})
})
describe("librarian agent special cases", () => {
test("librarian uses ZAI when ZAI is available regardless of other providers", () => {
// given ZAI and Claude are available
const config = createConfig({
hasClaude: true,
hasZaiCodingPlan: true,
})
// when generateModelConfig is called
const result = generateModelConfig(config)
// then librarian should use ZAI_MODEL
expect(result.agents?.librarian?.model).toBe("zai-coding-plan/glm-4.7")
})
test("librarian uses claude-sonnet when ZAI not available but Claude is", () => {
// given only Claude is available (no ZAI)
const config = createConfig({ hasClaude: true })
// when generateModelConfig is called
const result = generateModelConfig(config)
// then librarian should use claude-sonnet-4-5 (third in fallback chain after ZAI and opencode/glm)
expect(result.agents?.librarian?.model).toBe("anthropic/claude-sonnet-4-5")
})
})
describe("schema URL", () => {
test("always includes correct schema URL", () => {
// given any config
const config = createConfig()
// when generateModelConfig is called
const result = generateModelConfig(config)
// then should include correct schema URL
expect(result.$schema).toBe(
"https://raw.githubusercontent.com/code-yeongyu/oh-my-opencode/master/assets/oh-my-opencode.schema.json"
)
})
})
})