|
| 1 | +import { describe, expect, it } from "bun:test" |
| 2 | +import { createAnthropicEffortHook } from "./index" |
| 3 | + |
| 4 | +interface ChatParamsInput { |
| 5 | + sessionID: string |
| 6 | + agent: { name?: string } |
| 7 | + model: { providerID: string; modelID: string; id?: string; api?: { npm?: string } } |
| 8 | + provider: { id: string } |
| 9 | + message: { variant?: string } |
| 10 | +} |
| 11 | + |
| 12 | +interface ChatParamsOutput { |
| 13 | + temperature?: number |
| 14 | + topP?: number |
| 15 | + topK?: number |
| 16 | + options: Record<string, unknown> |
| 17 | +} |
| 18 | + |
| 19 | +function createMockParams(overrides: { |
| 20 | + providerID?: string |
| 21 | + modelID?: string |
| 22 | + variant?: string |
| 23 | + agentName?: string |
| 24 | + existingOptions?: Record<string, unknown> |
| 25 | +}): { input: ChatParamsInput; output: ChatParamsOutput } { |
| 26 | + const providerID = overrides.providerID ?? "anthropic" |
| 27 | + const modelID = overrides.modelID ?? "claude-opus-4-6" |
| 28 | + const variant = "variant" in overrides ? overrides.variant : "max" |
| 29 | + const agentName = overrides.agentName ?? "sisyphus" |
| 30 | + const existingOptions = overrides.existingOptions ?? {} |
| 31 | + |
| 32 | + return { |
| 33 | + input: { |
| 34 | + sessionID: "test-session", |
| 35 | + agent: { name: agentName }, |
| 36 | + model: { providerID, modelID }, |
| 37 | + provider: { id: providerID }, |
| 38 | + message: { variant }, |
| 39 | + }, |
| 40 | + output: { |
| 41 | + temperature: 0.1, |
| 42 | + options: { ...existingOptions }, |
| 43 | + }, |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +describe("createAnthropicEffortHook", () => { |
| 48 | + describe("opus 4-6 with variant max", () => { |
| 49 | + it("should inject effort max for anthropic opus-4-6 with variant max", async () => { |
| 50 | + //#given anthropic opus-4-6 model with variant max |
| 51 | + const hook = createAnthropicEffortHook() |
| 52 | + const { input, output } = createMockParams({}) |
| 53 | + |
| 54 | + //#when chat.params hook is called |
| 55 | + await hook["chat.params"](input, output) |
| 56 | + |
| 57 | + //#then effort should be injected into options |
| 58 | + expect(output.options.effort).toBe("max") |
| 59 | + }) |
| 60 | + |
| 61 | + it("should inject effort max for github-copilot claude-opus-4-6", async () => { |
| 62 | + //#given github-copilot provider with claude-opus-4-6 |
| 63 | + const hook = createAnthropicEffortHook() |
| 64 | + const { input, output } = createMockParams({ |
| 65 | + providerID: "github-copilot", |
| 66 | + modelID: "claude-opus-4-6", |
| 67 | + }) |
| 68 | + |
| 69 | + //#when chat.params hook is called |
| 70 | + await hook["chat.params"](input, output) |
| 71 | + |
| 72 | + //#then effort should be injected (github-copilot resolves to anthropic) |
| 73 | + expect(output.options.effort).toBe("max") |
| 74 | + }) |
| 75 | + |
| 76 | + it("should inject effort max for opencode provider with claude-opus-4-6", async () => { |
| 77 | + //#given opencode provider with claude-opus-4-6 |
| 78 | + const hook = createAnthropicEffortHook() |
| 79 | + const { input, output } = createMockParams({ |
| 80 | + providerID: "opencode", |
| 81 | + modelID: "claude-opus-4-6", |
| 82 | + }) |
| 83 | + |
| 84 | + //#when chat.params hook is called |
| 85 | + await hook["chat.params"](input, output) |
| 86 | + |
| 87 | + //#then effort should be injected |
| 88 | + expect(output.options.effort).toBe("max") |
| 89 | + }) |
| 90 | + |
| 91 | + it("should handle normalized model ID with dots (opus-4.6)", async () => { |
| 92 | + //#given model ID with dots instead of hyphens |
| 93 | + const hook = createAnthropicEffortHook() |
| 94 | + const { input, output } = createMockParams({ |
| 95 | + modelID: "claude-opus-4.6", |
| 96 | + }) |
| 97 | + |
| 98 | + //#when chat.params hook is called |
| 99 | + await hook["chat.params"](input, output) |
| 100 | + |
| 101 | + //#then should normalize and inject effort |
| 102 | + expect(output.options.effort).toBe("max") |
| 103 | + }) |
| 104 | + }) |
| 105 | + |
| 106 | + describe("conditions NOT met - should skip", () => { |
| 107 | + it("should NOT inject effort when variant is not max", async () => { |
| 108 | + //#given opus-4-6 with variant high (not max) |
| 109 | + const hook = createAnthropicEffortHook() |
| 110 | + const { input, output } = createMockParams({ variant: "high" }) |
| 111 | + |
| 112 | + //#when chat.params hook is called |
| 113 | + await hook["chat.params"](input, output) |
| 114 | + |
| 115 | + //#then effort should NOT be injected |
| 116 | + expect(output.options.effort).toBeUndefined() |
| 117 | + }) |
| 118 | + |
| 119 | + it("should NOT inject effort when variant is undefined", async () => { |
| 120 | + //#given opus-4-6 with no variant |
| 121 | + const hook = createAnthropicEffortHook() |
| 122 | + const { input, output } = createMockParams({ variant: undefined }) |
| 123 | + |
| 124 | + //#when chat.params hook is called |
| 125 | + await hook["chat.params"](input, output) |
| 126 | + |
| 127 | + //#then effort should NOT be injected |
| 128 | + expect(output.options.effort).toBeUndefined() |
| 129 | + }) |
| 130 | + |
| 131 | + it("should NOT inject effort for non-opus model", async () => { |
| 132 | + //#given claude-sonnet-4-5 (not opus) |
| 133 | + const hook = createAnthropicEffortHook() |
| 134 | + const { input, output } = createMockParams({ |
| 135 | + modelID: "claude-sonnet-4-5", |
| 136 | + }) |
| 137 | + |
| 138 | + //#when chat.params hook is called |
| 139 | + await hook["chat.params"](input, output) |
| 140 | + |
| 141 | + //#then effort should NOT be injected |
| 142 | + expect(output.options.effort).toBeUndefined() |
| 143 | + }) |
| 144 | + |
| 145 | + it("should NOT inject effort for non-anthropic provider with non-claude model", async () => { |
| 146 | + //#given openai provider with gpt model |
| 147 | + const hook = createAnthropicEffortHook() |
| 148 | + const { input, output } = createMockParams({ |
| 149 | + providerID: "openai", |
| 150 | + modelID: "gpt-5.2", |
| 151 | + }) |
| 152 | + |
| 153 | + //#when chat.params hook is called |
| 154 | + await hook["chat.params"](input, output) |
| 155 | + |
| 156 | + //#then effort should NOT be injected |
| 157 | + expect(output.options.effort).toBeUndefined() |
| 158 | + }) |
| 159 | + |
| 160 | + it("should NOT throw when model.modelID is undefined", async () => { |
| 161 | + //#given model with undefined modelID (runtime edge case) |
| 162 | + const hook = createAnthropicEffortHook() |
| 163 | + const input = { |
| 164 | + sessionID: "test-session", |
| 165 | + agent: { name: "sisyphus" }, |
| 166 | + model: { providerID: "anthropic", modelID: undefined as unknown as string }, |
| 167 | + provider: { id: "anthropic" }, |
| 168 | + message: { variant: "max" as const }, |
| 169 | + } |
| 170 | + const output = { temperature: 0.1, options: {} } |
| 171 | + |
| 172 | + //#when chat.params hook is called with undefined modelID |
| 173 | + await hook["chat.params"](input, output) |
| 174 | + |
| 175 | + //#then should gracefully skip without throwing |
| 176 | + expect(output.options.effort).toBeUndefined() |
| 177 | + }) |
| 178 | + }) |
| 179 | + |
| 180 | + describe("preserves existing options", () => { |
| 181 | + it("should NOT overwrite existing effort if already set", async () => { |
| 182 | + //#given options already have effort set |
| 183 | + const hook = createAnthropicEffortHook() |
| 184 | + const { input, output } = createMockParams({ |
| 185 | + existingOptions: { effort: "high" }, |
| 186 | + }) |
| 187 | + |
| 188 | + //#when chat.params hook is called |
| 189 | + await hook["chat.params"](input, output) |
| 190 | + |
| 191 | + //#then existing effort should be preserved |
| 192 | + expect(output.options.effort).toBe("high") |
| 193 | + }) |
| 194 | + |
| 195 | + it("should preserve other existing options when injecting effort", async () => { |
| 196 | + //#given options with existing thinking config |
| 197 | + const hook = createAnthropicEffortHook() |
| 198 | + const { input, output } = createMockParams({ |
| 199 | + existingOptions: { |
| 200 | + thinking: { type: "enabled", budgetTokens: 31999 }, |
| 201 | + }, |
| 202 | + }) |
| 203 | + |
| 204 | + //#when chat.params hook is called |
| 205 | + await hook["chat.params"](input, output) |
| 206 | + |
| 207 | + //#then effort should be added without affecting thinking |
| 208 | + expect(output.options.effort).toBe("max") |
| 209 | + expect(output.options.thinking).toEqual({ |
| 210 | + type: "enabled", |
| 211 | + budgetTokens: 31999, |
| 212 | + }) |
| 213 | + }) |
| 214 | + }) |
| 215 | +}) |
0 commit comments