"""The second scroll, read (bishe'arecha §IV): citations found as written, resolved against the snapshot, and given to the anchoring as sources.""" import os from pathlib import Path import pytest REPO = Path(__file__).resolve().parents[2] os.environ.setdefault("MESORAH_MOUNT_POINT", str(REPO / "mesorah")) from korban_processor import mesorah # noqa: E402 from korban_processor.bedikah import examine # noqa: E402 needs_scroll = pytest.mark.skipif(not mesorah.available(), reason="mesorah snapshot not built at MESORAH_MOUNT_POINT") @needs_scroll def test_citations_are_found_as_written_in_their_several_forms(): text = ("Rashi on Devarim 6:5 reads the verse one way; Ramban to Deut. 6:5 another. Onkelos Bereishit 1:1 renders it. " "Mishnah Berakhot 1:1 opens with the evening Shema; Berakhot 2a asks why. Bereshit Rabbah 1:1 begins with wisdom. " "Sifrei Devarim 32 comments. Bavli Shabbat 31a has Hillel.") refs = {r.slug: r for r in mesorah.find_mesorah_references(text)} assert refs["rashi_on_devarim"].address == (6, 5) assert refs["ramban_on_devarim"].address == (6, 5) assert refs["onkelos_bereishit"].address == (1, 1) assert refs["mishnah_berakhot"].address == (1, 1) assert refs["bavli_berakhot"].address == (3,) # 2a → daf index 3 assert refs["bavli_shabbat"].address == (61,) # 31a assert refs["bereshit_rabbah"].address == (1, 1) assert refs["sifrei_devarim"].address == (32,) assert refs["rashi_on_devarim"].as_written == "Rashi on Devarim 6:5" [t] = mesorah.find_mesorah_references("Midrash Tanchuma, Bereshit 1 opens the same way.") assert t.slug == "midrash_tanchuma" and t.address == ("Bereshit", 1) and t.ref_id == "midrash_tanchuma_bereshit_1" @needs_scroll def test_complex_works_resolve_through_their_unnamed_node_and_tanchuma_through_its_parashah(): assert mesorah.resolve("ramban_on_bereishit", (1, 1))[0] assert mesorah.resolve("ibn_ezra_on_bereishit", (1, 1))[0] assert mesorah.resolve("sforno_on_bereishit", (1, 1))[0] assert mesorah.resolve("midrash_tanchuma", ("Bereshit", 1))[0] assert not mesorah.resolve("midrash_tanchuma", ("Nowhere", 1))[0] @needs_scroll def test_a_citation_the_scroll_has_resolves_to_text_and_one_it_lacks_does_not(): exists, texts = mesorah.resolve("rashi_on_devarim", (6, 5)) assert exists and "he" in texts and len(texts["he"]) > 20 assert "<" not in texts["he"] # tags stripped exists, texts = mesorah.resolve("rashi_on_devarim", (99, 1)) assert not exists and texts == {} exists, _ = mesorah.resolve("bavli_berakhot", (3,)) assert exists @needs_scroll def test_the_examination_lists_the_citations_and_anchors_a_claim_in_the_cited_passage(): he, _ = mesorah.resolve("rashi_on_bereishit", (1, 1))[1].get("he", ""), None # Quote six consecutive words of Rashi on Bereishit 1:1 in the output: the claim must anchor in the passage. words = he.split() quote = " ".join(words[2:9]) out = f"Rashi on Bereishit 1:1 says: {quote}. This suggests the Torah opens with a question about where it should begin." ex = examine(out, "claude", "") refs = [r for r in ex.mesorah_references if r["work"] == "rashi_on_bereishit"] assert refs and refs[0]["found"] and refs[0]["address"] == [1, 1] and "he" in refs[0]["languages"] anchored = [a.source for c in ex.claims for a in c.anchors] assert "mesorah:rashi_on_bereishit_1_1" in anchored @needs_scroll def test_a_citation_to_nothing_is_reported_not_invented(): ex = examine("Rashi on Devarim 99:1 explains this, which suggests the matter is settled.", "gpt", "") assert ex.mesorah_references == [{"as_written": "Rashi on Devarim 99:1", "ref": "rashi_on_devarim_99_1", "work": "rashi_on_devarim", "address": [99, 1], "found": False, "languages": []}] assert not any(a.source.startswith("mesorah:") for c in ex.claims for a in c.anchors) def test_without_the_scroll_nothing_is_found_and_nothing_breaks(monkeypatch): monkeypatch.setattr(mesorah, "MOUNT", "/nonexistent/mesorah") mesorah._index.cache_clear() mesorah._work.cache_clear() try: assert mesorah.find_mesorah_references("Rashi on Devarim 6:5 says so.") == [] assert mesorah.read("Berakhot 2a") == [] finally: mesorah._index.cache_clear() mesorah._work.cache_clear()