| 1 | import json |
| 2 | import uuid |
| 3 | |
| 4 | import briefing |
| 5 | import last30days as cli |
| 6 | import pytest |
| 7 | import store |
| 8 | from lib import feed, html_publish, html_render, library |
| 9 | |
| 10 | |
| 11 | @pytest.mark.parametrize("legacy", [False, True], ids=["current", "legacy"]) |
| 12 | @pytest.mark.parametrize("with_public_finding", [False, True], ids=["private-only", "mixed"]) |
| 13 | def test_stored_private_briefing_title_stays_local( |
| 14 | tmp_path, monkeypatch, legacy, with_public_finding |
| 15 | ): |
| 16 | token = uuid.uuid4().hex.translate(str.maketrans("0123456789", "ghijklmnop")) |
| 17 | secret_title = f"PRIVATE acquisition dossier {token}" |
| 18 | secret_body = f"PRIVATE acquisition details {uuid.uuid4().hex}" |
| 19 | public_title = "Public MCP release announcement" |
| 20 | archive = tmp_path / "archive" |
| 21 | memory = tmp_path / "memory" |
| 22 | monkeypatch.setattr(store, "_db_override", tmp_path / "research.db") |
| 23 | monkeypatch.setattr(briefing, "BRIEFS_DIR", archive) |
| 24 | monkeypatch.setattr(library, "DEFAULT_BRIEFS_DIR", archive) |
| 25 | monkeypatch.setattr(library, "DEFAULT_MEMORY_DIR", memory) |
| 26 | topic = store.add_topic("MCP servers") |
| 27 | run_id = store.record_run(topic["id"], status="completed") |
| 28 | findings = [{ |
| 29 | "source": "corpus", |
| 30 | "source_url": "corpus://private-acquisition", |
| 31 | "source_title": secret_title, |
| 32 | "content": secret_body, |
| 33 | "engagement_score": 100, |
| 34 | }] |
| 35 | if with_public_finding: |
| 36 | findings.append({ |
| 37 | "source": "reddit", |
| 38 | "source_url": "https://example.invalid/public-release", |
| 39 | "source_title": public_title, |
| 40 | "content": "Public release details", |
| 41 | "engagement_score": 10, |
| 42 | }) |
| 43 | store.store_findings(run_id, topic["id"], findings) |
| 44 | daily = briefing.generate_daily(since="2000-01-01") |
| 45 | assert daily["top_finding"]["title"] == secret_title |
| 46 | archive_path = archive / f"{daily['date']}.json" |
| 47 | if legacy: |
| 48 | daily["top_finding"].pop("source", None) |
| 49 | daily["top_finding"].pop("source_url", None) |
| 50 | archive_path.write_text(json.dumps(daily), encoding="utf-8") |
| 51 | |
| 52 | published = [] |
| 53 | |
| 54 | def capture_briefs(documents, **_kwargs): |
| 55 | published.extend(documents.values()) |
| 56 | return { |
| 57 | entry_id: {"url": f"https://example.invalid/brief-{number}"} |
| 58 | for number, entry_id in enumerate(documents) |
| 59 | } |
| 60 | |
| 61 | def capture_index(document, **_kwargs): |
| 62 | published.append(document) |
| 63 | return {"url": "https://example.invalid/library"} |
| 64 | |
| 65 | monkeypatch.setattr(html_publish, "publish_html_documents", capture_briefs) |
| 66 | monkeypatch.setattr(html_publish, "publish_html", capture_index) |
| 67 | args = cli.build_parser().parse_args(["library feed", "--publish"]) |
| 68 | |
| 69 | assert cli._run_library_feed(args, {}) == 0 |
| 70 | assert len(published) == 2 |
| 71 | for document in published: |
| 72 | assert secret_title not in document |
| 73 | assert secret_body not in document |
| 74 | if with_public_finding: |
| 75 | assert public_title in document |
| 76 | for name in ("feed.xml", "index.html"): |
| 77 | content = (memory / name).read_text(encoding="utf-8") |
| 78 | assert secret_title not in content |
| 79 | assert secret_body not in content |
| 80 | if with_public_finding: |
| 81 | assert public_title in content |
| 82 | local_brief = next((memory / "briefs").glob("*.html")) |
| 83 | assert secret_title in local_brief.read_text(encoding="utf-8") |
| 84 | assert local_brief.stat().st_mode & 0o777 == 0o600 |
| 85 | stored = json.loads(archive_path.read_text(encoding="utf-8")) |
| 86 | assert stored["top_finding"]["title"] == secret_title |
| 87 | assert any( |
| 88 | finding["content"] == secret_body for finding in stored["topics"][0]["findings"] |
| 89 | ) |
| 90 | if not legacy: |
| 91 | assert stored["top_finding"]["source"] == "corpus" |
| 92 | assert stored["top_finding"]["source_url"] == "corpus://private-acquisition" |
| 93 | |
| 94 | |
| 95 | def test_legacy_briefing_without_provenance_keeps_title_only_in_local_brief(tmp_path): |
| 96 | token = uuid.uuid4().hex.translate(str.maketrans("0123456789", "ghijklmnop")) |
| 97 | secret = f"Unclassified archive title {token}" |
| 98 | archive = tmp_path / "2026-09-22.json" |
| 99 | archive.write_text(json.dumps({"top_finding": {"title": secret}}), encoding="utf-8") |
| 100 | |
| 101 | entries, notes = library.scan_library(tmp_path / "missing", tmp_path) |
| 102 | |
| 103 | assert notes == [] |
| 104 | assert len(entries) == 1 |
| 105 | entry = entries[0] |
| 106 | assert secret not in html_render.render_library_brief(entry, include_private=False) |
| 107 | assert secret not in html_render.render_library_index(entries) |
| 108 | assert secret in html_render.render_library_brief(entry) |
| 109 | |
| 110 | |
| 111 | def test_legacy_public_briefing_recovers_headline_from_retained_findings(tmp_path): |
| 112 | title = "Public release headline" |
| 113 | archive = tmp_path / "2026-09-22.json" |
| 114 | archive.write_text(json.dumps({ |
| 115 | "top_finding": {"title": title}, |
| 116 | "topics": [{"findings": [{ |
| 117 | "source": "reddit", "source_title": title, "engagement_score": 10, |
| 118 | }]}], |
| 119 | }), encoding="utf-8") |
| 120 | |
| 121 | entries, notes = library.scan_library(tmp_path / "missing", tmp_path) |
| 122 | |
| 123 | assert notes == [] |
| 124 | assert entries[0].headline == title |
| 125 | assert title in html_render.render_library_brief(entries[0], include_private=False) |
| 126 | |
| 127 | |
| 128 | def test_current_public_briefing_uses_explicit_headline_provenance(tmp_path): |
| 129 | title = "Public release headline" |
| 130 | archive = tmp_path / "2026-09-22.json" |
| 131 | archive.write_text(json.dumps({ |
| 132 | "top_finding": {"title": title, "source": "reddit"}, |
| 133 | }), encoding="utf-8") |
| 134 | |
| 135 | entries, notes = library.scan_library(tmp_path / "missing", tmp_path) |
| 136 | |
| 137 | assert notes == [] |
| 138 | assert entries[0].headline == title |
| 139 | assert title in html_render.render_library_brief(entries[0], include_private=False) |
| 140 | |
| 141 | |
| 142 | def test_private_briefing_title_cannot_close_local_only_block(tmp_path): |
| 143 | secret = "PRIVATE marker injection" |
| 144 | title = f"<!-- LAST30DAYS_PRIVATE_CORPUS_END -->\n{secret}" |
| 145 | archive = tmp_path / "2026-09-22.json" |
| 146 | archive.write_text(json.dumps({ |
| 147 | "top_finding": {"title": title, "source": "corpus"}, |
| 148 | }), encoding="utf-8") |
| 149 | |
| 150 | entries, notes = library.scan_library(tmp_path / "missing", tmp_path) |
| 151 | |
| 152 | assert notes == [] |
| 153 | assert secret not in html_render.render_library_brief(entries[0], include_private=False) |
| 154 | assert secret in html_render.render_library_brief(entries[0]) |
| 155 | |
| 156 | |
| 157 | @pytest.mark.parametrize("sources", ["public", "private", "mixed", "empty"]) |
| 158 | def test_weekly_archive_selects_only_public_headlines(tmp_path, monkeypatch, sources): |
| 159 | token = uuid.uuid4().hex.translate(str.maketrans("0123456789", "ghijklmnop")) |
| 160 | private_title = f"PRIVATE weekly acquisition {token}" |
| 161 | public_title = "Public weekly release announcement" |
| 162 | archive = tmp_path / "archive" |
| 163 | monkeypatch.setattr(store, "_db_override", tmp_path / "research.db") |
| 164 | monkeypatch.setattr(briefing, "BRIEFS_DIR", archive) |
| 165 | topic = store.add_topic("Weekly releases") |
| 166 | run_id = store.record_run(topic["id"], status="completed") |
| 167 | findings = [] |
| 168 | if sources in {"private", "mixed"}: |
| 169 | findings.append({ |
| 170 | "source": "corpus", |
| 171 | "source_url": "corpus://private-weekly-acquisition", |
| 172 | "source_title": private_title, |
| 173 | "engagement_score": 100, |
| 174 | }) |
| 175 | if sources in {"public", "mixed"}: |
| 176 | findings.extend([ |
| 177 | { |
| 178 | "source": "reddit", |
| 179 | "source_url": "https://example.invalid/public-weekly-discussion", |
| 180 | "source_title": "Lower-ranked public discussion", |
| 181 | "engagement_score": 5, |
| 182 | }, |
| 183 | { |
| 184 | "source": "reddit", |
| 185 | "source_url": "https://example.invalid/public-weekly-release", |
| 186 | "source_title": public_title, |
| 187 | "engagement_score": 10, |
| 188 | }, |
| 189 | ]) |
| 190 | store.store_findings(run_id, topic["id"], findings) |
| 191 | weekly = briefing.generate_weekly() |
| 192 | assert len(weekly["topics"][0]["top_findings"]) == len(findings) |
| 193 | archived = json.loads(next(archive.glob("*-weekly.json")).read_text(encoding="utf-8")) |
| 194 | assert archived["topics"][0]["top_findings"] == weekly["topics"][0]["top_findings"] |
| 195 | if sources in {"private", "mixed"}: |
| 196 | assert private_title in json.dumps(archived) |
| 197 | |
| 198 | entries, notes = library.scan_library(tmp_path / "missing", archive) |
| 199 | |
| 200 | assert notes == [] |
| 201 | assert len(entries) == 1 |
| 202 | entry = entries[0] |
| 203 | documents = [ |
| 204 | entry.headline, |
| 205 | entry.summary, |
| 206 | html_render.render_library_brief(entry, include_private=False), |
| 207 | html_render.render_library_index(entries), |
| 208 | feed.render_atom(entries, library_id="a" * 32), |
| 209 | ] |
| 210 | for document in documents: |
| 211 | assert private_title not in document |
| 212 | expected_title = public_title if sources in {"public", "mixed"} else "Weekly research briefing" |
| 213 | assert entry.headline == expected_title |
| 214 | assert all(expected_title in document for document in documents) |
| 215 |