GLOBALGRID2050 / EXPLORE CODE

Part of: Explore Code / Automation and CI / lightning / ci

Test Backfill and 1 more file

A test in the tests folder of Lightning that checks Collect and History. It backfills tests (`BackfillTests`), sets up (`setUp`) and backfills plan tests (`BackfillPlanTests`).

445 lines of Python from lightning, in 2 files. Topic: Automation and CI. Section 1821 of 2044.

Cut 2026-12-06T03:07:51+00:00. A copy for reading; the repository at the stated commit is the source.

In the State lines below, domain stage, users and canonical status are rule-based first approximations from code and path evidence (rules explore-agent-rules-v1), not observed facts: the rules.

In this section

  1. CI Verify Data automation · lightning / ci/verify_data.py lines 1–120
  2. Test Backfill test · lightning / tests/test_backfill.py lines 1–325

CI Verify Data

A Python command-line tool in Lightning that reads UK lightning, using SHA-256. It checks feed (`check_feed`) and checks status (`check_status`).

automationlightning / ci/verify_data.py · lines 1–120 of 120 · Entry point

What it does
It loads UK lightning. Defines check_feed, check_status, staged_parts, main, run. Uses libraries __future__, argparse, json, subprocess, sys. Side effects: reads or writes files; starts processes.
Inputs & outputs
In: GeoJSON file uk_lightning.geojson; JSON file status.json; JSON file index.json; command-line arguments.
Downstream impact
Used directly by 6 files: Backfill Lightning Workflow (lightning); Backfill Parallel Lightning Workflow (lightning); Update Lightning Workflow (lightning); Verify Lightning Data Workflow (lightning); and 2 more. Reached from 2 other entry points, including Test Lightning Workflow (lightning); Test Concurrent Writers (lightning).
What it achieves
Verify the committed data before it is pushed (quick) or after it landed (deep). It accepts `--data-dir`, `--staged` and `--deep`.
Why it matters
Run by 5 workflows and scripts, including Backfill Lightning Workflow, Backfill Parallel Lightning Workflow and Update Lightning Workflow. Checked by Test CI Verify.
Application
lightning · ci · Python CLI
Uses
Grid (import), History (import), CI Push with Replay (script call), __future__, argparse, json, subprocess, sys, pathlib
Used by
run by Backfill Lightning Workflow (workflow run), run by Backfill Parallel Lightning Workflow (workflow run), run by Update Lightning Workflow (workflow run), run by Verify Lightning Data Workflow (workflow run), run by CI Push with Replay (script call), run by Test CI Verify (script call)
Produces
check_feed, check_status, staged_parts, main
Defines here
check_feed, check_status, staged_parts, main, run
Cannot conclude
Whether this code runs, is used by a live app today, or is correct. This is a static reading.
Evidence
lightning@2882145e28a0412de3a5bcfa40a12d7cd894ba90 ci/verify_data.py:1-120 · blob e6564ce389abde19a1308ae13d2205c77c2b24da · GitHub at these lines
State
achieves: comment · context: readme_derived · stage: ingest · effects: filesystem, process · domain stage: unknown · users: none · canonical: unknown
#!/usr/bin/env python3
"""Verify the committed data before it is pushed (quick) or after it landed (deep).

    python ci/verify_data.py [--data-dir data] [--staged] [--deep]

Checks, each only when its file exists:

- feed    ``uk_lightning.geojson``: JSON FeatureCollection, a features list of
          Point features, at most ``collect.MAX_RENDER_POINTS``, metadata object;
- status  ``status.json``: a JSON object of schema ``uk-lightning-status.*``;
- archive ``history/index.json``: ``history.verify_archive`` (index rows vs shard
          bytes, sha256 and counts; integrity block) - always blocking;
- grid    ``history/grid/``: ``grid.verify_grids``. Quick mode WARNS only,
          because the archive design lets a grid failure never block the
          archive (history.py ingest); ``--deep`` recounts every day from its
          shards and FAILS on any difference.

``--staged`` limits the checks to the parts with staged changes (``git diff
--cached``), which is how ``ci/push-with-replay.sh`` runs it before each
commit (PUSH_VERIFY): a feed-only commit never waits on the archive, and a
heartbeat-only commit is never blocked by an unrelated archive problem.

Exit 0 when every blocking check passes, 1 otherwise (``::error::`` lines).
"""
from __future__ import annotations

import argparse
import json
import subprocess
import sys
from pathlib import Path

ROOT = Path(__file__).resolve().parents[1]
sys.path.insert(0, str(ROOT))

import grid  # noqa: E402
import history  # noqa: E402

MAX_RENDER_POINTS = 40000  # collect.MAX_RENDER_POINTS (collect.py is not imported: no eumdac needed)


def check_feed(path: Path) -> str:
    obj = json.loads(path.read_text(encoding="utf-8"))
    if not isinstance(obj, dict) or obj.get("type") != "FeatureCollection":
        raise ValueError(f"{path}: not a GeoJSON FeatureCollection")
    features = obj.get("features")
    if not isinstance(features, list):
        raise ValueError(f"{path}: features is not a list")
    if len(features) > MAX_RENDER_POINTS:
        raise ValueError(f"{path}: {len(features)} features exceed the {MAX_RENDER_POINTS} display cap")
    if not isinstance(obj.get("metadata"), dict):
        raise ValueError(f"{path}: metadata object missing")
    for i, feature in enumerate(features):
        geometry = (feature or {}).get("geometry") or {}
        coords = geometry.get("coordinates")
        if geometry.get("type") != "Point" or not isinstance(coords, list) or len(coords) != 2:
            raise ValueError(f"{path}: feature {i} is not a Point")
    return f"feed ok ({len(features)} features)"


def check_status(path: Path) -> str:
    obj = json.loads(path.read_text(encoding="utf-8"))
    if not isinstance(obj, dict) or not str(obj.get("schema", "")).startswith("uk-lightning-status."):
        raise ValueError(f"{path}: not a uk-lightning-status object")
    return f"status ok ({obj['schema']})"


def staged_parts(data_dir: Path) -> set[str]:
    out = subprocess.run(["git", "diff", "--cached", "--name-only", "--", str(data_dir)],
                         check=True, capture_output=True, text=True).stdout.split()
    prefix = data_dir.as_posix().rstrip("/") + "/"
    parts = set()
    for name in out:
        rel = name[len(prefix):] if name.startswith(prefix) else name
        if rel == "uk_lightning.geojson":
            parts.add("feed")
        elif rel == "status.json":
            parts.add("status")
        elif rel.startswith("history/"):
            parts.add("archive")
    return parts


def main(argv=None) -> int:
    parser = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter)
    parser.add_argument("--data-dir", type=Path, default=Path("data"))
    parser.add_argument("--staged", action="store_true", help="Only the parts with staged git changes")
    parser.add_argument("--deep", action="store_true", help="Recount grids from shards; a grid difference fails")
    args = parser.parse_args(argv)
    data = args.data_dir
    parts = staged_parts(data) if args.staged else {"feed", "status", "archive"}
    archive_dir = data / "history"
    failed = False

    def run(label, check, *, blocking=True):
        nonlocal failed
        try:
            print(f"verify-data: {check()}")
        except Exception as error:  # noqa: BLE001 - reported, then exit 1
            if blocking:
                failed = True
                print(f"::error::verify-data {label}: {type(error).__name__}: {error}")
            else:
                print(f"::warning::verify-data {label} (not blocking): {type(error).__name__}: {error}")

    if "feed" in parts and (data / "uk_lightning.geojson").exists():
        run("feed", lambda: check_feed(data / "uk_lightning.geojson"))
    if "status" in parts and (data / "status.json").exists():
        run("status", lambda: check_status(data / "status.json"))
    if "archive" in parts and (archive_dir / "index.json").exists():
        run("archive", lambda: "archive ok " + json.dumps(history.verify_archive(archive_dir)))
        run("grid", lambda: f"grid ok ({'deep' if args.deep else 'quick'}) "
                            + json.dumps(grid.verify_grids(archive_dir, deep=args.deep)), blocking=args.deep)
    if not parts:
        print("verify-data: no data changes staged; nothing to verify")
    return 1 if failed else 0


if __name__ == "__main__":
    raise SystemExit(main())

Test Backfill

A test in the tests folder of Lightning that checks Collect and History. It backfills tests (`BackfillTests`), sets up (`setUp`) and backfills plan tests (`BackfillPlanTests`).

testlightning / tests/test_backfill.py · lines 1–325 of 325 · Entry point · pattern test_plan_lists_never_searched_dates_newest_first (shared by 2 files)

What it does
It tests its inputs. Defines 42 functions or classes, including slot_name, flash, FakeDownloader, __init__, __call__. Uses libraries json, sys, tempfile, unittest, datetime. Side effects: reads or writes files.
Inputs & outputs
In: JSON file index.json; JSON file rb.json; JSON file result.json; JSON file status.json. Out: JSON file status.json; JSON file index.json.
Downstream impact
Used directly by 5 files: Backfill Lightning Workflow (lightning); Backfill Parallel Lightning Workflow (lightning); Test Lightning Workflow (lightning); Update Lightning Workflow (lightning); and 1 more.
What it achieves
Backfill of never-searched UTC dates from a fresh, whole-day EUMETSAT search.
Why it matters
Run by Backfill Lightning Workflow, Backfill Parallel Lightning Workflow and Update Lightning Workflow. Checked by 2 tests, including Test Lightning Workflow.
Application
lightning · tests · test
Uses
Collect (import), History (import), Status (import), json, sys, tempfile, unittest, datetime, pathlib, contextlib, io
Used by
run by Backfill Lightning Workflow (workflow run), run by Backfill Parallel Lightning Workflow (workflow run), run by Test Lightning Workflow (workflow run), run by Update Lightning Workflow (workflow run), imported by Test Grid (import)
Produces
slot_name, flash, FakeDownloader, rows, BackfillTests, BackfillPlanTests, BackfillStatusTests
Defines here
slot_name, flash, FakeDownloader, __init__, __call__, rows, BackfillTests, setUp, tearDown, index, snapshot, rebuild, backfill, test_fixture_marks_the_target_date_never_searched, test_backfill_ingests_a_rebuild_with_its_real_coverage_and_count, test_backfill_keeps_the_hourly_heartbeat_fields_of_the_index, test_a_fully_searched_day_without_flashes_is_zero_with_full_coverage, test_heavy_day_respects_the_byte_caps_and_splits_into_hourly_parts, assert_refused, test_shard_too_large_changes_nothing_and_the_day_stays_unknown, test_refuses_a_date_that_was_already_searched, test_refuses_a_shard_lost_date_and_points_to_recover, test_refuses_a_record_for_another_date, test_refuses_a_record_without_parsed_slots, test_refuses_a_failed_record, test_refuses_a_search_made_before_the_date_ended, test_refuses_a_flash_outside_the_examined_slots, test_refuses_without_an_index, test_eumetsat_error_or_rate_limit_writes_no_record, test_cli_backfill_writes_a_result_and_runs_verify
Cannot conclude
Whether this code runs, is used by a live app today, or is correct. This is a static reading.
Evidence
lightning@2882145e28a0412de3a5bcfa40a12d7cd894ba90 tests/test_backfill.py:1-325 · blob 2e2d639a5b8aea053031f7c1f8a158309c2208dd · GitHub at these lines
State
achieves: comment · context: readme_derived · stage: output · effects: filesystem · domain stage: unknown · users: none · canonical: unknown
"""Backfill of never-searched UTC dates from a fresh, whole-day EUMETSAT search.

``collect.py --rebuild-day D`` (here with a fake downloader, never the network)
writes a run record; ``history.py backfill --day D`` ingests it only for a date
the index marks never searched (coverage none / count null, not shard_lost), with
the record's real examined slots as coverage: a gap stays unknown, a zero is
only written for searched time, and any refusal or failure changes nothing.

Synthetic features exist ONLY inside temporary directories created by these
tests (ids start with "TEST-"); nothing synthetic is ever published.
"""
import json
import sys
import tempfile
import unittest
from datetime import date, datetime, timedelta, timezone
from pathlib import Path

sys.path.insert(0, str(Path(__file__).resolve().parents[1]))
import collect
import history
import status as status_module
from collect import build_feature
from history import archive_observations, day_path, successful_scan, verify_archive

NOW = datetime(2026, 10, 27, 6, 0, tzinfo=timezone.utc)
TARGET = "2026-10-20"          # never searched in the fixture archive
SEARCHED = "2026-10-26"        # searched by an "hourly" run in the fixture archive
DAY0 = datetime(2026, 10, 20, tzinfo=timezone.utc)


def slot_name(start: datetime) -> str:
    end = start + timedelta(minutes=10)
    return ("W_XX-EUMETSAT-Darmstadt,IMG+SAT,MTI1+LI-2-LFL--FD--x-x--ARC-x_C_EUMT_"
            f"{end:%Y%m%d%H%M%S}_L2PF_OPE_{start:%Y%m%d%H%M%S}_{end:%Y%m%d%H%M%S}_N__O_0001_0000")


def flash(moment: datetime, tag: str, index: int = 0) -> dict:
    return build_feature(-2.12, 51.11, moment, "TEST-" + tag, index)


class FakeDownloader:
    """Stands in for collect.download: 10-minute slots of [start, end) minus ``skip``."""

    def __init__(self, flashes=(), skip_slots=(), error=None):
        self.flashes, self.skip, self.error, self.calls = list(flashes), set(skip_slots), error, []

    def __call__(self, start, end):
        self.calls.append((start, end))
        if self.error:
            raise self.error
        names, cursor, i = [], start, 0
        while cursor < end:
            if i not in self.skip:
                names.append(slot_name(cursor))
            cursor += timedelta(minutes=10)
            i += 1
        return [f for f in self.flashes if start <= collect.parse_utc(f["properties"]["time_utc"]) < end], len(names), names


def rows(index):
    return {r["date"]: r for r in index["days"]}


class BackfillTests(unittest.TestCase):
    def setUp(self):
        self.temp = tempfile.TemporaryDirectory()
        self.base = Path(self.temp.name)
        self.root = self.base / "history"
        hourly = datetime(2026, 10, 26, tzinfo=timezone.utc)
        archive_observations(
            self.root, [flash(hourly + timedelta(hours=3), "hourly")], now=NOW - timedelta(hours=1),
            scanned_windows=[successful_scan((hourly + timedelta(hours=1)).isoformat(),
                                             (hourly + timedelta(hours=7)).isoformat(), 36)],
            lookback_hours=6.0)
        self.before = json.loads((self.root / "index.json").read_text())
        self.record = self.base / "rb.json"

    def tearDown(self):
        self.temp.cleanup()

    def index(self):
        return json.loads((self.root / "index.json").read_text())

    def snapshot(self):
        return {p.relative_to(self.base).as_posix(): p.read_bytes() for p in sorted(self.base.rglob("*")) if p.is_file()
                and p != self.record}

    def rebuild(self, day=TARGET, **fake):
        fake_dl = FakeDownloader(**fake)
        collect.rebuild_day_record(day, self.record, now=NOW, downloader=fake_dl)
        return fake_dl

    def backfill(self, day=TARGET, **kwargs):
        return history.backfill_day(self.root, day, now=NOW, rebuild_record=self.record, **kwargs)

    # --- the fixture: TARGET is never searched, SEARCHED is ---------------------
    def test_fixture_marks_the_target_date_never_searched(self):
        r = rows(self.before)
        self.assertEqual((r[TARGET]["coverage"], r[TARGET]["count"]), ("none", None))
        self.assertEqual(r[SEARCHED]["coverage"], "partial")

    # --- happy path: real coverage, real count, gaps stay unknown ---------------
    def test_backfill_ingests_a_rebuild_with_its_real_coverage_and_count(self):
        flashes = [flash(DAY0 + timedelta(hours=h, minutes=3), f"bf{h}") for h in (2, 9, 17)]
        fake = self.rebuild(flashes=flashes, skip_slots={30, 31})  # 05:00-05:20 missing at EUMETSAT
        self.assertEqual(fake.calls, [(DAY0, DAY0 + timedelta(days=1))])
        result = self.backfill()
        row = rows(self.index())[TARGET]
        self.assertEqual(row["coverage"], "partial")
        self.assertEqual(row["scanned_seconds"], 86400 - 1200)
        self.assertEqual(row["count"], 3)
        self.assertEqual(row["path"], day_path(date(2026, 10, 20)))
        self.assertEqual(verify_archive(self.root)["integrity"], "ok")
        self.assertEqual(result["row"]["count"], 3)
        self.assertEqual(result["unscanned_ranges"],
                         [{"start_utc": "2026-10-20T05:00:00.000Z", "end_utc": "2026-10-20T05:20:00.000Z"}])

    def test_backfill_keeps_the_hourly_heartbeat_fields_of_the_index(self):
        self.rebuild(flashes=[flash(DAY0 + timedelta(hours=4), "x")])
        self.backfill()
        after = self.index()
        for key in ("last_successful_scan_end_utc", "last_run_lookback_hours", "archive_first_enabled_utc"):
            self.assertEqual(after[key], self.before[key], key)
        self.assertEqual(after["coverage_start_utc"], "2026-10-20T00:00:00.000Z")
        self.assertEqual(rows(after)[SEARCHED], {**rows(self.before)[SEARCHED], "sealed": rows(after)[SEARCHED]["sealed"]})

    def test_a_fully_searched_day_without_flashes_is_zero_with_full_coverage(self):
        self.rebuild(flashes=[])
        self.backfill()
        row = rows(self.index())[TARGET]
        self.assertEqual((row["coverage"], row["scanned_seconds"], row["count"], row["path"]), ("full", 86400, 0, None))

    def test_heavy_day_respects_the_byte_caps_and_splits_into_hourly_parts(self):
        flashes = [flash(DAY0 + timedelta(hours=h, seconds=s), f"heavy{h}", s) for h in (1, 2) for s in range(20)]
        self.rebuild(flashes=flashes)
        self.backfill(target_shard_bytes=4000, max_shard_bytes=25_000)
        row = rows(self.index())[TARGET]
        self.assertIsNone(row["path"])
        self.assertEqual([p["hour"] for p in row["parts"]], ["01", "02"])
        self.assertEqual(row["count"], 40)
        self.assertEqual(verify_archive(self.root)["shards_checked"], 2)

    # --- refusals change nothing -------------------------------------------------
    def assert_refused(self, message, **kwargs):
        before = self.snapshot()
        with self.assertRaisesRegex(ValueError, message):
            self.backfill(**kwargs)
        self.assertEqual(self.snapshot(), before, "a refused backfill must not change any file")
        self.assertEqual(sorted(p.name for p in self.base.iterdir()), sorted(["history", "rb.json"]))

    def test_shard_too_large_changes_nothing_and_the_day_stays_unknown(self):
        self.rebuild(flashes=[flash(DAY0 + timedelta(hours=1, seconds=s), "big", s) for s in range(20)])
        self.assert_refused("max_shard_bytes", max_shard_bytes=3000, target_shard_bytes=2000)
        self.assertIsNone(rows(self.index())[TARGET]["count"])

    def test_refuses_a_date_that_was_already_searched(self):
        self.rebuild(day=SEARCHED)
        before = self.snapshot()
        with self.assertRaisesRegex(ValueError, "already searched"):
            history.backfill_day(self.root, SEARCHED, now=NOW, rebuild_record=self.record)
        self.assertEqual(self.snapshot(), before)

    def test_refuses_a_shard_lost_date_and_points_to_recover(self):
        lost_day = datetime(2026, 10, 26, tzinfo=timezone.utc)
        (self.root / day_path(lost_day.date())).unlink()
        archive_observations(self.root, [], now=NOW - timedelta(minutes=30))
        self.rebuild(day=SEARCHED)
        with self.assertRaisesRegex(ValueError, "shard_lost.*recover"):
            history.backfill_day(self.root, SEARCHED, now=NOW, rebuild_record=self.record)

    def test_refuses_a_record_for_another_date(self):
        self.rebuild(day="2026-10-19")
        self.assert_refused("2026-10-19")

    def test_refuses_a_record_without_parsed_slots(self):
        self.rebuild()
        rec = json.loads(self.record.read_text())
        rec["slots_parsed"] = False
        self.record.write_text(json.dumps(rec))
        self.assert_refused("slots_parsed")

    def test_refuses_a_failed_record(self):
        self.rebuild()
        rec = json.loads(self.record.read_text())
        rec.update(outcome="failure", error="HTTPError: 429 Too Many Requests")
        self.record.write_text(json.dumps(rec))
        self.assert_refused("did not succeed")

    def test_refuses_a_search_made_before_the_date_ended(self):
        collect.rebuild_day_record(TARGET, self.record, now=DAY0 + timedelta(hours=20), downloader=FakeDownloader())
        self.assert_refused("whole UTC date")

    def test_refuses_a_flash_outside_the_examined_slots(self):
        self.rebuild(skip_slots={12})
        rec = json.loads(self.record.read_text())
        rec["features"] = [flash(DAY0 + timedelta(hours=2, minutes=5), "ghost")]
        self.record.write_text(json.dumps(rec))
        self.assert_refused("outside the examined slots")

    def test_refuses_without_an_index(self):
        self.rebuild()
        with self.assertRaisesRegex(ValueError, "no index.json"):
            history.backfill_day(self.base / "empty", TARGET, now=NOW, rebuild_record=self.record)
        self.assertFalse((self.base / "empty").exists())

    # --- EUMETSAT errors: no record, nothing to ingest --------------------------
    def test_eumetsat_error_or_rate_limit_writes_no_record(self):
        with self.assertRaisesRegex(RuntimeError, "429"):
            collect.rebuild_day_record(TARGET, self.record, now=NOW,
                                       downloader=FakeDownloader(error=RuntimeError("HTTP 429 Too Many Requests")))
        self.assertFalse(self.record.exists())

    # --- CLI ------------------------------------------------------------------
    def test_cli_backfill_writes_a_result_and_runs_verify(self):
        self.rebuild(flashes=[flash(DAY0 + timedelta(hours=4), "cli")])
        out = self.base / "result.json"
        code = history.main(["backfill", "--archive-dir", str(self.root), "--day", TARGET,
                             "--rebuild-record", str(self.record), "--result-out", str(out), "--now", NOW.isoformat()])
        self.assertEqual(code, 0)
        result = json.loads(out.read_text())
        self.assertEqual((result["outcome"], result["date"], result["row"]["count"]), ("success", TARGET, 1))
        self.assertEqual(result["verify"]["integrity"], "ok")


class BackfillPlanTests(unittest.TestCase):
    def setUp(self):
        self.temp = tempfile.TemporaryDirectory()
        self.root = Path(self.temp.name) / "history"
        d = datetime(2026, 10, 24, tzinfo=timezone.utc)
        archive_observations(self.root, [flash(d + timedelta(hours=1), "p")], now=NOW,
                             scanned_windows=[successful_scan((d + timedelta(hours=1)).isoformat(),
                                                              (d + timedelta(hours=2)).isoformat(), 6)])

    def tearDown(self):
        self.temp.cleanup()

    def test_plan_lists_never_searched_dates_newest_first(self):
        plan = history.backfill_plan(self.root, end_date=date(2026, 10, 26), days=5, today=NOW.date())
        self.assertEqual(plan, ["2026-10-26", "2026-10-25", "2026-10-23", "2026-10-22"])

    def test_plan_skips_shard_lost_dates(self):
        (self.root / day_path(date(2026, 10, 24))).unlink()
        archive_observations(self.root, [], now=NOW)
        plan = history.backfill_plan(self.root, end_date=date(2026, 10, 25), days=3, today=NOW.date())
        self.assertEqual(plan, ["2026-10-25", "2026-10-23"])

    def test_plan_stops_at_the_retention_cutoff(self):
        plan = history.backfill_plan(self.root, end_date=date(2025, 10, 28), days=5, today=NOW.date())
        self.assertEqual(plan, ["2025-10-28", "2025-10-27"])

    def test_plan_limits(self):
        for end, days in ((date(2026, 10, 26), 32), (date(2026, 10, 26), 0), (NOW.date(), 1)):
            with self.assertRaises(ValueError):
                history.backfill_plan(self.root, end_date=end, days=days, today=NOW.date())
        with self.assertRaisesRegex(ValueError, "no index.json"):
            history.backfill_plan(Path(self.temp.name) / "none", end_date=date(2026, 10, 26), days=1, today=NOW.date())

    def test_cli_plan_defaults_to_yesterday(self):
        import contextlib
        import io
        out = io.StringIO()
        with contextlib.redirect_stdout(out):
            code = history.main(["backfill-plan", "--archive-dir", str(self.root), "--end-date", "", "--days", "3",
                                 "--now", NOW.isoformat()])
        self.assertEqual(code, 0)
        self.assertEqual(out.getvalue().split(), ["2026-10-26", "2026-10-25"])


class BackfillStatusTests(unittest.TestCase):
    PREVIOUS = {
        "schema": "uk-lightning-status.v1", "written_utc": "2026-10-27T05:18:00.000Z", "health": "ok", "freshness": "ok",
        "integrity": {"state": "ok", "alert": None, "lost_days": [], "quarantined_observations": 0, "source": "this_run"},
        "last_attempt_utc": "2026-10-27T05:17:00.000Z", "last_attempt_outcome": "success", "last_error": None,
        "consecutive_failures": 0, "last_successful_scan_end_utc": "2026-10-27T05:10:09.000Z",
        "archive": {"outcome": "success", "archived_observations": 1, "index_generated_utc": "2026-10-27T05:18:00.000Z"},
    }

    def test_backfill_block_leaves_the_hourly_heartbeat_untouched(self):
        index = {"generated_utc": "2026-10-27T07:00:00.000Z", "archived_observations": 4,
                 "integrity": {"state": "ok", "alert": None, "lost_days": []}}
        result = status_module.backfill_status(
            self.PREVIOUS, now=NOW, planned=["2026-10-26", "2026-10-25", "2026-10-24"], filled=["2026-10-26"],
            stopped="eumetsat_error", error="2026-10-25: RuntimeError: HTTP 429", index=index)
        for key in ("written_utc", "last_attempt_utc", "last_attempt_outcome", "last_error", "consecutive_failures",
                    "last_successful_scan_end_utc", "freshness", "health"):
            self.assertEqual(result[key], self.PREVIOUS[key], key)
        block = result["backfill"]
        self.assertEqual(block["outcome"], "stopped")
        self.assertEqual(block["filled_days"], ["2026-10-26"])
        self.assertEqual(block["remaining_unknown_days"], ["2026-10-25", "2026-10-24"])
        self.assertIn("429", block["error"])
        self.assertEqual(result["archive"]["archived_observations"], 4)
        self.assertEqual(result["archive"]["index_generated_utc"], "2026-10-27T07:00:00.000Z")
        self.assertEqual(result["archive"]["outcome"], "success")

    def test_hourly_status_carries_the_last_backfill_forward(self):
        previous = {**self.PREVIOUS, "backfill": {"last_run_utc": "2026-10-27T06:00:00.000Z", "outcome": "completed"}}
        result = status_module.build_status(now=NOW, previous=previous, record=None, archive_result=None,
                                            collect_outcome="failure", archive_outcome="skipped")
        self.assertEqual(result["backfill"], previous["backfill"])

    def test_cli_backfill_reads_plan_and_filled_files(self):
        with tempfile.TemporaryDirectory() as tmp:
            tmp = Path(tmp)
            (tmp / "status.json").write_text(json.dumps(self.PREVIOUS))
            (tmp / "plan.txt").write_text("2026-10-26\n2026-10-25\n")
            (tmp / "filled.txt").write_text("2026-10-26\n2026-10-25\n\n")
            (tmp / "history").mkdir()
            (tmp / "history" / "index.json").write_text(json.dumps(
                {"generated_utc": "2026-10-27T07:00:00.000Z", "archived_observations": 9,
                 "integrity": {"state": "ok", "alert": None, "lost_days": []}}))
            code = status_module.main(["backfill", "--status", str(tmp / "status.json"), "--plan", str(tmp / "plan.txt"),
                                       "--filled", str(tmp / "filled.txt"), "--stopped", "completed", "--error", "",
                                       "--archive-dir", str(tmp / "history"), "--now", NOW.isoformat()])
            self.assertEqual(code, 0)
            written = json.loads((tmp / "status.json").read_text())
            self.assertEqual(written["backfill"]["outcome"], "completed")
            self.assertEqual(written["backfill"]["remaining_unknown_days"], [])
            self.assertIsNone(written["backfill"]["error"])
            self.assertEqual(written["archive"]["archived_observations"], 9)


if __name__ == "__main__":
    unittest.main()