Merge remote-tracking branch 'origin/main' into j.lechner/song-upload

# Conflicts:
#	src/Radiosender/Radiosender.csproj
#	src/Radiosender/Views/MainWindow.xaml
This commit is contained in:
fraujulian committed 2026-11-23 10:37:26 +01:00
commit 360077ff64
13 files changed
+728

No files matched your search

@@ -0,0 +1,107 @@
using Microsoft.Data.Sqlite;
using Radiosender.Controllers;
using Radiosender.Models;
using Radiosender.Services;
namespace Radiosender.Tests;
public sealed class LibraryControllerTests : IDisposable
{
private readonly string _directory = Path.Combine(Path.GetTempPath(), "radiosender-tests", Guid.NewGuid().ToString("N"));
private readonly SongRepository _repository;
private readonly LibraryController _controller;
public LibraryControllerTests()
{
_repository = new SongRepository(Path.Combine(_directory, "test.db"));
_controller = new LibraryController(_repository);
}
public void Dispose()
{
SqliteConnection.ClearAllPools(); // pooled connections keep the file locked
Directory.Delete(_directory, recursive: true);
}
private Song AddSong(string title, string artist)
{
var song = new Song
{
Title = title,
Artist = artist,
Duration = TimeSpan.FromSeconds(200),
FilePath = @"C:\music\song.mp3",
Source = SongSource.Upload,
License = "CC BY 4.0",
};
_repository.Add(song);
return song;
}
[Theory]
[InlineData(null)]
[InlineData("")]
[InlineData(" ")]
public void Search_WithoutQuery_ReturnsAllSongs(string? query)
{
AddSong("Alpha", "Band");
AddSong("Beta", "Other");
Assert.Equal(2, _controller.Search(query).Count);
}
[Fact]
public void Search_MatchesTitleOrArtistCaseInsensitive()
{
AddSong("Morning Light", "Band");
AddSong("Evening", "The Lights");
AddSong("Unrelated", "Nobody");
var titles = _controller.Search(" LIGHT ").Select(s => s.Title).ToList();
Assert.Equal(["Evening", "Morning Light"], titles.Order());
}
[Fact]
public void Update_WritesEditableFieldsAndKeepsTheRest()
{
var song = AddSong("Old", "Old Artist");
Assert.True(_controller.Update(song.Id, " New ", "New Artist", " ", "by someone"));
var loaded = _repository.GetById(song.Id)!;
Assert.Equal("New", loaded.Title);
Assert.Equal("New Artist", loaded.Artist);
Assert.Null(loaded.License);
Assert.Equal("by someone", loaded.Attribution);
Assert.Equal(song.FilePath, loaded.FilePath);
Assert.Equal(song.Duration, loaded.Duration);
Assert.Equal(song.Source, loaded.Source);
}
[Fact]
public void Update_RejectsBlankTitleOrArtist()
{
var song = AddSong("Title", "Artist");
Assert.ThrowsAny<ArgumentException>(() => _controller.Update(song.Id, " ", "Artist", null, null));
Assert.ThrowsAny<ArgumentException>(() => _controller.Update(song.Id, "Title", "", null, null));
Assert.Equal("Title", _repository.GetById(song.Id)!.Title);
}
[Fact]
public void Update_ReturnsFalseForUnknownSong()
{
Assert.False(_controller.Update(42, "Title", "Artist", null, null));
}
[Fact]
public void Delete_RemovesSong()
{
var song = AddSong("Title", "Artist");
Assert.True(_controller.Delete(song.Id));
Assert.Empty(_controller.Search(null));
Assert.False(_controller.Delete(song.Id));
}
}
+134
View File
@@ -0,0 +1,134 @@
using System.IO;
using NAudio.Wave;
using Radiosender.Services;
namespace Radiosender.Tests;
/// <summary>Skips the test on machines without an audio output device (for example CI agents).</summary>
public sealed class AudioDeviceFactAttribute : FactAttribute
{
public AudioDeviceFactAttribute()
{
if (WaveOut.DeviceCount == 0) Skip = "No audio output device available.";
}
}
public sealed class MusicPlayerTests : IDisposable
{
private readonly string _directory = Path.Combine(Path.GetTempPath(), "radiosender-tests", Guid.NewGuid().ToString("N"));
public MusicPlayerTests() => Directory.CreateDirectory(_directory);
public void Dispose()
{
MusicPlayer.Instance.Stop();
Directory.Delete(_directory, recursive: true);
}
// Writes a short, quiet sine tone as 16-bit mono PCM.
private string CreateWav(TimeSpan length)
{
var path = Path.Combine(_directory, "tone.wav");
var format = new WaveFormat(44100, 16, 1);
using var writer = new WaveFileWriter(path, format);
var samples = (int)(format.SampleRate * length.TotalSeconds);
for (var i = 0; i < samples; i++)
writer.WriteSample(0.05f * MathF.Sin(2 * MathF.PI * 440 * i / format.SampleRate));
return path;
}
[Fact]
public void Instance_ReturnsSameObjectOnRepeatedAccess()
{
Assert.Same(MusicPlayer.Instance, MusicPlayer.Instance);
}
[Fact]
public void Instance_ReturnsSameObjectAcrossThreads()
{
var instances = new MusicPlayer[16];
Parallel.For(0, instances.Length, i => instances[i] = MusicPlayer.Instance);
Assert.All(instances, instance => Assert.Same(MusicPlayer.Instance, instance));
}
[Fact]
public void Play_MissingFile_ThrowsAndStaysStopped()
{
var path = Path.Combine(_directory, "missing.mp3");
var exception = Assert.Throws<FileNotFoundException>(() => MusicPlayer.Instance.Play(path));
Assert.Equal(path, exception.FileName);
Assert.False(MusicPlayer.Instance.IsPlaying);
Assert.Equal(TimeSpan.Zero, MusicPlayer.Instance.Duration);
}
[Fact]
public void Play_UnreadableFile_ThrowsInvalidDataAndStaysStopped()
{
var path = Path.Combine(_directory, "broken.wav");
File.WriteAllText(path, "not audio");
Assert.Throws<InvalidDataException>(() => MusicPlayer.Instance.Play(path));
Assert.False(MusicPlayer.Instance.IsPlaying);
}
[Fact]
public void Volume_OutOfRange_Throws()
{
Assert.Throws<ArgumentOutOfRangeException>(() => MusicPlayer.Instance.Volume = 1.5f);
Assert.Throws<ArgumentOutOfRangeException>(() => MusicPlayer.Instance.Volume = -0.1f);
}
[AudioDeviceFact]
public void Stop_DoesNotRaiseTrackFinished()
{
var player = MusicPlayer.Instance;
var finished = 0;
EventHandler handler = (_, _) => Interlocked.Increment(ref finished);
player.TrackFinished += handler;
try
{
player.Play(CreateWav(TimeSpan.FromSeconds(0.5)));
Assert.True(player.IsPlaying);
player.Stop();
Thread.Sleep(1000); // longer than the track, so a natural end would have fired by now
Assert.Equal(0, finished);
Assert.False(player.IsPlaying);
}
finally
{
player.TrackFinished -= handler;
}
}
[AudioDeviceFact]
public void Play_ShortWav_RaisesTrackFinishedAndPositionChanged()
{
var player = MusicPlayer.Instance;
using var finished = new ManualResetEventSlim();
var positionChanged = false;
EventHandler finishedHandler = (_, _) => finished.Set();
EventHandler<TimeSpan> positionHandler = (_, _) => positionChanged = true;
player.TrackFinished += finishedHandler;
player.PositionChanged += positionHandler;
try
{
player.Play(CreateWav(TimeSpan.FromSeconds(0.6)));
Assert.InRange(player.Duration.TotalSeconds, 0.59, 0.61);
Assert.True(finished.Wait(TimeSpan.FromSeconds(5)), "TrackFinished was not raised.");
Assert.True(positionChanged);
Assert.False(player.IsPlaying);
Assert.Equal(TimeSpan.Zero, player.Duration);
}
finally
{
player.TrackFinished -= finishedHandler;
player.PositionChanged -= positionHandler;
}
}
}
@@ -0,0 +1,28 @@
using Radiosender.Services;
namespace Radiosender.Tests;
public class PlaceholderContentTests
{
[Fact]
public void AdSpots_AreNonEmptyWithTitlesAndPositiveDurations()
{
Assert.NotEmpty(PlaceholderContent.AdSpots);
Assert.All(PlaceholderContent.AdSpots, ad =>
{
Assert.False(string.IsNullOrWhiteSpace(ad.Title));
Assert.True(ad.Duration > TimeSpan.Zero);
});
}
[Fact]
public void ModeratorSegments_AreNonEmptyWithTitlesAndPositiveDurations()
{
Assert.NotEmpty(PlaceholderContent.ModeratorSegments);
Assert.All(PlaceholderContent.ModeratorSegments, segment =>
{
Assert.False(string.IsNullOrWhiteSpace(segment.Title));
Assert.True(segment.Duration > TimeSpan.Zero);
});
}
}