AtlatestRepositorysigil-lsp

sigil-lsp / tree / testtest-protocol.sgl

1(import (sigil test)
2 (sigil lsp protocol)
3 (sigil json)
4 (sigil io)
5 (sigil string))
6
7(test-group "parse-message"
8 (test "request with id+method+params"
9 (let* ((json (json-decode "{\"jsonrpc\":\"2.0\",\"id\":1,\"method\":\"initialize\",\"params\":{}}"))
10 (msg (parse-message json)))
11 (assert-true (lsp-request? msg))
12 (assert-equal 1 (lsp-request-id msg))
13 (assert-equal "initialize" (lsp-request-method msg))))
15 (test "notification (no id)"
16 (let* ((json (json-decode "{\"jsonrpc\":\"2.0\",\"method\":\"initialized\",\"params\":{}}"))
17 (msg (parse-message json)))
18 (assert-true (lsp-notification? msg))
19 (assert-equal "initialized" (lsp-notification-method msg))))
21 (test "wrong jsonrpc version returns #f"
22 (assert-false (parse-message
23 (json-decode "{\"jsonrpc\":\"1.0\",\"id\":1,\"method\":\"x\"}")))))
25(test-group "serialization"
26 (test "response->json"
27 (let* ((resp (make-response 7 "ok"))
28 (parsed (json-decode (response->json resp))))
29 (assert-equal "2.0" (dict-ref parsed jsonrpc:))
30 (assert-equal 7 (dict-ref parsed id:))
31 (assert-equal "ok" (dict-ref parsed result:))))
33 (test "error-response->json"
34 (let* ((resp (make-error-response 3 -32601 "not found"))
35 (parsed (json-decode (error-response->json resp)))
36 (err (dict-ref parsed error:)))
37 (assert-equal -32601 (dict-ref err code:))
38 (assert-equal "not found" (dict-ref err message:))))
40 (test "notification->json omits id"
41 (let* ((notif (make-notification "didChange"))
42 (parsed (json-decode (notification->json notif))))
43 (assert-equal "didChange" (dict-ref parsed method:))
44 (assert-false (dict-ref parsed id: #f)))))
46(test-group "framed io"
47 (test "read-framed-message parses CRLF-framed request"
48 (let* ((body "{\"jsonrpc\":\"2.0\",\"id\":1,\"method\":\"initialize\",\"params\":{}}")
49 (frame (string-append
50 "Content-Length: " (number->string (string-length body))
51 "\r\n\r\n" body))
52 (port (open-input-string frame))
53 (msg (read-framed-message port)))
54 (assert-true (lsp-request? msg))
55 (assert-equal "initialize" (lsp-request-method msg))))
57 (test "read-framed-message tolerates extra header"
58 (let* ((body "{\"jsonrpc\":\"2.0\",\"id\":2,\"method\":\"shutdown\"}")
59 (frame (string-append
60 "Content-Type: application/vscode-jsonrpc; charset=utf-8\r\n"
61 "Content-Length: " (number->string (string-length body))
62 "\r\n\r\n" body))
63 (port (open-input-string frame))
64 (msg (read-framed-message port)))
65 (assert-true (lsp-request? msg))
66 (assert-equal "shutdown" (lsp-request-method msg))))
68 (test "read-framed-message returns eof on empty input"
69 (let ((port (open-input-string "")))
70 (assert-true (eof-object? (read-framed-message port))))))