|
| 1 | +"""Tests for inventory.build_inventory merge logic.""" |
| 2 | + |
| 3 | +import unittest |
| 4 | +from unittest.mock import patch |
| 5 | + |
| 6 | + |
| 7 | +def _md(doc, title="Test", rev=0, base="P9999"): |
| 8 | + return { |
| 9 | + "doc_number": doc, "base": base, "revision": rev, |
| 10 | + "title": title, "authors": "A", "date": "2026-01-01", |
| 11 | + "audience": "LEWG", "intent": "ask", "brutal_summary": "Test.", |
| 12 | + "md_path": f"/src/{doc.lower()}.md", "md_mtime": 1000.0, |
| 13 | + "folder_idx": 1, |
| 14 | + } |
| 15 | + |
| 16 | + |
| 17 | +def _pdf(doc, rev=0, base="P9999"): |
| 18 | + return { |
| 19 | + "doc_number": doc, "base": base, "revision": rev, |
| 20 | + "title": "", "authors": "", "date": "", "audience": "", |
| 21 | + "brutal_summary": None, |
| 22 | + "pdf_path": f"/out/{doc.lower()}.pdf", "pdf_mtime": 2000.0, |
| 23 | + } |
| 24 | + |
| 25 | + |
| 26 | +class TestBuildInventory(unittest.TestCase): |
| 27 | + |
| 28 | + def _build(self, md_papers, pdf_papers, remote_papers=None): |
| 29 | + with patch("lib.inventory.scan_markdown_dirs", return_value=md_papers), \ |
| 30 | + patch("lib.inventory.scan_pdf_dir", return_value=pdf_papers): |
| 31 | + from lib.inventory import build_inventory |
| 32 | + return build_inventory([], "/out", remote_papers) |
| 33 | + |
| 34 | + def _find(self, papers, base): |
| 35 | + return next((p for p in papers if p["base"] == base), None) |
| 36 | + |
| 37 | + def test_pdf_matches_markdown_at_r0(self): |
| 38 | + """Baseline: R0 markdown + R0-keyed PDF merge correctly.""" |
| 39 | + md = {"D4035R0": _md("D4035R0", rev=0, base="P4035")} |
| 40 | + pdf = {"D4035R0": _pdf("D4035R0", rev=0, base="P4035")} |
| 41 | + result = self._build(md, pdf) |
| 42 | + p = self._find(result, "P4035") |
| 43 | + self.assertIsNotNone(p) |
| 44 | + self.assertIsNotNone(p["md_path"]) |
| 45 | + self.assertIsNotNone(p["pdf_path"]) |
| 46 | + |
| 47 | + def test_pdf_matches_markdown_at_r2(self): |
| 48 | + """The bug: filename-derived R0 PDF must match R2 markdown.""" |
| 49 | + md = {"P4003R2": _md("P4003R2", rev=2, base="P4003")} |
| 50 | + # PDF keyed as R0 (filename fallback: d4003-io-awaitables.pdf -> D4003 -> R0) |
| 51 | + pdf = {"D4003R0": _pdf("D4003R0", rev=0, base="P4003")} |
| 52 | + result = self._build(md, pdf) |
| 53 | + p = self._find(result, "P4003") |
| 54 | + self.assertIsNotNone(p) |
| 55 | + self.assertEqual(p["revision"], 2) |
| 56 | + self.assertIsNotNone(p["md_path"]) |
| 57 | + self.assertIsNotNone(p["pdf_path"]) |
| 58 | + |
| 59 | + def test_orphan_pdf_still_appears(self): |
| 60 | + """PDF with no markdown or remote is still in the inventory.""" |
| 61 | + pdf = {"D4099R0": _pdf("D4099R0", rev=0, base="P4099")} |
| 62 | + result = self._build({}, pdf) |
| 63 | + p = self._find(result, "P4099") |
| 64 | + self.assertIsNotNone(p) |
| 65 | + self.assertIsNotNone(p["pdf_path"]) |
| 66 | + self.assertIsNone(p["md_path"]) |
| 67 | + |
| 68 | + def test_pdf_plus_remote_at_non_r0(self): |
| 69 | + """PDF matched by base when remote provides the authoritative revision.""" |
| 70 | + pdf = {"D4007R0": _pdf("D4007R0", rev=0, base="P4007")} |
| 71 | + remote = [{"doc_number": "P4007R2", "title": "Open Issues", |
| 72 | + "author": "A", "status": "Draft", "date": "2026-04-08", |
| 73 | + "form_id": "99", "form_url": "https://isocpp.org/papers/form/99"}] |
| 74 | + result = self._build({}, pdf, remote) |
| 75 | + p = self._find(result, "P4007") |
| 76 | + self.assertIsNotNone(p) |
| 77 | + self.assertEqual(p["revision"], 2) |
| 78 | + self.assertIsNotNone(p["pdf_path"]) |
| 79 | + |
| 80 | + |
| 81 | +if __name__ == "__main__": |
| 82 | + unittest.main() |
0 commit comments