Adds the local music artwork plugin, its fixtures, and its unit and integration test suites

This commit is contained in:
BinaryAlley committed 2026-10-04 21:16:42 +03:00
1 parent fb05561619
commit 62b0e9297a
25 files changed
+2720

No files matched your search

@@ -0,0 +1,42 @@
#region ========================================================================= USING =====================================================================================
using Bogus;
using Lumina.Plugins.LocalMusicArtwork.Common.Models.DTO.Settings;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
#endregion
namespace Lumina.Plugins.LocalMusicArtwork.Fixtures.Common.Models.DTO.Settings;
/// <summary>
/// Fixture class for the <see cref="LocalMusicArtworkSettingsDto"/> class.
/// </summary>
[ExcludeFromCodeCoverage]
internal sealed class LocalMusicArtworkSettingsDtoFixture
{
private readonly Faker _faker = new();
/// <summary>
/// Creates a new <see cref="LocalMusicArtworkSettingsDto"/> instance with randomized test data.
/// </summary>
/// <param name="shouldExtractEmbeddedCover">Optional. Whether the cover embedded in the audio files is extracted when the album has no cover image on disk.</param>
/// <returns>A configured <see cref="LocalMusicArtworkSettingsDto"/> instance.</returns>
public LocalMusicArtworkSettingsDto Create(
bool? shouldExtractEmbeddedCover = null)
{
return new LocalMusicArtworkSettingsDto
{
ShouldExtractEmbeddedCover = shouldExtractEmbeddedCover ?? _faker.Random.Bool()
};
}
/// <summary>
/// Creates multiple <see cref="LocalMusicArtworkSettingsDto"/> instances with randomized test data.
/// </summary>
/// <param name="count">Number of instances to create.</param>
/// <returns>List of configured <see cref="LocalMusicArtworkSettingsDto"/> instances.</returns>
public List<LocalMusicArtworkSettingsDto> CreateMany(int count = 3)
{
return [.. Enumerable.Range(0, count).Select(_ => Create())];
}
}
@@ -0,0 +1,124 @@
#region ========================================================================= USING =====================================================================================
using System;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Text;
#endregion
namespace Lumina.Plugins.LocalMusicArtwork.Fixtures.Core;
/// <summary>
/// Builds minimal, structurally valid WAV files used as test input for the artwork scanning and embedded cover extraction tests.
/// The files are produced into a temporary directory at test runtime, because a WAV is a binary container whose embedded pictures can only be written by the tagging library itself.
/// </summary>
[ExcludeFromCodeCoverage]
public static class TestAudioFileFactory
{
/// <summary>
/// Creates a minimal, untagged PCM WAV file.
/// </summary>
/// <param name="path">The path where the WAV is written.</param>
/// <param name="durationMs">The duration of the audio of the file, in milliseconds.</param>
public static void CreateWav(string path, int durationMs = 200)
{
WriteFile(path, BuildWav(durationMs));
}
/// <summary>
/// Creates a PCM WAV file that carries the provided image as its embedded front cover.
/// </summary>
/// <param name="path">The path where the WAV is written.</param>
/// <param name="imageBytes">The bytes of the embedded cover image.</param>
/// <param name="mimeType">The MIME type of the embedded cover image, defaulting to JPEG.</param>
/// <param name="durationMs">The duration of the audio of the file, in milliseconds.</param>
public static void CreateWavWithEmbeddedCover(string path, byte[] imageBytes, string? mimeType = "image/jpeg", int durationMs = 200)
{
CreateTaggedWav(path, tagFile =>
{
tagFile.Tag.Pictures =
[
new TagLib.Picture
{
MimeType = mimeType!,
Type = TagLib.PictureType.FrontCover,
Data = [.. imageBytes]
}
];
}, durationMs);
}
/// <summary>
/// Creates a PCM WAV file whose embedded tags are configured by the provided callback.
/// </summary>
/// <param name="path">The path where the WAV is written.</param>
/// <param name="configureTags">The callback that configures the tags of the file.</param>
/// <param name="durationMs">The duration of the audio of the file, in milliseconds.</param>
public static void CreateTaggedWav(string path, Action<TagLib.File> configureTags, int durationMs = 200)
{
WriteFile(path, BuildWav(durationMs));
using (TagLib.File tagFile = TagLib.File.Create(path))
{
configureTags(tagFile);
tagFile.Save();
}
}
/// <summary>
/// Creates a file whose content is not a recognizable audio container, so that the resilience of the embedded cover extraction can be exercised.
/// </summary>
/// <param name="path">The path where the file is written.</param>
public static void CreateUnsupportedFile(string path)
{
WriteFile(path, Encoding.UTF8.GetBytes("This is not an audio file."));
}
/// <summary>
/// Writes the provided bytes at <paramref name="path"/>, creating the containing directory when it does not exist yet.
/// </summary>
/// <param name="path">The path where the bytes are written.</param>
/// <param name="contents">The bytes of the file.</param>
private static void WriteFile(string path, byte[] contents)
{
string? directory = Path.GetDirectoryName(path);
if (!string.IsNullOrEmpty(directory))
Directory.CreateDirectory(directory);
File.WriteAllBytes(path, contents);
}
/// <summary>
/// Builds the bytes of a minimal, valid PCM WAV file with a single silent audio channel.
/// </summary>
/// <param name="durationMs">The duration of the audio of the file, in milliseconds.</param>
/// <returns>The bytes of the WAV file.</returns>
private static byte[] BuildWav(int durationMs)
{
const int SAMPLE_RATE = 44100;
const short CHANNELS = 1;
const short BITS_PER_SAMPLE = 16;
int bytesPerSample = BITS_PER_SAMPLE / 8;
int dataSize = SAMPLE_RATE * CHANNELS * bytesPerSample * durationMs / 1000;
using (MemoryStream memoryStream = new())
{
using (BinaryWriter writer = new(memoryStream))
{
writer.Write(Encoding.ASCII.GetBytes("RIFF"));
writer.Write(36 + dataSize);
writer.Write(Encoding.ASCII.GetBytes("WAVE"));
writer.Write(Encoding.ASCII.GetBytes("fmt "));
writer.Write(16);
writer.Write((short)1);
writer.Write(CHANNELS);
writer.Write(SAMPLE_RATE);
writer.Write(SAMPLE_RATE * CHANNELS * bytesPerSample);
writer.Write((short)(CHANNELS * bytesPerSample));
writer.Write(BITS_PER_SAMPLE);
writer.Write(Encoding.ASCII.GetBytes("data"));
writer.Write(dataSize);
writer.Write(new byte[dataSize]);
}
return memoryStream.ToArray();
}
}
}
@@ -0,0 +1,91 @@
#region ========================================================================= USING =====================================================================================
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Text;
#endregion
namespace Lumina.Plugins.LocalMusicArtwork.Fixtures.Core;
/// <summary>
/// Creates image files of the formats a local music library stores next to its tracks, so that artwork scanning can be exercised against real files.
/// Each file carries the magic bytes of its format, mirroring the images a music library keeps in the folders of its artists and albums.
/// </summary>
[ExcludeFromCodeCoverage]
public static class TestImageFileFactory
{
/// <summary>
/// Creates a JPEG image file at the provided path.
/// </summary>
/// <param name="path">The path where the image is written.</param>
public static void CreateJpeg(string path)
{
WriteFile(path, [0xFF, 0xD8, 0xFF, 0xE0, 0x00, 0x10, 0x4A, 0x46, 0x49, 0x46, 0x00, 0x01, 0x01, 0x00, 0x00, 0x01, 0x00, 0x01, 0x00, 0x00, 0xFF, 0xD9]);
}
/// <summary>
/// Creates a PNG image file at the provided path.
/// </summary>
/// <param name="path">The path where the image is written.</param>
public static void CreatePng(string path)
{
WriteFile(path, [0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A, 0x00, 0x00, 0x00, 0x0D, 0x49, 0x48, 0x44, 0x52]);
}
/// <summary>
/// Creates a WebP image file at the provided path.
/// </summary>
/// <param name="path">The path where the image is written.</param>
public static void CreateWebp(string path)
{
WriteFile(path, [.. Encoding.ASCII.GetBytes("RIFF"), 0x00, 0x00, 0x00, 0x00, .. Encoding.ASCII.GetBytes("WEBP")]);
}
/// <summary>
/// Creates a BMP image file at the provided path.
/// </summary>
/// <param name="path">The path where the image is written.</param>
public static void CreateBmp(string path)
{
WriteFile(path, [0x42, 0x4D, 0x00, 0x00, 0x00, 0x00]);
}
/// <summary>
/// Creates a GIF image file at the provided path.
/// </summary>
/// <param name="path">The path where the image is written.</param>
public static void CreateGif(string path)
{
WriteFile(path, [.. Encoding.ASCII.GetBytes("GIF89a")]);
}
/// <summary>
/// Creates a TIFF image file at the provided path.
/// </summary>
/// <param name="path">The path where the image is written.</param>
public static void CreateTiff(string path)
{
WriteFile(path, [0x49, 0x49, 0x2A, 0x00]);
}
/// <summary>
/// Creates a file whose content is not an image, so that the exclusion of non image files can be exercised.
/// </summary>
/// <param name="path">The path where the file is written.</param>
public static void CreateTextFile(string path)
{
WriteFile(path, Encoding.UTF8.GetBytes("This is not an image."));
}
/// <summary>
/// Writes the provided bytes at <paramref name="path"/>, creating the containing directory when it does not exist yet.
/// </summary>
/// <param name="path">The path where the bytes are written.</param>
/// <param name="contents">The bytes of the file.</param>
private static void WriteFile(string path, byte[] contents)
{
string? directory = Path.GetDirectoryName(path);
if (!string.IsNullOrEmpty(directory))
Directory.CreateDirectory(directory);
File.WriteAllBytes(path, contents);
}
}
@@ -0,0 +1,26 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net10.0</TargetFramework>
<ImplicitUsings>disable</ImplicitUsings>
<Nullable>enable</Nullable>
<BaseOutputPath>$(SolutionDir)..\bin\api\</BaseOutputPath>
<IsPackable>false</IsPackable>
<IsTestProject>false</IsTestProject>
<GenerateAssemblyInfo>false</GenerateAssemblyInfo>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="AutoFixture" />
<PackageReference Include="AutoFixture.AutoNSubstitute" />
<PackageReference Include="AutoFixture.Xunit2" />
<PackageReference Include="Bogus" />
<PackageReference Include="TagLibSharp" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\..\plugins\Lumina.Plugins.LocalMusicArtwork\Lumina.Plugins.LocalMusicArtwork.csproj" />
<ProjectReference Include="..\..\..\src\Lumina.Plugins.Contracts\Lumina.Plugins.Contracts.csproj" />
</ItemGroup>
</Project>
@@ -0,0 +1,21 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("Lumina.Plugins.LocalMusicArtwork.Fixtures")]
[assembly: AssemblyProduct("Lumina.Plugins.LocalMusicArtwork.Fixtures")]
// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]
// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("3401EC09-8D4F-4E45-9F40-62FEA3C81373")]
[assembly: InternalsVisibleTo("Lumina.Plugins.LocalMusicArtwork.UnitTests")]
[assembly: InternalsVisibleTo("Lumina.Plugins.LocalMusicArtwork.IntegrationTests")]
[assembly: InternalsVisibleTo("DynamicProxyGenAssembly2")]