#region ========================================================================= USING ===================================================================================== using System; using System.Collections.Generic; using System.Diagnostics.CodeAnalysis; using System.IO; using System.IO.Compression; using System.Linq; using System.Text.Json; #endregion namespace Lumina.Infrastructure.Fixtures.Core.Themes; /// /// Fixture class for generating theme pack ZIP archives used by theme tests. /// [ExcludeFromCodeCoverage] public sealed class ThemePackFixture { /// /// Creates a valid theme pack ZIP archive containing a manifest, the template files referenced by it and a preview asset. /// /// Optional theme id written to the manifest. /// Optional display name written to the manifest. /// Optional description written to the manifest. /// Optional author written to the manifest. /// Optional version written to the manifest. /// Optional schema version written to the manifest. /// Optional template mappings written to the manifest. /// Optional preview path written to the manifest, or to omit the preview. /// Optional content for the default template file. /// Whether to include the preview asset file in the archive. /// Optional additional files written into the archive. /// The created theme pack ZIP archive. public byte[] Create( string themeId = "test-theme", string? name = null, string? description = null, string? author = null, string? version = null, int? schemaVersion = null, Dictionary? templates = null, string? preview = "assets/preview.png", string? defaultTemplateContent = null, bool includePreviewAsset = true, IReadOnlyDictionary? additionalFiles = null) { Dictionary manifestTemplates = templates ?? new Dictionary(StringComparer.OrdinalIgnoreCase) { ["default"] = "templates/default.html" }; string manifestJson = CreateManifestJson( themeId, name, description, author, version, schemaVersion, manifestTemplates, preview); List entries = [new("theme.json", manifestJson)]; foreach ((string key, string templatePath) in manifestTemplates) entries.Add(new(templatePath, defaultTemplateContent ?? $"{key} template")); if (preview is not null && includePreviewAsset) entries.Add(new("assets/preview.png", "preview-image-bytes")); if (additionalFiles is not null) entries.AddRange(additionalFiles.Select(file => new ThemeArchiveEntry(file.Key, file.Value))); return CreateArchive([.. entries]); } /// /// Creates the JSON content of a theme manifest with test defaults that can be overridden per field. /// /// Optional theme id written to the manifest. /// Optional display name written to the manifest. /// Optional description written to the manifest. /// Optional author written to the manifest. /// Optional version written to the manifest. /// Optional schema version written to the manifest. /// Optional template mappings written to the manifest. /// Optional preview path written to the manifest, or to omit the preview. /// The created manifest JSON content. public string CreateManifestJson( string themeId = "test-theme", string? name = null, string? description = null, string? author = null, string? version = null, int? schemaVersion = null, Dictionary? templates = null, string? preview = "assets/preview.png") { object manifest = new { schemaVersion = schemaVersion ?? 1, id = themeId, name = name ?? "Test Theme", description = description ?? "A theme used for testing.", author = author ?? "Lumina Tests", version = version ?? "1.0.0", preview, templates = templates ?? new Dictionary(StringComparer.OrdinalIgnoreCase) { ["default"] = "templates/default.html" } }; return JsonSerializer.Serialize(manifest); } /// /// Creates a ZIP archive from the provided entries, allowing tests to build arbitrary valid and invalid theme packs. /// /// The entries to write into the archive. /// The created ZIP archive bytes. public byte[] CreateArchive(params ThemeArchiveEntry[] entries) { using MemoryStream output = new(); using (ZipArchive archive = new(output, ZipArchiveMode.Create, leaveOpen: true)) { foreach (ThemeArchiveEntry entry in entries) { ZipArchiveEntry zipEntry = archive.CreateEntry(entry.Path); if (entry.UnixFileType is not null) zipEntry.ExternalAttributes = (entry.UnixFileType.Value << 16) | 0x81A4; using StreamWriter writer = new(zipEntry.Open()); writer.Write(entry.Content); } } return output.ToArray(); } /// /// Creates multiple valid theme pack ZIP archives with randomized test data. /// /// Number of archives to create. /// List of configured theme pack ZIP archives. public List CreateMany(int count = 3) { return [.. Enumerable.Range(0, count).Select(index => Create(themeId: $"test-theme-{index}"))]; } }