package syncsocket import ( "fmt" "testing" ) // TestSessionTimingMapsStayBounded checks that fresh client-chosen session IDs cannot grow the // per-session timing maps past maxSyncSessions. func TestSessionTimingMapsStayBounded(t *testing.T) { hub := NewHub() p := &peer{clientID: "client-1"} for i := 0; i < maxSyncSessions+100; i++ { hub.handleSyncUpdate(p, map[string]any{ "clientId": "client-1", "sessionId": fmt.Sprintf("session-%d", i), "paused": false, "playAt": "2026-10-19T10:00:00Z", "playAtLocalMs": float64(1), "capturedAtLocalMs": float64(1), }) } if n := len(hub.playAt); n > maxSyncSessions { t.Errorf("playAt holds %d sessions, want at most %d", n, maxSyncSessions) } if n := len(hub.playAtLocalMs); n > maxSyncSessions { t.Errorf("playAtLocalMs holds %d sessions, want at most %d", n, maxSyncSessions) } if n := len(hub.capturedAtMs); n > maxSyncSessions { t.Errorf("capturedAtMs holds %d sessions, want at most %d", n, maxSyncSessions) } } // TestHubRefusesPeersBeyondTheConnectionCap checks that the hub stops registering connections // once it holds maxSyncConnections peers. func TestHubRefusesPeersBeyondTheConnectionCap(t *testing.T) { hub := NewHub() for i := 0; i < maxSyncConnections; i++ { if !hub.add(&peer{}) { t.Fatalf("add refused peer %d, below the cap", i) } } if hub.add(&peer{}) { t.Fatal("add accepted a peer beyond maxSyncConnections") } }