|
| 1 | +import { ProviderDriverKind, ProviderInstanceId, type ServerProvider } from "@t3tools/contracts"; |
| 2 | +import { DEFAULT_UNIFIED_SETTINGS, type UnifiedSettings } from "@t3tools/contracts/settings"; |
| 3 | +import { createModelSelection } from "@t3tools/shared/model"; |
| 4 | +import { describe, expect, it } from "vite-plus/test"; |
| 5 | + |
| 6 | +import { |
| 7 | + applyProviderInstanceSettings, |
| 8 | + deriveProviderInstanceEntries, |
| 9 | + sortProviderInstanceEntries, |
| 10 | +} from "../../providerInstances"; |
| 11 | +import { |
| 12 | + modelSelectionForEffortChange, |
| 13 | + resolveDefaultModelSettingsPresentation, |
| 14 | +} from "./ProjectDefaultsSettings.logic"; |
| 15 | + |
| 16 | +const claudeId = ProviderInstanceId.make("claudeAgent"); |
| 17 | +const codexId = ProviderInstanceId.make("codex"); |
| 18 | +const high = [{ id: "effort", value: "high" }]; |
| 19 | +const savedClaude = createModelSelection(claudeId, "claude-opus-4-8", high); |
| 20 | +function provider(input: { |
| 21 | + instanceId: string; |
| 22 | + driver: string; |
| 23 | + enabled?: boolean; |
| 24 | + availability?: ServerProvider["availability"]; |
| 25 | + models: ReadonlyArray<string>; |
| 26 | +}): ServerProvider { |
| 27 | + return { |
| 28 | + instanceId: ProviderInstanceId.make(input.instanceId), |
| 29 | + driver: ProviderDriverKind.make(input.driver), |
| 30 | + enabled: input.enabled ?? true, |
| 31 | + installed: true, |
| 32 | + version: null, |
| 33 | + status: "ready", |
| 34 | + ...(input.availability ? { availability: input.availability } : {}), |
| 35 | + auth: { status: "authenticated" }, |
| 36 | + checkedAt: "2026-01-01T00:00:00.000Z", |
| 37 | + models: input.models.map((slug, index) => ({ |
| 38 | + slug, |
| 39 | + name: slug, |
| 40 | + isCustom: false, |
| 41 | + ...(index === 0 ? { isDefault: true } : {}), |
| 42 | + capabilities: {}, |
| 43 | + })), |
| 44 | + slashCommands: [], |
| 45 | + skills: [], |
| 46 | + }; |
| 47 | +} |
| 48 | + |
| 49 | +const claudeAndCodex = [ |
| 50 | + provider({ instanceId: "codex", driver: "codex", models: ["gpt-5.6"] }), |
| 51 | + provider({ instanceId: "claudeAgent", driver: "claudeAgent", models: ["claude-opus-4-8"] }), |
| 52 | +]; |
| 53 | + |
| 54 | +function present(input: { |
| 55 | + providers: ReadonlyArray<ServerProvider>; |
| 56 | + saved: ReturnType<typeof createModelSelection> | null; |
| 57 | + settings?: UnifiedSettings; |
| 58 | +}) { |
| 59 | + const settings = input.settings ?? DEFAULT_UNIFIED_SETTINGS; |
| 60 | + return resolveDefaultModelSettingsPresentation({ |
| 61 | + entries: sortProviderInstanceEntries( |
| 62 | + applyProviderInstanceSettings(deriveProviderInstanceEntries(input.providers), settings, null), |
| 63 | + ), |
| 64 | + providers: input.providers, |
| 65 | + settings, |
| 66 | + saved: input.saved, |
| 67 | + }); |
| 68 | +} |
| 69 | + |
| 70 | +function claudeOff(): UnifiedSettings { |
| 71 | + return { |
| 72 | + ...DEFAULT_UNIFIED_SETTINGS, |
| 73 | + providerInstances: { |
| 74 | + [claudeId]: { driver: ProviderDriverKind.make("claudeAgent"), enabled: false }, |
| 75 | + }, |
| 76 | + }; |
| 77 | +} |
| 78 | + |
| 79 | +describe("resolveDefaultModelSettingsPresentation", () => { |
| 80 | + it("keeps a saved model when its provider is turned off and locks effort", () => { |
| 81 | + const row = present({ |
| 82 | + providers: claudeAndCodex.map((entry) => |
| 83 | + entry.instanceId === claudeId ? { ...entry, enabled: false } : entry, |
| 84 | + ), |
| 85 | + saved: savedClaude, |
| 86 | + settings: claudeOff(), |
| 87 | + }); |
| 88 | + expect(row).toMatchObject({ |
| 89 | + selection: savedClaude, |
| 90 | + effortLocked: true, |
| 91 | + note: "Claude is turned off.", |
| 92 | + }); |
| 93 | + }); |
| 94 | + |
| 95 | + it("keeps the saved model when settings turn the provider off before the snapshot updates", () => { |
| 96 | + const row = present({ providers: claudeAndCodex, saved: savedClaude, settings: claudeOff() }); |
| 97 | + expect(row.selection).toBe(savedClaude); |
| 98 | + expect(row.effortLocked).toBe(true); |
| 99 | + expect(row.note).toBe("Claude is turned off."); |
| 100 | + }); |
| 101 | + |
| 102 | + it("keeps the saved model when its provider is unavailable", () => { |
| 103 | + const row = present({ |
| 104 | + providers: claudeAndCodex.map((entry) => |
| 105 | + entry.instanceId === claudeId ? { ...entry, availability: "unavailable" as const } : entry, |
| 106 | + ), |
| 107 | + saved: savedClaude, |
| 108 | + }); |
| 109 | + expect(row).toMatchObject({ |
| 110 | + selection: savedClaude, |
| 111 | + effortLocked: true, |
| 112 | + note: "Claude is unavailable.", |
| 113 | + }); |
| 114 | + }); |
| 115 | + |
| 116 | + it("keeps the saved model when its provider is absent from this environment", () => { |
| 117 | + const row = present({ |
| 118 | + providers: claudeAndCodex.filter((entry) => entry.instanceId !== claudeId), |
| 119 | + saved: savedClaude, |
| 120 | + }); |
| 121 | + expect(row).toMatchObject({ |
| 122 | + selection: savedClaude, |
| 123 | + effortLocked: true, |
| 124 | + note: "This provider is not available on this environment.", |
| 125 | + }); |
| 126 | + }); |
| 127 | + |
| 128 | + it("leaves effort editable when the saved provider can run", () => { |
| 129 | + const row = present({ providers: claudeAndCodex, saved: savedClaude }); |
| 130 | + expect(row.selection).toBe(savedClaude); |
| 131 | + expect(row.effortLocked).toBe(false); |
| 132 | + expect(row.note).toBeNull(); |
| 133 | + }); |
| 134 | + |
| 135 | + it("shows the automatic fallback when nothing is saved", () => { |
| 136 | + const row = present({ providers: claudeAndCodex, saved: null }); |
| 137 | + expect(row).toMatchObject({ |
| 138 | + effortLocked: false, |
| 139 | + note: null, |
| 140 | + selection: { instanceId: codexId, model: "gpt-5.6" }, |
| 141 | + }); |
| 142 | + }); |
| 143 | + |
| 144 | + it("still follows a hidden model on the same provider", () => { |
| 145 | + const settings: UnifiedSettings = { |
| 146 | + ...DEFAULT_UNIFIED_SETTINGS, |
| 147 | + providerModelPreferences: { |
| 148 | + [claudeId]: { hiddenModels: ["claude-opus-4-8"], modelOrder: [] }, |
| 149 | + }, |
| 150 | + }; |
| 151 | + const row = present({ |
| 152 | + providers: [ |
| 153 | + provider({ |
| 154 | + instanceId: "claudeAgent", |
| 155 | + driver: "claudeAgent", |
| 156 | + models: ["claude-sonnet-5", "claude-opus-4-8"], |
| 157 | + }), |
| 158 | + ], |
| 159 | + saved: savedClaude, |
| 160 | + settings, |
| 161 | + }); |
| 162 | + expect(row.effortLocked).toBe(false); |
| 163 | + expect(row.selection).toMatchObject({ instanceId: claudeId, model: "claude-sonnet-5" }); |
| 164 | + }); |
| 165 | +}); |
| 166 | + |
| 167 | +describe("modelSelectionForEffortChange", () => { |
| 168 | + it("writes effort onto the saved model when another instance is displayed", () => { |
| 169 | + const displayed = createModelSelection(codexId, "gpt-5.6"); |
| 170 | + expect(modelSelectionForEffortChange(savedClaude, displayed, high)).toEqual( |
| 171 | + createModelSelection(claudeId, "claude-opus-4-8", high), |
| 172 | + ); |
| 173 | + }); |
| 174 | + |
| 175 | + it("writes effort onto the displayed model when it is the saved instance", () => { |
| 176 | + const displayed = createModelSelection(claudeId, "claude-sonnet-5"); |
| 177 | + expect( |
| 178 | + modelSelectionForEffortChange(savedClaude, displayed, [{ id: "effort", value: "low" }]), |
| 179 | + ).toEqual(createModelSelection(claudeId, "claude-sonnet-5", [{ id: "effort", value: "low" }])); |
| 180 | + }); |
| 181 | + |
| 182 | + it("pins the displayed automatic model when nothing is saved", () => { |
| 183 | + const displayed = createModelSelection(codexId, "gpt-5.6"); |
| 184 | + expect(modelSelectionForEffortChange(null, displayed, high)).toEqual( |
| 185 | + createModelSelection(codexId, "gpt-5.6", high), |
| 186 | + ); |
| 187 | + }); |
| 188 | +}); |
0 commit comments