-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprocessor_test.go
More file actions
98 lines (76 loc) · 2.33 KB
/
processor_test.go
File metadata and controls
98 lines (76 loc) · 2.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package chit_test
import (
"context"
"errors"
"testing"
"github.com/zoobz-io/chit"
)
func TestProcessorFunc(t *testing.T) {
called := false
var receivedInput string
var receivedHistory []chit.Message
fn := chit.ProcessorFunc(func(ctx context.Context, input string, history []chit.Message) (chit.Result, error) {
called = true
receivedInput = input
receivedHistory = history
return &chit.Response{Content: "processed: " + input}, nil
})
history := []chit.Message{{Role: "user", Content: "previous"}}
result, err := fn.Process(context.Background(), "hello", history)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if !called {
t.Error("expected ProcessorFunc to be called")
}
if receivedInput != "hello" {
t.Errorf("expected input 'hello', got %q", receivedInput)
}
if len(receivedHistory) != 1 || receivedHistory[0].Content != "previous" {
t.Error("expected history to be passed through")
}
resp, ok := result.(*chit.Response)
if !ok {
t.Fatal("expected Response result")
}
if resp.Content != "processed: hello" {
t.Errorf("expected 'processed: hello', got %q", resp.Content)
}
}
func TestProcessorFunc_ReturnsError(t *testing.T) {
expectedErr := errors.New("processor error")
fn := chit.ProcessorFunc(func(_ context.Context, _ string, _ []chit.Message) (chit.Result, error) {
return nil, expectedErr
})
result, err := fn.Process(context.Background(), "test", nil)
if !errors.Is(err, expectedErr) {
t.Errorf("expected error %v, got %v", expectedErr, err)
}
if result != nil {
t.Error("expected nil result on error")
}
}
func TestProcessorFunc_ReturnsYield(t *testing.T) {
fn := chit.ProcessorFunc(func(_ context.Context, _ string, _ []chit.Message) (chit.Result, error) {
return &chit.Yield{
Prompt: "need more info",
Continuation: func(_ context.Context, input string, _ []chit.Message) (chit.Result, error) {
return &chit.Response{Content: "got: " + input}, nil
},
}, nil
})
result, err := fn.Process(context.Background(), "start", nil)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
yield, ok := result.(*chit.Yield)
if !ok {
t.Fatal("expected Yield result")
}
if yield.Prompt != "need more info" {
t.Errorf("expected prompt 'need more info', got %q", yield.Prompt)
}
if yield.Continuation == nil {
t.Error("expected continuation to be set")
}
}