using System.Buffers.Binary; using System.IO.Compression; using XFEWarframe.Client.Runtime; using Xunit; namespace XFEWarframe.Client.Tests; public sealed class VisualAssetTests { [Fact] public void NativeCardBatchesPreserveAllRanksAndFitActualUtf8RequestLimits() { var cards = Enumerable.Range(0, 417).Select(i => new NativeCardIdentity("/Lotus/Upgrades/Mods/" + i, i % 7 == 0 ? "{\"stats\":\"" + new string('中', 5000) + "\"}" : "{\"lvl\":" + i % 10 + "}")).ToArray(); var requests = NativeCardCacheRequest.Batches("zh", cards).ToArray(); Assert.Equal(cards, requests.SelectMany(x => x.Cards)); Assert.All(requests, request => { Assert.InRange(request.Cards.Length, 1, 200); Assert.InRange(System.Text.Json.JsonSerializer.SerializeToUtf8Bytes(request, VisualCacheJson.Default.NativeCardCacheRequest).Length, 1, NativeCardCacheRequest.MaximumBatchBytes); }); Assert.Empty(NativeCardCacheRequest.Batches("zh", [])); Assert.All(NativeCardCacheRequest.Batches("zh", cards, refreshExisting: true), request => Assert.True(request.RefreshExisting)); Assert.Throws(() => NativeCardCacheRequest.Batches("zh", [new("/Lotus/Invalid", new string('x', 16385))]).ToArray()); Assert.Throws(() => NativeCardCacheRequest.Batches("zh", [new("/Lotus/Invalid", new string('中', 16384))]).ToArray()); } [Fact] public void CaptureCroppingPreservesEveryRgbaPixelAndProducesAValidPng() { var pixels = Enumerable.Range(0, 4 * 3 * 4).Select(i => (byte)i).ToArray(); var png = PngImages.CropRgba(pixels, 4, 3, 1, 1, 2, 2); PngImages.Validate(png); Assert.Equal(2, BinaryPrimitives.ReadInt32BigEndian(png.AsSpan(16, 4))); Assert.Equal(2, BinaryPrimitives.ReadInt32BigEndian(png.AsSpan(20, 4))); using var data = new MemoryStream(); for (var position = 8; position < png.Length;) { var length = BinaryPrimitives.ReadInt32BigEndian(png.AsSpan(position, 4)); if (png.AsSpan(position + 4, 4).SequenceEqual("IDAT"u8)) data.Write(png, position + 8, length); position += length + 12; } data.Position = 0; using var zip = new ZLibStream(data, CompressionMode.Decompress); using var decoded = new MemoryStream(); zip.CopyTo(decoded); Assert.Equal(new byte[] { 0 }.Concat(pixels.Skip(20).Take(8)).Concat(new byte[] { 0 }).Concat(pixels.Skip(36).Take(8)), decoded.ToArray()); Assert.Throws(() => PngImages.CropRgba(pixels, 4, 3, int.MaxValue, 1, 2, 2)); Assert.Throws(() => PngImages.CropRgba(pixels[..4], 4, 3, 0, 0, 1, 1)); } [Fact] public async Task CacheRejectsCorruptOrTruncatedImagesAndKeepsTheLastValidOriginal() { var key = VisualAssetCache.Key("test", Guid.NewGuid().ToString("N")); var png = PngImages.CropRgba([12, 34, 56, 255], 1, 1, 0, 0, 1, 1); string? path = null; try { path = await VisualAssetCache.StoreAsync(key, png); Assert.Equal(path, VisualAssetCache.Find(key)); var corrupt = png.ToArray(); corrupt[45] ^= 1; await Assert.ThrowsAsync(() => VisualAssetCache.StoreAsync(key, corrupt)); await Assert.ThrowsAsync(() => VisualAssetCache.StoreAsync(key, png[..^3])); await Assert.ThrowsAsync(() => VisualAssetCache.StoreAsync("../escape", png)); Assert.Equal(png, await File.ReadAllBytesAsync(path)); Assert.Empty(Directory.EnumerateFiles(VisualAssetCache.Root, key + ".*.tmp")); } finally { if (path is not null) File.Delete(path); } } [Fact] public void OriginalCardIdentityIncludesRankLanguageAndBuildButIgnoresJsonFormatting() { const string type = "/Lotus/Upgrades/Mods/Warframe/AvatarHealthMaxMod"; string Key(string fp, string language = "zh", string build = "2026.08.19.11.06") => VisualAssetCache.Key("card-compact", type, fp, language, build); Assert.Equal(Key(""), Key("{}")); Assert.Equal(Key("{}"), Key("{ \"lvl\" : 0 }")); Assert.Equal(Key("{\"a\":1,\"zzz\":2}"), Key("{\"zzz\":2,\"lvl\":0,\"a\":1}")); Assert.NotEqual(Key("{\"lvl\":1}"), Key("{\"lvl\":2}")); Assert.NotEqual(Key("{}"), Key("{}", "en")); Assert.NotEqual(Key("{}"), Key("{}", build: "future-build")); Assert.Null(VisualAssetCache.Find("../escape")); } [Fact] public void CustomInfestationKingFaceCannotCollideWithItsOriginalNativeCarrier() { const string carrier = "/Lotus/Powersuits/Infestation/InfestPassiveAugmentCard"; var original = NativeCardIdentity.FromGame(carrier, "{\"lvl\":3}"); var custom = NativeCardIdentity.FromGame(carrier, "{\"lvl\":3,\"xfe\":\"InfestationKing\"}"); Assert.Equal(carrier, original.Type); Assert.Equal("/Lotus/Upgrades/XFEWarframe/InfestationKing", custom.Type); Assert.Equal("{\"lvl\":3}", custom.Fingerprint); Assert.NotEqual(VisualAssetCache.Key("card-compact", original.Type, original.Fingerprint), VisualAssetCache.Key("card-compact", custom.Type, custom.Fingerprint)); var rendered = custom.ToGame(); Assert.Equal(carrier, rendered.Type); var recovered = NativeCardIdentity.FromGame(rendered.Type, rendered.Fingerprint); Assert.Equal(custom.Type, recovered.Type); Assert.Equal(VisualAssetCache.Key("card-compact", custom.Type, custom.Fingerprint), VisualAssetCache.Key("card-compact", recovered.Type, recovered.Fingerprint)); Assert.Equal(original, original.ToGame()); } [Fact] public void ImportedLegacyArcaneUsesCurrentNativeResourceWithoutChangingFingerprint() { var card = new NativeCardIdentity("/Lotus/Upgrades/CosmeticEnhancers/Offensive/MeleeDamageOnCrit", "{\"lvl\":5}"); var native = card.ToGame(); Assert.Equal("/Lotus/Upgrades/CosmeticEnhancers/Offensive/GolemArcaneMeleeDamageOnCrit", native.Type); Assert.Equal(card.Fingerprint, native.Fingerprint); } [Fact] public void NativeMattingPreservesOriginalColorsAndTransparencyWithoutGameplayPixels() { const int frameWidth = 120, frameHeight = 80, width = 100, height = 64; var dark = new byte[frameWidth * frameHeight * 4]; var light = Enumerable.Repeat((byte)255, dark.Length).ToArray(); var expected = new byte[width * height * 4]; for (var y = 8; y < 56; y++) for (var x = 10; x < 90; x++) { var index = (y * width + x) * 4; var frame = ((y + 8) * frameWidth + x + 10) * 4; var alpha = x < 14 ? 128 : 255; for (var c = 0; c < 3; c++) { var color = 16 + (x * 17 + y * 7 + c * 61) % 224; expected[index + c] = (byte)color; dark[frame + c] = (byte)((color * alpha + 127) / 255); light[frame + c] = (byte)((color * alpha + 255 * (255 - alpha) + 127) / 255); } expected[index + 3] = (byte)alpha; } var png = NativeCardPixels.Encode(dark, light, frameWidth, frameHeight, 10, 8, width, height); PngImages.Validate(png); var decoded = DecodePixels(png, width, height); for (var i = 0; i < expected.Length; i++) Assert.InRange(Math.Abs(expected[i] - decoded[i]), 0, 1); var trimmed = NativeCardPixels.Encode(dark, light, frameWidth, frameHeight, 10, 8, width, height, trimTransparentMargin: true); Assert.Equal(84, BinaryPrimitives.ReadInt32BigEndian(trimmed.AsSpan(16, 4))); Assert.Equal(52, BinaryPrimitives.ReadInt32BigEndian(trimmed.AsSpan(20, 4))); var compact = DecodePixels(trimmed, 84, 52); for (var y = 0; y < 52; y++) for (var x = 0; x < 84; x++) for (var c = 0; c < 4; c++) Assert.InRange(Math.Abs(expected[((y + 6) * width + x + 8) * 4 + c] - compact[(y * 84 + x) * 4 + c]), 0, 1); } [Fact] public void NativeMattingRejectsUnpaintedStaleOccludedAndOutOfBoundsFrames() { var black = new byte[100 * 64 * 4]; var white = Enumerable.Repeat((byte)255, black.Length).ToArray(); Assert.Throws(() => NativeCardPixels.Encode(black, white, 100, 64, 0, 0, 100, 64)); Assert.Throws(() => NativeCardPixels.Encode(white, white, 100, 64, 0, 0, 100, 64)); for (var y = 20; y < 28; y++) for (var x = 30; x < 50; x++) for (var c = 0; c < 3; c++) black[(y * 100 + x) * 4 + c] = white[(y * 100 + x) * 4 + c] = (byte)(x * 5); Assert.Throws(() => NativeCardPixels.Encode(black, white, 100, 64, 0, 0, 100, 64)); Assert.Throws(() => NativeCardPixels.Encode(black, white, 100, 64, -1, 0, 100, 64)); Assert.Throws(() => NativeCardPixels.Encode(black, white, 100, 64, 0, 0, 101, 64)); Assert.Throws(() => NativeCardPixels.Encode(black, white[..^1], 100, 64, 0, 0, 100, 64)); } private static byte[] DecodePixels(byte[] png, int width, int height) { using var data = new MemoryStream(); for (var position = 8; position < png.Length;) { var length = BinaryPrimitives.ReadInt32BigEndian(png.AsSpan(position, 4)); if (png.AsSpan(position + 4, 4).SequenceEqual("IDAT"u8)) data.Write(png, position + 8, length); position += length + 12; } data.Position = 0; using var zip = new ZLibStream(data, CompressionMode.Decompress); using var decoded = new MemoryStream(); zip.CopyTo(decoded); var filtered = decoded.ToArray(); var result = new byte[width * height * 4]; for (var y = 0; y < height; y++) { Assert.Equal(0, filtered[y * (width * 4 + 1)]); filtered.AsSpan(y * (width * 4 + 1) + 1, width * 4).CopyTo(result.AsSpan(y * width * 4)); } return result; } }