AtlatestRepositorysigil-mcp
1
(import (sigil test)2
(sigil mcp protocol)3
(sigil json))5
;; ============================================================6
;; parse-message7
;; ============================================================9
(test-group "parse-message"10
(test "request with id+method+params"11
(let* ((json (json-decode "{\"jsonrpc\":\"2.0\",\"id\":1,\"method\":\"ping\",\"params\":{}}"))12
(msg (parse-message json)))13
(assert-true (jsonrpc-request? msg))14
(assert-equal 1 (jsonrpc-request-id msg))15
(assert-equal "ping" (jsonrpc-request-method msg))))17
(test "notification (no id)"18
(let* ((json (json-decode "{\"jsonrpc\":\"2.0\",\"method\":\"notify\",\"params\":{}}"))19
(msg (parse-message json)))20
(assert-true (jsonrpc-notification? msg))21
(assert-equal "notify" (jsonrpc-notification-method msg))))23
(test "missing jsonrpc field returns #f"24
(let* ((json (json-decode "{\"id\":1,\"method\":\"test\"}"))25
(msg (parse-message json)))26
(assert-false msg)))28
(test "wrong version returns #f"29
(let* ((json (json-decode "{\"jsonrpc\":\"1.0\",\"id\":1,\"method\":\"test\"}"))30
(msg (parse-message json)))31
(assert-false msg)))33
(test "missing method returns #f"34
(let* ((json (json-decode "{\"jsonrpc\":\"2.0\",\"id\":1}"))35
(msg (parse-message json)))36
(assert-false msg)))38
(test "non-dict returns #f"39
(assert-false (parse-message "not a dict"))))41
;; ============================================================42
;; parse-message-string43
;; ============================================================45
(test-group "parse-message-string"46
(test "JSON string roundtrip"47
(let ((msg (parse-message-string "{\"jsonrpc\":\"2.0\",\"id\":42,\"method\":\"tools/list\",\"params\":{}}")))48
(assert-true (jsonrpc-request? msg))49
(assert-equal 42 (jsonrpc-request-id msg))50
(assert-equal "tools/list" (jsonrpc-request-method msg)))))52
;; ============================================================53
;; Serialization54
;; ============================================================56
(test-group "serialization"57
(test "response->json produces valid JSON"58
(let* ((resp (make-response 1 "ok"))59
(json-str (response->json resp))60
(parsed (json-decode json-str)))61
(assert-true (dict? parsed))62
(assert-equal "2.0" (dict-ref parsed jsonrpc:))63
(assert-equal 1 (dict-ref parsed id:))64
(assert-equal "ok" (dict-ref parsed result:))))66
(test "error-response->json includes error"67
(let* ((resp (make-error-response 1 -32600 "Invalid Request"))68
(json-str (error-response->json resp))69
(parsed (json-decode json-str)))70
(assert-true (dict? parsed))71
(assert-equal 1 (dict-ref parsed id:))72
(let ((err (dict-ref parsed error:)))73
(assert-true (dict? err))74
(assert-equal -32600 (dict-ref err code:))75
(assert-equal "Invalid Request" (dict-ref err message:)))))77
(test "notification->json omits id"78
(let* ((notif (make-notification "update"))79
(json-str (notification->json notif))80
(parsed (json-decode json-str)))81
(assert-equal "2.0" (dict-ref parsed jsonrpc:))82
(assert-equal "update" (dict-ref parsed method:))83
(assert-false (dict-ref parsed id: #f))))85
(test "request->json includes all fields"86
(let* ((req (jsonrpc-request id: 5 method: "test" params: (dict)))87
(json-str (request->json req))88
(parsed (json-decode json-str)))89
(assert-equal 5 (dict-ref parsed id:))90
(assert-equal "test" (dict-ref parsed method:)))))92
;; ============================================================93
;; Helper constructors94
;; ============================================================96
(test-group "helper constructors"97
(test "make-response"98
(let ((r (make-response 1 "data")))99
(assert-true (jsonrpc-response? r))100
(assert-equal 1 (jsonrpc-response-id r))101
(assert-equal "data" (jsonrpc-response-result r))))103
(test "make-error-response without data"104
(let ((r (make-error-response 2 -32700 "Parse error")))105
(assert-true (jsonrpc-error-response? r))106
(assert-equal 2 (jsonrpc-error-response-id r))107
(assert-equal -32700 (jsonrpc-error-response-code r))108
(assert-equal "Parse error" (jsonrpc-error-response-message r))))110
(test "make-notification"111
(let ((n (make-notification "event")))112
(assert-true (jsonrpc-notification? n))113
(assert-equal "event" (jsonrpc-notification-method n)))))115
;; ============================================================116
;; Error codes117
;; ============================================================119
(test-group "error codes"120
(test "error-parse"121
(assert-equal -32700 error-parse))123
(test "error-invalid-request"124
(assert-equal -32600 error-invalid-request))126
(test "error-method-not-found"127
(assert-equal -32601 error-method-not-found))129
(test "error-invalid-params"130
(assert-equal -32602 error-invalid-params))132
(test "error-internal"133
(assert-equal -32603 error-internal)))135
(run-tests)