let test_error_creation () = let error = Zulip.Error.create ~code:Invalid_api_key ~msg:"test error" () in Alcotest.(check string) "error message" "test error" (Zulip.Error.message error); Alcotest.(check bool) "error code" true (match Zulip.Error.code error with Invalid_api_key -> true | _ -> false) let test_auth_creation () = let auth = Zulip.Auth.create ~server_url:"https://test.zulip.com" ~email:"test@example.com" ~api_key:"test-key" in Alcotest.(check string) "server url" "https://test.zulip.com" (Zulip.Auth.server_url auth); Alcotest.(check string) "email" "test@example.com" (Zulip.Auth.email auth) let test_message_type () = Alcotest.(check string) "direct message type" "direct" (Zulip.Message_type.to_string `Direct); Alcotest.(check string) "channel message type" "stream" (Zulip.Message_type.to_string `Channel); match Zulip.Message_type.of_string "direct" with | Some `Direct -> () | _ -> Alcotest.fail "should parse direct message type" let test_message_creation () = let message = Zulip.Message.create ~type_:`Channel ~to_:["general"] ~content:"test message" ~topic:"test topic" () in Alcotest.(check string) "message content" "test message" (Zulip.Message.content message); match Zulip.Message.topic message with | Some "test topic" -> () | _ -> Alcotest.fail "should have topic" let test_message_json () = let message = Zulip.Message.create ~type_:`Direct ~to_:["user@example.com"] ~content:"Hello world" () in let json = Zulip.Message.to_json message in match json with | `O fields -> (match List.assoc "type" fields with | `String "direct" -> () | _ -> Alcotest.fail "type should be direct"); (match List.assoc "content" fields with | `String "Hello world" -> () | _ -> Alcotest.fail "content should match") | _ -> Alcotest.fail "should be JSON object" let test_error_json () = let error_json = `O [ ("code", `String "INVALID_API_KEY"); ("msg", `String "Invalid API key"); ("result", `String "error") ] in match Zulip.Error.of_json error_json with | Some error -> Alcotest.(check string) "error message" "Invalid API key" (Zulip.Error.message error); (match Zulip.Error.code error with | Invalid_api_key -> () | _ -> Alcotest.fail "should be Invalid_api_key") | None -> Alcotest.fail "should parse error JSON" let test_message_response_json () = let response_json = `O [ ("id", `Float 12345.0); ("result", `String "success") ] in match Zulip.Message_response.of_json response_json with | Ok response -> Alcotest.(check int) "message id" 12345 (Zulip.Message_response.id response) | Error _ -> Alcotest.fail "should parse message response JSON" let test_client_creation () = let auth = Zulip.Auth.create ~server_url:"https://test.zulip.com" ~email:"test@example.com" ~api_key:"test-key" in let client = Zulip.Client.create () auth in (* Test basic client functionality with mock *) match Zulip.Client.request client ~method_:`GET ~path:"/test" () with | Ok _response -> () (* Mock always succeeds *) | Error _ -> Alcotest.fail "mock request should succeed" let test_messages_send () = let auth = Zulip.Auth.create ~server_url:"https://test.zulip.com" ~email:"test@example.com" ~api_key:"test-key" in let client = Zulip.Client.create () auth in let message = Zulip.Message.create ~type_:`Channel ~to_:["general"] ~content:"test message" () in (* Since client is mocked, this will return a mock error but verify the interface works *) match Zulip.Messages.send client message with | Ok _response -> () (* If mock succeeds, that's fine *) | Error _err -> () (* Expected since we're using mock client *) let () = let open Alcotest in run "Zulip Tests" [ "error", [ test_case "Error creation" `Quick test_error_creation; test_case "Error JSON parsing" `Quick test_error_json; ]; "auth", [ test_case "Auth creation" `Quick test_auth_creation; ]; "message_type", [ test_case "Message type conversion" `Quick test_message_type; ]; "message", [ test_case "Message creation" `Quick test_message_creation; test_case "Message JSON serialization" `Quick test_message_json; ]; "message_response", [ test_case "Message response JSON parsing" `Quick test_message_response_json; ]; "client", [ test_case "Client creation and mock request" `Quick test_client_creation; ]; "messages", [ test_case "Message send API" `Quick test_messages_send; ]; ]