Some testing and fixing receiving messages over SSE
This commit is contained in:
@@ -189,3 +189,22 @@
|
||||
"timestamp" 1717930000000
|
||||
"message" "new message here")))
|
||||
(should (string-match-p "Sarah> new message here" (buffer-string))))))
|
||||
|
||||
(ert-deftest noise-receive-handle-sse-event-stores-message ()
|
||||
"An SSE envelope event stores the message and refreshes the open buffer."
|
||||
(noise-receive-test--with-db
|
||||
(noise-db-upsert-conversation "+1" :type "direct" :name "Sarah"
|
||||
:members '("+1"))
|
||||
(require 'noise-conversation)
|
||||
(with-current-buffer (noise-conversation-open "+1")
|
||||
(noise-receive--handle-sse-event
|
||||
(noise-receive-test--ht
|
||||
"envelope"
|
||||
(noise-receive-test--ht
|
||||
"sourceNumber" "+1" "sourceName" "Sarah"
|
||||
"dataMessage" (noise-receive-test--ht
|
||||
"timestamp" 1717930000000
|
||||
"message" "from sse"))))
|
||||
(should (string-match-p "Sarah> from sse" (buffer-string))))
|
||||
(let ((msg (car (noise-db-get-messages "+1"))))
|
||||
(should (string= "from sse" (gethash "body" msg))))))
|
||||
|
||||
+64
-15
@@ -60,24 +60,73 @@
|
||||
|
||||
(ert-deftest noise-sse--filter-processes-complete-event ()
|
||||
"Process filter parses and calls handler for complete events."
|
||||
(let ((processed-events '()))
|
||||
(cl-letf (((symbol-function 'noise-sse--handle-event)
|
||||
(lambda (event) (push event processed-events))))
|
||||
(let ((noise-sse--buffer ""))
|
||||
(noise-sse--filter nil "data: {\"type\":\"message\"}\n\n")
|
||||
(should (= 1 (length processed-events)))
|
||||
(should (hash-table-p (car processed-events)))))))
|
||||
(let ((processed-events '())
|
||||
(noise-sse--buffer "")
|
||||
(noise-sse--http-buffer "")
|
||||
(noise-sse--chunk-buffer "")
|
||||
(noise-sse--headers-received t)
|
||||
(noise-sse--chunked-response-p nil))
|
||||
(let ((noise-sse--event-handler
|
||||
(lambda (event) (push event processed-events))))
|
||||
(noise-sse--filter nil "data: {\"type\":\"message\"}\n\n")
|
||||
(should (= 1 (length processed-events)))
|
||||
(should (hash-table-p (car processed-events))))))
|
||||
|
||||
(ert-deftest noise-sse--filter-buffers-incomplete-event ()
|
||||
"Process filter buffers incomplete events until newline."
|
||||
(let ((processed-events '()))
|
||||
(cl-letf (((symbol-function 'noise-sse--handle-event)
|
||||
(lambda (event) (push event processed-events))))
|
||||
(let ((noise-sse--buffer ""))
|
||||
(noise-sse--filter nil "data: {\"type\":\"message\"")
|
||||
(should (= 0 (length processed-events)))
|
||||
(noise-sse--filter nil "}\n\n")
|
||||
(should (= 1 (length processed-events)))))))
|
||||
(let ((processed-events '())
|
||||
(noise-sse--buffer "")
|
||||
(noise-sse--http-buffer "")
|
||||
(noise-sse--chunk-buffer "")
|
||||
(noise-sse--headers-received t)
|
||||
(noise-sse--chunked-response-p nil))
|
||||
(let ((noise-sse--event-handler
|
||||
(lambda (event) (push event processed-events))))
|
||||
(noise-sse--filter nil "data: {\"type\":\"message\"")
|
||||
(should (= 0 (length processed-events)))
|
||||
(noise-sse--filter nil "}\n\n")
|
||||
(should (= 1 (length processed-events))))))
|
||||
|
||||
(ert-deftest noise-sse--filter-skips-http-headers ()
|
||||
"HTTP response headers should not block the first SSE event."
|
||||
(let ((processed-events '())
|
||||
(noise-sse--buffer "")
|
||||
(noise-sse--http-buffer "")
|
||||
(noise-sse--chunk-buffer "")
|
||||
(noise-sse--headers-received nil)
|
||||
(noise-sse--chunked-response-p nil))
|
||||
(let ((noise-sse--event-handler
|
||||
(lambda (event) (push event processed-events))))
|
||||
(noise-sse--filter nil
|
||||
(concat "HTTP/1.1 200 OK\r\n"
|
||||
"Content-Type: text/event-stream\r\n\r\n"
|
||||
"data: {\"type\":\"message\"}\n\n"))
|
||||
(should (= 1 (length processed-events)))
|
||||
(should (equal "message" (gethash "type" (car processed-events)))))))
|
||||
|
||||
(ert-deftest noise-sse--filter-decodes-chunked-http-body ()
|
||||
"Chunked HTTP SSE responses should be decoded before parsing events."
|
||||
(let ((processed-events '())
|
||||
(noise-sse--buffer "")
|
||||
(noise-sse--http-buffer "")
|
||||
(noise-sse--chunk-buffer "")
|
||||
(noise-sse--headers-received nil)
|
||||
(noise-sse--chunked-response-p nil))
|
||||
(let ((noise-sse--event-handler
|
||||
(lambda (event) (push event processed-events))))
|
||||
(noise-sse--filter nil
|
||||
(concat "HTTP/1.1 200 OK\r\n"
|
||||
"Transfer-Encoding: chunked\r\n"
|
||||
"Content-Type: text/event-stream\r\n\r\n"
|
||||
"12\r\n"
|
||||
"data: {\"type\":\"msg"
|
||||
"\r\n"
|
||||
"4\r\n"
|
||||
"\"}\n\n"
|
||||
"\r\n"
|
||||
"0\r\n\r\n"))
|
||||
(should (= 1 (length processed-events)))
|
||||
(should (equal "msg" (gethash "type" (car processed-events)))))))
|
||||
|
||||
(ert-deftest noise-sse--build-url ()
|
||||
"Build SSE endpoint URL from base address."
|
||||
|
||||
Reference in New Issue
Block a user