Repository navigation
Expand file tree
/
Copy pathcommand_test.go
More file actions
97 lines (85 loc) · 2.73 KB
/
Copy pathcommand_test.go
File metadata and controls
97 lines (85 loc) · 2.73 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
// ABOUTME: Tests for the runner's control commands (help/status/cancel/runs).
package chatops
import (
"context"
"strings"
"testing"
"time"
tracker "github.com/2389-research/tracker"
)
func fakePosts(ui *fakeUI) []string {
ui.mu.Lock()
defer ui.mu.Unlock()
return append([]string(nil), ui.posts...)
}
func lastContains(ui *fakeUI, sub string) bool {
for _, p := range fakePosts(ui) {
if strings.Contains(p, sub) {
return true
}
}
return false
}
func TestRunner_HelpCommand(t *testing.T) {
r, _, uis := newTestRunner(t, t.TempDir())
r.OnMention(context.Background(), "C", "T1", "<@BOT> help")
if !lastContains(uis.ui("T1"), "trackerbot") {
t.Fatalf("help not posted: %v", fakePosts(uis.ui("T1")))
}
// An empty mention also yields help — and starts no run.
r.OnMention(context.Background(), "C", "T2", "<@BOT>")
if !lastContains(uis.ui("T2"), "trackerbot") {
t.Fatal("empty mention should post help")
}
}
func TestRunner_StatusAndCancel(t *testing.T) {
workDir := t.TempDir()
writeWorkflow(t, workDir, "gate.dip", gateDip)
r, _, uis := newTestRunner(t, workDir)
runs := recordRuns(r)
r.OnMention(context.Background(), "C", "T1", "gate")
ui := uis.ui("T1")
awaitGate(t, ui) // run is active, blocked at its gate
// status reports the run.
r.OnMention(context.Background(), "C", "T1", "status")
if !lastContains(ui, "running") && !lastContains(ui, "starting") {
t.Fatalf("status not reported: %v", fakePosts(ui))
}
// cancel stops it.
r.OnMention(context.Background(), "C", "T1", "cancel")
if !lastContains(ui, "cancelling") {
t.Fatalf("cancel not acknowledged: %v", fakePosts(ui))
}
run := runs.first(t, "T1")
select {
case <-run.Done():
case <-time.After(5 * time.Second):
t.Fatal("run did not stop after cancel")
}
if run.State() != tracker.RunCanceled {
t.Fatalf("state after cancel = %s, want canceled", run.State())
}
}
func TestRunner_RunsListAndUnknownStatus(t *testing.T) {
r, _, uis := newTestRunner(t, t.TempDir())
// No runs yet.
r.OnMention(context.Background(), "C", "T0", "runs")
if !lastContains(uis.ui("T0"), "No runs") {
t.Fatalf("expected empty runs list: %v", fakePosts(uis.ui("T0")))
}
// status in a thread with no run.
r.OnMention(context.Background(), "C", "T0", "status")
if !lastContains(uis.ui("T0"), "No run in this thread") {
t.Fatalf("expected no-run status: %v", fakePosts(uis.ui("T0")))
}
}
func TestRunner_WorkflowsCommand(t *testing.T) {
r, _, uis := newTestRunner(t, t.TempDir())
r.OnMention(context.Background(), "C", "T0", "workflows")
posts := strings.Join(fakePosts(uis.ui("T0")), "\n")
for _, want := range []string{"Workflows you can run", "build_product"} {
if !strings.Contains(posts, want) {
t.Errorf("workflows list missing %q:\n%s", want, posts)
}
}
}