OCaml client library for Claude Code
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

WIP: dune build fixes — json/value.t, plain to_string, sw moves

+182
+180
test/test_error.ml
··· 1 + (*--------------------------------------------------------------------------- 2 + Copyright (c) 2025 Anil Madhavapeddy <anil@recoil.org>. All rights reserved. 3 + SPDX-License-Identifier: ISC 4 + ---------------------------------------------------------------------------*) 5 + 6 + (** Tests for Error module: error formatting, raisers, and result helpers. *) 7 + 8 + let test_cli_not_found_format () = 9 + let err = Claude.Error.Cli_not_found "claude not in PATH" in 10 + Alcotest.(check string) 11 + "format" "CLI not found: claude not in PATH" 12 + (Claude.Error.to_string err) 13 + 14 + let test_process_error_format () = 15 + let err = Claude.Error.Process_error "exit code 1" in 16 + Alcotest.(check string) 17 + "format" "Process error: exit code 1" 18 + (Claude.Error.to_string err) 19 + 20 + let test_connection_error_format () = 21 + let err = Claude.Error.Connection_error "refused" in 22 + Alcotest.(check string) 23 + "format" "Connection error: refused" 24 + (Claude.Error.to_string err) 25 + 26 + let test_protocol_error_format () = 27 + let err = Claude.Error.Protocol_error "bad json" in 28 + Alcotest.(check string) 29 + "format" "Protocol error: bad json" 30 + (Claude.Error.to_string err) 31 + 32 + let test_timeout_format () = 33 + let err = Claude.Error.Timeout "30s elapsed" in 34 + Alcotest.(check string) 35 + "format" "Timeout: 30s elapsed" 36 + (Claude.Error.to_string err) 37 + 38 + let test_permission_denied_format () = 39 + let err = 40 + Claude.Error.Permission_denied 41 + { tool_name = "Bash"; message = "not allowed" } 42 + in 43 + Alcotest.(check string) 44 + "format" "Permission denied for tool 'Bash': not allowed" 45 + (Claude.Error.to_string err) 46 + 47 + let test_hook_error_format () = 48 + let err = 49 + Claude.Error.Hook_error { callback_id = "cb-1"; message = "hook failed" } 50 + in 51 + Alcotest.(check string) 52 + "format" "Hook error (callback_id=cb-1): hook failed" 53 + (Claude.Error.to_string err) 54 + 55 + let test_control_error_format () = 56 + let err = 57 + Claude.Error.Control_error { request_id = "req-42"; message = "invalid" } 58 + in 59 + Alcotest.(check string) 60 + "format" "Control error (request_id=req-42): invalid" 61 + (Claude.Error.to_string err) 62 + 63 + let test_raise_cli_not_found () = 64 + match Claude.Error.cli_not_found "missing" with 65 + | exception Claude.Error.E (Claude.Error.Cli_not_found "missing") -> () 66 + | exception _ -> Alcotest.fail "Wrong exception type" 67 + | _ -> Alcotest.fail "Expected exception" 68 + 69 + let test_raise_process_error () = 70 + match Claude.Error.process_error "crash" with 71 + | exception Claude.Error.E (Claude.Error.Process_error "crash") -> () 72 + | exception _ -> Alcotest.fail "Wrong exception type" 73 + | _ -> Alcotest.fail "Expected exception" 74 + 75 + let test_raise_connection_error () = 76 + match Claude.Error.connection_error "reset" with 77 + | exception Claude.Error.E (Claude.Error.Connection_error "reset") -> () 78 + | exception _ -> Alcotest.fail "Wrong exception type" 79 + | _ -> Alcotest.fail "Expected exception" 80 + 81 + let test_raise_protocol_error () = 82 + match Claude.Error.protocol_error "malformed" with 83 + | exception Claude.Error.E (Claude.Error.Protocol_error "malformed") -> () 84 + | exception _ -> Alcotest.fail "Wrong exception type" 85 + | _ -> Alcotest.fail "Expected exception" 86 + 87 + let test_raise_timeout () = 88 + match Claude.Error.timeout "expired" with 89 + | exception Claude.Error.E (Claude.Error.Timeout "expired") -> () 90 + | exception _ -> Alcotest.fail "Wrong exception type" 91 + | _ -> Alcotest.fail "Expected exception" 92 + 93 + let test_raise_permission_denied () = 94 + match Claude.Error.permission_denied ~tool_name:"Edit" ~message:"blocked" with 95 + | exception 96 + Claude.Error.E 97 + (Claude.Error.Permission_denied 98 + { tool_name = "Edit"; message = "blocked" }) -> 99 + () 100 + | exception _ -> Alcotest.fail "Wrong exception type" 101 + | _ -> Alcotest.fail "Expected exception" 102 + 103 + let test_raise_hook_error () = 104 + match Claude.Error.hook_error ~callback_id:"cb-x" ~message:"fail" with 105 + | exception 106 + Claude.Error.E 107 + (Claude.Error.Hook_error { callback_id = "cb-x"; message = "fail" }) -> 108 + () 109 + | exception _ -> Alcotest.fail "Wrong exception type" 110 + | _ -> Alcotest.fail "Expected exception" 111 + 112 + let test_raise_control_error () = 113 + match Claude.Error.control_error ~request_id:"req-1" ~message:"bad" with 114 + | exception 115 + Claude.Error.E 116 + (Claude.Error.Control_error { request_id = "req-1"; message = "bad" }) 117 + -> 118 + () 119 + | exception _ -> Alcotest.fail "Wrong exception type" 120 + | _ -> Alcotest.fail "Expected exception" 121 + 122 + let test_ok_success () = 123 + let v = Claude.Error.ok ~msg:"test: " (Ok 42) in 124 + Alcotest.(check int) "ok value" 42 v 125 + 126 + let test_ok_error () = 127 + match Claude.Error.ok ~msg:"prefix: " (Error "reason") with 128 + | exception Claude.Error.E (Claude.Error.Protocol_error msg) -> 129 + Alcotest.(check bool) "contains prefix" true (String.length msg > 0) 130 + | exception _ -> Alcotest.fail "Wrong exception type" 131 + | _ -> Alcotest.fail "Expected exception" 132 + 133 + let test_ok'_success () = 134 + let v = Claude.Error.ok' ~msg:"test: " (Ok "hello") in 135 + Alcotest.(check string) "ok' value" "hello" v 136 + 137 + let test_ok'_error () = 138 + match Claude.Error.ok' ~msg:"prefix: " (Error "reason") with 139 + | exception Claude.Error.E (Claude.Error.Protocol_error _) -> () 140 + | exception _ -> Alcotest.fail "Wrong exception type" 141 + | _ -> Alcotest.fail "Expected exception" 142 + 143 + let test_pp_output () = 144 + let err = Claude.Error.Timeout "10s" in 145 + let buf = Buffer.create 32 in 146 + let ppf = Format.formatter_of_buffer buf in 147 + Claude.Error.pp ppf err; 148 + Format.pp_print_flush ppf (); 149 + Alcotest.(check string) "pp output" "Timeout: 10s" (Buffer.contents buf) 150 + 151 + let suite = 152 + ( "err", 153 + [ 154 + Alcotest.test_case "Cli_not_found format" `Quick test_cli_not_found_format; 155 + Alcotest.test_case "Process_error format" `Quick test_process_error_format; 156 + Alcotest.test_case "Connection_error format" `Quick 157 + test_connection_error_format; 158 + Alcotest.test_case "Protocol_error format" `Quick 159 + test_protocol_error_format; 160 + Alcotest.test_case "Timeout format" `Quick test_timeout_format; 161 + Alcotest.test_case "Permission_denied format" `Quick 162 + test_permission_denied_format; 163 + Alcotest.test_case "Hook_error format" `Quick test_hook_error_format; 164 + Alcotest.test_case "Control_error format" `Quick test_control_error_format; 165 + Alcotest.test_case "raise cli_not_found" `Quick test_raise_cli_not_found; 166 + Alcotest.test_case "raise process_error" `Quick test_raise_process_error; 167 + Alcotest.test_case "raise connection_error" `Quick 168 + test_raise_connection_error; 169 + Alcotest.test_case "raise protocol_error" `Quick test_raise_protocol_error; 170 + Alcotest.test_case "raise timeout" `Quick test_raise_timeout; 171 + Alcotest.test_case "raise permission_denied" `Quick 172 + test_raise_permission_denied; 173 + Alcotest.test_case "raise hook_error" `Quick test_raise_hook_error; 174 + Alcotest.test_case "raise control_error" `Quick test_raise_control_error; 175 + Alcotest.test_case "ok success" `Quick test_ok_success; 176 + Alcotest.test_case "ok error" `Quick test_ok_error; 177 + Alcotest.test_case "ok' success" `Quick test_ok'_success; 178 + Alcotest.test_case "ok' error" `Quick test_ok'_error; 179 + Alcotest.test_case "pp output" `Quick test_pp_output; 180 + ] )
+2
test/test_error.mli
··· 1 + val suite : string * unit Alcotest.test_case list 2 + (** Test suite. *)