GLOBALGRID2050 / EXPLORE CODE

Part of: Explore Code / Electricity markets and data pipelines / globalgrid2050 / automation/news/tests

Test Pipeline Headlines

A test in the tests folder of GlobalGrid2050 that checks Datacentre Feed and Pipeline Headlines.

475 lines of Python from globalgrid2050, in 1 file. Topic: Electricity markets and data pipelines. Section 807 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. Test Pipeline Headlines test · globalgrid2050 / automation/news/tests/test_pipeline_headlines.py lines 1–475

Test Pipeline Headlines

A test in the tests folder of GlobalGrid2050 that checks Datacentre Feed and Pipeline Headlines.

testglobalgrid2050 / automation/news/tests/test_pipeline_headlines.py · lines 1–475 of 475 · Entry point · pattern repd_row (shared by 2 files)

What it does
It tests solar and the REPD register. Defines 37 functions or classes, including repd_row, test_real_headlines_are_kept_and_classified, test_out_of_scope_headlines_are_dropped, test_every_category_and_technology_is_in_the_contract_list, test_owner_example_botley_west_is_a_possible_match_on_name_plus_technology. Uses libraries __future__, json, re, sys, pathlib. Side effects: reads or writes files; makes network requests.
Why it exists in the grid workflow
Domain stage upstream_foresight (topic 'markets-data'): upstream engineering foresight over GB transmission and distribution topology (TSO/DNO, 132kV and above, the REPD pipeline, data centres), which Ventus Ltd states it models for foresight. Domain terms in the code: solar, the REPD register.
Inputs & outputs
In: JSON file pipeline-headlines.v1.schema.json; JSON file pipeline_headlines.json; JSON file feeds.json; JSON file out.json; JSON file pipeline_feeds.json; JSON file never.json.
Downstream impact
Used directly by 5 files: Compare Legacy Collector (globalgrid2050); Test News Priority Export (globalgrid2050); GlobalGrid2050 Agent7 V922 CI Workflow (globalgrid2050); GlobalGrid2050 Pipeline Project Evidence Weekly Workflow (globalgrid2050); and 1 more. Reached from 1 other entry point, including GlobalGrid2050 Pipeline Headlines Workflow (globalgrid2050).
What it achieves
It is built from the REPD row helper (`repd_row`), the run step (`run`) and the validate step (`validate`). Tests for `automation/news/pipeline_headlines.py`. Offline: fixtures hold real headlines, links and dates only. Its code works with 840 MW, 150 MW and 1 GWh.
Why it matters
Run by GlobalGrid2050 Agent7 V922 CI Workflow, GlobalGrid2050 Pipeline Project Evidence Weekly Workflow and GlobalGrid2050 Project Signals Acid Workflow. Checked by 2 tests, including Compare Legacy Collector.
Application
globalgrid2050 · automation/news/tests · test
Uses
Datacentre Feed (sys.path import), Pipeline Headlines (sys.path import), __future__, json, re, sys, pathlib, pytest
Used by
run by Compare Legacy Collector (script call), imported by Test News Priority Export (import), run by GlobalGrid2050 Agent7 V922 CI Workflow (workflow run), run by GlobalGrid2050 Pipeline Project Evidence Weekly Workflow (workflow run), run by GlobalGrid2050 Project Signals Acid Workflow (workflow run)
Produces
repd_row, test_real_headlines_are_kept_and_classified, test_out_of_scope_headlines_are_dropped, test_every_category_and_technology_is_in_the_contract_list, test_owner_example_botley_west_is_a_possible_match_on_name_plus_technology, test_match_bases_developer_authority_county, test_name_alone_is_never_a_match, test_name_must_be_exact_and_word_bounded, test_generic_names_never_match, test_colocated_rows_of_one_site_give_the_lowest_ref_and_two_sites_give_nothing, test_name_without_colocated_tail_and_single_distinctive_word, test_single_word_plus_technology_does_not_pick_the_wrong_hunterston
Defines here
repd_row, test_real_headlines_are_kept_and_classified, test_out_of_scope_headlines_are_dropped, test_every_category_and_technology_is_in_the_contract_list, test_owner_example_botley_west_is_a_possible_match_on_name_plus_technology, test_match_bases_developer_authority_county, test_name_alone_is_never_a_match, test_name_must_be_exact_and_word_bounded, test_generic_names_never_match, test_colocated_rows_of_one_site_give_the_lowest_ref_and_two_sites_give_nothing, test_name_without_colocated_tail_and_single_distinctive_word, test_single_word_plus_technology_does_not_pick_the_wrong_hunterston, test_branded_content_is_dropped, test_data_centre_headlines_never_get_a_repd_match, run, validate, test_fixture_run_matches_schema_and_contract, test_previous_items_are_kept_first_seen_is_stable_and_rerun_is_identical, test_all_feeds_failing_keeps_earlier_items_and_fails_only_without_any, test_one_bad_feed_does_not_stop_the_others, fetch, test_global_feed_needs_a_uk_word_and_foreign_headlines_are_dropped, rss, test_bbc_keyword_only_headlines_are_kept_without_an_event_word, test_bbc_headline_about_neither_topic_is_dropped_and_scope_rules_still_apply, test_bbc_lane_uses_the_event_category_and_keeps_the_botley_west_wording, test_the_same_keyword_only_headline_on_a_secondary_feed_is_not_kept, test_priority_items_kept_on_their_summary_survive_later_runs, test_live_feed_list_has_bbc_first_and_excludes_owner_rejected_publisher, test_datacentre_money_lane
Cannot conclude
Whether this code runs, is used by a live app today, or is correct. This is a static reading.
Evidence
globalgrid2050@0aca5121f0795e469b6cf4fc3d3b628edba8d883 automation/news/tests/test_pipeline_headlines.py:1-475 · blob 372f9baab8ed2db72dabc08344e59ddcebedcd0b · GitHub at these lines
State
achieves: comment · context: readme_derived · stage: output · effects: filesystem, network · domain stage: upstream_foresight · users: IPP_developer, ICP_grid_engineer · canonical: production_core
"""Tests for automation/news/pipeline_headlines.py. Offline: fixtures hold real headlines, links and dates only."""
from __future__ import annotations

import json
import re
import sys
from pathlib import Path

import pytest

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

import datacentre_feed as D  # noqa: E402
import pipeline_headlines as P  # noqa: E402

FIXTURES = Path(__file__).resolve().parent / "fixtures" / "pipeline"
SCHEMA = json.loads((PKG / "schema" / "pipeline-headlines.v1.schema.json").read_text(encoding="utf-8"))
NOW = "2026-11-14T12:00:00Z"


def repd_row(ref, site, tech="Solar Photovoltaics", operator="Example Energy Ltd", county="Oxfordshire",
             authority="West Oxfordshire"):
    return {"Ref ID": ref, "Site Name": site, "Technology Type": tech, "Operator (or Applicant)": operator,
            "County": county, "Planning Authority": authority, "Region": "South East"}


REPD = P.Repd([
    repd_row("12588", "Botley West, Botley - Botley West Solar Project",
             operator="Photovolt Development Partners & SolarFive",
             authority="The Planning Inspectorate - National Infrastructure"),
    repd_row("10529", "Botley - Solar Photovoltaic Array", operator="Red House Solar Limited"),
    repd_row("13599", "Beacon Fen Energy Park", operator="Low Carbon Limited", county="Lincolnshire"),
    repd_row("13600", "Beacon Fen Energy Park", tech="Battery", operator="Low Carbon Limited", county="Lincolnshire"),
    repd_row("15793", "Green Hill Solar Farm & Battery Storage", operator="Island Green Power",
             county="Northamptonshire"),
    repd_row("15252", "Steeple Renewables Project", operator="Renewable Energy Systems RES Limited",
             county="Nottinghamshire"),
    repd_row("70001", "Manor Farm - Solar Farm"),
    repd_row("70002", "Twin Oaks Solar Farm", county="Kent"),
    repd_row("70003", "Twin Oaks Solar Farm", county="Devon"),
    repd_row("70004", "Zennorwick - Battery Storage", tech="Battery", county="Cornwall"),
    repd_row("412", "Hunterston", tech="Battery", operator="Scottish Enterprise & SSE", county="Ayrshire",
             authority="North Ayrshire"),
    repd_row("10535", "Hunterston Grid Services Complex - Energy Storage Facility", tech="Battery",
             operator="Revera Energy", county="Ayrshire", authority="Scottish Government (S36)"),
])


@pytest.mark.parametrize("title, category, technology", [
    ("UK government refuses 840MW Botley West Solar Farm in first-ever solar NSIP no", "refusal", "solar"),
    ("Planned solar farm in Oxfordshire scrapped as government rejects application", "refusal", "solar"),
    ("Botley West Solar Farm: development consent order, Planning Act 2008", "dco", "solar"),
    ("Great North Road Solar and Biodiversity Park development consent decision announced", "dco", "solar"),
    ("Major solar projects by Elements Green, Cubico granted DCOs", "consent", "solar"),
    ("Low Carbon wins DCO for Beacon Fen Energy Park in Lincolnshire", "consent", None),
    ("Llyr offshore wind farm secures Section 36 Consent from Welsh Ministers", "consent", "offshore_wind"),
    ("PACE wins appeal for 49.5MW solar plant in Gloucestershire", "consent", "solar"),
    ("Blocked BESS in Britain: Clearstone's appeal fails for 150 MW site at Axminster [UPDATED]", "refusal", "battery"),
    ("UK's largest Solar farm approval deemed lawful by High Court", "planning_decision", "solar"),
    ("Decision due on huge solar farm on palace estate", "planning_decision", "solar"),
    ("Challenge to UK's largest solar farm plan rejected", "planning_decision", "solar"),
    ("Healey to veto new offshore wind projects if they will not lower bills", "policy", "offshore_wind"),
    ("RWE and KKR Reach Financial Close for 2.8 GW UK Offshore Wind Projects", "finance", "offshore_wind"),
    ("TotalEnergies acquires 450MW solar, 150MW BESS NSIP project", "finance", "solar"),
    ("Qair begins construction of 46 MW Brecks Solar Farm in Nottinghamshire", "construction", "solar"),
    ("Harmony Energy switches on 70 MWh Skeeby BESS in UK", "energisation", "battery"),
    ("1GWh Coalburn 1 battery energy storage enters operations in Scotland", "energisation", "battery"),
    ("Clean energy projects to be prioritised as grid connections reform evidence window opens", "grid", None),
    ("DESNZ steps back from proposal to require solar over car parks", "policy", "solar"),
    ("AI datacentres plan on-site power stations on huge scale despite climate risks, research reveals",
     "datacentre", None),
    ("Thistle Wind Partners submits offshore plans for Bowdun offshore wind farm", "other", "offshore_wind"),
])
def test_real_headlines_are_kept_and_classified(title, category, technology):
    assert P.category_of(title) == category
    assert P.technology_of(title) == technology


@pytest.mark.parametrize("title", [
    "Mini-gardens for bees on bus stop roofs approved",                       # event word, no topic
    "Investment delivers better bus stops and shelters across Lincolnshire",
    "Kier wins planning battle for Birmingham towers",
    "SMA Solar narrows 2026 guidance off strong nine-month performance",      # topic, no event word
    "Uskmouth BESS makes progress",
    "World is now in 'age of electricity', energy agency chief says",
    "Independent investigation concludes allegations against NESO were unsubstantiated",
    "Total solar eclipse approved as school holiday",                         # not the energy sense of solar
    "UK government approves new North Sea gas field, says tax revenue will fund clean energy",
    "Foreign Secretary's speech at the Clean Energy Transition summit",
    "Sea Link development consent decision announced",                        # no topic word: a known miss
])
def test_out_of_scope_headlines_are_dropped(title):
    assert P.category_of(title) is None


def test_every_category_and_technology_is_in_the_contract_list():
    assert set(SCHEMA["$defs"]["category"]["enum"]) == set(P.CATEGORIES)
    assert set(SCHEMA["properties"]["items"]["items"]["properties"]["technology"]["enum"]) == set(P.TECHNOLOGIES) | {None}


# ---------------------------------------------------------------- possible match

def test_owner_example_botley_west_is_a_possible_match_on_name_plus_technology():
    m = REPD.match("UK government refuses 840MW Botley West Solar Farm in first-ever solar NSIP no")
    assert m == {"repd_ref": "12588", "site_name": "Botley West, Botley - Botley West Solar Project",
                 "basis": "technology"}


def test_match_bases_developer_authority_county():
    assert REPD.match("SolarFive loses Botley West decision")["basis"] == "developer"
    assert REPD.match("Planning Inspectorate reports on Botley West")["basis"] == "planning_authority"
    assert REPD.match("Oxfordshire reacts to Botley West ruling")["basis"] == "county"


def test_name_alone_is_never_a_match():
    assert REPD.match("Botley West decision announced") is None
    assert REPD.match("Steeple Renewables Project development consent decision announced") is None


def test_name_must_be_exact_and_word_bounded():
    assert REPD.match("Botley Westfield solar farm refused") is None
    assert REPD.match("Green Hills solar farm approved") is None
    assert REPD.match("West Botley solar farm approved") is None      # 'Botley' alone is too short to stand as a name


def test_generic_names_never_match():
    assert REPD.match("Manor Farm solar farm approved in Oxfordshire") is None
    assert "manor farm" not in REPD.by_phrase and "solar farm" not in REPD.by_phrase


def test_colocated_rows_of_one_site_give_the_lowest_ref_and_two_sites_give_nothing():
    m = REPD.match("Low Carbon wins DCO for Beacon Fen Energy Park in Lincolnshire")
    assert (m["repd_ref"], m["basis"]) == ("13599", "developer")
    assert REPD.match("Twin Oaks Solar Farm approved") is None         # two rows, same name, same technology
    assert REPD.match("Twin Oaks Solar Farm approved in Devon")["repd_ref"] == "70003"


def test_name_without_colocated_tail_and_single_distinctive_word():
    assert REPD.match("Green Hill Solar Farm development consent decision announced")["repd_ref"] == "15793"
    assert REPD.match("Zennorwick battery scheme approved in Cornwall")["repd_ref"] == "70004"
    assert REPD.match("Zennorwick battery scheme approved") is None     # one word plus technology is not enough
    assert REPD.match("Cornwall battery scheme approved in Zennorwick") is None  # the word must sit before the technology


def test_single_word_plus_technology_does_not_pick_the_wrong_hunterston():
    assert REPD.match("Revera's Hunterston battery storage project starts construction") is None
    assert REPD.match("Hunterston BESS reaches financial close") is None


def test_branded_content_is_dropped():
    body = (b"<rss version='2.0'><channel><item><title>BRANDED CONTENT: Solar farm construction made easy</title>"
            b"<link>https://example.org/b</link></item></channel></rss>")
    items, _ = P.collect([{"url": "https://x.example/f", "source": "X", "uk_only": True}], lambda u: body, REPD, NOW)
    assert items == []


def test_data_centre_headlines_never_get_a_repd_match():
    title = "Botley West data centre approved"
    assert P.label(title, REPD) == ("datacentre", None, None)


# ---------------------------------------------------------------- end to end on fixtures

def run(tmp_path, *extra):
    out = tmp_path / "pipeline_headlines.json"
    code = P.main(["--out", str(out), "--feeds", str(FIXTURES / "feeds.json"), "--fixtures", str(FIXTURES),
                   "--now", NOW, *extra])
    return code, (json.loads(out.read_text(encoding="utf-8")) if out.exists() else None)


def validate(node, schema, root=SCHEMA, path="$"):
    """Small JSON Schema checker for the keywords this schema uses (no third-party dependency)."""
    if "$ref" in schema:
        schema = root["$defs"][schema["$ref"].split("/")[-1]]
    if "anyOf" in schema:
        errors = []
        for option in schema["anyOf"]:
            try:
                return validate(node, option, root, path)
            except AssertionError as error:
                errors.append(str(error))
        raise AssertionError("%s: no anyOf option fits: %s" % (path, errors))
    if "const" in schema:
        assert node == schema["const"], path
    if "enum" in schema:
        assert node in schema["enum"], "%s: %r" % (path, node)
    kind = schema.get("type")
    if kind:
        ok = {"object": dict, "array": list, "string": str, "integer": int, "null": type(None)}[kind]
        assert isinstance(node, ok) and not (kind == "integer" and isinstance(node, bool)), "%s: not %s" % (path, kind)
    if isinstance(node, str):
        assert len(node) >= schema.get("minLength", 0) and len(node) <= schema.get("maxLength", 10 ** 9), path
        if "pattern" in schema:
            assert re.search(schema["pattern"], node), "%s: %r" % (path, node)
    if isinstance(node, int) and "minimum" in schema:
        assert node >= schema["minimum"], path
    if isinstance(node, list):
        assert len(node) <= schema.get("maxItems", 10 ** 9), path
        for n, child in enumerate(node):
            validate(child, schema.get("items", {}), root, "%s[%d]" % (path, n))
    if isinstance(node, dict):
        for key in schema.get("required", []):
            assert key in node, "%s: missing %s" % (path, key)
        props, extra = schema.get("properties", {}), schema.get("additionalProperties", True)
        for key, child in node.items():
            if "propertyNames" in schema:
                validate(key, schema["propertyNames"], root, path + "." + key)
            if key in props:
                validate(child, props[key], root, path + "." + key)
            else:
                assert extra is not False, "%s: unexpected %s" % (path, key)
                if isinstance(extra, dict):
                    validate(child, extra, root, path + "." + key)


def test_fixture_run_matches_schema_and_contract(tmp_path):
    code, d = run(tmp_path)
    assert code == 0
    validate(d, SCHEMA)
    assert d["status"] == "partial" and d["counts"]["feeds_failed"] == 1 and d["counts"]["feeds_ok"] == 5
    assert d["feeds"][0]["source"] == "BBC News"                    # priority feeds are fetched first
    bbc = {i["title"]: i for i in d["items"] if i["source"] == "BBC News"}
    assert set(bbc) == {"Decision due on huge solar farm on palace estate",
                        "Government decision on 2,400 acre solar farm delayed"}
    failed = [f for f in d["feeds"] if f["status"] == "failed"]
    assert len(failed) == 1 and "error" in failed[0]
    items = d["items"]
    assert d["counts"]["items"] == len(items) >= 10
    assert sum(d["counts"]["by_category"].values()) == len(items)
    keys = [(P.when(i), i["link"]) for i in items]
    assert keys == sorted(keys, reverse=True)                       # newest first
    assert len({i["link"] for i in items}) == len(items)
    assert all(set(i) == set(P.ITEM_FIELDS) for i in items)         # nothing beyond the contract is stored
    titles = [i["title"] for i in items]
    botley = [i for i in items if "Botley West" in i["title"]]
    assert {i["category"] for i in botley} == {"dco"}  # gov.uk primary evidence remains
    assert all(i["possible_match"]["repd_ref"] == "12588" for i in botley)
    assert not any("bus stop" in t for t in titles)
    assert not any(" | " in t for t in titles)                      # opinion column dropped
    assert "Seaway7 Kicks Off Cable Installation Work at Ørsted's Hornsea 3" not in titles   # no UK word, global feed


def test_previous_items_are_kept_first_seen_is_stable_and_rerun_is_identical(tmp_path):
    out = tmp_path / "pipeline_headlines.json"
    old = {"schema": P.SCHEMA, "items": [
        {"title": "Example Wind Farm refused by council", "link": "https://example.org/old-story", "source": "Old",
         "date": "2026-08-10T08:00:00Z", "first_seen": "2026-08-10T13:00:00Z", "category": "consent",
         "technology": None, "possible_match": None},
        {"title": "Solar farm approved long ago", "link": "https://example.org/ancient", "source": "Old",
         "date": "2026-01-01T08:00:00Z", "first_seen": "2026-01-01T13:00:00Z", "category": "consent",
         "technology": "solar", "possible_match": None},
        {"title": "A headline today's rules would not keep", "link": "https://example.org/off-topic", "source": "Old",
         "date": "2026-11-01T08:00:00Z", "first_seen": "2026-11-01T13:00:00Z", "category": "other",
         "technology": None, "possible_match": None}]}
    out.write_text(json.dumps(old), encoding="utf-8")
    _, first = run(tmp_path)
    kept = {i["link"]: i for i in first["items"]}
    # 96 days old: kept until max_age_days (120), whatever the shared link merge does with age
    assert kept["https://example.org/old-story"]["first_seen"] == "2026-08-10T13:00:00Z"
    assert kept["https://example.org/old-story"]["category"] == "refusal"     # re-labelled with today's rules
    assert "https://example.org/ancient" not in kept                          # older than max_age_days
    assert "https://example.org/off-topic" not in kept
    _, second = run(tmp_path, "--now", "2026-11-14T18:00:00Z")
    assert [i for i in second["items"]] == [i for i in first["items"]]        # first_seen does not move


def test_all_feeds_failing_keeps_earlier_items_and_fails_only_without_any(tmp_path):
    feeds = tmp_path / "feeds.json"
    feeds.write_text(json.dumps({"feeds": [{"url": "https://example.invalid/a.xml", "source": "A", "uk_only": True}]}))
    out = tmp_path / "out.json"
    args = ["--out", str(out), "--feeds", str(feeds), "--fixtures", str(tmp_path), "--now", NOW]
    assert P.main(args) == 1                                                  # nothing fetched, nothing to keep
    out.write_text(json.dumps({"schema": P.SCHEMA, "items": [
        {"title": "Solar farm approved", "link": "https://example.org/a", "source": "A", "date": "2026-11-13T08:00:00Z",
         "first_seen": "2026-11-13T13:00:00Z", "category": "consent", "technology": "solar", "possible_match": None}]}))
    assert P.main(args) == 0
    d = json.loads(out.read_text())
    assert d["status"] == "failed" and len(d["items"]) == 1


def test_one_bad_feed_does_not_stop_the_others():
    def fetch(url):
        if "bad" in url:
            raise OSError("boom")
        return (b'<rss version="2.0"><channel><item><title>Sponsored: solar farm approved</title>'
                b'<link>https://example.org/s</link></item><item><title>Solar farm refused in Kent</title>'
                b'<link>http://Example.org/x?utm_source=a#top</link><description>Body text never stored</description>'
                b'</item></channel></rss>')
    feeds = [{"url": "https://bad.example/f", "source": "Bad", "uk_only": True},
             {"url": "https://good.example/f", "source": "Good", "uk_only": True}]
    items, health = P.collect(feeds, fetch, P.Repd(), NOW)
    assert [h["status"] for h in health] == ["failed", "ok"]
    assert [(i["title"], i["link"], i["date"]) for i in items] == [
        ("Solar farm refused in Kent", "https://example.org/x", None)]        # sponsored dropped, link canonical
    assert "Body text" not in json.dumps(items)


def test_global_feed_needs_a_uk_word_and_foreign_headlines_are_dropped():
    body = (b'<rss version="2.0"><channel>'
            b'<item><title>Solar farm approved in Scotland</title><link>https://example.org/1</link></item>'
            b'<item><title>Solar farm approved in Greece</title><link>https://example.org/2</link></item>'
            b'<item><title>Solar farm approved somewhere</title><link>https://example.org/3</link><description>In Kent, England</description></item>'
            b'<item><title>Wind and battery project approved in New South Wales</title><link>https://example.org/4</link></item>'
            b'<item><title>Twin Oaks Solar Farm approved</title><link>https://example.org/5</link></item>'
            b'</channel></rss>')
    world, _ = P.collect([{"url": "https://w.example/f", "source": "W", "uk_only": False}], lambda u: body, REPD, NOW)
    # a UK word or a REPD site name in the title; the feed summary ("In Kent, England") is never read
    assert [i["link"] for i in world] == ["https://example.org/1", "https://example.org/5"]
    uk, _ = P.collect([{"url": "https://w.example/f", "source": "W", "uk_only": True}], lambda u: body, REPD, NOW)
    assert [i["link"] for i in uk] == ["https://example.org/1", "https://example.org/3", "https://example.org/5"]


# ---------------------------------------------------------------- BBC priority lane

BBC = [{"url": "https://feeds.bbci.co.uk/news/rss.xml", "source": "BBC News", "uk_only": True, "lane": "keywords", "primary": True}]


def rss(*entries):
    return ("<rss version='2.0'><channel>" + "".join(
        "<item><title>%s</title><link>https://www.bbc.co.uk/news/articles/%d</link><description>%s</description></item>"
        % (title, n, summary) for n, (title, summary) in enumerate(entries)) + "</channel></rss>").encode()


def test_bbc_keyword_only_headlines_are_kept_without_an_event_word():
    body = rss(("Huge solar farm on palace estate divides village", ""),
               ("The village that fears being surrounded", "Residents say three solar farms would encircle them."),
               ("Tech giant picks town for new datacentre", ""),
               ("What the new AI growth zone means for the valley", ""),
               ("Why your street may get a new neighbour", "A hyperscale data center is planned nearby."),
               ("Solar panels for every new home", ""))
    items, _ = P.collect(BBC, lambda u: body, REPD, NOW)
    assert [(i["category"], i["technology"]) for i in items] == [
        ("other", "solar"), ("other", None), ("datacentre", None), ("datacentre", None), ("datacentre", None),
        ("other", "solar")]
    assert all(set(i) - {"_feed"} == set(P.ITEM_FIELDS) for i in items)
    assert "encircle" not in json.dumps(items) and "hyperscale" not in json.dumps(items)   # summary never stored


def test_bbc_headline_about_neither_topic_is_dropped_and_scope_rules_still_apply():
    body = rss(("Mini-gardens for bees on bus stop roofs approved", "Council approves the plan."),
               ("Chancellor to cut tariffs on marmalade", ""),
               ("Sponsored: solar panels for your home", ""),
               ("Texas solar farm destroyed by hail", ""),
               ("Total solar eclipse: where to watch", ""),
               ("US firm to build data centre in Wales", ""))
    items, _ = P.collect(BBC, lambda u: body, REPD, NOW)
    assert [i["title"] for i in items] == ["US firm to build data centre in Wales"]


def test_bbc_lane_uses_the_event_category_and_keeps_the_botley_west_wording():
    body = rss(("UK government refuses 840MW Botley West Solar Farm in first-ever solar NSIP no", ""),
               ("Planned solar farm in Oxfordshire scrapped as government rejects application", ""),
               ("Botley West Solar Farm: development consent order, Planning Act 2008", ""),
               ("Decision due on huge farm on palace estate", "The Botley West solar farm would cover 2,400 acres."),
               ("Challenge to UK's largest solar farm plan rejected", ""))
    items, _ = P.collect(BBC, lambda u: body, REPD, NOW)
    assert [i["category"] for i in items] == ["refusal", "refusal", "dco", "planning_decision", "planning_decision"]
    assert [bool(i["possible_match"]) for i in items] == [True, False, True, False, False]   # match reads the title only


def test_the_same_keyword_only_headline_on_a_secondary_feed_is_not_kept():
    other = [{"url": "https://example.org/f", "source": "Other", "uk_only": True}]
    items, _ = P.collect(other, lambda u: rss(("Huge solar farm on palace estate divides village", "")), REPD, NOW)
    assert items == []


def test_priority_items_kept_on_their_summary_survive_later_runs():
    body = rss(("The village that fears being surrounded", "Residents say three solar farms would encircle them."))
    items, health = P.collect(BBC, lambda u: body, REPD, NOW)
    first = P.build({}, items, health, REPD, NOW, lanes={"BBC News": "keywords"})
    later = P.build(first, [], [], REPD, "2026-11-15T12:00:00Z", lanes={"BBC News": "keywords"})
    assert [i["title"] for i in later["items"]] == ["The village that fears being surrounded"]
    assert later["items"][0]["first_seen"] == NOW


def test_live_feed_list_has_bbc_first_and_excludes_owner_rejected_publisher():
    feeds = json.loads((PKG / "pipeline_feeds.json").read_text(encoding="utf-8"))["feeds"]
    sources = list(dict.fromkeys(f["source"] for f in feeds))
    assert sources[0] == "BBC News"
    assert "Solar Power Portal" not in sources
    assert [f["source"] for f in feeds if f.get("primary")] == [f["source"] for f in feeds][:sum(bool(f.get("primary")) for f in feeds)]
    assert all(f.get("lane") == "keywords" for f in feeds if "feeds.bbci.co.uk" in f["url"])
    by_source = {}
    for f in feeds:
        by_source.setdefault(f["source"], set()).add(f.get("lane"))
    assert all(len(v) == 1 for v in by_source.values())             # one lane per source label
    assert not any("reuters" in f["url"] or "news.google" in f["url"] for f in feeds)


# ---------------------------------------------------------------- world lanes

@pytest.mark.parametrize("title, kept", [
    ("Meta Sells $25 Billion of Bonds to Fund AI Data Centers", True),
    ("AI Bubble Fears Hit Chipmaker Shares as Hyperscaler Capex Soars", True),
    ("UK Government offers £180m for AI data centre – but there’s a catch", True),
    ("Drax partners with Brookfield on £20bn-plus AI data centre plans", True),
    ("Microsoft Opens New Data Center in Wisconsin", False),          # data centre, no money
    ("Oil Rises as Traders Weigh Supply Risks", False),               # money and markets, no data centre
    ("Vertiv completes KES acquisition to expand EMEA services", False),
])
def test_datacentre_money_lane(title, kept):
    assert (P.lane_label(title, "datacentre_money") == ("datacentre", None, None)) is kept


@pytest.mark.parametrize("title, kept", [
    ("Chinese PV Industry Brief: Polysilicon prices rise again", True),
    ("China to end export tax rebates for solar modules", True),
    ("JinkoSolar, LONGi cut module output as prices slide", True),
    ("Germany awards 288 MW of solar in rooftop tender at average price of €0.0967/kWh", False),
    ("Slovenia tenders solar-plus-storage project", False),
    ("China approves new nuclear reactors", False),
])
def test_china_solar_lane(title, kept):
    assert (P.lane_label(title, "china_solar") == ("other", "solar", None)) is kept


def test_world_lanes_are_capped_never_matched_to_repd_and_do_not_crowd_out_uk_news():
    def entries(prefix, n):
        return "".join("<item><title>%s %d</title><link>https://example.org/%s/%d</link>"
                       "<pubDate>Fri, %02d Nov 2026 09:00:00 GMT</pubDate></item>" % (prefix, k, abs(hash(prefix)), k, 1 + k % 13)
                       for k in range(n))
    bodies = {"b": "Botley West data centers draw $1 billion investment", "p": "China solar module prices fall, week"}
    feeds = [{"url": "https://b.example/f", "source": "Bloomberg", "uk_only": False, "lane": "datacentre_money"},
             {"url": "https://p.example/f", "source": "pv magazine", "uk_only": False, "lane": "china_solar"},
             {"url": "https://u.example/f", "source": "UK", "uk_only": True}]

    def fetch(url):
        if "u.example" in url:
            return b"<rss version='2.0'><channel><item><title>Solar farm refused in Kent</title><link>https://example.org/uk</link></item></channel></rss>"
        return ("<rss version='2.0'><channel>" + entries(bodies[url[8]], 25) + "</channel></rss>").encode()
    items, health = P.collect(feeds, fetch, REPD, NOW)
    assert len(items) == 51
    lanes = {f["source"]: f["lane"] for f in feeds if f.get("lane")}
    d = P.build({}, items, health, REPD, NOW, lanes=lanes)
    by_source = {}
    for i in d["items"]:
        by_source.setdefault(i["source"], []).append(i)
    assert {k: len(v) for k, v in by_source.items()} == {"Bloomberg": 10, "pv magazine": 10, "UK": 1}
    assert all(i["possible_match"] is None for i in by_source["Bloomberg"] + by_source["pv magazine"])
    assert {i["category"] for i in by_source["Bloomberg"]} == {"datacentre"}
    assert {(i["category"], i["technology"]) for i in by_source["pv magazine"]} == {("other", "solar")}
    again = P.build(d, [], [], REPD, "2026-11-15T12:00:00Z", lanes=lanes)     # the cap holds on later runs
    assert len(again["items"]) == 21


def test_near_identical_titles_without_event_or_project_identity_are_not_silently_collapsed():
    # Former test assumed 85% text overlap alone proved one event. This is unsafe:
    # separate article URLs can cover separate decisions, even for the same developer.
    # Keep all three for independent project/article adjudication when source identity
    # has no REPD Ref, original URL lineage or common decision reference.
    a = {"title": "Statkraft hits key milestone at first UK solar scheme in Yorkshire", "link": "https://a.example/1",
         "source": "A", "date": "2026-09-01T09:00:00Z", "first_seen": NOW}
    b = dict(a, title="Statkraft hits key milestone at first UK solar scheme in Yorkshire - Example Site",
             link="https://b.example/2", source="B", date="2026-09-02T09:00:00Z")
    c = dict(a, title="Statkraft hits a key milestone at first UK solar scheme in Yorkshire", link="https://c.example/3")
    merged = P.merge([], [b, a, c], P.Repd(), NOW)
    assert {i["link"] for i in merged} == {a["link"], b["link"], c["link"]}
    assert len(merged) == 3


def test_probe_report_lists_every_feed_and_kept_headline(tmp_path):
    report = tmp_path / "probe.md"
    assert P.main(["--probe", str(report), "--feeds", str(FIXTURES / "feeds.json"), "--fixtures", str(FIXTURES),
                   "--now", NOW, "--out", str(tmp_path / "never.json")]) == 0
    text = report.read_text(encoding="utf-8")
    assert "FAILED OSError" in text and "Botley West" in text and "REPD 12588" in text
    assert not (tmp_path / "never.json").exists()


def test_live_feed_list_is_https_and_reuses_the_shared_fetcher():
    feeds = P.load_feeds(PKG / "pipeline_feeds.json")
    assert feeds and all(f["url"].startswith("https://") and f["source"] for f in feeds)
    assert len({f["url"] for f in feeds}) == len(feeds)
    assert P.D.fetch_url is D.fetch_url and P.D.parse_feed is D.parse_feed