aboutsummaryrefslogtreecommitdiff
path: root/internal/engine/undo_identity_test.go
diff options
context:
space:
mode:
authorLukasz Kasprzak <lukas@labunix.xyz>2026-09-14 21:30:49 +0200
committerLukasz Kasprzak <lukas@labunix.xyz>2026-09-14 21:30:49 +0200
commit643eac4a2cba7b34b8a33af1b3dbdc54d9816a53 (patch)
tree5c53881b5d85e5042080a45e070ef3922904389c /internal/engine/undo_identity_test.go
parent7e0d8494074398854f30feb75211c52ea5cc2635 (diff)
downloadkrino-643eac4a2cba7b34b8a33af1b3dbdc54d9816a53.tar.gz
krino-643eac4a2cba7b34b8a33af1b3dbdc54d9816a53.zip
plan 9: undo checks trash identity, groups by directory, re-checks at execution, removes directories it recreates
Diffstat (limited to 'internal/engine/undo_identity_test.go')
-rw-r--r--internal/engine/undo_identity_test.go225
1 files changed, 225 insertions, 0 deletions
diff --git a/internal/engine/undo_identity_test.go b/internal/engine/undo_identity_test.go
new file mode 100644
index 0000000..9c93b3b
--- /dev/null
+++ b/internal/engine/undo_identity_test.go
@@ -0,0 +1,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)
+ }
+}