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

Function TestWithRecovery

codersdk/toolsdk/toolsdk_test.go:2383–2437  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

2381}
2382
2383func TestWithRecovery(t *testing.T) {
2384 t.Parallel()
2385 t.Run("OK", func(t *testing.T) {
2386 t.Parallel()
2387 fakeTool := toolsdk.GenericTool{
2388 Tool: aisdk.Tool{
2389 Name: "echo",
2390 Description: "Echoes the input.",
2391 },
2392 Handler: func(ctx context.Context, tb toolsdk.Deps, args json.RawMessage) (json.RawMessage, error) {
2393 return args, nil
2394 },
2395 }
2396
2397 wrapped := toolsdk.WithRecover(fakeTool.Handler)
2398 v, err := wrapped(context.Background(), toolsdk.Deps{}, []byte(`{}`))
2399 require.NoError(t, err)
2400 require.JSONEq(t, `{}`, string(v))
2401 })
2402
2403 t.Run("Error", func(t *testing.T) {
2404 t.Parallel()
2405 fakeTool := toolsdk.GenericTool{
2406 Tool: aisdk.Tool{
2407 Name: "fake_tool",
2408 Description: "Returns an error for testing.",
2409 },
2410 Handler: func(ctx context.Context, tb toolsdk.Deps, args json.RawMessage) (json.RawMessage, error) {
2411 return nil, assert.AnError
2412 },
2413 }
2414 wrapped := toolsdk.WithRecover(fakeTool.Handler)
2415 v, err := wrapped(context.Background(), toolsdk.Deps{}, []byte(`{}`))
2416 require.Nil(t, v)
2417 require.ErrorIs(t, err, assert.AnError)
2418 })
2419
2420 t.Run("Panic", func(t *testing.T) {
2421 t.Parallel()
2422 panicTool := toolsdk.GenericTool{
2423 Tool: aisdk.Tool{
2424 Name: "panic_tool",
2425 Description: "Panics for testing.",
2426 },
2427 Handler: func(ctx context.Context, tb toolsdk.Deps, args json.RawMessage) (json.RawMessage, error) {
2428 panic("you can't sweat this fever out")
2429 },
2430 }
2431
2432 wrapped := toolsdk.WithRecover(panicTool.Handler)
2433 v, err := wrapped(context.Background(), toolsdk.Deps{}, []byte("disco"))
2434 require.Empty(t, v)
2435 require.ErrorContains(t, err, "you can't sweat this fever out")
2436 })
2437}
2438
2439type testContextKey struct{}
2440

Callers

nothing calls this directly

Calls 4

WithRecoverFunction · 0.92
wrappedFunction · 0.85
RunMethod · 0.65
EmptyMethod · 0.45

Tested by

no test coverage detected