#!/usr/bin/env python3
"""Adversarial deterministic parser and safety tests."""
from __future__ import annotations
import importlib.util
import io
import sys
from email.message import Message
from pathlib import Path
SKILL_DIR = Path(__file__).resolve().parents[1]
FIXTURES = SKILL_DIR / "tests" / "fixtures"
spec = importlib.util.spec_from_file_location("nz_regulatory_cli", SKILL_DIR / "scripts" / "cli.py")
assert spec and spec.loader
cli = importlib.util.module_from_spec(spec)
sys.modules[spec.name] = cli
spec.loader.exec_module(cli)
class FakeResponse:
def __init__(self, body: bytes, content_type: str = "text/html") -> None:
self._body = io.BytesIO(body)
self.headers = Message()
self.headers["Content-Type"] = content_type
def read(self, amount: int) -> bytes:
return self._body.read(amount)
def geturl(self) -> str:
return cli.REGULATION_INDEX
def expect_cli_error(fn, code: int, blocked: bool = False) -> None:
try:
fn()
except cli.CliError as exc:
assert exc.code == code
assert exc.blocked is blocked
else:
raise AssertionError("expected CliError")
def main() -> int:
regulation = cli.parse_regulation_search((FIXTURES / "regulation-search.html").read_text(encoding="utf-8"))
assert regulation == [{
"title": "Regulatory Impact Statement: Sample measure",
"url": cli.REGULATION_INDEX + "sample-ris/",
"published": "12 September 2026",
"author": "Example Department",
"document_type": "regulatory_impact_statement",
"publisher": "Ministry for Regulation",
}]
print("[PASS] fixture regulation search parses one allowlisted typed result")
environment, total = cli.parse_environment_search((FIXTURES / "environment-search.html").read_text(encoding="utf-8"))
assert total == 1 and environment[0]["document_type"] == "publication_package"
assert environment[0]["document_types"] == ["cabinet_paper", "regulatory_impact_statement"]
assert environment[0]["tags"] == ["Cabinet Paper", "Regulatory Impact Statement"]
print("[PASS] fixture environment embedded listing parses tags, total, and URL")
detail = cli.parse_detail((FIXTURES / "detail-page.html").read_text(encoding="utf-8"), cli.ENVIRONMENT_INDEX + "synthetic-package/")
assert detail["document_count"] == 3
assert detail["document_type"] == "publication_page"
assert [item["document_type"] for item in detail["documents"]] == [
"regulatory_impact_statement", "independent_quality_assessment", "cabinet_paper"
]
assert all(item["url"].startswith(cli.ENVIRONMENT_ROOT) for item in detail["documents"])
assert all("#" not in item["url"] for item in detail["documents"])
print("[PASS] fixture detail deduplicates fragments, excludes external PDFs, and distinguishes document types")
first_page = (FIXTURES / "regulation-search.html").read_text(encoding="utf-8")
second_page = (FIXTURES / "regulation-search-page-2.html").read_text(encoding="utf-8")
first_url = cli.source_url("regulation", "sample")
second_url = cli.REGULATION_INDEX + "?query=sample&start=12"
requested: list[str] = []
real_fetch = cli.fetch_html
def fake_fetch(url: str) -> tuple[str, str]:
requested.append(url)
if url == first_url:
return first_page, first_url
if url == second_url:
return second_page, second_url
raise AssertionError(f"unexpected pagination URL: {url}")
setattr(cli, "fetch_html", fake_fetch)
try:
paged, _ = cli.run_search("sample", "regulation", 25)
finally:
setattr(cli, "fetch_html", real_fetch)
assert requested == [first_url, second_url]
assert paged["returned"] == 2 and paged["source_totals"] == {"regulation": 2}
assert paged["source_urls"] == requested
real_page_limit = cli.MAX_REGULATION_PAGES
setattr(cli, "fetch_html", fake_fetch)
setattr(cli, "MAX_REGULATION_PAGES", 1)
requested.clear()
try:
expect_cli_error(lambda: cli.run_search("sample", "regulation", 25), 6)
finally:
setattr(cli, "fetch_html", real_fetch)
setattr(cli, "MAX_REGULATION_PAGES", real_page_limit)
assert requested == [first_url]
print("[PASS] regulation pagination is followed accurately and fails closed at its page bound")
cases = {
"RAS": "regulatory_analysis_summary",
"Regulatory Impact Assessment": "regulatory_impact_assessment",
"Supplementary Analysis Report": "supplementary_analysis_report",
"Cost Recovery Impact Statement": "cost_recovery_impact_statement",
"Agency quality assurance statement": "agency_quality_assessment",
"Quality assessment": "quality_assessment",
"Cabinet minute": "cabinet_minute",
}
for title, expected in cases.items():
assert cli.classify_document(title) == expected, (title, cli.classify_document(title))
assert cli.classify_document("Neutral package", cli.ENVIRONMENT_INDEX + "neutral/") == "publication_page"
print("[PASS] adversarial type classifier keeps RAS, RIS, RIA, QA, SAR, CRIS, and Cabinet records distinct")
malicious = '