yao/agent/robot/api/robot_test.go
Max 5f4f51e806 Add Robot E2E Tests and Update Makefile and Workflows
- Introduced a new `unit-test-robot-e2e` target in the Makefile to run end-to-end tests for the robot API, requiring real LLM calls and specific environment setup.
- Updated the `pr-test.yml` and `unit-test.yml` workflows to include a dedicated job for Robot E2E Tests, ensuring proper execution in CI with necessary services and dependencies.
- Enhanced the TODO.md to reflect the completion of various E2E test flows, marking them as done and outlining future integration with OpenAPI.
- Refactored context handling in several test files to utilize `context.Background()` for improved clarity and consistency in execution context.
2026-01-19 11:16:33 +08:00

102 lines
2.7 KiB
Go

package api_test
import (
"context"
"testing"
"github.com/stretchr/testify/assert"
"github.com/yaoapp/yao/agent/robot/api"
"github.com/yaoapp/yao/agent/robot/types"
"github.com/yaoapp/yao/agent/testutils"
)
// TestGetRobotValidation tests parameter validation for GetRobot
func TestGetRobotValidation(t *testing.T) {
if testing.Short() {
t.Skip("Skipping integration test")
}
testutils.Prepare(t)
defer testutils.Clean(t)
t.Run("returns error for empty member_id", func(t *testing.T) {
ctx := types.NewContext(context.Background(), nil)
robot, err := api.GetRobot(ctx, "")
assert.Error(t, err)
assert.Nil(t, robot)
assert.Contains(t, err.Error(), "member_id is required")
})
t.Run("returns error for non-existent robot", func(t *testing.T) {
ctx := types.NewContext(context.Background(), nil)
robot, err := api.GetRobot(ctx, "non_existent_member_id_xyz")
assert.Error(t, err)
assert.Nil(t, robot)
})
}
// TestListRobotsValidation tests parameter validation for ListRobots
func TestListRobotsValidation(t *testing.T) {
if testing.Short() {
t.Skip("Skipping integration test")
}
testutils.Prepare(t)
defer testutils.Clean(t)
ctx := types.NewContext(context.Background(), nil)
t.Run("applies default pagination when query is nil", func(t *testing.T) {
result, err := api.ListRobots(ctx, nil)
assert.NoError(t, err)
assert.NotNil(t, result)
assert.Equal(t, 1, result.Page)
assert.Equal(t, 20, result.PageSize)
})
t.Run("applies default pagination when values are zero", func(t *testing.T) {
result, err := api.ListRobots(ctx, &api.ListQuery{
Page: 0,
PageSize: 0,
})
assert.NoError(t, err)
assert.NotNil(t, result)
assert.Equal(t, 1, result.Page)
assert.Equal(t, 20, result.PageSize)
})
t.Run("caps pagesize at 100", func(t *testing.T) {
result, err := api.ListRobots(ctx, &api.ListQuery{
Page: 1,
PageSize: 500,
})
assert.NoError(t, err)
assert.NotNil(t, result)
assert.Equal(t, 100, result.PageSize)
})
}
// TestGetRobotStatusValidation tests parameter validation for GetRobotStatus
func TestGetRobotStatusValidation(t *testing.T) {
if testing.Short() {
t.Skip("Skipping integration test")
}
testutils.Prepare(t)
defer testutils.Clean(t)
t.Run("returns error for empty member_id", func(t *testing.T) {
ctx := types.NewContext(context.Background(), nil)
status, err := api.GetRobotStatus(ctx, "")
assert.Error(t, err)
assert.Nil(t, status)
assert.Contains(t, err.Error(), "member_id is required")
})
t.Run("returns error for non-existent robot", func(t *testing.T) {
ctx := types.NewContext(context.Background(), nil)
status, err := api.GetRobotStatus(ctx, "non_existent_member_id_xyz")
assert.Error(t, err)
assert.Nil(t, status)
})
}