项目文件夹

文件
2026-06-26 23:04:18 +01:00

68 行
1.9 KiB
Go

package flow
import (
"context"
"encoding/json"
"testing"
"go-micro.dev/v6/client"
codecbytes "go-micro.dev/v6/codec/bytes"
)
// fakeClient embeds the default client (so NewRequest works) and
// overrides Call with a test-supplied function.
type fakeClient struct {
client.Client
callFn func(req client.Request, rsp interface{}) error
}
func (c *fakeClient) Call(ctx context.Context, req client.Request, rsp interface{}, opts ...client.CallOption) error {
return c.callFn(req, rsp)
}
// A flow with Agent set hands the rendered prompt to that agent's
// Agent.Chat endpoint over RPC and records its reply — it does not run
// its own model.
func TestExecuteDispatchesToAgent(t *testing.T) {
f := New("welcome", Agent("comms"), Prompt("welcome {{.Data}}"))
var svc, ep, parentID string
f.client = &fakeClient{
Client: client.DefaultClient,
callFn: func(req client.Request, rsp interface{}) error {
svc, ep = req.Service(), req.Endpoint()
reqFrame := req.Body().(*codecbytes.Frame)
var body map[string]string
if err := json.Unmarshal(reqFrame.Data, &body); err != nil {
t.Fatalf("request body: %v", err)
}
parentID = body["parent_id"]
frame := rsp.(*codecbytes.Frame)
frame.Data = []byte(`{"reply":"welcomed bob","agent":"comms"}`)
return nil
},
}
if err := f.Execute(context.Background(), "bob"); err != nil {
t.Fatalf("Execute: %v", err)
}
if svc != "comms" || ep != "Agent.Chat" {
t.Errorf("dispatched to %s.%s, want comms.Agent.Chat", svc, ep)
}
if parentID == "" {
t.Fatal("dispatch request parent_id is empty")
}
results := f.Results()
if len(results) != 1 {
t.Fatalf("got %d results, want 1", len(results))
}
if results[0].Reply != "welcomed bob" {
t.Errorf("result reply = %q, want %q", results[0].Reply, "welcomed bob")
}
if results[0].Prompt != "welcome bob" {
t.Errorf("rendered prompt = %q, want %q", results[0].Prompt, "welcome bob")
}
}