aboutsummaryrefslogtreecommitdiff
path: root/internal/engine/undo_identity_test.go
blob: 9c93b3b4a139989e7edcc4024550a9d7ea092b4b (plain) (blame)
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
// SPDX-License-Identifier: GPL-3.0-or-later

package engine

import (
	"context"
	"os"
	"path/filepath"
	"sort"
	"strings"
	"testing"
	"time"

	"krino/internal/journal"
	"krino/internal/plan"
	"krino/internal/trash"
)

// appliedRun makes each directory of files under the sandbox home (a map of
// directory name to file name to content, every file two hours old), writes
// rules for each, and applies every chain of every directory in one run. It
// returns the engine, the run id, the home directory and the log path.
func appliedRun(t *testing.T, files map[string]map[string]string, rules map[string]string) (*Engine, string, string, string) {
	t.Helper()
	h := sandbox(t)
	old := time.Now().Add(-2 * time.Hour)
	var names []string
	for dir, fs := range files {
		names = append(names, dir)
		for name, body := range fs {
			p := filepath.Join(h, dir, 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)
			}
			if err := os.Chtimes(p, old, old); err != nil {
				t.Fatal(err)
			}
		}
	}
	sort.Strings(names)
	main := writeConfig(t, h, `(include "`+strings.Join(names, `" "`)+`")`, rules)
	e, errs := Load(main)
	if len(errs) > 0 {
		t.Fatal(errs)
	}
	logPath := filepath.Join(h, ".local", "state", "krino", "krino.log")
	j, err := journal.Open(logPath)
	if err != nil {
		t.Fatal(err)
	}
	defer j.Close()
	run := journal.NewRunID(time.Now())
	claims := plan.NewClaims()
	for _, d := range e.Dirs {
		dp, err := e.Plan(context.Background(), d, claims)
		if err != nil {
			t.Fatal(err)
		}
		approved := map[string]bool{}
		for _, c := range dp.Chains {
			approved[c.File.Rel] = true
		}
		res, err := e.Apply(context.Background(), dp, approved, j, run)
		if err != nil || res.Failed != 0 {
			t.Fatalf("apply %s: %v, %+v", d.Name, err, res)
		}
	}
	return e, run, h, logPath
}

// undoFileNamed returns the plan's file for dir and name, failing the test
// when there is none.
func undoFileNamed(t *testing.T, up *UndoPlan, dir, name string) UndoFile {
	t.Helper()
	for _, f := range up.Files {
		if f.Dir == dir && f.File == name {
			return f
		}
	}
	t.Fatalf("no %s/%s in the undo plan: %+v", dir, name, up.Files)
	return UndoFile{}
}

// TestUndoRefusesAReusedTrashEntry: the Trash is emptied and another file of
// the same name trashed after the run; undo must not restore that file in
// place of the one the run trashed (review M2).
func TestUndoRefusesAReusedTrashEntry(t *testing.T) {
	e, run, h, _ := appliedRun(t, map[string]map[string]string{"dl": {"a.pdf": "one"}},
		map[string]string{"dl": "(path \"~/dl\")\n(rule \"r\" (rename \"r-{name}\") (delete))\n"})
	if err := os.RemoveAll(trash.Dir()); err != nil {
		t.Fatal(err)
	}
	other := filepath.Join(h, "Documents", "r-a.pdf")
	if err := os.MkdirAll(filepath.Dir(other), 0o755); err != nil {
		t.Fatal(err)
	}
	if err := os.WriteFile(other, []byte("an unrelated document"), 0o644); err != nil {
		t.Fatal(err)
	}
	if entry, err := trash.Put(other); err != nil || entry != "r-a.pdf" {
		t.Fatalf("trash.Put = %q, %v", entry, err)
	}
	up, err := e.PlanUndo(run)
	if err != nil {
		t.Fatal(err)
	}
	if f := undoFileNamed(t, up, "dl", "a.pdf"); !strings.Contains(f.Refused, "trash entry") {
		t.Errorf("Refused = %q; want a refusal naming the trash entry", f.Refused)
	}
}

// TestUndoRefusesATrashEntryThatChanged: a trash entry that is no longer the
// file the run put there (its size changed) is refused, like a moved file
// that changed (review M2).
func TestUndoRefusesATrashEntryThatChanged(t *testing.T) {
	e, run, _, _ := appliedRun(t, map[string]map[string]string{"dl": {"a.pdf": "one"}},
		map[string]string{"dl": "(path \"~/dl\")\n(rule \"r\" (delete))\n"})
	if err := os.WriteFile(filepath.Join(trash.Dir(), "files", "a.pdf"), []byte("a longer, different body"), 0o644); err != nil {
		t.Fatal(err)
	}
	up, err := e.PlanUndo(run)
	if err != nil {
		t.Fatal(err)
	}
	if f := undoFileNamed(t, up, "dl", "a.pdf"); f.Refused == "" {
		t.Error("a changed trash entry was not refused")
	}
}

// TestUndoKeepsSameNamedFilesOfTwoDirectoriesApart: a.pdf from dl and
// a.pdf from scans, moved in one run, are two files to undo, each refused or
// restored on its own (review M7).
func TestUndoKeepsSameNamedFilesOfTwoDirectoriesApart(t *testing.T) {
	e, run, h, logPath := appliedRun(t,
		map[string]map[string]string{"dl": {"a.pdf": "from dl"}, "scans": {"a.pdf": "from scans"}},
		map[string]string{
			"dl":    "(path \"~/dl\")\n(rule \"r\" (move \"~/Archive\"))\n",
			"scans": "(path \"~/scans\")\n(rule \"r\" (move \"~/Archive\"))\n",
		})
	up, err := e.PlanUndo(run)
	if err != nil {
		t.Fatal(err)
	}
	if len(up.Files) != 2 {
		t.Fatalf("undo plan has %d files, want 2: %+v", len(up.Files), up.Files)
	}
	scansCopy := undoFileNamed(t, up, "scans", "a.pdf").Steps[0].Src
	if err := os.WriteFile(scansCopy, []byte("from scans, edited since"), 0o644); err != nil {
		t.Fatal(err)
	}
	up, err = e.PlanUndo(run)
	if err != nil {
		t.Fatal(err)
	}
	if undoFileNamed(t, up, "scans", "a.pdf").Refused == "" || undoFileNamed(t, up, "dl", "a.pdf").Refused != "" {
		t.Fatalf("want only the edited scans file refused: %+v", up.Files)
	}
	j, err := journal.Open(logPath)
	if err != nil {
		t.Fatal(err)
	}
	defer j.Close()
	if _, err := e.ApplyUndo(context.Background(), up, j, journal.NewRunID(time.Now())); err != nil {
		t.Fatal(err)
	}
	if b, err := os.ReadFile(filepath.Join(h, "dl", "a.pdf")); err != nil || string(b) != "from dl" {
		t.Errorf("dl/a.pdf after undo: %q, %v", b, err)
	}
}

// TestUndoRechecksAtExecution: a file edited after the undo was planned (for
// example while its review was open) is not moved back (review undo F8).
func TestUndoRechecksAtExecution(t *testing.T) {
	e, run, h, logPath := appliedRun(t, map[string]map[string]string{"dl": {"a.pdf": "one"}},
		map[string]string{"dl": "(path \"~/dl\")\n(rule \"r\" (move \"Out\"))\n"})
	up, err := e.PlanUndo(run)
	if err != nil {
		t.Fatal(err)
	}
	moved := filepath.Join(h, "dl", "Out", "a.pdf")
	if err := os.WriteFile(moved, []byte("edited during review"), 0o644); err != nil {
		t.Fatal(err)
	}
	j, err := journal.Open(logPath)
	if err != nil {
		t.Fatal(err)
	}
	defer j.Close()
	res, err := e.ApplyUndo(context.Background(), up, j, journal.NewRunID(time.Now()))
	if err != nil {
		t.Fatal(err)
	}
	if res.Failed != 1 {
		t.Errorf("Failed = %d, want 1", res.Failed)
	}
	if b, _ := os.ReadFile(moved); string(b) != "edited during review" {
		t.Errorf("the edited file was moved or changed: %q", b)
	}
}

// TestUndoLeavesNoDirectoriesBehind: undo passing a file back through a
// directory it had already removed for another file recreates it; that
// directory must be gone again when the undo ends (review undo F6).
func TestUndoLeavesNoDirectoriesBehind(t *testing.T) {
	e, run, h, logPath := appliedRun(t, map[string]map[string]string{"dl": {"a.pdf": "one", "b.pdf": "two"}},
		map[string]string{"dl": "(path \"~/dl\")\n(rule \"r\" (move \"Out/{mtime:%Y}\") (move \"Out\"))\n"})
	up, err := e.PlanUndo(run)
	if err != nil {
		t.Fatal(err)
	}
	j, err := journal.Open(logPath)
	if err != nil {
		t.Fatal(err)
	}
	defer j.Close()
	if res, err := e.ApplyUndo(context.Background(), up, j, journal.NewRunID(time.Now())); err != nil || res.Failed != 0 {
		t.Fatalf("undo: %v, %+v", err, res)
	}
	if _, err := os.Stat(filepath.Join(h, "dl", "Out")); !os.IsNotExist(err) {
		t.Errorf("dl/Out is left behind: %v", err)
	}
}