-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(server): implemented ai generated tests to learn upon
- Loading branch information
1 parent
c1c55fb
commit 6f5fb4d
Showing
12 changed files
with
708 additions
and
194 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package main | ||
|
||
import ( | ||
"net/http/httptest" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestMain_RootRoute(t *testing.T) { | ||
router := setupRouter() | ||
w := httptest.NewRecorder() | ||
req := httptest.NewRequest("GET", "/", nil) | ||
router.ServeHTTP(w, req) | ||
|
||
assert.Equal(t, 200, w.Code) | ||
assert.Contains(t, w.Body.String(), "Welcome") | ||
} | ||
|
||
func TestMain_GroupRoutes(t *testing.T) { | ||
router := setupRouter() | ||
|
||
tests := []struct { | ||
method string | ||
endpoint string | ||
expected int | ||
}{ | ||
{"POST", "/api/user", 400}, // Assuming it requires a body and proper setup | ||
{"GET", "/api/user", 302}, | ||
{"POST", "/api/game", 400}, // Assuming it requires a body and proper setup | ||
{"GET", "/api/game", 302}, | ||
} | ||
|
||
for _, tt := range tests { | ||
w := httptest.NewRecorder() | ||
req := httptest.NewRequest(tt.method, tt.endpoint, nil) | ||
router.ServeHTTP(w, req) | ||
|
||
assert.Equal(t, tt.expected, w.Code) | ||
} | ||
} | ||
|
||
func TestMain_WebSocketRoutes(t *testing.T) { | ||
router := setupRouter() | ||
|
||
tests := []struct { | ||
method string | ||
endpoint string | ||
expected int | ||
}{ | ||
{"GET", "/ws/dart/123", 400}, // Assuming it requires a proper WebSocket setup | ||
{"GET", "/ws/dart", 400}, // Assuming it requires a proper WebSocket setup | ||
} | ||
|
||
for _, tt := range tests { | ||
w := httptest.NewRecorder() | ||
req := httptest.NewRequest(tt.method, tt.endpoint, nil) | ||
router.ServeHTTP(w, req) | ||
|
||
assert.Equal(t, tt.expected, w.Code) | ||
} | ||
} |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package api | ||
|
||
import ( | ||
"github.com/google/uuid" | ||
"net/http/httptest" | ||
"server/internal/triffgonix/database" | ||
"server/internal/triffgonix/models" | ||
"testing" | ||
|
||
"github.com/gin-gonic/gin" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestCreatePlayer(t *testing.T) { | ||
router := setupRouter() | ||
w := httptest.NewRecorder() | ||
req := httptest.NewRequest("POST", "/api/user", nil) | ||
router.ServeHTTP(w, req) | ||
|
||
assert.Equal(t, 400, w.Code) | ||
} | ||
|
||
func setupRouter() *gin.Engine { | ||
gin.SetMode(gin.TestMode) | ||
r := gin.Default() | ||
group := r.Group("/api") | ||
{ | ||
group.POST("/user", CreatePlayer) | ||
group.GET("/user", GetPlayers) | ||
group.POST("/game", CreateGame) | ||
group.GET("/game", GetGames) | ||
} | ||
return r | ||
} | ||
|
||
func TestDatabaseConnections(t *testing.T) { | ||
database.AutoMigrate() | ||
err, _ := database.CreatePlayer(&models.Player{PlayerName: uuid.Must(uuid.NewRandom()).String()}) | ||
assert.Nil(t, err) | ||
games := database.FindAllGames() | ||
assert.NotNil(t, games) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package socket | ||
|
||
import ( | ||
"server/pkg/logging" | ||
"testing" | ||
) | ||
|
||
// Mock WebSocket connection | ||
type MockConn struct { | ||
WriteJSONFunc func(v interface{}) error | ||
ReadJSONFunc func(v interface{}) error | ||
CloseFunc func() error | ||
} | ||
|
||
func (m *MockConn) WriteJSON(v interface{}) error { | ||
return m.WriteJSONFunc(v) | ||
} | ||
|
||
func (m *MockConn) ReadJSON(v interface{}) error { | ||
return m.ReadJSONFunc(v) | ||
} | ||
|
||
func (m *MockConn) Close() error { | ||
return m.CloseFunc() | ||
} | ||
|
||
func TestRegisterNewClient(t *testing.T) { | ||
hub := &Hub{Clients: make(map[*Client]bool)} | ||
mockConn := &MockConn{} | ||
logger = logging.NewLogger() // Ensure logger is initialized | ||
|
||
hub.RegisterNewClient(mockConn) | ||
|
||
if len(hub.Clients) != 1 { | ||
t.Errorf("expected 1 client in hub, got: %d", len(hub.Clients)) | ||
} | ||
} | ||
|
||
func TestBroadcastMessage(t *testing.T) { | ||
hub := &Hub{Clients: make(map[*Client]bool)} | ||
mockConn := &MockConn{ | ||
WriteJSONFunc: func(v interface{}) error { | ||
return nil | ||
}, | ||
} | ||
client := &Client{Connection: mockConn} | ||
hub.Clients[client] = true | ||
logger = logging.NewLogger() // Ensure logger is initialized | ||
|
||
message := OutgoingMessage{Type: GameState, Content: "test content"} | ||
hub.broadcastMessage(message) | ||
// No assertion needed. If WriteJSON panics, the test will fail | ||
} | ||
|
||
func TestCleanupClient(t *testing.T) { | ||
hub := &Hub{Clients: make(map[*Client]bool)} | ||
mockConn := &MockConn{} | ||
client := &Client{Connection: mockConn} | ||
hub.Clients[client] = true | ||
logger = logging.NewLogger() // Ensure logger is initialized | ||
|
||
if len(hub.Clients) != 1 { | ||
t.Errorf("expected 1 client, got: %d", len(hub.Clients)) | ||
} | ||
|
||
hub.cleanupClient(mockConn) | ||
|
||
if len(hub.Clients) != 0 { | ||
t.Errorf("expected 0 clients after cleanup, got: %d", len(hub.Clients)) | ||
} | ||
} |
Oops, something went wrong.