piphi-runtime-testkit-python 0.3.1__tar.gz → 0.3.3__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (28) hide show
  1. {piphi_runtime_testkit_python-0.3.1 → piphi_runtime_testkit_python-0.3.3}/PKG-INFO +1 -1
  2. {piphi_runtime_testkit_python-0.3.1 → piphi_runtime_testkit_python-0.3.3}/pyproject.toml +1 -1
  3. {piphi_runtime_testkit_python-0.3.1 → piphi_runtime_testkit_python-0.3.3}/src/piphi_runtime_testkit_python/__init__.py +4 -0
  4. {piphi_runtime_testkit_python-0.3.1 → piphi_runtime_testkit_python-0.3.3}/src/piphi_runtime_testkit_python/behavior_contract.py +116 -13
  5. {piphi_runtime_testkit_python-0.3.1 → piphi_runtime_testkit_python-0.3.3}/src/piphi_runtime_testkit_python/cross_contract.py +26 -7
  6. {piphi_runtime_testkit_python-0.3.1 → piphi_runtime_testkit_python-0.3.3}/tests/test_behavior_contract.py +22 -0
  7. {piphi_runtime_testkit_python-0.3.1 → piphi_runtime_testkit_python-0.3.3}/tests/test_strict_contracts.py +81 -0
  8. {piphi_runtime_testkit_python-0.3.1 → piphi_runtime_testkit_python-0.3.3}/LICENSE +0 -0
  9. {piphi_runtime_testkit_python-0.3.1 → piphi_runtime_testkit_python-0.3.3}/README.md +0 -0
  10. {piphi_runtime_testkit_python-0.3.1 → piphi_runtime_testkit_python-0.3.3}/src/piphi_runtime_testkit_python/assertions.py +0 -0
  11. {piphi_runtime_testkit_python-0.3.1 → piphi_runtime_testkit_python-0.3.3}/src/piphi_runtime_testkit_python/builders.py +0 -0
  12. {piphi_runtime_testkit_python-0.3.1 → piphi_runtime_testkit_python-0.3.3}/src/piphi_runtime_testkit_python/camera.py +0 -0
  13. {piphi_runtime_testkit_python-0.3.1 → piphi_runtime_testkit_python-0.3.3}/src/piphi_runtime_testkit_python/harness.py +0 -0
  14. {piphi_runtime_testkit_python-0.3.1 → piphi_runtime_testkit_python-0.3.3}/src/piphi_runtime_testkit_python/mock_core.py +0 -0
  15. {piphi_runtime_testkit_python-0.3.1 → piphi_runtime_testkit_python-0.3.3}/src/piphi_runtime_testkit_python/pytest_plugin.py +0 -0
  16. {piphi_runtime_testkit_python-0.3.1 → piphi_runtime_testkit_python-0.3.3}/src/piphi_runtime_testkit_python/waits.py +0 -0
  17. {piphi_runtime_testkit_python-0.3.1 → piphi_runtime_testkit_python-0.3.3}/tests/__init__.py +0 -0
  18. {piphi_runtime_testkit_python-0.3.1 → piphi_runtime_testkit_python-0.3.3}/tests/test_assertions.py +0 -0
  19. {piphi_runtime_testkit_python-0.3.1 → piphi_runtime_testkit_python-0.3.3}/tests/test_builders.py +0 -0
  20. {piphi_runtime_testkit_python-0.3.1 → piphi_runtime_testkit_python-0.3.3}/tests/test_camera.py +0 -0
  21. {piphi_runtime_testkit_python-0.3.1 → piphi_runtime_testkit_python-0.3.3}/tests/test_command_contract.py +0 -0
  22. {piphi_runtime_testkit_python-0.3.1 → piphi_runtime_testkit_python-0.3.3}/tests/test_fastapi_runtime_example.py +0 -0
  23. {piphi_runtime_testkit_python-0.3.1 → piphi_runtime_testkit_python-0.3.3}/tests/test_mock_core.py +0 -0
  24. {piphi_runtime_testkit_python-0.3.1 → piphi_runtime_testkit_python-0.3.3}/tests/test_mock_core_sequences.py +0 -0
  25. {piphi_runtime_testkit_python-0.3.1 → piphi_runtime_testkit_python-0.3.3}/tests/test_pytest_plugin.py +0 -0
  26. {piphi_runtime_testkit_python-0.3.1 → piphi_runtime_testkit_python-0.3.3}/tests/test_runtime_config_contract.py +0 -0
  27. {piphi_runtime_testkit_python-0.3.1 → piphi_runtime_testkit_python-0.3.3}/tests/test_runtime_flow.py +0 -0
  28. {piphi_runtime_testkit_python-0.3.1 → piphi_runtime_testkit_python-0.3.3}/tests/test_runtime_harness.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: piphi-runtime-testkit-python
3
- Version: 0.3.1
3
+ Version: 0.3.3
4
4
  Summary: Pytest-first helpers for testing PiPhi runtime integrations
5
5
  Keywords: piphi,pytest,testing,runtime,iot
6
6
  Author-Email: KelvinSan <support@piphi.network>
@@ -1,6 +1,6 @@
1
1
  [project]
2
2
  name = "piphi-runtime-testkit-python"
3
- version = "0.3.1"
3
+ version = "0.3.3"
4
4
  description = "Pytest-first helpers for testing PiPhi runtime integrations"
5
5
  authors = [
6
6
  { name = "KelvinSan", email = "support@piphi.network" },
@@ -32,6 +32,8 @@ from .camera import (
32
32
  from .behavior_contract import (
33
33
  BehaviorTelemetryIssue,
34
34
  assert_behavior_conditions_match_telemetry,
35
+ assert_behavior_conditions_match_state,
36
+ find_behavior_state_issues,
35
37
  find_behavior_telemetry_issues,
36
38
  )
37
39
  from .mock_core import CapturedRequest, MockCoreServer, RUNTIME_CONFIG_FETCH_PATH
@@ -77,6 +79,7 @@ __all__ = [
77
79
  "assert_onboarding_declaration",
78
80
  "assert_camera_capability",
79
81
  "assert_behavior_conditions_match_telemetry",
82
+ "assert_behavior_conditions_match_state",
80
83
  "assert_event_sent",
81
84
  "assert_event_payload",
82
85
  "assert_never_event",
@@ -95,6 +98,7 @@ __all__ = [
95
98
  "build_core_config_row",
96
99
  "build_runtime_headers",
97
100
  "find_behavior_telemetry_issues",
101
+ "find_behavior_state_issues",
98
102
  "wait_for_event",
99
103
  "wait_for_request",
100
104
  "wait_for_telemetry",
@@ -1,4 +1,4 @@
1
- """Conformance checks between automation behaviors and emitted telemetry."""
1
+ """Conformance checks between automation behaviors and runtime observations."""
2
2
 
3
3
  from __future__ import annotations
4
4
 
@@ -9,7 +9,7 @@ from typing import Any
9
9
 
10
10
  @dataclass(frozen=True)
11
11
  class BehaviorTelemetryIssue:
12
- """One behavior condition that cannot be evaluated from runtime telemetry."""
12
+ """One behavior condition that cannot be evaluated from runtime samples."""
13
13
 
14
14
  device_id: str
15
15
  condition_id: str
@@ -55,6 +55,52 @@ def _metric_samples(
55
55
  return samples
56
56
 
57
57
 
58
+ def _runtime_state_samples(
59
+ state_samples: Iterable[Mapping[str, Any]] | Mapping[str, Any],
60
+ ) -> list[Mapping[str, Any]]:
61
+ raw_samples = (
62
+ [state_samples] if isinstance(state_samples, Mapping) else list(state_samples)
63
+ )
64
+ samples: list[Mapping[str, Any]] = []
65
+
66
+ def append_state(value: Any) -> None:
67
+ state = _as_mapping(value)
68
+ if not state:
69
+ return
70
+ nested_state = state.get("state")
71
+ if isinstance(nested_state, Mapping):
72
+ state = nested_state
73
+ samples.append(state)
74
+ components = state.get("components")
75
+ if isinstance(components, Mapping):
76
+ samples.extend(
77
+ component for component in components.values() if isinstance(component, Mapping)
78
+ )
79
+ else:
80
+ samples.extend(
81
+ component
82
+ for component in _as_list(components)
83
+ if isinstance(component, Mapping)
84
+ )
85
+
86
+ for raw_sample in raw_samples:
87
+ payload = _as_mapping(raw_sample)
88
+ entries = payload.get("entries")
89
+ if isinstance(entries, Mapping):
90
+ entry_values = entries.values()
91
+ elif isinstance(entries, (list, tuple)):
92
+ entry_values = entries
93
+ else:
94
+ entry_values = None
95
+ if entry_values is not None:
96
+ for entry in entry_values:
97
+ mapped_entry = _as_mapping(entry)
98
+ append_state(mapped_entry.get("latest_state", mapped_entry))
99
+ else:
100
+ append_state(payload.get("latest_state", payload))
101
+ return samples
102
+
103
+
58
104
  def _field_aliases(runtime: Mapping[str, Any]) -> tuple[str, ...]:
59
105
  raw_aliases = runtime.get("fieldAliases", runtime.get("field_aliases"))
60
106
  return tuple(
@@ -85,6 +131,7 @@ def _condition_issue(
85
131
  device_id: str,
86
132
  samples: list[Mapping[str, Any]],
87
133
  dynamic_condition_ids: set[str],
134
+ sample_kind: str = "telemetry",
88
135
  template_id: str | None = None,
89
136
  ) -> BehaviorTelemetryIssue | None:
90
137
  condition_id = str(condition.get("id") or condition.get("field") or "condition").strip()
@@ -134,7 +181,7 @@ def _condition_issue(
134
181
  condition_id=condition_id,
135
182
  field=field,
136
183
  aliases=aliases,
137
- reason="field was not emitted by any representative telemetry sample",
184
+ reason=f"field was not emitted by any representative {sample_kind} sample",
138
185
  template_id=template_id,
139
186
  )
140
187
 
@@ -169,20 +216,13 @@ def _condition_leaves(node: Any) -> Iterable[Mapping[str, Any]]:
169
216
  yield current
170
217
 
171
218
 
172
- def find_behavior_telemetry_issues(
219
+ def _find_behavior_sample_issues(
173
220
  behaviors: Mapping[str, Any],
174
- telemetry_samples: Iterable[Mapping[str, Any]] | Mapping[str, Any],
221
+ samples: list[Mapping[str, Any]],
175
222
  *,
176
223
  dynamic_condition_ids: Iterable[str] = (),
224
+ sample_kind: str,
177
225
  ) -> list[BehaviorTelemetryIssue]:
178
- """Return conditions that representative runtime telemetry cannot evaluate.
179
-
180
- ``telemetry_samples`` may contain raw metric mappings or Core telemetry
181
- payloads with a ``metrics`` object. Parameter-driven dynamic conditions must
182
- be named explicitly in ``dynamic_condition_ids`` and tested separately.
183
- """
184
-
185
- samples = _metric_samples(telemetry_samples)
186
226
  dynamic_ids = {
187
227
  str(condition_id or "").strip()
188
228
  for condition_id in dynamic_condition_ids
@@ -216,6 +256,7 @@ def find_behavior_telemetry_issues(
216
256
  device_id=device_id,
217
257
  samples=samples,
218
258
  dynamic_condition_ids=dynamic_ids,
259
+ sample_kind=sample_kind,
219
260
  )
220
261
  if issue:
221
262
  issues.append(issue)
@@ -245,6 +286,7 @@ def find_behavior_telemetry_issues(
245
286
  device_id=device_id,
246
287
  samples=samples,
247
288
  dynamic_condition_ids=dynamic_ids,
289
+ sample_kind=sample_kind,
248
290
  template_id=template_id,
249
291
  )
250
292
  if issue:
@@ -252,6 +294,47 @@ def find_behavior_telemetry_issues(
252
294
  return issues
253
295
 
254
296
 
297
+ def find_behavior_telemetry_issues(
298
+ behaviors: Mapping[str, Any],
299
+ telemetry_samples: Iterable[Mapping[str, Any]] | Mapping[str, Any],
300
+ *,
301
+ dynamic_condition_ids: Iterable[str] = (),
302
+ ) -> list[BehaviorTelemetryIssue]:
303
+ """Return conditions that representative runtime telemetry cannot evaluate.
304
+
305
+ ``telemetry_samples`` may contain raw metric mappings or Core telemetry
306
+ payloads with a ``metrics`` object. Parameter-driven dynamic conditions must
307
+ be named explicitly in ``dynamic_condition_ids`` and tested separately.
308
+ """
309
+
310
+ return _find_behavior_sample_issues(
311
+ behaviors,
312
+ _metric_samples(telemetry_samples),
313
+ dynamic_condition_ids=dynamic_condition_ids,
314
+ sample_kind="telemetry",
315
+ )
316
+
317
+
318
+ def find_behavior_state_issues(
319
+ behaviors: Mapping[str, Any],
320
+ state_samples: Iterable[Mapping[str, Any]] | Mapping[str, Any],
321
+ *,
322
+ dynamic_condition_ids: Iterable[str] = (),
323
+ ) -> list[BehaviorTelemetryIssue]:
324
+ """Return state-backed conditions absent from representative runtime state.
325
+
326
+ Samples may be individual state mappings or a runtime ``/state`` response
327
+ containing ``entries`` with ``latest_state`` and nested ``components``.
328
+ """
329
+
330
+ return _find_behavior_sample_issues(
331
+ behaviors,
332
+ _runtime_state_samples(state_samples),
333
+ dynamic_condition_ids=dynamic_condition_ids,
334
+ sample_kind="state",
335
+ )
336
+
337
+
255
338
  def assert_behavior_conditions_match_telemetry(
256
339
  behaviors: Mapping[str, Any],
257
340
  telemetry_samples: Iterable[Mapping[str, Any]] | Mapping[str, Any],
@@ -270,3 +353,23 @@ def assert_behavior_conditions_match_telemetry(
270
353
  raise AssertionError(
271
354
  "Behavior conditions do not match emitted telemetry:\n" + details
272
355
  )
356
+
357
+
358
+ def assert_behavior_conditions_match_state(
359
+ behaviors: Mapping[str, Any],
360
+ state_samples: Iterable[Mapping[str, Any]] | Mapping[str, Any],
361
+ *,
362
+ dynamic_condition_ids: Iterable[str] = (),
363
+ ) -> None:
364
+ """Fail when advertised state-backed conditions lack representative state."""
365
+
366
+ issues = find_behavior_state_issues(
367
+ behaviors,
368
+ state_samples,
369
+ dynamic_condition_ids=dynamic_condition_ids,
370
+ )
371
+ if issues:
372
+ details = "\n".join(f"- {issue.describe()}" for issue in issues)
373
+ raise AssertionError(
374
+ "Behavior conditions do not match representative runtime state:\n" + details
375
+ )
@@ -1,10 +1,13 @@
1
1
  from __future__ import annotations
2
2
 
3
- from collections.abc import Mapping, Set
3
+ from collections.abc import Iterable, Mapping, Set
4
4
  from typing import Any
5
5
 
6
6
  from .assertions import assert_entities_response
7
- from .behavior_contract import assert_behavior_conditions_match_telemetry
7
+ from .behavior_contract import (
8
+ assert_behavior_conditions_match_state,
9
+ assert_behavior_conditions_match_telemetry,
10
+ )
8
11
 
9
12
 
10
13
  def _mapping_keys(value: Any) -> set[str]:
@@ -18,10 +21,15 @@ def assert_cross_contract(
18
21
  behaviors: Mapping[str, Any],
19
22
  exercised_commands: Set[str],
20
23
  captured_event_types: Set[str],
21
- telemetry_samples: list[Mapping[str, Any]] | None = None,
24
+ telemetry_samples: Iterable[Mapping[str, Any]] | Mapping[str, Any] | None = None,
25
+ state_samples: Iterable[Mapping[str, Any]] | Mapping[str, Any] | None = None,
22
26
  dynamic_condition_ids: Set[str] = frozenset(),
23
27
  ) -> None:
24
- """Check manifest, runtime, behaviors, and observed execution as one contract."""
28
+ """Check manifest, runtime, behaviors, and observed execution as one contract.
29
+
30
+ State samples take precedence for state-backed behavior conditions. Existing
31
+ telemetry-only callers retain the legacy validation path.
32
+ """
25
33
 
26
34
  assert_entities_response(runtime_entities, strict=True)
27
35
  manifest_capabilities = _mapping_keys(manifest.get("capabilities"))
@@ -63,9 +71,16 @@ def assert_cross_contract(
63
71
  for device in behaviors.get("devices", [])
64
72
  )
65
73
  if has_conditions:
66
- if telemetry_samples is None:
67
- issues.append("behavior conditions were advertised but no telemetry samples were supplied")
68
- else:
74
+ if state_samples is not None:
75
+ try:
76
+ assert_behavior_conditions_match_state(
77
+ behaviors,
78
+ state_samples,
79
+ dynamic_condition_ids=dynamic_condition_ids,
80
+ )
81
+ except AssertionError as exc:
82
+ issues.append(str(exc))
83
+ elif telemetry_samples is not None:
69
84
  try:
70
85
  assert_behavior_conditions_match_telemetry(
71
86
  behaviors,
@@ -74,5 +89,9 @@ def assert_cross_contract(
74
89
  )
75
90
  except AssertionError as exc:
76
91
  issues.append(str(exc))
92
+ else:
93
+ issues.append(
94
+ "behavior conditions were advertised but no state or telemetry samples were supplied"
95
+ )
77
96
  if issues:
78
97
  raise AssertionError("Cross-contract conformance failed:\n- " + "\n- ".join(issues))
@@ -3,6 +3,7 @@ import pytest
3
3
  from piphi_runtime_testkit_python import (
4
4
  CapturedRequest,
5
5
  MockCoreServer,
6
+ assert_behavior_conditions_match_state,
6
7
  assert_behavior_conditions_match_telemetry,
7
8
  find_behavior_telemetry_issues,
8
9
  )
@@ -61,6 +62,27 @@ def test_behavior_conditions_accept_fields_aliases_types_and_templates() -> None
61
62
  )
62
63
 
63
64
 
65
+ def test_behavior_conditions_accept_nested_runtime_state_components() -> None:
66
+ assert_behavior_conditions_match_state(
67
+ _behaviors(),
68
+ {
69
+ "entries": [{
70
+ "config_id": "shelly-kitchen",
71
+ "latest_state": {
72
+ "connected": True,
73
+ "components": [{
74
+ "key": "switch:0",
75
+ "type": "switch",
76
+ "channel": 0,
77
+ "power_state": True,
78
+ "brightness": 42,
79
+ }],
80
+ },
81
+ }],
82
+ },
83
+ )
84
+
85
+
64
86
  def test_behavior_conditions_report_missing_field_and_type_mismatch() -> None:
65
87
  issues = find_behavior_telemetry_issues(
66
88
  _behaviors(),
@@ -138,3 +138,84 @@ def test_cross_contract_requires_every_behavior_action_and_trigger_to_be_exercis
138
138
  exercised_commands=set(),
139
139
  captured_event_types={"runtime.config.applied"},
140
140
  )
141
+
142
+
143
+ def test_cross_contract_validates_shelly_condition_from_keyed_runtime_state_entries() -> None:
144
+ capabilities = {"on": {"kind": "sensor", "unit": "bool"}}
145
+ entities = {
146
+ "entities": [{
147
+ "id": "shelly-aabbccddeeff:switch:0",
148
+ "name": "Switch 0",
149
+ "config_id": "shelly-test",
150
+ "device_id": "shelly-aabbccddeeff",
151
+ "capabilities": ["on"],
152
+ "available_commands": [],
153
+ }],
154
+ "capabilities": capabilities,
155
+ "commands": {},
156
+ }
157
+ behaviors = {"devices": [{
158
+ "id": "shelly_device",
159
+ "capabilities": ["on"],
160
+ "conditions": [{
161
+ "id": "switch_is_on",
162
+ "type": "boolean",
163
+ "runtime": {"source": "state", "field": "on"},
164
+ }],
165
+ }]}
166
+ state_response = {
167
+ "entries": {"shelly-test": {
168
+ "config_id": "shelly-test",
169
+ "device_id": "shelly-aabbccddeeff",
170
+ "latest_state": {
171
+ "connected": True,
172
+ "components": [{
173
+ "key": "switch:0",
174
+ "type": "switch",
175
+ "channel": 0,
176
+ "on": False,
177
+ }],
178
+ },
179
+ }},
180
+ }
181
+
182
+ assert_cross_contract(
183
+ manifest={"capabilities": capabilities, "commands": {}},
184
+ runtime_entities=entities,
185
+ behaviors=behaviors,
186
+ exercised_commands=set(),
187
+ captured_event_types=set(),
188
+ telemetry_samples={"metrics": {"switch_0_power_w": 4.2}},
189
+ state_samples=state_response,
190
+ )
191
+
192
+ with pytest.raises(AssertionError, match="representative state sample"):
193
+ assert_cross_contract(
194
+ manifest={"capabilities": capabilities, "commands": {}},
195
+ runtime_entities=entities,
196
+ behaviors=behaviors,
197
+ exercised_commands=set(),
198
+ captured_event_types=set(),
199
+ state_samples={"entries": [{"latest_state": {"components": []}}]},
200
+ )
201
+
202
+
203
+ def test_cross_contract_retains_telemetry_sample_compatibility() -> None:
204
+ behaviors = {"devices": [{
205
+ "id": "device",
206
+ "capabilities": ["connected"],
207
+ "conditions": [{
208
+ "id": "is_connected",
209
+ "type": "boolean",
210
+ "runtime": {"source": "state", "field": "connected"},
211
+ }],
212
+ }]}
213
+
214
+ assert_cross_contract(
215
+ manifest={"capabilities": CAPABILITIES, "commands": COMMANDS},
216
+ runtime_entities=ENTITIES,
217
+ behaviors=behaviors,
218
+ exercised_commands=set(),
219
+ captured_event_types=set(),
220
+ telemetry_samples={"metrics": {"connected": True}},
221
+ )