|
| 1 | +import { afterEach, beforeEach, describe, expect, test } from "bun:test"; |
| 2 | +import { mkdtempSync, rmSync } from "node:fs"; |
| 3 | +import { tmpdir } from "node:os"; |
| 4 | +import { join } from "node:path"; |
| 5 | +import { saveConfig } from "../src/config"; |
| 6 | +import { startServer } from "../src/server"; |
| 7 | +import type { OcxConfig } from "../src/types"; |
| 8 | + |
| 9 | +const previousHome = process.env.OPENCODEX_HOME; |
| 10 | +let testHome = ""; |
| 11 | + |
| 12 | +function effortConfig(): OcxConfig { |
| 13 | + return { |
| 14 | + port: 0, |
| 15 | + hostname: "127.0.0.1", |
| 16 | + defaultProvider: "kimi", |
| 17 | + providers: { |
| 18 | + kimi: { |
| 19 | + adapter: "openai-chat", |
| 20 | + baseUrl: "https://kimi.test/v1", |
| 21 | + models: ["k3", "kimi-for-coding"], |
| 22 | + modelReasoningEfforts: { |
| 23 | + k3: ["low", "high", "max"], |
| 24 | + "kimi-for-coding": [], |
| 25 | + }, |
| 26 | + modelDefaultReasoningEfforts: { k3: "high" }, |
| 27 | + }, |
| 28 | + }, |
| 29 | + }; |
| 30 | +} |
| 31 | + |
| 32 | +beforeEach(() => { |
| 33 | + testHome = mkdtempSync(join(tmpdir(), "ocx-grok-effort-list-")); |
| 34 | + process.env.OPENCODEX_HOME = testHome; |
| 35 | +}); |
| 36 | + |
| 37 | +afterEach(() => { |
| 38 | + if (previousHome === undefined) delete process.env.OPENCODEX_HOME; |
| 39 | + else process.env.OPENCODEX_HOME = previousHome; |
| 40 | + if (testHome) rmSync(testHome, { recursive: true, force: true }); |
| 41 | + testHome = ""; |
| 42 | +}); |
| 43 | + |
| 44 | +describe("raw /v1/models list reasoning-effort advertisement (Grok Build discovery)", () => { |
| 45 | + test("routed models with configured tiers advertise the Grok reasoning catalog shape", async () => { |
| 46 | + saveConfig(effortConfig()); |
| 47 | + const server = startServer(0); |
| 48 | + try { |
| 49 | + const res = await fetch(new URL("/v1/models", server.url)); |
| 50 | + expect(res.status).toBe(200); |
| 51 | + const body = await res.json() as { data: Array<Record<string, unknown>> }; |
| 52 | + const k3 = body.data.find(m => m.id === "kimi/k3"); |
| 53 | + expect(k3).toBeDefined(); |
| 54 | + expect(k3!.supports_reasoning_effort).toBe(true); |
| 55 | + expect(k3!.reasoning_effort).toBe("high"); |
| 56 | + expect(k3!.reasoning_efforts).toEqual([ |
| 57 | + { value: "low", label: "Low Effort" }, |
| 58 | + { value: "high", label: "High Effort", default: true }, |
| 59 | + { value: "max", label: "Max Effort" }, |
| 60 | + ]); |
| 61 | + // Native rows preserve the pinned per-model ladder and default. Sol and Terra include |
| 62 | + // ultra, while Luna intentionally ends at max, matching the canonical Codex catalog. |
| 63 | + const nativeExpectations = [ |
| 64 | + { |
| 65 | + id: "gpt-5.6-sol", |
| 66 | + defaultEffort: "low", |
| 67 | + efforts: ["low", "medium", "high", "xhigh", "max", "ultra"], |
| 68 | + }, |
| 69 | + { |
| 70 | + id: "gpt-5.6-terra", |
| 71 | + defaultEffort: "medium", |
| 72 | + efforts: ["low", "medium", "high", "xhigh", "max", "ultra"], |
| 73 | + }, |
| 74 | + { |
| 75 | + id: "gpt-5.6-luna", |
| 76 | + defaultEffort: "medium", |
| 77 | + efforts: ["low", "medium", "high", "xhigh", "max"], |
| 78 | + }, |
| 79 | + ]; |
| 80 | + for (const expected of nativeExpectations) { |
| 81 | + const native = body.data.find(m => m.id === expected.id); |
| 82 | + expect(native).toBeDefined(); |
| 83 | + expect(native!.supports_reasoning_effort).toBe(true); |
| 84 | + expect(native!.reasoning_effort).toBe(expected.defaultEffort); |
| 85 | + expect((native!.reasoning_efforts as Array<{ value: string }>).map(option => option.value)) |
| 86 | + .toEqual(expected.efforts); |
| 87 | + } |
| 88 | + } finally { |
| 89 | + await server.stop(true); |
| 90 | + } |
| 91 | + }); |
| 92 | + |
| 93 | + test("models with an empty tier list advertise no effort fields", async () => { |
| 94 | + saveConfig(effortConfig()); |
| 95 | + const server = startServer(0); |
| 96 | + try { |
| 97 | + const res = await fetch(new URL("/v1/models", server.url)); |
| 98 | + const body = await res.json() as { data: Array<Record<string, unknown>> }; |
| 99 | + const plain = body.data.find(m => m.id === "kimi/kimi-for-coding"); |
| 100 | + expect(plain).toBeDefined(); |
| 101 | + expect("supports_reasoning_effort" in plain!).toBe(false); |
| 102 | + expect("reasoning_effort" in plain!).toBe(false); |
| 103 | + expect("reasoning_efforts" in plain!).toBe(false); |
| 104 | + } finally { |
| 105 | + await server.stop(true); |
| 106 | + } |
| 107 | + }); |
| 108 | + |
| 109 | + test("a ladder without a configured default uses the canonical medium default", async () => { |
| 110 | + const config = effortConfig(); |
| 111 | + config.providers.kimi!.modelDefaultReasoningEfforts = {}; |
| 112 | + config.providers.kimi!.modelReasoningEfforts = { k3: ["low", "medium", "high"] }; |
| 113 | + saveConfig(config); |
| 114 | + const server = startServer(0); |
| 115 | + try { |
| 116 | + const res = await fetch(new URL("/v1/models", server.url)); |
| 117 | + const body = await res.json() as { data: Array<Record<string, unknown>> }; |
| 118 | + const k3 = body.data.find(m => m.id === "kimi/k3"); |
| 119 | + expect(k3!.reasoning_effort).toBe("medium"); |
| 120 | + const options = k3!.reasoning_efforts as Array<Record<string, unknown>>; |
| 121 | + expect(options[1]).toEqual({ value: "medium", label: "Medium Effort", default: true }); |
| 122 | + } finally { |
| 123 | + await server.stop(true); |
| 124 | + } |
| 125 | + }); |
| 126 | + |
| 127 | + test("an invalid configured default falls back with the canonical medium/high/first order", async () => { |
| 128 | + const config = effortConfig(); |
| 129 | + config.providers.kimi!.modelDefaultReasoningEfforts = { k3: "medium" }; |
| 130 | + saveConfig(config); |
| 131 | + const server = startServer(0); |
| 132 | + try { |
| 133 | + const res = await fetch(new URL("/v1/models", server.url)); |
| 134 | + const body = await res.json() as { data: Array<Record<string, unknown>> }; |
| 135 | + const k3 = body.data.find(m => m.id === "kimi/k3"); |
| 136 | + // k3's ladder is low/high/max: no medium, so the canonical fallback picks high. |
| 137 | + expect(k3!.reasoning_effort).toBe("high"); |
| 138 | + const options = k3!.reasoning_efforts as Array<Record<string, unknown>>; |
| 139 | + expect(options[1]).toEqual({ value: "high", label: "High Effort", default: true }); |
| 140 | + } finally { |
| 141 | + await server.stop(true); |
| 142 | + } |
| 143 | + }); |
| 144 | + |
| 145 | + test("falls back to the first tier when neither medium nor high is available", async () => { |
| 146 | + const config = effortConfig(); |
| 147 | + config.providers.kimi!.models = [...(config.providers.kimi!.models ?? []), "custom-test"]; |
| 148 | + config.providers.kimi!.modelReasoningEfforts = { "custom-test": ["low", "max"] }; |
| 149 | + config.providers.kimi!.modelDefaultReasoningEfforts = { "custom-test": "medium" }; |
| 150 | + saveConfig(config); |
| 151 | + const server = startServer(0); |
| 152 | + try { |
| 153 | + const res = await fetch(new URL("/v1/models", server.url)); |
| 154 | + const body = await res.json() as { data: Array<Record<string, unknown>> }; |
| 155 | + const model = body.data.find(m => m.id === "kimi/custom-test"); |
| 156 | + expect(model!.reasoning_effort).toBe("low"); |
| 157 | + expect(model!.reasoning_efforts).toEqual([ |
| 158 | + { value: "low", label: "Low Effort", default: true }, |
| 159 | + { value: "max", label: "Max Effort" }, |
| 160 | + ]); |
| 161 | + } finally { |
| 162 | + await server.stop(true); |
| 163 | + } |
| 164 | + }); |
| 165 | +}); |
0 commit comments