open Camlet_domain let value = function | Ok value -> value | Error message -> Alcotest.fail message let head = value (Commit_id.of_string "055bce9f3785aba0158eb715c742d2030e9594eb") let remote_commit = value (Commit_id.of_string "155bce9f3785aba0158eb715c742d2030e9594eb") let rid_text = "rad:z2Xvmqh4146DHcGgmTCZ645qhvqnA" let did_text = "did:plc:rvyb4oxpswgjgdvvncihletw" let main_ref = value (Git_ref.of_string "main") let contains ~needle value = let rec find index = if index + String.length needle > String.length value then false else if String.sub value index (String.length needle) = needle then true else find (index + 1) in find 0 let test_commit_validation () = Alcotest.(check bool) "valid commit" true (Result.is_ok (Commit_id.of_string "abcdef0")); Alcotest.(check bool) "reject non-hexadecimal commit" true (Result.is_error (Commit_id.of_string "abcdefg")) let test_git_ref_validation () = Alcotest.(check bool) "valid tracking ref" true (Result.is_ok (Git_ref.of_string "refs/remotes/tangled/main")); List.iter (fun reference -> Alcotest.(check bool) ("reject " ^ String.escaped reference) true (Result.is_error (Git_ref.of_string reference))) [ "bad..ref"; "bad ref"; "refs//main"; "main.lock"; "unsafe\027ref" ] let test_forge_urls () = let tangled = Forge_url.tangled ("git@tangled.org:" ^ did_text) in let tangled_path = Forge_url.tangled "https://tangled.org/dc-tec.tngl.sh/camlet.git" in let radicle_fetch = Forge_url.radicle "rad://z2Xvmqh4146DHcGgmTCZ645qhvqnA" in let radicle_push = Forge_url.radicle "rad://z2Xvmqh4146DHcGgmTCZ645qhvqnA/z6MkoXGjdyqC8TfSqZNB9xhBfSB7HZiXXv2vfwZc8TkD8x9n" in let tangled_value = match tangled with | Some (Forge_url.Repository_did did) -> Repository_did.to_string did | _ -> Alcotest.fail "expected a Tangled repository DID" in Alcotest.(check string) "Tangled DID" did_text tangled_value; Alcotest.(check bool) "Tangled readable path" true (tangled_path = Some (Forge_url.Repository_path "dc-tec.tngl.sh/camlet")); List.iter (fun parsed -> let actual = Option.map Radicle_rid.to_string parsed in Alcotest.(check (option string)) "Radicle RID" (Some rid_text) actual) [ radicle_fetch; radicle_push ]; Alcotest.(check bool) "reject malformed Radicle URL" true (Option.is_none (Forge_url.radicle "rad://not-a-rid")); Alcotest.(check bool) "do not infer Tangled from an unrelated host" true (Option.is_none (Forge_url.tangled ("https://example.invalid/repositories/" ^ did_text))) let comparison commit ahead behind = Some Camlet_application.Context_ports.{ commit; ahead; behind } let remote ?tracking_ref ~name ~fetch_url ~comparison () = let tracking_ref = Option.map (fun reference -> value (Git_ref.of_string reference)) tracking_ref in Camlet_application.Context_ports. { name ; fetch_url ; push_url = "https://credential:supersecret@example.invalid/repository" ; tracking_ref ; comparison } let git_snapshot ?(branch = Some main_ref) remotes = Camlet_application.Context_ports. { root = "/work/camlet"; head; branch; dirty = false; remotes } let ports git_result jj_result = Camlet_application.Context_ports. { inspect_git = (fun _ -> git_result); inspect_jj = (fun _ -> jj_result) } let test_resolve_and_redact () = let remotes = [ remote ~name:"tangled" ~fetch_url:("git@tangled.org:" ^ did_text) ~tracking_ref:"refs/remotes/tangled/main" ~comparison:(comparison head 0 0) () ; remote ~name:"rad" ~fetch_url:"rad://z2Xvmqh4146DHcGgmTCZ645qhvqnA" ~tracking_ref:"refs/remotes/rad/main" ~comparison:(comparison remote_commit 1 1) () ] in let context = Camlet_application.Resolve_context.run ~ports:(ports (Ok (git_snapshot remotes)) (Ok None)) ~repo:"." |> function | Ok context -> context | Error _ -> Alcotest.fail "context resolution failed" in Alcotest.(check int) "two recognized surfaces" 2 (List.length context.surfaces); Alcotest.(check bool) "divergence detected" true (context.agreement = Repository_context.Diverged_surfaces); let json = Camlet_presentation.Context_output.json context in Alcotest.(check bool) "credential absent" false (contains ~needle:"supersecret" json); let document = Yojson.Safe.from_string json in let expected = `Assoc [ ("schema", `String "org.camlet.context.v1") ; ( "result" , `Assoc [ ("repository", `Assoc [ ("root", `String "/work/camlet") ]) ; ( "git" , `Assoc [ ("head", `String (Commit_id.to_string head)) ; ("branch", `String "main") ; ("dirty", `Bool false) ] ) ; ("jujutsu", `Null) ; ( "surfaces" , `List [ `Assoc [ ("forge", `String "tangled") ; ("remote", `String "tangled") ; ( "repository" , `Assoc [ ("kind", `String "did") ; ("value", `String did_text) ] ) ; ("tracking_ref", `String "refs/remotes/tangled/main") ; ("revision", `String (Commit_id.to_string head)) ; ( "relation" , `Assoc [ ("kind", `String "equal") ; ("ahead", `Int 0) ; ("behind", `Int 0) ] ) ] ; `Assoc [ ("forge", `String "radicle") ; ("remote", `String "rad") ; ( "repository" , `Assoc [ ("kind", `String "rid") ; ("value", `String rid_text) ] ) ; ("tracking_ref", `String "refs/remotes/rad/main") ; ("revision", `String (Commit_id.to_string remote_commit)) ; ( "relation" , `Assoc [ ("kind", `String "diverged") ; ("ahead", `Int 1) ; ("behind", `Int 1) ] ) ] ] ) ; ("agreement", `String "diverged") ; ("observation", `String "local_tracking_refs") ] ) ] in Alcotest.(check bool) "complete context schema" true (Yojson.Safe.equal expected document); let expected_human = {|Repository Root: /work/camlet Git HEAD: 055bce9f Branch: main Working tree: clean Jujutsu Status: not detected Surfaces (local tracking refs) tangled (tangled) Repository: did:plc:rvyb4oxpswgjgdvvncihletw Tracking ref: refs/remotes/tangled/main Revision: 055bce9f Relation: equal radicle (rad) Repository: rad:z2Xvmqh4146DHcGgmTCZ645qhvqnA Tracking ref: refs/remotes/rad/main Revision: 155bce9f Relation: diverged (ahead 1, behind 1) Agreement State: diverged Basis: local tracking refs only|} in Alcotest.(check string) "complete human context" expected_human (Camlet_presentation.Context_output.human context) let test_missing_tracking_ref () = let remotes = [ remote ~name:"tangled" ~fetch_url:("git@tangled.org:" ^ did_text) ~tracking_ref:"refs/remotes/tangled/main" ~comparison:None () ] in let context = Camlet_application.Resolve_context.run ~ports:(ports (Ok (git_snapshot remotes)) (Ok None)) ~repo:"." |> function | Ok context -> context | Error _ -> Alcotest.fail "context resolution failed" in Alcotest.(check bool) "missing ref means incomplete" true (context.agreement = Repository_context.Incomplete) let test_agreement_precedence () = let out_of_sync = [ remote ~name:"tangled" ~fetch_url:("git@tangled.org:" ^ did_text) ~tracking_ref:"refs/remotes/tangled/main" ~comparison:(comparison remote_commit 1 0) () ] |> git_snapshot |> fun snapshot -> Camlet_application.Resolve_context.run ~ports:(ports (Ok snapshot) (Ok None)) ~repo:"." in (match out_of_sync with | Error _ -> Alcotest.fail "out-of-sync context resolution failed" | Ok context -> Alcotest.(check bool) "fast-forward drift is not divergence" true (context.agreement = Repository_context.Out_of_sync)); let mixed = [ remote ~name:"tangled" ~fetch_url:("git@tangled.org:" ^ did_text) ~tracking_ref:"refs/remotes/tangled/main" ~comparison:None () ; remote ~name:"rad" ~fetch_url:"rad://z2Xvmqh4146DHcGgmTCZ645qhvqnA" ~tracking_ref:"refs/remotes/rad/main" ~comparison:(comparison remote_commit 1 1) () ] |> git_snapshot |> fun snapshot -> Camlet_application.Resolve_context.run ~ports:(ports (Ok snapshot) (Ok None)) ~repo:"." in match mixed with | Error _ -> Alcotest.fail "mixed context resolution failed" | Ok context -> Alcotest.(check bool) "known divergence wins over missing evidence" true (context.agreement = Repository_context.Diverged_surfaces) let test_detached_context () = let remotes = [ remote ~name:"tangled" ~fetch_url:("git@tangled.org:" ^ did_text) ~comparison:None () ] in let context = Camlet_application.Resolve_context.run ~ports:(ports (Ok (git_snapshot ~branch:None remotes)) (Ok None)) ~repo:"." |> function | Ok context -> context | Error _ -> Alcotest.fail "detached context resolution failed" in let document = Camlet_presentation.Context_output.json context |> Yojson.Safe.from_string in let branch = Yojson.Safe.Util.( document |> member "result" |> member "git" |> member "branch") in let tracking_ref = Yojson.Safe.Util.( document |> member "result" |> member "surfaces" |> index 0 |> member "tracking_ref") in Alcotest.(check bool) "detached branch is null" true (branch = `Null); Alcotest.(check bool) "unobserved tracking ref is null" true (tracking_ref = `Null); Alcotest.(check bool) "detached context is incomplete" true (context.agreement = Repository_context.Incomplete); Alcotest.(check bool) "human output names detached state" true (contains ~needle:" Branch: detached" (Camlet_presentation.Context_output.human context)) let test_boundary_failure () = let error = App_error.make ~category:External_tool ~code:"git.timeout" ~message:"Git timed out" in match Camlet_application.Resolve_context.run ~ports:(ports (Error error) (Ok None)) ~repo:"." with | Ok _ -> Alcotest.fail "expected the port error" | Error actual -> Alcotest.(check string) "stable code" "git.timeout" (App_error.code actual); Alcotest.(check int) "stable exit code" 9 (App_error.exit_code actual) let () = Alcotest.run "context" [ ( "domain" , [ Alcotest.test_case "commit validation" `Quick test_commit_validation ; Alcotest.test_case "Git ref validation" `Quick test_git_ref_validation ; Alcotest.test_case "forge URL parsing" `Quick test_forge_urls ] ) ; ( "application" , [ Alcotest.test_case "resolve and redact" `Quick test_resolve_and_redact ; Alcotest.test_case "missing tracking ref" `Quick test_missing_tracking_ref ; Alcotest.test_case "agreement precedence" `Quick test_agreement_precedence ; Alcotest.test_case "detached context" `Quick test_detached_context ; Alcotest.test_case "boundary failure" `Quick test_boundary_failure ] ) ]