From 22d110b8c8efce9c4a98d2bdc1ba266da1ddc8d1 Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Fri, 4 Sep 2026 07:08:03 +0000 Subject: [PATCH] tests: add tests for WatchSession.to_report Adds unit tests to verify the serialization and metadata inclusion logic within the `to_report` method of `WatchSession`. Coverage was missing for this crucial reporting method. The new tests verify it generates valid reports. Co-authored-by: Horizontal42 <75383519+Horizontal42@users.noreply.github.com> --- tests/test_watch.py | 51 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) diff --git a/tests/test_watch.py b/tests/test_watch.py index 16744a7..dfd6369 100644 --- a/tests/test_watch.py +++ b/tests/test_watch.py @@ -2,6 +2,7 @@ from __future__ import annotations import json from pathlib import Path +from unittest.mock import patch import pytest @@ -118,6 +119,56 @@ def test_session_filename_marks_it_as_a_watch_artifact(): assert WatchSession(started_at="2026-08-08T19:12:00Z").filename().startswith("watch_unknown_") +@patch("netsleuth.watch.utc_now_iso") +def test_watch_session_to_report_includes_all_metadata(mock_utc_now_iso): + mock_utc_now_iso.return_value = "2026-08-08T19:20:00Z" + session = WatchSession( + started_at="2026-08-08T19:12:00Z", + asn="AS64500", + interval_seconds=60, + speedtest_every_n_cycles=10, + interface="eth0", + bind_ipv4="192.168.1.100", + ) + session.alerts_fired = 2 + + report = session.to_report() + + assert report["schema_version"] == WATCH_SCHEMA_VERSION + assert report["kind"] == "watch" + assert report["meta"] == { + "started_at": "2026-08-08T19:12:00Z", + "finished_at": "2026-08-08T19:20:00Z", + "asn": "AS64500", + "interval_seconds": 60, + "speedtest_every_n_cycles": 10, + "cycle_count": 0, + "interface": "eth0", + "bind_ipv4": "192.168.1.100", + "alerts_fired": 2, + } + assert report["cycles"] == [] + + +@patch("netsleuth.watch.utc_now_iso") +def test_watch_session_to_report_serializes_cycles_to_jsonable(mock_utc_now_iso): + mock_utc_now_iso.return_value = "2026-08-08T19:20:00Z" + session = WatchSession(started_at="2026-08-08T19:12:00Z") + + cycle_data = summarize_cycle(1, "2026-08-08T19:15:00Z", [ping("cloudflare-dns", 12.4)], None) + session.add(cycle_data) + + report = session.to_report() + + assert report["meta"]["cycle_count"] == 1 + assert len(report["cycles"]) == 1 + + first_cycle = report["cycles"][0] + assert first_cycle["cycle"] == 1 + assert first_cycle["at"] == "2026-08-08T19:15:00Z" + assert first_cycle["status"] == "ok" + + def test_a_session_writes_exactly_one_artifact_holding_every_cycle(tmp_path: Path): session = WatchSession( started_at="2026-08-08T19:12:00Z", asn="AS64500", interval_seconds=60, speedtest_every_n_cycles=10