using System.Text.Json; using System.Numerics; using GamePort.Assets; using GamePort.Application; using GamePort.Game; var root = Path.Combine(Path.GetTempPath(), "gameport-observer-tests-" + Guid.NewGuid().ToString("N")); Directory.CreateDirectory(root); var checks = 0; void Check(bool value, string message) { if (!value) throw new Exception(message); checks++; } FidelityObservation Open(string name, int limit = 250000, TextWriter? sink = null) => FidelityObservation.Create(Path.Combine(root, name), "session-1", "fresh-natural-edge", "port_fixture", Path.GetFullPath("."), Path.Combine(root, "Original"), limit, sink); JsonElement[] Events(string name) => File.ReadAllLines(Path.Combine(root, name, "events.jsonl")) .Select(line => JsonDocument.Parse(line).RootElement.Clone()).ToArray(); JsonElement Summary(string name) => JsonDocument.Parse(File.ReadAllText(Path.Combine(root, name, "observation-summary.json"))).RootElement.Clone(); try { Check(FidelityObservation.FromArguments([], ".", Path.Combine(root, "Original")) == null, "disabled must allocate no observer"); using (var observer = Open("identity")) { observer.RecordInput("LV1B", 17, new(true, false, true, false, true), false, "script"); observer.RecordInput("LV1B", 18, default, true, "Jimmy"); observer.Complete(0); } var records = Events("identity"); Check(records.All(e => e.GetProperty("session_id").GetString() == "session-1" && e.GetProperty("case_id").GetString() == "fresh-natural-edge"), "identity must be bound"); Check(records[1].GetProperty("elapsed_seconds").GetDouble() >= records[0].GetProperty("elapsed_seconds").GetDouble(), "one monotonic clock"); Check(records[0].GetProperty("data").GetProperty("requested_input").GetProperty("forward").GetBoolean() && !records[0].GetProperty("data").GetProperty("accepts_input").GetBoolean() && records[0].GetProperty("data").GetProperty("accepted_input").ValueKind == JsonValueKind.Null, "requested is distinct from accepted"); Check(records[0].GetProperty("simulation_tick").GetInt32() == 17 && records[0].GetProperty("data").GetProperty("control_owner").GetString() == "script", "owner and simulation tick are separate"); using (var observer = Open("bounded", 1)) { observer.RecordInput("LV1B", 1, default, true, "Jimmy"); observer.RecordInput("LV1B", 2, default, true, "Jimmy"); observer.Complete(0); } Check(Events("bounded").Length == 1 && Summary("bounded").GetProperty("incomplete").GetBoolean(), "record limit must mark incomplete"); using (var observer = Open("failure", sink: new BrokenWriter())) { observer.RecordInput("LV1B", 1, default, true, "Jimmy"); observer.Complete(0); } Check(Summary("failure").GetProperty("incomplete").GetBoolean(), "write error must not escape or claim completion"); var disposed = Open("disposed"); disposed.Dispose(); disposed.Dispose(); disposed.RecordInput("LV1B", 1, default, true, "Jimmy"); Check(Summary("disposed").GetProperty("incomplete").GetBoolean(), "disposal without completion remains incomplete and idempotent"); Check(!Summary("identity").GetProperty("incomplete").GetBoolean(), "successful flush and complete"); var rejected = false; try { using var duplicate = Open("identity"); } catch (IOException) { rejected = true; } Check(rejected, "existing sessions cannot be overwritten"); using (var observer = Open("continuation")) { observer.RecordSceneEntry("LV1B", 0, false, false, "tsk-story"); observer.RecordControl("LV1B", 0, false, null, true, "Carl", "owner_snapshot"); observer.RecordInput("LV1B", 1, new(true, false, false, false, false), false, "script"); observer.RecordControl("LV1B", 1, true, "Jimmy", false, null, "director_step"); observer.RecordInput("LV1B", 2, default, true, "Jimmy"); observer.RecordTransition("LV1B", "VR04.gam", 2, false); observer.RecordSceneEntry("VR04", 0, false, true, "retained"); observer.RecordControl("VR04", 0, false, null, true, "Intro", "owner_snapshot"); observer.RecordControl("VR04", 0, true, "Jimmy", false, null, "skip:test"); observer.RecordTransition("VR04", "LV1B.gam", 0, true); observer.RecordSceneEntry("LV1B", 0, true, true, "retained"); observer.Complete(0); } var continuation = Events("continuation"); Check(continuation.Any(e => e.GetProperty("name").GetString() == "dialogue_end" && e.GetProperty("method").GetString() == "natural_director_step"), "natural completion has actual director-step method"); Check(continuation.Any(e => e.GetProperty("name").GetString() == "dialogue_skip" && e.GetProperty("method").GetString() == "skip:test"), "skip is an explicit action path"); Check(continuation.Where(e => e.GetProperty("name").GetString() == "input_step").Last().GetProperty("data").GetProperty("accepted_input").GetProperty("forward").GetBoolean() == false, "first accepted neutral is copied without mutation"); Check(continuation.Select(e => e.GetProperty("sequence").GetInt64()).SequenceEqual(Enumerable.Range(1, continuation.Length).Select(i => (long)i)), "ordered transitions share sequence"); Check(continuation.Last().GetProperty("scene").GetString() == "LV1B" && continuation.Last().GetProperty("data").GetProperty("returning").GetBoolean(), "RETURN and arrival remain paired"); Check(continuation.Zip(continuation.Skip(1)).All(p => p.First.GetProperty("elapsed_seconds").GetDouble() <= p.Second.GetProperty("elapsed_seconds").GetDouble()), "clock survives scene tick reset"); using (var fresh = FidelityObservation.Create(Path.Combine(root, "fresh"), "independent-session", "fresh-natural-ordinary", "port_fixture", ".", Path.Combine(root, "Original"))) { fresh.RecordSceneEntry("LV1B", 0, false, false, "fresh-direct-scene"); fresh.Complete(0); } Check(Events("fresh")[0].GetProperty("clock_id").GetString() != continuation[0].GetProperty("clock_id").GetString() && Events("fresh")[0].GetProperty("simulation_tick").GetInt32() == 0 && !Summary("fresh").GetProperty("incomplete").GetBoolean(), "fresh observer owns independent clock from scene ticks"); rejected = false; try { using var invalid = FidelityObservation.Create(Path.Combine(root, "Original", "unsafe"), "safe", "case", "port_fixture", ".", Path.Combine(root, "Original")); } catch (ArgumentException) { rejected = true; } Check(rejected && !Directory.Exists(Path.Combine(root, "Original")), "original overlap rejected before write"); using (var failedFlush = Open("flush", sink: new FlushFailureWriter())) { failedFlush.RecordInput("LV1B", 1, default, true, "Jimmy"); failedFlush.Complete(0); } Check(Summary("flush").GetProperty("incomplete").GetBoolean(), "flush failure cannot claim success"); Check(continuation.Where(e => e.GetProperty("name").GetString() == "scene_entry").Skip(1) .All(e => continuation.Any(d => d.GetProperty("name").GetString() == "scene_departure" && d.GetProperty("sequence").GetInt64() == e.GetProperty("data").GetProperty("departure_sequence").GetInt64())), "arrival links actual departure sequence"); if (!OperatingSystem.IsWindows()) { Directory.CreateDirectory(Path.Combine(root, "Original")); Directory.CreateSymbolicLink(Path.Combine(root, "asset-alias"), Path.Combine(root, "Original")); rejected = false; try { using var invalid = FidelityObservation.Create(Path.Combine(root, "Original", "unsafe"), "safe", "case", "port_fixture", ".", Path.Combine(root, "asset-alias")); } catch (ArgumentException) { rejected = true; } Check(rejected && !Directory.Exists(Path.Combine(root, "Original", "unsafe")), "asset aliases cannot bypass original containment"); rejected = false; try { using var invalid = FidelityObservation.Create(Path.Combine(root, "asset-alias", "unsafe"), "safe", "case", "port_fixture", ".", Path.Combine(root, "unrelated")); } catch (ArgumentException) { rejected = true; } Check(rejected, "output cannot traverse symbolic-link ancestors"); } var positions = new[] { new Vector3(-100000, 0, -100000), new Vector3(-100000, 0, 100000), new Vector3(100000, 0, 100000), new Vector3(-100000, 0, -100000), new Vector3(100000, 0, 100000), new Vector3(100000, 0, -100000) }; var floor = new Mesh(positions.Select(p => new MeshVertex(p, Vector2.Zero, Vector3.One)).ToArray(), [0, 1, 2, 3, 4, 5]); var standing = new Mesh([new(new(-20, -20, -20), Vector2.Zero, Vector3.One), new(new(20, 80, 20), Vector2.Zero, Vector3.One)], []); var off = new LabSimulation(new SceneCollision(floor), standing, new Vector3(0, 25, 0), 0f); var failed = new LabSimulation(new SceneCollision(floor), standing, new Vector3(0, 25, 0), 0f); using (var observer = Open("failed-simulation", sink: new BrokenWriter())) { for (var tick = 0; tick < 120; tick++) { var input = new PlayerInput(tick < 80, false, tick > 15 && tick < 45, false, tick < 20); off.Step(input); failed.Step(input); observer.RecordInput("fixture-floor", failed.Steps, input, true, "Jimmy"); observer.RecordState("fixture-floor", failed.Steps, failed.ActorRoot, failed.Rotation, failed.Velocity, failed.Grounded, failed.MovementMode, failed.InputAfterPlayerUpdate, failed.ControllerSteps, failed.PhysicsSteps, failed.EffectSteps); } observer.Complete(0); } Check(off.ActorRoot == failed.ActorRoot && off.Rotation == failed.Rotation && off.Velocity == failed.Velocity && off.Steps == failed.Steps && off.ControllerSteps == failed.ControllerSteps && off.PhysicsSteps == failed.PhysicsSteps && off.EffectSteps == failed.EffectSteps && Summary("failed-simulation").GetProperty("incomplete").GetBoolean(), "write failure preserves actual 120-step movement/body/effect state exactly"); Console.WriteLine($"Fidelity observation checks: {checks} passed"); } finally { Directory.Delete(root, true); } sealed class BrokenWriter : StringWriter { public override void WriteLine(string? value) => throw new IOException("deliberate output failure"); } sealed class FlushFailureWriter : StringWriter { protected override void Dispose(bool disposing) => throw new IOException("deliberate flush failure"); }