open Camlet_domain let value = function | Ok value -> value | Error message -> Alcotest.fail message let result_value = function | Ok value -> value | Error error -> Alcotest.fail (App_error.code error ^ ": " ^ App_error.message error) let result_error = function | Ok _ -> Alcotest.fail "expected an error" | Error error -> error let error_code result = App_error.code (result_error result) 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 rec project_root directory = if Sys.file_exists (Filename.concat directory "dune-project") then directory else let parent = Filename.dirname directory in if String.equal parent directory then Alcotest.fail "could not locate project root" else project_root parent let fixture name = let root = match Sys.getenv_opt "DUNE_SOURCEROOT" with | Some value -> value | None -> project_root (Sys.getcwd ()) in let path = Filename.concat root ("test/contract/fixtures/issue_list/v1/" ^ name) in let channel = open_in_bin path in Fun.protect ~finally:(fun () -> close_in_noerr channel) (fun () -> really_input_string channel (in_channel_length channel)) let repository_did = value (Repository_did.of_string "did:plc:aaaaaaaaaaaaaaaaaaaaaaaa") let bobbin = value (Origin.of_string "https://api.tangled.org") let observed_at = value (Timestamp.of_string "2026-01-04T03:04:05Z") let decode_page name = Camlet_atproto_protocol.Issue_list_codec.decode_page ~repository:repository_did (fixture name) |> result_value let decode_coverage name = Camlet_atproto_protocol.Issue_list_codec.decode_coverage (fixture name) |> result_value let first_issue (page : Issue_list.page) = match page.Issue_list.issues with | [ issue ] -> issue | _ -> Alcotest.fail "expected one issue" let test_fixture_pages_and_coverage () = let first = decode_page "managed/list-page-1.json" in let second = decode_page "managed/list-page-2.json" in Alcotest.(check (option string)) "continuation" (Some "opaque-issue-cursor") first.cursor; Alcotest.(check (option string)) "terminal null" None second.cursor; let first = first_issue first in let second = first_issue second in Alcotest.(check string) "first key" "3exampleone" (Record_key.to_string first.record_key); Alcotest.(check string) "first state" "open" (Issue_list.state_to_string first.state); Alcotest.(check string) "second state" "closed" (Issue_list.state_to_string second.state); Alcotest.(check int64) "comment count" 1L second.comment_count; Alcotest.(check bool) "state timestamp" true (Option.is_some second.state_updated_at); let before = decode_coverage "managed/coverage-before.json" in let after = decode_coverage "managed/coverage-after.json" in Alcotest.(check bool) "ready before" true before.ready; Alcotest.(check int64) "events before" 135037L before.events_processed; Alcotest.(check int64) "cursor after" 158330L after.last_cursor let issue_json ?(repo = "did:plc:aaaaaaaaaaaaaaaaaaaaaaaa") ?(uri = "at://did:plc:bbbbbbbbbbbbbbbbbbbbbbbb/sh.tangled.repo.issue/3example") ?(title = "Example") ?(body = `String "Body") ?(state = "open") ?(state_updated = `Null) ?(comments = `Int 0) () = `Assoc [ ("uri", `String uri) ; ( "cid" , `String "bafyreigfkphxytbcofvk7bjh622rep55av3l3anzp2isioecbidkgzjzkm" ) ; ( "value" , `Assoc [ ("$type", `String "sh.tangled.repo.issue") ; ("repo", `String repo) ; ("title", `String title) ; ("body", body) ; ("createdAt", `String "2026-01-02T03:04:05Z") ] ) ; ("state", `String state) ; ("stateUpdatedAt", state_updated) ; ("commentCount", comments) ] let page_body items cursor = `Assoc [ ("items", `List items); ("cursor", cursor) ] |> Yojson.Safe.to_string let decode body = Camlet_atproto_protocol.Issue_list_codec.decode_page ~repository:repository_did body let test_decoder_optional_and_unknown_values () = let item = match issue_json ~state:"triaged" ~body:`Null () with | `Assoc fields -> `Assoc (List.map (fun (name, value) -> if String.equal name "cid" then (name, `Null) else (name, value)) fields) | _ -> Alcotest.fail "issue fixture must be an object" in let issue = page_body [ item ] `Null |> decode |> result_value |> first_issue in Alcotest.(check (option string)) "optional CID" None (Option.map Cid.to_string issue.cid); Alcotest.(check (option string)) "optional body" None issue.body; Alcotest.(check string) "unknown state preserved" "triaged" (Issue_list.state_to_string issue.state) let test_decoder_rejects_malformed_and_inconsistent_items () = let mismatch = page_body [ issue_json ~repo:"did:plc:cccccccccccccccccccccccc" () ] `Null |> decode |> result_error in Alcotest.(check string) "repository mismatch" "issue.list_repository_mismatch" (App_error.code mismatch); Alcotest.(check string) "consistency category" "consistency" (App_error.category_name (App_error.category mismatch)); Alcotest.(check string) "wrong collection" "issue.list_invalid" (page_body [ issue_json ~uri: "at://did:plc:bbbbbbbbbbbbbbbbbbbbbbbb/sh.tangled.repo.pull/3example" () ] `Null |> decode |> error_code); Alcotest.(check string) "negative comments" "issue.list_invalid" (page_body [ issue_json ~comments:(`Int (-1)) () ] `Null |> decode |> error_code); Alcotest.(check string) "unsafe state" "issue.list_invalid" (page_body [ issue_json ~state:"open\027[31m" () ] `Null |> decode |> error_code); Alcotest.(check string) "empty title" "issue.list_invalid" (page_body [ issue_json ~title:"" () ] `Null |> decode |> error_code) let test_decoder_duplicate_fields_bounds_and_redaction () = let duplicate = {|{"items":[{"uri":"at://did:plc:bbbbbbbbbbbbbbbbbbbbbbbb/sh.tangled.repo.issue/3example","value":{"$type":"sh.tangled.repo.issue","repo":"did:plc:aaaaaaaaaaaaaaaaaaaaaaaa","title":"one","title":"two","createdAt":"2026-01-02T03:04:05Z"},"state":"open","commentCount":0}]}|} in Alcotest.(check string) "duplicate title" "issue.list_invalid" (decode duplicate |> error_code); let conflicting_type = {|{"items":[{"uri":"at://did:plc:bbbbbbbbbbbbbbbbbbbbbbbb/sh.tangled.repo.issue/3example","value":{"$type":"sh.tangled.repo.issue","repo":"did:plc:aaaaaaaaaaaaaaaaaaaaaaaa","title":"one","createdAt":"2026-01-02T03:04:05Z","$type":"sh.tangled.repo.pull"},"state":"open","commentCount":0}]}|} in Alcotest.(check string) "conflicting duplicate type" "issue.list_invalid" (decode conflicting_type |> error_code); Alcotest.(check string) "cursor type" "issue.list_invalid" (page_body [] (`Int 1) |> decode |> error_code); let oversized_cursor = String.make ((4 * 1024) + 1) 'x' in Alcotest.(check string) "cursor size" "issue.list_invalid" (page_body [] (`String oversized_cursor) |> decode |> error_code); let too_many = List.init 101 (fun _ -> issue_json ()) in Alcotest.(check string) "per-page item bound" "issue.list_invalid" (page_body too_many `Null |> decode |> error_code); let secret = "issue-secret-which-must-not-escape" in let error = decode ("{\"items\":[\"" ^ secret) |> result_error in Alcotest.(check bool) "malformed body redacted" false (contains ~needle:secret (App_error.message error)) let test_coverage_decoder_boundaries () = Alcotest.(check string) "negative coverage" "issue.coverage_invalid" (Camlet_atproto_protocol.Issue_list_codec.decode_coverage {|{"ready":true,"eventsProcessed":-1,"lastCursor":2}|} |> error_code); Alcotest.(check string) "duplicate coverage" "issue.coverage_invalid" (Camlet_atproto_protocol.Issue_list_codec.decode_coverage {|{"ready":true,"ready":false,"eventsProcessed":1,"lastCursor":2}|} |> error_code) let response ~status ~content_type ~body = Camlet_adapter_http.Http_client.{ status; content_type; body } let test_adapter_classification_and_redaction () = let page = Camlet_adapter_bobbin.Issue_lister.decode_list_response repository_did (response ~status:200 ~content_type:(Some "application/json; charset=utf-8") ~body:(fixture "managed/list-page-1.json")) |> result_value in Alcotest.(check int) "decoded response" 1 (List.length page.issues); Alcotest.(check string) "wrong media type" "issue.list_content_type" (Camlet_adapter_bobbin.Issue_lister.decode_list_response repository_did (response ~status:200 ~content_type:(Some "text/plain") ~body:"{}") |> error_code); Alcotest.(check string) "invalid request" "issue.list_invalid_request" (Camlet_adapter_bobbin.Issue_lister.decode_list_response repository_did (response ~status:400 ~content_type:(Some "application/json") ~body:(fixture "errors/invalid-request.json")) |> error_code); let secret = "upstream-secret" in let failure = Camlet_adapter_bobbin.Issue_lister.decode_list_response repository_did (response ~status:503 ~content_type:(Some "text/plain") ~body:secret) |> result_error in Alcotest.(check int) "transport exit" 7 (App_error.exit_code failure); Alcotest.(check bool) "response redacted" false (contains ~needle:secret (App_error.message failure)); Alcotest.(check string) "coverage media type" "issue.coverage_content_type" (Camlet_adapter_bobbin.Issue_lister.decode_coverage_response (response ~status:200 ~content_type:(Some "text/plain") ~body:"{}") |> error_code) let coverage_before = Issue_list.{ ready = true; events_processed = 10L; last_cursor = 20L } let coverage_after = Issue_list.{ ready = true; events_processed = 12L; last_cursor = 22L } let fixture_pages () = ( decode_page "managed/list-page-1.json" , decode_page "managed/list-page-2.json" ) let test_application_pagination_discovery_and_output () = let first, second = fixture_pages () in let discovery = ref None in let requested_filter = ref None in let coverage_calls = ref 0 in let ports = Camlet_application.Issue_list_ports. { discover_local = (fun repo -> discovery := Some repo; Ok repository_did) ; list_issues = (fun origin repository filter ~cursor -> Alcotest.(check string) "Bobbin origin" (Origin.to_string bobbin) (Origin.to_string origin); Alcotest.(check string) "repository" (Repository_did.to_string repository_did) (Repository_did.to_string repository); requested_filter := Some filter; match cursor with | None -> Ok first | Some "opaque-issue-cursor" -> Ok second | Some _ -> Alcotest.fail "unexpected cursor") ; read_coverage = (fun _ -> incr coverage_calls; Ok (if !coverage_calls = 1 then coverage_before else coverage_after)) ; now = (fun () -> Ok observed_at) } in let listing = Camlet_application.List_issues.run ~ports ~repo:"/work/repository" ~repository_did:None ~bobbin ~state_filter:Issue_list.All |> result_value in Alcotest.(check (option string)) "local discovery" (Some "/work/repository") !discovery; Alcotest.(check bool) "filter forwarded" true (match !requested_filter with Some Issue_list.All -> true | _ -> false); Alcotest.(check int) "all pages" 2 (List.length listing.issues); Alcotest.(check int) "coverage bracket" 2 !coverage_calls; let document = Camlet_presentation.Issue_list_output.json listing |> Yojson.Safe.from_string in let expected = Yojson.Safe.from_string {|{"schema":"org.camlet.issue-list.v1","result":{"repository":{"did":"did:plc:aaaaaaaaaaaaaaaaaaaaaaaa"},"filter":{"state":"all"},"issues":[{"id":"3exampleone","record":{"uri":"at://did:plc:bbbbbbbbbbbbbbbbbbbbbbbb/sh.tangled.repo.issue/3exampleone","cid":"bafyreigfkphxytbcofvk7bjh622rep55av3l3anzp2isioecbidkgzjzkm","created_at":"2026-01-03T03:04:05Z"},"content":{"title":"First observed issue","body":"Sanitized issue body."},"derived":{"state":"open","state_updated_at":null,"comment_count":0,"source":"bobbin"}},{"id":"3exampletwo","record":{"uri":"at://did:plc:cccccccccccccccccccccccc/sh.tangled.repo.issue/3exampletwo","cid":"bafyreiergfi2oj5or54hagpsnkfogk6pucxlab7nzgjioxa2zjf74wpmpu","created_at":"2026-01-02T03:04:05Z"},"content":{"title":"Second observed issue","body":"Another sanitized issue body."},"derived":{"state":"closed","state_updated_at":"2026-01-02T04:05:06Z","comment_count":1,"source":"bobbin"}}],"observation":{"observed_at":"2026-01-04T03:04:05Z","source":{"kind":"bobbin","endpoint":"https://api.tangled.org"},"enumeration":"bobbin_complete_pagination","snapshot":"non_atomic","completeness":"indexed_observation_only","coverage":{"before":{"ready":true,"events_processed":10,"last_cursor":20},"after":{"ready":true,"events_processed":12,"last_cursor":22}}}}}|} in Alcotest.(check bool) "complete issue-list schema" true (Yojson.Safe.equal expected document); let expected_human = {|Repository DID: did:plc:aaaaaaaaaaaaaaaaaaaaaaaa Bobbin: https://api.tangled.org State filter: all Issues (2) 3exampleone State: open Comments: 0 Created: 2026-01-03T03:04:05Z Title: First observed issue URI: at://did:plc:bbbbbbbbbbbbbbbbbbbbbbbb/sh.tangled.repo.issue/3exampleone 3exampletwo State: closed Comments: 1 Created: 2026-01-02T03:04:05Z Title: Second observed issue URI: at://did:plc:cccccccccccccccccccccccc/sh.tangled.repo.issue/3exampletwo Observation Time: 2026-01-04T03:04:05Z Coverage: ready -> ready; events 10 -> 12; cursor 20 -> 22 Enumeration: Bobbin complete pagination Snapshot: non-atomic Completeness: indexed observations only|} in Alcotest.(check string) "complete human issue list" expected_human (Camlet_presentation.Issue_list_output.human listing) let unexpected_error () = App_error.make ~category:Internal ~code:"test.unexpected" ~message:"An unexpected test port was called" let failure_ports ?(coverage = fun _ -> Ok coverage_before) list_issues = Camlet_application.Issue_list_ports. { discover_local = (fun _ -> Error (unexpected_error ())) ; list_issues ; read_coverage = coverage ; now = (fun () -> Error (unexpected_error ())) } let run_failure ports = Camlet_application.List_issues.run ~ports ~repo:"." ~repository_did:(Some repository_did) ~bobbin ~state_filter:Issue_list.All let test_application_cursor_duplicate_and_page_bounds () = let repeated = failure_ports (fun _ _ _ ~cursor:_ -> Ok Issue_list.{ issues = []; cursor = Some "same" }) in Alcotest.(check string) "repeated cursor" "issue.list_cursor_repeated" (run_failure repeated |> error_code); let issue = first_issue (decode_page "managed/list-page-1.json") in let duplicates = failure_ports (fun _ _ _ ~cursor:_ -> Ok Issue_list.{ issues = [ issue; issue ]; cursor = None }) in Alcotest.(check string) "duplicate URI" "issue.list_duplicate_uri" (run_failure duplicates |> error_code); let calls = ref 0 in let pages = failure_ports (fun _ _ _ ~cursor:_ -> incr calls; Ok Issue_list.{ issues = []; cursor = Some (string_of_int !calls) }) in Alcotest.(check string) "page limit" "issue.list_page_limit" (run_failure pages |> error_code); Alcotest.(check int) "bounded page calls" 100 !calls let test_application_item_and_coverage_bounds () = let issue = first_issue (decode_page "managed/list-page-1.json") in let filter_mismatch = failure_ports (fun _ _ _ ~cursor:_ -> Ok Issue_list.{ issues = [ issue ]; cursor = None }) in Alcotest.(check string) "state filter mismatch" "issue.list_filter_mismatch" (Camlet_application.List_issues.run ~ports:filter_mismatch ~repo:"." ~repository_did:(Some repository_did) ~bobbin ~state_filter:Issue_list.Closed_only |> error_code); let oversized = failure_ports (fun _ _ _ ~cursor:_ -> Ok Issue_list. { issues = List.init 10_001 (fun _ -> issue); cursor = None }) in Alcotest.(check string) "item limit" "issue.list_item_limit" (run_failure oversized |> error_code); let coverage_calls = ref 0 in let regressing _ = incr coverage_calls; if !coverage_calls = 1 then Ok coverage_before else Ok Issue_list.{ ready = false; events_processed = 9L; last_cursor = 19L } in let regression = failure_ports ~coverage:regressing (fun _ _ _ ~cursor:_ -> Ok Issue_list.{ issues = []; cursor = None }) in let error = run_failure regression |> result_error in Alcotest.(check string) "coverage regression" "issue.coverage_regressed" (App_error.code error); Alcotest.(check int) "consistency exit" 10 (App_error.exit_code error) let test_human_output_empty_and_terminal_escaping () = let empty = Issue_list. { repository_did ; bobbin ; state_filter = All ; issues = [] ; coverage_before ; coverage_after ; observed_at } in let output = Camlet_presentation.Issue_list_output.human empty in let expected_empty = {|Repository DID: did:plc:aaaaaaaaaaaaaaaaaaaaaaaa Bobbin: https://api.tangled.org State filter: all Issues (0) Status: no indexed issues observed Observation Time: 2026-01-04T03:04:05Z Coverage: ready -> ready; events 10 -> 12; cursor 20 -> 22 Enumeration: Bobbin complete pagination Snapshot: non-atomic Completeness: indexed observations only|} in Alcotest.(check string) "explicit indexed absence" expected_empty output; let issue = first_issue (decode_page "managed/list-page-1.json") in let unsafe = { issue with title = "line\nnext\027[31m" ; state = Issue_list.Unknown "state\nnext\027[31m" } in let output = Camlet_presentation.Issue_list_output.human { empty with issues = [ unsafe ] } in Alcotest.(check bool) "no literal escape" false (String.contains output '\027'); Alcotest.(check bool) "escaped control text" true (contains ~needle:"\\027" output); Alcotest.(check bool) "state is escaped" true (contains ~needle:"state\\nnext\\027[31m" output) let () = Alcotest.run "issue" [ ( "protocol" , [ Alcotest.test_case "fixture pages and coverage" `Quick test_fixture_pages_and_coverage ; Alcotest.test_case "optional and unknown values" `Quick test_decoder_optional_and_unknown_values ; Alcotest.test_case "malformed and inconsistent items" `Quick test_decoder_rejects_malformed_and_inconsistent_items ; Alcotest.test_case "duplicates, bounds, and redaction" `Quick test_decoder_duplicate_fields_bounds_and_redaction ; Alcotest.test_case "coverage boundaries" `Quick test_coverage_decoder_boundaries ; Alcotest.test_case "adapter classification" `Quick test_adapter_classification_and_redaction ] ) ; ( "application" , [ Alcotest.test_case "pagination, discovery, and output" `Quick test_application_pagination_discovery_and_output ; Alcotest.test_case "cursor, duplicate, and page bounds" `Quick test_application_cursor_duplicate_and_page_bounds ; Alcotest.test_case "item and coverage bounds" `Quick test_application_item_and_coverage_bounds ; Alcotest.test_case "empty output and escaping" `Quick test_human_output_empty_and_terminal_escaping ] ) ]