package service import ( "os" "path/filepath" "testing" ) func TestFindByVideoID(t *testing.T) { lib, dir := newLibrary(t) writeItem(t, dir, "alpha", "name = \"Alpha\"\nvideo_id = \"aaa\"\n", nil) writeItem(t, dir, "beta", "name = \"Beta\"\nvideo_id = \"bbb\"\n", nil) got, ok := lib.FindByVideoID(dir, "aaa") if !ok || filepath.Base(got) != "alpha" { t.Fatalf("expected to find alpha, got %q ok=%v", got, ok) } if _, ok := lib.FindByVideoID(dir, "zzz"); ok { t.Error("expected no match for unknown id") } // An empty id never matches. if _, ok := lib.FindByVideoID(dir, ""); ok { t.Error("expected no match for empty id") } } func TestPruneToIDSet(t *testing.T) { lib, dir := newLibrary(t) writeItem(t, dir, "keep", "name = \"Keep\"\nvideo_id = \"k1\"\n", nil) writeItem(t, dir, "drop", "name = \"Drop\"\nvideo_id = \"d1\"\n", nil) // An item without identity must never be pruned (we can't positively id it). writeItem(t, dir, "noid", "name = \"NoId\"\n", nil) keep := map[string]bool{"k1": true} removed, err := lib.PruneToIDSet(dir, keep) if err != nil { t.Fatalf("prune: %v", err) } if removed != 1 { t.Errorf("expected 1 removed, got %d", removed) } if _, err := os.Stat(filepath.Join(dir, "drop")); !os.IsNotExist(err) { t.Error("expected 'drop' to be removed") } if _, err := os.Stat(filepath.Join(dir, "keep")); err != nil { t.Error("expected 'keep' to survive") } if _, err := os.Stat(filepath.Join(dir, "noid")); err != nil { t.Error("expected unidentified item to survive") } } // A symlinked library root used to break cache eviction: the cache is keyed off // the resolved root, so computing the key from the raw configured path missed, // and a pruned item stayed visible until the scan TTL expired. func TestPruneEvictsCacheUnderSymlinkedRoot(t *testing.T) { parent := t.TempDir() real := filepath.Join(parent, "real") link := filepath.Join(parent, "link") if err := os.MkdirAll(real, 0o755); err != nil { t.Fatalf("mkdir real: %v", err) } if err := os.Symlink(real, link); err != nil { t.Skipf("symlinks unavailable: %v", err) } lib := NewLibraryService(link, "ffmpeg", "ffprobe") writeItem(t, real, "drop", "name = \"Drop\"\nvideo_id = \"d1\"\n", nil) // Populate the scan cache, the way a listing or detail page would. if _, err := lib.GetByRelPath("drop"); err != nil { t.Fatalf("seed cache: %v", err) } // Prune against the resolved root, which is what resolveBaseLibraryDir hands // the production caller. removed, err := lib.PruneToIDSet(lib.libraryRoot(), map[string]bool{"other": true}) if err != nil { t.Fatalf("prune: %v", err) } if removed != 1 { t.Fatalf("expected 1 removed, got %d", removed) } if _, err := lib.GetByRelPath("drop"); err == nil { t.Error("pruned item still served from the scan cache") } }