diff --git a/tests/GamePort.FidelityObservationTests/Program.cs b/tests/GamePort.FidelityObservationTests/Program.cs index dba8d25..6d7c4c4 100644 --- a/tests/GamePort.FidelityObservationTests/Program.cs +++ b/tests/GamePort.FidelityObservationTests/Program.cs @@ -40,6 +40,36 @@ try 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 = Open("fresh")) { fresh.RecordSceneEntry("LV1B", 0, false, false, "fresh-direct-scene"); fresh.Complete(0); } + Check(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"); + var writer = new StringWriter(); + 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"); Console.WriteLine($"Fidelity observation checks: {checks} passed"); } finally { Directory.Delete(root, true); } @@ -48,3 +78,8 @@ 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"); +} diff --git a/tests/test_fidelity_observation.py b/tests/test_fidelity_observation.py index 8e21b94..ae11fc0 100644 --- a/tests/test_fidelity_observation.py +++ b/tests/test_fidelity_observation.py @@ -88,6 +88,52 @@ class FidelityObservation(unittest.TestCase): (output / 'events.jsonl').unlink() with self.assertRaises((ValueError, OSError)): receipts.validate_receipt(receipt, output) + def test_retained_continuation_keeps_one_ordered_session_clock(self): + with tempfile.TemporaryDirectory(prefix='gameport-observer-return-') as temporary: + output = Path(temporary) / 'observation' + result = self.run_port('--headless', '--mute', '--no-validation', '--smoke-practice-return', + '--width', '320', '--height', '240', '--fidelity-output', output, + '--fidelity-session', 'retained-process', '--fidelity-case', 'retained-skipped-edge') + self.assertEqual(result.returncode, 0, result.stdout + result.stderr) + records = [json.loads(line) for line in (output / 'events.jsonl').read_text().splitlines()] + entries = [e for e in records if e['name'] == 'scene_entry'] + self.assertEqual([e['scene'] for e in entries], ['LV1B', 'VR04', 'LV1B'], + 'actual continuation must publish source/destination scene entries') + self.assertEqual([e['data']['returning'] for e in entries], [False, False, True]) + self.assertEqual([e['data']['retained_player'] for e in entries], [False, True, True]) + departures = [e for e in records if e['name'] == 'scene_departure'] + self.assertEqual(len(departures), 2) + self.assertEqual(departures[-1]['data']['returning'], True) + self.assertEqual({e['session_id'] for e in records}, {'retained-process'}) + self.assertEqual({e['evidence_kind'] for e in records}, {'port_fixture'}) + self.assertEqual([e['sequence'] for e in records], list(range(1, len(records) + 1))) + times = [e['elapsed_seconds'] for e in records] + self.assertEqual(times, sorted(times)) + + def test_observed_off_fixture_state_and_readbacks_match(self): + with tempfile.TemporaryDirectory(prefix='gameport-observer-parity-') as temporary: + root = Path(temporary) + on, off = root / 'on', root / 'off' + on.mkdir(); off.mkdir() + enabled, disabled = self.fixture(on), self.fixture(off, observed=False) + captures = sorted(p.name for p in on.glob('*.ppm')) + self.assertGreaterEqual(len(captures), 4) + self.assertEqual(captures, sorted(p.name for p in off.glob('*.ppm'))) + for name in captures: + self.assertEqual((on / name).read_bytes(), (off / name).read_bytes(), name) + prefixes = ('Movement ', 'Carl ', 'Control-phase ', 'Scene sequence:', 'Simulation:') + self.assertEqual([s for s in enabled.stdout.splitlines() if s.startswith(prefixes)], + [s for s in disabled.stdout.splitlines() if s.startswith(prefixes)]) + + def test_skip_and_natural_paths_are_distinct(self): + _, _, _, records, _ = self.observed_fixture() + skips = [e for e in records if e['name'] == 'dialogue_skip'] + self.assertTrue(skips, 'fixture uses actual skip paths') + self.assertTrue(all(e['method'].startswith('skip:') for e in skips)) + ends = [e for e in records if e['name'] == 'dialogue_end'] + self.assertTrue(any(e['method'].startswith('skip:') for e in ends)) + self.assertFalse(any(e['method'] == 'natural_director_step' for e in ends)) + if __name__ == '__main__': unittest.main()