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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
|
// SPDX-License-Identifier: GPL-3.0-or-later
package model
import (
"context"
"errors"
"os"
"path/filepath"
"sort"
"strings"
"testing"
"time"
"krino/internal/engine"
"krino/internal/lock"
"krino/internal/plan"
)
// sandboxDir builds a home with one configured directory holding files, and
// returns the loaded engine and the home. HOME and every XDG_* live inside
// the test's temporary directory, as the engine's own tests do.
func sandboxDir(t *testing.T, conf string, files map[string]string) (*engine.Engine, string) {
t.Helper()
h := t.TempDir()
t.Setenv("HOME", h)
for _, v := range []string{"XDG_CONFIG_HOME", "XDG_STATE_HOME", "XDG_DATA_HOME", "XDG_CACHE_HOME"} {
t.Setenv(v, "")
}
old := time.Now().Add(-2 * time.Hour)
for name, body := range files {
p := filepath.Join(h, "dl", name)
if err := os.MkdirAll(filepath.Dir(p), 0o755); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(p, []byte(body), 0o644); err != nil {
t.Fatal(err)
}
os.Chtimes(p, old, old)
}
cdir := filepath.Join(h, ".config", "krino")
if err := os.MkdirAll(filepath.Join(cdir, "dirs"), 0o755); err != nil {
t.Fatal(err)
}
main := filepath.Join(cdir, "krino.conf")
os.WriteFile(main, []byte("(include \"dl\")\n"), 0o644)
os.WriteFile(filepath.Join(cdir, "dirs", "dl.conf"), []byte(conf), 0o644)
e, errs := engine.Load(main)
if len(errs) > 0 {
t.Fatal(errs)
}
return e, h
}
func planTab(t *testing.T, e *engine.Engine) *PlanTab {
t.Helper()
tab, err := Plan(context.Background(), e, e.Dirs[0])
if err != nil {
t.Fatal(err)
}
t.Cleanup(func() { tab.Close() })
return tab
}
// TestPlanRowsAndCounts: the tab shows one row per file with steps, a row
// for a file krino could not decide about, the rule that matched, and the
// counts of the plan's summary line.
func TestPlanRowsAndCounts(t *testing.T) {
conf := "(path \"~/dl\")\n(max-read 1K)\n(exclude (name \"^keep-\"))\n" +
"(rule \"pdfs\" (when (type pdf)) (move \"Docs\"))\n" +
"(rule \"secret\" (when (content \"classified\")) (move \"Secret\"))\n"
e, _ := sandboxDir(t, conf, map[string]string{
"a.pdf": "one",
"keep-b.pdf": "two",
"c.txt": strings.Repeat("x", 2048), // over max-read: content unknown
"plain.txt": "nothing",
})
tab := planTab(t, e)
if tab.Counts.Scanned != 4 || tab.Counts.Acting != 1 || tab.Counts.Excluded != 1 {
t.Errorf("counts = %+v", tab.Counts)
}
var acting, attention int
for _, r := range tab.Rows {
if r.Actable {
acting++
if r.Rel != "a.pdf" || r.Rule != "pdfs" || !r.Selected {
t.Errorf("acting row = %+v", r)
}
}
// A file no rule could act on that still raised a warning is shown
// and never selectable; a file with steps stays selectable even
// when another rule warned about it.
if len(r.Steps) == 0 {
attention++
if r.Rel != "c.txt" || len(r.Warnings) == 0 || r.Selected || r.Actable {
t.Errorf("needs-attention row = %+v", r)
}
}
}
if acting != 1 || attention != 1 {
t.Errorf("rows = %+v", tab.Rows)
}
}
// TestSelectionAndApply: only the selected files are acted on, the rest are
// logged as declined, and every row carries its outcome afterwards.
func TestSelectionAndApply(t *testing.T) {
conf := "(path \"~/dl\")\n(rule \"all\" (move \"Out\"))\n"
e, h := sandboxDir(t, conf, map[string]string{"a.pdf": "one", "b.pdf": "two"})
tab := planTab(t, e)
if tab.SelectedCount() != 2 {
t.Fatalf("rows do not start selected: %+v", tab.Rows)
}
tab.SelectNone()
if tab.SelectedCount() != 0 {
t.Fatal("SelectNone left rows selected")
}
for i, r := range tab.Rows {
if r.Rel == "a.pdf" {
tab.Toggle(i)
}
}
res, err := tab.Apply(context.Background())
if err != nil {
t.Fatal(err)
}
if res.Applied != 1 || res.Declined != 1 {
t.Errorf("result = %+v", res)
}
if _, err := os.Stat(filepath.Join(h, "dl", "Out", "a.pdf")); err != nil {
t.Errorf("the selected file was not moved: %v", err)
}
if _, err := os.Stat(filepath.Join(h, "dl", "b.pdf")); err != nil {
t.Errorf("the unselected file was moved: %v", err)
}
for _, r := range tab.Rows {
want := "done"
if r.Rel == "b.pdf" {
want = "declined"
}
if r.Outcome != want {
t.Errorf("%s: outcome %q, want %q", r.Rel, r.Outcome, want)
}
}
}
// TestReplaceWithTrash: "Trash instead" swaps the planned steps for one
// trash step, as the terminal review's t key does, and applying it trashes
// the file.
func TestReplaceWithTrash(t *testing.T) {
conf := "(path \"~/dl\")\n(rule \"all\" (move \"Out\"))\n"
e, h := sandboxDir(t, conf, map[string]string{"a.pdf": "one"})
tab := planTab(t, e)
if err := tab.Replace(0, plan.Trash); err != nil {
t.Fatal(err)
}
if tab.Rows[0].Steps[0].Kind != plan.Trash || tab.Rows[0].Rule != "(review)" {
t.Fatalf("row = %+v", tab.Rows[0])
}
if _, err := tab.Apply(context.Background()); err != nil {
t.Fatal(err)
}
if _, err := os.Stat(filepath.Join(h, "dl", "a.pdf")); !os.IsNotExist(err) {
t.Errorf("the file was not trashed: %v", err)
}
if entries, _ := os.ReadDir(filepath.Join(h, ".local", "share", "Trash", "files")); len(entries) != 1 {
t.Errorf("the Trash holds %d entries, want 1", len(entries))
}
if err := tab.Replace(0, plan.Move); err == nil {
t.Error("Replace accepted a move")
}
}
// TestPlanLeavesTheDirectoryAlone: planning changes no file, so a window
// can sit open on a plan.
func TestPlanLeavesTheDirectoryAlone(t *testing.T) {
conf := "(path \"~/dl\")\n(rule \"all\" (move \"Out\"))\n"
e, h := sandboxDir(t, conf, map[string]string{"a.pdf": "one"})
planTab(t, e)
if _, err := os.Stat(filepath.Join(h, "dl", "a.pdf")); err != nil {
t.Errorf("planning moved the file: %v", err)
}
if _, err := os.Stat(filepath.Join(h, "dl", "Out")); !os.IsNotExist(err) {
t.Errorf("planning created the destination: %v", err)
}
}
// TestPlanHoldsTheLock: while a plan is open nothing else may work in that
// directory - what the window shows stays the truth while the user chooses -
// and closing the tab lets the next run in (GUI design §3).
func TestPlanHoldsTheLock(t *testing.T) {
conf := "(path \"~/dl\")\n(rule \"all\" (move \"Out\"))\n"
e, _ := sandboxDir(t, conf, map[string]string{"a.pdf": "one"})
tab := planTab(t, e)
if _, err := lock.Acquire(context.Background(), e.Config.LockFile("dl"), false); !errors.Is(err, lock.ErrHeld) {
t.Fatalf("an open plan does not hold the lock: %v", err)
}
if err := tab.Close(); err != nil {
t.Fatal(err)
}
l, err := lock.Acquire(context.Background(), e.Config.LockFile("dl"), false)
if err != nil {
t.Fatalf("closing the tab did not release the lock: %v", err)
}
l.Release()
if err := tab.Close(); err != nil {
t.Errorf("closing twice: %v", err)
}
}
// TestApplyReleasesTheLock: once a plan has been applied it is history, so
// the directory is free again without closing the window (GUI design §3).
func TestApplyReleasesTheLock(t *testing.T) {
conf := "(path \"~/dl\")\n(rule \"all\" (move \"Out\"))\n"
e, _ := sandboxDir(t, conf, map[string]string{"a.pdf": "one"})
tab := planTab(t, e)
if _, err := tab.Apply(context.Background()); err != nil {
t.Fatal(err)
}
l, err := lock.Acquire(context.Background(), e.Config.LockFile("dl"), false)
if err != nil {
t.Fatalf("applying did not release the lock: %v", err)
}
l.Release()
if err := tab.Close(); err != nil {
t.Errorf("closing an applied tab: %v", err)
}
}
// TestPlanRefusesAHeldDirectory: a directory another krino is working in is
// not planned at all, and the message names it.
func TestPlanRefusesAHeldDirectory(t *testing.T) {
conf := "(path \"~/dl\")\n(rule \"all\" (move \"Out\"))\n"
e, _ := sandboxDir(t, conf, map[string]string{"a.pdf": "one"})
held, err := lock.Acquire(context.Background(), e.Config.LockFile("dl"), false)
if err != nil {
t.Fatal(err)
}
defer held.Release()
if _, err := Plan(context.Background(), e, e.Dirs[0]); !errors.Is(err, lock.ErrHeld) {
t.Fatalf("Plan = %v, want lock.ErrHeld", err)
} else if !strings.Contains(err.Error(), "dl") {
t.Errorf("the message does not name the directory: %v", err)
}
}
// TestRescanForgetsUnusedNames: a destination the last plan claimed but
// never used is free again on the next scan, so looking twice at the same
// directory does not creep up through name-1, name-2 (spec §7.4).
func TestRescanForgetsUnusedNames(t *testing.T) {
conf := "(path \"~/dl\")\n(rule \"all\" (move \"Out\") (rename \"same.pdf\"))\n"
e, _ := sandboxDir(t, conf, map[string]string{"a.pdf": "one", "b.pdf": "two"})
tab := planTab(t, e)
// Each file moves into Out and is then renamed; the second file's name
// is taken, so the plan reserves a suffixed one for it.
want := destinations(t, tab)
if len(want) != 4 || filepath.Base(want[2]) != "same.pdf" || filepath.Base(want[3]) != "same_1.pdf" {
t.Fatalf("first plan = %v", want)
}
// Looking again without applying anything: the names the closed plan
// reserved are free, so the second look reads the same as the first.
if err := tab.Close(); err != nil {
t.Fatal(err)
}
again, err := Plan(context.Background(), e, e.Dirs[0])
if err != nil {
t.Fatal(err)
}
if got := destinations(t, again); !equal(got, want) {
t.Errorf("second plan = %v, want %v", got, want)
}
// And again after an apply that applied nothing.
again.SelectNone()
if _, err := again.Apply(context.Background()); err != nil {
t.Fatal(err)
}
if err := again.Close(); err != nil {
t.Fatal(err)
}
third, err := Plan(context.Background(), e, e.Dirs[0])
if err != nil {
t.Fatal(err)
}
defer third.Close()
if got := destinations(t, third); !equal(got, want) {
t.Errorf("third plan = %v, want %v", got, want)
}
}
// destinations is every step's destination in the tab, sorted.
func destinations(t *testing.T, tab *PlanTab) []string {
t.Helper()
var out []string
for _, r := range tab.Rows {
for _, st := range r.Steps {
if st.Dst != "" {
out = append(out, st.Dst)
}
}
}
sort.Strings(out)
return out
}
func equal(a, b []string) bool {
if len(a) != len(b) {
return false
}
for i := range a {
if a[i] != b[i] {
return false
}
}
return true
}
|