-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathtea_test.go
169 lines (130 loc) · 2.93 KB
/
tea_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
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
package tea
import (
"errors"
"io"
"os"
"runtime"
"strings"
"testing"
"time"
"github.com/MarvinJWendt/testza"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/x/exp/teatest"
"github.com/satisfactorymodding/ficsit-cli/cfg"
"github.com/satisfactorymodding/ficsit-cli/cli"
"github.com/satisfactorymodding/ficsit-cli/tea/scenes"
)
func init() {
cfg.SetDefaults()
}
func TestTea(t *testing.T) {
if runtime.GOOS == "windows" {
// Windows just sucks
return
}
serverLocation := os.Getenv("SF_DEDICATED_SERVER")
if serverLocation == "" {
return
}
ctx, err := cli.InitCLI(false)
testza.AssertNoError(t, err)
ctx.Provider = cli.MockProvider{}
err = ctx.Wipe()
testza.AssertNoError(t, err)
err = ctx.ReInit()
testza.AssertNoError(t, err)
root := newModel(ctx)
m := scenes.NewMainMenu(root)
tm := teatest.NewTestModel(
t, m,
teatest.WithInitialTermSize(70, 35),
)
t.Cleanup(func() {
if err := tm.Quit(); err != nil {
t.Fatal(err)
}
})
time.Sleep(time.Second)
// Go to Installations
press(tm, tea.KeyEnter)
// Create new installation
write(tm, "n")
// Enter installation path
write(tm, serverLocation)
// Accept path
press(tm, tea.KeyEnter)
// Go back to main menu
write(tm, "q")
// Go to all mods
press(tm, tea.KeyDown)
press(tm, tea.KeyDown)
press(tm, tea.KeyDown)
press(tm, tea.KeyEnter)
// Filter for mod
write(tm, "/")
write(tm, "Refined Power")
press(tm, tea.KeyEnter)
// Select mod
press(tm, tea.KeyEnter)
// Install mod
press(tm, tea.KeyEnter)
// Go back to main menu
write(tm, "q")
// Apply changes
press(tm, tea.KeyDown)
press(tm, tea.KeyDown)
press(tm, tea.KeyDown)
eat(tm)
press(tm, tea.KeyEnter)
i := 0
buffer := ""
for {
s := read(tm)
buffer += "\n-------------------------\n" + s
if strings.Contains(s, "Done! Press Enter to return") {
break
}
if strings.Contains(s, "Cancelled! Press Enter to return") {
testza.AssertNoError(t, errors.New("installation cancelled"))
println(buffer)
break
}
i++
if i >= 60 {
testza.AssertNoError(t, errors.New("failed installing"))
println(buffer)
return
}
time.Sleep(time.Second)
}
eat(tm)
// Go back to main menu
press(tm, tea.KeyEnter)
// Exit program
press(tm, tea.KeyDown)
press(tm, tea.KeyDown)
press(tm, tea.KeyEnter)
}
// dump the current tea buffer to stderr
func dump(tm *teatest.TestModel) { // nolint
_, _ = io.Copy(os.Stderr, tm.Output())
}
// eat the current tea buffer
func eat(tm *teatest.TestModel) {
_, _ = io.ReadAll(tm.Output())
}
// read reads the current tea buffer
func read(tm *teatest.TestModel) string {
out, _ := io.ReadAll(tm.Output())
return string(out)
}
func press(tm *teatest.TestModel, key tea.KeyType) {
println("Pressing", key.String())
tm.Send(tea.KeyMsg{Type: key})
time.Sleep(time.Millisecond * 250)
}
func write(tm *teatest.TestModel, txt string) {
println("Writing", txt)
tm.Type(txt)
time.Sleep(time.Millisecond * 250)
}