diff options
| author | Joel Klinghed <the_jk@spawned.biz> | 2026-11-16 22:19:39 +0100 |
|---|---|---|
| committer | Joel Klinghed <the_jk@spawned.biz> | 2026-11-16 22:19:39 +0100 |
| commit | 94e2850fb73005f3fb30af8489905e5cdfb6fac5 (patch) | |
| tree | 1aea8e7e6683af770f0c57c0fb654003778c9f16 /web/test/deckHelpers.test.ts | |
| parent | 4f50f2008f4d7c2a635f7f26861fcfc7f7504423 (diff) | |
Phase 4: cards and decks (M4)
Full card play in the browser: ability-line rendering, monster ability
decks, the full modifier-deck (AMD) suite, and the loot deck — all
server commands, faithful to the reference flows and layout.
Web (Svelte 5):
- Line renderer + in-game translations: $lib/game/line/ renders the
reference line_builder output (emphasis, range markers, element
styling); gameI18n.ts loads the /i18n/ in-game ability-text tables
(the client-side TranslationService equivalent)
- Card components: stat cards, monster ability cards (OG + FH styles,
rendered lines, element/flying tokens), modifier cards (gfx resolver
incl. P<n> perk graphics), loot cards (party-size value rules,
owner, enhancements)
- Monster ability decks: AbilityCardSlot in each monster row (front of
the current card while active in playTurns, else the back with the
draw-pile count); AbilityCardsMenu (reveal top cards, draw extra,
extra shuffle, activate/inactivate, drag-reorder, remove); card zoom
- Modifier decks: ModifierDeckWidget (tap to draw, face-up revealed top
card, empty-box class icon for the current character's own deck,
discard stack); ModifierDeckHeader (bad omen, corrosive spew,
±1/−2/+0, imbue 1/2, reveal with the Cassandra 8-button cap,
save/don't-save revealed cards, removed count, Hail perk, CS
sanctuary/party cards, faction icons, bless/curse and the
empower/enfeeble counters); ModifierDeckMenu (draw-pile
drag-reorder + send-to-bottom/shuffle/remove, discard
remove/return-top, removed pile with return, faction grid with
gray-out, perks menu with the Frosthaven opt-in); card zoom
- Loot deck: LootDeckWidget (player-gated draw, current-character
icon, discard stack, hidden when both piles empty); LootCardsMenu
(set owner, per-character loot totals, add card, enhance with the
reference material/herb/coin splits, special cards 1418/1419,
return top/bottom)
- Bottom DeckArea: loot deck left; the Character Decks toggle with the
reference open-state cycling (oneOpen/allOpen/noOpen per round
state; one-deck view for the current character during playTurns);
ally deck behind shouldShowAlliesDeck; global deck
- Pure helper modules ported from the reference: rules.ts (FH card
styling, inactive-for-rule, ally-deck gate, deck resolution),
modifierDeck.ts (pile derivations, faction cards), perks.ts (active
perk set, card-text tokens, descriptions, add/remove gating),
cards/lootCard.ts (values + per-character totals)
- App wiring: campaign data loaded on currentCampaign change (guarded
against out-of-order fetches); ability/AMD/loot menus and zooms
driven by live-derived state so open menus track pile changes;
Escape closes every dialog
Server:
- /i18n/ static mount: the in-game ability-text translation files the
card renderer resolves (the reference TranslationService data)
- Loot deck JSON gains pools (pool card ids per pool — the client
derives card gfx from pool key + index and reads the enhancements
map; ids only, since pool cards are the same live instances that
mutate in the piles)
- Modifier deck JSON gains removables (the counter totals the header
reads) plus load-time round-trip for saves written before the field
existed
Docs:
- PLAN.md: Phase 3 and Phase 4 marked complete (corrected the
'character ability decks' mislabel — ability decks are monster-only),
progress log entries for M3 and M4
svelte-check 0 errors; web tests 177/177; build OK; dart analyze
0 errors; dart test 592/592; scripts/ci.sh green.
Diffstat (limited to 'web/test/deckHelpers.test.ts')
| -rw-r--r-- | web/test/deckHelpers.test.ts | 486 |
1 files changed, 486 insertions, 0 deletions
diff --git a/web/test/deckHelpers.test.ts b/web/test/deckHelpers.test.ts new file mode 100644 index 0000000..8aef385 --- /dev/null +++ b/web/test/deckHelpers.test.ts @@ -0,0 +1,486 @@ +/** + * Phase 4: unit tests for the deck view helpers — the modifier-deck + * derivations (`$lib/game/modifierDeck`), the loot-card value logic + * (`$lib/game/cards/lootCard`), the rule predicates + * (`$lib/game/rules`), and the perk helpers (`$lib/game/perks`). + */ + +import { describe, it, expect } from 'vitest'; +import { + drawPileTopFirst, + deckHasCard, + deckHasMinus1, + deckHasPlus0, + deckMinus2Present, + removableTotal, + revealedCards, + factionCards, + factionCardInAnyCharacterDeck, + deckHasSanctuaryCard, + deckHasPartyCard +} from '$lib/game/modifierDeck'; +import { + poolCardGfx, + lootValue, + characterLootTotal +} from '$lib/game/cards/lootCard'; +import { + isCSCampaign, + isCustomCampaign, + isInactiveForRule, + isFrosthavenStyleForMonster, + modifierDeckByName, + hasCharacterClass, + currentCharacterId, + shouldShowAlliesDeck +} from '$lib/game/rules'; +import { + activePerks, + cardText, + perkDescription, + canAddPerk, + canRemovePerk +} from '$lib/game/perks'; +import type { + CharacterItem, + LootCard, + MonsterItem, + ModifierCard, + ModifierDeck, + SpecialRule +} from '$lib/protocol/types'; +import type { ClassData, PerkDetail } from '$lib/data/gameData'; +import { + emptyModifierDeck, + emptyGameState +} from './fixtures'; + +// --- fixtures ---------------------------------------------------------------- + +function card(gfx: string): ModifierCard { + return { gfx }; +} + +function deck(overrides: Partial<ModifierDeck> = {}): ModifierDeck { + return { ...emptyModifierDeck(), ...overrides }; +} + +function character( + id: string, + overrides: Partial<CharacterItem> = {} +): CharacterItem { + return { + id, + turnState: 0, + note: '', + characterClass: 'Bounty Hunter', + edition: 'Jaws of the Lion', + characterState: { + level: 1, + health: 10, + maxHealth: 10, + chill: 0, + conditions: [], + conditionsAddedThisTurn: [], + conditionsAddedPreviousTurn: [], + initiative: 1, + xp: 0, + display: id, + modifierDeck: emptyModifierDeck(), + summonList: [], + useFHPerks: false, + perkList: [] + }, + ...overrides + }; +} + +function monster(overrides: Partial<MonsterItem> = {}): MonsterItem { + return { + id: 'Worg', + turnState: 0, + note: '', + isActive: true, + type: 'Worg', + monsterInstances: [], + isAlly: false, + v: 1, + level: 1, + ...overrides + }; +} + +function loot(overrides: Partial<LootCard> = {}): LootCard { + return { + gfx: 'hide', + owner: 'c1', + id: 100, + enhanced: 0, + baseValue: 0, + lootType: 0, + ...overrides + }; +} + +function perk(text = '', adds: string[] = [], removes: string[] = []): PerkDetail { + return { text, adds, removes }; +} + +function classData(perks: PerkDetail[], perksFH: PerkDetail[] = []): ClassData { + return { + id: 'Bounty Hunter', + name: 'Bounty Hunter', + edition: 'Jaws of the Lion', + hidden: false, + health: [10], + color: '000000', + colorSecondary: '000000', + icon: '/images/class-icons/Bounty%20Hunter.png', + perks: perks.map((p) => p.adds), + perksFH: perksFH.map((p) => p.adds), + perkDetails: perks, + perkDetailsFH: perksFH + }; +} + +function inactiveRule( + name: string, + list: number[], + overrides: Partial<SpecialRule> = {} +): SpecialRule { + return { + type: 'InactiveMonster', + note: '', + name, + health: '', + condition: '', + init: 0, + level: 0, + startOfRound: false, + list, + ...overrides + }; +} + +// --- modifierDeck ------------------------------------------------------------- + +describe('modifierDeck', () => { + it('drawPileTopFirst reverses the bottom-first wire pile', () => { + const d = deck({ drawPile: [card('a'), card('b'), card('c')] }); + expect(drawPileTopFirst(d).map((c) => c.gfx)).toEqual(['c', 'b', 'a']); + // The original pile is not mutated. + expect(d.drawPile.map((c) => c.gfx)).toEqual(['a', 'b', 'c']); + }); + + it('deckHasCard looks in draw and discard piles only', () => { + const d = deck({ + drawPile: [card('minus1')], + discardPile: [card('plus0')], + removedPile: [card('minus2')] + }); + expect(deckHasCard(d, 'minus1')).toBe(true); + expect(deckHasCard(d, 'plus0')).toBe(true); + expect(deckHasCard(d, 'minus2')).toBe(false); + expect(deckHasMinus1(d)).toBe(true); + expect(deckHasPlus0(d)).toBe(true); + }); + + it('deckMinus2Present is true until the minus2 card is removed', () => { + expect(deckMinus2Present(deck())).toBe(true); + expect( + deckMinus2Present(deck({ removedPile: [card('minus2')] })) + ).toBe(false); + // A minus2 in the draw pile still counts as present. + expect( + deckMinus2Present(deck({ drawPile: [card('minus2')] })) + ).toBe(true); + }); + + it('removableTotal falls back to 0', () => { + const d = deck(); + d.removables['curse'] = 3; + expect(removableTotal(d, 'curse')).toBe(3); + expect(removableTotal(d, 'bless')).toBe(0); + }); + + it('revealedCards returns the top N draw cards, clamped', () => { + const d = deck({ drawPile: [card('a'), card('b'), card('c')] }); + expect(revealedCards(d, 1).map((c) => c.gfx)).toEqual(['c']); + expect(revealedCards(d, 5).map((c) => c.gfx)).toEqual(['c', 'b', 'a']); + expect(revealedCards(d, 0).map((c) => c.gfx)).toEqual([]); + }); + + it('factionCards lists four cards per faction, none for others', () => { + expect(factionCards('Demons')).toEqual([ + 'Demons-perks/plus1any', + 'Demons-perks/plus1retaliate1flip', + 'Demons-perks/plus0wardallyflip', + 'Demons-perks/unique/fuck3' + ]); + expect(factionCards('Merchant-Guild')[0]).toBe( + 'Merchant-Guild-perks/plus1curse' + ); + expect(factionCards('Military').length).toBe(4); + expect(factionCards('Something Else')).toEqual([]); + }); + + it('factionCardInAnyCharacterDeck grays out cards other decks have', () => { + const decks = [ + { name: 'c1', deck: deck({ drawPile: [card('a')] }) }, + { name: 'c2', deck: deck({ discardPile: [card('b')] }) } + ]; + expect(factionCardInAnyCharacterDeck(decks, 'a')).toBe(true); + expect(factionCardInAnyCharacterDeck(decks, 'b')).toBe(true); + expect(factionCardInAnyCharacterDeck(decks, 'c')).toBe(false); + }); + + it('deckHasSanctuaryCard/deckHasPartyCard match gfx prefixes', () => { + expect( + deckHasSanctuaryCard(deck({ drawPile: [card('sanctuary/1')] })) + ).toBe(true); + expect(deckHasSanctuaryCard(deck({ drawPile: [card('party/1')] }))).toBe( + false + ); + expect( + deckHasPartyCard(deck({ discardPile: [card('party/2')] })) + ).toBe(true); + expect(deckHasPartyCard(deck())).toBe(false); + }); +}); + +// --- lootCard ----------------------------------------------------------------- + +describe('lootCard', () => { + it('poolCardGfx derives the coin denomination from the index', () => { + expect(poolCardGfx('coin', 0)).toBe('coin 1'); + expect(poolCardGfx('coin', 11)).toBe('coin 1'); + expect(poolCardGfx('coin', 12)).toBe('coin 2'); + expect(poolCardGfx('coin', 17)).toBe('coin 2'); + expect(poolCardGfx('coin', 18)).toBe('coin 3'); + expect(poolCardGfx('coin', 19)).toBe('coin 3'); + expect(poolCardGfx('hide', 3)).toBe('hide'); + }); + + it('lootValue applies party size to the base value', () => { + const c = loot({ baseValue: 1 }); // one if 4, two if not + expect(lootValue(c, 4)).toBe(1); + expect(lootValue(c, 3)).toBe(2); + const b = loot({ baseValue: 2 }); // one if 3 or 4, two if not + expect(lootValue(b, 4)).toBe(1); + expect(lootValue(b, 3)).toBe(1); + expect(lootValue(b, 2)).toBe(2); + // Enhancements always add. + expect(lootValue(loot({ baseValue: 0, enhanced: 2 }), 2)).toBe(3); + // Non-materiel cards show only the enhancement. + expect(lootValue(loot({ lootType: 1 }), 4)).toBe(null); + expect(lootValue(loot({ lootType: 1, enhanced: 3 }), 4)).toBe(3); + }); + + it('characterLootTotal sums a character’s coins and loot', () => { + const pile = [ + loot({ gfx: 'coin 1', id: 1 }), + loot({ gfx: 'coin 2', id: 2, enhanced: 1 }), + loot({ gfx: 'coin 3', id: 3 }), + loot({ gfx: 'hide', id: 4, baseValue: 1 }), + loot({ gfx: 'hide', id: 5, owner: 'c2' }) // someone else's + ]; + // 4 characters: baseValue-1 hide counts 1. + expect(characterLootTotal(pile, 'c1', 'coin', 4)).toBe(1 + 3 + 3); + expect(characterLootTotal(pile, 'c1', 'hide', 4)).toBe(1); + expect(characterLootTotal(pile, 'c2', 'hide', 4)).toBe(1); + expect(characterLootTotal(pile, 'c1', 'metal', 4)).toBe(0); + }); +}); + +// --- rules -------------------------------------------------------------------- + +describe('rules', () => { + it('campaign predicates', () => { + expect(isCSCampaign('Crimson Scales')).toBe(true); + expect(isCSCampaign('Trail of Ashes')).toBe(true); + expect(isCSCampaign('CCUG')).toBe(false); + expect(isCustomCampaign('CCUG')).toBe(true); + expect(isCustomCampaign('Frosthaven')).toBe(false); + }); + + it('modifierDeckByName resolves party, ally, and character decks', () => { + const gs = emptyGameState(); + const personal = emptyModifierDeck(); + gs.modifierDeck.addedMinusOnes = 1; + gs.modifierDeckAllies.addedMinusOnes = 2; + const c = character('c1'); + c.characterState.modifierDeck.addedMinusOnes = 3; + gs.currentList = [c]; + expect(modifierDeckByName(gs, '')).toBe(gs.modifierDeck); + expect(modifierDeckByName(gs, 'allies')).toBe(gs.modifierDeckAllies); + expect(modifierDeckByName(gs, 'c1').addedMinusOnes).toBe(3); + // Unknown names fall back to the party deck. + expect(modifierDeckByName(gs, 'nobody')).toBe(gs.modifierDeck); + }); + + it('hasCharacterClass / currentCharacterId', () => { + const gs = emptyGameState({ + currentList: [ + character('c1'), + character('c2', { turnState: 1, characterClass: 'Pyromancer' }) + ] + }); + expect(hasCharacterClass(gs, 'Pyromancer')).toBe(true); + expect(hasCharacterClass(gs, 'Cassandra')).toBe(false); + expect(currentCharacterId(gs)).toBe('c2'); + expect(currentCharacterId(emptyGameState())).toBe(null); + }); + + it('shouldShowAlliesDeck follows the reference gates', () => { + const ally = monster({ id: 'Ally', isAlly: true }); + // The explicit setting always wins. + expect( + shouldShowAlliesDeck(emptyGameState({ showAllyDeck: true })) + ).toBe(true); + // OG edition without the setting and without an ally: hidden. + expect( + shouldShowAlliesDeck( + emptyGameState({ allyDeckInOGGloom: false, currentList: [ally] }) + ) + ).toBe(false); + // OG edition with the setting: shown only with an ally. + expect( + shouldShowAlliesDeck( + emptyGameState({ allyDeckInOGGloom: true, currentList: [ally] }) + ) + ).toBe(true); + expect( + shouldShowAlliesDeck( + emptyGameState({ + allyDeckInOGGloom: true, + currentList: [monster()] + }) + ) + ).toBe(false); + // Frosthaven: no OG gate. + const fh = emptyGameState({ currentCampaign: 'Frosthaven' }); + expect(shouldShowAlliesDeck(fh)).toBe(false); + fh.currentList = [ally]; + expect(shouldShowAlliesDeck(fh)).toBe(true); + }); + + it('isInactiveForRule matches the scenario round list', () => { + const gs = emptyGameState({ + round: 2, + scenarioSpecialRules: [inactiveRule('Worg', [1, 3])] + }); + expect(isInactiveForRule(gs, 'Worg')).toBe(false); + gs.round = 1; + expect(isInactiveForRule(gs, 'Worg')).toBe(true); + expect(isInactiveForRule(gs, 'Another')).toBe(false); + expect(isInactiveForRule(emptyGameState(), 'Worg')).toBe(false); + }); + + it('isFrosthavenStyleForMonster keys off edition and theme', () => { + const gs = emptyGameState(); // Jaws of the Lion: OG edition + expect( + isFrosthavenStyleForMonster('Frosthaven', gs, 'gloomhaven') + ).toBe(true); // the monster edition always wins + expect( + isFrosthavenStyleForMonster('Jaws of the Lion', gs, 'frosthaven') + ).toBe(true); // theme override + expect( + isFrosthavenStyleForMonster('Jaws of the Lion', gs, 'gloomhaven') + ).toBe(false); + // Solo: #5 and #100 are OG-styled, #19 and #200 are not. + const solo = emptyGameState({ currentCampaign: 'Solo' }); + expect( + isFrosthavenStyleForMonster('Solo', { ...solo, scenario: '#5 X' }, 'gloomhaven') + ).toBe(true); + expect( + isFrosthavenStyleForMonster('Solo', { ...solo, scenario: '#19 X' }, 'gloomhaven') + ).toBe(false); + expect( + isFrosthavenStyleForMonster('Solo', { ...solo, scenario: '#200 X' }, 'gloomhaven') + ).toBe(false); + }); +}); + +// --- perks -------------------------------------------------------------------- + +describe('perks', () => { + const classFH = classData( + [perk('', ['plus1'])], + [perk('', ['plus2'])] + ); + + function characterState(useFHPerks: boolean, perks?: boolean[]) { + const c = character('c1'); + c.characterState.useFHPerks = useFHPerks; + c.characterState.perkList = perks ?? []; + return c.characterState; + } + + it('activePerks switches on the FH opt-in', () => { + expect(activePerks(classFH, characterState(true)).map((p) => p.adds[0])).toEqual( + ['plus2'] + ); + expect(activePerks(classFH, characterState(false)).map((p) => p.adds[0])).toEqual( + ['plus1'] + ); + // No FH list: the opt-in has nothing to fall back to. + expect( + activePerks(classData([perk('', ['plus1'])]), characterState(true)) + ).toEqual(classFH.perkDetails); + }); + + it('cardText renders modifier tokens', () => { + expect(cardText('plus1')).toBe('+1'); + expect(cardText('minus2')).toBe('-2'); + expect(cardText('minus2flip')).toBe('-2%flip%'); + expect(cardText('perks/minus1flip')).toBe('-1%flip%'); + expect(cardText('plus1range2')).toBe('+1 %range% 2'); + expect(cardText('minus1target2')).toBe('-1+ 2%target%'); + expect(cardText('plus1heal2')).toBe('+1 "%heal%2, self"'); + }); + + it('perkDescription phrases adds/removes', () => { + expect(perkDescription(perk('', ['plus1']))).toBe('Add one +1 card'); + expect(perkDescription(perk('', ['plus1', 'plus2']))).toBe( + 'Add two +1 cards' + ); + expect(perkDescription(perk('', [], ['minus1']))).toBe( + 'Remove one -1 card' + ); + expect( + perkDescription(perk('', [], ['minus1', 'minus2'])) + ).toBe('Remove two -1 cards'); + expect(perkDescription(perk('', ['plus1'], ['minus1']))).toBe( + 'Replace one -1 card with one +1 card' + ); + expect(perkDescription(perk('Special text'))).toBe('Special text'); + }); + + it('canAddPerk needs the removed cards in the deck', () => { + const perks = [perk('', ['plus1'], ['minus1'])]; + const empty = deck(); + const withCard = deck({ drawPile: [card('minus1')] }); + expect(canAddPerk(empty, perks, [false], 0)).toBe(false); + expect(canAddPerk(withCard, perks, [false], 0)).toBe(true); + // Another active perk adding the same card counts as present. + const adder = [perk('', ['minus1']), perk('', [], ['minus1'])]; + expect(canAddPerk(empty, adder, [true, false], 1)).toBe(true); + expect(canAddPerk(empty, adder, [false, false], 1)).toBe(false); + }); + + it('canRemovePerk needs the added cards in the deck', () => { + const perks = [perk('', ['plus1'])]; + expect( + canRemovePerk(deck(), perks, 0) + ).toBe(false); + expect(canRemovePerk(deck({ discardPile: [card('plus1')] }), perks, 0)).toBe( + true + ); + // A perk card is tracked by its P<n> id, not its gfx. + const perkCard = [perk('', ['perks/plus2flip'])]; + expect( + canRemovePerk(deck({ drawPile: [card('P0')] }), perkCard, 0) + ).toBe(true); + expect(canRemovePerk(deck(), perkCard, 0)).toBe(false); + }); +}); |
