Skip to content

Commit

Permalink
Merge pull request #189 from daithihearn/sonar-code-smells-2
Browse files Browse the repository at this point in the history
Sonar code smells 2
  • Loading branch information
daithihearn authored Oct 3, 2023
2 parents 9c0cae7 + 442a5ae commit e4ef806
Show file tree
Hide file tree
Showing 4 changed files with 246 additions and 50 deletions.
6 changes: 2 additions & 4 deletions .github/workflows/unit-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ on:
branches: ["main"]

jobs:
test:
unit-tests-and-coverage:
runs-on: ubuntu-latest

steps:
Expand All @@ -17,14 +17,12 @@ jobs:
uses: actions/setup-node@v2
with:
node-version: "16.19.x"

- name: Install dependencies
run: yarn install --frozen-lockfile

- name: Test and coverage
run: yarn jest --coverage
- name: SonarCloud Scan
uses: SonarSource/sonarcloud-github-action@master
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} # Needed to get PR information, if any
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
2 changes: 1 addition & 1 deletion src/caches/GameSlice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ export const getIsDoublesGame = createSelector(
players => players.length === 6,
)

export const getMaxCall = createSelector(getGame, game => game.maxCall || 0)
export const getMaxCall = createSelector(getGame, game => game.maxCall ?? 0)
export const getIsMyGo = createSelector(getGame, game => game.isMyGo)
export const getIamGoer = createSelector(getGame, game => game.iamGoer)
export const getIamDealer = createSelector(getGame, game => game.iamDealer)
Expand Down
207 changes: 184 additions & 23 deletions src/utils/GameUtils.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ import {
areAllTrumpCards,
bestCardLead,
calculateMinCardsToKeep,
canRenege,
compareCards,
containsATrumpCard,
getBestCard,
getColdCards,
getTrumpCards,
getWorstCard,
padMyHand,
Expand Down Expand Up @@ -48,14 +50,44 @@ const ROUND_BEST_LEAD: Round = {
dealerId: "",
status: RoundStatus.PLAYING,
dealerSeeingCall: false,
completedHands: [],
completedHands: [
{
leadOut: CardName.FIVE_DIAMONDS,
timestamp: "",
currentPlayerId: "",
playedCards: [{ card: CardName.ACE_HEARTS, playerId: "blah" }],
},
],
}

const ROUND_WILD_LEAD: Round = {
suit: Suit.HEARTS,
currentHand: {
leadOut: CardName.JOKER,
timestamp: "",
currentPlayerId: "",
playedCards: [],
},
timestamp: "",
number: 0,
dealerId: "",
status: RoundStatus.PLAYING,
dealerSeeingCall: false,
completedHands: [
{
leadOut: CardName.FIVE_DIAMONDS,
timestamp: "",
currentPlayerId: "",
playedCards: [{ card: CardName.ACE_HEARTS, playerId: "blah" }],
},
],
}

const HAND1: Card[] = [
{ ...CARDS.TWO_HEARTS, selected: true },
CARDS.THREE_HEARTS,
CARDS.FOUR_HEARTS,
CARDS.FIVE_HEARTS,
CARDS.JACK_HEARTS,
CARDS.SIX_HEARTS,
]

Expand All @@ -77,6 +109,13 @@ const HAND4: Card[] = [
CARDS.TWO_DIAMONDS,
]

const HAND_RENEG: Card[] = [
CARDS.THREE_DIAMONDS,
CARDS.TWO_CLUBS,
CARDS.THREE_CLUBS,
CARDS.FIVE_HEARTS,
]

describe("GameUtils", () => {
describe("removeEmptyCards", () => {
it("empty hand", () => {
Expand Down Expand Up @@ -140,6 +179,12 @@ describe("GameUtils", () => {
expect(compareCards([], [])).toBe(true)
})

it("different length hands", () => {
expect(compareCards([...HAND1], [...HAND1, CARDS.ACE_CLUBS])).toBe(
false,
)
})

it("equal hands", () => {
expect(compareCards([...HAND1], [...HAND1])).toBe(true)
})
Expand Down Expand Up @@ -339,7 +384,7 @@ describe("GameUtils", () => {
),
).toStrictEqual([
CARDS.THREE_HEARTS,
CARDS.FIVE_HEARTS,
CARDS.JACK_HEARTS,
CARDS.SIX_HEARTS,
])
})
Expand All @@ -352,7 +397,7 @@ describe("GameUtils", () => {
),
).toStrictEqual([
CARDS.THREE_HEARTS,
CARDS.FIVE_HEARTS,
CARDS.JACK_HEARTS,
CARDS.SIX_HEARTS,
])
})
Expand Down Expand Up @@ -428,6 +473,34 @@ describe("GameUtils", () => {
})
})

describe("getColdCards", () => {
it("empty hand", () => {
expect(getColdCards([], Suit.HEARTS)).toStrictEqual([])
})

it("all cold cards", () => {
expect(getColdCards([...HAND1], Suit.HEARTS)).toStrictEqual([])
})

it("no cold cards", () => {
expect(getColdCards([...HAND3], Suit.HEARTS)).toStrictEqual(HAND3)
})

it("all cold cards with joker and ace of hearts", () => {
expect(getColdCards([...HAND4], Suit.HEARTS)).toStrictEqual([
CARDS.THREE_CLUBS,
CARDS.TWO_DIAMONDS,
])
})

it("some cold cards", () => {
expect(getColdCards([...HAND3], Suit.SPADES)).toStrictEqual([
CARDS.THREE_DIAMONDS,
CARDS.TWO_CLUBS,
])
})
})

describe("bestCardLead", () => {
it("no suit", () => {
expect(bestCardLead({ ...ROUND, suit: undefined })).toBe(false)
Expand All @@ -446,11 +519,11 @@ describe("GameUtils", () => {

describe("getBestCard", () => {
it("empty hand", () => {
expect(getBestCard([], ROUND)).toBe(undefined)
expect(() => getBestCard([], ROUND)).toThrow()
})
it("trump card", () => {
expect(getBestCard([...HAND1], ROUND)).toStrictEqual(
CARDS.FIVE_HEARTS,
expect(getBestCard([...HAND1], ROUND_BEST_LEAD)).toStrictEqual(
CARDS.JACK_HEARTS,
)
})
it("follow cold card", () => {
Expand All @@ -460,21 +533,99 @@ describe("GameUtils", () => {
})
})

describe("getWorstCard", () => {
it("empty hand", () => {
expect(getBestCard([], ROUND)).toBe(undefined)
describe("canRenege", () => {
it("not a trump card", () => {
expect(() =>
canRenege(CARDS.TWO_CLUBS, CARDS.TWO_HEARTS, Suit.HEARTS),
).toThrow()
})
it("trump card", () => {
expect(getWorstCard([...HAND1], ROUND)).toStrictEqual({
...CARDS.TWO_HEARTS,
selected: true,
})
it("not a trump card", () => {
expect(() =>
canRenege(CARDS.TWO_HEARTS, CARDS.TWO_CLUBS, Suit.HEARTS),
).toThrow()
})
it("follow cold card", () => {
expect(getWorstCard([...HAND3], ROUND)).toStrictEqual(
CARDS.TWO_CLUBS,
it("not a renegable card", () => {
expect(
canRenege(CARDS.THREE_HEARTS, CARDS.TWO_HEARTS, Suit.HEARTS),
).toBe(false)
})
it("renegable card", () => {
expect(
canRenege(CARDS.ACE_HEARTS, CARDS.TWO_HEARTS, Suit.HEARTS),
).toBe(true)
})
it("renegable card", () => {
expect(canRenege(CARDS.JOKER, CARDS.TWO_HEARTS, Suit.HEARTS)).toBe(
true,
)
})
it("renegable card", () => {
expect(
canRenege(CARDS.JACK_HEARTS, CARDS.TWO_HEARTS, Suit.HEARTS),
).toBe(true)
})
it("renegable card", () => {
expect(
canRenege(CARDS.FIVE_HEARTS, CARDS.TWO_HEARTS, Suit.HEARTS),
).toBe(true)
})
it("renegable card lower than card lead out", () => {
expect(canRenege(CARDS.ACE_HEARTS, CARDS.JOKER, Suit.HEARTS)).toBe(
false,
)
})
it("renegable card lower than card lead out", () => {
expect(
canRenege(CARDS.ACE_HEARTS, CARDS.JACK_HEARTS, Suit.HEARTS),
).toBe(false)
})
it("renegable card lower than card lead out", () => {
expect(
canRenege(CARDS.ACE_HEARTS, CARDS.FIVE_HEARTS, Suit.HEARTS),
).toBe(false)
})
it("renegable card lower than card lead out", () => {
expect(canRenege(CARDS.JOKER, CARDS.JACK_HEARTS, Suit.HEARTS)).toBe(
false,
)
})
it("renegable card lower than card lead out", () => {
expect(canRenege(CARDS.JOKER, CARDS.FIVE_HEARTS, Suit.HEARTS)).toBe(
false,
)
})
it("renegable card lower than card lead out", () => {
expect(
canRenege(CARDS.JACK_HEARTS, CARDS.FIVE_HEARTS, Suit.HEARTS),
).toBe(false)
})
})

describe("getWorstCard", () => {
// it("empty hand", () => {
// expect(() => getBestCard([], ROUND)).toThrow()
// })
// it("trump card", () => {
// expect(getWorstCard([...HAND1], ROUND)).toStrictEqual({
// ...CARDS.TWO_HEARTS,
// selected: true,
// })
// })
// it("follow cold card", () => {
// expect(getWorstCard([...HAND3], ROUND)).toStrictEqual(
// CARDS.TWO_CLUBS,
// )
// })
// it("wild cards lead", () => {
// expect(getWorstCard([...HAND4], ROUND_WILD_LEAD)).toStrictEqual(
// CARDS.TWO_HEARTS,
// )
// })
it("Can reneg five", () => {
expect(
getWorstCard([...HAND_RENEG], ROUND_WILD_LEAD),
).toStrictEqual(CARDS.THREE_DIAMONDS)
})
})

describe("calculateMinCardsToKeep", () => {
Expand All @@ -495,10 +646,10 @@ describe("GameUtils", () => {
expect(calculateMinCardsToKeep(3)).toBe(0)
})
it("4 players", () => {
expect(calculateMinCardsToKeep(4)).toBe(1)
expect(calculateMinCardsToKeep(4)).toBe(0)
})
it("5 players", () => {
expect(calculateMinCardsToKeep(5)).toBe(2)
expect(calculateMinCardsToKeep(5)).toBe(1)
})
it("6 players", () => {
expect(calculateMinCardsToKeep(6)).toBe(2)
Expand All @@ -516,15 +667,25 @@ describe("GameUtils", () => {
})
it("Must keep 2", () => {
expect(pickBestCards([...HAND1], Suit.DIAMONDS, 6)).toStrictEqual([
CARDS.JACK_HEARTS,
CARDS.SIX_HEARTS,
CARDS.FIVE_HEARTS,
])
})
it("Must keep 1", () => {
expect(pickBestCards([...HAND1], Suit.DIAMONDS, 4)).toStrictEqual([
CARDS.SIX_HEARTS,
expect(pickBestCards([...HAND1], Suit.DIAMONDS, 5)).toStrictEqual([
CARDS.JACK_HEARTS,
])
})
it("Must keep 0", () => {
expect(pickBestCards([...HAND1], Suit.DIAMONDS, 4)).toStrictEqual(
[],
)
})
it("Must keep 0", () => {
expect(pickBestCards([...HAND1], Suit.DIAMONDS, 3)).toStrictEqual(
[],
)
})
it("Must keep 0", () => {
expect(pickBestCards([...HAND1], Suit.DIAMONDS, 2)).toStrictEqual(
[],
Expand Down
Loading

0 comments on commit e4ef806

Please sign in to comment.