From a57698a7e276e37fed7746bfd8ac4424462181a2 Mon Sep 17 00:00:00 2001 From: Claude Date: Tue, 17 Nov 2026 22:17:44 -0500 Subject: [PATCH] ui-duel: accept untyped duel profiles from the collection and shape-check them (#58) Co-Authored-By: Claude Opus 5.5 --- src/game/duel/decks.ts | 12 ++++++++++-- tests/ui-duel-adapter.test.ts | 9 +++++++++ 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/src/game/duel/decks.ts b/src/game/duel/decks.ts index 877cf82..2239ef8 100644 --- a/src/game/duel/decks.ts +++ b/src/game/duel/decks.ts @@ -2,7 +2,15 @@ import type { DuelAction, DuelCard, PlayerView } from '../../../shared/duel/types.ts'; /** A collection card as ui-pvp passes it (ownership's ChainCard satisfies this). `duel` is the server-derived frozen profile. */ -export type BotMatchCard = { tokenId: string; title: string; url?: string; imageUrl?: string; extract?: string; duel?: DuelCard }; +export type BotMatchCard = { tokenId: string; title: string; url?: string; imageUrl?: string; extract?: string; duel?: unknown }; + +const TYPES = ['Figure', 'Location', 'Event', 'Institution', 'Doctrine']; +/** Shape check for a profile that arrives untyped over the wire (the engine's validateDeck does the rest). */ +export function isDuelCard(v: unknown): v is DuelCard { + const c = v as Partial | null; + return !!c && typeof c === 'object' && typeof c.title === 'string' && TYPES.includes(c.type as string) && typeof c.cost === 'number' + && Array.isArray(c.keywords) && Array.isArray(c.effects) && typeof c.rulesText === 'string'; +} /** The bot sees only its own seat's view (Night 1 rule, #5). */ export type DuelBot = { name: string; choose(view: PlayerView, legal: DuelAction[], rng: () => number): DuelAction }; @@ -25,7 +33,7 @@ export function playerDeck(deck: string[], cards: BotMatchCard[], deckSize: numb const out: DuelCard[] = [], pending: string[] = [], missing: string[] = []; for (const id of deck) { const c = byId.get(id); - if (!c) missing.push(id); else if (!c.duel) pending.push(c.title); else out.push({ ...c.duel, cardId: id }); + if (!c) missing.push(id); else if (!isDuelCard(c.duel)) pending.push(c.title); else out.push({ ...c.duel, cardId: id }); } return { cards: out.length === deckSize && deck.length === deckSize ? out : [], pending, missing }; } diff --git a/tests/ui-duel-adapter.test.ts b/tests/ui-duel-adapter.test.ts index 9db5a59..817c8cf 100644 --- a/tests/ui-duel-adapter.test.ts +++ b/tests/ui-duel-adapter.test.ts @@ -114,3 +114,12 @@ test('bot match decks: exactly deckSize owned, profiled cards; the bot deck is i assert.ok(a.every(c => c.cardId.startsWith('bot:'))); assert.notDeepEqual(botDeck(pool, 40, 'other').map(c => c.cardId), a.map(c => c.cardId)); }); + +test('bot match: untyped profiles from the wire are shape-checked', async () => { + const { playerDeck, isDuelCard } = await import('../src/game/duel/decks.ts'); + assert.equal(isDuelCard(card('x')), true); + assert.equal(isDuelCard({ title: 'x' }), false); + assert.equal(isDuelCard(null), false); + const cards = Array.from({ length: 40 }, (_, i) => ({ tokenId: `t${i}`, title: `C${i}`, duel: i === 5 ? { junk: true } : card(`d${i}`) })); + assert.deepEqual(playerDeck(cards.map(c => c.tokenId), cards, 40).pending, ['C5']); +});