defmodule VavPipeline.AnchorsTest do @moduledoc """ Every halt rule cites its verse (bishearecha.md §X; Devarim 4:2). """ use ExUnit.Case, async: true alias VavPipeline.{Anchors, Doorpost} test "the anchors file is a Doorpost file" do assert "talmudic_engine/src/halt/anchors.pl" in Doorpost.files() end test "every halt name the rule modules can return is anchored, and every anchor is in L1" do assert :ok = Anchors.verify() {:ok, rules} = Anchors.rules_in_modules() {:ok, anchors} = Anchors.declared() assert rules == Enum.sort(Enum.map(anchors, & &1.rule)) assert "even_vaaven" in rules and "lo_taaseh_lekha" in rules and "avodah_zarah" in rules assert length(rules) == 10 end test "the anchors read as citations" do assert Anchors.for_rule("even_vaaven") == "Devarim 25:13" assert Anchors.for_rule("lo_taaseh_lekha") == "Shemot 20:4" assert Anchors.for_rule("parah_adumah") == "Bamidbar 19:7" assert Anchors.for_rule("no_such_rule") == nil end test "for_violations/1 names the verse of each rule that fired, once" do violations = [ "halt(even_vaaven,action(weigh, by(gpt), measure(claims), lean(a), counts(2, 0)),claims,...)", "halt(lo_taaseh_lekha,action(frame, by(gpt), form(virtue_certification)),virtue_certification,...)", "halt(lo_taaseh_lekha,action(frame, by(gpt), form(contested_laundering)),contested_laundering,...)" ] assert Anchors.for_violations(violations) == %{"even_vaaven" => "Devarim 25:13", "lo_taaseh_lekha" => "Shemot 20:4"} assert Anchors.for_violations([]) == %{} end describe "drift (synthetic spec root)" do setup do temp = Path.join(System.tmp_dir!(), "anchors_test_#{System.unique_integer([:positive])}") File.mkdir_p!(Path.join(temp, "talmudic_engine/src/halt")) for file <- Doorpost.files(), String.starts_with?(file, "talmudic_engine/src/halt/") do File.cp!(Path.join(Doorpost.repo_root(), file), Path.join(temp, file)) end on_exit(fn -> File.rm_rf!(temp) end) {:ok, temp: temp} end test "a mirror verifies", %{temp: temp} do assert :ok = Anchors.verify(temp) end test "a rule without an anchor fails: lo tosif", %{temp: temp} do path = Path.join(temp, "talmudic_engine/src/halt/structure.pl") File.write!(path, File.read!(path) <> "\nhalt_check_new(_, halt(new_rule, x, y, z)).\n") assert {:error, reason} = Anchors.verify(temp) assert reason =~ "ANCHOR_MISSING" and reason =~ "new_rule" end test "an anchor to a verse that is not in L1 fails", %{temp: temp} do path = Path.join(temp, "talmudic_engine/src/halt/anchors.pl") File.write!(path, String.replace(File.read!(path), "verse(devarim, 25, 13)", "verse(devarim, 25, 99)")) assert {:error, reason} = Anchors.verify(temp) assert reason =~ "ANCHOR_NOT_IN_L1" and reason =~ "even_vaaven" and reason =~ "Devarim 25:99" end test "an anchor in a commented-out rule does not count; a halt name in a comment is not a rule", %{temp: temp} do path = Path.join(temp, "talmudic_engine/src/halt/structure.pl") File.write!(path, File.read!(path) <> "\n%% halt(imaginary, x, y, z) — only a comment\n") assert :ok = Anchors.verify(temp) end test "a missing anchors file fails", %{temp: temp} do File.rm!(Path.join(temp, "talmudic_engine/src/halt/anchors.pl")) assert {:error, reason} = Anchors.verify(temp) assert reason =~ "ANCHORS_UNREADABLE" end end end