meshai/work/tests/test_clock_seam.py

38 lines
1.1 KiB
Python
Raw Normal View History

refactor(phase0): source-agnostic formatter/gating scaffold + harness (inert) (#28) Foundation for making all hazard formatting+gating source-agnostic. ZERO behavior change — the formatter/decider registries are empty (get_formatter/ get_decider return None → existing precomposed/Mode-B path preserved), and the shadow comparator is off unless MESHAI_SHADOW_CATEGORIES is set. - notifications/formatters/ (registry+dispatch with family fallback), gating/ (GateResult + deferred-commit contract), both empty registries. - notifications/clock.py determinism seam; route wfigs/quake/nws gating time reads through it (identical values) so goldens can freeze time. - formatters/_budget.py = copy of central/budget.py; central/budget.py is now a re-export shim (import-smoke test guards it). - compose_mesh_message consults the registry first (verbatim, no Mode-B re-cap), falls back to legacy; _resolve_budget injects per-category budget. - notifications/shadow.py + two DRY-RUN hooks (consumer._normalize, dispatcher render): compute the new result and diff-log SHADOW_MISMATCH JSONL, but NEVER commit/emit/write tables and always broadcast the OLD result. Inert by default. - tests/harness (pinned_time/pinned_tz, byte-golden, gate-sequence) + scripts/capture_fixtures.py (ephemeral read-only NATS capture); tzdata pinned. Tests: +60 (18 scaffold + 42 harness/shadow); 0 new failures (34 baseline). Co-authored-by: Matt Johnson <mj@k7zvx.com> Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-07-04 12:55:48 -06:00
"""Phase-0: verify the clock seam is monkeypatchable and handlers use it.
Tests:
1. clock.now() returns a float close to time.time() at runtime.
chore: excise the dead Central NATS consumer path (-11,328 LOC) (#144) * chore: excise the dead Central NATS consumer path Central was retired and its database dropped 2026-07-15; its NATS broker no longer exists. Verified against the live CT108 deployment: all 12 adapters run feed_source=native, zero on central, and central.enabled is False (default, never overridden). The consumer and its handlers were unreachable. Removed: - central/consumer.py and 6 dead handlers (nws, quake, swpc, nwis, avy, incident) -- their handle_* entrypoints were reachable only from the consumer's dispatch - the Central wiring in main.py (init, guarded start, retry loop, stop path) - the dead config surface: CentralConsumerConfig, EnvironmentalConfig.central, adapter_config ("central","severity_thresholds") and its display block - the nats-py dependency (consumer.py was its only importer) - 19 test files that exercised only the dead path KEPT -- these live under central/ but are imported directly by native adapters, and deleting them would break production: - wfigs_handler.py: firms_handler._handle_pass_boundary() calls its _render() on the live FIRMS growth-fire path (env/firms.py -> ingest_hotspot_pixel) - firms_handler, satpass_handler, tle_handler: split files whose handle_* entrypoints are dead but whose engines are live. Left intact; splitting them is separate work. - pass_predictor, budget, idaho_gauge_sites: fully live. The usgs_quake keys global_mag_floor / regional_mag_floor / regional_centroid / regional_radius_mi / broadcast_pager_alerts are NOT removed despite comments labelling them "CENTRAL-PATH ONLY" -- notifications/gating/quake.py reads them unconditionally in the native path. Those comments are corrected separately. Test-count note: the suite drops ~425 tests. Most were migration PARITY tests whose sole purpose was proving the native rewrite byte-matched the Central handler (golden byte-parity, cross-source identity, gate-sequence replay). With the handler deleted there is nothing left to compare against, so they cannot exist. Native-only tests were kept and reworked where a test reached for a central symbol incidentally. This is a real coverage loss, accepted deliberately: the parity harness proved the refactor faithful, and git history preserves the originals. Suite: 1984 passed, 6 failed -- the same 6 pre-existing failures as main (stale SCHEMA_VERSION x3, expired TLE fixtures x2, one order-dependent), all being fixed on fix/green-test-suite. No new failures. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> * test(nws): restore native-only golden coverage for the wire formatter Commit ca751fb5 deleted the Central nws_handler parity harness along with the handler itself, which took the ONLY tests that pinned formatters.nws .format()'s literal wire output. Gate-sequence and schema-conformance tests already survived natively; the formatter's actual rendered text did not have any native-only regression net. Add TestFormatterGolden to test_nws_refactor.py: 3 real-fixture cases plus 6 hand-built pathological cases mined from the deleted test_nws_handler.py (SVR path-sampling, the "no dangling separator" regression, TOR on-ground vs radar-indicated, FFW flood-cause detection). Every literal was verified by temporarily restoring the pre-excision central.nws_handler._render() from git history (ca751fb5^) in a throwaway, uncommitted script, confirming byte-identical output against the current native format() for all 37 real fixtures (nws/ + nws_last/) and all 9 pathological cases, then pinning the confirmed-matching string as the literal -- not a blind snapshot of current behavior. quake/swpc/avalanche/hydro/incident/fire were checked and already carry equivalent native-only golden coverage (added directly in ca751fb5), so no changes were needed there. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> --------- Co-authored-by: Matt Johnson <mj@k7zvx.com> Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-07-17 14:03:40 -06:00
2. Monkeypatching clock.now propagates into wfigs_handler._now.
refactor(phase0): source-agnostic formatter/gating scaffold + harness (inert) (#28) Foundation for making all hazard formatting+gating source-agnostic. ZERO behavior change — the formatter/decider registries are empty (get_formatter/ get_decider return None → existing precomposed/Mode-B path preserved), and the shadow comparator is off unless MESHAI_SHADOW_CATEGORIES is set. - notifications/formatters/ (registry+dispatch with family fallback), gating/ (GateResult + deferred-commit contract), both empty registries. - notifications/clock.py determinism seam; route wfigs/quake/nws gating time reads through it (identical values) so goldens can freeze time. - formatters/_budget.py = copy of central/budget.py; central/budget.py is now a re-export shim (import-smoke test guards it). - compose_mesh_message consults the registry first (verbatim, no Mode-B re-cap), falls back to legacy; _resolve_budget injects per-category budget. - notifications/shadow.py + two DRY-RUN hooks (consumer._normalize, dispatcher render): compute the new result and diff-log SHADOW_MISMATCH JSONL, but NEVER commit/emit/write tables and always broadcast the OLD result. Inert by default. - tests/harness (pinned_time/pinned_tz, byte-golden, gate-sequence) + scripts/capture_fixtures.py (ephemeral read-only NATS capture); tzdata pinned. Tests: +60 (18 scaffold + 42 harness/shadow); 0 new failures (34 baseline). Co-authored-by: Matt Johnson <mj@k7zvx.com> Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-07-04 12:55:48 -06:00
"""
import time
import pytest
import meshai.notifications.clock as clock_mod
import meshai.central.wfigs_handler as wfigs_handler
_FROZEN_TS = 1_700_000_000.0
def test_clock_now_returns_float_close_to_time():
before = time.time()
result = clock_mod.now()
after = time.time()
assert isinstance(result, float), "clock.now() must return float"
assert before <= result <= after + 0.1, "clock.now() must be current time"
def test_clock_now_is_monkeypatchable(monkeypatch):
monkeypatch.setattr(clock_mod, "now", lambda: _FROZEN_TS)
assert clock_mod.now() == _FROZEN_TS
def test_wfigs_handler_now_uses_clock_seam(monkeypatch):
"""wfigs_handler._now() must reflect a monkeypatched clock.now."""
monkeypatch.setattr(clock_mod, "now", lambda: _FROZEN_TS)
result = wfigs_handler._now()
assert result == int(_FROZEN_TS), (
f"wfigs_handler._now() returned {result!r}, expected {int(_FROZEN_TS)}"
)