From b4b73484d17bf214e16e2ac29a9b39ae88684428 Mon Sep 17 00:00:00 2001 From: MFlys Date: Thu, 3 Dec 2026 01:52:35 +0300 Subject: [PATCH] Exercise real Cosa parser in offline scenario runner --- examples/diff/cosa/README.md | 19 +++++ examples/diff/cosa/test_scenario_consumer.py | 77 ++++++++++++++++++++ 2 files changed, 96 insertions(+) create mode 100644 examples/diff/cosa/test_scenario_consumer.py diff --git a/examples/diff/cosa/README.md b/examples/diff/cosa/README.md index 6608dd6..9edeb3b 100644 --- a/examples/diff/cosa/README.md +++ b/examples/diff/cosa/README.md @@ -79,3 +79,22 @@ The existing parser tests remain independent of this report producer. Render this completed report in the standard-library-only [Studio viewer](../../studio/README.md#view-compatibility-observations). The viewer can also combine it with the climate Lab reports. + +## Scenario runner consumer test + +The same pinned consumer and optional environment now exercise the shared +scenario runner through the actual `api.parse_endpoint` function: + + PYTHONPATH="$PWD:$PWD/cosa-comparison-upstream" python -m unittest discover -s examples/diff/cosa -p test_scenario_consumer.py -v + +A trusted test callback adapts the synthetic climate response to the existing +reviewed Cosa fixture and policy, audits the projection, then calls the real +parser. Four observations cover a 31-second virtual sequence, including +integer-to-float normalization on recovery. Negative controls detect an +incorrect recovery expectation and an intentionally stale snapshot; the +consumer's narrower temperature policy still rejects out-of-range values. +The callback maps synthetic 503 responses to unavailable; this does not test +Cosa's actual transport, coordinator cache or received_at clock. The real +parser's wall-clock timestamp is not asserted. No hardware, HTTP or credentials +are used. The existing comparison job discovers these tests automatically. +The released Scenario Preview P1 archive is unchanged. diff --git a/examples/diff/cosa/test_scenario_consumer.py b/examples/diff/cosa/test_scenario_consumer.py new file mode 100644 index 0000000..d19c5bf --- /dev/null +++ b/examples/diff/cosa/test_scenario_consumer.py @@ -0,0 +1,77 @@ +"""Exercise the published Cosa parser inside the synthetic scenario runner.""" +from copy import deepcopy +import json +from pathlib import Path +import unittest + +from custom_components.cosa_v2 import api +from iot_fixturekit import build_fixture, FixtureError +from iot_fixturekit.audit import audit_json +from examples.scenario.offline_regression import REQUEST, run_document + + +class CosaScenarioTests(unittest.TestCase): + def setUp(self): + root = Path(api.__file__).resolve().parents[2] + self.policy = json.loads((root / 'tests/fixture_sources/cosa-endpoint-policy.json').read_text()) + self.baseline = json.loads((root / 'tests/fixtures/cosa-endpoint.json').read_text())['data'] + self.times = [] + self.document = {'schema_version': 1, 'steps': [ + {'response': {'status': 200, 'temperature': 21.2}, + 'expected': {'status': 'available', 'temperature': 21.2}}, + {'advance_ms': 1000, 'response': {'status': 503}, + 'expected': {'status': 'unavailable'}}, + {'advance_ms': 29000, 'response': {'status': 503}, + 'expected': {'status': 'unavailable'}}, + {'advance_ms': 1000, 'response': {'status': 200, 'temperature': 23}, + 'expected': {'status': 'available', 'temperature': 23.0}}, + ]} + + def read_cosa(self, session): + self.times.append(session.monotonic_ms()) + frame = session.request(REQUEST) + if frame.status == 503: + return {'status': 'unavailable'} + if frame.status != 200: + raise FixtureError('unexpected response status') + raw = deepcopy(self.baseline) + raw['endpoint']['temperature'] = json.loads(frame.body)['temperature'] + fixture = build_fixture(raw, self.policy, profile='cosa') + if audit_json(fixture): + raise FixtureError('unexpected fixture finding') + try: + snapshot = api.parse_endpoint(fixture['data'], 'endpoint-test-1') + except api.CosaPayloadError: + raise FixtureError('consumer parser rejected payload') from None + return {'status': 'available', 'temperature': snapshot.room_temperature} + + def test_parser_recovery_and_numeric_normalization(self): + baseline = deepcopy(self.baseline) + document = deepcopy(self.document) + self.assertEqual(run_document(self.document, self.read_cosa), { + 'kind': 'custom-climate', 'status': 'pass', 'observations': 4, + 'differences': {}, 'virtual_elapsed_ms': 31000}) + self.assertEqual(self.times, [0, 1000, 30000, 31000]) + self.assertEqual(self.baseline, baseline) + self.assertEqual(self.document, document) + + def test_wrong_recovery_expectation_detected(self): + self.document['steps'][-1]['expected']['temperature'] = 24.0 + result = run_document(self.document, self.read_cosa) + self.assertEqual(result['status'], 'fail') + self.assertEqual(result['differences'], {'values_changed': 1}) + + def test_cached_snapshot_during_outage_detected(self): + last = None + def stale(session): + nonlocal last + state = self.read_cosa(session) + if state['status'] == 'available': + last = state + return last if state['status'] == 'unavailable' else state + self.assertEqual(run_document(self.document, stale)['status'], 'fail') + + def test_consumer_policy_remains_stricter(self): + self.document['steps'][-1]['response']['temperature'] = 90 + self.assertEqual(run_document(self.document, self.read_cosa), { + 'kind': 'custom-climate', 'status': 'error', 'stage': 'replay'})