MCPcopy Index your code
hub / github.com/coder/coder / TestResolveAdvisorModelOverride

Function TestResolveAdvisorModelOverride

coderd/x/chatd/advisor_internal_test.go:161–417  ·  view source on GitHub ↗

TestResolveAdvisorModelOverride covers the early-return, each fallback branch, and the success path. Prior tests only hit the ModelConfigID == uuid.Nil early return, so the override body never executed.

(t *testing.T)

Source from the content-addressed store, hash-verified

159// branch, and the success path. Prior tests only hit the ModelConfigID ==
160// uuid.Nil early return, so the override body never executed.
161func TestResolveAdvisorModelOverride(t *testing.T) {
162 t.Parallel()
163
164 fallbackModel := &chattest.FakeModel{ProviderName: "stub", ModelName: "stub"}
165 fallbackCallConfig := codersdk.ChatModelCallConfig{}
166 logger := slog.Make()
167
168 t.Run("NilModelConfigReturnsFallback", func(t *testing.T) {
169 t.Parallel()
170 ctx := testutil.Context(t, testutil.WaitShort)
171 // Panic if the cache is consulted; the early return must skip it.
172 store := &advisorOverrideStubStore{}
173 p := newAdvisorTestServer(ctx, t, store)
174
175 gotModel, gotCfg := p.resolveAdvisorModelOverrideOrFallback(
176 ctx,
177 database.Chat{},
178 codersdk.AdvisorConfig{},
179 fallbackModel,
180 fallbackCallConfig,
181 chatprovider.ProviderAPIKeys{},
182 modelBuildOptions{},
183 logger,
184 )
185 require.Equal(t, fallbackModel, gotModel)
186 require.Equal(t, fallbackCallConfig, gotCfg)
187 })
188
189 t.Run("ConfigLookupErrorReturnsFallback", func(t *testing.T) {
190 t.Parallel()
191 ctx := testutil.Context(t, testutil.WaitShort)
192 store := &advisorOverrideStubStore{
193 getEnabledChatModelConfigByID: func(context.Context, uuid.UUID) (database.ChatModelConfig, error) {
194 return database.ChatModelConfig{}, xerrors.New("lookup failed")
195 },
196 }
197 p := newAdvisorTestServer(ctx, t, store)
198
199 gotModel, gotCfg := p.resolveAdvisorModelOverrideOrFallback(
200 ctx,
201 database.Chat{},
202 codersdk.AdvisorConfig{ModelConfigID: uuid.New()},
203 fallbackModel,
204 fallbackCallConfig,
205 chatprovider.ProviderAPIKeys{OpenAI: "sk-test"},
206 modelBuildOptions{},
207 logger,
208 )
209 require.Equal(t, fallbackModel, gotModel)
210 require.Equal(t, fallbackCallConfig, gotCfg)
211 })
212
213 // Covers the sql.ErrNoRows branch separately from the generic-error
214 // branch above. GetEnabledChatModelConfigByID returns ErrNoRows when
215 // an admin disables the advisor model or its provider, and that case
216 // has a distinct log message. Without this test, removing the
217 // errors.Is(err, sql.ErrNoRows) check would still pass the sibling
218 // test.

Callers

nothing calls this directly

Calls 9

ContextFunction · 0.92
newAdvisorTestServerFunction · 0.85
RunMethod · 0.65
NewMethod · 0.65
ModelMethod · 0.65
EqualMethod · 0.45
MarshalMethod · 0.45
ProviderMethod · 0.45

Tested by

no test coverage detected