diff --git a/src/netcheck/exporter.py b/src/netcheck/exporter.py index 898d81b..a75453f 100644 --- a/src/netcheck/exporter.py +++ b/src/netcheck/exporter.py @@ -89,8 +89,9 @@ def egress_asn(report: dict[str, Any]) -> str | None: def write_report(report: dict[str, Any], markdown: str, logs_dir: Path) -> tuple[Path, Path]: - started = (report.get("meta") or {}).get("started_at", "") - asn = egress_asn(report) + meta = report.get("meta") or {} + started = meta.get("started_at", "") + asn = meta.get("target") or egress_asn(report) base = Path(logs_dir) json_path = atomic_write(base / report_filename(asn, started, "json"), dump_json(report)) md_path = atomic_write(base / report_filename(asn, started, "md"), markdown) diff --git a/tests/test_exporter.py b/tests/test_exporter.py index f8df2eb..1e6883d 100644 --- a/tests/test_exporter.py +++ b/tests/test_exporter.py @@ -193,6 +193,13 @@ def test_egress_asn_is_read_out_of_the_assembled_report(): assert egress_asn({}) is None +def test_write_report_names_the_file_after_the_target_in_target_mode(tmp_path: Path): + report = build_report({**meta(), "mode": "target", "target": "AS15169"}, modules(), [], {}) + json_path, md_path = write_report(report, "# netcheck report\n", tmp_path) + assert json_path.name == "report_AS15169_20260808T191200Z.json" + assert md_path.name == "report_AS15169_20260808T191200Z.md" + + def test_atomic_write_creates_the_directory_and_leaves_no_temp_file(tmp_path: Path): target = tmp_path / "logs" / "report.json" atomic_write(target, '{"a": 1}')