forked from WikiDeck/wikideck
- src/game/collection/: collection model (GET /api/game/collection, local fallback), 40-card eligibility with the 4-per-article copy limit, "23 / 40 cards. Mine to play" gate, constructed deck picker. No loaners, starters or borrowed decks (BRIEF decisions 1-2). - PlaySection: PvP hub with quick match, private lobby + invite link, join by link, ladder; bot match slot waits for ui-duel's board. - online.ts / OnlinePlay.tsx: duel PvP client against host-pvp's routes (idToken in body, Bearer seat token, ?v= polling with 204), validated responses. - Leaderboards: Elo ladder with W/L fallback. - src/game/recap/: /battle/<id> (and /recap/<id>) standalone page rendering duel-design's RecapDoc shape; deterministic template fallback over real match data until #69 merges. - App: /play/invite/<code> and /battle/<id> deep links; session.idToken(). - tests/ui-pvp-*.test.ts, tests/e2e/ui-pvp-smoke.mjs (Playwright, stubbed API). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
100 lines
5.5 KiB
TypeScript
100 lines
5.5 KiB
TypeScript
import test from 'node:test';
|
|
import assert from 'node:assert/strict';
|
|
import { collectionFromBlocks, deckProblem, eligibility, fillDeck, parseCollection, playableCount, progressLabel, type Collection } from '../src/game/collection/collection.ts';
|
|
import { CLASSIC, parseDuelModes } from '../src/game/collection/duelModes.ts';
|
|
import { inviteCode, inviteFromPath, parseDuelStatus, parseLadder, parseSeat } from '../src/game/online.ts';
|
|
import type { TestnetBlock } from '../shared/testnet.ts';
|
|
|
|
const card = (i: number, pageId = 1000 + i) => ({ tokenId: `t${i}`, pageId, title: `Article ${pageId}` });
|
|
const coll = (n: number, pages?: (i: number) => number, deckSize = 40): Collection =>
|
|
({ deckSize, copyLimit: 4, source: 'server', cards: Array.from({ length: n }, (_, i) => card(i, pages?.(i))) });
|
|
|
|
test('23 owned cards: not eligible, shows "23 / 40 cards" and needs 17 more', () => {
|
|
const e = eligibility(coll(23));
|
|
assert.equal(e.eligible, false);
|
|
assert.equal(e.needed, 17);
|
|
assert.equal(progressLabel(e), '23 / 40 cards');
|
|
});
|
|
|
|
test('40 owned distinct cards are eligible; the copy limit caps playable cards per article', () => {
|
|
assert.equal(eligibility(coll(40)).eligible, true);
|
|
// 44 cards over 10 articles: at most 4 copies each → 40 playable.
|
|
assert.equal(playableCount(coll(44, i => i % 10)), 40);
|
|
// 50 cards of 5 articles: only 20 playable, so not eligible despite owning 50.
|
|
const e = eligibility(coll(50, i => i % 5));
|
|
assert.equal(e.owned, 50);
|
|
assert.equal(e.playable, 20);
|
|
assert.equal(e.eligible, false);
|
|
});
|
|
|
|
test('deckProblem: exactly deckSize owned cards, no repeats, copy limit enforced', () => {
|
|
const c = coll(60, i => (i < 8 ? 1 : 1000 + i));
|
|
const legal = fillDeck([], c);
|
|
assert.equal(legal.length, 40);
|
|
assert.equal(deckProblem(legal, c), '');
|
|
assert.match(deckProblem(legal.slice(1), c), /exactly 40/);
|
|
assert.match(deckProblem([...legal.slice(1), 'not-mine'], c), /no longer own/);
|
|
assert.match(deckProblem([...legal.slice(1), legal[2]], c), /twice/);
|
|
const fiveCopies = [...c.cards.slice(0, 5).map(x => x.tokenId), ...c.cards.slice(8, 43).map(x => x.tokenId)];
|
|
assert.match(deckProblem(fiveCopies, c), /At most 4 copies/);
|
|
});
|
|
|
|
test('fillDeck keeps still-owned saved cards first and drops sold ones', () => {
|
|
const c = coll(45);
|
|
const deck = fillDeck(['t44', 'gone', 't43'], c);
|
|
assert.deepEqual(deck.slice(0, 2), ['t44', 't43']);
|
|
assert.equal(deck.length, 40);
|
|
assert.ok(!deck.includes('gone'));
|
|
});
|
|
|
|
test('a testnet mode may lower deckSize; production default stays 40', () => {
|
|
assert.equal(parseCollection({}).deckSize, 40);
|
|
assert.equal(eligibility(coll(12, undefined, 10)).eligible, true);
|
|
});
|
|
|
|
test('parseCollection drops malformed cards, duplicate tokenIds and non-https urls', () => {
|
|
const c = parseCollection({ deckSize: 40, copyLimit: 4, cards: [
|
|
{ tokenId: 'a', pageId: 5, title: 'Five', url: 'javascript:alert(1)', imageUrl: 'https://upload.wikimedia.org/x.jpg' },
|
|
{ tokenId: 'a', pageId: 6, title: 'Dup' }, { tokenId: 'b' }, null, 'x', { tokenId: 'c', pageId: -1 },
|
|
] });
|
|
assert.equal(c.cards.length, 1);
|
|
assert.equal(c.cards[0].url, undefined);
|
|
assert.equal(c.cards[0].imageUrl, 'https://upload.wikimedia.org/x.jpg');
|
|
});
|
|
|
|
test('the local fallback reads real owned blocks only', () => {
|
|
const block = { tokenId: 'tok', height: 7, article: { pageId: 9, title: 'Nine', revisionId: 3, url: 'https://en.wikipedia.org/wiki/Nine', extract: 'x', category: 'Places', source: 'live' } } as unknown as TestnetBlock;
|
|
const c = collectionFromBlocks([block]);
|
|
assert.equal(c.source, 'local');
|
|
assert.deepEqual(c.cards.map(x => [x.tokenId, x.pageId, x.height]), [['tok', 9, 7]]);
|
|
});
|
|
|
|
test('invite links: parse a pasted URL, a bare code, and the deep link path', () => {
|
|
assert.equal(inviteCode('https://wikideck.eom.dev/play/invite/AbC_123'), 'AbC_123');
|
|
assert.equal(inviteCode(' AbC_123 '), 'AbC_123');
|
|
assert.equal(inviteCode('https://evil.example/<script>'), '');
|
|
assert.equal(inviteFromPath('/play/invite/xyz9'), 'xyz9');
|
|
assert.equal(inviteFromPath('/play/join/lob_1'), 'lob_1');
|
|
assert.equal(inviteFromPath('/battle/abc'), '');
|
|
});
|
|
|
|
test('host responses are validated: seats, statuses and the ladder', () => {
|
|
assert.deepEqual(parseSeat({ lobbyId: 'L1', seat: 1, token: 'tok', invite: 'inv1' }), { lobbyId: 'L1', seat: 1, token: 'tok', invite: 'inv1' });
|
|
assert.throws(() => parseSeat({ lobbyId: 'L1', seat: 2, token: 'tok' }), /invalid seat/);
|
|
const s = parseDuelStatus({ lobbyId: 'L1', version: 4, phase: 'bogus', players: [{ name: 'x'.repeat(99), rating: 1211.6 }], matchId: '../etc' });
|
|
assert.equal(s.phase, 'waiting');
|
|
assert.equal(s.players[0].name.length, 24);
|
|
assert.equal(s.players[0].rating, 1212);
|
|
assert.equal(s.matchId, undefined);
|
|
const l = parseLadder({ entries: [{ name: 'alice', rating: 1224, wins: 3, losses: 1 }, { name: '', wins: -2 }, 7] });
|
|
assert.deepEqual(l, [{ name: 'alice', rating: 1224, wins: 3, losses: 1, draws: 0 }, { name: 'Anonymous', rating: null, wins: 0, losses: 0, draws: 0 }]);
|
|
});
|
|
|
|
test('only duel-engine modes are offered; Classic is the default', () => {
|
|
assert.equal(CLASSIC.id, 'classic');
|
|
const m = parseDuelModes({ modes: [{ mode: { id: 'standard', name: 'Edit War', engine: 'editwar' } },
|
|
{ mode: { id: 'classic', name: 'Classic', engine: 'duel', limits: { rated: true } } },
|
|
{ mode: { id: 'e2e', name: 'Testnet E2E', engine: 'duel', rules: { deckSize: 20 } } }] });
|
|
assert.deepEqual(m.map(x => [x.id, x.ranked, x.deckSize]), [['classic', true, undefined], ['e2e', false, 20]]);
|
|
});
|