import json import unittest from pathlib import Path from indexer.cluster import Post, cluster_posts from indexer.write_index import write_index ROOT = Path(__file__).resolve().parents[2] FIXTURE = ROOT / "testdata" / "posts.json" class ClusterTests(unittest.TestCase): def setUp(self): raw = json.loads(FIXTURE.read_text(encoding="utf-8"))["data"] self.posts = [Post.from_reddit(p) for p in raw] self.posts = [p for p in self.posts if p] def test_groups_a_series_by_title_and_links(self): novels = {n.name.lower(): n for n in cluster_posts(self.posts)} # Meta en link-posts vallen af; drie verhalen over. self.assertEqual(len(novels), 3) terran = novels["the last terran"] self.assertEqual([c.id for c in terran.chapters], ["aaa111", "aaa222", "aaa333"]) self.assertEqual(terran.author, "WriterOne") self.assertFalse(terran.oneshot) self.assertEqual(terran.id, "aaa111") def test_oneshot_and_nsfw(self): novels = {n.author: n for n in cluster_posts(self.posts)} self.assertTrue(novels["WriterTwo"].oneshot) self.assertTrue(novels["WriterThree"].over_18) self.assertEqual(len(novels["WriterThree"].chapters), 2) def test_write_index_contains_no_story_text(self): import tempfile novels = cluster_posts(self.posts) with tempfile.TemporaryDirectory() as tmp: meta = write_index(novels, Path(tmp), page_size=2) blob = Path(tmp).joinpath("series/a/aaa111.json").read_text(encoding="utf-8") self.assertNotIn("It begins", blob) self.assertNotIn("The end of this chapter", blob) self.assertIn("aaa111", blob) self.assertEqual(meta["novelCount"], 3) self.assertEqual(meta["pageCount"], 2) self.assertTrue((Path(tmp) / "catalog/page-0001.json").exists()) self.assertTrue((Path(tmp) / "search-index.json").exists()) import json payload = json.loads((Path(tmp) / "catalog/page-0001.json").read_text(encoding="utf-8")) cover = payload["novels"][0]["cover"] self.assertTrue(cover and cover.startswith("covers/") and (cover.endswith(".png") or cover.endswith(".svg"))) first = (Path(tmp) / cover).read_bytes() self.assertTrue(first.startswith(b"\x89PNG") or first.startswith(b"