|
| 1 | +import { afterEach, expect, test } from "bun:test"; |
| 2 | +import { mkdtemp, mkdir, rm, writeFile } from "node:fs/promises"; |
| 3 | +import { tmpdir } from "node:os"; |
| 4 | +import { join } from "node:path"; |
| 5 | +import { loadConfig } from "./index.js"; |
| 6 | +import { isSettings } from "./settings.js"; |
| 7 | +import { clearSourceCredentials } from "./source-credentials.js"; |
| 8 | +import { |
| 9 | + resetPricingMetadataRefreshForTests, |
| 10 | + applyPricingCacheMetadata, |
| 11 | +} from "../cost/pricing-metadata.js"; |
| 12 | +import { setProviderContextWindowOverrides } from "../provider/context-window.js"; |
| 13 | +import { buildSessionSourcesFromConfig } from "../session/runtime-assembly.js"; |
| 14 | +import { applyLiveModelSwitch } from "../session/live-model-switch.js"; |
| 15 | +import { generateSessionId, initSessionDir } from "../session/index.js"; |
| 16 | +import { saveState } from "../session/state.js"; |
| 17 | +import { createOpenAICompatibleAdapter } from "../provider/openai-compatible-adapter.js"; |
| 18 | + |
| 19 | +const directories: string[] = []; |
| 20 | + |
| 21 | +afterEach(async () => { |
| 22 | + resetPricingMetadataRefreshForTests(); |
| 23 | + applyPricingCacheMetadata(null); |
| 24 | + setProviderContextWindowOverrides(undefined); |
| 25 | + clearSourceCredentials(); |
| 26 | + for (const dir of directories.splice(0)) { |
| 27 | + await rm(dir, { recursive: true, force: true }); |
| 28 | + } |
| 29 | +}); |
| 30 | + |
| 31 | +test("config accepts a custom-only local pin and rejects a disabled family rung", async () => { |
| 32 | + const cwd = await mkdtemp(join(tmpdir(), "custom-reasoning-config-")); |
| 33 | + directories.push(cwd); |
| 34 | + const globalSettingsPath = join(cwd, "global.json"); |
| 35 | + await mkdir(join(cwd, ".corbits")); |
| 36 | + await writeFile( |
| 37 | + globalSettingsPath, |
| 38 | + JSON.stringify({ |
| 39 | + providers: { |
| 40 | + custom: { |
| 41 | + baseURL: "https://custom.example/v1", |
| 42 | + keyless: true, |
| 43 | + models: ["custom-model"], |
| 44 | + reasoningEfforts: ["low", "xhigh", "max"], |
| 45 | + defaultReasoningEffort: "max", |
| 46 | + }, |
| 47 | + }, |
| 48 | + }), |
| 49 | + ); |
| 50 | + const localPath = join(cwd, ".corbits", "settings.json"); |
| 51 | + const options = { |
| 52 | + globalSettingsPath, |
| 53 | + home: cwd, |
| 54 | + pricing: { |
| 55 | + cachePath: join(cwd, "pricing.json"), |
| 56 | + fetchImpl: (() => |
| 57 | + Promise.reject(new Error("offline"))) as unknown as typeof fetch, |
| 58 | + }, |
| 59 | + }; |
| 60 | + await writeFile(localPath, JSON.stringify({ reasoningEffort: "xhigh" })); |
| 61 | + const config = await loadConfig(["--cwd", cwd], options); |
| 62 | + expect(config.configured).toBe(true); |
| 63 | + if (!config.configured) throw new Error("expected config"); |
| 64 | + expect(config.reasoningEffort).toBe("xhigh"); |
| 65 | + await writeFile(localPath, JSON.stringify({ reasoningEffort: "medium" })); |
| 66 | + await expect(loadConfig(["--cwd", cwd], options)).rejects.toThrow( |
| 67 | + /supported: low, xhigh, max/, |
| 68 | + ); |
| 69 | +}); |
| 70 | + |
| 71 | +test("settings validate nonempty canonical ladders and enabled defaults", () => { |
| 72 | + const provider = { |
| 73 | + baseURL: "https://custom.example/v1", |
| 74 | + keyless: true, |
| 75 | + models: ["custom-model"], |
| 76 | + }; |
| 77 | + const allows = (reasoning: Record<string, unknown>) => |
| 78 | + isSettings({ providers: { custom: { ...provider, ...reasoning } } }); |
| 79 | + expect(allows({})).toBe(true); |
| 80 | + expect( |
| 81 | + allows({ reasoningEfforts: ["low", "max"], defaultReasoningEffort: "max" }), |
| 82 | + ).toBe(true); |
| 83 | + expect(allows({ reasoningEfforts: [] })).toBe(false); |
| 84 | + expect(allows({ reasoningEfforts: ["bogus"] })).toBe(false); |
| 85 | + expect( |
| 86 | + allows({ reasoningEfforts: ["low"], defaultReasoningEffort: "max" }), |
| 87 | + ).toBe(false); |
| 88 | + expect(allows({ defaultReasoningEffort: "max" })).toBe(false); |
| 89 | +}); |
| 90 | + |
| 91 | +test("startup, resume, and live model switch serialize their custom defaults", async () => { |
| 92 | + const cwd = await mkdtemp(join(tmpdir(), "custom-reasoning-session-")); |
| 93 | + directories.push(cwd); |
| 94 | + const globalSettingsPath = join(cwd, "global.json"); |
| 95 | + const custom = { |
| 96 | + baseURL: "https://custom.example/v1", |
| 97 | + keyless: true, |
| 98 | + models: ["custom-model"], |
| 99 | + reasoningEfforts: ["low", "max"], |
| 100 | + defaultReasoningEffort: "max", |
| 101 | + }; |
| 102 | + await writeFile( |
| 103 | + globalSettingsPath, |
| 104 | + JSON.stringify({ |
| 105 | + defaultProvider: "custom", |
| 106 | + providers: { |
| 107 | + custom, |
| 108 | + other: { |
| 109 | + ...custom, |
| 110 | + reasoningEfforts: ["high", "xhigh"], |
| 111 | + defaultReasoningEffort: "xhigh", |
| 112 | + }, |
| 113 | + }, |
| 114 | + }), |
| 115 | + ); |
| 116 | + const options = { |
| 117 | + globalSettingsPath, |
| 118 | + home: cwd, |
| 119 | + pricing: { |
| 120 | + cachePath: join(cwd, "pricing.json"), |
| 121 | + fetchImpl: (() => |
| 122 | + Promise.reject(new Error("offline"))) as unknown as typeof fetch, |
| 123 | + }, |
| 124 | + }; |
| 125 | + const sessionId = generateSessionId(); |
| 126 | + await initSessionDir(cwd, sessionId, cwd); |
| 127 | + await saveState( |
| 128 | + cwd, |
| 129 | + sessionId, |
| 130 | + { |
| 131 | + status: "done", |
| 132 | + turnsUsed: 1, |
| 133 | + task: "inspect", |
| 134 | + startedAt: 1, |
| 135 | + finishedAt: 2, |
| 136 | + }, |
| 137 | + cwd, |
| 138 | + ); |
| 139 | + for (const args of [[], ["resume", sessionId]]) { |
| 140 | + const loaded = await loadConfig([...args, "--cwd", cwd], options); |
| 141 | + if (!loaded.configured) throw new Error("expected config"); |
| 142 | + let config = loaded; |
| 143 | + const effortOnWire = () => { |
| 144 | + const source = buildSessionSourcesFromConfig(config, config.sessionId) |
| 145 | + .sources[0]; |
| 146 | + if (source === undefined) throw new Error("missing source"); |
| 147 | + const adapter = createOpenAICompatibleAdapter({ |
| 148 | + sourceId: source.id, |
| 149 | + provider: source.provider, |
| 150 | + model: source.model, |
| 151 | + }); |
| 152 | + return ( |
| 153 | + JSON.parse( |
| 154 | + adapter.buildRequest([], source.model, source.defaults ?? {}).body, |
| 155 | + ) as Record<string, unknown> |
| 156 | + ).reasoning_effort; |
| 157 | + }; |
| 158 | + expect(effortOnWire()).toBe("max"); |
| 159 | + let switchedEffort: unknown; |
| 160 | + applyLiveModelSwitch( |
| 161 | + { providerName: "other", model: "custom-model" }, |
| 162 | + { |
| 163 | + applyIdentity: (next) => { |
| 164 | + config = { ...config, ...next }; |
| 165 | + }, |
| 166 | + setPermissionIdentity: () => undefined, |
| 167 | + rebuildInference: () => { |
| 168 | + switchedEffort = effortOnWire(); |
| 169 | + }, |
| 170 | + refreshAdvertisedSchemas: () => undefined, |
| 171 | + }, |
| 172 | + ); |
| 173 | + expect(switchedEffort).toBe("xhigh"); |
| 174 | + } |
| 175 | +}); |
0 commit comments