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

Function TestAdvisorRunError

coderd/x/chatd/chatadvisor/runner_test.go:248–309  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

246}
247
248func TestAdvisorRunError(t *testing.T) {
249 t.Parallel()
250
251 runtime, err := chatadvisor.NewRuntime(chatadvisor.RuntimeConfig{
252 Model: &chattest.FakeModel{
253 ProviderName: "test-provider",
254 ModelName: "test-model",
255 StreamFn: func(_ context.Context, _ fantasy.Call) (fantasy.StreamResponse, error) {
256 return nil, xerrors.New("boom")
257 },
258 },
259 MaxUsesPerRun: 1,
260 MaxOutputTokens: 64,
261 })
262 require.NoError(t, err)
263
264 result, err := runtime.RunAdvisor(t.Context(), "what failed?", nil, nil)
265 require.NoError(t, err)
266 require.Equal(t, chatadvisor.ResultTypeError, result.Type)
267 require.Contains(t, result.Error, "boom")
268 // A transient nested run failure must not consume quota: callers
269 // can retry up to MaxUsesPerRun times despite the failure.
270 require.Equal(t, 1, result.RemainingUses)
271
272 // Confirm the refund left the runtime in a usable state by issuing
273 // a successful call after the failure, even though MaxUsesPerRun=1.
274 runtime2, err := chatadvisor.NewRuntime(chatadvisor.RuntimeConfig{
275 Model: &chattest.FakeModel{
276 ProviderName: "test-provider",
277 ModelName: "test-model",
278 StreamFn: func() func(context.Context, fantasy.Call) (fantasy.StreamResponse, error) {
279 var calls int
280 return func(_ context.Context, _ fantasy.Call) (fantasy.StreamResponse, error) {
281 calls++
282 if calls == 1 {
283 return nil, xerrors.New("boom")
284 }
285 return streamFromParts([]fantasy.StreamPart{
286 {Type: fantasy.StreamPartTypeTextStart, ID: "text-1"},
287 {Type: fantasy.StreamPartTypeTextDelta, ID: "text-1", Delta: "recovered"},
288 {Type: fantasy.StreamPartTypeTextEnd, ID: "text-1"},
289 {Type: fantasy.StreamPartTypeFinish, FinishReason: fantasy.FinishReasonStop},
290 }), nil
291 }
292 }(),
293 },
294 MaxUsesPerRun: 1,
295 MaxOutputTokens: 64,
296 })
297 require.NoError(t, err)
298
299 failed, err := runtime2.RunAdvisor(t.Context(), "first?", nil, nil)
300 require.NoError(t, err)
301 require.Equal(t, chatadvisor.ResultTypeError, failed.Type)
302 require.Equal(t, 1, failed.RemainingUses)
303
304 retried, err := runtime2.RunAdvisor(t.Context(), "retry?", nil, nil)
305 require.NoError(t, err)

Callers

nothing calls this directly

Calls 7

RunAdvisorMethod · 0.95
NewRuntimeFunction · 0.92
streamFromPartsFunction · 0.70
NewMethod · 0.65
ContextMethod · 0.65
EqualMethod · 0.45
ContainsMethod · 0.45

Tested by

no test coverage detected