|
| 1 | +// npx vitest src/utils/__tests__/reasoning-effort.spec.ts |
| 2 | + |
| 3 | +import { describe, expect, it } from "vitest" |
| 4 | + |
| 5 | +import type { ModelInfo, ProviderSettings } from "@roo-code/types" |
| 6 | + |
| 7 | +import { getReasoningEffortSelection, normalizeReasoningEffortOnModelChange } from "@src/utils/reasoning-effort" |
| 8 | + |
| 9 | +// Capability arrays the fetcher advertises per model family (see |
| 10 | +// src/api/providers/fetchers/ollama.ts getOllamaThinkingEfforts). gpt-oss |
| 11 | +// ignores think: false and accepts only low/medium/high, so its array omits |
| 12 | +// "disable" and "max" — unlike disable-capable thinking models (e.g. qwen3 → |
| 13 | +// ["disable","low","medium","high","max"]). |
| 14 | +const gptOssModel: ModelInfo = { |
| 15 | + contextWindow: 131072, |
| 16 | + supportsPromptCache: true, |
| 17 | + supportsReasoningEffort: ["low", "medium", "high"], |
| 18 | + reasoningEffort: "medium", |
| 19 | +} |
| 20 | + |
| 21 | +describe("normalizeReasoningEffortOnModelChange", () => { |
| 22 | + it("returns undefined when no stored value is set (fresh profile)", () => { |
| 23 | + const selection = getReasoningEffortSelection(undefined, gptOssModel) |
| 24 | + expect(normalizeReasoningEffortOnModelChange(selection)).toBeUndefined() |
| 25 | + }) |
| 26 | + |
| 27 | + it("returns undefined when the stored value is still a valid option", () => { |
| 28 | + const apiConfiguration = { reasoningEffort: "medium" } as ProviderSettings |
| 29 | + const selection = getReasoningEffortSelection(apiConfiguration, gptOssModel) |
| 30 | + expect(normalizeReasoningEffortOnModelChange(selection)).toBeUndefined() |
| 31 | + }) |
| 32 | + |
| 33 | + it("persists the clamped fallback when switching from a disable-capable model to gpt-oss with reasoningEffort: disable", () => { |
| 34 | + // Regression: the user saved "disable" on qwen3 (which honors think: |
| 35 | + // false), then switched to gpt-oss (which omits "disable" from its |
| 36 | + // capability array). getReasoningEffortSelection clamps the *displayed* |
| 37 | + // effort to the fallback (gpt-oss's first option "low", since gpt-oss is |
| 38 | + // not required-reasoning so the default is "disable" which is itself |
| 39 | + // invalid, falling through to availableOptions[0] = "low"), but without |
| 40 | + // normalization the *stored* value stays "disable" — so the native |
| 41 | + // request mapper would send think: false while the UI shows "Low". The |
| 42 | + // helper returns the clamped value to persist so the stored effort, the |
| 43 | + // displayed effort, and the request stay in sync. |
| 44 | + const apiConfiguration = { reasoningEffort: "disable" } as ProviderSettings |
| 45 | + const selection = getReasoningEffortSelection(apiConfiguration, gptOssModel) |
| 46 | + |
| 47 | + // Sanity: the display clamped away from "disable"... |
| 48 | + expect(selection.currentReasoningEffort).not.toBe("disable") |
| 49 | + // ...and the stored value is no longer valid for gpt-oss's array. |
| 50 | + expect(selection.storedReasoningEffort).toBe("disable") |
| 51 | + expect(gptOssModel.supportsReasoningEffort).not.toContain("disable") |
| 52 | + |
| 53 | + // The helper returns the clamped fallback ("low") to persist. |
| 54 | + expect(normalizeReasoningEffortOnModelChange(selection)).toBe(selection.currentReasoningEffort) |
| 55 | + expect(normalizeReasoningEffortOnModelChange(selection)).toBe("low") |
| 56 | + }) |
| 57 | + |
| 58 | + it("clamps 'max' to the fallback when switching from a max-capable model to gpt-oss", () => { |
| 59 | + // Symmetric case: "max" is valid for qwen3 but not gpt-oss. Switching |
| 60 | + // models should normalize the stored "max" to gpt-oss's first option. |
| 61 | + const apiConfiguration = { reasoningEffort: "max" } as ProviderSettings |
| 62 | + const selection = getReasoningEffortSelection(apiConfiguration, gptOssModel) |
| 63 | + |
| 64 | + expect(normalizeReasoningEffortOnModelChange(selection)).toBe("low") |
| 65 | + }) |
| 66 | + |
| 67 | + it("does not normalize when switching back to a model that still supports the stored value", () => { |
| 68 | + // "low" is valid for both qwen3 and gpt-oss, so switching between them |
| 69 | + // must not trigger a write. |
| 70 | + const apiConfiguration = { reasoningEffort: "low" } as ProviderSettings |
| 71 | + const selection = getReasoningEffortSelection(apiConfiguration, gptOssModel) |
| 72 | + expect(normalizeReasoningEffortOnModelChange(selection)).toBeUndefined() |
| 73 | + }) |
| 74 | +}) |
0 commit comments