Skip to content
32 changes: 23 additions & 9 deletions src/apps/castle/__tests__/rules.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { describe, it, expect } from 'vitest'
import { cardsOf, describeEnding, describeLastPlay, describePile, headlineOf, rowInPlay, standingOf, toggleSelection } from '../rules'
import { cardsOf, describeEnding, describeLastPlay, describePile, enteredSince, headlineOf, rowInPlay, standingOf, toggleSelection } from '../rules'
import type { CastlePlayer, CastleView } from '../wire'

const seat = (over: Partial<CastlePlayer> = {}): CastlePlayer => ({
Expand Down Expand Up @@ -82,13 +82,11 @@ describe('describeLastPlay', () => {
})

describe('describePile', () => {
it('names the price the next play must match', () => {
expect(describePile(view())).toBe('Empty pile: anything goes')
expect(describePile(view({ run: [{ rank: '8', suit: '♥' }], pileCount: 3 }))).toBe(
'8♥ on top: play one or more of 8 or higher'
)
it('names the price the next play must match, and nothing the table already shows', () => {
expect(describePile(view())).toBe('Anything goes')
expect(describePile(view({ run: [{ rank: '8', suit: '♥' }], pileCount: 3 }))).toBe('Play 8 or higher')
expect(describePile(view({ run: [{ rank: '8', suit: '♠' }, { rank: '8', suit: '♥' }], pileCount: 3 }))).toBe(
'two 8s on top: play two or more of 8 or higher'
'Play two or more, 8 or higher'
)
// A queen on a queen: two show, but one king answers, since the
// count to match is the last play's.
Expand All @@ -100,11 +98,11 @@ describe('describePile', () => {
lastPlay: { playerId: 'bob', cards: [{ rank: 'Q', suit: '♥' }], burned: false, pickedUp: false }
})
)
).toBe('two Qs on top: play one or more of Q or higher')
).toBe('Play Q or higher')
// Three 3s must not read as arithmetic.
expect(
describePile(view({ run: [{ rank: '3', suit: '♠' }, { rank: '3', suit: '♦' }, { rank: '3', suit: '♥' }], pileCount: 3 }))
).toBe('three 3s on top: play three or more of 3 or higher')
).toBe('Play three or more, 3 or higher')
})
})

Expand Down Expand Up @@ -133,3 +131,19 @@ describe('standingOf and headlineOf', () => {
expect(headlineOf({ finished: [] }, 'alice')).toBe('The table broke up')
})
})

describe('enteredSince', () => {
it('names the cards that were not there, counting duplicates as a multiset', () => {
const hand = [
{ rank: 'K', suit: '♣' },
{ rank: 'K', suit: '♣' },
{ rank: '3', suit: '♦' }
]
// One K♣ was there: the second is new, the first is not.
expect(enteredSince(['K♣'], hand)).toEqual([1, 2])
expect(enteredSince(['K♣', 'K♣', '3♦'], hand)).toEqual([])
expect(enteredSince([], hand)).toEqual([0, 1, 2])
// Order changed, nothing arrived.
expect(enteredSince(['3♦', 'K♣', 'K♣'], hand)).toEqual([])
})
})
Loading
Loading