From a2cb20851499e9c00bd3bf642680a9ce3148cae8 Mon Sep 17 00:00:00 2001 From: Lukasz Kasprzak Date: Thu, 17 Sep 2026 10:15:45 +0200 Subject: gui: name the other copy of a duplicate, and offer to keep this one instead --- gui/internal/model/plan_test.go | 55 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) (limited to 'gui/internal/model/plan_test.go') diff --git a/gui/internal/model/plan_test.go b/gui/internal/model/plan_test.go index a54768c..0aa0361 100644 --- a/gui/internal/model/plan_test.go +++ b/gui/internal/model/plan_test.go @@ -408,3 +408,58 @@ func TestAgeText(t *testing.T) { t.Errorf("a file from the future = %q, want 0m", got) } } + +// TestKeepThisCopy: choosing the downloaded copy over the filed one puts it +// in the other's place and sends the other to the Trash, where undo can +// still reach it (his request, 2026-09-17). +func TestKeepThisCopy(t *testing.T) { + conf := "(path \"~/dl\")\n(rule \"dupes\" (when (duplicate \"~/docs\")) (move \"Dupes\"))\n" + e, h := sandboxDir(t, conf, map[string]string{"report.pdf": "the same bytes"}) + filed := filepath.Join(h, "docs", "work", "report.pdf") + if err := os.MkdirAll(filepath.Dir(filed), 0o755); err != nil { + t.Fatal(err) + } + if err := os.WriteFile(filed, []byte("the same bytes"), 0o644); err != nil { + t.Fatal(err) + } + tab := planTab(t, e) + if len(tab.Rows) != 1 || tab.Rows[0].DuplicateOf != filed { + t.Fatalf("row = %+v, want it a duplicate of %s", tab.Rows[0], filed) + } + if err := tab.KeepThisCopy(0); err != nil { + t.Fatal(err) + } + step := tab.Rows[0].Steps[0] + if step.Kind != plan.Move || step.Dst != filed || step.Displaces != filed { + t.Fatalf("step = %+v", step) + } + if _, err := tab.Apply(context.Background()); err != nil { + t.Fatal(err) + } + // The download is now the filed copy, with its own bytes. + if body, err := os.ReadFile(filed); err != nil || string(body) != "the same bytes" { + t.Errorf("the kept copy is not in place: %v", err) + } + if _, err := os.Stat(filepath.Join(h, "dl", "report.pdf")); !os.IsNotExist(err) { + t.Errorf("the download is still where it was: %v", err) + } + // The copy it replaced went to the Trash, not to oblivion. + entries, _ := os.ReadDir(filepath.Join(h, ".local", "share", "Trash", "files")) + if len(entries) != 1 { + t.Errorf("the Trash holds %d files, want the replaced one", len(entries)) + } +} + +// TestKeepThisCopyRefusesANonDuplicate: the choice only means something for +// a file krino found another copy of. +func TestKeepThisCopyRefusesANonDuplicate(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.KeepThisCopy(0); err == nil { + t.Error("a file that duplicates nothing was accepted") + } + if err := tab.KeepThisCopy(9); err == nil { + t.Error("a row that does not exist was accepted") + } +} -- cgit v1.3