My agentic slop goes here. Not intended for anyone else!
1let test_bot_config_from_toml () =
2 let toml_content = {|
3name = "Test Bot"
4version = "1.0"
5
6[bot]
7api_key = "test-key"
8timeout = 30
9enabled = true
10
11[features]
12caching = false
13|} in
14
15 let temp_file = Filename.temp_file "bot_config" ".toml" in
16 let oc = open_out temp_file in
17 output_string oc toml_content;
18 close_out oc;
19
20 (match Zulip_bot.Bot_config.from_file temp_file with
21 | Ok config ->
22 Alcotest.(check (option string)) "name" (Some "Test Bot") (Zulip_bot.Bot_config.get config ~key:"name");
23 Alcotest.(check (option string)) "version" (Some "1.0") (Zulip_bot.Bot_config.get config ~key:"version");
24 Alcotest.(check (option string)) "api_key" (Some "test-key") (Zulip_bot.Bot_config.get config ~key:"api_key");
25 Alcotest.(check (option string)) "timeout" (Some "30") (Zulip_bot.Bot_config.get config ~key:"timeout");
26 Alcotest.(check (option string)) "enabled" (Some "true") (Zulip_bot.Bot_config.get config ~key:"enabled");
27 Alcotest.(check (option string)) "caching" (Some "false") (Zulip_bot.Bot_config.get config ~key:"caching");
28 Sys.remove temp_file
29 | Error err ->
30 Sys.remove temp_file;
31 Alcotest.fail ("Failed to parse bot config TOML: " ^ Zulip.Error.message err))
32
33let test_bot_config_required_key () =
34 let config = Zulip_bot.Bot_config.create [
35 ("required_key", "present");
36 ("optional_key", "also_present");
37 ] in
38
39 (match Zulip_bot.Bot_config.get_required config ~key:"required_key" with
40 | Ok value -> Alcotest.(check string) "required value" "present" value
41 | Error _ -> Alcotest.fail "Should have found required key");
42
43 (match Zulip_bot.Bot_config.get_required config ~key:"missing_key" with
44 | Ok _ -> Alcotest.fail "Should have failed for missing key"
45 | Error err -> Alcotest.(check bool) "has missing key error" true
46 (String.contains (Zulip.Error.message err) 'm'))
47
48let test_bot_config_env_vars () =
49 (* Set some test environment variables *)
50 Unix.putenv "BOT_API_KEY" "env-test-key";
51 Unix.putenv "BOT_TIMEOUT" "45";
52 Unix.putenv "OTHER_VAR" "should-be-ignored";
53
54 (match Zulip_bot.Bot_config.from_env ~prefix:"BOT_" with
55 | Ok config ->
56 Alcotest.(check (option string)) "api_key from env" (Some "env-test-key")
57 (Zulip_bot.Bot_config.get config ~key:"API_KEY");
58 Alcotest.(check (option string)) "timeout from env" (Some "45")
59 (Zulip_bot.Bot_config.get config ~key:"TIMEOUT");
60 Alcotest.(check (option string)) "other var not included" None
61 (Zulip_bot.Bot_config.get config ~key:"OTHER_VAR")
62 | Error err ->
63 Alcotest.fail ("Failed to read environment config: " ^ Zulip.Error.message err))
64
65let () =
66 let open Alcotest in
67 run "Bot Config Tests" [
68 "bot_config", [
69 test_case "Parse bot config TOML" `Quick test_bot_config_from_toml;
70 test_case "Required vs optional keys" `Quick test_bot_config_required_key;
71 test_case "Environment variable config" `Quick test_bot_config_env_vars;
72 ];
73 ]