diff options
| author | Jeff Halter <868228+jhalter@users.noreply.github.com> | 2026-03-16 11:46:04 -0700 |
|---|---|---|
| committer | Jeff Halter <868228+jhalter@users.noreply.github.com> | 2026-03-16 11:46:04 -0700 |
| commit | 2f46f87177234070044b5304ca3e0db471699ad8 (patch) | |
| tree | 6dc72ce596e95850a587001a85b3f45e394b0e34 /hotline/server_test.go | |
| parent | 92f4b7af1073b6cdd0786ce2090d6e446e0903ad (diff) | |
Improve test coverage for hotline and internal/mobius packages
Add comprehensive test cases across both packages to increase coverage:
- hotline: 52.4% → ~55% (Disconnect, handleTransaction, SendAll,
sendBanMessage, MemClientMgr, and other tests)
- internal/mobius: 75.9% → ~80% (HandleUpdateUser, HandleDeleteUser,
HandleSetUser, HandleNewUser, HandleUserBroadcast success paths)
Diffstat (limited to 'hotline/server_test.go')
| -rw-r--r-- | hotline/server_test.go | 110 |
1 files changed, 110 insertions, 0 deletions
diff --git a/hotline/server_test.go b/hotline/server_test.go index abc2f9a..4bb3669 100644 --- a/hotline/server_test.go +++ b/hotline/server_test.go @@ -13,6 +13,7 @@ import ( "time" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/mock" "golang.org/x/text/encoding" "golang.org/x/text/encoding/charmap" ) @@ -749,6 +750,107 @@ func TestServer_registerWithTrackers_EdgeCases(t *testing.T) { } } +func TestServer_CurrentStats(t *testing.T) { + stats := NewStats() + stats.Increment(StatCurrentlyConnected) + stats.Increment(StatDownloadCounter) + stats.Increment(StatDownloadCounter) + + srv := &Server{Stats: stats} + result := srv.CurrentStats() + + assert.Equal(t, 1, result["CurrentlyConnected"]) + assert.Equal(t, 2, result["DownloadCounter"]) + assert.Equal(t, 0, result["UploadsInProgress"]) +} + +func TestServer_sendTransaction(t *testing.T) { + t.Run("sends transaction to client connection", func(t *testing.T) { + wBuf := &bytes.Buffer{} + mockMgr := &MockClientMgr{} + mockMgr.On("Get", ClientID{0, 1}).Return(&ClientConn{ + Connection: &nopCloserRWC{Buffer: wBuf}, + }) + + srv := &Server{ClientMgr: mockMgr} + + tran := NewTransaction(TranChatMsg, ClientID{0, 1}, NewField(FieldData, []byte("hello"))) + err := srv.sendTransaction(tran) + + assert.NoError(t, err) + assert.Greater(t, wBuf.Len(), 0) + mockMgr.AssertExpectations(t) + }) + + t.Run("returns nil when client not found", func(t *testing.T) { + mockMgr := &MockClientMgr{} + mockMgr.On("Get", ClientID{0, 99}).Return((*ClientConn)(nil)) + + srv := &Server{ClientMgr: mockMgr} + + tran := NewTransaction(TranChatMsg, ClientID{0, 99}) + err := srv.sendTransaction(tran) + + assert.NoError(t, err) + mockMgr.AssertExpectations(t) + }) +} + +func TestServer_SendAll(t *testing.T) { + mockMgr := &MockClientMgr{} + mockMgr.On("List").Return([]*ClientConn{ + {ID: ClientID{0, 1}}, + {ID: ClientID{0, 2}}, + {ID: ClientID{0, 3}}, + }) + + outbox := make(chan Transaction, 10) + srv := &Server{ + ClientMgr: mockMgr, + outbox: outbox, + } + + srv.SendAll(TranChatMsg, NewField(FieldData, []byte("broadcast"))) + + assert.Len(t, outbox, 3) + + // Verify each transaction targets a different client + clientIDs := make(map[ClientID]bool) + for range 3 { + tran := <-outbox + clientIDs[tran.ClientID] = true + assert.Equal(t, TranChatMsg, tran.Type) + } + assert.True(t, clientIDs[ClientID{0, 1}]) + assert.True(t, clientIDs[ClientID{0, 2}]) + assert.True(t, clientIDs[ClientID{0, 3}]) + + mockMgr.AssertExpectations(t) +} + +type nopCloserRWC struct { + *bytes.Buffer +} + +func (n *nopCloserRWC) Close() error { return nil } + +func TestServer_NewClientConn(t *testing.T) { + mockMgr := &MockClientMgr{} + mockMgr.On("Add", mock.AnythingOfType("*hotline.ClientConn")).Return() + + srv := &Server{ClientMgr: mockMgr} + + rwc := &nopCloserRWC{Buffer: &bytes.Buffer{}} + + cc := srv.NewClientConn(rwc, "192.168.1.1:12345") + + assert.NotNil(t, cc) + assert.Equal(t, "192.168.1.1:12345", cc.RemoteAddr) + assert.Equal(t, []byte{0, 0}, cc.Icon) + assert.Equal(t, srv, cc.Server) + mockMgr.AssertExpectations(t) +} + func TestServer_registerWithAllTrackers(t *testing.T) { tests := []struct { name string @@ -819,3 +921,11 @@ func TestServer_registerWithAllTrackers(t *testing.T) { }) } } + +func TestSendBanMessage(t *testing.T) { + buf := &bytes.Buffer{} + sendBanMessage(buf, "You are banned") + + assert.Greater(t, buf.Len(), 0) + assert.Contains(t, buf.String(), "You are banned") +} |