Pulse/internal/websocket/hub_more_test.go
rcourtman a6a8efaa65 test: Add comprehensive test coverage across packages
New test files with expanded coverage:

API tests:
- ai_handler_test.go: AI handler unit tests with mocking
- agent_profiles_tools_test.go: Profile management tests
- alerts_endpoints_test.go: Alert API endpoint tests
- alerts_test.go: Updated for interface changes
- audit_handlers_test.go: Audit handler tests
- frontend_embed_test.go: Frontend embedding tests
- metadata_handlers_test.go, metadata_provider_test.go: Metadata tests
- notifications_test.go: Updated for interface changes
- profile_suggestions_test.go: Profile suggestion tests
- saml_service_test.go: SAML authentication tests
- sensor_proxy_gate_test.go: Sensor proxy tests
- updates_test.go: Updated for interface changes

Agent tests:
- dockeragent/signature_test.go: Docker agent signature tests
- hostagent/agent_metrics_test.go: Host agent metrics tests
- hostagent/commands_test.go: Command execution tests
- hostagent/network_helpers_test.go: Network helper tests
- hostagent/proxmox_setup_test.go: Updated setup tests
- kubernetesagent/*_test.go: Kubernetes agent tests

Core package tests:
- monitoring/kubernetes_agents_test.go, reload_test.go
- remoteconfig/client_test.go, signature_test.go
- sensors/collector_test.go
- updates/adapter_installsh_*_test.go: Install adapter tests
- updates/manager_*_test.go: Update manager tests
- websocket/hub_*_test.go: WebSocket hub tests

Library tests:
- pkg/audit/export_test.go: Audit export tests
- pkg/metrics/store_test.go: Metrics store tests
- pkg/proxmox/*_test.go: Proxmox client tests
- pkg/reporting/reporting_test.go: Reporting tests
- pkg/server/*_test.go: Server tests
- pkg/tlsutil/extra_test.go: TLS utility tests

Total: ~8000 lines of new test code
2026-01-19 19:26:18 +00:00

127 lines
2.6 KiB
Go

package websocket
import (
"encoding/json"
"testing"
"time"
)
func TestDispatchToClientsDropsFullClient(t *testing.T) {
hub := NewHub(nil)
client := &Client{
hub: hub,
send: make(chan []byte, 1),
id: "client-1",
}
hub.mu.Lock()
hub.clients[client] = true
hub.mu.Unlock()
client.send <- []byte("filled")
hub.dispatchToClients([]byte("payload"), "drop")
if hub.GetClientCount() != 0 {
t.Fatalf("expected client to be dropped")
}
<-client.send
if _, ok := <-client.send; ok {
t.Fatal("expected send channel to be closed")
}
}
func TestRunBroadcastSequencerImmediate(t *testing.T) {
hub := NewHub(nil)
client := &Client{
hub: hub,
send: make(chan []byte, 1),
id: "client-1",
}
hub.mu.Lock()
hub.clients[client] = true
hub.mu.Unlock()
done := make(chan struct{})
go func() {
hub.runBroadcastSequencer()
close(done)
}()
hub.broadcastSeq <- Message{
Type: "alert",
Data: map[string]string{"id": "a1"},
}
select {
case data := <-client.send:
var msg Message
if err := json.Unmarshal(data, &msg); err != nil {
t.Fatalf("unmarshal message: %v", err)
}
if msg.Type != "alert" {
t.Fatalf("unexpected message type: %s", msg.Type)
}
case <-time.After(200 * time.Millisecond):
t.Fatal("expected broadcast message")
}
close(hub.stopChan)
select {
case <-done:
case <-time.After(200 * time.Millisecond):
t.Fatal("broadcast sequencer did not exit")
}
}
func TestRunBroadcastSequencerCoalescesRawData(t *testing.T) {
hub := NewHub(nil)
hub.coalesceWindow = 5 * time.Millisecond
client := &Client{
hub: hub,
send: make(chan []byte, 1),
id: "client-1",
}
hub.mu.Lock()
hub.clients[client] = true
hub.mu.Unlock()
done := make(chan struct{})
go func() {
hub.runBroadcastSequencer()
close(done)
}()
hub.broadcastSeq <- Message{Type: "rawData", Data: map[string]string{"value": "first"}}
hub.broadcastSeq <- Message{Type: "rawData", Data: map[string]string{"value": "second"}}
select {
case data := <-client.send:
var msg Message
if err := json.Unmarshal(data, &msg); err != nil {
t.Fatalf("unmarshal message: %v", err)
}
payload := msg.Data.(map[string]interface{})
if payload["value"] != "second" {
t.Fatalf("expected coalesced value 'second', got %v", payload["value"])
}
case <-time.After(200 * time.Millisecond):
t.Fatal("expected coalesced message")
}
select {
case <-client.send:
t.Fatal("expected only one coalesced message")
default:
}
close(hub.stopChan)
select {
case <-done:
case <-time.After(200 * time.Millisecond):
t.Fatal("broadcast sequencer did not exit")
}
}