1
0
Fork 0
mirror of https://github.com/fiso64/slsk-batchdl.git synced 2024-12-22 22:42:41 +00:00
slsk-batchdl/slsk-batchdl/Test.cs

491 lines
20 KiB
C#
Raw Normal View History

2024-08-26 21:57:31 +00:00
using Data;
using Enums;
2024-08-30 08:57:18 +00:00
using FileSkippers;
2024-08-26 21:57:31 +00:00
using System.Diagnostics;
using System.Reflection;
using static Test.Helpers;
namespace Test
{
static class Test
{
public static async Task RunAllTests()
{
TestStringUtils();
TestAutoProfiles();
TestProfileConditions();
await TestStringExtractor();
TestM3uEditor();
Console.WriteLine('\n' + new string('#', 50) + '\n' + "All tests passed.");
}
public static void TestStringUtils()
{
SetCurrentTest("TestStringUtils");
// RemoveFt
Assert("blah blah ft. blah blah" .RemoveFt() == "blah blah");
Assert("blah blah feat. blah blah" .RemoveFt() == "blah blah");
Assert("blah (feat. blah blah) blah".RemoveFt() == "blah blah");
Assert("blah (ft. blah blah) blah" .RemoveFt() == "blah blah");
// RemoveConsecutiveWs
Assert(" blah blah blah blah ".RemoveConsecutiveWs() == " blah blah blah blah ");
// RemoveSquareBrackets
Assert("foo [aaa] bar".RemoveSquareBrackets() == "foo bar");
// ReplaceInvalidChars
Assert("Invalid chars: \\/:|?<>*\"".ReplaceInvalidChars("", true) == "Invalid chars ");
Assert("Invalid chars: \\/:|?<>*\"".ReplaceInvalidChars("", true, false) == "Invalid chars \\/");
// ContainsWithBoundary
Assert("foo blah bar".ContainsWithBoundary("blah"));
Assert("foo/blah/bar".ContainsWithBoundary("blah"));
Assert("foo - blah 2".ContainsWithBoundary("blah"));
Assert(!"foo blah bar".ContainsWithBoundaryIgnoreWs("blah"));
Assert(!"foo - blah 2".ContainsWithBoundaryIgnoreWs("blah"));
Assert("foo - blah 2 - bar".ContainsWithBoundaryIgnoreWs("blah 2"));
Assert("foo/blah/bar".ContainsWithBoundaryIgnoreWs("blah"));
Assert("01 blah".ContainsWithBoundaryIgnoreWs("blah", acceptLeftDigit: true));
Assert(!"foo - blah 2blah".ContainsWithBoundaryIgnoreWs("blah", acceptLeftDigit: true));
Assert("foo - blah 2 blah".ContainsWithBoundaryIgnoreWs("blah", acceptLeftDigit: true));
// GreatestCommonPath
var paths = new string[]
{
"/home/user/docs/nested/file",
"/home/user/docs/nested/folder/",
"/home/user/docs/letter.txt",
"/home/user/docs/report.pdf",
"/home/user/docs/",
};
2024-08-30 08:57:18 +00:00
Assert(Utils.GreatestCommonPath(paths) == "/home/user/docs/");
Assert(Utils.GreatestCommonPath(new string[] { "/path/file", "" }) == "");
Assert(Utils.GreatestCommonPath(new string[] { "/path/file", "/" }) == "/");
Assert(Utils.GreatestCommonPath(new string[] { "/path/dir1", "/path/dir2" }) == "/path/");
Assert(Utils.GreatestCommonPath(new string[] { "/path\\dir1/blah", "/path/dir2\\blah" }) == "/path\\");
Assert(Utils.GreatestCommonPath(new string[] { "dir1", "dir2" }) == "");
2024-08-26 21:57:31 +00:00
// RemoveDiacritics
Assert(" Café Crème à la mode Ü".RemoveDiacritics() == " Cafe Creme a la mode U");
Passed();
}
public static void TestAutoProfiles()
{
SetCurrentTest("TestAutoProfiles");
2024-09-02 07:28:38 +00:00
ResetConfig();
Config.I.inputType = InputType.YouTube;
Config.I.interactiveMode = true;
Config.I.aggregate = false;
Config.I.maxStaleTime = 50000;
2024-08-26 21:57:31 +00:00
string path = Path.Join(Directory.GetCurrentDirectory(), "test_conf.conf");
string content =
"max-stale-time = 5" +
"\nfast-search = true" +
"\nformat = flac" +
"\n[profile-true-1]" +
"\nprofile-cond = input-type == \"youtube\" && download-mode == \"album\"" +
"\nmax-stale-time = 10" +
"\n[profile-true-2]" +
"\nprofile-cond = !aggregate" +
"\nfast-search = false" +
"\n[profile-false-1]" +
"\nprofile-cond = input-type == \"string\"" +
"\nformat = mp3" +
"\n[profile-no-cond]" +
"\nformat = opus";
File.WriteAllText(path, content);
2024-09-07 09:06:29 +00:00
Config.I.LoadAndParse(new string[] { "-c", path });
2024-08-26 21:57:31 +00:00
2024-09-02 07:28:38 +00:00
var tle = new TrackListEntry(TrackType.Album);
Config.UpdateProfiles(tle);
2024-08-26 21:57:31 +00:00
2024-09-02 07:28:38 +00:00
Assert(Config.I.maxStaleTime == 10 && !Config.I.fastSearch && Config.I.necessaryCond.Formats[0] == "flac");
2024-08-26 21:57:31 +00:00
2024-09-02 07:28:38 +00:00
ResetConfig();
Config.I.inputType = InputType.CSV;
Config.I.album = true;
Config.I.interactiveMode = true;
Config.I.useYtdlp = false;
Config.I.maxStaleTime = 50000;
2024-08-26 21:57:31 +00:00
content =
"\n[no-stale]" +
"\nprofile-cond = interactive && download-mode == \"album\"" +
"\nmax-stale-time = 999999" +
"\n[youtube]" +
"\nprofile-cond = input-type == \"youtube\"" +
"\nyt-dlp = true";
File.WriteAllText(path, content);
2024-09-02 07:28:38 +00:00
2024-09-07 09:06:29 +00:00
Config.I.LoadAndParse(new string[] { "-c", path });
2024-09-02 07:28:38 +00:00
Config.UpdateProfiles(tle);
Assert(Config.I.maxStaleTime == 999999 && !Config.I.useYtdlp);
ResetConfig();
Config.I.inputType = InputType.YouTube;
Config.I.album = false;
Config.I.interactiveMode = true;
Config.I.useYtdlp = false;
Config.I.maxStaleTime = 50000;
2024-08-26 21:57:31 +00:00
content =
"\n[no-stale]" +
"\nprofile-cond = interactive && download-mode == \"album\"" +
"\nmax-stale-time = 999999" +
"\n[youtube]" +
"\nprofile-cond = input-type == \"youtube\"" +
"\nyt-dlp = true";
File.WriteAllText(path, content);
2024-09-07 09:06:29 +00:00
Config.I.LoadAndParse(new string[] { "-c", path });
2024-09-02 07:28:38 +00:00
Config.UpdateProfiles(new TrackListEntry(TrackType.Normal));
2024-08-26 21:57:31 +00:00
2024-09-02 07:28:38 +00:00
Assert(Config.I.maxStaleTime == 50000 && Config.I.useYtdlp);
2024-08-26 21:57:31 +00:00
if (File.Exists(path))
File.Delete(path);
Passed();
}
public static void TestProfileConditions()
{
SetCurrentTest("TestProfileConditions");
2024-09-02 07:28:38 +00:00
Config.I.inputType = InputType.YouTube;
Config.I.interactiveMode = true;
Config.I.album = true;
Config.I.aggregate = false;
2024-08-26 21:57:31 +00:00
var conds = new (bool, string)[]
{
2024-09-02 07:28:38 +00:00
(true, "input-type == \"youtube\""),
(true, "download-mode == \"album\""),
2024-08-26 21:57:31 +00:00
(false, "aggregate"),
(true, "interactive"),
(true, "album"),
(false, "!interactive"),
(true, "album && input-type == \"youtube\""),
(false, "album && input-type != \"youtube\""),
(false, "(interactive && aggregate)"),
(true, "album && (interactive || aggregate)"),
(true, "input-type == \"spotify\" || aggregate || input-type == \"csv\" || interactive && album"),
(true, " input-type!=\"youtube\"||(album&&!interactive ||(aggregate || interactive ) )"),
(false, " input-type!=\"youtube\"||(album&&!interactive ||(aggregate || !interactive ) )"),
};
foreach ((var b, var c) in conds)
{
Console.WriteLine(c);
2024-09-02 07:28:38 +00:00
Assert(b == Config.I.ProfileConditionSatisfied(c));
2024-08-26 21:57:31 +00:00
}
Passed();
}
public static async Task TestStringExtractor()
{
SetCurrentTest("TestStringExtractor");
var strings = new List<string>()
{
"Some Title",
"Some, Title",
"artist = Some artist, title = some title",
"Artist - Title, length = 42",
"title=Some, Title, artist=Some, Artist, album = Some, Album, length= 42",
"Some, Artist = a - Some, Title = b, album = Some, Album, length = 42",
"Foo Bar",
"Foo - Bar",
"Artist - Title, length=42",
"title=Title, artist=Artist, length=42",
};
var tracks = new List<Track>()
{
new Track() { Title="Some Title" },
new Track() { Title="Some, Title" },
new Track() { Title = "some title", Artist = "Some artist" },
new Track() { Title = "Title", Artist = "Artist", Length = 42 },
new Track() { Title="Some, Title", Artist = "Some, Artist", Album = "Some, Album", Length = 42 },
new Track() { Title="Some, Title = b", Artist = "Some, Artist = a", Album = "Some, Album", Length = 42 },
new Track() { Title = "Foo Bar" },
new Track() { Title = "Bar", Artist = "Foo" },
new Track() { Title = "Title", Artist = "Artist", Length = 42 },
new Track() { Title = "Title", Artist = "Artist", Length = 42 },
};
var albums = new List<Track>()
{
new Track() { Album="Some Title", Type = TrackType.Album },
new Track() { Album="Some, Title", Type = TrackType.Album },
new Track() { Title = "some title", Artist = "Some artist", Type = TrackType.Album },
new Track() { Album = "Title", Artist = "Artist", Length = 42, Type = TrackType.Album },
new Track() { Title="Some, Title", Artist = "Some, Artist", Album = "Some, Album", Length = 42, Type = TrackType.Album },
new Track() { Artist = "Some, Artist = a", Album = "Some, Album", Length = 42, Type = TrackType.Album },
new Track() { Album = "Foo Bar", Type = TrackType.Album },
new Track() { Album = "Bar", Artist = "Foo", Type = TrackType.Album },
new Track() { Album = "Title", Artist = "Artist", Length = 42, Type = TrackType.Album },
new Track() { Title = "Title", Artist = "Artist", Length = 42, Type = TrackType.Album },
2024-08-26 21:57:31 +00:00
};
var extractor = new Extractors.StringExtractor();
2024-09-02 07:28:38 +00:00
Config.I.aggregate = false;
Config.I.album = false;
2024-08-26 21:57:31 +00:00
Console.WriteLine("Testing songs: ");
for (int i = 0; i < strings.Count; i++)
{
2024-09-02 07:28:38 +00:00
Config.I.input = strings[i];
Console.WriteLine(Config.I.input);
var res = await extractor.GetTracks(Config.I.input, 0, 0, false);
2024-08-26 21:57:31 +00:00
var t = res[0].list[0][0];
2024-09-02 07:28:38 +00:00
Assert(Extractors.StringExtractor.InputMatches(Config.I.input));
2024-08-26 21:57:31 +00:00
Assert(t.ToKey() == tracks[i].ToKey());
}
Console.WriteLine();
Console.WriteLine("Testing albums");
2024-09-02 07:28:38 +00:00
Config.I.album = true;
2024-08-26 21:57:31 +00:00
for (int i = 0; i < strings.Count; i++)
{
2024-09-02 07:28:38 +00:00
Config.I.input = strings[i];
Console.WriteLine(Config.I.input);
var t = (await extractor.GetTracks(Config.I.input, 0, 0, false))[0].source;
Assert(Extractors.StringExtractor.InputMatches(Config.I.input));
2024-08-26 21:57:31 +00:00
Assert(t.ToKey() == albums[i].ToKey());
}
Passed();
}
public static void TestM3uEditor()
{
SetCurrentTest("TestM3uEditor");
2024-09-02 07:28:38 +00:00
Config.I.m3uOption = M3uOption.All;
Config.I.skipMode = SkipMode.M3u;
Config.I.musicDir = "";
Config.I.printOption = PrintOption.Tracks | PrintOption.Full;
Config.I.skipExisting = true;
2024-08-26 21:57:31 +00:00
string path = Path.Join(Directory.GetCurrentDirectory(), "test_m3u.m3u8");
if (File.Exists(path))
File.Delete(path);
File.WriteAllText(path, $"#SLDL:" +
2024-08-27 16:00:31 +00:00
$"{Path.Join(Directory.GetCurrentDirectory(), "file1.5")},\"Artist, 1.5\",,\"Title, , 1.5\",-1,0,3,0;" +
$"path/to/file1,\"Artist, 1\",,\"Title, , 1\",-1,0,3,0;" +
$"path/to/file2,\"Artist, 2\",,Title2,-1,0,3,0;" +
$",\"Artist; ,3\",,Title3 ;a,-1,0,4,0;" +
$",\"Artist,,, ;4\",,Title4,-1,0,4,3;" +
$",,,,-1,0,0,0;");
2024-08-26 21:57:31 +00:00
var notFoundInitial = new List<Track>()
{
new() { Artist = "Artist; ,3", Title = "Title3 ;a" },
new() { Artist = "Artist,,, ;4", Title = "Title4", State = TrackState.Failed, FailureReason = FailureReason.NoSuitableFileFound }
};
var existingInitial = new List<Track>()
{
new() { Artist = "Artist, 1", Title = "Title, , 1", DownloadPath = "path/to/file1", State = TrackState.Downloaded },
new() { Artist = "Artist, 1.5", Title = "Title, , 1.5", DownloadPath = Path.Join(Directory.GetCurrentDirectory(), "file1.5"), State = TrackState.Downloaded },
new() { Artist = "Artist, 2", Title = "Title2", DownloadPath = "path/to/file2", State = TrackState.AlreadyExists }
};
var toBeDownloadedInitial = new List<Track>()
{
new() { Artist = "ArtistA", Album = "Albumm", Title = "TitleA" },
new() { Artist = "ArtistB", Album = "Albumm", Title = "TitleB" }
};
var trackLists = new TrackLists();
2024-09-01 17:52:50 +00:00
trackLists.AddEntry(new TrackListEntry(TrackType.Normal));
2024-08-26 21:57:31 +00:00
foreach (var t in notFoundInitial)
trackLists.AddTrackToLast(t);
foreach (var t in existingInitial)
trackLists.AddTrackToLast(t);
foreach (var t in toBeDownloadedInitial)
trackLists.AddTrackToLast(t);
2024-09-02 07:28:38 +00:00
Program.m3uEditor = new M3uEditor(path, trackLists, Config.I.m3uOption);
2024-08-26 21:57:31 +00:00
2024-08-30 08:57:18 +00:00
Program.outputDirSkipper = new M3uSkipper(Program.m3uEditor, false);
2024-08-27 16:00:31 +00:00
2024-08-26 21:57:31 +00:00
var notFound = (List<Track>)ProgramInvoke("DoSkipNotFound", new object[] { trackLists[0].list[0] });
2024-08-27 16:00:31 +00:00
var existing = (List<Track>)ProgramInvoke("DoSkipExisting", new object[] { trackLists[0].list[0] });
2024-08-26 21:57:31 +00:00
var toBeDownloaded = trackLists[0].list[0].Where(t => t.State == TrackState.Initial).ToList();
Assert(notFound.SequenceEqualUpToPermutation(notFoundInitial));
Assert(existing.SequenceEqualUpToPermutation(existingInitial));
Assert(toBeDownloaded.SequenceEqualUpToPermutation(toBeDownloadedInitial));
2024-09-02 07:28:38 +00:00
Printing.PrintTracksTbd(toBeDownloaded, existing, notFound, TrackType.Normal);
2024-08-26 21:57:31 +00:00
Program.m3uEditor.Update();
string output = File.ReadAllText(path);
string need =
2024-08-27 16:00:31 +00:00
"#SLDL:./file1.5,\"Artist, 1.5\",,\"Title, , 1.5\",-1,0,3,0;path/to/file1,\"Artist, 1\",,\"Title, , 1\",-1,0,3,0;path/to/file2,\"Artist, 2\",,Title2,-1,0,3,0;,\"Artist; ,3\",,Title3 ;a,-1,0,4,0;,\"Artist,,, ;4\",,Title4,-1,0,4,3;,,,,-1,0,0,0;" +
2024-08-26 21:57:31 +00:00
"\n" +
2024-09-02 07:28:38 +00:00
"\n#FAIL: Artist; ,3 - Title3 ;a [NoSuitableFileFound]" +
"\n#FAIL: Artist,,, ;4 - Title4 [NoSuitableFileFound]" +
2024-08-26 21:57:31 +00:00
"\npath/to/file1" +
"\nfile1.5" +
"\npath/to/file2" +
"\n";
Assert(output == need);
toBeDownloaded[0].State = TrackState.Downloaded;
toBeDownloaded[0].DownloadPath = "new/file/path";
toBeDownloaded[1].State = TrackState.Failed;
toBeDownloaded[1].FailureReason = FailureReason.NoSuitableFileFound;
existing[1].DownloadPath = "/other/new/file/path";
Program.m3uEditor.Update();
output = File.ReadAllText(path);
need =
2024-08-27 16:00:31 +00:00
"#SLDL:/other/new/file/path,\"Artist, 1.5\",,\"Title, , 1.5\",-1,0,3,0;path/to/file1,\"Artist, 1\",,\"Title, , 1\",-1,0,3,0;path/to/file2,\"Artist, 2\",,Title2,-1,0,3,0;,\"Artist; ,3\",,Title3 ;a,-1,0,4,0;,\"Artist,,, ;4\",,Title4,-1,0,4,3;" +
",,,,-1,0,0,0;new/file/path,ArtistA,Albumm,TitleA,-1,0,1,0;,ArtistB,Albumm,TitleB,-1,0,2,3;" +
2024-08-26 21:57:31 +00:00
"\n" +
2024-09-02 07:28:38 +00:00
"\n#FAIL: Artist; ,3 - Title3 ;a [NoSuitableFileFound]" +
"\n#FAIL: Artist,,, ;4 - Title4 [NoSuitableFileFound]" +
2024-08-26 21:57:31 +00:00
"\npath/to/file1" +
"\n/other/new/file/path" +
"\npath/to/file2" +
"\nnew/file/path" +
2024-09-02 07:28:38 +00:00
"\n#FAIL: ArtistB - TitleB [NoSuitableFileFound]" +
2024-08-26 21:57:31 +00:00
"\n";
Assert(output == need);
Console.WriteLine();
Console.WriteLine(output);
2024-09-02 07:28:38 +00:00
Program.m3uEditor = new M3uEditor(path, trackLists, Config.I.m3uOption);
2024-08-26 21:57:31 +00:00
foreach (var t in trackLists.Flattened(false, false))
{
Program.m3uEditor.TryGetPreviousRunResult(t, out var prev);
Assert(prev != null);
Assert(prev.ToKey() == t.ToKey());
Assert(prev.DownloadPath == t.DownloadPath);
Assert(prev.State == t.State || prev.State == TrackState.NotFoundLastTime);
Assert(prev.FailureReason == t.FailureReason);
}
Program.m3uEditor.Update();
output = File.ReadAllText(path);
Assert(output == need);
2024-08-27 16:00:31 +00:00
var test = new List<Track>
{
new() { Artist = "ArtistA", Album = "AlbumA", Type = TrackType.Album },
new() { Artist = "ArtistB", Album = "AlbumB", Type = TrackType.Album },
new() { Artist = "ArtistC", Album = "AlbumC", Type = TrackType.Album },
};
trackLists = new TrackLists();
foreach (var t in test)
trackLists.AddEntry(new TrackListEntry(t));
File.WriteAllText(path, "");
2024-09-02 07:28:38 +00:00
Config.I.m3uOption = M3uOption.Index;
Program.m3uEditor = new M3uEditor(path, trackLists, Config.I.m3uOption);
2024-08-27 16:00:31 +00:00
Program.m3uEditor.Update();
Assert(File.ReadAllText(path) == "");
test[0].State = TrackState.Downloaded;
test[0].DownloadPath = "download/path";
test[1].State = TrackState.Failed;
test[1].FailureReason = FailureReason.NoSuitableFileFound;
test[2].State = TrackState.AlreadyExists;
Program.m3uEditor.Update();
2024-09-02 07:28:38 +00:00
Program.m3uEditor = new M3uEditor(path, trackLists, Config.I.m3uOption);
2024-08-27 16:00:31 +00:00
foreach (var t in test)
{
Program.m3uEditor.TryGetPreviousRunResult(t, out var tt);
Assert(tt != null);
Assert(tt.ToKey() == t.ToKey());
t.DownloadPath = "this should not change tt.DownloadPath";
Assert(t.DownloadPath != tt.DownloadPath);
}
2024-08-26 21:57:31 +00:00
File.Delete(path);
Passed();
}
}
static class Helpers
{
static string? currentTest = null;
public static void SetCurrentTest(string name)
{
currentTest = name;
}
public static object? ProgramInvoke(string name, object[] parameters)
{
var method = typeof(Program).GetMethod(name, BindingFlags.NonPublic | BindingFlags.Static);
return method.Invoke(null, parameters);
}
public class AssertionError : Exception
{
public AssertionError() : base("Assertion failed.") { }
public AssertionError(string message) : base(message) { }
}
public static void Assert(bool condition, string message = "Assertion failed")
{
if (!condition)
{
var stackTrace = new StackTrace(true);
var frame = stackTrace.GetFrame(1);
var fileName = frame.GetFileName();
var lineNumber = frame.GetFileLineNumber();
throw new AssertionError($"{currentTest}: {message} (at {fileName}:{lineNumber})");
}
}
2024-09-02 07:28:38 +00:00
public static void ResetConfig()
2024-08-26 21:57:31 +00:00
{
2024-09-02 07:28:38 +00:00
var singletonType = typeof(Config);
var instanceField = singletonType.GetField("Instance", BindingFlags.Static | BindingFlags.NonPublic);
var constructor = singletonType.GetConstructor(BindingFlags.Instance | BindingFlags.NonPublic, null, Type.EmptyTypes, null);
var newInstance = constructor.Invoke(null);
instanceField.SetValue(null, newInstance);
2024-08-26 21:57:31 +00:00
}
public static void Passed()
{
Console.WriteLine($"{currentTest} passed");
currentTest = null;
}
}
}