using System.IO; using System.Linq; using SongTidy.Services; using SongTidy.Tests.TestUtilities; namespace SongTidy.Tests.Services; /// /// Contains tests for . /// [TestClass] public sealed class AudioFileScannerTests { private readonly TempDirectory _tempDirectory = new(); /// /// Removes the temporary directory created for the current test. /// [TestCleanup] public void Cleanup() { _tempDirectory.Dispose(); } /// /// Verifies that supported extensions are matched case-insensitively and others are ignored. /// [TestMethod] public void ScansSupportedExtensionsCaseInsensitively() { CreateFile("a.mp3"); CreateFile("b.FLAC"); CreateFile("c.txt"); IReadOnlyList files = AudioFileScanner.Scan(_tempDirectory.FullPath); Assert.HasCount(2, files); Assert.IsTrue(files.Any(file => file.EndsWith("a.mp3", StringComparison.OrdinalIgnoreCase))); Assert.IsTrue(files.Any(file => file.EndsWith("b.FLAC", StringComparison.OrdinalIgnoreCase))); } /// /// Verifies that subdirectories are not scanned. /// [TestMethod] public void DoesNotScanSubdirectories() { CreateFile("root.mp3"); CreateFile(Path.Combine("sub", "nested.wav")); IReadOnlyList files = AudioFileScanner.Scan(_tempDirectory.FullPath); Assert.HasCount(1, files); Assert.IsTrue(files[0].EndsWith("root.mp3", StringComparison.OrdinalIgnoreCase)); } /// /// Creates an empty file inside the test directory. /// /// The relative file name, which may include a subdirectory. private void CreateFile(string fileName) { string path = Path.Combine(_tempDirectory.FullPath, fileName); string directory = Path.GetDirectoryName(path) ?? _tempDirectory.FullPath; Directory.CreateDirectory(directory); File.WriteAllText(path, string.Empty); } }