Files

162 lines
9.6 KiB
C#

using System.Numerics;
using System.Text.Json;
using GamePort.Application;
using GamePort.Assets;
using GamePort.Game;
var count = 0;
void Check(bool condition, string message) { if (!condition) throw new Exception(message); }
void Near(float actual, float expected, string message) =>
Check(MathF.Abs(actual - expected) < .0002f, $"{message}: {actual} != {expected}");
void Test(string name, Action action) { action(); count++; Console.WriteLine($"PASS {name}"); }
void Invalid(Action action)
{
try { action(); }
catch (Exception exception) when (exception is ArgumentException or InvalidDataException) { return; }
throw new Exception("Invalid input accepted");
}
BubbleEffect Active(bool conventionalBlend = false)
{
var effect = new BubbleEffect(conventionalBlend);
effect.Start(); effect.Step(.5f); effect.Step(.5f); effect.Step(.01f);
Check(effect.Active && !effect.Expanding, "expansion completes strictly after1");
return effect;
}
Test("strict expansion boundary changes all RGBA without changing geometry", () =>
{
var effect = new BubbleEffect();
Check(!effect.Visible && effect.Width == 200 && effect.Height == 200, "constructor hidden200 square");
effect.Start(); effect.Step(.5f);
Check(effect.Visible && effect.Expanding && !effect.Active && effect.Color == new Vector4(.5f), "half expansion fullRGBA");
Check(effect.Width == 200 && effect.Height == 200, "fade retains source dimensions");
effect.Step(.5f);
Check(effect.Expanding && !effect.Active && effect.Color == Vector4.One, "exact1 still expanding");
effect.Step(.01f);
Check(effect.Active && !effect.Expanding && effect.Width == 200, "completion frame still fade branch");
effect.Step(0);
Check(effect.Width == 240, "next active invocation adopts pulse geometry");
Near(effect.Height, (float)(240 + Math.Sin((double)effect.Timer * 10) * 30), "height pulse");
});
Test("active graphics branches preserve additive RGBA or set half alpha", () =>
{
foreach (var conventional in new[] { false, true })
{
var initial = new BubbleEffect(conventional);
Check(initial.Color == (conventional ? new Vector4(.9f, .9f, .9f, 1) : Vector4.One), "source initialization graphics branch");
var effect = Active(conventional);
effect.Step(.25f);
Check(effect.Color == (conventional ? new Vector4(1, 1, 1, .5f) : Vector4.One), "source graphics branch");
Near(effect.Width, 240, "pulse fixed width");
Check(effect.Height >= 210 && effect.Height <= 270, "30 unit height excursion");
}
});
Test("Stop leaf retains expansion priority; cleanup explicitly clears it", () =>
{
var leaf = new BubbleEffect(); leaf.Start(); leaf.Step(.25f); leaf.Stop(); leaf.Step(.25f);
Check(leaf.Expanding && leaf.Collapsing && leaf.Color == new Vector4(.25f), "expansion still wins without cleanup write");
leaf.Stop(); leaf.ClearExpansion(); leaf.Step(.25f);
Check(!leaf.Expanding && leaf.Collapsing && leaf.Color == new Vector4(.75f), "cleanup reveals collapse");
leaf.Step(.75f);
Check(leaf.Timer == 1 && leaf.Color == Vector4.Zero && leaf.Visible && leaf.Collapsing, "exactzero remains visible/collapsing");
leaf.Step(.001f);
Check(!leaf.Visible && !leaf.Collapsing && leaf.Color == Vector4.Zero, "strictnegative hides");
});
Test("fade compares unrounded addition even when stored timer rounds to one", () =>
{
var effect = new BubbleEffect(); effect.Start(); effect.Step(.5f); effect.Step(.5f);
effect.Step(MathF.Pow(2, -25));
Check(effect.Timer == 1 && effect.Active && !effect.Expanding, "x87 sum before float store controls strict comparison");
effect.Stop(); effect.ClearExpansion(); effect.Step(.5f); effect.Step(.5f); effect.Step(MathF.Pow(2, -25));
Check(effect.Timer == 1 && !effect.Collapsing && !effect.Visible, "unrounded collapse comparison");
});
Test("independent timer, retained dimensions, Reset and bounded timestep", () =>
{
var effect = Active(); effect.Step(.2f);
var height = effect.Height;
effect.Stop(); effect.ClearExpansion(); effect.Step(.5f);
Check(effect.Width == 240 && effect.Height == height && effect.Color == new Vector4(.5f), "collapse fades last active geometry");
effect.Step(.6f);
var hiddenTimer = effect.Timer; effect.Step(.1f);
Check(effect.Timer > hiddenTimer, "hidden effect still advances independently");
effect.Reset();
Check(!effect.Visible && !effect.Active && !effect.Expanding && !effect.Collapsing
&& effect.Timer == 0 && effect.Color == Vector4.One && effect.Width == 200 && effect.Height == 200, "explicit reset");
foreach (var seconds in new[] { -.1f, 1f, float.NaN, float.PositiveInfinity }) Invalid(() => effect.Step(seconds));
});
var prepared = args.Length == 0 ? "artifacts/extracted" : args[0];
Test("original texture world quad preserves source colors and collision metadata", () =>
{
var source = new Mesh([new(Vector3.Zero, Vector2.Zero, Vector3.One)], [0, 0, 0])
{
CollisionPolygons = [new(2, 3, Vector3.UnitY, "material")], HasCollisionMaterialIds = true,
Textures = new() { [int.MinValue + 16000] = PngImage.White }
};
var visual = new BubbleVisuals(source, prepared);
Check(visual.Mesh.Vertices.Length == 5 && visual.Mesh.Indices.Length == 9 && visual.Mesh.Draws.Length == 2, "append quad and implicit source draw");
Check(ReferenceEquals(visual.Mesh.CollisionPolygons, source.CollisionPolygons) && visual.Mesh.HasCollisionMaterialIds, "collision metadata preserved");
var draw = visual.Mesh.Draws[^1];
Check(!draw.ScreenSpace && draw.Blend == MeshBlend.Additive && !draw.Visible, "world-space blended draw starts hidden");
Check(draw.TextureId != int.MinValue + 16000 && visual.Mesh.Textures[draw.TextureId].Width == 146, "texture collision avoided");
var effect = new BubbleEffect(); effect.Start(); effect.Step(.5f);
var root = new Vector3(10, 20, 30);
visual.Update(visual.Mesh, effect, root, new(0, 0, -1000), Vector3.Zero);
Check(draw.Visible && draw.Opacity == .5f && draw.Blend == MeshBlend.Additive, "RGBA alpha and additive blend");
var corners = visual.Mesh.Vertices.Skip(1).ToArray();
Check(corners.All(v => v.Color == new Vector3(.5f)), "RGB fade carried by all vertices");
Check(corners.Aggregate(Vector3.Zero, (sum, v) => sum + v.Position) / 4 == root, "caller root is quad center");
Near(Vector3.Distance(corners[0].Position, corners[1].Position), 200, "width remains200 while fading");
Near(Vector3.Distance(corners[1].Position, corners[2].Position), 200, "height remains200 while fading");
Check(corners.Select(v => v.Uv).SequenceEqual(new Vector2[] { new(0, 1), new(1, 1), new(1, 0), new(0, 0) }), "entire original image");
effect = Active(true); effect.Step(.1f);
visual.Update(visual.Mesh, effect, root, new(0, 0, -1000), Vector3.Zero);
Check(draw.Blend == MeshBlend.Alpha && draw.Opacity == .5f && visual.Mesh.Vertices[1].Color == Vector3.One, "conventional graphics branch");
effect.Reset(); visual.Update(visual.Mesh, effect, root, Vector3.Zero, Vector3.Zero);
Check(!draw.Visible, "reset hides owned quad");
});
Test("camera degeneracies produce finite orthonormal corners", () =>
{
var visual = new BubbleVisuals(new([], []), prepared);
var effect = Active();
foreach (var target in new[] { Vector3.Zero, Vector3.UnitY, -Vector3.UnitY, new Vector3(3, 4, 5) })
{
visual.Update(visual.Mesh, effect, Vector3.One, Vector3.Zero, target);
Check(visual.Mesh.Vertices.All(v => float.IsFinite(v.Position.X + v.Position.Y + v.Position.Z)), "finite camera-facing basis");
Near(Vector3.Distance(visual.Mesh.Vertices[0].Position, visual.Mesh.Vertices[1].Position), 200, "orthonormal horizontal edge");
Near(Vector3.Distance(visual.Mesh.Vertices[1].Position, visual.Mesh.Vertices[2].Position), 200, "orthonormal vertical edge");
}
Invalid(() => visual.Update(new([], []), effect, Vector3.Zero, Vector3.Zero, Vector3.UnitZ));
Invalid(() => visual.Update(visual.Mesh, effect, new(float.NaN), Vector3.Zero, Vector3.UnitZ));
});
Test("manifest traversal, dimensions and altered original pixels are rejected", () =>
{
var directory = Path.Combine(Path.GetTempPath(), "neutron-bubble-test-" + Guid.NewGuid());
Directory.CreateDirectory(Path.Combine(directory, "bubble"));
try
{
var source = Path.Combine(prepared, "bubble");
using var manifest = JsonDocument.Parse(File.ReadAllBytes(Path.Combine(source, "bubble.json")));
var root = JsonSerializer.Deserialize<Dictionary<string, JsonElement>>(manifest.RootElement.GetRawText())!;
var texture = JsonSerializer.Deserialize<Dictionary<string, JsonElement>>(root["textures"][0].GetRawText())!;
void Save() { root["textures"] = JsonSerializer.SerializeToElement(new[] { texture }); File.WriteAllText(Path.Combine(directory, "bubble/bubble.json"), JsonSerializer.Serialize(root)); }
texture["path"] = JsonSerializer.SerializeToElement("../outside.rgba"); Save();
File.Copy(Path.Combine(source, "canvas_1.rgba"), Path.Combine(directory, "outside.rgba"));
Invalid(() => new BubbleVisuals(new([], []), directory));
texture["path"] = JsonSerializer.SerializeToElement("canvas_1.rgba");
texture["width"] = JsonSerializer.SerializeToElement(145); Save();
Invalid(() => new BubbleVisuals(new([], []), directory));
texture["width"] = JsonSerializer.SerializeToElement(146); Save();
File.WriteAllBytes(Path.Combine(directory, "bubble/canvas_1.rgba"), new byte[146 * 146 * 4]);
Invalid(() => new BubbleVisuals(new([], []), directory));
}
finally { Directory.Delete(directory, true); }
});
Console.WriteLine($"Bubble effect/visual checks passed: {count}");