test(01-04): specify passive port input observation
This commit is contained in:
1 parent
e815534895
commit
773c228a30
3 files changed
+152
No files matched your search
@@ -0,0 +1,9 @@
|
|||||||
|
<Project Sdk="Microsoft.NET.Sdk">
|
||||||
|
<PropertyGroup><OutputType>Exe</OutputType><TargetFramework>net10.0</TargetFramework><ImplicitUsings>enable</ImplicitUsings><Nullable>enable</Nullable><EnableDefaultCompileItems>false</EnableDefaultCompileItems></PropertyGroup>
|
||||||
|
<ItemGroup>
|
||||||
|
<Compile Include="Program.cs"/>
|
||||||
|
<Compile Include="../../src/GamePort/Application/FidelityObservation.cs"/>
|
||||||
|
<Compile Include="../../src/GamePort/Assets/*.cs"/>
|
||||||
|
<Compile Include="../../src/GamePort/Game/*.cs"/>
|
||||||
|
</ItemGroup>
|
||||||
|
</Project>
|
||||||
@@ -0,0 +1,50 @@
|
|||||||
|
using System.Text.Json;
|
||||||
|
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");
|
||||||
|
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");
|
||||||
|
}
|
||||||
@@ -0,0 +1,93 @@
|
|||||||
|
"""Actual port-process observation plumbing; fixtures never fill native acceptance."""
|
||||||
|
import json
|
||||||
|
import os
|
||||||
|
from pathlib import Path
|
||||||
|
import subprocess
|
||||||
|
import sys
|
||||||
|
import tempfile
|
||||||
|
import unittest
|
||||||
|
|
||||||
|
ROOT = Path(__file__).resolve().parents[1]
|
||||||
|
sys.path.insert(0, str(ROOT / 'tools/recon'))
|
||||||
|
import fidelity_receipts as receipts
|
||||||
|
|
||||||
|
|
||||||
|
class FidelityObservation(unittest.TestCase):
|
||||||
|
def run_port(self, *args):
|
||||||
|
# Fixed shell source; caller values are positional argv, never shell code.
|
||||||
|
return subprocess.run(['bash', '-lc', 'source scripts/env.sh && dotnet run --no-build -- "$@"',
|
||||||
|
'observer-test', *map(str, args)], cwd=ROOT, text=True,
|
||||||
|
capture_output=True, timeout=90)
|
||||||
|
|
||||||
|
def test_real_port_exposes_observation_options(self):
|
||||||
|
result = self.run_port('--help')
|
||||||
|
self.assertEqual(result.returncode, 0, result.stderr)
|
||||||
|
self.assertIn('--fidelity-output', result.stdout, 'actual port must expose passive observation')
|
||||||
|
self.assertIn('--fidelity-session', result.stdout)
|
||||||
|
self.assertIn('--fidelity-case', result.stdout)
|
||||||
|
|
||||||
|
def fixture(self, directory, observed=True):
|
||||||
|
args = ['--headless', '--mute', '--no-validation', '--smoke-control-phases',
|
||||||
|
'--width', '320', '--height', '240', '--screenshot', directory / 'frame.ppm']
|
||||||
|
if observed:
|
||||||
|
args += ['--fidelity-output', directory / 'observation', '--fidelity-session', 'process-session',
|
||||||
|
'--fidelity-case', 'fresh-skipped-edge']
|
||||||
|
result = self.run_port(*args)
|
||||||
|
self.assertEqual(result.returncode, 0, result.stdout + result.stderr)
|
||||||
|
return result
|
||||||
|
|
||||||
|
def observed_fixture(self):
|
||||||
|
temporary = tempfile.TemporaryDirectory(prefix='gameport-observer-')
|
||||||
|
self.addCleanup(temporary.cleanup)
|
||||||
|
root = Path(temporary.name)
|
||||||
|
result = self.fixture(root)
|
||||||
|
output = root / 'observation'
|
||||||
|
summary = json.loads((output / 'observation-summary.json').read_text())
|
||||||
|
records = [json.loads(line) for line in (output / 'events.jsonl').read_text().splitlines()]
|
||||||
|
self.assertTrue(records, 'real process must emit records')
|
||||||
|
return root, output, summary, records, result
|
||||||
|
|
||||||
|
def receipt(self, output, summary, records):
|
||||||
|
duration = summary['elapsed_seconds']
|
||||||
|
return dict(schema_version=1, receipt_id='observer-fixture', session_id=summary['session_id'],
|
||||||
|
case_id=summary['case_id'], evidence_kind=summary['evidence_kind'], status='captured',
|
||||||
|
platform={'os': 'linux', 'execution': 'port_fixture'}, historical=False,
|
||||||
|
scope='observer-wiring', identity={}, invocation={'argv': ['bounded-existing-fixture'], 'cwd': '.'},
|
||||||
|
clocks={'wall_start': summary['wall_start'], 'wall_end': summary['wall_end'],
|
||||||
|
'monotonic_start': 0, 'monotonic_end': duration},
|
||||||
|
artifacts=[dict(receipts.identify_file(output / 'events.jsonl'), path='events.jsonl',
|
||||||
|
role='input_events', duration_seconds=duration)], derivatives=[],
|
||||||
|
events=[dict(name=e['name'], artifact='events.jsonl', media_seconds=e['elapsed_seconds'],
|
||||||
|
elapsed_seconds=e['elapsed_seconds'], method=e['method'], uncertainty_seconds=0,
|
||||||
|
simulation_tick=e['simulation_tick']) for e in records],
|
||||||
|
limitations=['Bounded injected fixture; observer perturbs timing'], missing_evidence=[],
|
||||||
|
acceptance={'state': 'open', 'remaining': ['Full native route and audiovisual review required']})
|
||||||
|
|
||||||
|
def test_actual_fixture_emits_requested_and_accepted_input(self):
|
||||||
|
_, output, summary, records, _ = self.observed_fixture()
|
||||||
|
self.assertFalse(summary['incomplete'])
|
||||||
|
self.assertEqual(summary['evidence_kind'], 'port_fixture')
|
||||||
|
inputs = [e for e in records if e['name'] == 'input_step']
|
||||||
|
self.assertTrue(any(not e['data']['accepts_input'] and e['data']['requested_input']['forward'] for e in inputs))
|
||||||
|
self.assertTrue(any(e['data']['accepts_input'] for e in inputs))
|
||||||
|
result = receipts.validate_receipt(self.receipt(output, summary, records), output)
|
||||||
|
self.assertTrue(result['valid'])
|
||||||
|
self.assertFalse(result['accepted'])
|
||||||
|
|
||||||
|
def test_fixture_cannot_promote_native_acceptance(self):
|
||||||
|
_, output, summary, records, _ = self.observed_fixture()
|
||||||
|
receipt = self.receipt(output, summary, records)
|
||||||
|
receipt['acceptance']['state'] = 'accepted'
|
||||||
|
with self.assertRaises(ValueError): receipts.validate_receipt(receipt, output)
|
||||||
|
|
||||||
|
def test_missing_output_and_incomplete_records_do_not_qualify(self):
|
||||||
|
_, output, summary, records, _ = self.observed_fixture()
|
||||||
|
receipt = self.receipt(output, summary, records)
|
||||||
|
receipt['status'] = 'aborted'
|
||||||
|
self.assertFalse(receipts.validate_receipt(receipt, output)['accepted'])
|
||||||
|
(output / 'events.jsonl').unlink()
|
||||||
|
with self.assertRaises((ValueError, OSError)): receipts.validate_receipt(receipt, output)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
unittest.main()
|
||||||
Reference in new issue
Block a user