My agentic slop goes here. Not intended for anyone else!
1let test_error_creation () =
2 let error = Zulip.Error.create ~code:Invalid_api_key ~msg:"test error" () in
3 Alcotest.(check string) "error message" "test error" (Zulip.Error.message error);
4 Alcotest.(check bool) "error code" true
5 (match Zulip.Error.code error with Invalid_api_key -> true | _ -> false)
6
7let test_auth_creation () =
8 let auth = Zulip.Auth.create
9 ~server_url:"https://test.zulip.com"
10 ~email:"test@example.com"
11 ~api_key:"test-key" in
12 Alcotest.(check string) "server url" "https://test.zulip.com" (Zulip.Auth.server_url auth);
13 Alcotest.(check string) "email" "test@example.com" (Zulip.Auth.email auth)
14
15let test_message_type () =
16 Alcotest.(check string) "direct message type" "direct" (Zulip.Message_type.to_string `Direct);
17 Alcotest.(check string) "channel message type" "stream" (Zulip.Message_type.to_string `Channel);
18 match Zulip.Message_type.of_string "direct" with
19 | Some `Direct -> ()
20 | _ -> Alcotest.fail "should parse direct message type"
21
22let test_message_creation () =
23 let message = Zulip.Message.create
24 ~type_:`Channel
25 ~to_:["general"]
26 ~content:"test message"
27 ~topic:"test topic"
28 () in
29 Alcotest.(check string) "message content" "test message" (Zulip.Message.content message);
30 match Zulip.Message.topic message with
31 | Some "test topic" -> ()
32 | _ -> Alcotest.fail "should have topic"
33
34let test_message_json () =
35 let message = Zulip.Message.create
36 ~type_:`Direct
37 ~to_:["user@example.com"]
38 ~content:"Hello world"
39 () in
40 let json = Zulip.Message.to_json message in
41 match json with
42 | `O fields ->
43 (match List.assoc "type" fields with
44 | `String "direct" -> ()
45 | _ -> Alcotest.fail "type should be direct");
46 (match List.assoc "content" fields with
47 | `String "Hello world" -> ()
48 | _ -> Alcotest.fail "content should match")
49 | _ -> Alcotest.fail "should be JSON object"
50
51let test_error_json () =
52 let error_json = `O [
53 ("code", `String "INVALID_API_KEY");
54 ("msg", `String "Invalid API key");
55 ("result", `String "error")
56 ] in
57 match Zulip.Error.of_json error_json with
58 | Some error ->
59 Alcotest.(check string) "error message" "Invalid API key" (Zulip.Error.message error);
60 (match Zulip.Error.code error with
61 | Invalid_api_key -> ()
62 | _ -> Alcotest.fail "should be Invalid_api_key")
63 | None -> Alcotest.fail "should parse error JSON"
64
65let test_message_response_json () =
66 let response_json = `O [
67 ("id", `Float 12345.0);
68 ("result", `String "success")
69 ] in
70 match Zulip.Message_response.of_json response_json with
71 | Ok response ->
72 Alcotest.(check int) "message id" 12345 (Zulip.Message_response.id response)
73 | Error _ -> Alcotest.fail "should parse message response JSON"
74
75let test_client_creation () =
76 let auth = Zulip.Auth.create
77 ~server_url:"https://test.zulip.com"
78 ~email:"test@example.com"
79 ~api_key:"test-key" in
80 let client = Zulip.Client.create () auth in
81 (* Test basic client functionality with mock *)
82 match Zulip.Client.request client ~method_:`GET ~path:"/test" () with
83 | Ok _response -> () (* Mock always succeeds *)
84 | Error _ -> Alcotest.fail "mock request should succeed"
85
86let test_messages_send () =
87 let auth = Zulip.Auth.create
88 ~server_url:"https://test.zulip.com"
89 ~email:"test@example.com"
90 ~api_key:"test-key" in
91 let client = Zulip.Client.create () auth in
92 let message = Zulip.Message.create
93 ~type_:`Channel
94 ~to_:["general"]
95 ~content:"test message"
96 () in
97 (* Since client is mocked, this will return a mock error but verify the interface works *)
98 match Zulip.Messages.send client message with
99 | Ok _response -> () (* If mock succeeds, that's fine *)
100 | Error _err -> () (* Expected since we're using mock client *)
101
102let () =
103 let open Alcotest in
104 run "Zulip Tests" [
105 "error", [
106 test_case "Error creation" `Quick test_error_creation;
107 test_case "Error JSON parsing" `Quick test_error_json;
108 ];
109 "auth", [
110 test_case "Auth creation" `Quick test_auth_creation;
111 ];
112 "message_type", [
113 test_case "Message type conversion" `Quick test_message_type;
114 ];
115 "message", [
116 test_case "Message creation" `Quick test_message_creation;
117 test_case "Message JSON serialization" `Quick test_message_json;
118 ];
119 "message_response", [
120 test_case "Message response JSON parsing" `Quick test_message_response_json;
121 ];
122 "client", [
123 test_case "Client creation and mock request" `Quick test_client_creation;
124 ];
125 "messages", [
126 test_case "Message send API" `Quick test_messages_send;
127 ];
128 ]