Adding music took two navigation items that each did half the job. The new Import page has both sources as tabs and one shared list of what was added in this session, with the reason for every failure and a link to the library. From computer: drop several MP3 or WAV files or pick them with a multi-select dialog. Files are imported one by one on a background thread with a progress bar, and a failing file does not stop the rest. Openverse: search and download show a progress ring with a status text. Results that are already in the library or have no duration are marked in their row and cannot be imported; a double-click imports. To detect duplicates, songs now store the Openverse result id. The repository adds the column to existing databases and a unique index keeps one song per Openverse result. Refs #13 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
132 lines
4.5 KiB
C#
132 lines
4.5 KiB
C#
using Microsoft.Data.Sqlite;
|
|
using NAudio.Wave;
|
|
using Radiosender.Models;
|
|
using Radiosender.Services;
|
|
|
|
namespace Radiosender.Tests;
|
|
|
|
public sealed class SongImportServiceTests : IDisposable
|
|
{
|
|
private readonly string _directory = Path.Combine(Path.GetTempPath(), "radiosender-tests", Guid.NewGuid().ToString("N"));
|
|
private readonly SongRepository _repository;
|
|
private readonly SongImportService _service;
|
|
|
|
private string SourceDirectory => Path.Combine(_directory, "source");
|
|
private string MusicDirectory => Path.Combine(_directory, "music");
|
|
|
|
public SongImportServiceTests()
|
|
{
|
|
Directory.CreateDirectory(SourceDirectory);
|
|
_repository = new SongRepository(Path.Combine(_directory, "test.db"));
|
|
_service = new SongImportService(_repository, MusicDirectory);
|
|
}
|
|
|
|
public void Dispose()
|
|
{
|
|
SqliteConnection.ClearAllPools(); // pooled connections keep the file locked
|
|
Directory.Delete(_directory, recursive: true);
|
|
}
|
|
|
|
private string CreateWav(string fileName, TimeSpan duration)
|
|
{
|
|
var path = Path.Combine(SourceDirectory, fileName);
|
|
var format = new WaveFormat(8000, 16, 1);
|
|
using var writer = new WaveFileWriter(path, format);
|
|
writer.Write(new byte[(int)(format.AverageBytesPerSecond * duration.TotalSeconds)]);
|
|
return path;
|
|
}
|
|
|
|
private string[] MusicFiles() =>
|
|
Directory.Exists(MusicDirectory) ? Directory.GetFiles(MusicDirectory) : [];
|
|
|
|
[Fact]
|
|
public void Import_Wav_CopiesFileAndSavesSong()
|
|
{
|
|
var source = CreateWav("My Song.wav", TimeSpan.FromSeconds(2));
|
|
|
|
var song = _service.Import(source);
|
|
|
|
var stored = _repository.GetById(song.Id);
|
|
Assert.NotNull(stored);
|
|
Assert.Equal("My Song", stored.Title);
|
|
Assert.Equal(TimeSpan.FromSeconds(2), stored.Duration);
|
|
Assert.Equal(SongSource.Upload, stored.Source);
|
|
Assert.Equal(Path.Combine(MusicDirectory, "My Song.wav"), stored.FilePath);
|
|
Assert.True(File.Exists(stored.FilePath));
|
|
Assert.True(File.Exists(source));
|
|
}
|
|
|
|
[Fact]
|
|
public void Import_SameFileTwice_DoesNotOverwrite()
|
|
{
|
|
var source = CreateWav("song.wav", TimeSpan.FromSeconds(1));
|
|
|
|
var first = _service.Import(source);
|
|
var second = _service.Import(source);
|
|
|
|
Assert.NotEqual(first.FilePath, second.FilePath);
|
|
Assert.Equal(Path.Combine(MusicDirectory, "song (2).wav"), second.FilePath);
|
|
Assert.Equal(2, MusicFiles().Length);
|
|
}
|
|
|
|
[Fact]
|
|
public void Import_WrongExtension_IsRejected()
|
|
{
|
|
var source = Path.Combine(SourceDirectory, "notes.txt");
|
|
File.WriteAllText(source, "not audio");
|
|
|
|
Assert.Throws<SongImportException>(() => _service.Import(source));
|
|
Assert.Empty(MusicFiles());
|
|
Assert.Empty(_repository.GetAll());
|
|
}
|
|
|
|
[Theory]
|
|
[InlineData("garbage.wav")]
|
|
[InlineData("garbage.mp3")]
|
|
public void Import_GarbageFile_IsRejectedWithoutLeftoverCopy(string fileName)
|
|
{
|
|
var source = Path.Combine(SourceDirectory, fileName);
|
|
File.WriteAllBytes(source, Enumerable.Range(0, 4096).Select(i => (byte)(i * 7)).ToArray());
|
|
|
|
Assert.Throws<SongImportException>(() => _service.Import(source));
|
|
Assert.Empty(MusicFiles());
|
|
Assert.Empty(_repository.GetAll());
|
|
}
|
|
|
|
[Fact]
|
|
public void Import_EmptyWav_IsRejectedWithoutLeftoverCopy()
|
|
{
|
|
var source = CreateWav("silence.wav", TimeSpan.Zero);
|
|
|
|
Assert.Throws<SongImportException>(() => _service.Import(source));
|
|
Assert.Empty(MusicFiles());
|
|
}
|
|
|
|
[Fact]
|
|
public void Import_MissingFile_IsRejected()
|
|
{
|
|
Assert.Throws<SongImportException>(() => _service.Import(Path.Combine(SourceDirectory, "missing.mp3")));
|
|
Assert.Empty(MusicFiles());
|
|
}
|
|
|
|
[Fact]
|
|
public void UploadController_ImportsEachFileAndReportsFailuresPerFile()
|
|
{
|
|
var controller = new Radiosender.Controllers.UploadController(_service);
|
|
string[] files =
|
|
[
|
|
CreateWav("first.wav", TimeSpan.FromSeconds(2)),
|
|
Path.Combine(SourceDirectory, "notes.txt"),
|
|
CreateWav("second.wav", TimeSpan.FromSeconds(3)),
|
|
];
|
|
File.WriteAllText(files[1], "not audio");
|
|
|
|
var results = files.Select(controller.Upload).ToList();
|
|
|
|
Assert.Equal([true, false, true], results.Select(r => r.Success));
|
|
Assert.Equal("first", results[0].Song!.Title);
|
|
Assert.Null(results[1].Song);
|
|
Assert.Contains("MP3 and WAV", results[1].Message);
|
|
Assert.Equal(2, _repository.GetAll().Count);
|
|
}}
|