Repository navigation
Expand file tree
/
Copy pathinterviewer_test.go
More file actions
193 lines (169 loc) · 5.24 KB
/
Copy pathinterviewer_test.go
File metadata and controls
193 lines (169 loc) · 5.24 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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
// ABOUTME: Tests for ThreadInterviewer — the channel bridge from tracker gates to a thread.
package chatops
import (
"context"
"fmt"
"sync"
"testing"
"time"
"github.com/2389-research/tracker/pipeline/handlers"
)
// fakeUI records posted gates on a channel so a test can await and resolve them.
type fakeUI struct {
gates chan Gate
mu sync.Mutex
posts []string
}
func newFakeUI() *fakeUI { return &fakeUI{gates: make(chan Gate, 4)} }
func (f *fakeUI) PostGate(g Gate) error { f.gates <- g; return nil }
func (f *fakeUI) Post(text string) error {
f.mu.Lock()
f.posts = append(f.posts, text)
f.mu.Unlock()
return nil
}
// seqIDs returns a deterministic unique-id generator.
func seqIDs() func() string {
var n int
var mu sync.Mutex
return func() string {
mu.Lock()
defer mu.Unlock()
n++
return fmt.Sprintf("g%d", n)
}
}
func awaitGate(t *testing.T, ui *fakeUI) Gate {
t.Helper()
select {
case g := <-ui.gates:
return g
case <-time.After(2 * time.Second):
t.Fatal("timed out waiting for a posted gate")
return Gate{}
}
}
func TestThreadInterviewer_ChoiceAndYesNo(t *testing.T) {
ui := newFakeUI()
iv := NewThreadInterviewer(ui, seqIDs())
got := make(chan string, 1)
go func() { a, _ := iv.Ask("pick one", []string{"alpha", "beta"}, "alpha"); got <- a }()
g := awaitGate(t, ui)
if g.Kind != GateChoice {
t.Fatalf("kind = %q, want choice", g.Kind)
}
if !iv.Resolve(g.ID, GateAnswer{Choice: "beta"}) {
t.Fatal("Resolve returned false for a pending gate")
}
if a := <-got; a != "beta" {
t.Fatalf("answer = %q, want beta", a)
}
// Yes/No renders as a yes_no gate.
go func() { a, _ := iv.Ask("proceed?", []string{"Yes", "No"}, "Yes"); got <- a }()
g = awaitGate(t, ui)
if g.Kind != GateYesNo {
t.Fatalf("kind = %q, want yes_no", g.Kind)
}
iv.Resolve(g.ID, GateAnswer{Choice: "No"})
if a := <-got; a != "No" {
t.Fatalf("answer = %q, want No", a)
}
}
func TestThreadInterviewer_FreeformAndLabels(t *testing.T) {
ui := newFakeUI()
iv := NewThreadInterviewer(ui, seqIDs())
got := make(chan string, 1)
go func() { a, _ := iv.AskFreeform("what next?"); got <- a }()
g := awaitGate(t, ui)
if g.Kind != GateFreeform {
t.Fatalf("kind = %q, want freeform", g.Kind)
}
iv.Resolve(g.ID, GateAnswer{Freeform: "ship it"})
if a := <-got; a != "ship it" {
t.Fatalf("answer = %q, want 'ship it'", a)
}
// A typed "other" reply wins over a label selection.
go func() { a, _ := iv.AskFreeformWithLabels("choose", []string{"a", "b"}, "a"); got <- a }()
g = awaitGate(t, ui)
iv.Resolve(g.ID, GateAnswer{Choice: "a", Freeform: "custom"})
if a := <-got; a != "custom" {
t.Fatalf("labeled answer = %q, want custom", a)
}
}
func TestThreadInterviewer_Interview(t *testing.T) {
ui := newFakeUI()
iv := NewThreadInterviewer(ui, seqIDs())
questions := []handlers.Question{
{Index: 1, Text: "name?"}, // open-ended → freeform
{Index: 2, Text: "color?", Options: []string{"red", "blue"}}, // options → choice
}
got := make(chan *handlers.InterviewResult, 1)
go func() { r, _ := iv.AskInterview(questions, nil); got <- r }()
// Q1 is presented as a freeform gate; reply.
g1 := awaitGate(t, ui)
if g1.Kind != GateFreeform {
t.Fatalf("q1 kind = %q, want freeform", g1.Kind)
}
iv.Resolve(g1.ID, GateAnswer{Freeform: "Ada"})
// Q2 is presented as a choice gate; click.
g2 := awaitGate(t, ui)
if g2.Kind != GateChoice || len(g2.Choices) != 2 {
t.Fatalf("q2 gate = %+v", g2)
}
iv.Resolve(g2.ID, GateAnswer{Choice: "blue"})
r := <-got
if r == nil || len(r.Questions) != 2 {
t.Fatalf("interview result = %+v", r)
}
if r.Questions[0].Answer != "Ada" || r.Questions[1].Answer != "blue" {
t.Fatalf("answers = %+v", r.Questions)
}
}
func TestThreadInterviewer_CancelUnblocks(t *testing.T) {
ui := newFakeUI()
iv := NewThreadInterviewer(ui, seqIDs())
// Freeform/choice cancel returns an error.
errc := make(chan error, 1)
go func() { _, err := iv.AskFreeform("hold"); errc <- err }()
awaitGate(t, ui)
iv.Cancel()
select {
case err := <-errc:
if err == nil {
t.Fatal("expected an error after Cancel")
}
case <-time.After(2 * time.Second):
t.Fatal("Cancel did not unblock the gate")
}
// A cancelled interview returns a Canceled result, not an error.
iv2 := NewThreadInterviewer(ui, seqIDs())
resc := make(chan *handlers.InterviewResult, 1)
go func() { r, _ := iv2.AskInterview([]handlers.Question{{Index: 1, Text: "q"}}, nil); resc <- r }()
awaitGate(t, ui)
iv2.Cancel()
if r := <-resc; r == nil || !r.Canceled {
t.Fatalf("expected canceled interview result, got %+v", r)
}
}
func TestThreadInterviewer_ContextCancelUnblocks(t *testing.T) {
ui := newFakeUI()
iv := NewThreadInterviewer(ui, seqIDs())
ctx, cancel := context.WithCancel(context.Background())
iv.SetPipelineContext(ctx)
errc := make(chan error, 1)
go func() { _, err := iv.Ask("pick", []string{"a", "b"}, "a"); errc <- err }()
awaitGate(t, ui)
cancel()
select {
case err := <-errc:
if err == nil {
t.Fatal("expected an error after context cancel")
}
case <-time.After(2 * time.Second):
t.Fatal("context cancel did not unblock the gate")
}
// Resolving an unknown/torn-down gate is a safe no-op.
if iv.Resolve("nonexistent", GateAnswer{Choice: "x"}) {
t.Fatal("Resolve on unknown gate should return false")
}
}