|
18 | 18 | _read_wiki_context, |
19 | 19 | _read_concept_briefs, |
20 | 20 | _add_related_link, |
| 21 | + _backlink_summary, |
| 22 | + _backlink_concepts, |
21 | 23 | ) |
22 | 24 |
|
23 | 25 |
|
@@ -207,6 +209,114 @@ def test_sorted_alphabetically(self, tmp_path): |
207 | 209 | assert slugs == ["apple", "mango", "zebra"] |
208 | 210 |
|
209 | 211 |
|
| 212 | +class TestBacklinkSummary: |
| 213 | + def test_adds_missing_concept_links(self, tmp_path): |
| 214 | + wiki = tmp_path / "wiki" |
| 215 | + summaries = wiki / "summaries" |
| 216 | + summaries.mkdir(parents=True) |
| 217 | + (summaries / "paper.md").write_text( |
| 218 | + "---\nsources: [paper.pdf]\n---\n\n# Summary\n\nContent about attention.", |
| 219 | + encoding="utf-8", |
| 220 | + ) |
| 221 | + _backlink_summary(wiki, "paper", ["attention", "transformer"]) |
| 222 | + text = (summaries / "paper.md").read_text() |
| 223 | + assert "[[concepts/attention]]" in text |
| 224 | + assert "[[concepts/transformer]]" in text |
| 225 | + |
| 226 | + def test_skips_already_linked(self, tmp_path): |
| 227 | + wiki = tmp_path / "wiki" |
| 228 | + summaries = wiki / "summaries" |
| 229 | + summaries.mkdir(parents=True) |
| 230 | + (summaries / "paper.md").write_text( |
| 231 | + "---\nsources: [paper.pdf]\n---\n\n# Summary\n\nSee [[concepts/attention]].", |
| 232 | + encoding="utf-8", |
| 233 | + ) |
| 234 | + _backlink_summary(wiki, "paper", ["attention", "transformer"]) |
| 235 | + text = (summaries / "paper.md").read_text() |
| 236 | + # attention already linked, should not duplicate |
| 237 | + assert text.count("[[concepts/attention]]") == 1 |
| 238 | + # transformer should be added |
| 239 | + assert "[[concepts/transformer]]" in text |
| 240 | + |
| 241 | + def test_no_op_when_all_linked(self, tmp_path): |
| 242 | + wiki = tmp_path / "wiki" |
| 243 | + summaries = wiki / "summaries" |
| 244 | + summaries.mkdir(parents=True) |
| 245 | + original = "# Summary\n\n[[concepts/attention]] and [[concepts/transformer]]" |
| 246 | + (summaries / "paper.md").write_text(original, encoding="utf-8") |
| 247 | + _backlink_summary(wiki, "paper", ["attention", "transformer"]) |
| 248 | + assert (summaries / "paper.md").read_text() == original |
| 249 | + |
| 250 | + def test_skips_if_file_missing(self, tmp_path): |
| 251 | + wiki = tmp_path / "wiki" |
| 252 | + wiki.mkdir() |
| 253 | + # Should not raise |
| 254 | + _backlink_summary(wiki, "nonexistent", ["attention"]) |
| 255 | + |
| 256 | + def test_merges_into_existing_section(self, tmp_path): |
| 257 | + """Second add should merge into existing ## Related Concepts, not duplicate.""" |
| 258 | + wiki = tmp_path / "wiki" |
| 259 | + summaries = wiki / "summaries" |
| 260 | + summaries.mkdir(parents=True) |
| 261 | + (summaries / "paper.md").write_text( |
| 262 | + "# Summary\n\nContent.\n\n## Related Concepts\n- [[concepts/attention]]\n", |
| 263 | + encoding="utf-8", |
| 264 | + ) |
| 265 | + _backlink_summary(wiki, "paper", ["attention", "transformer"]) |
| 266 | + text = (summaries / "paper.md").read_text() |
| 267 | + assert text.count("## Related Concepts") == 1 |
| 268 | + assert "[[concepts/transformer]]" in text |
| 269 | + assert text.count("[[concepts/attention]]") == 1 |
| 270 | + |
| 271 | + |
| 272 | +class TestBacklinkConcepts: |
| 273 | + def test_adds_summary_link_to_concept(self, tmp_path): |
| 274 | + wiki = tmp_path / "wiki" |
| 275 | + concepts = wiki / "concepts" |
| 276 | + concepts.mkdir(parents=True) |
| 277 | + (concepts / "attention.md").write_text( |
| 278 | + "---\nsources: [paper.pdf]\n---\n\n# Attention\n\nContent.", |
| 279 | + encoding="utf-8", |
| 280 | + ) |
| 281 | + _backlink_concepts(wiki, "paper", ["attention"]) |
| 282 | + text = (concepts / "attention.md").read_text() |
| 283 | + assert "[[summaries/paper]]" in text |
| 284 | + assert "## Related Documents" in text |
| 285 | + |
| 286 | + def test_skips_if_already_linked(self, tmp_path): |
| 287 | + wiki = tmp_path / "wiki" |
| 288 | + concepts = wiki / "concepts" |
| 289 | + concepts.mkdir(parents=True) |
| 290 | + (concepts / "attention.md").write_text( |
| 291 | + "# Attention\n\nBased on [[summaries/paper]].", |
| 292 | + encoding="utf-8", |
| 293 | + ) |
| 294 | + _backlink_concepts(wiki, "paper", ["attention"]) |
| 295 | + text = (concepts / "attention.md").read_text() |
| 296 | + assert text.count("[[summaries/paper]]") == 1 |
| 297 | + assert "## Related Documents" not in text |
| 298 | + |
| 299 | + def test_merges_into_existing_section(self, tmp_path): |
| 300 | + wiki = tmp_path / "wiki" |
| 301 | + concepts = wiki / "concepts" |
| 302 | + concepts.mkdir(parents=True) |
| 303 | + (concepts / "attention.md").write_text( |
| 304 | + "# Attention\n\n## Related Documents\n- [[summaries/old-paper]]\n", |
| 305 | + encoding="utf-8", |
| 306 | + ) |
| 307 | + _backlink_concepts(wiki, "new-paper", ["attention"]) |
| 308 | + text = (concepts / "attention.md").read_text() |
| 309 | + assert text.count("## Related Documents") == 1 |
| 310 | + assert "[[summaries/old-paper]]" in text |
| 311 | + assert "[[summaries/new-paper]]" in text |
| 312 | + |
| 313 | + def test_skips_missing_concept_file(self, tmp_path): |
| 314 | + wiki = tmp_path / "wiki" |
| 315 | + (wiki / "concepts").mkdir(parents=True) |
| 316 | + # Should not raise |
| 317 | + _backlink_concepts(wiki, "paper", ["nonexistent"]) |
| 318 | + |
| 319 | + |
210 | 320 | class TestAddRelatedLink: |
211 | 321 | def test_adds_see_also_link(self, tmp_path): |
212 | 322 | wiki = tmp_path / "wiki" |
|
0 commit comments