|
9 | 9 | "time"
|
10 | 10 |
|
11 | 11 | "github.com/google/uuid"
|
| 12 | + "github.com/kylecarbs/aisdk-go" |
| 13 | + "github.com/stretchr/testify/assert" |
12 | 14 | "github.com/stretchr/testify/require"
|
13 | 15 |
|
14 | 16 | "github.com/coder/coder/v2/coderd/coderdtest"
|
@@ -379,3 +381,61 @@ func TestMain(m *testing.M) {
|
379 | 381 |
|
380 | 382 | os.Exit(code)
|
381 | 383 | }
|
| 384 | + |
| 385 | +func TestWithRecovery(t *testing.T) { |
| 386 | + t.Parallel() |
| 387 | + t.Run("OK", func(t *testing.T) { |
| 388 | + t.Parallel() |
| 389 | + fakeTool := toolsdk.Tool[string, string]{ |
| 390 | + Tool: aisdk.Tool{ |
| 391 | + Name: "fake_tool", |
| 392 | + Description: "Returns a string for testing.", |
| 393 | + }, |
| 394 | + Handler: func(tb toolsdk.Toolbox, args string) (string, error) { |
| 395 | + require.Equal(t, "test", args) |
| 396 | + return "ok", nil |
| 397 | + }, |
| 398 | + } |
| 399 | + |
| 400 | + wrapped := toolsdk.WithRecover(fakeTool.Handler) |
| 401 | + v, err := wrapped(nil, "test") |
| 402 | + require.NoError(t, err) |
| 403 | + require.Equal(t, "ok", v) |
| 404 | + }) |
| 405 | + |
| 406 | + t.Run("Error", func(t *testing.T) { |
| 407 | + t.Parallel() |
| 408 | + fakeTool := toolsdk.Tool[string, string]{ |
| 409 | + Tool: aisdk.Tool{ |
| 410 | + Name: "fake_tool", |
| 411 | + Description: "Returns an error for testing.", |
| 412 | + }, |
| 413 | + Handler: func(tb toolsdk.Toolbox, args string) (string, error) { |
| 414 | + require.Equal(t, "test", args) |
| 415 | + return "", assert.AnError |
| 416 | + }, |
| 417 | + } |
| 418 | + wrapped := toolsdk.WithRecover(fakeTool.Handler) |
| 419 | + v, err := wrapped(nil, "test") |
| 420 | + require.Empty(t, v) |
| 421 | + require.ErrorIs(t, err, assert.AnError) |
| 422 | + }) |
| 423 | + |
| 424 | + t.Run("Panic", func(t *testing.T) { |
| 425 | + t.Parallel() |
| 426 | + panicTool := toolsdk.Tool[string, string]{ |
| 427 | + Tool: aisdk.Tool{ |
| 428 | + Name: "panic_tool", |
| 429 | + Description: "Panics for testing.", |
| 430 | + }, |
| 431 | + Handler: func(tb toolsdk.Toolbox, args string) (string, error) { |
| 432 | + panic("you can't sweat this fever out") |
| 433 | + }, |
| 434 | + } |
| 435 | + |
| 436 | + wrapped := toolsdk.WithRecover(panicTool.Handler) |
| 437 | + v, err := wrapped(nil, "disco") |
| 438 | + require.Empty(t, v) |
| 439 | + require.ErrorContains(t, err, "you can't sweat this fever out") |
| 440 | + }) |
| 441 | +} |
0 commit comments