Files
arrange/revive_test.go
T
smandClaude Opus 5.5 f63b855179 choose what to arrange: -kinds, -exclude, am survey
-kinds picks any of image, raw, video (default all). -exclude skips
files and whole directories by name glob, and can be repeated. am
survey stats media per directory (counts by kind, videos over 1G,
sizes) so things like ripped DVDs stand out before arranging. It
never opens files.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-26 15:57:23 -07:00

334 lines
8.9 KiB
Go

package arrange
import (
"bytes"
"encoding/binary"
"os"
"path/filepath"
"strings"
"testing"
"time"
)
// atom builds a QuickTime atom with a 32-bit size.
func atom(typ string, body ...[]byte) []byte {
b := bytes.Join(body, nil)
out := make([]byte, 8, 8+len(b))
binary.BigEndian.PutUint32(out, uint32(8+len(b)))
copy(out[4:], typ)
return append(out, b...)
}
// mvhd builds a movie header body of the given version with creation time t.
func mvhd(version byte, t time.Time) []byte {
secs := uint64(t.Sub(epoch1904) / time.Second)
if version == 0 {
b := make([]byte, 100)
binary.BigEndian.PutUint32(b[4:], uint32(secs))
return b
}
b := make([]byte, 112)
b[0] = 1
binary.BigEndian.PutUint64(b[4:], secs)
return b
}
func TestQuickTimeCreated(t *testing.T) {
when := time.Date(2019, 7, 4, 20, 15, 0, 0, time.UTC)
// 64-bit size atom: size field 1, then the real size
big := make([]byte, 16+4)
binary.BigEndian.PutUint32(big, 1)
copy(big[4:], "free")
binary.BigEndian.PutUint64(big[8:], uint64(len(big)))
tests := []struct {
name string
file []byte
want time.Time
ok bool
}{
{"v0", bytes.Join([][]byte{atom("ftyp", []byte("qt ")), atom("moov", atom("mvhd", mvhd(0, when)))}, nil), when, true},
{"v1", bytes.Join([][]byte{atom("ftyp", []byte("isom")), atom("mdat", make([]byte, 64)), atom("moov", atom("trak"), atom("mvhd", mvhd(1, when)))}, nil), when, true},
{"largesize", bytes.Join([][]byte{big, atom("moov", atom("mvhd", mvhd(0, when)))}, nil), when, true},
{"zero time", atom("moov", atom("mvhd", mvhd(0, epoch1904))), time.Time{}, false},
{"no moov", atom("ftyp", []byte("isom")), time.Time{}, false},
{"garbage", []byte("a\n"), time.Time{}, false},
{"lying size", append(atom("moov", atom("mvhd", mvhd(0, when)))[:4], []byte("moov")...), time.Time{}, false},
}
for _, test := range tests {
got, err := quickTimeCreated(bytes.NewReader(test.file))
if (err == nil) != test.ok {
t.Errorf("%s: err = %v, want ok=%v", test.name, err, test.ok)
continue
}
if test.ok && !got.Equal(test.want) {
t.Errorf("%s: got %v, want %v", test.name, got, test.want)
}
}
}
func TestParseQuickTimeDate(t *testing.T) {
when := time.Date(2019, 7, 4, 20, 15, 0, 0, time.UTC)
p := filepath.Join(t.TempDir(), "clip.MP4")
if err := os.WriteFile(p, atom("moov", atom("mvhd", mvhd(0, when))), 0644); err != nil {
t.Fatal(err)
}
m, err := ParseFile(p)
if err != nil {
t.Fatal(err)
}
if !m.Time.Equal(when) {
t.Errorf("got %v, want %v from mvhd", m.Time, when)
}
if m.Extension != ".mp4" {
t.Errorf("extension %q, want .mp4", m.Extension)
}
}
func TestParseHEIC(t *testing.T) {
m, err := ParseFile(filepath.Join("testdata", "exif.heic"))
if err != nil {
t.Fatal(err)
}
want := time.Date(2006, 3, 27, 17, 19, 27, 0, time.Local)
if !m.Time.Equal(want) {
t.Errorf("got %v, want %v", m.Time, want)
}
if len(m.Hash) != 64 {
t.Errorf("hash %q isn't sha256", m.Hash)
}
}
func TestSourceSkips(t *testing.T) {
root := t.TempDir()
for _, p := range []string{
"keep.jpg",
"sub/KEEP.HEIC",
"._resource-fork.jpg",
".@__thumb/thumb.jpg",
"notes.txt",
} {
full := filepath.Join(root, p)
if err := os.MkdirAll(filepath.Dir(full), 0755); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(full, nil, 0644); err != nil {
t.Fatal(err)
}
}
unreadable := filepath.Join(root, "locked")
if err := os.Mkdir(unreadable, 0); err != nil {
t.Fatal(err)
}
defer os.Chmod(unreadable, 0755)
var got []string
for p := range Source(root) {
rel, _ := filepath.Rel(root, p)
got = append(got, rel)
}
if want := "keep.jpg sub/KEEP.HEIC"; strings.Join(got, " ") != want {
t.Errorf("got %q, want %q", got, want)
}
}
func TestSourceFiltered(t *testing.T) {
root := t.TempDir()
for _, p := range []string{
"pics/a.jpg",
"pics/b.NEF",
"pics/clip.mov",
"rips/Movie/VIDEO_TS/VTS_01_1.mp4",
"rips/film.mkv",
"rips/cover.jpg",
} {
full := filepath.Join(root, p)
if err := os.MkdirAll(filepath.Dir(full), 0755); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(full, nil, 0644); err != nil {
t.Fatal(err)
}
}
tests := []struct {
name string
f Filter
want string
}{
{"everything", Filter{}, "pics/a.jpg pics/b.NEF pics/clip.mov rips/Movie/VIDEO_TS/VTS_01_1.mp4 rips/cover.jpg rips/film.mkv"},
{"images", Filter{Classes: map[Class]bool{Image: true}}, "pics/a.jpg rips/cover.jpg"},
{"no video", Filter{Classes: map[Class]bool{Image: true, Raw: true}}, "pics/a.jpg pics/b.NEF rips/cover.jpg"},
{"exclude dir", Filter{Exclude: []string{"VIDEO_TS"}}, "pics/a.jpg pics/b.NEF pics/clip.mov rips/cover.jpg rips/film.mkv"},
{"exclude glob", Filter{Exclude: []string{"*.mkv", "rip*"}}, "pics/a.jpg pics/b.NEF pics/clip.mov"},
{"root not excluded", Filter{Exclude: []string{filepath.Base(root)}}, "pics/a.jpg pics/b.NEF pics/clip.mov rips/Movie/VIDEO_TS/VTS_01_1.mp4 rips/cover.jpg rips/film.mkv"},
}
for _, test := range tests {
var got []string
for p := range SourceFiltered(root, test.f) {
rel, _ := filepath.Rel(root, p)
got = append(got, rel)
}
if g := strings.Join(got, " "); g != test.want {
t.Errorf("%s:\n got %s\nwant %s", test.name, g, test.want)
}
}
}
// arranged returns a store with one file arranged into it, and that Media.
func arranged(t *testing.T) (string, Media) {
t.Helper()
root := t.TempDir()
if err := PrepOutput(root); err != nil {
t.Fatal(err)
}
m, err := ParseFile(filepath.Join("testdata", "lenna.png"))
if err != nil {
t.Fatal(err)
}
if err := m.Move(root); err != nil {
t.Fatal(err)
}
return root, m
}
func dated(t *testing.T, root string) []string {
t.Helper()
var out []string
for p := range Source(filepath.Join(root, "date")) {
out = append(out, p)
}
return out
}
// The old clean deleted date files whose content was missing from the store,
// then failed to relink them, losing the file.
func TestCleanKeepsFilesMissingFromStore(t *testing.T) {
root, m := arranged(t)
d := dated(t, root)
if len(d) != 1 {
t.Fatalf("expected one dated file, got %v", d)
}
if err := os.Remove(m.Content(root)); err != nil {
t.Fatal(err)
}
pm, err := ParseFile(d[0])
if err != nil {
t.Fatal(err)
}
in := make(chan Media, 1)
in <- pm
close(in)
out, errs := MissingLink(in, root)
var nerr int
go func() {
for range errs {
nerr++
}
}()
for m := range out {
t.Errorf("MissingLink sent %q on for relinking with no content", m.Path)
}
if err := Relink(pm, root); err == nil {
t.Error("Relink should refuse when content is missing")
}
if _, err := os.Stat(d[0]); err != nil {
t.Errorf("dated file is gone: %v", err)
}
}
func TestRelinkReplacesCopy(t *testing.T) {
root, m := arranged(t)
d := dated(t, root)[0]
// break the hardlink: replace the dated file with an identical copy
b, err := os.ReadFile(d)
if err != nil {
t.Fatal(err)
}
if err := os.Remove(d); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(d, b, 0644); err != nil {
t.Fatal(err)
}
pm, err := ParseFile(d)
if err != nil {
t.Fatal(err)
}
if err := Relink(pm, root); err != nil {
t.Fatal(err)
}
a, _ := os.Stat(d)
c, _ := os.Stat(m.Content(root))
if !os.SameFile(a, c) {
t.Error("dated file isn't a hardlink to content after Relink")
}
if left, _ := filepath.Glob(filepath.Join(filepath.Dir(d), ".arrange-*")); len(left) > 0 {
t.Errorf("temp files left behind: %v", left)
}
}
func TestMoveVerifiesHash(t *testing.T) {
root := t.TempDir()
if err := PrepOutput(root); err != nil {
t.Fatal(err)
}
m, err := ParseFile(filepath.Join("testdata", "lenna.png"))
if err != nil {
t.Fatal(err)
}
// as if the file changed between hashing and copying
m.Hash = strings.Repeat("ab", 32)
if err := m.Move(root); err == nil {
t.Fatal("Move should fail when the copy doesn't match the hash")
}
left, _ := filepath.Glob(filepath.Join(root, "content", "ab", "*"))
hidden, _ := filepath.Glob(filepath.Join(root, "content", "ab", ".*"))
if len(left)+len(hidden) > 0 {
t.Errorf("store not clean after failed move: %v %v", left, hidden)
}
if err := m.Move(root); err == nil {
t.Fatal("a second try should fail too, not be called a dup")
}
}
func TestVerify(t *testing.T) {
root, m := arranged(t)
problems := func() map[string]int {
out := map[string]int{}
ps, checked := Verify(root, 2)
for p := range ps {
out[p.Kind]++
}
out["checked"] = checked()
return out
}
if got := problems(); got["checked"] != 1 || len(got) != 1 {
t.Fatalf("healthy store: %v", got)
}
// orphan: content nothing links to
for _, d := range dated(t, root) {
os.Remove(d)
}
// stale: a temp file from a crashed run
os.WriteFile(filepath.Join(root, "content", "00", ".tmp-123"), []byte("x"), 0644)
if got := problems(); got["orphan"] != 1 || got["stale"] != 1 {
t.Errorf("want one orphan and one stale, got %v", got)
}
// corrupt: bytes no longer match the name
if err := os.WriteFile(m.Content(root), []byte("bitrot"), 0644); err != nil {
t.Fatal(err)
}
if got := problems(); got["corrupt"] != 1 {
t.Errorf("want one corrupt, got %v", got)
}
}