diff --git a/pkg/game/game-service_test.go b/pkg/game/game-service_test.go index 3798de4..1b0b0d8 100644 --- a/pkg/game/game-service_test.go +++ b/pkg/game/game-service_test.go @@ -12,19 +12,17 @@ func TestCreate(t *testing.T) { ctx := context.Background() tests := []struct { - name string - inputPlayerIDs []string - inputName string - inputAdminID string - mockError *[]error - expectingError bool + name string + inputPlayerIDs []string + inputAdminID string + mockUpsertError *[]error + expectingError bool }{ { - name: "simple create", - inputPlayerIDs: []string{"1", "2"}, - inputName: "test", - inputAdminID: "1", - mockError: &[]error{nil}, + name: "simple create", + inputPlayerIDs: []string{"1", "2"}, + inputAdminID: "1", + mockUpsertError: &[]error{nil}, }, { name: "duplicate player IDs", @@ -32,9 +30,14 @@ func TestCreate(t *testing.T) { "1", "1", }, - inputName: "test", - inputAdminID: "1", - mockError: &[]error{nil}, + inputAdminID: "1", + mockUpsertError: &[]error{nil}, + expectingError: true, + }, + { + name: "admin not in game", + inputPlayerIDs: []string{"1", "2"}, + inputAdminID: "3", expectingError: true, }, { @@ -43,17 +46,16 @@ func TestCreate(t *testing.T) { "1", "2", }, - inputName: "test", - inputAdminID: "1", - mockError: &[]error{errors.New("failed to upsert")}, - expectingError: true, + inputAdminID: "1", + mockUpsertError: &[]error{errors.New("failed to upsert")}, + expectingError: true, }, } for _, test := range tests { t.Run(test.name, func(t *testing.T) { mockCol := &db.MockCollection[Game]{ - MockUpsertErr: test.mockError, + MockUpsertErr: test.mockUpsertError, } ds := &Service{ @@ -68,7 +70,7 @@ func TestCreate(t *testing.T) { } } else { if result.Name != test.name { - t.Errorf("expected name %s, got %s", test.inputName, result.Name) + t.Errorf("expected name %s, got %s", test.name, result.Name) } if result.AdminID != test.inputAdminID { t.Errorf("expected admin id %s, got %s", test.inputAdminID, result.AdminID) diff --git a/pkg/game/game-utils.go b/pkg/game/game-utils.go index 56e6ce5..63ccde5 100644 --- a/pkg/game/game-utils.go +++ b/pkg/game/game-utils.go @@ -129,6 +129,18 @@ func NewGame(playerIDs []string, name string, adminID string) (Game, error) { return Game{}, err } + // Verify the admin is in the list of players + adminFound := false + for _, playerID := range playerIDs { + if playerID == adminID { + adminFound = true + break + } + } + if !adminFound { + return Game{}, errors.New("admin not found in players") + } + // Randomise the order of the players shuffledPlayerIDs := shuffle(playerIDs) @@ -192,3 +204,11 @@ func containsAllUnique(referenceSlice, targetSlice []CardName) bool { } return true } + +// compare checks if targetSlice and referenceSlice are equivalent +func compare(referenceSlice, targetSlice []CardName) bool { + if len(referenceSlice) != len(targetSlice) { + return false + } + return containsAllUnique(referenceSlice, targetSlice) +} diff --git a/pkg/game/game_test.go b/pkg/game/game_test.go index e01c41f..3483474 100644 --- a/pkg/game/game_test.go +++ b/pkg/game/game_test.go @@ -443,7 +443,7 @@ func TestGame_SelectSuit(t *testing.T) { if err != nil { t.Errorf("expected no error, got %v", err) } - if !containsAllUnique(state.Cards, test.cards) { + if !compare(state.Cards, test.cards) { t.Errorf("expected player to have all of the selected cards %v, got %v", test.cards, state.Cards) } }