Repository navigation
Expand file tree
/
Copy pathcli_edit_test.go
More file actions
267 lines (245 loc) · 8.67 KB
/
Copy pathcli_edit_test.go
File metadata and controls
267 lines (245 loc) · 8.67 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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
package main
import (
"database/sql"
"encoding/json"
"os"
"os/exec"
"path/filepath"
"strings"
"testing"
"time"
"github.com/aliasproject/notch/internal/db"
)
// TestMain lets a test run the notch CLI in a child process: with
// NOTCH_TEST_CLI=1 the test binary acts as notch itself. The CLI exits
// the process on errors, so it can't run in the test's own.
func TestMain(m *testing.M) {
if os.Getenv("NOTCH_TEST_CLI") == "1" {
main()
os.Exit(0)
}
os.Exit(m.Run())
}
// notchCLI runs `notch args...` against the database at dbPath and
// returns stdout, stderr and whether it exited 0.
func notchCLI(t *testing.T, dbPath string, args ...string) (string, string, bool) {
t.Helper()
cmd := exec.Command(os.Args[0], args...)
cmd.Env = append(os.Environ(), "NOTCH_TEST_CLI=1", "NOTCH_DB="+dbPath)
var out, errOut strings.Builder
cmd.Stdout, cmd.Stderr = &out, &errOut
err := cmd.Run()
if err != nil {
if _, ok := err.(*exec.ExitError); !ok {
t.Fatalf("run notch %v: %v", args, err)
}
}
return out.String(), errOut.String(), err == nil
}
// seedTask creates entries 1..n (stopped) of task "build" on a fresh
// database's project, plus entry n+1 of task "other", and returns the
// database path and the project's ID.
func seedTask(t *testing.T, n int) (string, int64) {
t.Helper()
path := filepath.Join(t.TempDir(), "notch.db")
d, err := db.Open(path)
if err != nil {
t.Fatalf("db.Open: %v", err)
}
defer d.Close()
c, err := d.CreateClient("Acme Co", 85)
if err != nil {
t.Fatalf("CreateClient: %v", err)
}
p, err := d.CreateProject(c.ID, "Website")
if err != nil {
t.Fatalf("CreateProject: %v", err)
}
for i := 0; i <= n; i++ {
task := "build"
if i == n {
task = "other"
}
e, err := d.StartEntry(p.ID, task)
if err != nil {
t.Fatalf("StartEntry: %v", err)
}
if err := d.StopEntry(e.ID); err != nil {
t.Fatalf("StopEntry: %v", err)
}
}
return path, p.ID
}
func decodeEntries(t *testing.T, s string) []entryOutput {
t.Helper()
var out []entryOutput
if err := json.Unmarshal([]byte(s), &out); err != nil {
t.Fatalf("decode %q: %v", s, err)
}
return out
}
func TestEditBilling_OneEntry(t *testing.T) {
path, _ := seedTask(t, 2)
_, stderr, ok := notchCLI(t, path, "edit", "--id", "1", "--paid")
if ok || !strings.Contains(stderr, "must be invoiced before it can be paid") {
t.Fatalf("--paid on an uninvoiced entry: ok=%v stderr=%q, want an error", ok, stderr)
}
stdout, stderr, ok := notchCLI(t, path, "edit", "--id", "1", "--invoiced", "--paid", "-json")
if !ok {
t.Fatalf("--invoiced --paid: %s", stderr)
}
var e entryOutput
if err := json.Unmarshal([]byte(stdout), &e); err != nil {
t.Fatalf("decode %q: %v", stdout, err)
}
if e.ID != 1 || !e.Invoiced || !e.Paid {
t.Fatalf("-json = %+v, want entry 1 invoiced and paid", e)
}
stdout, stderr, ok = notchCLI(t, path, "edit", "--id", "1", "--invoiced=false", "-json")
if !ok {
t.Fatalf("--invoiced=false: %s", stderr)
}
if err := json.Unmarshal([]byte(stdout), &e); err != nil {
t.Fatalf("decode %q: %v", stdout, err)
}
if e.Invoiced || e.Paid {
t.Fatalf("-json = %+v, want un-invoicing to clear paid too", e)
}
// Entry 2 is untouched throughout.
stdout, _, _ = notchCLI(t, path, "entries", "--project", "1", "--task", "build", "-json")
for _, x := range decodeEntries(t, stdout) {
if x.ID == 2 && (x.Invoiced || x.Paid) {
t.Errorf("entry 2 = %+v, want it untouched", x)
}
}
}
func TestEditBilling_WholeTask(t *testing.T) {
path, project := seedTask(t, 2)
pid := "1"
if project != 1 {
t.Fatalf("project ID = %d, want 1", project)
}
_, stderr, ok := notchCLI(t, path, "edit", "--from-project", pid, "--from-task", "build", "--paid")
if ok || !strings.Contains(stderr, "must be invoiced") {
t.Fatalf("--paid on uninvoiced entries: ok=%v stderr=%q, want an error", ok, stderr)
}
stdout, stderr, ok := notchCLI(t, path, "edit", "--from-project", pid, "--from-task", "build", "--invoiced", "-json")
if !ok {
t.Fatalf("--invoiced: %s", stderr)
}
got := decodeEntries(t, stdout)
if len(got) != 2 {
t.Fatalf("-json listed %d entries, want the task's 2", len(got))
}
for _, x := range got {
if !x.Invoiced || x.Paid {
t.Errorf("entry %d = %+v, want invoiced, not paid", x.ID, x)
}
}
// Rename and mark paid in one call: billing applies to the renamed entries.
stdout, stderr, ok = notchCLI(t, path, "edit", "--from-project", pid, "--from-task", "build", "--task", "build v2", "--paid")
if !ok || strings.TrimSpace(stdout) != "updated 2 entries" {
t.Fatalf("rename + --paid: ok=%v stdout=%q stderr=%q", ok, stdout, stderr)
}
stdout, _, _ = notchCLI(t, path, "entries", "--project", pid, "--task", "build v2", "-json")
for _, x := range decodeEntries(t, stdout) {
if !x.Invoiced || !x.Paid {
t.Errorf("entry %d = %+v, want invoiced and paid", x.ID, x)
}
}
stdout, _, _ = notchCLI(t, path, "entries", "--project", pid, "--task", "other", "-json")
for _, x := range decodeEntries(t, stdout) {
if x.Invoiced || x.Paid {
t.Errorf("other task's entry %d = %+v, want it untouched", x.ID, x)
}
}
}
// "--invoiced false" must fail, not set invoiced and drop the rest.
func TestEdit_RejectsSpaceSeparatedBool(t *testing.T) {
path, _ := seedTask(t, 1)
_, stderr, ok := notchCLI(t, path, "edit", "--id", "1", "--invoiced", "false", "--paid=false")
if ok || !strings.Contains(stderr, `unexpected argument "false"`) {
t.Fatalf("ok=%v stderr=%q, want an unexpected-argument error", ok, stderr)
}
stdout, _, _ := notchCLI(t, path, "entries", "--project", "1", "--task", "build", "-json")
if e := decodeEntries(t, stdout); len(e) != 1 || e[0].Invoiced {
t.Fatalf("entries = %+v, want entry 1 left uninvoiced", e)
}
}
func TestEditBilling_RunningEntry(t *testing.T) {
path, _ := seedTask(t, 1)
if _, stderr, ok := notchCLI(t, path, "start", "--project", "1", "--task", "live"); !ok {
t.Fatalf("start: %s", stderr)
}
// The running entry (no --id), and its task as a whole.
for _, args := range [][]string{
{"edit", "--invoiced"},
{"edit", "--from-project", "1", "--from-task", "live", "--invoiced", "--paid", "--client-name", "New Co"},
} {
_, stderr, ok := notchCLI(t, path, args...)
if ok || !strings.Contains(stderr, "stop the timer") {
t.Fatalf("%v: ok=%v stderr=%q, want a stop-the-timer error", args, ok, stderr)
}
}
// The rejected task edit must not have created "New Co".
stdout, _, _ := notchCLI(t, path, "clients")
if strings.Contains(stdout, "New Co") {
t.Errorf("clients = %q, want no client created by a rejected edit", stdout)
}
// Stopping it in the same edit makes it invoiceable.
start := time.Now().Add(-2 * time.Minute).Format(time.RFC3339)
end := time.Now().Add(-time.Minute).Format(time.RFC3339)
stdout, stderr, ok := notchCLI(t, path, "edit", "--invoiced", "--start", start, "--end", end, "-json")
if !ok {
t.Fatalf("--invoiced --end: %s", stderr)
}
var e entryOutput
if err := json.Unmarshal([]byte(stdout), &e); err != nil {
t.Fatalf("decode %q: %v", stdout, err)
}
if !e.Invoiced || e.EndTime == "" {
t.Fatalf("-json = %+v, want stopped and invoiced", e)
}
}
// Older versions could store paid without invoiced. Such an entry must
// stay editable for its other fields; only a billing flag applies the rules.
func TestEdit_LegacyPaidNotInvoicedStaysEditable(t *testing.T) {
path, _ := seedTask(t, 1)
raw, err := sql.Open("sqlite", path)
if err != nil {
t.Fatalf("sql.Open: %v", err)
}
if _, err := raw.Exec(`UPDATE entries SET paid=1, invoiced=0 WHERE id=1`); err != nil {
t.Fatalf("seed legacy state: %v", err)
}
raw.Close()
if _, stderr, ok := notchCLI(t, path, "edit", "--id", "1", "--task", "renamed"); !ok {
t.Fatalf("rename one entry: %s", stderr)
}
if _, stderr, ok := notchCLI(t, path, "edit", "--from-project", "1", "--from-task", "renamed", "--task", "again"); !ok {
t.Fatalf("rename the task: %s", stderr)
}
// Un-invoicing clears the stray paid, as in the TUI.
stdout, stderr, ok := notchCLI(t, path, "edit", "--id", "1", "--invoiced=false", "-json")
if !ok {
t.Fatalf("--invoiced=false: %s", stderr)
}
var e entryOutput
if err := json.Unmarshal([]byte(stdout), &e); err != nil {
t.Fatalf("decode %q: %v", stdout, err)
}
if e.Invoiced || e.Paid {
t.Fatalf("-json = %+v, want neither invoiced nor paid", e)
}
}
func TestEdit_UnexpectedArgumentHint(t *testing.T) {
path, _ := seedTask(t, 1)
_, stderr, _ := notchCLI(t, path, "edit", "--id", "1", "--task", "Fix", "login")
if !strings.Contains(stderr, `unexpected argument "login" -- quote values with spaces`) {
t.Errorf("stderr = %q, want the quoting hint", stderr)
}
_, stderr, _ = notchCLI(t, path, "edit", "--id", "1", "--invoiced", "1", "--paid")
if !strings.Contains(stderr, `like --invoiced=1`) {
t.Errorf("stderr = %q, want the = hint for a bool value", stderr)
}
}