Model/backend/documents_parser/tests/test_summary_pdf_mapper_chain.py
Khalim Conn-Kowlessar ccef01bf27 Slice 99c: Elmhurst mapper — RR gables external for flats + SO wall code
Cert 9501 worksheet line (29a) lodges both RR gable walls (13.50 +
15.95 m²) as EXTERNAL walls at U=1.7 (the main-wall U for age B
Solid Brick), contributing +50.07 W/K on top of the 168.74 W/K main-
wall HLC for a (29a) total of 218.81 W/K. Two mapper gaps blocked
this:

1. The Summary mapper defaulted un-typed RR gable walls
   (`surface.gable_type=None`) to `gable_wall` (party, U=0.25 per
   RdSAP Table 4 row 2). For flats with RR — top-floor dwellings
   that sit at the end of a building block with no neighbour above
   — the gable walls are exposed external, not party. Threading
   `is_flat=property_type.lower()=='flat'` through
   `_map_elmhurst_building_parts` → `_map_elmhurst_room_in_roof` →
   `_map_elmhurst_rir_surface` switches the default for un-typed
   gables on flats to `gable_wall_external` (cascade falls through
   to main-wall U `uw`).

2. The Elmhurst wall-construction code map was missing "SO Solid
   Brick" (newer Elmhurst PDF variant; the cohort certs lodge "SB
   Solid Brick"). Cert 9501's main wall fell through to
   wall_construction=None → cascade uw=1.5 (Table-18 unknown-cons
   age-B default) instead of 1.7 (Table-18 solid-brick age-B).
   Added "SO": 3 alongside "SB": 3 — same SAP10 mapping.

Joint effect on cert 9501 Summary path:
- walls HLC 148.89 → 218.81 (exact worksheet match)
- party_walls HLC 7.36 → 0.00 (gables no longer route to party)
- (37) total HLC 229.71 → 296.68 (exact worksheet match)

Cohort regression check: 259/0 mapper-chain + extractor + golden
tests pass. Houses keep the historical un-typed-gable → party
default. Houses lodging "SO" instead of "SB" now also pick up the
correct solid-brick U-value.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-06-01 16:28:45 +00:00

855 lines
38 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

"""End-to-end validation for the Elmhurst Summary→EpcPropertyData chain.
The 6 Elmhurst worksheet fixtures in `domain.sap10_calculator.worksheet.tests`
build their `EpcPropertyData` synthetically — they validate the
calculator + cascade in isolation from the mapper. This file pins
the OTHER half of the chain: `from_elmhurst_site_notes` must produce
a calculator-equivalent `EpcPropertyData` when fed the Summary PDF
the worksheet was generated from. Together with the worksheet
cascade tests, this closes the loop: extractor + mapper + cascade
+ calculator validated end-to-end against the authoritative
Elmhurst documents.
Status: GREEN. For cert U985-0001-000474, this pipeline produces an
unrounded SAP within 0.5 of the worksheet PDF's `62.2584` (line 257).
The cascade itself reproduces Elmhurst's calculator exactly on
hand-built inputs (handbuilt → 62.2584 to 4 d.p.); the remaining
sub-half-point gap from the mapped path is non-load-bearing field
drift (e.g. central_heating_pump_age the Summary PDF doesn't lodge).
Preprocessing: the existing `ElmhurstSiteNotesExtractor` was written
against Textract-style output (label\\nvalue pairs in spatial
reading order). We don't have Textract in the test environment, so
this helper converts `pdftotext -layout` output (label-whitespace-
value on a single line) into the Textract-style sequence the
extractor expects. Test-only preprocessing; production runs through
Textract directly.
"""
from __future__ import annotations
import dataclasses
import json
import re
import subprocess
from pathlib import Path
from typing import cast
from backend.documents_parser.elmhurst_extractor import ElmhurstSiteNotesExtractor
from datatypes.epc.domain.mapper import EpcPropertyDataMapper
from domain.sap10_calculator.calculator import calculate_sap_from_inputs
from domain.sap10_calculator.rdsap.cert_to_inputs import SAP_10_2_SPEC_PRICES, cert_to_inputs
from domain.sap10_calculator.worksheet.tests import (
_elmhurst_worksheet_000474 as _w000474,
_elmhurst_worksheet_000477 as _w000477,
_elmhurst_worksheet_000480 as _w000480,
_elmhurst_worksheet_000487 as _w000487,
_elmhurst_worksheet_000490 as _w000490,
_elmhurst_worksheet_000516 as _w000516,
)
_FIXTURES = Path(__file__).parent / "fixtures"
_SUMMARY_000474_PDF = _FIXTURES / "Summary_000474.pdf"
_SUMMARY_000477_PDF = _FIXTURES / "Summary_000477.pdf"
_SUMMARY_000480_PDF = _FIXTURES / "Summary_000480.pdf"
_SUMMARY_000487_PDF = _FIXTURES / "Summary_000487.pdf"
_SUMMARY_000490_PDF = _FIXTURES / "Summary_000490.pdf"
_SUMMARY_000516_PDF = _FIXTURES / "Summary_000516.pdf"
_SUMMARY_001479_PDF = _FIXTURES / "Summary_001479.pdf"
_SUMMARY_000897_PDF = _FIXTURES / "Summary_000897.pdf"
_SUMMARY_000784_PDF = _FIXTURES / "Summary_000784.pdf"
# GOV.UK EPB API JSON for cert 001479 — the API-path counterpart of the
# Summary_001479.pdf fixture. Together they drive the API ≡ Summary
# parity workstream; Layer 4 of the validation stack is "API cascade SAP
# matches worksheet continuous SAP at 1e-4".
_API_001479_JSON = (
Path(__file__).parents[3]
/ "domain/sap10_calculator/rdsap/tests/fixtures/golden"
/ "0535-9020-6509-0821-6222.json"
)
def _summary_pdf_to_textract_style_pages(pdf_path: Path) -> list[str]:
"""Convert a Summary PDF into the per-page text format the existing
`ElmhurstSiteNotesExtractor` expects (label\\nvalue sequences).
`pdftotext -layout` preserves the spatial pairing of label and value
on each line; we split each line on 2+ spaces to surface the
label/value tokens, then concatenate them back into a single
newline-delimited stream per page.
"""
info = subprocess.run(
["pdfinfo", str(pdf_path)], capture_output=True, text=True, check=True
).stdout
m = re.search(r"Pages:\s+(\d+)", info)
if m is None:
raise RuntimeError(f"Could not parse page count from {pdf_path}")
page_count = int(m.group(1))
pages: list[str] = []
for i in range(1, page_count + 1):
layout = subprocess.run(
[
"pdftotext", "-layout", "-f", str(i), "-l", str(i),
str(pdf_path), "-",
],
capture_output=True, text=True, check=True,
).stdout
tokens: list[str] = []
for line in layout.splitlines():
if not line.strip():
tokens.append("")
continue
parts = [p for p in re.split(r"\s{2,}", line.strip()) if p]
tokens.extend(parts)
pages.append("\n".join(tokens))
return pages
def test_summary_000474_mapper_produces_three_building_parts() -> None:
# Arrange — cert U985-0001-000474 is a mid-terrace with 3 building
# parts (Main + 2 extensions) per the hand-built worksheet fixture
# at domain/sap10_calculator/worksheet/tests/
# _elmhurst_worksheet_000474.py. Routing the Summary PDF through
# extractor + mapper must yield the same count.
pages = _summary_pdf_to_textract_style_pages(_SUMMARY_000474_PDF)
site_notes = ElmhurstSiteNotesExtractor(pages).extract()
# Act
epc = EpcPropertyDataMapper.from_elmhurst_site_notes(site_notes)
# Assert
assert len(epc.sap_building_parts) == 3
def test_summary_000474_mapper_extracts_seven_windows() -> None:
# Arrange — cert U985-0001-000474's §11 table lodges 7 windows
# across Main + 1st Extension + 2nd Extension. The legacy Textract-
# style window parser couldn't anchor on the Summary PDF's tabular
# layout; the new W/H/Area-plus-Manufacturer anchor pair picks them
# all up.
pages = _summary_pdf_to_textract_style_pages(_SUMMARY_000474_PDF)
site_notes = ElmhurstSiteNotesExtractor(pages).extract()
# Act
epc = EpcPropertyDataMapper.from_elmhurst_site_notes(site_notes)
# Assert
assert len(epc.sap_windows) == 7
# Cohort chain SAP-pin tests follow. NOTE: certs 000474, 000480, 000487,
# 000490 previously had chain tests here pinning their cascade SAP
# against the U985 worksheet PDF — those tests were removed because
# their worksheets violate RdSAP 10 §5 (12) "Floor infiltration
# (suspended timber ground floor only)". Our cascade applies the spec
# rule (via `cert_to_inputs._has_suspended_timber_floor_per_spec`);
# the worksheet does not. So the spec-correct chain SAP for those
# certs can't match the worksheet SAP — by design, not by mapper bug.
# The Layer 1 hand-built fixtures for those 4 certs absorb the
# worksheet quirk by lodging `has_suspended_timber_floor=False`
# explicitly (overriding the spec inference) — so Layer 1 cascade pins
# still pin the worksheet value exactly. The chain tests below remain
# only for 000477, 000516 (and 001479 further down), where the
# worksheet IS spec-correct.
def test_summary_000477_full_chain_sap_matches_worksheet_pdf_exactly() -> None:
# Arrange — cert U985-0001-000477 is a single-bp mid-terrace with
# a 15.06 m² Room-in-Roof storey and zero baths lodged. Worksheet
# PDF lodges unrounded SAP 65.0057. Drives the chain through the
# `RoomInRoof.detailed_surfaces` cascade with stud walls @ 100mm
# Mineral, two uninsulated slopes, two party gable walls, plus the
# RR/storey-area suspended-timber-floor heuristic (RIR < storey →
# 0.2 ACH floor infiltration).
pages = _summary_pdf_to_textract_style_pages(_SUMMARY_000477_PDF)
site_notes = ElmhurstSiteNotesExtractor(pages).extract()
epc = EpcPropertyDataMapper.from_elmhurst_site_notes(site_notes)
# Act
result = calculate_sap_from_inputs(
cert_to_inputs(epc, prices=SAP_10_2_SPEC_PRICES)
)
# Assert
worksheet_unrounded_sap = 65.0057
assert abs(result.sap_score_continuous - worksheet_unrounded_sap) < 1e-4
def test_summary_000516_full_chain_sap_matches_worksheet_pdf_exactly() -> None:
# Arrange — cert U985-0001-000516 is a mid-terrace with main bp +
# 19.02 m² room-in-roof. Worksheet PDF lodges unrounded SAP 62.7937.
# The §11 table mixes 5 vertical windows (U=2.80) with 1 roof
# window (U=3.10 in cert, U=3.40 Table 24 raw); the mapper
# discriminates by `U > 3.0` and routes the high-U entry to
# `sap_roof_windows` so its solar gains feed §6 with the right
# pitch (45°) and Table-24 U-value.
pages = _summary_pdf_to_textract_style_pages(_SUMMARY_000516_PDF)
site_notes = ElmhurstSiteNotesExtractor(pages).extract()
epc = EpcPropertyDataMapper.from_elmhurst_site_notes(site_notes)
# Act
result = calculate_sap_from_inputs(
cert_to_inputs(epc, prices=SAP_10_2_SPEC_PRICES)
)
# Assert
worksheet_unrounded_sap = 62.7937
assert abs(result.sap_score_continuous - worksheet_unrounded_sap) < 1e-4
def test_summary_001479_mapper_extensions_count_matches_extension_bps() -> None:
# Arrange — cert 0535-9020-6509-0821-6222 (Summary_001479) is the first
# cohort cert with an actual GOV.UK API counterpart. Worksheet PDF
# lodges Main + Extension 1 + Extension 2 (3 building parts, 2
# extensions). Pre-slice the Elmhurst mapper hard-coded
# `extensions_count=0` regardless of survey.extensions; this asserts
# the count flows through.
pages = _summary_pdf_to_textract_style_pages(_SUMMARY_001479_PDF)
site_notes = ElmhurstSiteNotesExtractor(pages).extract()
# Act
epc = EpcPropertyDataMapper.from_elmhurst_site_notes(site_notes)
# Assert
assert epc.extensions_count == 2
assert len(epc.sap_building_parts) == 3
def test_summary_001479_main_party_wall_construction_is_cavity_unfilled() -> None:
# Arrange — cert 001479 Main §7 Walls lodges "Party Wall Type: CU
# Cavity masonry unfilled". The Elmhurst leading-code map previously
# only knew "S" and "C"; "CU" fell through to None, which made the
# cascade default to U=0.25 instead of the worksheet's lodged U=0.50.
# The fix adds "CU" → SAP10 wall_construction code 4 (WALL_CAVITY),
# which `u_party_wall` resolves to U=0.50 — matching the worksheet's
# §3 `Party walls Main … 0.50` row.
pages = _summary_pdf_to_textract_style_pages(_SUMMARY_001479_PDF)
site_notes = ElmhurstSiteNotesExtractor(pages).extract()
# Act
epc = EpcPropertyDataMapper.from_elmhurst_site_notes(site_notes)
# Assert
assert epc.sap_building_parts[0].party_wall_construction == 4
def test_summary_001479_ext2_floor_is_exposed_to_external_air() -> None:
# Arrange — cert 001479 Ext2 §9 lodges "Location: E To external air"
# — a cantilevered exposed timber floor (the upper-storey extension
# over the back garden). The worksheet's §3 row `Exposed floor Ext2
# … 1.92, 1.20, 1.20` pins this as U=1.20 via Table 20. Pre-slice the
# mapper only routed "U Above unheated space" through `is_exposed_
# floor=True`; "E To external air" fell through to the BS EN ISO
# 13370 ground-floor cascade, dropping the lodged exposure entirely.
pages = _summary_pdf_to_textract_style_pages(_SUMMARY_001479_PDF)
site_notes = ElmhurstSiteNotesExtractor(pages).extract()
# Act
epc = EpcPropertyDataMapper.from_elmhurst_site_notes(site_notes)
# Assert
ext2 = epc.sap_building_parts[2]
assert ext2.floor_type == "To external air"
assert ext2.sap_floor_dimensions[0].is_exposed_floor is True
def test_summary_001479_ext2_sloping_ceiling_roof_uninsulated_for_pre_1950() -> None:
# Arrange — cert 001479 Ext2 §8 lodges "Type: PS Pitched, sloping
# ceiling" + "Insulation Thickness: As Built" + age band C (1930-49).
# Original 1930s construction had no sloping-ceiling insulation;
# worksheet §3 `External roof Ext2 … 2.30` pins U=2.30 (uninsulated
# Table 16 row 0). Pre-slice the mapper passed thickness=None through,
# routing to `u_roof`'s pitched-roof Table 18 col 1 default (0.40 for
# age C, assumes loft-joist retrofit) — wrong geometry for PS.
# Ext1's PS roof at age M leaves thickness=None (modern build,
# cascade default U=0.15 matches worksheet).
pages = _summary_pdf_to_textract_style_pages(_SUMMARY_001479_PDF)
site_notes = ElmhurstSiteNotesExtractor(pages).extract()
# Act
epc = EpcPropertyDataMapper.from_elmhurst_site_notes(site_notes)
# Assert
assert epc.sap_building_parts[2].roof_insulation_thickness == 0
assert epc.sap_building_parts[1].roof_insulation_thickness is None
def test_summary_001479_secondary_heating_routes_mains_gas_fuel() -> None:
# Arrange — cert 001479 §14.1 Main Heating2 lodges "Secondary Heating
# Code: SAP code 605, Flush fitting live effect gas fire, sealed to
# chimney". The Summary surfaces only the SAP code (605); the fuel
# type 26 (mains gas) must be derived from the code range so the
# `_fuel_cost` orchestrator's `secondary_high_rate_gbp_per_kwh`
# picks up Table 32's gas tariff (£0.0348/kWh) rather than the
# default standard-electricity tariff (£0.132/kWh). Worksheet line
# (242) "Space heating - secondary … 3.4800 70.5022" confirms gas
# pricing.
pages = _summary_pdf_to_textract_style_pages(_SUMMARY_001479_PDF)
site_notes = ElmhurstSiteNotesExtractor(pages).extract()
# Act
epc = EpcPropertyDataMapper.from_elmhurst_site_notes(site_notes)
# Assert
assert epc.sap_heating.secondary_heating_type == 605
assert epc.sap_heating.secondary_fuel_type == 26
def test_summary_9501_flat_has_no_built_form_in_summary_pdf() -> None:
# Arrange — cert 9501 (Summary_000784.pdf) is a flat. The Elmhurst
# Summary's §1.0 "Property type" section lodges the built-form
# descriptor (e.g. "M Mid-Terrace", "D Detached") only for houses;
# flats have no built-form line — the §2.0 "Number of Storeys"
# section follows immediately after the "F Flat" property type.
#
# The extractor's `_extract_attachment` regex previously captured
# the line immediately after the property-type value
# unconditionally, so cert 9501 ends up with attachment
# "2.0 Number of Storeys:" — pure section-header noise that the
# mapper then surfaces on EpcPropertyData.built_form, breaking the
# cascade's flat-exposure routing downstream.
pages = _summary_pdf_to_textract_style_pages(_SUMMARY_000784_PDF)
site_notes = ElmhurstSiteNotesExtractor(pages).extract()
# Act
epc = EpcPropertyDataMapper.from_elmhurst_site_notes(site_notes)
# Assert — built_form is empty for flats. Houses set it to their
# attachment descriptor; flats lodge no attachment.
assert epc.built_form == ""
def test_summary_9501_dwelling_type_is_top_floor_flat() -> None:
# Arrange — cert 9501's worksheet treats the cert as a TOP-floor
# flat: §3 (28a) "Ground floor Main … U=0.0" because the floor
# sits over "Another dwelling below" (worksheet line 9.0 Floor
# location); §3 (30) has both an external roof + RR contributions
# so the roof IS exposed. The cascade's `_dwelling_exposure`
# function does prefix matching on `dwelling_type.lower()` to gate
# which surfaces are party — without "top-floor flat" the cert
# falls through to fully-exposed houses (Δ +9.25 W/K on floor).
#
# Floor-position inference rules:
# - floor.location indicates "Another dwelling below"
# → not ground floor (rules out ground-floor flat)
# - room_in_roof OR external roof present
# → roof exposed (rules out mid-floor flat)
# - therefore → top-floor flat
pages = _summary_pdf_to_textract_style_pages(_SUMMARY_000784_PDF)
site_notes = ElmhurstSiteNotesExtractor(pages).extract()
# Act
epc = EpcPropertyDataMapper.from_elmhurst_site_notes(site_notes)
# Assert
assert epc.dwelling_type is not None
assert epc.dwelling_type.lower().startswith("top-floor")
def test_summary_9501_rr_gable_walls_route_to_external_walls_hlc() -> None:
# Arrange — cert 9501's worksheet §3 lodges "Roof room Main Gable
# Wall 1" + "Gable Wall 2" as line (29a) entries (external walls)
# at the main-wall U (= 1.70 for age B Solid Brick): 13.50×1.70 +
# 15.95×1.70 = 50.07 W/K added on top of the regular external-walls
# 168.74 → 218.81 W/K total.
#
# The Summary mapper currently lodges these as
# `SapRoomInRoofSurface(kind='gable_wall', ...)` — the cascade's
# cohort-house default which routes to party walls at U=0.25
# (Table 4 row 2). For a top-floor flat in a mid-terrace block,
# the gables sit at the ends of the building (no neighbour above)
# — they're EXTERNAL not party. Surface them as
# `gable_wall_external` so the cascade's (29a) sum picks them up.
pages = _summary_pdf_to_textract_style_pages(_SUMMARY_000784_PDF)
site_notes = ElmhurstSiteNotesExtractor(pages).extract()
epc = EpcPropertyDataMapper.from_elmhurst_site_notes(site_notes)
# Act
from domain.sap10_calculator.rdsap.cert_to_inputs import (
heat_transmission_section_from_cert,
)
ht = heat_transmission_section_from_cert(epc)
# Assert — worksheet (29a) total walls = 168.7420 (main) +
# 22.95 (Gable 1) + 27.115 (Gable 2) = 218.807 W/K. Tolerance
# 1e-2 absorbs the 2-d.p. rounding of the underlying U/area
# products; the 1e-4 chain test downstream will tighten this
# to the cascade-internal rounding floor.
worksheet_walls_w_per_k = 218.807
assert abs(ht.walls_w_per_k - worksheet_walls_w_per_k) <= 1e-2
def test_summary_001479_full_chain_sap_matches_worksheet_pdf_exactly() -> None:
# Arrange — cert 001479 (Summary_001479.pdf / P960-0001-001479.pdf)
# is the first cohort cert with a real GOV.UK EPB API counterpart
# (cert ref 0535-9020-6509-0821-6222). Worksheet PDF line "SAP value"
# lodges unrounded SAP **69.0094** (rating C 69, also the API-
# published integer). This is the load-bearing forcing function for
# the API↔Elmhurst parity workstream: any drift from 1e-4 means a
# mapper gap, not a calculator bug — the cohort 6 cert cascades all
# reproduce Elmhurst exactly at 1e-4 on hand-built fixtures.
#
# Source-data caveat (documented for future debuggers): Summary §3
# lodges Ext1 age band as "M 2023 onwards"; the worksheet header
# records "Ext1: L". Likely assessor data-entry inconsistency. The
# mapper trusts the Summary (its source of truth); accept whatever
# residual the M vs L disagreement produces.
pages = _summary_pdf_to_textract_style_pages(_SUMMARY_001479_PDF)
site_notes = ElmhurstSiteNotesExtractor(pages).extract()
epc = EpcPropertyDataMapper.from_elmhurst_site_notes(site_notes)
# Act
result = calculate_sap_from_inputs(
cert_to_inputs(epc, prices=SAP_10_2_SPEC_PRICES)
)
# Assert — 1e-4 pin, no widening, no xfail (project memory
# `feedback_zero_error_strict`).
worksheet_unrounded_sap = 69.0094
assert abs(result.sap_score_continuous - worksheet_unrounded_sap) < 1e-4
def test_summary_0330_full_chain_sap_matches_worksheet_pdf_exactly() -> None:
# Arrange — cert 0330-2249-8150-2326-4121 (Summary_000897.pdf /
# dr87-0001-000897.pdf) is the second boiler cert under per-cert
# mapper validation: mains-gas boiler (PCDB idx 10241), mid-terrace
# 2-bp dwelling, TFA 69.14 m². Worksheet PDF "SAP value" line lodges
# unrounded SAP **61.5993**. Same load-bearing role as cert 001479
# (the first boiler) — Summary path proves itself against the
# worksheet, then becomes the canonical reference for the API path.
# Expected RED at Δ +0.4667 at handover-baseline (Summary mapper
# cascade SAP 62.0660); mapper gaps to close are §11 glazing_type=14
# (windows HLC +6.71 W/K) and the §4 hot-water cascade (kWh +1060).
pages = _summary_pdf_to_textract_style_pages(_SUMMARY_000897_PDF)
site_notes = ElmhurstSiteNotesExtractor(pages).extract()
epc = EpcPropertyDataMapper.from_elmhurst_site_notes(site_notes)
# Act
result = calculate_sap_from_inputs(
cert_to_inputs(epc, prices=SAP_10_2_SPEC_PRICES)
)
# Assert — 1e-4 pin, no widening, no xfail (project memory
# `feedback_zero_error_strict`).
worksheet_unrounded_sap = 61.5993
assert abs(result.sap_score_continuous - worksheet_unrounded_sap) < 1e-4
_API_0330_JSON = (
Path(__file__).parents[3]
/ "domain/sap10_calculator/rdsap/tests/fixtures/golden"
/ "0330-2249-8150-2326-4121.json"
)
def test_api_0330_full_chain_sap_matches_worksheet_pdf_exactly() -> None:
# Arrange — cert 0330-2249-8150-2326-4121 (second boiler validation
# cert: mains-gas Vaillant PCDB idx 10241, mid-terrace 2-bp dwelling,
# TFA 90.56 m²) has both an Elmhurst Summary PDF and a GOV.UK EPB API
# JSON. The Summary path lands at 1e-4 vs worksheet SAP 61.5993
# above; this Layer 4 production gate asserts the API path matches
# the worksheet to the same 1e-4 tolerance — same forcing function
# as cert 001479's Layer 4 test, applied to the second boiler cert.
#
# Slices 96-99 (flat-roof Table 18 col (3) U-values + glazing_type=2
# surfacing + shower-outlets list normalisation + window-area
# rounding alignment) jointly closed the API path from
# Δ +2.1453 → Δ -0.000011 vs worksheet 61.5993.
doc = json.loads(_API_0330_JSON.read_text())
epc = EpcPropertyDataMapper.from_api_response(doc)
# Act
result = calculate_sap_from_inputs(
cert_to_inputs(epc, prices=SAP_10_2_SPEC_PRICES)
)
# Assert — 1e-4 pin against the worksheet's continuous SAP.
worksheet_unrounded_sap = 61.5993
assert abs(result.sap_score_continuous - worksheet_unrounded_sap) < 1e-4
def test_api_001479_full_chain_sap_matches_worksheet_pdf_exactly() -> None:
# Arrange — cert 001479 has both an Elmhurst Summary PDF and a GOV.UK
# EPB API JSON (ref 0535-9020-6509-0821-6222). The Summary cascade
# already pins at worksheet's 69.0094 ± 1e-4 above; this test is the
# Layer 4 production-path gate: API JSON → from_api_response →
# cert_to_inputs → calculate_sap_from_inputs must also hit 69.0094
# at 1e-4. Identical inputs must produce identical outputs; the
# calculator is deterministic, so any drift is a mapper coverage gap.
doc = json.loads(_API_001479_JSON.read_text())
epc = EpcPropertyDataMapper.from_api_response(doc)
# Act
result = calculate_sap_from_inputs(
cert_to_inputs(epc, prices=SAP_10_2_SPEC_PRICES)
)
# Assert — 1e-4 pin against the worksheet's continuous SAP. ±0.5 is
# the API-only fallback (project memory `feedback_api_tolerance_1e_
# minus_4`); when the worksheet is available, identical-inputs-must-
# produce-identical-outputs is the bar.
worksheet_unrounded_sap = 69.0094
assert abs(result.sap_score_continuous - worksheet_unrounded_sap) < 1e-4
# ============================================================================
# Mapper-vs-hand-built EpcPropertyData diff tests
# ============================================================================
# The 6 cohort hand-builts (_elmhurst_worksheet_NNNNNN.build_epc) are the
# 100%-correct calculator-input ground truth — each cascades to its
# worksheet PDF's lodged SAP at 1e-4. The chain tests above only assert
# cascade-output equivalence; the mapper can pass them by producing a
# *different* EpcPropertyData that happens to cascade to the same number.
#
# These tests pin the missing layer: the mapper's EpcPropertyData must
# match the hand-built's load-bearing fields exactly. Every divergence
# surfaced here is a mapper coverage gap to close as its own slice.
#
# "Load-bearing" = the subset of EpcPropertyData fields that drive the
# SAP cascade or carry semantic cross-mapper meaning. Cert-metadata
# fields (address, registration dates, descriptive EnergyElement lists,
# tariff strings) are excluded because they don't change calculator
# output and vary by mapper pathway (the API publishes some, the
# Elmhurst Summary publishes others) without semantic disagreement.
# SapWindow sub-fields the cascade doesn't read (descriptive Union[int,
# str] codes lodged differently by each mapper). The cascade reads
# window_width / window_height / orientation / window_location /
# frame_factor / window_transmission_details.{u_value,solar_
# transmittance} — those WILL still be diffed; everything else on
# SapWindow is metadata and excluded to avoid noise from the int/str
# dual encoding (API mapper produces int codes; Elmhurst mapper
# surfaces the Summary's lodged strings).
_NON_LOAD_BEARING_WINDOW_SUBFIELDS: frozenset[str] = frozenset({
"frame_material",
"glazing_gap",
"window_type",
"glazing_type",
"window_wall_type",
"draught_proofed",
"permanent_shutters_present",
"permanent_shutters_insulated",
})
def _is_excluded_path(path: str) -> bool:
"""Return True for paths the diff should silently skip — non-cascade-
affecting Union[int, str] encoding differences between the API and
Elmhurst mapper outputs that cohort hand-built fixtures don't pin."""
if path.startswith("sap_windows[") and "]." in path:
suffix = path.split("].", 1)[1]
if suffix in _NON_LOAD_BEARING_WINDOW_SUBFIELDS:
return True
if suffix == "window_transmission_details.data_source":
return True
# `roof_construction_type` is set by the Elmhurst mapper from
# `roof.roof_type` (e.g. "Pitched (slates/tiles), access to loft") and
# left None by the cohort hand-builts. The cascade in
# `heat_transmission.py:562` only dispatches on the "sloping ceiling"
# substring (RdSAP §3.8); none of the cohort certs lodge pitched-
# sloping-ceiling roofs, so both values produce identical cascade
# output. Exclude from the diff to avoid flagging informational drift.
if path.startswith("sap_building_parts[") and path.endswith(".roof_construction_type"):
return True
# `sap_ventilation.has_suspended_timber_floor` and
# `..._sealed` are set explicitly on the hand-builts (to mirror the
# cohort U985 worksheets' (12) infiltration values) but left None by
# the Elmhurst mapper because the Summary PDF doesn't surface floor-
# construction in a parseable form. When None, `cert_to_inputs._
# has_suspended_timber_floor_per_spec` infers the value mechanically
# from per-bp floor-construction data — producing the same cascade
# output the explicit-bool hand-built path produces for cohort 000477
# / 000516 (where the spec inference and the worksheet agree). Where
# the spec inference and worksheet disagree (cohort 000474, 000480,
# 000487, 000490), the chain SAP-pin tests fail separately — that's
# a known Elmhurst-worksheet-vs-RdSAP-10 §5 (12) divergence, not a
# mapper diff issue.
if path == "sap_ventilation.has_suspended_timber_floor":
return True
if path == "sap_ventilation.suspended_timber_floor_sealed":
return True
return False
_LOAD_BEARING_FIELDS: tuple[str, ...] = (
# Cascade-driving structural fields
"sap_building_parts",
"sap_windows",
"sap_roof_windows",
"sap_heating",
"sap_ventilation",
"sap_energy_source",
"total_floor_area_m2",
# Building-classification fields driving default cascades
"dwelling_type",
"built_form",
"property_type",
"country_code",
"postcode",
# Counts and openings
"door_count",
"insulated_door_count",
"insulated_door_u_value",
"habitable_rooms_count",
"heated_rooms_count",
"wet_rooms_count",
"extensions_count",
"open_chimneys_count",
"blocked_chimneys_count",
"extract_fans_count",
# Lighting
"cfl_fixed_lighting_bulbs_count",
"led_fixed_lighting_bulbs_count",
"incandescent_fixed_lighting_bulbs_count",
"low_energy_fixed_lighting_bulbs_count",
"fixed_lighting_outlets_count",
"low_energy_fixed_lighting_outlets_count",
# HW / appliances
"solar_water_heating",
"has_hot_water_cylinder",
"has_fixed_air_conditioning",
"has_conservatory",
"has_heated_separate_conservatory",
# Envelope drivers
"percent_draughtproofed",
"mechanical_ventilation",
"pressure_test",
# Construction-detail flags
"addendum",
"lzc_energy_sources",
"any_unheated_rooms",
"number_of_storeys",
"sap_flat_details",
)
def _diff_load_bearing(
mapped: object, hand_built: object, path: str = "",
) -> list[str]:
"""Recursive field diff; yields one line per leaf divergence between
mapped EpcPropertyData and the hand-built fixture. Int/float type
differences with the same numeric value are not flagged.
Strict-pyright posture: arguments typed `object` so each branch
narrows via `isinstance` rather than threading `Any` through the
recursion (which pyright can't reason about under
`strict`/`typeCheckingMode = strict`)."""
out: list[str] = []
if type(mapped) is not type(hand_built):
if not (isinstance(mapped, (int, float)) and isinstance(hand_built, (int, float))):
if not _is_excluded_path(path):
out.append(
f"{path}: TYPE {type(mapped).__name__} vs "
f"{type(hand_built).__name__} mapped={mapped!r} "
f"handbuilt={hand_built!r}"
)
return out
if dataclasses.is_dataclass(mapped) and not isinstance(mapped, type) \
and dataclasses.is_dataclass(hand_built) and not isinstance(hand_built, type):
for fld in dataclasses.fields(mapped):
out.extend(_diff_load_bearing(
getattr(mapped, fld.name),
getattr(hand_built, fld.name),
f"{path}.{fld.name}" if path else fld.name,
))
return out
if isinstance(mapped, list) and isinstance(hand_built, list):
mapped_list = cast("list[object]", mapped)
hand_built_list = cast("list[object]", hand_built)
if len(mapped_list) != len(hand_built_list):
out.append(f"{path}: LEN {len(mapped_list)} vs {len(hand_built_list)}")
return out
for i, (m_item, h_item) in enumerate(zip(mapped_list, hand_built_list)):
out.extend(_diff_load_bearing(m_item, h_item, f"{path}[{i}]"))
return out
if mapped != hand_built:
if not _is_excluded_path(path):
out.append(f"{path}: mapped={mapped!r} handbuilt={hand_built!r}")
return out
def test_from_elmhurst_site_notes_matches_hand_built_000474() -> None:
# Arrange — _elmhurst_worksheet_000474.build_epc() is the canonical
# hand-built EpcPropertyData for cert U985-0001-000474; it cascades
# to the worksheet PDF's `SAP value 62.2584` at 1e-4 (cohort SAP-
# result pin). Routing the corresponding Summary PDF through the
# Elmhurst mapper MUST produce a load-bearing-field-equivalent
# EpcPropertyData; any divergence is a mapper-coverage gap.
#
# Tracer-bullet scope: cert 000474 only. Once GREEN, parametrize
# over the 5 other cohort fixtures and add cert 001479 (after
# `_elmhurst_worksheet_001479` lands at 1e-4 via Slice 62 iteration).
pages = _summary_pdf_to_textract_style_pages(_SUMMARY_000474_PDF)
site_notes = ElmhurstSiteNotesExtractor(pages).extract()
mapped = EpcPropertyDataMapper.from_elmhurst_site_notes(site_notes)
hand_built = _w000474.build_epc()
# Act
diffs: list[str] = []
for field_name in _LOAD_BEARING_FIELDS:
diffs.extend(_diff_load_bearing(
getattr(mapped, field_name, None),
getattr(hand_built, field_name, None),
field_name,
))
# Assert
assert not diffs, (
f"{len(diffs)} load-bearing divergence(s) between mapped and "
f"hand-built EpcPropertyData for cohort cert 000474:\n " +
"\n ".join(diffs)
)
def test_from_elmhurst_site_notes_matches_hand_built_000477() -> None:
# Arrange — _elmhurst_worksheet_000477.build_epc() is the canonical
# hand-built EpcPropertyData for cert U985-0001-000477 (single-bp
# mid-terrace, age band B, RIR with stud walls + party gables, no
# extension); it cascades to the worksheet PDF's `SAP value 65.0057`
# at 1e-4. Routing the Summary PDF through the Elmhurst mapper MUST
# produce a load-bearing-field-equivalent EpcPropertyData; any
# divergence is a mapper-coverage gap to close as its own slice.
pages = _summary_pdf_to_textract_style_pages(_SUMMARY_000477_PDF)
site_notes = ElmhurstSiteNotesExtractor(pages).extract()
mapped = EpcPropertyDataMapper.from_elmhurst_site_notes(site_notes)
hand_built = _w000477.build_epc()
# Act
diffs: list[str] = []
for field_name in _LOAD_BEARING_FIELDS:
diffs.extend(_diff_load_bearing(
getattr(mapped, field_name, None),
getattr(hand_built, field_name, None),
field_name,
))
# Assert
assert not diffs, (
f"{len(diffs)} load-bearing divergence(s) between mapped and "
f"hand-built EpcPropertyData for cohort cert 000477:\n " +
"\n ".join(diffs)
)
def test_from_elmhurst_site_notes_matches_hand_built_000480() -> None:
# Arrange — _elmhurst_worksheet_000480.build_epc() is the canonical
# hand-built EpcPropertyData for cert U985-0001-000480 (mid-terrace
# with main + 1 extension + 19.83 m² RIR, gas combi); it cascades
# to the worksheet PDF's `SAP value 61.2986` at 1e-4. Routing the
# Summary PDF through the Elmhurst mapper MUST produce a load-
# bearing-field-equivalent EpcPropertyData; any divergence is a
# mapper-coverage gap to close as its own slice.
pages = _summary_pdf_to_textract_style_pages(_SUMMARY_000480_PDF)
site_notes = ElmhurstSiteNotesExtractor(pages).extract()
mapped = EpcPropertyDataMapper.from_elmhurst_site_notes(site_notes)
hand_built = _w000480.build_epc()
# Act
diffs: list[str] = []
for field_name in _LOAD_BEARING_FIELDS:
diffs.extend(_diff_load_bearing(
getattr(mapped, field_name, None),
getattr(hand_built, field_name, None),
field_name,
))
# Assert
assert not diffs, (
f"{len(diffs)} load-bearing divergence(s) between mapped and "
f"hand-built EpcPropertyData for cohort cert 000480:\n " +
"\n ".join(diffs)
)
def test_from_elmhurst_site_notes_matches_hand_built_000487() -> None:
# Arrange — _elmhurst_worksheet_000487.build_epc() is the canonical
# hand-built EpcPropertyData for cert U985-0001-000487 (Enclosed
# Mid-Terrace, main + 1 extension + 21.03 m² RIR with explicit-U
# gable_wall_external, gas combi, 1 electric shower, 1.43 m²
# timber-frame alt wall on the extension); it cascades to the
# worksheet PDF's `SAP value 61.6431` at 1e-4. Routing the Summary
# PDF through the Elmhurst mapper MUST produce a load-bearing-
# field-equivalent EpcPropertyData; any divergence is a mapper-
# coverage gap to close as its own slice.
pages = _summary_pdf_to_textract_style_pages(_SUMMARY_000487_PDF)
site_notes = ElmhurstSiteNotesExtractor(pages).extract()
mapped = EpcPropertyDataMapper.from_elmhurst_site_notes(site_notes)
hand_built = _w000487.build_epc()
# Act
diffs: list[str] = []
for field_name in _LOAD_BEARING_FIELDS:
diffs.extend(_diff_load_bearing(
getattr(mapped, field_name, None),
getattr(hand_built, field_name, None),
field_name,
))
# Assert
assert not diffs, (
f"{len(diffs)} load-bearing divergence(s) between mapped and "
f"hand-built EpcPropertyData for cohort cert 000487:\n " +
"\n ".join(diffs)
)
def test_from_elmhurst_site_notes_matches_hand_built_000490() -> None:
# Arrange — _elmhurst_worksheet_000490.build_epc() is the canonical
# hand-built EpcPropertyData for cert U985-0001-000490 (End-Terrace,
# main + 1 extension, gas combi + gas-secondary; sheltered_sides=1
# per RdSAP §S5); it cascades to the worksheet PDF's `SAP value
# 57.3979` at 1e-4. Routing the Summary PDF through the Elmhurst
# mapper MUST produce a load-bearing-field-equivalent
# EpcPropertyData; any divergence is a mapper-coverage gap.
pages = _summary_pdf_to_textract_style_pages(_SUMMARY_000490_PDF)
site_notes = ElmhurstSiteNotesExtractor(pages).extract()
mapped = EpcPropertyDataMapper.from_elmhurst_site_notes(site_notes)
hand_built = _w000490.build_epc()
# Act
diffs: list[str] = []
for field_name in _LOAD_BEARING_FIELDS:
diffs.extend(_diff_load_bearing(
getattr(mapped, field_name, None),
getattr(hand_built, field_name, None),
field_name,
))
# Assert
assert not diffs, (
f"{len(diffs)} load-bearing divergence(s) between mapped and "
f"hand-built EpcPropertyData for cohort cert 000490:\n " +
"\n ".join(diffs)
)
def test_from_elmhurst_site_notes_matches_hand_built_000516() -> None:
# Arrange — _elmhurst_worksheet_000516.build_epc() is the canonical
# hand-built EpcPropertyData for cert U985-0001-000516 (Mid-Terrace,
# main + 19.02 m² RIR, 5 vertical windows + 1 roof window which the
# mapper routes to `sap_roof_windows` per `U > 3.0` discrimination);
# it cascades to the worksheet PDF's `SAP value 62.7937` at 1e-4.
# Routing the Summary PDF through the Elmhurst mapper MUST produce
# a load-bearing-field-equivalent EpcPropertyData.
pages = _summary_pdf_to_textract_style_pages(_SUMMARY_000516_PDF)
site_notes = ElmhurstSiteNotesExtractor(pages).extract()
mapped = EpcPropertyDataMapper.from_elmhurst_site_notes(site_notes)
hand_built = _w000516.build_epc()
# Act
diffs: list[str] = []
for field_name in _LOAD_BEARING_FIELDS:
diffs.extend(_diff_load_bearing(
getattr(mapped, field_name, None),
getattr(hand_built, field_name, None),
field_name,
))
# Assert
assert not diffs, (
f"{len(diffs)} load-bearing divergence(s) between mapped and "
f"hand-built EpcPropertyData for cohort cert 000516:\n " +
"\n ".join(diffs)
)