"""Verify the DOT Drug & Alcohol compliance binder generates correctly. Renders the binder for the default FMCSA (trucking) variant plus a non-default DOT mode with a state Drug-Free Workplace addendum, and asserts the PDFs are multi-page and contain the key deliverable sections the program promises: written policy, program-management instructions, supervisor training, EAP/SAP resources, random-testing instructions, recordkeeping, forms, and regulations. Requires: reportlab, pypdf, and (for text extraction) pdfplumber. Run from the repo root with a venv that has those installed. """ from __future__ import annotations import importlib.util import os import tempfile _GEN = os.path.abspath( os.path.join( os.path.dirname(__file__), "..", "document_gen", "templates", "dot_da_binder_generator.py", ) ) _spec = importlib.util.spec_from_file_location("dot_da_binder_generator", _GEN) _mod = importlib.util.module_from_spec(_spec) _spec.loader.exec_module(_mod) # type: ignore[union-attr] generate_da_binder = _mod.generate_da_binder MODE_META = _mod.MODE_META def _extract_text(path: str) -> str: import pypdf reader = pypdf.PdfReader(path) return "\n".join(page.extract_text() or "" for page in reader.pages) def test_fmcsa_binder_has_all_sections(): with tempfile.TemporaryDirectory() as d: out = os.path.join(d, "binder.pdf") path = generate_da_binder( output_path=out, carrier_name="Acme Trucking LLC", dot_number="3456789", mode="fmcsa", cdl_drivers=4, owner_operators=1, der_name="Jane Owner", provider_name="Performance West Consortium", ) assert path and os.path.exists(path) import pypdf assert len(pypdf.PdfReader(path).pages) >= 10, "binder should be substantial" text = _extract_text(path) # FMCSA-specific facts assert "49 CFR Part 382" in text assert "Federal Motor Carrier Safety Administration" in text assert "Clearinghouse" in text, "FMCSA binder must mention the Clearinghouse" # The promised deliverables (one assertion each) for needle in [ "How to Manage Your Program", # program-management instructions "Written Drug & Alcohol Testing Policy", # written policy "Random Testing Program", # random testing instructions "Supervisor", # supervisor training materials "Substance Abuse Professional", # SAP access "SAMHSA", # EAP / rehab / treatment resources "Recordkeeping", # recordkeeping instructions "Required Compliance Forms", # required forms "Acknowledgment", # an actual form "The Regulations", # copies/citations of regulations ]: assert needle in text, f"missing deliverable section: {needle!r}" def test_non_fmcsa_mode_and_state_addendum(): with tempfile.TemporaryDirectory() as d: out = os.path.join(d, "binder_phmsa.pdf") path = generate_da_binder( output_path=out, carrier_name="Pipeline Co", dot_number="111", mode="phmsa", cdl_drivers=3, state_dfwp="Georgia", ) assert path and os.path.exists(path) text = _extract_text(path) # PHMSA variant swaps in the right rule + agency, no Clearinghouse assert "49 CFR Part 199" in text assert "Pipeline and Hazardous Materials Safety Administration" in text assert "clearinghouse.fmcsa.dot.gov" not in text.lower() # State addendum present assert "Georgia Drug-Free Workplace Program" in text def test_all_modes_render(): with tempfile.TemporaryDirectory() as d: for mode in MODE_META: out = os.path.join(d, f"{mode}.pdf") path = generate_da_binder( output_path=out, carrier_name="Test Co", dot_number="1", mode=mode, cdl_drivers=2, ) assert path and os.path.exists(path), f"mode {mode} failed to render" if __name__ == "__main__": test_fmcsa_binder_has_all_sections() test_non_fmcsa_mode_and_state_addendum() test_all_modes_render() print("All DOT D&A binder tests passed.")