From 15baff4f2671a2c1b772a893435494d30910d5ca Mon Sep 17 00:00:00 2001 From: James Brechtel Date: Thu, 11 Jun 2026 12:45:25 -0400 Subject: [PATCH] feat: add SSE URL construction and connection management --- noise-sse.el | 29 +++++++++++++++++++++++++++++ test/noise-sse-test.el | 19 +++++++++++++++++++ 2 files changed, 48 insertions(+) diff --git a/noise-sse.el b/noise-sse.el index f10934d..eb01277 100644 --- a/noise-sse.el +++ b/noise-sse.el @@ -71,5 +71,34 @@ Buffers incoming STRING and processes complete events." This function will be overridden in noise-receive to process envelopes." (message "SSE event received: %s" event)) +(defvar noise-signal-cli-address) +(defvar noise-account) + +(defun noise-sse--build-url () + "Build the SSE endpoint URL for signal-cli." + (let ((base (concat noise-signal-cli-address "/api/v1/events"))) + (if (and (boundp 'noise-account) noise-account) + (concat base "?account=" (url-hexify-string noise-account)) + base))) + +(defun noise-sse--sentinel (proc status) + "Process sentinel for SSE connection. +Handles connection close and triggers reconnection." + (when (or (equal status "finished\n") + (string-match-p "failed" status) + (string-match-p "deleted" status)) + (message "Noise: SSE connection lost, reconnecting in %ds..." + noise-sse--reconnect-delay) + (setq noise-sse--process nil) + (noise-sse--schedule-reconnect))) + +(defun noise-sse--schedule-reconnect () + "Schedule a reconnection attempt after the current delay." + (when noise-sse--reconnect-timer + (cancel-timer noise-sse--reconnect-timer)) + (setq noise-sse--reconnect-timer + (run-with-timer noise-sse--reconnect-delay nil + #'noise-sse-connect))) + (provide 'noise-sse) ;;; noise-sse.el ends here diff --git a/test/noise-sse-test.el b/test/noise-sse-test.el index 00c80f2..4f98d7f 100644 --- a/test/noise-sse-test.el +++ b/test/noise-sse-test.el @@ -9,6 +9,9 @@ (require 'ert) (require 'noise-sse) +(defvar noise-signal-cli-address) +(defvar noise-account) + (ert-deftest noise-sse--parse-event-basic () "Parse a basic SSE event with data field." (let ((event "data: {\"type\":\"message\",\"content\":\"hello\"}\n\n")) @@ -76,5 +79,21 @@ (noise-sse--filter nil "}\n\n") (should (= 1 (length processed-events))))))) +(ert-deftest noise-sse--build-url () + "Build SSE endpoint URL from base address." + (let ((noise-signal-cli-address "http://localhost:8080") + (noise-account "+15551234567")) + (let ((url (noise-sse--build-url))) + (should (string-match-p "/api/v1/events" url)) + (should (string-match-p "account=%2B15551234567" url))))) + +(ert-deftest noise-sse--build-url-no-account () + "Build SSE endpoint URL without account parameter." + (let ((noise-signal-cli-address "http://localhost:8080") + (noise-account nil)) + (let ((url (noise-sse--build-url))) + (should (string-match-p "/api/v1/events" url)) + (should-not (string-match-p "account=" url))))) + (provide 'noise-sse-test) ;;; noise-sse-test.el ends here