-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbot_test.go
100 lines (89 loc) · 1.86 KB
/
bot_test.go
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
99
100
package main
import (
"bytes"
"flag"
"os"
"sync"
"testing"
)
func TestParseArgs(t *testing.T) {
testCases := []struct {
name string
args []string
expected Args
}{
{
name: "no flags",
args: []string{"arg1", "arg2", "arg3"},
expected: Args{
Interactive: false,
Prompt: "arg1 arg2 arg3",
},
},
{
name: "interactive mode with args",
args: []string{"-i", "arg1", "arg2", "arg3"},
expected: Args{
Interactive: true,
Prompt: "arg1 arg2 arg3",
},
},
{
name: "no flags and no args",
args: []string{},
expected: Args{
Interactive: false,
Prompt: "",
},
},
{
name: "interactive mode without args",
args: []string{"-i"},
expected: Args{
Interactive: true,
Prompt: "",
},
},
{
name: "-i is part of prompt when not in the first position",
args: []string{"arg1", "-i", "arg2"},
expected: Args{
Interactive: false,
Prompt: "arg1 -i arg2",
},
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
os.Args = append([]string{"main"}, tc.args...)
flag.CommandLine = flag.NewFlagSet(os.Args[0], flag.ExitOnError)
args := parseArgs()
if args.Interactive != tc.expected.Interactive || args.Prompt != tc.expected.Prompt {
t.Errorf("Expected %+v, got %+v", tc.expected, args)
}
})
}
}
func TestRenderCompletionStream(t *testing.T) {
tokens := []string{"this", "is", "a", "test"}
expected := "thisisatest\n"
output := &bytes.Buffer{}
respCh := make(chan string)
var wg sync.WaitGroup
wg.Add(2)
go func() {
defer wg.Done()
defer close(respCh)
for _, token := range tokens {
respCh <- token
}
}()
go func() {
defer wg.Done()
RenderCompletionStreamResponse(output, respCh)
}()
wg.Wait()
if output.String() != expected {
t.Errorf("Expected %q, but got %q", expected, output.String())
}
}