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,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);
}
}