(* Copyright (c) 2024-2026 Thomas Gazagnaire Permission to use, copy, modify, and distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies. THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. *) let hash = Test_helpers.hash let test_roundtrip () = let content = "test content" in let blob = Git.Blob.of_string content in let value = Git.Value.blob blob in let s = Git.Value.to_string value in match Git.Value.of_string_with_header s with | Ok value' -> ( match value' with | Git.Value.Blob b -> Alcotest.(check string) "content" content (Git.Blob.to_string b) | _ -> Alcotest.fail "expected blob") | Error (`Msg m) -> Alcotest.fail m let test_bytesrw_roundtrip () = let content = "bytesrw test content" in let blob = Git.Blob.of_string content in let value = Git.Value.blob blob in (* Write to buffer *) let buf = Buffer.create 64 in let writer = Bytesrw.Bytes.Writer.of_buffer buf in Git.Value.write writer value; let written = Buffer.contents buf in (* Read back *) let reader = Bytesrw.Bytes.Reader.of_string written in match Git.Value.read reader with | Ok value' -> ( match value' with | Git.Value.Blob b -> Alcotest.(check string) "content" content (Git.Blob.to_string b) | _ -> Alcotest.fail "expected blob") | Error (`Msg m) -> Alcotest.fail m let test_bytesrw_commit () = let tree_hash = Git.Hash.of_hex "da39a3ee5e6b4b0d3255bfef95601890afd80709" in let author = Git.User.v ~name:"Alice" ~email:"alice@example.com" ~date:1234567890L () in let commit = Git.Commit.v ~tree:tree_hash ~author ~committer:author (Some "Test commit\n") in let value = Git.Value.commit commit in (* Write to buffer *) let buf = Buffer.create 256 in let writer = Bytesrw.Bytes.Writer.of_buffer buf in Git.Value.write writer value; let written = Buffer.contents buf in (* Read back *) let reader = Bytesrw.Bytes.Reader.of_string written in match Git.Value.read reader with | Ok value' -> ( match value' with | Git.Value.Commit c -> Alcotest.(check hash) "tree" tree_hash (Git.Commit.tree c); Alcotest.(check (option string)) "message" (Some "Test commit\n") (Git.Commit.message c) | _ -> Alcotest.fail "expected commit") | Error (`Msg m) -> Alcotest.fail m let tests = [ Alcotest.test_case "roundtrip" `Quick test_roundtrip; Alcotest.test_case "bytesrw_roundtrip" `Quick test_bytesrw_roundtrip; Alcotest.test_case "bytesrw_commit" `Quick test_bytesrw_commit; ] let suite = ("value", tests)