|
1 | 1 | package processor |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "errors" |
| 5 | + "fmt" |
4 | 6 | "os" |
5 | 7 | "path/filepath" |
| 8 | + "strings" |
6 | 9 | "testing" |
7 | 10 |
|
| 11 | + "github.com/cloudbox/autoscan" |
8 | 12 | "github.com/cloudbox/autoscan/stats" |
9 | 13 | ) |
10 | 14 |
|
@@ -42,6 +46,104 @@ func newTestProcessor(anchors []string) *Processor { |
42 | 46 | } |
43 | 47 | } |
44 | 48 |
|
| 49 | +// mockTarget is a minimal autoscan.Target for testing callTargets. |
| 50 | +type mockTarget struct { |
| 51 | + scanFn func(autoscan.Scan) error |
| 52 | +} |
| 53 | + |
| 54 | +func (m *mockTarget) Scan(scan autoscan.Scan) error { |
| 55 | + return m.scanFn(scan) |
| 56 | +} |
| 57 | + |
| 58 | +func (*mockTarget) Available() error { |
| 59 | + return nil |
| 60 | +} |
| 61 | + |
| 62 | +func TestCallTargets(t *testing.T) { |
| 63 | + p := &Processor{} |
| 64 | + scan := autoscan.Scan{Folder: "/media/movies"} |
| 65 | + |
| 66 | + t.Run("AllMatch", func(t *testing.T) { |
| 67 | + targets := []autoscan.Target{ |
| 68 | + &mockTarget{scanFn: func(_ autoscan.Scan) error { return nil }}, |
| 69 | + &mockTarget{scanFn: func(_ autoscan.Scan) error { return nil }}, |
| 70 | + &mockTarget{scanFn: func(_ autoscan.Scan) error { return nil }}, |
| 71 | + } |
| 72 | + if err := p.callTargets(targets, scan); err != nil { |
| 73 | + t.Errorf("expected nil error, got: %v", err) |
| 74 | + } |
| 75 | + }) |
| 76 | + |
| 77 | + t.Run("AllSkipped", func(t *testing.T) { |
| 78 | + targets := []autoscan.Target{ |
| 79 | + &mockTarget{scanFn: func(_ autoscan.Scan) error { |
| 80 | + return fmt.Errorf("%w: /tv", autoscan.ErrLibraryNotMatched) |
| 81 | + }}, |
| 82 | + &mockTarget{scanFn: func(_ autoscan.Scan) error { |
| 83 | + return fmt.Errorf("%w: /tv", autoscan.ErrLibraryNotMatched) |
| 84 | + }}, |
| 85 | + &mockTarget{scanFn: func(_ autoscan.Scan) error { |
| 86 | + return fmt.Errorf("%w: /tv", autoscan.ErrLibraryNotMatched) |
| 87 | + }}, |
| 88 | + } |
| 89 | + // All skipped — scan is consumed, not retried. No error returned. |
| 90 | + if err := p.callTargets(targets, scan); err != nil { |
| 91 | + t.Errorf("expected nil error when all targets skipped, got: %v", err) |
| 92 | + } |
| 93 | + }) |
| 94 | + |
| 95 | + t.Run("MixMatchAndSkip", func(t *testing.T) { |
| 96 | + targets := []autoscan.Target{ |
| 97 | + &mockTarget{scanFn: func(_ autoscan.Scan) error { return nil }}, |
| 98 | + &mockTarget{scanFn: func(_ autoscan.Scan) error { |
| 99 | + return fmt.Errorf("%w: /tv", autoscan.ErrLibraryNotMatched) |
| 100 | + }}, |
| 101 | + &mockTarget{scanFn: func(_ autoscan.Scan) error { return nil }}, |
| 102 | + } |
| 103 | + if err := p.callTargets(targets, scan); err != nil { |
| 104 | + t.Errorf("expected nil error for mixed match/skip, got: %v", err) |
| 105 | + } |
| 106 | + }) |
| 107 | + |
| 108 | + t.Run("RealError", func(t *testing.T) { |
| 109 | + targets := []autoscan.Target{ |
| 110 | + &mockTarget{scanFn: func(_ autoscan.Scan) error { return nil }}, |
| 111 | + &mockTarget{scanFn: func(_ autoscan.Scan) error { |
| 112 | + return errors.New("connection refused") |
| 113 | + }}, |
| 114 | + } |
| 115 | + err := p.callTargets(targets, scan) |
| 116 | + if err == nil { |
| 117 | + t.Fatal("expected non-nil error, got nil") |
| 118 | + } |
| 119 | + if !strings.Contains(err.Error(), "connection refused") { |
| 120 | + t.Errorf("expected error to contain 'connection refused', got: %v", err) |
| 121 | + } |
| 122 | + }) |
| 123 | + |
| 124 | + t.Run("RealErrorPlusSkip", func(t *testing.T) { |
| 125 | + targets := []autoscan.Target{ |
| 126 | + &mockTarget{scanFn: func(_ autoscan.Scan) error { |
| 127 | + return fmt.Errorf("%w: /movies", autoscan.ErrLibraryNotMatched) |
| 128 | + }}, |
| 129 | + &mockTarget{scanFn: func(_ autoscan.Scan) error { return nil }}, |
| 130 | + &mockTarget{scanFn: func(_ autoscan.Scan) error { |
| 131 | + return errors.New("timeout") |
| 132 | + }}, |
| 133 | + } |
| 134 | + err := p.callTargets(targets, scan) |
| 135 | + if err == nil { |
| 136 | + t.Fatal("expected non-nil error, got nil") |
| 137 | + } |
| 138 | + if !strings.Contains(err.Error(), "timeout") { |
| 139 | + t.Errorf("expected error to contain 'timeout', got: %v", err) |
| 140 | + } |
| 141 | + if errors.Is(err, autoscan.ErrLibraryNotMatched) { |
| 142 | + t.Error("returned error must not wrap ErrLibraryNotMatched") |
| 143 | + } |
| 144 | + }) |
| 145 | +} |
| 146 | + |
45 | 147 | func TestCheckAnchorsNoAnchors(t *testing.T) { |
46 | 148 | p := newTestProcessor(nil) |
47 | 149 |
|
@@ -135,25 +237,3 @@ func TestCheckAnchorsStateTransitions(t *testing.T) { |
135 | 237 | t.Fatal("expected anchorState to be true after restore") |
136 | 238 | } |
137 | 239 | } |
138 | | - |
139 | | -func TestCheckAnchorsDirectorySupport(t *testing.T) { |
140 | | - dir := t.TempDir() |
141 | | - anchorDir := filepath.Join(dir, "mount-check") |
142 | | - if err := os.Mkdir(anchorDir, 0o755); err != nil { |
143 | | - t.Fatal(err) |
144 | | - } |
145 | | - |
146 | | - p := newTestProcessor([]string{anchorDir}) |
147 | | - |
148 | | - if !p.CheckAnchors() { |
149 | | - t.Error("expected CheckAnchors to return true for directory anchor") |
150 | | - } |
151 | | - |
152 | | - // Remove directory |
153 | | - if err := os.Remove(anchorDir); err != nil { |
154 | | - t.Fatal(err) |
155 | | - } |
156 | | - if p.CheckAnchors() { |
157 | | - t.Error("expected CheckAnchors to return false after directory removed") |
158 | | - } |
159 | | -} |
|
0 commit comments