feat: init Scalper — pipeline légal scrape+traduction (SCAL-1..SCAL-12)
- legal-source-check (liste de blocage scanlation, robots.txt, rate limiting) - sources: demo (fixture) + mangadex (API officielle) - cleaner, translator (LLM Mistral + glossaire), QA, exporter (md/json/html) - CLI + pipeline resumable (state.json), 22 tests unitaires - sous-agents Mistral Agents SDK: orchestrator + scraper/translator/qa - skills/ (6 SKILL.md), docs légales, CI GitHub Actions Refs: Jira SCAL-1 (Epic CDC), SCAL-2..SCAL-12 (backlog Scrum)
This commit is contained in:
commit
f38a1d630b
38 files changed
+1733
No files matched your search
@@ -0,0 +1,40 @@
|
||||
import unittest
|
||||
from importlib import resources
|
||||
|
||||
from scalper.cleaner import clean_chapter, detect_language
|
||||
|
||||
|
||||
class TestCleaner(unittest.TestCase):
|
||||
def setUp(self):
|
||||
raw = (resources.files("scalper") / "data" / "demo" / "chapter-1.html").read_text(encoding="utf-8")
|
||||
self.chapter = clean_chapter(raw, {"source": "demo", "license": "fixture"})
|
||||
|
||||
def test_boilerplate_removed(self):
|
||||
text = self.chapter.full_text().lower()
|
||||
self.assertNotIn("soutenez-nous", text)
|
||||
self.assertNotIn("scanlation", text)
|
||||
self.assertNotIn("suivant", text)
|
||||
|
||||
def test_blocks_typed(self):
|
||||
types = [b.type for b in self.chapter.blocks]
|
||||
self.assertIn("narration", types)
|
||||
self.assertIn("dialogue", types)
|
||||
self.assertIn("onomatopoeia", types)
|
||||
self.assertIn("image", types)
|
||||
|
||||
def test_image_placeholder(self):
|
||||
ph = self.chapter.placeholders()
|
||||
self.assertEqual(len(ph), 1)
|
||||
self.assertTrue(ph[0].startswith("[IMG:"))
|
||||
|
||||
def test_language_detected(self):
|
||||
self.assertEqual(self.chapter.meta["language"], "en")
|
||||
|
||||
def test_detect_language(self):
|
||||
self.assertEqual(detect_language("Bonjour le monde, ceci est un test"), "fr")
|
||||
self.assertEqual(detect_language("Hello the world this is a test"), "en")
|
||||
self.assertEqual(detect_language("これはテストです"), "ja")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
@@ -0,0 +1,32 @@
|
||||
import unittest
|
||||
|
||||
from scalper import legal
|
||||
|
||||
|
||||
class TestLegal(unittest.TestCase):
|
||||
def test_demo_allowed(self):
|
||||
v = legal.check_source("demo")
|
||||
self.assertTrue(v.allowed)
|
||||
|
||||
def test_mangas_origines_blocked(self):
|
||||
for url in ("https://mangas-origines.fr/", "https://www.mangas-origines.fr/manga/x", "mangas-origines.fr"):
|
||||
v = legal.check_source(url)
|
||||
self.assertFalse(v.allowed, url)
|
||||
self.assertIn("blocage", v.reasons[0])
|
||||
|
||||
def test_unknown_domain_blocked(self):
|
||||
v = legal.check_source("https://site-inconnu.example/manga")
|
||||
self.assertFalse(v.allowed)
|
||||
|
||||
def test_mangadex_trusted(self):
|
||||
v = legal.check_source("api.mangadex.org")
|
||||
self.assertTrue(v.allowed)
|
||||
self.assertIn("MangaDex", v.license)
|
||||
|
||||
def test_domain_of(self):
|
||||
self.assertEqual(legal.domain_of("https://WWW.Example.COM/x"), "www.example.com")
|
||||
self.assertEqual(legal.domain_of("demo"), "demo")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
@@ -0,0 +1,64 @@
|
||||
import json
|
||||
import tempfile
|
||||
import unittest
|
||||
from pathlib import Path
|
||||
|
||||
from scalper import pipeline
|
||||
from scalper.exporter import export_chapter, slugify
|
||||
from scalper.models import Block, Chapter
|
||||
from scalper.sources import get_adapter
|
||||
|
||||
|
||||
class TestPipeline(unittest.TestCase):
|
||||
def test_blocked_source(self):
|
||||
with tempfile.TemporaryDirectory() as tmp:
|
||||
res = pipeline.run_pipeline("https://mangas-origines.fr", "x", "fr", out_root=tmp, mock_llm=True)
|
||||
self.assertEqual(res["status"], "blocked")
|
||||
|
||||
def test_demo_end_to_end_mock_llm(self):
|
||||
with tempfile.TemporaryDirectory() as tmp:
|
||||
res = pipeline.run_pipeline("demo", "demo-1", "fr", out_root=tmp, mock_llm=True)
|
||||
self.assertEqual(res["status"], "done")
|
||||
self.assertEqual(len(res["chapters"]), 2)
|
||||
for ch in res["chapters"]:
|
||||
self.assertEqual(ch["status"], "ok", ch)
|
||||
self.assertGreaterEqual(ch["qa"]["score"], 80)
|
||||
state = json.loads((Path(tmp) / "state.json").read_text(encoding="utf-8"))
|
||||
self.assertEqual(len(state["done"]), 2)
|
||||
raw1 = Path(tmp) / "raw" / "demo" / "demo-1" / "demo-1-ch1"
|
||||
self.assertTrue((raw1 / "chapter.fr.json").exists())
|
||||
self.assertTrue((raw1 / "export" / (raw1.name + ".md").replace("demo-1-ch1", "demo-1-1-fr")).exists() or list((raw1 / "export").glob("*.md")))
|
||||
|
||||
def test_resume_skips_fetched(self):
|
||||
with tempfile.TemporaryDirectory() as tmp:
|
||||
pipeline.run_pipeline("demo", "demo-1", "fr", out_root=tmp, mock_llm=True, max_chapters=1)
|
||||
res = pipeline.run_pipeline("demo", "demo-1", "fr", out_root=tmp, mock_llm=True)
|
||||
self.assertTrue(any(c.get("skipped") for c in res["chapters"]))
|
||||
|
||||
|
||||
class TestExporter(unittest.TestCase):
|
||||
def test_exports_contain_license(self):
|
||||
ch = Chapter(
|
||||
meta={"source": "demo", "license": "fixture test", "fetched_at": "2026-01-01", "url": "demo://x", "title": "T", "language": "fr"},
|
||||
blocks=[Block(index=0, type="narration", text="Bonjour"), Block(index=1, type="image", image="images/p.png")],
|
||||
)
|
||||
with tempfile.TemporaryDirectory() as tmp:
|
||||
md = export_chapter(ch, "md", Path(tmp))
|
||||
self.assertIn("Source et licence", md.read_text(encoding="utf-8"))
|
||||
self.assertIn("[IMG:1]", md.read_text(encoding="utf-8"))
|
||||
js = export_chapter(ch, "json", Path(tmp))
|
||||
self.assertEqual(json.loads(js.read_text(encoding="utf-8"))["blocks"][1]["image"], "images/p.png")
|
||||
ht = export_chapter(ch, "html", Path(tmp))
|
||||
self.assertIn("Bonjour", ht.read_text(encoding="utf-8"))
|
||||
|
||||
def test_slugify(self):
|
||||
self.assertEqual(slugify("Chapitre 12 : L'orage !"), "chapitre-12-l-orage")
|
||||
|
||||
def test_adapter_registry(self):
|
||||
self.assertEqual(get_adapter("demo").id, "demo")
|
||||
with self.assertRaises(KeyError):
|
||||
get_adapter("inconnu")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
@@ -0,0 +1,63 @@
|
||||
import unittest
|
||||
from importlib import resources
|
||||
|
||||
from scalper.cleaner import clean_chapter
|
||||
from scalper.models import Block, Chapter
|
||||
from scalper.qa import qa_chapter
|
||||
from scalper.translator import FakeLLM, chunk_blocks, translate_chapter
|
||||
|
||||
|
||||
def demo_chapter():
|
||||
raw = (resources.files("scalper") / "data" / "demo" / "chapter-1.html").read_text(encoding="utf-8")
|
||||
return clean_chapter(raw, {"source": "demo", "license": "fixture", "language": "en"})
|
||||
|
||||
|
||||
class TestTranslator(unittest.TestCase):
|
||||
def test_translate_preserves_structure(self):
|
||||
src = demo_chapter()
|
||||
glossary = {"hero": "héros"}
|
||||
tr = translate_chapter(src, "fr", glossary, FakeLLM())
|
||||
self.assertEqual(len(tr.blocks), len(src.blocks))
|
||||
self.assertEqual(tr.placeholders(), src.placeholders())
|
||||
self.assertEqual(tr.meta["language"], "fr")
|
||||
self.assertNotEqual(tr.full_text(), src.full_text())
|
||||
|
||||
def test_chunking(self):
|
||||
blocks = [Block(index=i, type="narration", text="x" * 100) for i in range(100)]
|
||||
chunks = chunk_blocks(blocks, max_chars=1000)
|
||||
self.assertGreater(len(chunks), 1)
|
||||
self.assertEqual(sum(len(c) for c in chunks), 100)
|
||||
|
||||
|
||||
class TestQA(unittest.TestCase):
|
||||
def test_good_translation_passes(self):
|
||||
src = demo_chapter()
|
||||
tr = translate_chapter(src, "fr", {}, FakeLLM())
|
||||
report = qa_chapter(src, tr, {}, "fr")
|
||||
self.assertTrue(report["pass"], report)
|
||||
self.assertGreaterEqual(report["score"], 80)
|
||||
|
||||
def test_glossary_violation_detected(self):
|
||||
src = Chapter(meta={"language": "en"}, blocks=[Block(index=0, type="narration", text="the hero wins")])
|
||||
tr = Chapter(meta={"language": "fr"}, blocks=[Block(index=0, type="narration", text="le champion gagne")])
|
||||
report = qa_chapter(src, tr, {"hero": "héros"}, "fr")
|
||||
self.assertFalse(report["pass"])
|
||||
self.assertTrue(any(i["type"] == "glossary" for i in report["issues"]))
|
||||
|
||||
def test_block_count_mismatch_critical(self):
|
||||
src = Chapter(meta={}, blocks=[Block(index=0, type="narration", text="a"), Block(index=1, type="narration", text="b")])
|
||||
tr = Chapter(meta={}, blocks=[Block(index=0, type="narration", text="a")])
|
||||
report = qa_chapter(src, tr, {}, "fr")
|
||||
self.assertFalse(report["pass"])
|
||||
self.assertTrue(any(i.get("critical") for i in report["issues"]))
|
||||
|
||||
def test_wrong_language_critical(self):
|
||||
src = Chapter(meta={"language": "en"}, blocks=[Block(index=0, type="narration", text="the hero wins today")])
|
||||
tr = Chapter(meta={"language": "fr"}, blocks=[Block(index=0, type="narration", text="the hero wins today")])
|
||||
report = qa_chapter(src, tr, {}, "fr")
|
||||
self.assertFalse(report["pass"])
|
||||
self.assertTrue(any(i["type"] == "wrong_language" for i in report["issues"]))
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
Reference in new issue
Block a user