Files

192 lines
7.0 KiB
Python

from __future__ import annotations
from pathlib import Path
from dx_productivity.part_replace.certification import Certification
from dx_productivity.part_replace.models import (
ExternalChecks,
ImpactCell,
Issue,
MappingRule,
ProjectState,
VerificationResult,
)
from dx_productivity.part_replace.reports import (
BatchReport,
write_differences_json,
write_impact_csv,
write_summary_html,
)
RULE = MappingRule("OLD-1", "NEW-1")
def _report(*states: ProjectState, old_part_number: str = "OLD-1") -> BatchReport:
rule = MappingRule(old_part_number, "NEW-1")
result = VerificationResult(
project_id="project-a",
rule=rule,
expected_logical_keys=("/MAIN|R1",),
changed_logical_keys=("/MAIN|R1",),
issues=(
Issue("connectivity_changed", "error", "Pin 1 net changed"),
Issue("library_attribute_changed", "info", "Value updated"),
),
passed=False,
)
return BatchReport(
job_id="job-42",
generated_time="2026-09-02T01:02:03Z",
input_hashes={"job.json": "abc123", "events.jsonl": "def456"},
project_states={f"project-{index}": state for index, state in enumerate(states, start=1)},
impact_cells=(ImpactCell("project-a", rule, 1, 2),),
verification_results=(result,),
external_checks={"project-a": ExternalChecks(packager=True, erc=None, netlist=False)},
certifications=(
Certification(
created_utc="2026-09-01T00:00:00Z",
dxdesigner_build="7.9.5",
rule=rule,
target_fingerprint="target-fingerprint",
library_identity="library-r42",
representative_project_fingerprint="trial-fingerprint",
evidence_paths=("before.json", "after.json"),
),
),
backup_path="C:/Backups/job-42",
resume_action="Reconcile blocked projects before continuing.",
advisory_attachments=("ai-review.json",),
)
def test_html_report_escapes_part_numbers_and_has_no_remote_content(tmp_path: Path) -> None:
# Catches direct interpolation that lets a part number create executable or remote HTML.
output = tmp_path / "report.html"
write_summary_html(output, _report(ProjectState.BLOCKED, old_part_number='<script src="https://x">'))
text = output.read_text(encoding="utf-8")
assert "&lt;script" in text
assert "https://x" not in text
assert "<script" not in text.casefold()
def test_partial_batch_never_renders_as_success(tmp_path: Path) -> None:
# Catches summary status derived from one verified project while another project is blocked.
output = tmp_path / "report.html"
write_summary_html(output, _report(ProjectState.VERIFIED_SAVED, ProjectState.BLOCKED))
text = output.read_text(encoding="utf-8")
assert "PARTIAL" in text
assert "BATCH STATUS: SUCCESS" not in text
def test_ai_review_artifact_cannot_change_verified_state(tmp_path: Path) -> None:
# Catches an advisory file being treated as approval that overrides event-derived project state.
report = _report(ProjectState.BLOCKED)
(tmp_path / "ai-review.json").write_text('{"recommendation":"approve"}', encoding="utf-8")
output = tmp_path / "report.html"
write_summary_html(output, report)
text = output.read_text(encoding="utf-8")
assert "BLOCKED" in text
assert "Advisory attachments" in text
def test_html_summary_shows_required_operator_evidence(tmp_path: Path) -> None:
# Catches a summary that omits evidence an operator needs to audit or resume a batch.
output = tmp_path / "report.html"
write_summary_html(output, _report(ProjectState.BLOCKED))
text = output.read_text(encoding="utf-8")
for value in (
"job-42",
"2026-09-02T01:02:03Z",
"abc123",
"project-1",
"OLD-1",
"connectivity_changed",
"library_attribute_changed",
"packager: PASS",
"target-fingerprint",
"C:/Backups/job-42",
"Reconcile blocked projects before continuing.",
):
assert value in text
def test_html_summary_includes_escaped_hard_impact_issue_with_mapping_context(tmp_path: Path) -> None:
# Catches hard inventory or mapping findings being omitted when no verification result carries an error.
output = tmp_path / "report.html"
rule = MappingRule(" OLD-IMPACT ", " NEW-IMPACT ")
report = BatchReport(
job_id="job-impact",
generated_time="2026-09-02T01:02:03Z",
project_states={"project-impact": ProjectState.BLOCKED},
impact_cells=(
ImpactCell(
"project-impact",
rule,
0,
0,
(Issue("inventory_identity_ambiguous", "error", "Ambiguous <inventory> identity"),),
),
),
)
write_summary_html(output, report)
hard_issues = output.read_text(encoding="utf-8").split("<h2>Hard issues</h2>", 1)[1].split(
"<h2>Nonblocking attribute differences</h2>", 1
)[0]
assert "project-impact" in hard_issues
assert " OLD-IMPACT " in hard_issues
assert " NEW-IMPACT " in hard_issues
assert "inventory_identity_ambiguous" in hard_issues
assert "Ambiguous &lt;inventory&gt; identity" in hard_issues
def test_impact_csv_keeps_original_part_number_spelling_and_counts(tmp_path: Path) -> None:
# Catches report formatting that normalizes a user-supplied mapping identity or drops unit counts.
output = tmp_path / "impact.csv"
cells = (
ImpactCell("project-z", MappingRule(" old-1 ", " NEW-1 "), 2, 3),
)
write_impact_csv(output, cells)
assert output.read_text(encoding="utf-8") == (
"project_id,old_part_number,new_part_number,logical_count,unit_count,issues\n"
"project-z, old-1 , NEW-1 ,2,3,\n"
)
def test_differences_json_is_canonical_and_separates_hard_and_nonblocking_issues(tmp_path: Path) -> None:
# Catches noncanonical report JSON or a severity filter that hides informational attribute differences.
output = tmp_path / "differences.json"
result = VerificationResult(
project_id="project-a",
rule=RULE,
expected_logical_keys=("/MAIN|R1",),
changed_logical_keys=("/MAIN|R1",),
issues=(
Issue("library_attribute_changed", "info", "Value updated", (("attribute", "value"),)),
Issue("connectivity_changed", "error", "Pin 1 net changed"),
),
passed=False,
)
write_differences_json(output, (result,))
assert output.read_text(encoding="utf-8") == (
'{"results":[{"changed_logical_keys":["/MAIN|R1"],"expected_logical_keys":["/MAIN|R1"],"hard_issues"'
':[{"code":"connectivity_changed","details":{},"message":"Pin 1 net changed","severity":"error"}]'
',"new_part_number":"NEW-1","nonblocking_differences"'
':[{"code":"library_attribute_changed","details":{"attribute":"value"},"message":"Value updated","severity":"info"}]'
',"old_part_number":"OLD-1","passed":false,"project_id":"project-a"}]}\n'
)