87 lines
2.3 KiB
Go
87 lines
2.3 KiB
Go
package builtin
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"errors"
|
|
"testing"
|
|
)
|
|
|
|
func TestQuestionTool_NilAskHookIsAClearError(t *testing.T) {
|
|
env := testEnv(t) // env.Ask bleibt nil, wie im Print-Modus
|
|
in, _ := json.Marshal(map[string]any{"question": "welches Format?"})
|
|
|
|
res, err := QuestionTool{}.Run(context.Background(), in, env)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if !res.IsError {
|
|
t.Fatal("expected IsError when env.Ask is nil")
|
|
}
|
|
if !contains(res.ForModel, "not supported") {
|
|
t.Errorf("expected a clear explanation, got: %q", res.ForModel)
|
|
}
|
|
}
|
|
|
|
func TestQuestionTool_EmptyQuestionIsRejected(t *testing.T) {
|
|
env := testEnv(t)
|
|
in, _ := json.Marshal(map[string]any{"question": ""})
|
|
|
|
res, err := QuestionTool{}.Run(context.Background(), in, env)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if !res.IsError {
|
|
t.Fatal("expected IsError for an empty question")
|
|
}
|
|
}
|
|
|
|
func TestQuestionTool_ReturnsTheUsersAnswer(t *testing.T) {
|
|
env := testEnv(t)
|
|
var gotQuestion string
|
|
var gotOptions []string
|
|
env.Ask = func(ctx context.Context, question string, options []string) (string, error) {
|
|
gotQuestion = question
|
|
gotOptions = options
|
|
return "JSON bitte", nil
|
|
}
|
|
|
|
in, _ := json.Marshal(map[string]any{"question": "Welches Format?", "options": []string{"JSON", "YAML"}})
|
|
res, err := QuestionTool{}.Run(context.Background(), in, env)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if res.IsError {
|
|
t.Fatalf("unexpected error: %s", res.ForModel)
|
|
}
|
|
if res.ForModel != "JSON bitte" {
|
|
t.Errorf("ForModel = %q, want the raw answer", res.ForModel)
|
|
}
|
|
if gotQuestion != "Welches Format?" {
|
|
t.Errorf("question passed to Ask = %q", gotQuestion)
|
|
}
|
|
if len(gotOptions) != 2 || gotOptions[0] != "JSON" || gotOptions[1] != "YAML" {
|
|
t.Errorf("options passed to Ask = %+v", gotOptions)
|
|
}
|
|
|
|
ui, ok := res.ForUI.(QuestionResult)
|
|
if !ok || ui.Answer != "JSON bitte" {
|
|
t.Errorf("ForUI = %+v", res.ForUI)
|
|
}
|
|
}
|
|
|
|
func TestQuestionTool_AskErrorSurfacesAsToolError(t *testing.T) {
|
|
env := testEnv(t)
|
|
env.Ask = func(ctx context.Context, question string, options []string) (string, error) {
|
|
return "", errors.New("boom")
|
|
}
|
|
in, _ := json.Marshal(map[string]any{"question": "x"})
|
|
|
|
res, err := QuestionTool{}.Run(context.Background(), in, env)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if !res.IsError {
|
|
t.Error("expected an Ask error to surface as IsError, not a Go error")
|
|
}
|
|
}
|