Skip to content

Commit

Permalink
add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
stefanseifert committed Oct 19, 2024
1 parent 6654330 commit ab267e6
Show file tree
Hide file tree
Showing 6 changed files with 167 additions and 13 deletions.
17 changes: 13 additions & 4 deletions src/util/NavigationState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,23 @@ export default class NavigationState {
const previousBotPersistence = getPreviousBotPersistence(state, this.round, this.turn)
this.cardDeck = CardDeck.fromPersistence(previousBotPersistence.cardDeck)
// draw next card
this.cardDeck.draw()
if (this.player == Player.BOT) {
this.cardDeck.draw()
}
// counters
this.evolutionCount = previousBotPersistence.evolutionCount
this.prosperityCount = previousBotPersistence.prosperityCount
// roll dice for action selection
this.actionRoll = rollDice(6)
this.territoryRoll = rollDice(6)
this.beaconRoll = rollDice(6)
if (this.player == Player.BOT) {
this.actionRoll = rollDice(6)
this.territoryRoll = rollDice(6)
this.beaconRoll = rollDice(6)
}
else {
this.actionRoll = 0
this.territoryRoll = 0
this.beaconRoll = 0
}
}
this.currentCard = this.cardDeck.currentCard ?? Cards.get(1)
}
Expand Down
4 changes: 2 additions & 2 deletions tests/unit/helper/mockCardDeck.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ export default function (params?: MockCardDeckParams) : CardDeck {
}

export interface MockCardDeckParams {
pile?: string[]
discard?: string[]
pile?: number[]
discard?: number[]
}
5 changes: 4 additions & 1 deletion tests/unit/helper/mockRound.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
import Player from '@/services/enum/Player'
import { Round, Turn } from '@/store/state'

export default function (params?: MockRoundParams) : Round {
const round : Round = {
round: params?.round ?? 1,
startPlayer: params?.startPlayer ?? Player.PLAYER,
turns: params?.turns ?? []
}
return round
}

export interface MockRoundParams {
round? : number
round? : number,
startPlayer?: Player,
turns? : Turn[]
}
13 changes: 10 additions & 3 deletions tests/unit/helper/mockState.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,25 @@
import DifficultyLevel from '@/services/enum/DifficultyLevel'
import { Round, State } from '@/store/state'
import ScoringCategory from '@/services/enum/ScoringCategory'
import { CardDeckPersistence, Round, State } from '@/store/state'

export default function (params?: MockStateParams) : State {
return {
language: 'en',
baseFontSize: 1,
setup: {
difficultyLevel: params?.difficultyLevel ?? DifficultyLevel.BEGINNER
difficultyLevel: params?.difficultyLevel ?? DifficultyLevel.BEGINNER,
eraScoringTiles: params?.eraScoringTiles ?? [],
finalScoringTiles: params?.finalScoringTiles ?? [],
initialCardDeck: params?.initialCardDeck
},
rounds: params?.rounds ?? []
}
}

export interface MockStateParams {
difficultyLevel?: DifficultyLevel
difficultyLevel?: DifficultyLevel,
eraScoringTiles?: ScoringCategory[],
finalScoringTiles?: ScoringCategory[],
initialCardDeck?: CardDeckPersistence,
rounds?: Round[]
}
27 changes: 24 additions & 3 deletions tests/unit/helper/mockTurn.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,34 @@
import { Turn } from '@/store/state'
import CardDeck from '@/services/CardDeck'
import Player from '@/services/enum/Player'
import { CardDeckPersistence, Turn } from '@/store/state'

export default function (params?: MockTurnParams) : Turn {
return {
const turn : Turn = {
round: params?.round ?? 1,
turn: params?.turn ?? 1
turn: params?.turn ?? 1,
player: params?.player ?? Player.PLAYER
}
if (params?.cardDeck || params?.evolutionCount || params?.prosperityCount || params?.actionRoll || params?.territoryRoll || params?.beaconRoll) {
turn.botPersistence = {
cardDeck: params?.cardDeck ?? CardDeck.new().toPersistence(),
evolutionCount: params?.evolutionCount ?? 0,
prosperityCount: params?.prosperityCount ?? 0,
actionRoll: params?.actionRoll ?? 0,
territoryRoll: params?.territoryRoll ?? 0,
beaconRoll: params?.beaconRoll ?? 0
}
}
return turn
}

export interface MockTurnParams {
round? : number
turn? : number
player? : Player
cardDeck?: CardDeckPersistence
evolutionCount?: number
prosperityCount?: number
actionRoll?: number
territoryRoll?: number
beaconRoll?: number
}
114 changes: 114 additions & 0 deletions tests/unit/util/NavigationState.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
import NavigationState from '@/util/NavigationState'
import { expect } from 'chai'
import mockRouteLocation from '../helper/mockRouteLocation'
import mockState from '../helper/mockState'
import Player from '@/services/enum/Player'
import mockRound from '../helper/mockRound'
import mockTurn from '../helper/mockTurn'

const stateBotData = mockState({initialCardDeck:{pile:[1,2,3,4],discard:[]}, rounds:[
mockRound({round:1, turns:[
mockTurn({round:1,turn:1,player:Player.PLAYER}),
mockTurn({round:1,turn:2,player:Player.BOT,cardDeck:{pile:[2,3,4],discard:[1]}}),
mockTurn({round:1,turn:3,player:Player.PLAYER}),
mockTurn({round:1,turn:4,player:Player.BOT,cardDeck:{pile:[3,4],discard:[2,1]},
evolutionCount:2, prosperityCount:1,
actionRoll:3, territoryRoll:4, beaconRoll:5})
]})
]})

describe('util/NavigationState', () => {
it('roundStart', () => {
const route = mockRouteLocation({name:'RoundStart', params:{round:'1'}})
const state = mockState()
const navigationState = new NavigationState(route, state)

expect(navigationState.player).to.eq(Player.PLAYER)
expect(navigationState.round).to.eq(1)
expect(navigationState.turn).to.eq(0)
})

it('turnPlayer', () => {
const route = mockRouteLocation({name:'TurnPlayer', params:{round:'1',turn:'3'}})
const state = mockState()
const navigationState = new NavigationState(route, state)

expect(navigationState.player).to.eq(Player.PLAYER)
expect(navigationState.round).to.eq(1)
expect(navigationState.turn).to.eq(3)
})

it('roundEnd', () => {
const route = mockRouteLocation({name:'RoundEnd', params:{round:'1'}})
const state = mockState()
const navigationState = new NavigationState(route, state)

expect(navigationState.player).to.eq(Player.PLAYER)
expect(navigationState.round).to.eq(1)
expect(navigationState.turn).to.eq(999)
})

it('startPlayer', () => {
const route = mockRouteLocation({name:'TurnBot', params:{round:'1',turn:'3'}})
const state = mockState({rounds:[
mockRound({round:1,startPlayer:Player.BOT})
]})
const navigationState = new NavigationState(route, state)

expect(navigationState.player).to.eq(Player.BOT)
expect(navigationState.round).to.eq(1)
expect(navigationState.turn).to.eq(3)
})

it('turnBot-currentTurn', () => {
const route = mockRouteLocation({name:'TurnBot', params:{round:'1',turn:'4'}})
const navigationState = new NavigationState(route, stateBotData)

expect(navigationState.player).to.eq(Player.BOT)
expect(navigationState.cardDeck.toPersistence()).to.eql({pile:[3,4],discard:[2,1]})
expect(navigationState.evolutionCount).to.eq(2)
expect(navigationState.prosperityCount).to.eq(1)
expect(navigationState.actionRoll).to.eq(3)
expect(navigationState.territoryRoll).to.eq(4)
expect(navigationState.beaconRoll).to.eq(5)
})

it('turnBot-lastTurn', () => {
const route = mockRouteLocation({name:'TurnBot', params:{round:'1',turn:'6'}})
const navigationState = new NavigationState(route, stateBotData)

expect(navigationState.player).to.eq(Player.BOT)
expect(navigationState.cardDeck.toPersistence()).to.eql({pile:[4],discard:[3,2,1]})
expect(navigationState.evolutionCount).to.eq(2)
expect(navigationState.prosperityCount).to.eq(1)
expect(navigationState.actionRoll).to.greaterThanOrEqual(1)
expect(navigationState.territoryRoll).to.greaterThanOrEqual(1)
expect(navigationState.beaconRoll).to.greaterThanOrEqual(1)
})

it('turnBot-lastRoundLastTurn', () => {
const route = mockRouteLocation({name:'TurnBot', params:{round:'2',turn:'1'}})
const navigationState = new NavigationState(route, stateBotData)

expect(navigationState.player).to.eq(Player.BOT)
expect(navigationState.cardDeck.toPersistence()).to.eql({pile:[4],discard:[3,2,1]})
expect(navigationState.evolutionCount).to.eq(2)
expect(navigationState.prosperityCount).to.eq(1)
expect(navigationState.actionRoll).to.greaterThanOrEqual(1)
expect(navigationState.territoryRoll).to.greaterThanOrEqual(1)
expect(navigationState.beaconRoll).to.greaterThanOrEqual(1)
})

it('turnBot-initialCardDeck', () => {
const route = mockRouteLocation({name:'TurnPlayer', params:{round:'1',turn:'1'}})
const navigationState = new NavigationState(route, stateBotData)

expect(navigationState.player).to.eq(Player.PLAYER)
expect(navigationState.cardDeck.toPersistence()).to.eql({pile:[1,2,3,4],discard:[]})
expect(navigationState.evolutionCount).to.eq(0)
expect(navigationState.prosperityCount).to.eq(0)
expect(navigationState.actionRoll).to.eq(0)
expect(navigationState.territoryRoll).to.eq(0)
expect(navigationState.beaconRoll).to.eq(0)
})
})

0 comments on commit ab267e6

Please sign in to comment.