Skip to content

The fastest, cheapest, most efficient open-source agent harness. Run more than 500 agents on a $10 VPS.

chore(ws_loop): make internal functions and constants pub(super) for unit testing

Steven Enamakel2f5e9623dd9c
diff --git a/crates/openhuman-core/src/platform/socket/ws_loop/connect.rs b/crates/openhuman-core/src/platform/socket/ws_loop/connect.rs
index b544a21d9c..e0a2daa93b 100644
--- a/crates/openhuman-core/src/platform/socket/ws_loop/connect.rs
+++ b/crates/openhuman-core/src/platform/socket/ws_loop/connect.rs
@@ -281,7 +281,7 @@ async fn read_sio_connect_ack(
///
/// On non-redirect failures the original error is returned and the caller
/// counts it toward the exponential backoff like before.
-async fn connect_with_redirects(
+pub(super) async fn connect_with_redirects(
ws_url: &mut String,
shared: &Arc<SharedState>,
) -> Result<WsStream, WsError> {
@@ -348,7 +348,7 @@ async fn connect_with_redirects(
/// 308 (Permanent Redirect) and 307 (Temporary Redirect) explicitly preserve
/// the method; 301/302 historically do too for upgrade requests in practice.
/// Anything else (300, 304, ...) stays an error.
-fn is_redirect_status(status: StatusCode) -> bool {
+pub(super) fn is_redirect_status(status: StatusCode) -> bool {
matches!(
status,
StatusCode::MOVED_PERMANENTLY
@@ -358,7 +358,7 @@ fn is_redirect_status(status: StatusCode) -> bool {
)
}
-fn extract_location_header(
+pub(super) fn extract_location_header(
response: &tokio_tungstenite::tungstenite::http::Response<Option<Vec<u8>>>,
) -> Option<String> {
response
@@ -374,7 +374,7 @@ fn extract_location_header(
/// `location` may be absolute (`https://host/path?q=1`) or relative
/// (`/socket.io/?EIO=4`). We use the `url` crate's relative-URL parser to do
/// the join the same way browsers do, then map `http`→`ws` / `https`→`wss`.
-fn resolve_redirect_target(current_ws_url: &str, location: &str) -> Result<String, String> {
+pub(super) fn resolve_redirect_target(current_ws_url: &str, location: &str) -> Result<String, String> {
let base = url::Url::parse(current_ws_url).map_err(|e| format!("invalid current URL: {e}"))?;
let resolved = base
.join(location)
@@ -396,7 +396,7 @@ fn resolve_redirect_target(current_ws_url: &str, location: &str) -> Result<Strin
/// Persist a one-shot, user-visible warning that the backend redirected the
/// configured socket URL. Subsequent redirects in the same connect attempt
/// don't overwrite — the first hop carries the actionable signal.
-fn record_redirect_warning(shared: &Arc<SharedState>, original: &str, resolved: &str) {
+pub(super) fn record_redirect_warning(shared: &Arc<SharedState>, original: &str, resolved: &str) {
let mut slot = shared.error.write();
if slot.is_some() {
return;
diff --git a/crates/openhuman-core/src/platform/socket/ws_loop/dispatch.rs b/crates/openhuman-core/src/platform/socket/ws_loop/dispatch.rs
index 126ecfb8b9..e330d7394e 100644
--- a/crates/openhuman-core/src/platform/socket/ws_loop/dispatch.rs
+++ b/crates/openhuman-core/src/platform/socket/ws_loop/dispatch.rs
@@ -50,7 +50,7 @@ pub(super) fn handle_eio_message(
}
/// Handle a Socket.IO packet (after stripping the Engine.IO '4' prefix).
-fn handle_sio_packet(
+pub(super) fn handle_sio_packet(
text: &str,
emit_tx: &mpsc::UnboundedSender<String>,
shared: &Arc<SharedState>,
@@ -124,7 +124,7 @@ fn handle_sio_packet(
}
}
-fn parse_sio_ack(text: &str) -> Option<(u64, serde_json::Value)> {
+pub(super) fn parse_sio_ack(text: &str) -> Option<(u64, serde_json::Value)> {
let json_start = text.find('[')?;
if json_start == 0 {
return None;
diff --git a/crates/openhuman-core/src/platform/socket/ws_loop/reconnect.rs b/crates/openhuman-core/src/platform/socket/ws_loop/reconnect.rs
index 0c39135c96..cc7e54804f 100644
--- a/crates/openhuman-core/src/platform/socket/ws_loop/reconnect.rs
+++ b/crates/openhuman-core/src/platform/socket/ws_loop/reconnect.rs
@@ -31,7 +31,7 @@ use crate::platform::socket::types::ConnectionOutcome;
///
/// See OPENHUMAN-TAURI-8M — a single gateway 503 incident generated 549
/// Sentry events because every retry was logged at `error`.
-const FAIL_ESCALATE_THRESHOLD: u32 = 5;
+pub(super) const FAIL_ESCALATE_THRESHOLD: u32 = 5;
/// Background loop that manages the WebSocket connection and reconnection.
///
@@ -332,7 +332,7 @@ pub(super) async fn ws_loop(
///
/// Extracted as a pure function so it can be unit-tested without running an
/// async event loop or touching the WS stack.
-fn log_connection_failure(consecutive: u32, reason: &str) {
+pub(super) fn log_connection_failure(consecutive: u32, reason: &str) {
if consecutive == FAIL_ESCALATE_THRESHOLD {
// Route the one-shot sustained-outage escalation through the
// observability classifier so an offline user (no wifi / airplane mode
@@ -371,7 +371,7 @@ fn log_connection_failure(consecutive: u32, reason: &str) {
/// Action the reconnect loop should take after receiving an "Invalid token"
/// rejection from the server.
-enum InvalidTokenAction {
+pub(super) enum InvalidTokenAction {
/// A genuinely different token is available — retry the connection
/// immediately (no backoff sleep). The fresh token is carried forward so
/// the next `run_connection` uses **exactly** the validated value, not
@@ -395,7 +395,7 @@ enum InvalidTokenAction {
/// - Provider returns the **same** token → `Escalate` (no point retrying).
/// - Provider returns an **empty** token → `Escalate` (treat as no session).
/// - Provider returns `Err` → `Escalate` with the provider error as reason.
-fn decide_after_invalid_token(
+pub(super) fn decide_after_invalid_token(
previous_token: &str,
provider: &TokenProvider,
) -> InvalidTokenAction {
@@ -428,7 +428,7 @@ fn decide_after_invalid_token(
///
/// A free function rather than an inline loop so the behaviour is directly
/// testable without standing up a socket.
-fn drain_pending_emits(rx: &mut mpsc::UnboundedReceiver<String>) -> usize {
+pub(super) fn drain_pending_emits(rx: &mut mpsc::UnboundedReceiver<String>) -> usize {
let mut dropped = 0usize;
while rx.try_recv().is_ok() {
dropped += 1;