import csv import os import stat import subprocess import textwrap import unittest from pathlib import Path from tempfile import TemporaryDirectory ROOT = Path(__file__).resolve().parents[1] IMPORTINFO_HTML = textwrap.dedent( """

Search Results

Run DateMaster BOLHouse BOLVoyage # Bill TypeCarrier CodeIMO #Vessel Name Arrival DateUS PortForeign PortQuantity WeightType of ServiceShipperConsignee Notify PartyCommodity
2026-05-01EGLV142653125618SNHBSHALAX264014084E House BillSNHB9604081EVER LOGIC 2026-05-01LOS ANGELES, CALIFORNIASHANGHAI CHINA (MAINLAND) 42 PKG12,578 KHouse to House TECH-FRONT (CHONGQING) COMPUTER COCEVA C/O VALVE CORPORATION CEVA C/O VALVE CORPORATIONGAME CONSOLE
2026-04-28CMDUCHN3170474EXDO6211281510XRAJ House BillEXDO9436379CMA CGM SAMSON 2026-04-28SAVANNAH, GEORGIASHANGHAI CHINA (MAINLAND) 15 PKG4,143 KGPier to Pier TECH-FRONT (CHONGQING) COMPUTER COPROMETHEAN INC. CHROMEBOX HTS:
""" ) def run_parser(args, reports): return subprocess.run( [ "python3", str(ROOT / "scripts" / "parse_importinfo_shipments.py"), *args, "--report-dir", str(reports), ], cwd=ROOT, text=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, check=False, ) def read_rows(reports): return list( csv.DictReader( (reports / "customs-shipments.tsv").read_text(encoding="utf-8").splitlines(), delimiter="\t", ) ) def write_executable(path, content): path.write_text(textwrap.dedent(content).lstrip(), encoding="utf-8") path.chmod(path.stat().st_mode | stat.S_IXUSR) class CustomsShipmentParserTests(unittest.TestCase): def test_extracts_relevant_importinfo_rows(self): with TemporaryDirectory() as tmp: tmp_path = Path(tmp) html = tmp_path / "ceva-valve.html" html.write_text(IMPORTINFO_HTML, encoding="utf-8") reports = tmp_path / "reports" result = run_parser(["--input", f"ceva-valve={html}"], reports) self.assertEqual("", result.stderr) self.assertEqual(0, result.returncode) rows = read_rows(reports) self.assertEqual(1, len(rows)) self.assertEqual("SNHBSHALAX264014", rows[0]["house_bol"]) self.assertEqual("GAME CONSOLE", rows[0]["commodity"]) self.assertEqual("CEVA C/O VALVE CORPORATION", rows[0]["consignee"]) key_lines = (reports / "customs-shipments-key-lines.txt").read_text( encoding="utf-8" ) self.assertIn("2026-05-01", key_lines) self.assertIn("SNHBSHALAX264014", key_lines) self.assertIn("GAME CONSOLE", key_lines) report = (reports / "customs-shipments.md").read_text(encoding="utf-8") self.assertIn("## Newest Relevant Shipments", report) self.assertIn("CEVA C/O VALVE CORPORATION", report) self.assertIn("TECH-FRONT (CHONGQING) COMPUTER CO", report) def test_preserves_distinct_same_day_bols(self): with TemporaryDirectory() as tmp: tmp_path = Path(tmp) html = tmp_path / "same-day.html" html.write_text( IMPORTINFO_HTML.replace( "", textwrap.dedent( """ 2026-05-01EGLV142653125669SNHBSHALAX264015084E House BillSNHB9604081EVER LOGIC 2026-05-01LOS ANGELES, CALIFORNIASHANGHAI CHINA (MAINLAND) 42 PKG12,596 KHouse to House TECH-FRONT (CHONGQING) COMPUTER COCEVA C/O VALVE CORPORATION CEVA C/O VALVE CORPORATIONGAME CONSOLE """ ), ), encoding="utf-8", ) reports = tmp_path / "reports" result = run_parser(["--input", f"ceva-valve={html}"], reports) self.assertEqual(0, result.returncode) rows = read_rows(reports) self.assertEqual( ["SNHBSHALAX264014", "SNHBSHALAX264015"], [row["house_bol"] for row in rows], ) def test_ignores_later_unrelated_table_headers(self): with TemporaryDirectory() as tmp: tmp_path = Path(tmp) html = tmp_path / "extra-table.html" html.write_text( IMPORTINFO_HTML.replace( "", textwrap.dedent( """
NameDescription
PROMETHEAN INC.CHROMEBOX HTS:
""" ), ), encoding="utf-8", ) reports = tmp_path / "reports" result = run_parser(["--input", f"ceva-valve={html}"], reports) self.assertEqual("", result.stderr) self.assertEqual(0, result.returncode) rows = read_rows(reports) self.assertEqual(["SNHBSHALAX264014"], [row["house_bol"] for row in rows]) def test_matches_relevance_terms_split_by_inline_markup(self): with TemporaryDirectory() as tmp: tmp_path = Path(tmp) html = tmp_path / "split-cells.html" html.write_text( IMPORTINFO_HTML.replace( "CEVA C/O VALVE CORPORATIONGAME CONSOLE", "CEVA C/O VALVE CORPORATIONGAME
CONSOLE", ), encoding="utf-8", ) reports = tmp_path / "reports" result = run_parser(["--input", f"ceva-valve={html}"], reports) self.assertEqual("", result.stderr) self.assertEqual(0, result.returncode) rows = read_rows(reports) self.assertEqual(1, len(rows)) self.assertEqual("CEVA C/O VALVE CORPORATION", rows[0]["consignee"]) self.assertEqual("GAME CONSOLE", rows[0]["commodity"]) def test_manifest_inputs_dedupe_duplicate_bols(self): with TemporaryDirectory() as tmp: tmp_path = Path(tmp) first = tmp_path / "first.html" second = tmp_path / "second.html" manifest = tmp_path / "manifest.tsv" first.write_text(IMPORTINFO_HTML, encoding="utf-8") second.write_text(IMPORTINFO_HTML, encoding="utf-8") manifest.write_text( "\n".join( ( "slug\tsource_url\thtml_path", f"ceva-valve\thttps://example.test/ceva\t{first}", f"ceva-valve\thttps://example.test/ceva\t{second}", ) ) + "\n", encoding="utf-8", ) reports = tmp_path / "reports" result = run_parser(["--manifest", str(manifest)], reports) self.assertEqual("", result.stderr) self.assertEqual(0, result.returncode) rows = read_rows(reports) self.assertEqual(["SNHBSHALAX264014"], [row["house_bol"] for row in rows]) self.assertEqual("https://example.test/ceva", rows[0]["source_url"]) class CheckCustomsShipmentsShellTests(unittest.TestCase): def test_fetches_pages_and_runs_parser(self): with TemporaryDirectory() as tmp: tmp_path = Path(tmp) run_dir = tmp_path / "run" fake_bin = tmp_path / "bin" fake_bin.mkdir() write_executable( fake_bin / "curl", f""" #!/bin/sh cat <<'HTML' {IMPORTINFO_HTML} HTML """, ) env = os.environ.copy() env["PATH"] = f"{fake_bin}{os.pathsep}{env['PATH']}" result = subprocess.run( [str(ROOT / "scripts" / "check_customs_shipments.sh"), str(run_dir)], cwd=ROOT, env=env, text=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, check=False, ) self.assertEqual("", result.stderr) self.assertEqual(0, result.returncode) self.assertTrue( (run_dir / "api" / "customs" / "importinfo-ceva-valve.html").exists() ) key_lines = ( run_dir / "reports" / "customs-shipments-key-lines.txt" ).read_text(encoding="utf-8") self.assertIn("SNHBSHALAX264014", key_lines) def test_records_blocked_fetches_without_failing_run(self): with TemporaryDirectory() as tmp: tmp_path = Path(tmp) run_dir = tmp_path / "run" fake_bin = tmp_path / "bin" fake_bin.mkdir() write_executable( fake_bin / "curl", """ #!/bin/sh exit 22 """, ) env = os.environ.copy() env["PATH"] = f"{fake_bin}{os.pathsep}{env['PATH']}" result = subprocess.run( [str(ROOT / "scripts" / "check_customs_shipments.sh"), str(run_dir)], cwd=ROOT, env=env, text=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, check=False, ) self.assertEqual("", result.stderr) self.assertEqual(0, result.returncode) errors = (run_dir / "reports" / "customs-shipments-errors.txt").read_text( encoding="utf-8" ) self.assertIn("failed to fetch", errors) class CustomsSummaryIntegrationTests(unittest.TestCase): def test_run_summary_and_status_draft_include_customs_lines(self): with TemporaryDirectory() as tmp: run_dir = Path(tmp) / "2026-05-05" reports = run_dir / "reports" reports.mkdir(parents=True) (reports / "customs-shipments-key-lines.txt").write_text( "2026-05-01\tCEVA C/O VALVE CORPORATION\tTECH-FRONT (CHONGQING) COMPUTER CO\tGAME CONSOLE\t42 PKG\t12596 Kgs\tSNHBSHALAX264015\n", encoding="utf-8", ) (reports / "customs-shipments-errors.txt").write_text("", encoding="utf-8") summary = subprocess.run( [ "python3", str(ROOT / "scripts" / "write_run_summary.py"), "--run-dir", str(run_dir), ], cwd=ROOT, text=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, check=False, ) draft = subprocess.run( [ "python3", str(ROOT / "scripts" / "draft_status_update.py"), "--run-dir", str(run_dir), ], cwd=ROOT, text=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, check=False, ) self.assertEqual(0, summary.returncode) self.assertEqual(0, draft.returncode) run_summary = (reports / "run-summary.md").read_text(encoding="utf-8") status_draft = (reports / "status-draft.md").read_text(encoding="utf-8") self.assertIn("Customs shipments", run_summary) self.assertIn("SNHBSHALAX264015", run_summary) self.assertIn("Customs / Shipments", status_draft) self.assertIn("GAME CONSOLE", status_draft) if __name__ == "__main__": unittest.main()