upleb.uk

Public git repos — served from a NIP-34 GRASP relay at git.upleb.uk

summaryrefslogtreecommitdiff
path: root/tests/unit/test_mcp_handler.c
diff options
context:
space:
mode:
Diffstat (limited to 'tests/unit/test_mcp_handler.c')
-rw-r--r--tests/unit/test_mcp_handler.c148
1 files changed, 148 insertions, 0 deletions
diff --git a/tests/unit/test_mcp_handler.c b/tests/unit/test_mcp_handler.c
new file mode 100644
index 0000000..aaa199d
--- /dev/null
+++ b/tests/unit/test_mcp_handler.c
@@ -0,0 +1,148 @@
1#include "test_framework.h"
2#include "mcp_handler.h"
3#include "config.h"
4#include "nucula_wallet.h"
5#include "cJSON.h"
6#include <string.h>
7#include <stdio.h>
8
9static tollgate_config_t g_test_config;
10static uint64_t g_wallet_balance = 0;
11static int g_wallet_proof_count = 0;
12static int g_wallet_send_rc = 0;
13static char g_wallet_send_token[256] = "cashuA_test_token";
14
15const tollgate_config_t *tollgate_config_get(void) {
16 return &g_test_config;
17}
18
19uint64_t nucula_wallet_balance(void) {
20 return g_wallet_balance;
21}
22
23int nucula_wallet_proof_count(void) {
24 return g_wallet_proof_count;
25}
26
27int nucula_wallet_send(uint64_t amount, char *token_out, size_t token_max) {
28 (void)amount;
29 (void)token_max;
30 if (g_wallet_send_rc == 0) {
31 strncpy(token_out, g_wallet_send_token, token_max - 1);
32 }
33 return g_wallet_send_rc;
34}
35
36static void test_mcp_parse_tool(void)
37{
38 printf("\n=== MCP tool parsing ===\n");
39 ASSERT_EQ_INT(MCP_TOOL_GET_CONFIG, mcp_parse_tool("get_config"), "get_config");
40 ASSERT_EQ_INT(MCP_TOOL_SET_CONFIG, mcp_parse_tool("set_config"), "set_config");
41 ASSERT_EQ_INT(MCP_TOOL_GET_BALANCE, mcp_parse_tool("get_balance"), "get_balance");
42 ASSERT_EQ_INT(MCP_TOOL_WALLET_SEND, mcp_parse_tool("wallet_send"), "wallet_send");
43 ASSERT_EQ_INT(MCP_TOOL_UNKNOWN, mcp_parse_tool("foo"), "unknown tool");
44 ASSERT_EQ_INT(MCP_TOOL_UNKNOWN, mcp_parse_tool(NULL), "NULL tool");
45}
46
47static void test_mcp_get_config(void)
48{
49 printf("\n=== MCP get_config ===\n");
50 memset(&g_test_config, 0, sizeof(g_test_config));
51 strncpy(g_test_config.ap_ssid, "TollGate-TEST", sizeof(g_test_config.ap_ssid) - 1);
52 strncpy(g_test_config.metric, "bytes", sizeof(g_test_config.metric) - 1);
53 g_test_config.price_per_step = 21;
54 g_test_config.step_size_ms = 60000;
55 g_test_config.step_size_bytes = 22020096;
56 strncpy(g_test_config.mint_url, "https://testnut.cashu.space", sizeof(g_test_config.mint_url) - 1);
57
58 mcp_response_t resp = mcp_handle_get_config();
59 ASSERT(resp.success, "get_config succeeds");
60
61 cJSON *result = cJSON_Parse(resp.result_json);
62 ASSERT(result != NULL, "result is valid JSON");
63 ASSERT_EQ_STR("bytes", cJSON_GetObjectItem(result, "metric")->valuestring, "metric=bytes");
64 ASSERT_EQ_INT(21, cJSON_GetObjectItem(result, "price_per_step")->valueint, "price=21");
65 cJSON_Delete(result);
66}
67
68static void test_mcp_set_config(void)
69{
70 printf("\n=== MCP set_config ===\n");
71 memset(&g_test_config, 0, sizeof(g_test_config));
72 g_test_config.price_per_step = 21;
73
74 const char *params = "{\"price_per_step\":42,\"metric\":\"milliseconds\"}";
75 mcp_response_t resp = mcp_handle_set_config(params);
76 ASSERT(resp.success, "set_config succeeds");
77 ASSERT_EQ_INT(42, g_test_config.price_per_step, "price updated to 42");
78 ASSERT_EQ_STR("milliseconds", g_test_config.metric, "metric updated");
79
80 resp = mcp_handle_set_config("not json");
81 ASSERT(!resp.success, "invalid JSON fails");
82}
83
84static void test_mcp_get_balance(void)
85{
86 printf("\n=== MCP get_balance ===\n");
87 g_wallet_balance = 500;
88 g_wallet_proof_count = 8;
89
90 mcp_response_t resp = mcp_handle_get_balance();
91 ASSERT(resp.success, "get_balance succeeds");
92
93 cJSON *result = cJSON_Parse(resp.result_json);
94 ASSERT(result != NULL, "result is valid JSON");
95 ASSERT_EQ_INT(500, (int)cJSON_GetObjectItem(result, "balance_sats")->valuedouble, "balance=500");
96 ASSERT_EQ_INT(8, cJSON_GetObjectItem(result, "proof_count")->valueint, "proofs=8");
97 cJSON_Delete(result);
98}
99
100static void test_mcp_wallet_send(void)
101{
102 printf("\n=== MCP wallet_send ===\n");
103 g_wallet_send_rc = 0;
104 strncpy(g_wallet_send_token, "cashuA_send_test", sizeof(g_wallet_send_token) - 1);
105
106 const char *params = "{\"amount\":21}";
107 mcp_response_t resp = mcp_handle_wallet_send(params);
108 ASSERT(resp.success, "wallet_send succeeds");
109
110 cJSON *result = cJSON_Parse(resp.result_json);
111 ASSERT(result != NULL, "result is valid JSON");
112 ASSERT_EQ_STR("cashuA_send_test", cJSON_GetObjectItem(result, "token")->valuestring, "token matches");
113 cJSON_Delete(result);
114
115 printf("\n--- wallet_send missing amount ---\n");
116 resp = mcp_handle_wallet_send("{}");
117 ASSERT(!resp.success, "missing amount fails");
118
119 printf("\n--- wallet_send send fails ---\n");
120 g_wallet_send_rc = -1;
121 resp = mcp_handle_wallet_send("{\"amount\":100}");
122 ASSERT(!resp.success, "send failure reported");
123}
124
125static void test_mcp_dispatch(void)
126{
127 printf("\n=== MCP dispatch ===\n");
128 mcp_request_t req = {0};
129 req.tool = MCP_TOOL_UNKNOWN;
130 strncpy(req.method, "bogus", sizeof(req.method) - 1);
131 mcp_response_t resp = mcp_dispatch(&req);
132 ASSERT(!resp.success, "unknown tool dispatch fails");
133
134 resp = mcp_dispatch(NULL);
135 ASSERT(!resp.success, "NULL request dispatch fails");
136}
137
138int main(void)
139{
140 printf("=== test_mcp_handler ===\n");
141 test_mcp_parse_tool();
142 test_mcp_get_config();
143 test_mcp_set_config();
144 test_mcp_get_balance();
145 test_mcp_wallet_send();
146 test_mcp_dispatch();
147 TEST_SUMMARY();
148}