In this section
- Acceptance test · globalgrid2050 / automation/repd/tests/acceptance.py lines 1103–1105
- Conftest test · globalgrid2050 / automation/repd/tests/conftest.py lines 1–111
- Publication 2026q2 test · globalgrid2050 / automation/repd/tests/fixtures/detect_fetch/publication_2026q2.html lines 1–7
- Publication 2026q3 test · globalgrid2050 / automation/repd/tests/fixtures/detect_fetch/publication_2026q3.html lines 1–21
- Harness test · globalgrid2050 / automation/repd/tests/pages/harness.js lines 1–441
- Harness Test test · globalgrid2050 / automation/repd/tests/pages/harness.test.js lines 1–86
Acceptance
A Python command-line tool in the tests folder of GlobalGrid2050 that tests Renewable Energy Planning Database (REPD) grid batteries, REPD solar operational over 4MW and REPD master, using SHA-256.
testglobalgrid2050 / automation/repd/tests/acceptance.py · lines 1103–1105 of 1105 (continued from section 827) · Entry point · pattern _resolve (shared by 2 files)
- What it does
- It tests REPD grid batteries, REPD solar operational over 4MW, REPD master and the REPD register. Defines 43 functions or classes, including norm_ws, norm_header, demojibake, is_mojibake, to_float. Uses libraries __future__, argparse, csv, datetime, decimal. Side effects: reads or writes files; starts processes.
- Why it exists in the grid workflow
- Domain stage upstream_foresight (path word 'repd'): 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: the REPD register.
- Inputs & outputs
- In: CSV file repd.csv; CSV file repd-grid-batteries.csv; CSV file repd-solar-operational.csv; CSV file repd-solar-operational-over-4mw.csv; JSON file repd_master.json; command-line arguments. Coordinate systems named: OSGB36 / EPSG:27700 (British National Grid), WGS84 / EPSG:4326.
- Downstream impact
- Used directly by 1 file: Test Acceptance (globalgrid2050). Reached from 1 other entry point, including GlobalGrid2050 Project Signals Acid Workflow (globalgrid2050).
- What it achieves
- It cleans ref (`clean_ref`), reads CSV (`read_csv`) and reads xlsx (`read_xlsx`). End-to-end acceptance harness for a REPD release candidate. Its code works with 1 MW and 4 MW. It accepts `--official-csv`, `--official-xlsx` and `--xlsx-sheet`.
- Why it matters
- Checked by Test Acceptance.
- Application
- globalgrid2050 · automation/repd/tests · Python CLI
- Uses
- __future__, argparse, csv, datetime, decimal, hashlib, io, json
- Used by
- imported by Test Acceptance (import)
- Produces
- norm_ws, norm_header, demojibake, is_mojibake, to_float, clean_ref, sha256_file, decimals, excel_serial, month_first_format, CsvFile, read_csv
- Defines here
- none
- 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/repd/tests/acceptance.py:1103-1105 · blob e6d00c4aeb9bae7eed5a5351b70ad938f86276dc · GitHub at these lines
- State
- achieves: comment · context: readme_derived · stage: output · effects: filesystem, process · domain stage: upstream_foresight · users: IPP_developer, ICP_grid_engineer · canonical: production_core
if __name__ == "__main__":
sys.exit(main())Conftest
A Python module in the tests folder of GlobalGrid2050 that tests the Renewable Energy Planning Database (REPD) register. It makes CSV (`make_csv`) and makes xlsx (`make_xlsx`).
testglobalgrid2050 / automation/repd/tests/conftest.py · lines 1–111 of 111 · Entry point · pattern _csv_text (shared by 2 files)
- What it does
- It tests the REPD register. Defines 6 functions or classes, including base_rows, _csv_text, make_csv, make_xlsx, rule. Uses libraries __future__, csv, io, sys, datetime. Side effects: reads or writes files.
- Why it exists in the grid workflow
- Domain stage upstream_foresight (path word 'repd'): 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: the REPD register.
- Downstream impact
- Used directly by 4 files: Test Clean (globalgrid2050); Test Date Rule (globalgrid2050); Test Monthly (globalgrid2050); GlobalGrid2050 Project Signals Acid Workflow (globalgrid2050).
- What it achieves
- Synthetic official-style REPD fixtures for the cleaning step.
- Why it matters
- Run by GlobalGrid2050 Project Signals Acid Workflow. Checked by 3 tests, including Test Clean.
- Application
- globalgrid2050 · automation/repd/tests · Python module
- Uses
- Renewable Energy Planning Database (REPD) Date Rule (sys.path import), Renewable Energy Planning Database (REPD) Clean (sys.path import), __future__, csv, io, sys, datetime, pathlib, openpyxl, pytest
- Used by
- imported by Test Clean (import), imported by Test Date Rule (import), imported by Test Monthly (import), run by GlobalGrid2050 Project Signals Acid Workflow (workflow run)
- Produces
- base_rows, make_csv, make_xlsx, rule, cl
- Defines here
- base_rows, _csv_text, make_csv, make_xlsx, rule, cl
- 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/repd/tests/conftest.py:1-111 · blob ed1b87810ccd5b52c3fc682ce2c09efadf9c78b8 · GitHub at these lines
- State
- achieves: comment · context: readme_derived · stage: ingest · effects: filesystem · domain stage: upstream_foresight · users: IPP_developer, ICP_grid_engineer · canonical: production_core
"""Synthetic official-style REPD fixtures for the cleaning step. No network, no real data."""
from __future__ import annotations
import csv
import io
import sys
from datetime import date, datetime
from pathlib import Path
import openpyxl
import pytest
PKG = Path(__file__).resolve().parents[1] # automation/repd
sys.path.insert(0, str(PKG))
# Official header order in miniature, keeping the real quirks: a trailing space and a
# double space inside two headers.
COLUMNS = [
"Old Ref ID", "Ref ID", "Record Last Updated (dd/mm/yyyy)", "Operator (or Applicant)",
"Site Name", "Technology Type", "Storage Co-location REPD Ref ID", "Installed Capacity (MWelec)",
"Development Status (short)", "Are they re-applying (Old REPD Ref) ", "Post Code",
"Planning Authority", "Planning Application Reference", "Judicial Review", "Offshore Wind Round",
"Planning Application Submitted", "Planning Application Withdrawn", "Planning Permission Refused",
"Appeal Lodged", "Appeal Withdrawn", "Appeal Refused", "Appeal Granted",
"Planning Permission Granted", "Secretary of State - Intervened", "Secretary of State - Refusal",
"Secretary of State - Granted", "Planning Permission Expired", "Under Construction", "Operational",
]
LOCALE_SHORT_DATE = "mm-dd-yy" # built-in format 14: what almost every official date cell carries
MONTH_FIRST = "m/d/yyyy" # explicit custom format carried by the 25 October cells
def base_rows(n: int = 6) -> list[dict]:
rows = []
for i in range(1, n + 1):
rows.append({
"Old Ref ID": "", "Ref ID": 100 + i,
"Record Last Updated (dd/mm/yyyy)": date(2026, 10, 8),
"Operator (or Applicant)": f"Operator {i} Ltd", "Site Name": f"Site {i}",
"Technology Type": "Solar Photovoltaics" if i % 2 else "Battery",
"Storage Co-location REPD Ref ID": "",
"Installed Capacity (MWelec)": 49.9 if i % 2 else 120,
"Development Status (short)": "Application Submitted",
"Are they re-applying (Old REPD Ref) ": "", "Post Code": f"SY{i} 1AA",
"Planning Authority": "Shropshire",
"Planning Application Reference": f"26/0{i}/FUL",
"Planning Application Submitted": date(2026, 8, 20 + i),
"Appeal Lodged": date(2026, 9, 13 + i) if i == 2 else None,
})
return rows
def _csv_text(value) -> str:
if value is None:
return ""
if isinstance(value, (date, datetime)):
return value.strftime("%d/%m/%Y")
return str(value)
def make_csv(rows: list[dict], overrides: dict | None = None, columns: list[str] | None = None,
newline: str = "\r\n", encoding: str = "cp1252") -> bytes:
"""Official-style CSV: cp1252, CRLF. overrides[(ref, column)] = literal CSV text."""
overrides = overrides or {}
columns = columns or COLUMNS
buffer = io.StringIO(newline="")
writer = csv.writer(buffer, lineterminator=newline)
writer.writerow(columns)
for row in rows:
writer.writerow([overrides.get((row["Ref ID"], c), _csv_text(row.get(c))) for c in columns])
return buffer.getvalue().encode(encoding)
def make_xlsx(rows: list[dict], formats: dict | None = None, overrides: dict | None = None,
columns: list[str] | None = None) -> bytes:
"""Workbook with a notes sheet first and typed date cells.
formats[(ref, column)] = number format; overrides[(ref, column)] = raw cell value."""
formats, overrides = formats or {}, overrides or {}
columns = columns or COLUMNS
book = openpyxl.Workbook()
book.active.title = "Definition Sheet"
book.active.append(["notes"])
sheet = book.create_sheet("REPD")
sheet.append(columns)
for r, row in enumerate(rows, start=2):
for c, name in enumerate(columns, start=1):
key = (row["Ref ID"], name)
value = overrides.get(key, row.get(name))
if isinstance(value, date) and not isinstance(value, datetime):
value = datetime(value.year, value.month, value.day)
if value is None or value == "":
continue
cell = sheet.cell(row=r, column=c, value=value)
if isinstance(value, datetime):
cell.number_format = formats.get(key, LOCALE_SHORT_DATE)
elif key in formats:
cell.number_format = formats[key]
out = io.BytesIO()
book.save(out)
return out.getvalue()
@pytest.fixture(scope="session")
def rule():
import date_rule
return date_rule
@pytest.fixture(scope="session")
def cl():
import clean
return cleanPublication 2026q2
A web page in the detect fetch folder of GlobalGrid2050 that tests Renewable Energy Planning Database (REPD) publication q2 and the REPD register. Published at https://globalgrid2050.com/automation/repd/tests/fixtures/detect_fetch/publication_2026q2.html.
testglobalgrid2050 / automation/repd/tests/fixtures/detect_fetch/publication_2026q2.html · lines 1–7 of 7 · Entry point
- What it does
- It tests REPD publication q2 and the REPD register. Side effects: changes the web page (DOM).
- Why it exists in the grid workflow
- Domain stage upstream_foresight (path word 'repd'): 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: the REPD register.
- Inputs & outputs
- In: CSV file REPD_Publication_Q2_2026.csv; Excel file REPD_Publication_Q2_2026.xlsx. Out: web page.
- Downstream impact
- It is itself an entry point, published at https://globalgrid2050.com/automation/repd/tests/fixtures/detect_fetch/publication_2026q2.html.
- What it achieves
- Constructed fixture: the page as described for the July 2026 release.
- Why it matters
- No other file in the estate was found to import it; it is an entry point.
- Application
- globalgrid2050 · automation/repd/tests/fixtures/detect_fetch · page
- Uses
- none found
- Used by
- none found
- Produces
- none declared
- Defines here
- none
- 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/repd/tests/fixtures/detect_fetch/publication_2026q2.html:1-7 · blob ab152266d1dae4f2a64c7c0f2c6143a0474a89e9 · GitHub at these lines
- State
- achieves: comment · context: readme_derived · stage: output · effects: dom · domain stage: upstream_foresight · users: IPP_developer, ICP_grid_engineer · canonical: production_core
<!DOCTYPE html>
<html lang="en"><body>
<!-- constructed fixture: the page as described for the July 2026 release -->
<div class="gem-c-metadata"><dl><dt>Last updated</dt><dd>3 August 2026</dd></dl></div>
<a class="govuk-link" href="https://assets.publishing.service.gov.uk/media/6a6cbdc00c36759b5ccaa305/REPD_Publication_Q2_2026.csv">Renewable Energy Planning Database (REPD): July 2026 (CSV)</a>
<a class="govuk-link" href="https://assets.publishing.service.gov.uk/media/6a6cbdd2862aaf18d9c62b02/REPD_Publication_Q2_2026.xlsx">Renewable Energy Planning Database (REPD): July 2026 (Excel)</a>
</body></html>Publication 2026q3
A web page in the detect fetch folder of GlobalGrid2050 that tests Renewable Energy Planning Database (REPD) publication q3 and the REPD register. Published at https://globalgrid2050.com/automation/repd/tests/fixtures/detect_fetch/publication_2026q3.html.
testglobalgrid2050 / automation/repd/tests/fixtures/detect_fetch/publication_2026q3.html · lines 1–21 of 21 · Entry point
- What it does
- It tests REPD publication q3 and the REPD register. Side effects: changes the web page (DOM).
- Why it exists in the grid workflow
- Domain stage upstream_foresight (path word 'repd'): 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: the REPD register.
- Inputs & outputs
- In: CSV file REPD_Publication_Q3_2026.csv; Excel file REPD_Publication_Q3_2026.xlsx; CSV file repd.csv. Out: web page.
- Downstream impact
- It is itself an entry point, published at https://globalgrid2050.com/automation/repd/tests/fixtures/detect_fetch/publication_2026q3.html.
- What it achieves
- Constructed fixture modelled on GOV.UK publication pages; not a captured page.
- Why it matters
- No other file in the estate was found to import it; it is an entry point.
- Application
- globalgrid2050 · automation/repd/tests/fixtures/detect_fetch · page
- Uses
- none found
- Used by
- none found
- Produces
- none declared
- Defines here
- none
- 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/repd/tests/fixtures/detect_fetch/publication_2026q3.html:1-21 · blob 24502e4b3f27be059e881add829a6993e00df416 · GitHub at these lines
- State
- achieves: comment · context: readme_derived · stage: output · effects: dom · domain stage: upstream_foresight · users: IPP_developer, ICP_grid_engineer · canonical: production_core
<!DOCTYPE html>
<html lang="en"><head><title>Renewable Energy Planning Database: quarterly extract - GOV.UK</title>
<script>var x = "REPD_Publication_Q9_2099.csv Last updated 1 January 2099";</script></head>
<body>
<!-- constructed fixture modelled on GOV.UK publication pages; not a captured page -->
<div class="gem-c-metadata"><dl>
<dt class="gem-c-metadata__term">From:</dt><dd><a href="/government/organisations/department-for-energy-security-and-net-zero">Department for Energy Security and Net Zero</a></dd>
<dt class="gem-c-metadata__term">Published</dt><dd>1 July 2014</dd>
<dt class="gem-c-metadata__term">Last updated</dt><dd>13 November 2026</dd>
</dl></div>
<section class="gem-c-attachment">
<h3 class="gem-c-attachment__title"><a class="govuk-link" href="https://assets.publishing.service.gov.uk/media/6af42ff0a4004bdbe54e54a5/REPD_Publication_Q3_2026.csv">Renewable Energy Planning Database (REPD): October 2026 (CSV)</a></h3>
<p class="gem-c-attachment__metadata"><span class="gem-c-attachment__attribute">CSV</span>, <span>4.98 MB</span></p>
<p><a class="govuk-link" href="/csv-preview/6af42ff0a4004bdbe54e54a5/REPD_Publication_Q3_2026.csv">View online</a></p>
</section>
<section class="gem-c-attachment">
<h3 class="gem-c-attachment__title"><a class="govuk-link" href="https://assets.publishing.service.gov.uk/media/6af42ffdb83b3cae0d0a21c9/REPD_Publication_Q3_2026.xlsx">Renewable Energy Planning Database (REPD): October 2026 (Excel)</a></h3>
<p class="gem-c-attachment__metadata"><span>MS Excel Spreadsheet</span>, <span>5.13 MB</span></p>
</section>
<p>Other links: <a href="https://www.data.gov.uk/dataset/repd.csv">data.gov.uk</a></p>
</body></html>Harness
A JavaScript module in the pages folder of GlobalGrid2050 that tests Renewable Energy Planning Database (REPD) grid batteries, REPD solar operational over 4MW and REPD master, using EPSG:27700 and SHA-256.
testglobalgrid2050 / automation/repd/tests/pages/harness.js · lines 1–441 of 441 · Entry point · pattern browserDecode (shared by 2 files)
- What it does
- It tests REPD grid batteries, REPD solar operational over 4MW, REPD master and the REPD register. Defines 21 functions or classes, including encodeCp1252, browserDecode, describeBytes, sha256, inlineScripts. Uses libraries fs, path, vm, crypto, papaparse. Side effects: changes the web page (DOM); reads or writes files; makes network requests; starts processes.
- Why it exists in the grid workflow
- Domain stage upstream_foresight (path word 'repd'): 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: the REPD register.
- Inputs & outputs
- In: CSV file repd.csv; CSV file repd-grid-batteries.csv; CSV file repd-solar-operational-over-4mw.csv; CSV file repd-solar-operational.csv; JSON file repd_master.json; command-line arguments. Out: interactive map. Coordinate systems named: OSGB36 / EPSG:27700 (British National Grid), WGS84 / EPSG:4326.
- Downstream impact
- Used directly by 2 files: Harness Test (globalgrid2050); Renewable Energy Planning Database (REPD) — Tests (globalgrid2050). Reached from 2 other entry points, including GlobalGrid2050 Project Signals Acid Workflow (globalgrid2050); Test Pages Harness (globalgrid2050).
- What it achieves
- It encodes cp1252 (`encodeCp1252`), describes bytes (`describeBytes`) and extracts papa script (`extractPapaScript`). REPD page harness: runs the real data-loading / row-processing JavaScript of the site pages that read the root REPD datasets, in Node, without a browser.
- Why it matters
- Checked by 2 tests, including Harness Test.
- Application
- globalgrid2050 · automation/repd/tests/pages · JavaScript module
- Uses
- fs, path, vm, crypto, papaparse, proj4
- Used by
- required by Harness Test (require), run by Renewable Energy Planning Database (REPD) — Tests (script call)
- Produces
- encodeCp1252, browserDecode, describeBytes, sha256, inlineScripts, extractPapaScript, extractAtlasLayers, makeSandbox, chain, cluster, $, el
- Defines here
- encodeCp1252, browserDecode, describeBytes, sha256, inlineScripts, extractPapaScript, extractAtlasLayers, makeSandbox, chain, cluster, $, el, runCsvPage, classify, analyseCsvPage, evalExpr, analyseAtlas, staleText, hashTree, main, toMarkdown
- 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/repd/tests/pages/harness.js:1-441 · blob 2f8e47bad04ce1fb0c3335a23c3a6c78dddcc7df · GitHub at these lines
- State
- achieves: comment · context: readme_derived · stage: output · effects: dom, filesystem, network, process · domain stage: upstream_foresight · users: IPP_developer, ICP_grid_engineer · canonical: production_core
#!/usr/bin/env node
/*
* REPD page harness: runs the real data-loading / row-processing JavaScript of the site pages
* that read the root REPD datasets, in Node, without a browser.
*
* node harness.js --pages <repo checkout> --set jan=<dir> --set oct=<dir> [--json out.json] [--md out.md]
*
* Each --set dir must hold repd.csv, repd-grid-batteries.csv, repd-solar-operational.csv,
* repd-solar-operational-over-4mw.csv and dist/repd_master.json (missing files are reported).
*
* CSV pages (Leaflet / MapLibre + PapaParse): the page's inline <script> that calls Papa.parse is
* extracted from the .md, '{{ site.baseurl }}' is replaced by '', and the script is executed in a vm
* context with the real papaparse 5.4.1 and proj4 2.11.0 and stubbed L / $ / maplibregl / document.
* Papa.parse(url, {download:true}) is served from the set's bytes decoded the way a browser XHR does
* for a text/csv response (UTF-8, BOM stripped, invalid bytes -> U+FFFD). Captured: every circle
* marker (lat/lng + popup), every DataTable row, every GeoJSON feature passed to setData.
*
* Atlas pages (repd_grid_atlasv3..v8): REPD layer definitions are extracted from index.html (every
* object literal whose url is /dist/repd_master.json) and their MapLibre filter expressions are
* evaluated against dist/repd_master.json parsed as UTF-8 JSON (what fetch().json() does).
*
* An independent classifier re-derives each CSV page's drop reasons; the harness asserts that its
* "would render" count equals the number of markers the page code actually produced.
*
* Read-only: sha256 of every page and dataset file is taken before and after; any change fails the run.
*/
'use strict';
const fs = require('fs');
const path = require('path');
const vm = require('vm');
const crypto = require('crypto');
const Papa = require('papaparse');
const proj4 = require('proj4');
const UK_BBOX = { lonMin: -9.0, lonMax: 2.5, latMin: 49.0, latMax: 61.0 };
const BAD_CHAR = '�';
// ---------------------------------------------------------------- page registry
const DEAD_OR_DONE = ['refused', 'abandoned', 'withdrawn', 'operational', 'decommissioned'];
const CSV_PAGES = [
{ file: 'repd-atlas.md', dataset: 'repd.csv', engine: 'leaflet', pre: () => true,
statusCol: 'Development Status' },
{ file: 'uk_grid-batteries.md', dataset: 'repd-grid-batteries.csv', engine: 'leaflet', pre: () => true },
{ file: 'uk_operating_large_solar_farms.md', dataset: 'repd-solar-operational-over-4mw.csv', engine: 'leaflet', pre: () => true },
{ file: 'repd-uk-operational-solar.md', dataset: 'repd-solar-operational.csv', engine: 'leaflet', pre: () => true },
{ file: 'uk_offshore_wind_atlas.md', dataset: 'repd.csv', engine: 'leaflet', pre: r => r['Technology Type'] === 'Wind Offshore' },
{ file: 'uk_onshore_wind_atlas.md', dataset: 'repd.csv', engine: 'leaflet', pre: r => r['Technology Type'] === 'Wind Onshore' },
{ file: 'uk_solar_in-planning-1-50mw.md', dataset: 'repd.csv', engine: 'leaflet',
pre: r => {
const cap = parseFloat(r['Installed Capacity (MWelec)']) || 0;
const st = (r['Development Status'] || 'Unknown').toLowerCase();
return (r['Technology Type'] || '').toLowerCase().includes('solar') &&
!DEAD_OR_DONE.some(w => st.includes(w)) && cap >= 1 && cap <= 50;
} },
{ file: 'repd_atlas_grid_model.md', dataset: 'repd.csv', engine: 'maplibre', pre: () => true },
];
const ATLAS_PAGES = ['repd_grid_atlasv3/index.html', 'repd_grid_atlasv4/index.html', 'repd_grid_atlasv5/index.html',
'repd_grid_atlasv6/index.html', 'repd_grid_atlasv7/index.html', 'repd_grid_atlasv8/index.html'];
const ATLAS_PROPS = ['name', 'capacity', 'status', 'tech', 'raw_tech', 'operator', 'mounting'];
const STATUS_COLOURS = ['operational', 'under construction', 'awaiting construction', 'consented',
'planning permission granted', 'planning approved', 'application submitted', 'pre-construction'];
const DATASETS = ['repd.csv', 'repd-grid-batteries.csv', 'repd-solar-operational.csv',
'repd-solar-operational-over-4mw.csv', 'dist/repd_master.json'];
// ---------------------------------------------------------------- encoding helpers
const CP1252_HIGH = { 0x20AC: 0x80, 0x201A: 0x82, 0x0192: 0x83, 0x201E: 0x84, 0x2026: 0x85, 0x2020: 0x86, 0x2021: 0x87,
0x02C6: 0x88, 0x2030: 0x89, 0x0160: 0x8A, 0x2039: 0x8B, 0x0152: 0x8C, 0x017D: 0x8E, 0x2018: 0x91, 0x2019: 0x92,
0x201C: 0x93, 0x201D: 0x94, 0x2022: 0x95, 0x2013: 0x96, 0x2014: 0x97, 0x02DC: 0x98, 0x2122: 0x99, 0x0161: 0x9A,
0x203A: 0x9B, 0x0153: 0x9C, 0x017E: 0x9E, 0x0178: 0x9F };
function encodeCp1252(text) {
const out = [];
let unencodable = 0;
for (const ch of text) {
const c = ch.codePointAt(0);
if (c < 0x80 || (c >= 0xA0 && c <= 0xFF)) out.push(c);
else if (CP1252_HIGH[c] !== undefined) out.push(CP1252_HIGH[c]);
else { out.push(0x3F); unencodable++; }
}
return { bytes: Buffer.from(out), unencodable };
}
/** What a browser XHR/fetch gives responseText for text/csv (charset utf-8 or absent): UTF-8, BOM stripped. */
function browserDecode(bytes, opts = {}) {
return new TextDecoder('utf-8', { fatal: false, ignoreBOM: !!opts.keepBOM }).decode(bytes);
}
function describeBytes(bytes) {
let utf8 = true;
try { new TextDecoder('utf-8', { fatal: true }).decode(bytes); } catch (e) { utf8 = false; }
return { size: bytes.length, bom: bytes[0] === 0xEF && bytes[1] === 0xBB && bytes[2] === 0xBF, valid_utf8: utf8,
crlf: bytes.includes(Buffer.from('\r\n')) };
}
const sha256 = b => crypto.createHash('sha256').update(b).digest('hex');
// ---------------------------------------------------------------- extraction
function inlineScripts(html) {
const out = [];
const re = /<script(\s[^>]*)?>([\s\S]*?)<\/script>/gi;
let m;
while ((m = re.exec(html))) if (!/\bsrc\s*=/.test(m[1] || '')) out.push(m[2]);
return out;
}
function extractPapaScript(pageText) {
const s = inlineScripts(pageText).filter(x => x.includes('Papa.parse'));
if (s.length !== 1) throw new Error(`expected 1 inline script with Papa.parse, found ${s.length}`);
return s[0].replace(/\{\{\s*site\.baseurl\s*\}\}/g, '');
}
function extractAtlasLayers(html) {
const layers = new Map();
for (const line of html.split('\n')) {
if (!line.includes('/dist/repd_master.json')) continue;
const m = line.match(/\{\s*id:[\s\S]*\}/);
if (!m) continue;
const obj = vm.runInNewContext('(' + m[0] + ')');
if (!layers.has(obj.id)) layers.set(obj.id, obj);
}
return [...layers.values()];
}
// ---------------------------------------------------------------- stubs for CSV pages
function makeSandbox(datasetText) {
const cap = { markers: [], table: null, tableCreated: false, geojson: null, papa: null, handlers: {}, fetches: [],
consoleErrors: [] };
const chain = (extra = {}) => { const o = Object.assign({}, extra); o.addTo = () => o; return o; };
const mapObj = { setView() { return mapObj; }, addLayer() { return mapObj; }, on(ev, fn) { cap.handlers['map:' + ev] = fn; return mapObj; },
getZoom: () => 6 };
const cluster = () => ({ addLayer(m) { cap.markers.push(m); }, clearLayers() { cap.markers = []; } });
const L = {
map: () => mapObj, tileLayer: () => chain(), markerClusterGroup: cluster, layerGroup: () => chain(),
control: { layers: () => chain() }, geoJSON: () => chain(),
circleMarker: (latlng, options) => {
const m = { latlng, options, popup: null };
m.bindPopup = h => { m.popup = h; return m; };
m.setRadius = r => { m.radius = r; };
return m;
},
};
const tableApi = { clear() { cap.table = []; return tableApi; }, rows: { add(d) { cap.table = d.slice(); return tableApi; } },
draw() { return tableApi; } };
const $ = sel => ({
on(ev, fn) { cap.handlers[sel + ':' + ev] = fn; return this; }, val() { return 0; }, text() { return this; },
DataTable(cfg) { cap.tableCreated = true; cap.table = (cfg.data || []).slice(); return tableApi; },
});
$.fn = { DataTable: { isDataTable: () => cap.tableCreated } };
const sources = {};
const maplibregl = { Map: function () {
this.on = (ev, fn) => { cap.handlers['map:' + ev] = fn; };
this.addSource = (id, def) => { sources[id] = { def, setData(d) { cap.geojson = d; } }; };
this.addLayer = () => {}; this.getSource = id => sources[id];
} };
const el = () => ({ innerHTML: '', innerText: '' });
const papaStub = {
parse(url, cfg) {
if (!cfg || !cfg.download) return Papa.parse(url, cfg);
cap.papa = { url, config: Object.assign({}, cfg, { complete: undefined }) };
const res = Papa.parse(datasetText, Object.assign({}, cfg, { download: false, complete: undefined }));
cap.papa.results = res;
cfg.complete(res);
},
};
const ctx = {
L, $, jQuery: $, Papa: papaStub, proj4, maplibregl, console: { log() {}, warn() {}, error: (...a) => cap.consoleErrors.push(a.join(' ')) },
document: { getElementById: el, querySelector: () => null }, setInterval: () => 0, setTimeout: () => 0,
fetch: url => { cap.fetches.push(url); return Promise.resolve({ ok: false, json: () => Promise.resolve({ type: 'FeatureCollection', features: [] }) }); },
Date, Math, JSON, parseFloat, parseInt, Number, String, Array, Object, isNaN, isFinite,
};
ctx.window = ctx;
return { ctx: vm.createContext(ctx), cap };
}
function runCsvPage(page, script, datasetText) {
const { ctx, cap } = makeSandbox(datasetText);
vm.runInContext(script, ctx, { filename: page.file, timeout: 120000 });
if (cap.handlers['map:load']) cap.handlers['map:load'](); // MapLibre pages parse inside map.on('load')
return cap;
}
// ---------------------------------------------------------------- independent classifier (CSV pages)
function classify(page, rows, fields) {
const r = { input_rows: rows.length, after_page_filter: 0, would_render: 0,
dropped: { blank_coordinate: 0, non_numeric_coordinate: 0, zero_coordinate: 0, projection_failed: 0 },
rendered_outside_uk_bbox: [], rendered_blank_capacity_as_0: 0, rendered_non_numeric_capacity_as_0: 0,
rendered_capacity_with_comma: 0, rendered_status_unknown: 0, rendered_with_U_FFFD: 0, U_FFFD_examples: [],
dropped_examples: [] };
for (const row of rows) {
if (!page.pre(row)) continue;
r.after_page_filter++;
const xs = row['X-coordinate'], ys = row['Y-coordinate'];
const x = parseFloat(xs), y = parseFloat(ys);
let reason = null;
if (!(x && y)) {
if ((xs || '').trim() === '' || (ys || '').trim() === '') reason = 'blank_coordinate';
else if (Number.isNaN(x) || Number.isNaN(y)) reason = 'non_numeric_coordinate';
else reason = 'zero_coordinate';
}
let lonlat = null;
if (!reason) {
try { lonlat = proj4('EPSG:27700', 'WGS84', [x, y]); if (!lonlat.every(Number.isFinite)) reason = 'projection_failed'; }
catch (e) { reason = 'projection_failed'; }
}
if (reason) {
r.dropped[reason]++;
if (r.dropped_examples.length < 8) r.dropped_examples.push({ ref: row['Ref ID'], name: row['Site Name'], reason, x: xs, y: ys });
continue;
}
r.would_render++;
const [lon, lat] = lonlat;
if (!(lon > UK_BBOX.lonMin && lon < UK_BBOX.lonMax && lat > UK_BBOX.latMin && lat < UK_BBOX.latMax))
r.rendered_outside_uk_bbox.push({ ref: row['Ref ID'], name: row['Site Name'], x: xs, y: ys, lon: +lon.toFixed(4), lat: +lat.toFixed(4) });
const capRaw = (row['Installed Capacity (MWelec)'] || '').trim();
if (capRaw === '') r.rendered_blank_capacity_as_0++;
else if (Number.isNaN(parseFloat(capRaw))) r.rendered_non_numeric_capacity_as_0++;
else if (capRaw.includes(',')) r.rendered_capacity_with_comma++;
if (!row['Development Status']) r.rendered_status_unknown++;
const shown = ['Site Name', 'Technology Type', 'County', 'Development Status', 'Operator (or Applicant)'].map(k => row[k] || '').join(' | ');
if (shown.includes(BAD_CHAR)) {
r.rendered_with_U_FFFD++;
if (r.U_FFFD_examples.length < 5) r.U_FFFD_examples.push({ ref: row['Ref ID'], shown });
}
}
return r;
}
const PAGE_COLUMNS = ['Site Name', 'Technology Type', 'Installed Capacity (MWelec)', 'Development Status', 'County',
'X-coordinate', 'Y-coordinate'];
function analyseCsvPage(page, script, bytes, opts = {}) {
const text = browserDecode(bytes, opts);
const cap = runCsvPage(page, script, text);
const res = cap.papa.results;
const fields = res.meta.fields || [];
const errs = {};
for (const e of res.errors) errs[e.code] = (errs[e.code] || 0) + 1;
const cls = classify(page, res.data, fields);
const rendered = page.engine === 'maplibre' ? (cap.geojson ? cap.geojson.features.length : 0) : cap.markers.length;
const out = {
dataset_bytes: describeBytes(bytes), decoded_has_U_FFFD: (text.match(/�/g) || []).length,
first_header_key: JSON.stringify(fields[0]), parsed_rows: res.data.length, papa_errors: errs,
papa_error_examples: res.errors.slice(0, 5).map(e => ({ code: e.code, row: e.row, message: e.message })),
missing_page_columns: PAGE_COLUMNS.filter(c => !fields.includes(c)),
rendered_markers: rendered, table_rows: cap.table ? cap.table.length : (page.engine === 'maplibre' ? 'not populated by page' : 0),
classifier: cls, classifier_agrees: cls.would_render === rendered,
status_values_rendered: {},
console_errors: cap.consoleErrors.slice(0, 5),
};
// Status split as the page colours it (Operational -> cyan, else orange); uses long 'Development Status'.
for (const row of res.data) if (page.pre(row)) {
const s = row['Development Status'] || 'Unknown';
out.status_values_rendered[s] = (out.status_values_rendered[s] || 0) + 1;
}
return out;
}
// ---------------------------------------------------------------- atlas pages
function evalExpr(e, p) {
if (!Array.isArray(e)) return e;
const [op, ...a] = e;
switch (op) {
case 'get': return p[a[0]] === undefined ? null : p[a[0]];
case 'has': return Object.prototype.hasOwnProperty.call(p, a[0]);
case '==': return evalExpr(a[0], p) === evalExpr(a[1], p);
case '!=': return evalExpr(a[0], p) !== evalExpr(a[1], p);
case '!': return !evalExpr(a[0], p);
case 'all': return a.every(x => evalExpr(x, p));
case 'any': return a.some(x => evalExpr(x, p));
case 'in': { const n = evalExpr(a[0], p), h = evalExpr(a[1], p); return h == null ? false : String(h).includes(n); }
case 'coalesce': for (const x of a) { const v = evalExpr(x, p); if (v != null) return v; } return null;
default: throw new Error('unsupported filter op ' + op);
}
}
function analyseAtlas(file, html, bytes) {
const layers = extractAtlasLayers(html);
if (!bytes) return { layers: layers.map(l => l.id), error: 'dist/repd_master.json missing' };
const gj = JSON.parse(new TextDecoder('utf-8').decode(bytes)); // fetch().json(): UTF-8, BOM stripped
const feats = Array.isArray(gj.features) ? gj.features : [];
const per = {};
for (const l of layers) per[l.id] = l.filter ? feats.filter(f => evalExpr(l.filter, f.properties || {})).length : feats.length;
const drawnTech = new Set(layers.filter(l => l.filter).flatMap(l => JSON.stringify(l.filter).match(/"tech"\],"([a-z_]+)"/g) || [])
.map(s => s.match(/"([a-z_]+)"$/)[1]));
const res = { features: feats.length, layers: per, undrawn_tech: {}, bad_geometry: 0, outside_uk_bbox: 0,
missing_props: {}, unknown_status: {}, capacity_zero: 0, name_U_FFFD: 0, top_level_keys: Object.keys(gj) };
for (const f of feats) {
const p = f.properties || {};
if (!drawnTech.has(p.tech)) res.undrawn_tech[p.tech] = (res.undrawn_tech[p.tech] || 0) + 1;
const c = f.geometry && f.geometry.coordinates;
if (!f.geometry || f.geometry.type !== 'Point' || !Array.isArray(c) || !c.every(Number.isFinite)) res.bad_geometry++;
else if (!(c[0] > UK_BBOX.lonMin && c[0] < UK_BBOX.lonMax && c[1] > UK_BBOX.latMin && c[1] < UK_BBOX.latMax)) res.outside_uk_bbox++;
for (const k of ATLAS_PROPS) if (!(k in p)) res.missing_props[k] = (res.missing_props[k] || 0) + 1;
const s = String(p.status ?? '').trim().toLowerCase();
if (!STATUS_COLOURS.includes(s)) res.unknown_status[s] = (res.unknown_status[s] || 0) + 1;
if (!(Number(p.capacity) > 0)) res.capacity_zero++;
if (String(p.name || '').includes(BAD_CHAR) || String(p.operator || '').includes(BAD_CHAR)) res.name_U_FFFD++;
}
return res;
}
// ---------------------------------------------------------------- stale text scan
function staleText(text) {
const hits = [];
const re = /(\b(?:jan|feb|mar|apr|may|jun|jul|aug|sep|oct|nov|dec)[a-z]*\.? 20\d\d\b|\bQ[1-4][ _-]?20\d\d\b|\b20\d\d-\d\d(?:-\d\d)?\b|\b\d{1,2}\/\d{1,2}\/20\d\d\b|\b\d{1,3}(?:,\d{3})+ (?:projects|sites|assets|records|features|rows)\b|\b\d{3,} (?:projects|sites|assets|records|features|rows)\b|last (?:updated|sync)[^<"\n]{0,40}|data as (?:of|at)[^<"\n]{0,30})/gi;
text.split('\n').forEach((line, i) => {
let m;
while ((m = re.exec(line))) hits.push({ line: i + 1, text: m[0] });
});
return hits;
}
// ---------------------------------------------------------------- main
function hashTree(files) { const h = {}; for (const f of files) if (fs.existsSync(f)) h[f] = sha256(fs.readFileSync(f)); return h; }
function main(argv) {
const args = { sets: [] };
for (let i = 2; i < argv.length; i++) {
const a = argv[i];
if (a === '--pages') args.pages = argv[++i];
else if (a === '--set') { const [k, v] = argv[++i].split('='); args.sets.push({ name: k, dir: v }); }
else if (a === '--json') args.json = argv[++i];
else if (a === '--md') args.md = argv[++i];
else if (a === '--no-encoding-variants') args.noVariants = true;
else throw new Error('unknown arg ' + a);
}
if (!args.pages || !args.sets.length) throw new Error('usage: --pages DIR --set name=DIR [--set ...] [--json F] [--md F]');
const pageFiles = [...CSV_PAGES.map(p => p.file), ...ATLAS_PAGES].map(f => path.join(args.pages, f));
const dataFiles = args.sets.flatMap(s => DATASETS.map(d => path.join(s.dir, d)));
const before = hashTree([...pageFiles, ...dataFiles]);
const report = { generated_utc: new Date().toISOString(), pages_root: args.pages, sets: {}, csv_pages: {}, atlas_pages: {},
stale_text: {}, read_only_check: null };
for (const s of args.sets) {
report.sets[s.name] = { dir: s.dir, files: {} };
for (const d of DATASETS) { const f = path.join(s.dir, d);
report.sets[s.name].files[d] = fs.existsSync(f) ? Object.assign({ sha256: before[f] }, describeBytes(fs.readFileSync(f))) : 'MISSING'; }
}
for (const page of CSV_PAGES) {
const text = fs.readFileSync(path.join(args.pages, page.file), 'utf8');
report.stale_text[page.file] = staleText(text);
const script = extractPapaScript(text);
const entry = { dataset: page.dataset, engine: page.engine, runs: {} };
for (const s of args.sets) {
const f = path.join(s.dir, page.dataset);
if (!fs.existsSync(f)) { entry.runs[s.name] = { error: 'dataset missing' }; continue; }
const bytes = fs.readFileSync(f);
entry.runs[s.name] = analyseCsvPage(page, script, bytes);
if (!args.noVariants) {
// Same content in the other encodings the pipeline could emit.
const txt = new TextDecoder('utf-8', { fatal: false }).decode(bytes);
const valid = describeBytes(bytes).valid_utf8;
if (valid) {
const cp = encodeCp1252(txt.replace(/^/, ''));
const v = analyseCsvPage(page, script, cp.bytes);
entry.runs[s.name + '@cp1252'] = { rendered_markers: v.rendered_markers, table_rows: v.table_rows, parsed_rows: v.parsed_rows,
rendered_with_U_FFFD: v.classifier.rendered_with_U_FFFD, U_FFFD_examples: v.classifier.U_FFFD_examples, unencodable_chars: cp.unencodable };
const bom = Buffer.concat([Buffer.from([0xEF, 0xBB, 0xBF]), Buffer.from(txt.replace(/^/, ''), 'utf8')]);
const b1 = analyseCsvPage(page, script, bom);
const b2 = analyseCsvPage(page, script, bom, { keepBOM: true });
entry.runs[s.name + '@utf8bom'] = { rendered_markers: b1.rendered_markers, first_header_key: b1.first_header_key,
if_bom_not_stripped: { rendered_markers: b2.rendered_markers, first_header_key: b2.first_header_key, missing_page_columns: b2.missing_page_columns } };
} else {
// cp1252 source: show what a correct UTF-8 transcode would render instead.
const fixed = Buffer.from(new TextDecoder('windows-1252').decode(bytes), 'utf8');
const v = analyseCsvPage(page, script, fixed);
entry.runs[s.name + '@as_utf8'] = { rendered_markers: v.rendered_markers, table_rows: v.table_rows,
rendered_with_U_FFFD: v.classifier.rendered_with_U_FFFD };
}
}
}
report.csv_pages[page.file] = entry;
}
for (const file of ATLAS_PAGES) {
const html = fs.readFileSync(path.join(args.pages, file), 'utf8');
report.stale_text[file] = staleText(html);
const entry = { runs: {} };
for (const s of args.sets) {
const f = path.join(s.dir, 'dist/repd_master.json');
entry.runs[s.name] = analyseAtlas(file, html, fs.existsSync(f) ? fs.readFileSync(f) : null);
}
report.atlas_pages[file] = entry;
}
const after = hashTree([...pageFiles, ...dataFiles]);
const changed = Object.keys(before).filter(k => before[k] !== after[k]);
report.read_only_check = { files_hashed: Object.keys(before).length, changed };
if (args.json) fs.writeFileSync(args.json, JSON.stringify(report, null, 2) + '\n');
const md = toMarkdown(report, args.sets.map(s => s.name));
if (args.md) fs.writeFileSync(args.md, md);
process.stdout.write(md);
const disagreements = Object.entries(report.csv_pages).flatMap(([f, e]) =>
Object.entries(e.runs).filter(([, r]) => r.classifier_agrees === false).map(([n]) => `${f}@${n}`));
if (changed.length) { console.error('READ-ONLY VIOLATION: ' + changed.join(', ')); process.exit(2); }
if (disagreements.length) { console.error('CLASSIFIER DISAGREES WITH PAGE: ' + disagreements.join(', ')); process.exit(3); }
}
function toMarkdown(rep, names) {
const L = [];
L.push('# REPD page harness results', '', `generated ${rep.generated_utc}`, '');
L.push('## Datasets', '', '| set | file | bytes | BOM | valid UTF-8 | CRLF |', '|---|---|---|---|---|---|');
for (const n of names) for (const [d, i] of Object.entries(rep.sets[n].files))
L.push(i === 'MISSING' ? `| ${n} | ${d} | MISSING | | | |` : `| ${n} | ${d} | ${i.size} | ${i.bom} | ${i.valid_utf8} | ${i.crlf} |`);
L.push('', '## CSV pages', '');
const hdr = ['page', 'dataset', ...names.flatMap(n => [`${n} parsed`, `${n} markers`, `${n} table`, `${n} dropped (blank/non-num/zero)`, `${n} outside UK`, `${n} blank cap shown 0`, `${n} U+FFFD rows`])];
L.push('| ' + hdr.join(' | ') + ' |', '|' + hdr.map(() => '---').join('|') + '|');
for (const [f, e] of Object.entries(rep.csv_pages)) {
const cells = [f, e.dataset];
for (const n of names) {
const r = e.runs[n];
if (!r || r.error) { cells.push(r ? r.error : '-', '', '', '', '', '', ''); continue; }
const c = r.classifier, d = c.dropped;
cells.push(r.parsed_rows, r.rendered_markers, r.table_rows, `${d.blank_coordinate}/${d.non_numeric_coordinate}/${d.zero_coordinate}`,
c.rendered_outside_uk_bbox.length, c.rendered_blank_capacity_as_0, c.rendered_with_U_FFFD);
}
L.push('| ' + cells.join(' | ') + ' |');
}
L.push('', '### Encoding variants (same content re-encoded)', '', '| page | variant | markers | U+FFFD rows | note |', '|---|---|---|---|---|');
for (const [f, e] of Object.entries(rep.csv_pages)) for (const [n, r] of Object.entries(e.runs)) {
if (!n.includes('@')) continue;
if (n.endsWith('@cp1252')) L.push(`| ${f} | ${n} | ${r.rendered_markers} | ${r.rendered_with_U_FFFD} | unencodable chars ${r.unencodable_chars} |`);
else if (n.endsWith('@utf8bom')) L.push(`| ${f} | ${n} | ${r.rendered_markers} | 0 | first key ${r.first_header_key}; if BOM kept: markers ${r.if_bom_not_stripped.rendered_markers}, key ${r.if_bom_not_stripped.first_header_key}, missing cols ${JSON.stringify(r.if_bom_not_stripped.missing_page_columns)} |`);
else L.push(`| ${f} | ${n} | ${r.rendered_markers} | ${r.rendered_with_U_FFFD} | transcoded to UTF-8 |`);
}
L.push('', '### Papa errors / missing columns', '');
for (const [f, e] of Object.entries(rep.csv_pages)) for (const n of names) {
const r = e.runs[n]; if (!r || r.error) continue;
if (Object.keys(r.papa_errors).length || r.missing_page_columns.length || r.classifier.rendered_outside_uk_bbox.length)
L.push(`- ${f} [${n}]: papa_errors=${JSON.stringify(r.papa_errors)} missing=${JSON.stringify(r.missing_page_columns)} outside_uk=${JSON.stringify(r.classifier.rendered_outside_uk_bbox.slice(0, 4))}`);
}
L.push('', '## Atlas pages (dist/repd_master.json)', '');
for (const [f, e] of Object.entries(rep.atlas_pages)) {
L.push(`### ${f}`, '', '| layer | ' + names.join(' | ') + ' |', '|---|' + names.map(() => '---').join('|') + '|');
const ids = [...new Set(names.flatMap(n => Object.keys((e.runs[n] || {}).layers || {})))];
for (const id of ids) L.push(`| ${id} | ` + names.map(n => { const v = (e.runs[n].layers || {})[id]; return v === undefined ? '-' : v; }).join(' | ') + ' |');
for (const n of names) { const r = e.runs[n];
if (r.error) { L.push(`| features (${n}) | ${r.error} | |`); continue; }
L.push(`| features (${n}) | ${r.features} | bad geom ${r.bad_geometry}, outside UK ${r.outside_uk_bbox}, undrawn ${JSON.stringify(r.undrawn_tech)}, missing props ${JSON.stringify(r.missing_props)}, unknown status ${JSON.stringify(r.unknown_status)}, capacity<=0 ${r.capacity_zero}, U+FFFD ${r.name_U_FFFD} |`); }
L.push('');
}
L.push('## Hard-coded date / count text', '');
for (const [f, h] of Object.entries(rep.stale_text)) L.push(`- ${f}: ${h.length ? h.map(x => `L${x.line} "${x.text}"`).join('; ') : 'none'}`);
L.push('', `Read-only check: ${rep.read_only_check.files_hashed} files hashed before/after, changed: ${JSON.stringify(rep.read_only_check.changed)}`, '');
return L.join('\n');
}
module.exports = { extractPapaScript, extractAtlasLayers, runCsvPage, analyseCsvPage, analyseAtlas, classify, evalExpr,
encodeCp1252, browserDecode, staleText, CSV_PAGES, ATLAS_PAGES };
if (require.main === module) main(process.argv);Harness Test
A test in the pages folder of GlobalGrid2050 that checks Harness.
testglobalgrid2050 / automation/repd/tests/pages/harness.test.js · lines 1–86 of 86 · Entry point
- What it does
- It tests solar, battery storage (BESS) and the REPD register. Uses libraries node:test, node:assert, fs, path. Side effects: reads or writes files.
- Why it exists in the grid workflow
- Domain stage upstream_foresight (path word 'repd'): 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, battery storage (BESS), the REPD register.
- Inputs & outputs
- In: CSV file mini.csv; CSV file repd.csv; JSON file repd_master.json.
- Downstream impact
- Used directly by 1 file: Test Pages Harness (globalgrid2050). Reached from 1 other entry point, including GlobalGrid2050 Project Signals Acid Workflow (globalgrid2050).
- What it achieves
- No statement of purpose was found in the file, a README line naming it, or its name.
- Why it matters
- Checked by Test Pages Harness.
- Application
- globalgrid2050 · automation/repd/tests/pages · test
- Uses
- Harness (require), node:test, node:assert, fs, path
- Used by
- run by Test Pages Harness (script call)
- Produces
- none declared
- Defines here
- none
- 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/repd/tests/pages/harness.test.js:1-86 · blob 50631bdf8a628c7448e8277c59c1faa52b066855 · GitHub at these lines
- State
- achieves: name · context: readme_derived · stage: ingest · effects: filesystem · domain stage: upstream_foresight · users: IPP_developer, ICP_grid_engineer · canonical: production_core
// node --test harness.test.js (synthetic fixtures only; real pages are exercised if REPD_PAGES_ROOT is set)
'use strict';
const test = require('node:test');
const assert = require('node:assert');
const fs = require('fs');
const path = require('path');
const H = require('./harness.js');
const FIX = path.join(__dirname, 'fixtures');
const csvBytes = fs.readFileSync(path.join(FIX, 'mini.csv'));
const pageText = fs.readFileSync(path.join(FIX, 'mini_page.md'), 'utf8');
const ALL = { file: 'mini_page.md', engine: 'leaflet', pre: () => true };
test('extracts the single inline Papa script and strips the Liquid baseurl', () => {
const s = H.extractPapaScript(pageText);
assert.ok(s.includes("Papa.parse('/repd.csv'"));
assert.ok(!s.includes('site.baseurl'));
});
test('runs page code: markers and table rows captured, drops classified', () => {
const r = H.analyseCsvPage(ALL, H.extractPapaScript(pageText), csvBytes);
assert.strictEqual(r.parsed_rows, 7);
assert.strictEqual(r.rendered_markers, 4); // rows 1,2,6,7
assert.strictEqual(r.table_rows, 4);
assert.ok(r.classifier_agrees);
assert.deepStrictEqual(r.classifier.dropped, { blank_coordinate: 1, non_numeric_coordinate: 1, zero_coordinate: 1, projection_failed: 0 });
assert.strictEqual(r.classifier.rendered_blank_capacity_as_0, 1);
assert.strictEqual(r.classifier.rendered_outside_uk_bbox.length, 1); // ref 6, lon 2.55
assert.deepStrictEqual(r.missing_page_columns, []);
});
test('cp1252 bytes decoded as UTF-8 show U+FFFD; BOM is invisible to the page', () => {
const script = H.extractPapaScript(pageText);
const cp = H.encodeCp1252(new TextDecoder().decode(csvBytes));
const r = H.analyseCsvPage(ALL, script, cp.bytes);
assert.strictEqual(r.rendered_markers, 4);
assert.strictEqual(r.classifier.rendered_with_U_FFFD, 1); // Bishop's Wind (U+2019 -> 0x92 -> U+FFFD)
const bom = Buffer.concat([Buffer.from([0xEF, 0xBB, 0xBF]), csvBytes]);
for (const keepBOM of [false, true]) {
const b = H.analyseCsvPage(ALL, script, bom, { keepBOM });
assert.strictEqual(b.first_header_key, JSON.stringify('Old Ref ID'));
assert.strictEqual(b.rendered_markers, 4);
}
});
test('renamed column is reported missing and yields zero markers', () => {
const renamed = Buffer.from(new TextDecoder().decode(csvBytes).replace('X-coordinate', 'X Coordinate'));
const r = H.analyseCsvPage(ALL, H.extractPapaScript(pageText), renamed);
assert.deepStrictEqual(r.missing_page_columns, ['X-coordinate']);
assert.strictEqual(r.rendered_markers, 0);
});
test('MapLibre filter evaluator and atlas layer extraction', () => {
const p = { tech: 'solar', status: 'operational', raw_tech: 'Wind Onshore', capacity: null };
assert.ok(H.evalExpr(['all', ['==', ['get', 'tech'], 'solar'], ['==', ['get', 'status'], 'operational']], p));
assert.ok(!H.evalExpr(['==', ['get', 'tech'], 'bess'], p));
assert.strictEqual(H.evalExpr(['coalesce', ['get', 'capacity'], 0], p), 0);
const html = '{ id: "solar", label: "S", url: "/dist/repd_master.json", filter: [\'==\', [\'get\', \'tech\'], \'solar\'], preload: false },\n' +
'{ id: "bess", url: "/dist/repd_master.json", filter: [\'==\', [\'get\', \'tech\'], \'bess\'] }';
const layers = H.extractAtlasLayers(html);
assert.deepStrictEqual(layers.map(l => l.id), ['solar', 'bess']);
const gj = { type: 'FeatureCollection', features: [
{ type: 'Feature', properties: { name: 'a', capacity: 5, status: 'operational', tech: 'solar', raw_tech: 'Solar Photovoltaics', operator: 'X', mounting: 'ground' }, geometry: { type: 'Point', coordinates: [-1, 52] } },
{ type: 'Feature', properties: { name: 'b', capacity: 0, status: 'revised', tech: 'other' }, geometry: { type: 'Point', coordinates: [5, 52] } }] };
const r = H.analyseAtlas('x', html, Buffer.from(JSON.stringify(gj)));
assert.deepStrictEqual(r.layers, { solar: 1, bess: 0 });
assert.deepStrictEqual(r.undrawn_tech, { other: 1 });
assert.strictEqual(r.outside_uk_bbox, 1);
assert.strictEqual(r.capacity_zero, 1);
assert.deepStrictEqual(r.unknown_status, { revised: 1 });
});
test('stale text scanner finds dates and counts', () => {
const hits = H.staleText('Data as of January 2026<br>1,234 projects\nnothing here');
assert.deepStrictEqual(hits.map(h => h.text), ['Data as of January 2026', '1,234 projects']);
});
const ROOT = process.env.REPD_PAGES_ROOT;
test('every real CSV page extracts and runs on the synthetic CSV', { skip: !ROOT && 'REPD_PAGES_ROOT not set' }, () => {
for (const page of H.CSV_PAGES) {
const s = H.extractPapaScript(fs.readFileSync(path.join(ROOT, page.file), 'utf8'));
const r = H.analyseCsvPage(page, s, csvBytes);
assert.ok(r.classifier_agrees, page.file);
assert.strictEqual(r.parsed_rows, 7, page.file);
}
});