-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain_test.go
More file actions
192 lines (171 loc) · 4.7 KB
/
main_test.go
File metadata and controls
192 lines (171 loc) · 4.7 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
package main
import (
"fmt"
"io"
"os"
"path/filepath"
"strings"
"testing"
)
// captureOutput redirects os.Stdout and os.Stderr, runs fn, then restores them.
// Returns captured stdout and stderr as strings.
func captureOutput(fn func()) (stdout, stderr string) {
oldStdout := os.Stdout
rOut, wOut, _ := os.Pipe()
os.Stdout = wOut
oldStderr := os.Stderr
rErr, wErr, _ := os.Pipe()
os.Stderr = wErr
fn()
wOut.Close()
wErr.Close()
outBytes, _ := io.ReadAll(rOut)
errBytes, _ := io.ReadAll(rErr)
os.Stdout = oldStdout
os.Stderr = oldStderr
return string(outBytes), string(errBytes)
}
func TestRoute_Version(t *testing.T) {
for _, flag := range []string{"--version", "-version", "-V"} {
t.Run(flag, func(t *testing.T) {
var code int
stdout, _ := captureOutput(func() {
code = route([]string{flag}, "1.2.3", nil, nil)
})
if code != 0 {
t.Errorf("expected exit 0, got %d", code)
}
if !strings.Contains(stdout, "lorah 1.2.3") {
t.Errorf("expected %q in stdout, got %q", "lorah 1.2.3", stdout)
}
})
}
}
func TestRoute_Help(t *testing.T) {
for _, flag := range []string{"--help", "-help", "-h"} {
t.Run(flag, func(t *testing.T) {
var code int
_, stderr := captureOutput(func() {
code = route([]string{flag}, "dev", nil, nil)
})
if code != 0 {
t.Errorf("expected exit 0, got %d", code)
}
if !strings.Contains(stderr, "Usage:") {
t.Errorf("expected usage in stderr, got %q", stderr)
}
})
}
}
// TestRoute_NoArgs verifies that no arguments prints usage to stderr and exits 1.
// This is distinct from --help which exits 0 (per cli.md §3).
func TestRoute_NoArgs(t *testing.T) {
var code int
_, stderr := captureOutput(func() {
code = route([]string{}, "dev", nil, nil)
})
if code != 1 {
t.Errorf("expected exit 1, got %d", code)
}
if !strings.Contains(stderr, "Usage:") {
t.Errorf("expected usage in stderr, got %q", stderr)
}
}
func TestRoute_Run(t *testing.T) {
var calledFile string
var calledFlags []string
runFn := func(file string, flags []string) {
calledFile = file
calledFlags = flags
}
code := route([]string{"run", "prompt.md", "--max-turns", "50"}, "dev", runFn, nil)
if code != 0 {
t.Errorf("expected exit 0, got %d", code)
}
if calledFile != "prompt.md" {
t.Errorf("expected prompt file %q, got %q", "prompt.md", calledFile)
}
if len(calledFlags) != 2 || calledFlags[0] != "--max-turns" || calledFlags[1] != "50" {
t.Errorf("unexpected flags: %v", calledFlags)
}
}
func TestRoute_Task(t *testing.T) {
var calledDir string
var calledArgs []string
taskFn := func(dir string, args []string) error {
calledDir = dir
calledArgs = args
return nil
}
code := route([]string{"task", "list"}, "dev", nil, taskFn)
if code != 0 {
t.Errorf("expected exit 0, got %d", code)
}
if calledDir != ".lorah" {
t.Errorf("expected default dir %q, got %q", ".lorah", calledDir)
}
if len(calledArgs) != 1 || calledArgs[0] != "list" {
t.Errorf("unexpected args: %v", calledArgs)
}
}
func TestRoute_DirFlag(t *testing.T) {
dataDir := t.TempDir()
var cmdErr error
captureOutput(func() {
err := route([]string{"--dir=" + dataDir, "task", "create", "--subject=test"}, "dev", nil, taskCmd)
if err != 0 {
cmdErr = fmt.Errorf("exit %d", err)
}
})
if cmdErr != nil {
t.Errorf("unexpected error: %v", cmdErr)
}
if _, err := os.Stat(filepath.Join(dataDir, "tasks.json")); err != nil {
t.Errorf("expected tasks.json in %s, got %v", dataDir, err)
}
}
func TestRoute_DirFlagSpaceSeparated(t *testing.T) {
dataDir := t.TempDir()
var cmdErr error
captureOutput(func() {
err := route([]string{"--dir", dataDir, "task", "create", "--subject=test"}, "dev", nil, taskCmd)
if err != 0 {
cmdErr = fmt.Errorf("exit %d", err)
}
})
if cmdErr != nil {
t.Errorf("unexpected error: %v", cmdErr)
}
if _, err := os.Stat(filepath.Join(dataDir, "tasks.json")); err != nil {
t.Errorf("expected tasks.json in %s, got %v", dataDir, err)
}
}
func TestRoute_DefaultDir(t *testing.T) {
orig, err := os.Getwd()
if err != nil {
t.Fatal(err)
}
dir := t.TempDir()
if err := os.Chdir(dir); err != nil {
t.Fatal(err)
}
t.Cleanup(func() { os.Chdir(orig) })
captureOutput(func() {
route([]string{"task", "create", "--subject=test"}, "dev", nil, taskCmd)
})
if _, err := os.Stat(filepath.Join(dir, ".lorah", "tasks.json")); err != nil {
t.Errorf("expected .lorah/tasks.json in %s, got %v", dir, err)
}
}
func TestRoute_UnknownCommand(t *testing.T) {
var code int
_, stderr := captureOutput(func() {
code = route([]string{"unknown"}, "dev", nil, nil)
})
if code != 1 {
t.Errorf("expected exit 1, got %d", code)
}
if !strings.Contains(stderr, "Unknown command: unknown") {
t.Errorf("expected error message in stderr, got %q", stderr)
}
}