module R = Git_eio.Repository module N = Git_eio.Notes module Tx = R.Ref_transaction let who = Git.User.v ~name:"Publisher" ~email:"p@example.test" ~date:1700000000L () let notes = "refs/notes/prepared" let companion = "refs/heads/companion" let ok result = match result with | Ok x -> x | Error _ -> Alcotest.fail "Git operation refused" let setup repo = let tree = R.write_tree repo (Git.Tree.v []) in let make parents message = R.write_commit repo (Git.Commit.v ~tree ~author:who ~committer:who ~parents (Some message)) in let before = make [] "before" in let after = make [ before ] "after" in R.write_ref repo companion before; let obj = R.write_blob repo "annotated" in ignore (ok (N.append repo ~author:who ~committer:who ~ref:notes obj "old")); let observed = ok (R.read_ref_strict repo notes) in let proposal = Option.get (ok (N.append_objects repo ~author:who ~committer:who ~ref:notes [ (obj, "new") ])) in let update : Tx.update = { name = companion; expected = Value before; target = Set after; reflog = None; } in (obj, before, after, observed, proposal, update) let test_publication () = Test_helpers.with_temp_repo @@ fun sw fs dir -> let repo = R.init ~sw ~fs dir in let obj, before, after, observed, proposal, update = setup repo in Alcotest.(check (option Test_helpers.hash)) "object proposal has not moved notes" observed (ok (R.read_ref_strict repo notes)); let tx = ok (Tx.prepare repo [ proposal; update ]) in Alcotest.(check (option Test_helpers.hash)) "prepared transaction moves no companion" (Some before) (ok (R.read_ref_strict repo companion)); ignore (ok (Tx.commit tx)); Alcotest.(check (option Test_helpers.hash)) "one transaction publishes companion" (Some after) (ok (R.read_ref_strict repo companion)); Alcotest.(check (option string)) "one transaction publishes note" (Some "old\nnew\n") (ok (N.find repo ~ref:notes obj)) let test_raced_notes () = Test_helpers.with_temp_repo @@ fun sw fs dir -> let repo = R.init ~sw ~fs dir in let obj, before, _, _, proposal, update = setup repo in let moved = ok (N.append repo ~author:who ~committer:who ~ref:notes obj "external") in (match Tx.prepare repo [ proposal; update ] with | Error (`Raced (name, _)) -> Alcotest.(check string) "observed notes ref refuses" notes name | Error _ -> Alcotest.fail "refusal did not name raced notes" | Ok tx -> Tx.abort tx; Alcotest.fail "stale notes proposal accepted"); Alcotest.(check (option Test_helpers.hash)) "companion unchanged on refusal" (Some before) (ok (R.read_ref_strict repo companion)); Alcotest.(check (option Test_helpers.hash)) "external notes unchanged on refusal" (Some moved) (ok (R.read_ref_strict repo notes)) let () = Alcotest.run "prepared notes objects" [ ( "native", [ Alcotest.test_case "publish through ordinary transaction" `Quick test_publication; Alcotest.test_case "stale notes refuse all updates" `Quick test_raced_notes; ] ); ]