Files
commander/backend/test/rooms.test.ts
2026-10-29 15:21:05 +01:00

56 lines
2.2 KiB
TypeScript

import assert from 'node:assert/strict'
import test from 'node:test'
import { findCardByName } from '../src/cards.js'
import { createMatch, getMatchView, joinMatch, leaveMatch, performAction, setDeck, startMatch } from '../src/game/rooms.js'
function deck(commanderName: string) {
const commander = findCardByName(commanderName)
const forest = findCardByName('Forest')
assert.ok(commander && forest)
return {
commander: commander.oracle_id,
cards: Array.from({ length: 99 }, () => forest.oracle_id),
}
}
test('lobby decks can be swapped and invalid decks are rejected', () => {
const first = deck("Atraxa, Praetors' Voice")
const second = deck('Kenrith, the Returned King')
const { matchId, token } = createMatch('Host', first, 2)
setDeck(matchId, token, second)
assert.equal(getMatchView(matchId, token).players[0].commander?.name, 'Kenrith, the Returned King')
assert.throws(() => setDeck(matchId, token, { ...second, cards: [] }), /100/)
assert.equal(getMatchView(matchId, token).players[0].commander?.name, 'Kenrith, the Returned King')
})
test('leaving a started game forfeits the seat instead of erroring', () => {
const host = createMatch('Host', deck("Atraxa, Praetors' Voice"), 2)
const guest = joinMatch(host.matchId, 'Guest', deck('Kenrith, the Returned King'))
startMatch(host.matchId, host.token)
leaveMatch(host.matchId, host.token)
const view = getMatchView(host.matchId, guest.token)
assert.equal(view.status, 'finished')
assert.equal(view.players.find(player => player.id === host.playerId)?.lost, true)
})
test('started matches wait for both players to keep their opening hands', () => {
const host = createMatch('Host', deck("Atraxa, Praetors' Voice"), 2)
const guest = joinMatch(host.matchId, 'Guest', deck('Kenrith, the Returned King'))
startMatch(host.matchId, host.token)
let view = getMatchView(host.matchId, host.token)
assert.equal(view.status, 'active')
assert.equal(view.game?.status, 'mulligan')
assert.equal(view.game?.mulligans?.length, 2)
performAction(host.matchId, host.token, { type: 'keep_hand' })
performAction(host.matchId, guest.token, { type: 'keep_hand' })
view = getMatchView(host.matchId, host.token)
assert.equal(view.game?.status, 'active')
})