mirror of
https://github.com/outbackdingo/optimclaw.git
synced 2026-08-31 00:29:24 +00:00
* feat(testing): add StubChannel test double for Channel trait Adds StubChannel to src/testing.rs alongside StubLlm. Supports message injection via mpsc sender, response/status capture, and configurable health check toggling. Includes handle methods for use after ownership transfer to ChannelManager. Co-Authored-By: Claude Opus 4.6 <[email protected]> * feat(testing): wire StubChannel into TestHarnessBuilder Add with_stub_channel() builder method that creates a StubChannel pre-registered in a ChannelManager. Tests can inject messages via the sender and verify routing through the manager. The channel field on TestHarness is Optional, defaulting to None for backward compat. Co-Authored-By: Claude Opus 4.6 <[email protected]> * test: gate external-service tests behind integration feature flag Replace silent try_connect() skip pattern with explicit feature gating. cargo test now runs only self-contained tests. cargo test --features integration runs tests requiring PostgreSQL. Co-Authored-By: Claude Opus 4.6 <[email protected]> * test(channels): add ChannelManager unit tests using StubChannel Cover add/start_all stream merging, respond routing, unknown channel errors, health_check_all with mixed health, empty-channels error path, and injection channel merging -- all via StubChannel test double. Co-Authored-By: Claude Opus 4.6 <[email protected]> * docs: document test tier separation (unit/integration/live) Co-Authored-By: Claude Opus 4.6 <[email protected]> * ci: add architecture boundary check script Grep-based checks for three architecture boundaries: - Direct database driver usage (tokio_postgres/libsql) outside src/db/ - .unwrap()/.expect() in production code (warning only) - Direct std::env::var reads outside config layer (warning only) The DB driver check is a hard violation; the other two are warnings for gradual cleanup. Run with: bash scripts/check-boundaries.sh Co-Authored-By: Claude Opus 4.6 <[email protected]> * test(search): add RRF edge case tests for empty inputs, limits, and config modes Co-Authored-By: Claude Opus 4.6 <[email protected]> * test(security): add regression tests for skill installer ZIP and SSRF protections Add 11 regression tests covering the security controls in skill_tools: ZIP extraction safety: - Valid SKILL.md extraction works correctly - Non-SKILL.md entries are ignored (returns error) - Path traversal entries (../../SKILL.md) do not match - Nested path entries (subdir/SKILL.md) do not match - Oversized entries (>1MB uncompressed) are rejected SSRF prevention: - Loopback addresses (127.0.0.1) are blocked - Private ranges (10.x, 172.16.x, 192.168.x) are blocked - Link-local addresses (169.254.x) are blocked - Public IPs (8.8.8.8, 1.1.1.1) are allowed - IPv4-mapped IPv6 unwrapping logic works correctly - Metadata endpoints and .internal/.local hostnames are blocked - Normal hostnames (github.com, clawhub.dev) are allowed Also documents a known gap: url::Url::host_str() returns bracketed IPv6 addresses that std::net::IpAddr cannot parse, so IPv4-mapped IPv6 URLs currently bypass IP-based checks in validate_fetch_url. Co-Authored-By: Claude Opus 4.6 <[email protected]> * refactor(testing): extract TestGatewayBuilder to eliminate gateway test duplication Both ws_gateway_integration.rs and openai_compat_integration.rs manually constructed GatewayState with 19+ fields. Extracted to a shared builder in src/channels/web/test_helpers.rs that provides sensible defaults and lets tests override only what they need. Co-Authored-By: Claude Opus 4.6 <[email protected]> * docs: add implementation plans for testing batches 1 and 2 Co-Authored-By: Claude Opus 4.6 <[email protected]> * fix(security): close IPv6 SSRF bypass in validate_fetch_url validate_fetch_url used host_str() which returns bracketed IPv6 (e.g. "[::ffff:7f00:1]") that IpAddr::parse() cannot handle, silently skipping IP-based SSRF checks for all IPv6 URLs. Switch to url::Host enum matching to extract proper IpAddr values without string parsing. IPv4-mapped IPv6 addresses like ::ffff:127.0.0.1 are now correctly unwrapped and blocked. Co-Authored-By: Claude Opus 4.6 <[email protected]> * test(skills): add activation criteria limits enforcement tests Adds test_activation_criteria_enforce_limits to verify that enforce_limits() correctly trims excess patterns (>5), keywords (>20), and tags (>10), and filters out short keywords/tags (<3 chars). Co-Authored-By: Claude Opus 4.6 <[email protected]> * test(wasm): add security regression tests for WASM tool loader Add 6 tests covering: tool name path separator rejection, empty name rejection, nonexistent file handling, invalid WASM bytes rejection, dotfile discovery behavior, and subdirectory non-recursion. Co-Authored-By: Claude Opus 4.6 <[email protected]> * refactor: address PR review feedback - Remove plan files from repo (ilblackdragon review) - Replace CLAUDE.md test tier rules with pointer to check-boundaries.sh - Add Check 4 to check-boundaries.sh: enforces integration tests are gated behind the 'integration' feature flag Co-Authored-By: Claude Opus 4.6 <[email protected]> * ci: add try_connect silent-skip pattern check to check-boundaries.sh Check 5 catches try_connect() and similar silent-skip patterns in integration tests. Tests should use feature gates to fail loudly when prerequisites are missing, not silently return. Co-Authored-By: Claude Opus 4.6 <[email protected]> * fix(security): harden skill fetch SSRF checks * fix(scripts): use bash arrays in check-boundaries.sh tier violation check Refactor Check 4 in check-boundaries.sh to use bash arrays and printf instead of string concatenation with echo -e. This is more robust with special characters in filenames and avoids portability concerns with echo -e. [skip-regression-check] Co-Authored-By: Claude Opus 4.6 <[email protected]> --------- Co-authored-by: Claude Opus 4.6 <[email protected]>
317 lines
10 KiB
Rust
317 lines
10 KiB
Rust
//! End-to-end integration tests for the WebSocket gateway.
|
|
//!
|
|
//! These tests start a real Axum server on a random port, connect a WebSocket
|
|
//! client, and verify the full message flow:
|
|
//! - WebSocket upgrade with auth
|
|
//! - Ping/pong
|
|
//! - Client message → agent msg_tx
|
|
//! - Broadcast SSE event → WebSocket client
|
|
//! - Connection tracking (counter increment/decrement)
|
|
//! - Gateway status endpoint
|
|
|
|
use std::net::SocketAddr;
|
|
use std::sync::Arc;
|
|
use std::time::Duration;
|
|
|
|
use futures::{SinkExt, StreamExt};
|
|
use tokio::sync::mpsc;
|
|
use tokio::time::timeout;
|
|
use tokio_tungstenite::tungstenite::Message;
|
|
use tokio_tungstenite::tungstenite::client::IntoClientRequest;
|
|
|
|
use ironclaw::channels::IncomingMessage;
|
|
use ironclaw::channels::web::server::GatewayState;
|
|
use ironclaw::channels::web::test_helpers::TestGatewayBuilder;
|
|
use ironclaw::channels::web::types::SseEvent;
|
|
|
|
const AUTH_TOKEN: &str = "test-token-12345";
|
|
const TIMEOUT: Duration = Duration::from_secs(5);
|
|
|
|
/// Start a gateway server on a random port and return the bound address + agent
|
|
/// message receiver.
|
|
async fn start_test_server() -> (
|
|
SocketAddr,
|
|
Arc<GatewayState>,
|
|
mpsc::Receiver<IncomingMessage>,
|
|
) {
|
|
let (agent_tx, agent_rx) = mpsc::channel(64);
|
|
|
|
let (addr, state) = TestGatewayBuilder::new()
|
|
.msg_tx(agent_tx)
|
|
.start(AUTH_TOKEN)
|
|
.await
|
|
.expect("Failed to start test server");
|
|
|
|
(addr, state, agent_rx)
|
|
}
|
|
|
|
/// Connect a WebSocket client with auth token in query parameter.
|
|
async fn connect_ws(
|
|
addr: SocketAddr,
|
|
) -> tokio_tungstenite::WebSocketStream<tokio_tungstenite::MaybeTlsStream<tokio::net::TcpStream>> {
|
|
let url = format!("ws://{}/api/chat/ws?token={}", addr, AUTH_TOKEN);
|
|
let mut request = url.into_client_request().unwrap();
|
|
// Server requires an Origin header from localhost to prevent cross-site WS hijacking.
|
|
request.headers_mut().insert(
|
|
"Origin",
|
|
format!("http://127.0.0.1:{}", addr.port()).parse().unwrap(),
|
|
);
|
|
let (stream, _response) = tokio_tungstenite::connect_async(request)
|
|
.await
|
|
.expect("Failed to connect WebSocket");
|
|
stream
|
|
}
|
|
|
|
/// Read the next text frame from the WebSocket, with a timeout.
|
|
async fn recv_text(
|
|
stream: &mut (impl StreamExt<Item = Result<Message, tokio_tungstenite::tungstenite::Error>> + Unpin),
|
|
) -> String {
|
|
let msg = timeout(TIMEOUT, stream.next())
|
|
.await
|
|
.expect("Timed out waiting for WS message")
|
|
.expect("Stream ended")
|
|
.expect("WS error");
|
|
match msg {
|
|
Message::Text(text) => text.to_string(),
|
|
other => panic!("Expected Text frame, got {:?}", other),
|
|
}
|
|
}
|
|
|
|
// ============================================================================
|
|
// Tests
|
|
// ============================================================================
|
|
|
|
#[tokio::test]
|
|
async fn test_ws_ping_pong() {
|
|
let (addr, _state, _agent_rx) = start_test_server().await;
|
|
let mut ws = connect_ws(addr).await;
|
|
|
|
// Send ping
|
|
let ping = r#"{"type":"ping"}"#;
|
|
ws.send(Message::Text(ping.into())).await.unwrap();
|
|
|
|
// Expect pong
|
|
let text = recv_text(&mut ws).await;
|
|
let parsed: serde_json::Value = serde_json::from_str(&text).unwrap();
|
|
assert_eq!(parsed["type"], "pong");
|
|
|
|
ws.close(None).await.unwrap();
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn test_ws_message_reaches_agent() {
|
|
let (addr, _state, mut agent_rx) = start_test_server().await;
|
|
let mut ws = connect_ws(addr).await;
|
|
|
|
// Send a chat message
|
|
let msg = r#"{"type":"message","content":"hello from ws","thread_id":"t42"}"#;
|
|
ws.send(Message::Text(msg.into())).await.unwrap();
|
|
|
|
// Verify it arrives on the agent's msg_tx
|
|
let incoming = timeout(TIMEOUT, agent_rx.recv())
|
|
.await
|
|
.expect("Timed out waiting for agent message")
|
|
.expect("Agent channel closed");
|
|
|
|
assert_eq!(incoming.content, "hello from ws");
|
|
assert_eq!(incoming.thread_id.as_deref(), Some("t42"));
|
|
assert_eq!(incoming.channel, "gateway");
|
|
assert_eq!(incoming.user_id, "test-user");
|
|
|
|
ws.close(None).await.unwrap();
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn test_ws_broadcast_event_received() {
|
|
let (addr, state, _agent_rx) = start_test_server().await;
|
|
let mut ws = connect_ws(addr).await;
|
|
|
|
// Give the connection a moment to fully establish
|
|
tokio::time::sleep(Duration::from_millis(50)).await;
|
|
|
|
// Broadcast an SSE event (simulates agent sending a response)
|
|
state.sse.broadcast(SseEvent::Response {
|
|
content: "agent says hi".to_string(),
|
|
thread_id: "t1".to_string(),
|
|
});
|
|
|
|
// The WS client should receive it
|
|
let text = recv_text(&mut ws).await;
|
|
let parsed: serde_json::Value = serde_json::from_str(&text).unwrap();
|
|
assert_eq!(parsed["type"], "event");
|
|
assert_eq!(parsed["event_type"], "response");
|
|
assert_eq!(parsed["data"]["content"], "agent says hi");
|
|
|
|
ws.close(None).await.unwrap();
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn test_ws_thinking_event() {
|
|
let (addr, state, _agent_rx) = start_test_server().await;
|
|
let mut ws = connect_ws(addr).await;
|
|
tokio::time::sleep(Duration::from_millis(50)).await;
|
|
|
|
state.sse.broadcast(SseEvent::Thinking {
|
|
message: "analyzing...".to_string(),
|
|
thread_id: None,
|
|
});
|
|
|
|
let text = recv_text(&mut ws).await;
|
|
let parsed: serde_json::Value = serde_json::from_str(&text).unwrap();
|
|
assert_eq!(parsed["type"], "event");
|
|
assert_eq!(parsed["event_type"], "thinking");
|
|
assert_eq!(parsed["data"]["message"], "analyzing...");
|
|
|
|
ws.close(None).await.unwrap();
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn test_ws_connection_tracking() {
|
|
let (addr, state, _agent_rx) = start_test_server().await;
|
|
let tracker = state.ws_tracker.as_ref().unwrap();
|
|
|
|
assert_eq!(tracker.connection_count(), 0);
|
|
|
|
// Connect first client
|
|
let ws1 = connect_ws(addr).await;
|
|
tokio::time::sleep(Duration::from_millis(50)).await;
|
|
assert_eq!(tracker.connection_count(), 1);
|
|
|
|
// Connect second client
|
|
let ws2 = connect_ws(addr).await;
|
|
tokio::time::sleep(Duration::from_millis(50)).await;
|
|
assert_eq!(tracker.connection_count(), 2);
|
|
|
|
// Disconnect first
|
|
drop(ws1);
|
|
tokio::time::sleep(Duration::from_millis(100)).await;
|
|
assert_eq!(tracker.connection_count(), 1);
|
|
|
|
// Disconnect second
|
|
drop(ws2);
|
|
tokio::time::sleep(Duration::from_millis(100)).await;
|
|
assert_eq!(tracker.connection_count(), 0);
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn test_ws_invalid_message_returns_error() {
|
|
let (addr, _state, _agent_rx) = start_test_server().await;
|
|
let mut ws = connect_ws(addr).await;
|
|
|
|
// Send invalid JSON
|
|
ws.send(Message::Text("not json".into())).await.unwrap();
|
|
|
|
// Should get an error message back
|
|
let text = recv_text(&mut ws).await;
|
|
let parsed: serde_json::Value = serde_json::from_str(&text).unwrap();
|
|
assert_eq!(parsed["type"], "error");
|
|
assert!(
|
|
parsed["message"]
|
|
.as_str()
|
|
.unwrap()
|
|
.contains("Invalid message")
|
|
);
|
|
|
|
ws.close(None).await.unwrap();
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn test_ws_unknown_type_returns_error() {
|
|
let (addr, _state, _agent_rx) = start_test_server().await;
|
|
let mut ws = connect_ws(addr).await;
|
|
|
|
// Send valid JSON but unknown message type
|
|
ws.send(Message::Text(r#"{"type":"foobar"}"#.into()))
|
|
.await
|
|
.unwrap();
|
|
|
|
let text = recv_text(&mut ws).await;
|
|
let parsed: serde_json::Value = serde_json::from_str(&text).unwrap();
|
|
assert_eq!(parsed["type"], "error");
|
|
|
|
ws.close(None).await.unwrap();
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn test_gateway_status_endpoint() {
|
|
let (addr, _state, _agent_rx) = start_test_server().await;
|
|
|
|
// Connect a WS client
|
|
let _ws = connect_ws(addr).await;
|
|
tokio::time::sleep(Duration::from_millis(50)).await;
|
|
|
|
// Hit the status endpoint
|
|
let client = reqwest::Client::new();
|
|
let resp = client
|
|
.get(format!("http://{}/api/gateway/status", addr))
|
|
.header("Authorization", format!("Bearer {}", AUTH_TOKEN))
|
|
.send()
|
|
.await
|
|
.expect("Failed to fetch status");
|
|
|
|
assert_eq!(resp.status(), 200);
|
|
|
|
let body: serde_json::Value = resp.json().await.unwrap();
|
|
assert_eq!(body["ws_connections"], 1);
|
|
assert!(body["total_connections"].as_u64().unwrap() >= 1);
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn test_ws_no_auth_rejected() {
|
|
let (addr, _state, _agent_rx) = start_test_server().await;
|
|
|
|
// Try to connect without auth token
|
|
let url = format!("ws://{}/api/chat/ws", addr);
|
|
let request = url.into_client_request().unwrap();
|
|
let result = tokio_tungstenite::connect_async(request).await;
|
|
|
|
// Should fail (401 from auth middleware before WS upgrade)
|
|
assert!(result.is_err());
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn test_ws_multiple_events_in_sequence() {
|
|
let (addr, state, _agent_rx) = start_test_server().await;
|
|
let mut ws = connect_ws(addr).await;
|
|
tokio::time::sleep(Duration::from_millis(50)).await;
|
|
|
|
// Broadcast multiple events rapidly
|
|
state.sse.broadcast(SseEvent::Thinking {
|
|
message: "step 1".to_string(),
|
|
thread_id: None,
|
|
});
|
|
state.sse.broadcast(SseEvent::ToolStarted {
|
|
name: "shell".to_string(),
|
|
thread_id: None,
|
|
});
|
|
state.sse.broadcast(SseEvent::ToolCompleted {
|
|
name: "shell".to_string(),
|
|
success: true,
|
|
error: None,
|
|
parameters: None,
|
|
thread_id: None,
|
|
});
|
|
state.sse.broadcast(SseEvent::Response {
|
|
content: "done".to_string(),
|
|
thread_id: "t1".to_string(),
|
|
});
|
|
|
|
// Receive all 4 in order
|
|
let t1 = recv_text(&mut ws).await;
|
|
let t2 = recv_text(&mut ws).await;
|
|
let t3 = recv_text(&mut ws).await;
|
|
let t4 = recv_text(&mut ws).await;
|
|
|
|
let p1: serde_json::Value = serde_json::from_str(&t1).unwrap();
|
|
let p2: serde_json::Value = serde_json::from_str(&t2).unwrap();
|
|
let p3: serde_json::Value = serde_json::from_str(&t3).unwrap();
|
|
let p4: serde_json::Value = serde_json::from_str(&t4).unwrap();
|
|
|
|
assert_eq!(p1["event_type"], "thinking");
|
|
assert_eq!(p2["event_type"], "tool_started");
|
|
assert_eq!(p3["event_type"], "tool_completed");
|
|
assert_eq!(p4["event_type"], "response");
|
|
|
|
ws.close(None).await.unwrap();
|
|
}
|