|
| 1 | +// Zaparoo Core |
| 2 | +// Copyright (c) 2026 The Zaparoo Project Contributors. |
| 3 | +// SPDX-License-Identifier: GPL-3.0-or-later |
| 4 | +// |
| 5 | +// This file is part of Zaparoo Core. |
| 6 | +// |
| 7 | +// Zaparoo Core is free software: you can redistribute it and/or modify |
| 8 | +// it under the terms of the GNU General Public License as published by |
| 9 | +// the Free Software Foundation, either version 3 of the License, or |
| 10 | +// (at your option) any later version. |
| 11 | +// |
| 12 | +// Zaparoo Core is distributed in the hope that it will be useful, |
| 13 | +// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | +// GNU General Public License for more details. |
| 16 | +// |
| 17 | +// You should have received a copy of the GNU General Public License |
| 18 | +// along with Zaparoo Core. If not, see <http://www.gnu.org/licenses/>. |
| 19 | + |
| 20 | +package mediascanner |
| 21 | + |
| 22 | +import ( |
| 23 | + "context" |
| 24 | + "os" |
| 25 | + "path/filepath" |
| 26 | + "runtime" |
| 27 | + "testing" |
| 28 | + |
| 29 | + "github.com/stretchr/testify/assert" |
| 30 | + "github.com/stretchr/testify/require" |
| 31 | +) |
| 32 | + |
| 33 | +func TestDirCache_CachesResults(t *testing.T) { |
| 34 | + dir := t.TempDir() |
| 35 | + require.NoError(t, os.WriteFile(filepath.Join(dir, "file.txt"), []byte("x"), 0o600)) |
| 36 | + |
| 37 | + cache := newDirCache() |
| 38 | + ctx := context.Background() |
| 39 | + |
| 40 | + entries1, err := cache.list(ctx, dir) |
| 41 | + require.NoError(t, err) |
| 42 | + require.Len(t, entries1, 1) |
| 43 | + |
| 44 | + // Add another file — cached result should still show 1 entry |
| 45 | + require.NoError(t, os.WriteFile(filepath.Join(dir, "file2.txt"), []byte("y"), 0o600)) |
| 46 | + |
| 47 | + entries2, err := cache.list(ctx, dir) |
| 48 | + require.NoError(t, err) |
| 49 | + assert.Len(t, entries2, 1, "should return cached result, not re-read directory") |
| 50 | +} |
| 51 | + |
| 52 | +func TestDirCache_FindEntry_ExactMatch(t *testing.T) { |
| 53 | + dir := t.TempDir() |
| 54 | + require.NoError(t, os.Mkdir(filepath.Join(dir, "NES"), 0o750)) |
| 55 | + |
| 56 | + cache := newDirCache() |
| 57 | + ctx := context.Background() |
| 58 | + |
| 59 | + name, err := cache.findEntry(ctx, dir, "NES") |
| 60 | + require.NoError(t, err) |
| 61 | + assert.Equal(t, "NES", name) |
| 62 | +} |
| 63 | + |
| 64 | +func TestDirCache_FindEntry_CaseInsensitive(t *testing.T) { |
| 65 | + dir := t.TempDir() |
| 66 | + require.NoError(t, os.Mkdir(filepath.Join(dir, "SNES"), 0o750)) |
| 67 | + |
| 68 | + cache := newDirCache() |
| 69 | + ctx := context.Background() |
| 70 | + |
| 71 | + name, err := cache.findEntry(ctx, dir, "snes") |
| 72 | + require.NoError(t, err) |
| 73 | + assert.Equal(t, "SNES", name) |
| 74 | +} |
| 75 | + |
| 76 | +func TestDirCache_FindEntry_PrefersExactOnLinux(t *testing.T) { |
| 77 | + if runtime.GOOS != "linux" { |
| 78 | + t.Skip("exact-match preference only applies on Linux") |
| 79 | + } |
| 80 | + |
| 81 | + dir := t.TempDir() |
| 82 | + // Create both cases — Linux allows this on case-sensitive filesystems |
| 83 | + require.NoError(t, os.Mkdir(filepath.Join(dir, "Games"), 0o750)) |
| 84 | + require.NoError(t, os.Mkdir(filepath.Join(dir, "games"), 0o750)) |
| 85 | + |
| 86 | + cache := newDirCache() |
| 87 | + ctx := context.Background() |
| 88 | + |
| 89 | + name, err := cache.findEntry(ctx, dir, "Games") |
| 90 | + require.NoError(t, err) |
| 91 | + assert.Equal(t, "Games", name) |
| 92 | + |
| 93 | + name, err = cache.findEntry(ctx, dir, "games") |
| 94 | + require.NoError(t, err) |
| 95 | + assert.Equal(t, "games", name) |
| 96 | +} |
| 97 | + |
| 98 | +func TestDirCache_FindEntry_NotFound(t *testing.T) { |
| 99 | + dir := t.TempDir() |
| 100 | + require.NoError(t, os.Mkdir(filepath.Join(dir, "NES"), 0o750)) |
| 101 | + |
| 102 | + cache := newDirCache() |
| 103 | + ctx := context.Background() |
| 104 | + |
| 105 | + name, err := cache.findEntry(ctx, dir, "GENESIS") |
| 106 | + require.NoError(t, err) |
| 107 | + assert.Empty(t, name) |
| 108 | +} |
| 109 | + |
| 110 | +func TestDirCache_ContextCancellation(t *testing.T) { |
| 111 | + ctx, cancel := context.WithCancel(context.Background()) |
| 112 | + cancel() |
| 113 | + |
| 114 | + cache := newDirCache() |
| 115 | + |
| 116 | + _, err := cache.list(ctx, t.TempDir()) |
| 117 | + require.Error(t, err) |
| 118 | + require.ErrorIs(t, err, context.Canceled) |
| 119 | + |
| 120 | + _, err = cache.findEntry(ctx, t.TempDir(), "anything") |
| 121 | + require.Error(t, err) |
| 122 | + require.ErrorIs(t, err, context.Canceled) |
| 123 | +} |
0 commit comments