diff --git a/src/main_test.go b/src/main_test.go new file mode 100644 index 0000000..f613369 --- /dev/null +++ b/src/main_test.go @@ -0,0 +1,125 @@ +package main + +import ( + "encoding/json" + "os" + "reflect" + "testing" +) + +func TestSanitizeFilename(t *testing.T) { + tests := []struct { + input string + expected string + }{ + {"file:name.mp4", "file_name.mp4"}, + {"file/name.mp4", "file_name.mp4"}, + {"file\\name.mp4", "file_name.mp4"}, + {"file?name.mp4", "file_name.mp4"}, + {"file*name.mp4", "file_name.mp4"}, + {"file.mp4", "file_name_.mp4"}, + {".hidden", "hidden"}, + } + + for _, test := range tests { + result := sanitizeFilename(test.input) + if result != test.expected { + t.Errorf("sanitizeFilename(%q) = %q, want %q", test.input, result, test.expected) + } + } +} + +func TestIsValidURL(t *testing.T) { + tests := []struct { + input string + expected bool + }{ + {"https://example.com", true}, + {"http://example.com", true}, + {"ftp://example.com", true}, + {"not a url", false}, + } + + for _, test := range tests { + result := isValidURL(test.input) + if result != test.expected { + t.Errorf("isValidURL(%q) = %v, want %v", test.input, result, test.expected) + } + } +} + +func TestParseMetadata(t *testing.T) { + tests := []struct { + input string + expected Metadata + }{ + {"Show Title; serie; 01", Metadata{Title: "Show Title", Type: "serie", Season: "S01"}}, + {"Movie Title; movie; ", Metadata{Title: "Movie Title", Type: "movie", Season: "S"}}, + {"Invalid Metadata", Metadata{}}, + } + + for _, test := range tests { + result := parseMetadata(test.input) + if !reflect.DeepEqual(result, test.expected) { + t.Errorf("parseMetadata(%q) = %v, want %v", test.input, result, test.expected) + } + } +} + +func TestParseInputFile(t *testing.T) { + tempFile, err := os.CreateTemp("", "test_input_*.json") + if err != nil { + t.Fatalf("Failed to create temp file: %v", err) + } + defer os.Remove(tempFile.Name()) + + testData := Items{ + Items: []Item{ + {MPD: "http://example.com/video1.mpd", Filename: "video1.mp4"}, + {MPD: "http://example.com/video2.mpd", Filename: "video2.mp4"}, + }, + } + jsonData, _ := json.Marshal(testData) + if _, err := tempFile.Write(jsonData); err != nil { + t.Fatalf("Failed to write to temp file: %v", err) + } + tempFile.Close() + + items, err := parseInputFile(tempFile.Name()) + if err != nil { + t.Fatalf("parseInputFile() returned an error: %v", err) + } + + if len(items) != len(testData.Items) { + t.Errorf("parseInputFile() returned %d items, want %d", len(items), len(testData.Items)) + } + + for i, item := range items { + if !reflect.DeepEqual(item, testData.Items[i]) { + t.Errorf("parseInputFile() item %d = %v, want %v", i, item, testData.Items[i]) + } + } +} + +func TestGroupItemsBySeason(t *testing.T) { + items := []Item{ + {Filename: "show1_s01e01.mp4", Metadata: "Show 1; serie; 01"}, + {Filename: "show1_s01e02.mp4", Metadata: "Show 1; serie; 01"}, + {Filename: "show2_s01e01.mp4", Metadata: "Show 2; serie; 01"}, + {Filename: "movie1.mp4", Metadata: "Movie 1; movie; "}, + } + + grouped := groupItemsBySeason(items) + + expectedGroups := map[string]int{ + "Show 1 - S01": 2, + "Show 2 - S01": 1, + "Movies": 1, + } + + for group, count := range expectedGroups { + if len(grouped[group]) != count { + t.Errorf("groupItemsBySeason() group %q has %d items, want %d", group, len(grouped[group]), count) + } + } +}