package main import ( tea "github.com/charmbracelet/bubbletea" "os" "path/filepath" "strings" "testing" ) func TestFindDuplicates(t *testing.T) { es := []FileEntry{ {"/a/x", "x", 10, "h1"}, {"/b/x", "x", 10, "h1"}, {"/c/y", "y", 10, "h2"}, {"/d/z", "z", 20, "h1"}, } g := FindDuplicates(es) if len(g) != 1 || len(g[0].Files) != 2 || g[0].Files[0].FilePath != "/a/x" { t.Fatalf("%+v", g) } } func setup(t *testing.T) (string, review) { root := t.TempDir() var es []FileEntry for _, p := range []string{"a/f.bin", "b/f.bin", "c/f.bin"} { full := filepath.Join(root, p) os.MkdirAll(filepath.Dir(full), 0o755) os.WriteFile(full, []byte("same"), 0o644) es = append(es, FileEntry{full, "f.bin", 4, "h"}) } return root, newReview(root, es) } func TestMoveAndLog(t *testing.T) { root, r := setup(t) r.inGroup = true r.doMove("new/dir") if _, err := os.Stat(filepath.Join(root, "new/dir/f.bin")); err != nil { t.Fatal(err) } if _, err := os.Stat(filepath.Join(root, "a/f.bin")); err == nil { t.Error("source remains") } // destination collision refused r.fi = 0 r.doMove("b") if !r.msgErr { t.Error("expected collision error") } b, _ := os.ReadFile(filepath.Join(root, logFileName)) if !strings.Contains(string(b), "MOVE") { t.Errorf("log: %s", b) } } func TestMarkSafetyAndApply(t *testing.T) { root, r := setup(t) r.inGroup = true for i := 0; i < 3; i++ { r.fi = i r.toggleMark() } if len(r.marks) != 2 || !r.msgErr { t.Fatalf("marks=%d", len(r.marks)) } r.doApply() if len(r.entries) != 1 || len(r.groups) != 0 { t.Errorf("entries=%d groups=%d", len(r.entries), len(r.groups)) } if _, err := os.Stat(filepath.Join(root, trashDir, "a/f.bin")); err != nil { t.Error(err) } b, _ := os.ReadFile(filepath.Join(root, logFileName)) if strings.Count(string(b), "DELETE") != 2 { t.Errorf("log: %s", b) } entries, _, _ := BuildIndex(t.Context(), root, 0, nil) for _, e := range entries { if strings.Contains(e.FilePath, trashDir) || e.FileName == logFileName { t.Errorf("scanned %s", e.FilePath) } } } func TestLoadIndexDropsStale(t *testing.T) { root, r := setup(t) if _, err := WriteIndex(root, r.entries); err != nil { t.Fatal(err) } os.Remove(filepath.Join(root, "a/f.bin")) os.WriteFile(filepath.Join(root, "b/f.bin"), []byte("different size"), 0o644) es, dropped, _, err := LoadIndex(root) if err != nil || len(es) != 1 || dropped != 2 { t.Fatalf("%v %d %v", es, dropped, err) } if _, _, _, err := LoadIndex(t.TempDir()); err == nil { t.Error("expected error for missing index") } } func dirTestReview(root string) review { p := func(s string) string { return filepath.Join(root, s) } return newReview(root, []FileEntry{ {p("a/x/f1"), "f1", 10, "h1"}, {p("b/f1"), "f1", 10, "h1"}, {p("a/y/g1"), "g1", 20, "h2"}, {p("a/y/g2"), "g1", 20, "h2"}, {p("ab/f1"), "f1", 10, "h1"}, {p("top"), "top", 30, "h3"}, {p("a/top"), "top", 30, "h3"}, }) } func rowNames(r *review) []string { var n []string for _, row := range r.dirRows() { n = append(n, row.name) } return n } func TestDirRowsAndNavigation(t *testing.T) { root := t.TempDir() r := dirTestReview(root) if got := strings.Join(rowNames(&r), ","); got != ",a,ab,b" { t.Fatalf("root rows: %s", got) } r.di = 1 // a r.dirKey("enter") if r.dir != filepath.Join(root, "a") { t.Fatal(r.dir) } if got := strings.Join(rowNames(&r), ","); got != "..,,x,y" { t.Fatalf("a rows: %s", got) } r.dirKey("up") // .. if rows := r.dirRows(); rows[r.di].kind != rowUp { t.Fatal("cursor not on ..") } r.dirKey("enter") if r.dir != root || r.dirRows()[r.di].name != "a" { t.Errorf("up: dir=%s cursor=%s", r.dir, r.dirRows()[r.di].name) } } func TestInspectFilter(t *testing.T) { root := t.TempDir() r := dirTestReview(root) r.di = 1 // a r.dirKey("i") if r.dirView || len(r.groups) != 3 { // h1 (a/x), h2 (a/y), h3 (a/top) t.Fatalf("a: dirView=%v groups=%d", r.dirView, len(r.groups)) } if len(r.groups[0].Files)+len(r.groups[1].Files)+len(r.groups[2].Files) != 7 { t.Error("full groups should be shown, including members outside the subtree") } r.update(keyMsg("esc")) if !r.dirView || len(r.groups) != len(r.all) { t.Fatal("esc should return to the directory view") } // the /a/b vs /a/bc style boundary: inspecting "b" must not match "ab" r.dir, r.di = root, 3 // b r.dirKey("i") if len(r.groups) != 1 || r.groups[0].Hash != "h1" { t.Fatalf("b: %+v", r.groups) } r.dirView, r.filtered = true, false r.refilter() // at the root shows everything r.di = 0 r.dirKey("i") if len(r.groups) != len(r.all) || r.dirView { t.Error("root inspect should show all") } } func keyMsg(s string) tea.KeyMsg { if s == "esc" { return tea.KeyMsg{Type: tea.KeyEsc} } return tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune(s)} } func TestPatternCleanup(t *testing.T) { root := t.TempDir() p := func(s string) string { return filepath.Join(root, s) } var es []FileEntry mk := func(path, hash string) { full := p(path) os.MkdirAll(filepath.Dir(full), 0o755) os.WriteFile(full, []byte("data"), 0o644) es = append(es, FileEntry{full, filepath.Base(path), 4, hash}) } mk("keep/a.jpg", "h1") mk("old/a.jpg", "h1") mk("keep/b.jpg", "h2") mk("old/b.jpg", "h2") mk("old/sub/b.jpg", "h2") // below the deleted dir: untouched mk("keep/c.jpg", "h3") mk("old/other.jpg", "h3") // different name: untouched mk("keep/d.jpg", "h4") // no copy in old: untouched mk("elsewhere/d.jpg", "h4") mk("keep/sub/e.jpg", "h5") // below preferred dir: not a keeper mk("old/e.jpg", "h5") r := newReview(root, es) r.dirView, r.inGroup = false, true for i, g := range r.groups { if g.Hash == "h1" { r.gi = i } } r.fi = 0 // keep/a.jpg r.togglePreferred() r.fi = 1 r.toggleMark() r.startPattern() if r.mode != modeConfirmPattern { t.Fatalf("msg: %s", r.msg) } var got []string for _, it := range r.plan { got = append(got, strings.TrimPrefix(it.Delete, root+"/")+">"+strings.TrimPrefix(it.Keep, root+"/")) } if want := "old/a.jpg>keep/a.jpg,old/b.jpg>keep/b.jpg"; strings.Join(got, ",") != want { t.Fatalf("plan: %v", got) } r.applyPattern() for _, gone := range []string{"old/a.jpg", "old/b.jpg"} { if _, err := os.Stat(p(gone)); err == nil { t.Errorf("%s still present", gone) } } for _, kept := range []string{"keep/a.jpg", "keep/b.jpg", "old/sub/b.jpg", "old/other.jpg", "old/e.jpg"} { if _, err := os.Stat(p(kept)); err != nil { t.Errorf("%s missing", kept) } } b, _ := os.ReadFile(filepath.Join(root, logFileName)) if strings.Count(string(b), "DELETE") != 2 || r.preferred != "" { t.Errorf("log: %s", b) } } func TestPatternRequiresPreferredAndMark(t *testing.T) { _, r := setup(t) r.dirView, r.inGroup = false, true r.startPattern() if r.mode == modeConfirmPattern || !r.msgErr { t.Error("expected refusal without preferred") } r.togglePreferred() r.startPattern() if r.mode == modeConfirmPattern { t.Error("expected refusal without marks") } r.fi = 1 r.toggleMark() r.fi = 0 r.toggleMark() if r.marks[r.groups[0].Files[0].FilePath] { t.Error("preferred file must not be markable") } }