subscription_library_test.go
⎇
Raw
1package service
2
3import (
4 "os"
5 "path/filepath"
6 "testing"
7)
8
9func TestFindByVideoID(t *testing.T) {
10 lib, dir := newLibrary(t)
11
12 writeItem(t, dir, "alpha", "name = \"Alpha\"\nvideo_id = \"aaa\"\n", nil)
13 writeItem(t, dir, "beta", "name = \"Beta\"\nvideo_id = \"bbb\"\n", nil)
14
15 got, ok := lib.FindByVideoID(dir, "aaa")
16 if !ok || filepath.Base(got) != "alpha" {
17 t.Fatalf("expected to find alpha, got %q ok=%v", got, ok)
18 }
19
20 if _, ok := lib.FindByVideoID(dir, "zzz"); ok {
21 t.Error("expected no match for unknown id")
22 }
23 // An empty id never matches.
24 if _, ok := lib.FindByVideoID(dir, ""); ok {
25 t.Error("expected no match for empty id")
26 }
27}
28
29func TestPruneToIDSet(t *testing.T) {
30 lib, dir := newLibrary(t)
31
32 writeItem(t, dir, "keep", "name = \"Keep\"\nvideo_id = \"k1\"\n", nil)
33 writeItem(t, dir, "drop", "name = \"Drop\"\nvideo_id = \"d1\"\n", nil)
34 // An item without identity must never be pruned (we can't positively id it).
35 writeItem(t, dir, "noid", "name = \"NoId\"\n", nil)
36
37 keep := map[string]bool{"k1": true}
38 removed, err := lib.PruneToIDSet(dir, keep)
39 if err != nil {
40 t.Fatalf("prune: %v", err)
41 }
42 if removed != 1 {
43 t.Errorf("expected 1 removed, got %d", removed)
44 }
45 if _, err := os.Stat(filepath.Join(dir, "drop")); !os.IsNotExist(err) {
46 t.Error("expected 'drop' to be removed")
47 }
48 if _, err := os.Stat(filepath.Join(dir, "keep")); err != nil {
49 t.Error("expected 'keep' to survive")
50 }
51 if _, err := os.Stat(filepath.Join(dir, "noid")); err != nil {
52 t.Error("expected unidentified item to survive")
53 }
54}
55
56// A symlinked library root used to break cache eviction: the cache is keyed off
57// the resolved root, so computing the key from the raw configured path missed,
58// and a pruned item stayed visible until the scan TTL expired.
59func TestPruneEvictsCacheUnderSymlinkedRoot(t *testing.T) {
60 parent := t.TempDir()
61 real := filepath.Join(parent, "real")
62 link := filepath.Join(parent, "link")
63 if err := os.MkdirAll(real, 0o755); err != nil {
64 t.Fatalf("mkdir real: %v", err)
65 }
66 if err := os.Symlink(real, link); err != nil {
67 t.Skipf("symlinks unavailable: %v", err)
68 }
69
70 lib := NewLibraryService(link, "ffmpeg", "ffprobe")
71 writeItem(t, real, "drop", "name = \"Drop\"\nvideo_id = \"d1\"\n", nil)
72
73 // Populate the scan cache, the way a listing or detail page would.
74 if _, err := lib.GetByRelPath("drop"); err != nil {
75 t.Fatalf("seed cache: %v", err)
76 }
77
78 // Prune against the resolved root, which is what resolveBaseLibraryDir hands
79 // the production caller.
80 removed, err := lib.PruneToIDSet(lib.libraryRoot(), map[string]bool{"other": true})
81 if err != nil {
82 t.Fatalf("prune: %v", err)
83 }
84 if removed != 1 {
85 t.Fatalf("expected 1 removed, got %d", removed)
86 }
87
88 if _, err := lib.GetByRelPath("drop"); err == nil {
89 t.Error("pruned item still served from the scan cache")
90 }
91}
92