feat: add JSON-RPC client with connection check command

This commit is contained in:
2026-06-09 19:31:03 -04:00
parent 9498cbb32b
commit f71a05de79
3 changed files with 143 additions and 0 deletions
+39
View File
@@ -0,0 +1,39 @@
;;; noise-rpc-test.el --- Tests for noise-rpc -*- lexical-binding: t; -*-
(require 'ert)
(require 'noise-rpc)
(ert-deftest noise-rpc--build-request-no-params ()
"Test that noise-rpc--build-request produces valid JSON-RPC 2.0 without params."
(let* ((body (noise-rpc--build-request "getVersion"))
(parsed (json-parse-string body)))
(should (string= "2.0" (gethash "jsonrpc" parsed)))
(should (string= "getVersion" (gethash "method" parsed)))
(should (integerp (gethash "id" parsed)))
(should-not (gethash "params" parsed))))
(ert-deftest noise-rpc--build-request-with-params ()
"Test that params are included when provided."
(let* ((body (noise-rpc--build-request "sendMessage" :recipient "+123" :message "hi"))
(parsed (json-parse-string body)))
(should (string= "sendMessage" (gethash "method" parsed)))
(let ((params (gethash "params" parsed)))
(should params)
(should (string= "+123" (gethash "recipient" params)))
(should (string= "hi" (gethash "message" params))))))
(ert-deftest noise-rpc--parse-response-success ()
"Test parsing a successful JSON-RPC response."
(let* ((raw "{\"jsonrpc\":\"2.0\",\"result\":{\"version\":\"0.13.0\"},\"id\":1}")
(result (noise-rpc--parse-response raw)))
(should result)
(should (string= "0.13.0" (gethash "version" result)))))
(ert-deftest noise-rpc--parse-response-error ()
"Test parsing a JSON-RPC error raises noise-rpc-error."
(let ((raw "{\"jsonrpc\":\"2.0\",\"error\":{\"code\":-32000,\"message\":\"method not found\"},\"id\":1}"))
(should-error (noise-rpc--parse-response raw) :type 'noise-rpc-error)))
(ert-deftest noise-rpc--parse-response-malformed ()
"Test that malformed JSON raises noise-rpc-error."
(should-error (noise-rpc--parse-response "not json") :type 'noise-rpc-error))