Files
wikideck/tests/e2e/ui-pvp-smoke.mjs
claudeanderic dbb89ce9c5 Night 2: 40-card Magic-style duel, PvP with owned decks, polite Wikimedia client, Android mining (#23)
Night 2 (2026-11-17), from the `claude/wikideck` fork's `night/2026-11-17` branch. It follows Night 1 (#22).

## What's in it
- **Duel engine** (fork #72, #80): 40-card decks of cards you own, Notability/Citations, keywords, effects T1–T12, Connected, a hidden-information-safe `viewFor`/`logFor`, and replay. Balance v1 tuning (#84) gives the first player about 51%.
- **Card profiles** (#70, #82): Figure, Location, Event, Institution or Doctrine, plus stats, keywords and rules text, all derived from real article data. Templates only.
- **Design and story** (#66, #69, #83): DUEL_DESIGN.md, plus the match log and the encyclopedic "Battle of X (2026)" recap.
- **PvP** (#74, #77, #81, #76, #85):
  - The server checks ownership, and 40-card eligibility is enforced.
  - Modes: Classic (ranked), Draft and Trending.
  - Invites, quick match, Elo, a ladder and recap pages. Online matches render on the duel board.
- **Duel board UI** (#79): MTG-style card faces and combat declaration, built for 360 px first.
- **Polite Wikimedia client** (#73, #75): throttled, cached and identified requests, the mint-path fix, and a prefetch pool.
- **Android mining** (#78): native RandomX, as a sideload build. The emulator mined a testnet block.
- **Testnet ops** (#67, #68).

PR numbers refer to `claude/wikideck`.

## Status
- **Tests:** 423, of which 422 pass, 0 fail and 1 is skipped. The build is OK (`node:24-alpine`, `--network none`).
- **Testnet:** running this commit (036489c) on r350.
- **Untouched:** no changes to `server/testnet/consensus.ts`, `randomx.ts`, `native/` or the mainnet compose.
- **Review:** night-review reviewed fork #66–#79. #80–#85 came from the agents' unfinished branches and were not reviewed.
- **Known gaps:**
  - The PvP end-to-end test hasn't been run (it needs two eom.dev accounts).
  - Bot matches aren't reachable from Play.
  - Card identity pre-bake hasn't been done.
  - The RandomX verifier latch (`server/testnet/randomx.ts:140`) is still open.

This PR is for Eric to merge.

🤖 Generated with [Claude Code](https://claude.com/claude-code)Reviewed-on: WikiDeck/wikideck#23

Co-authored-by: Claude <claude@eom.dev>
2026-11-18 18:12:24 +00:00

89 lines
6.3 KiB
JavaScript

// Playwright smoke test for the ui-pvp screens (not part of `npm test`: Playwright isn't a repo dependency).
// Every /api call is stubbed in the browser, so it makes zero network calls beyond localhost.
// Run: npm run build && npx vite preview --port 4179 & then
// PLAYWRIGHT_FROM=<dir containing node_modules/playwright>/ node tests/e2e/ui-pvp-smoke.mjs http://localhost:4179 <out-dir>
import { createRequire } from 'node:module';
import { readFileSync, mkdirSync } from 'node:fs';
import assert from 'node:assert/strict';
const { chromium } = createRequire(process.env.PLAYWRIGHT_FROM ? `${process.env.PLAYWRIGHT_FROM.replace(/\/?$/, '/')}x.js` : import.meta.url)('playwright');
const base = process.argv[2] ?? 'http://localhost:4179';
const out = process.argv[3] ?? 'screenshots';
mkdirSync(out, { recursive: true });
const sample = JSON.parse(readFileSync(new URL('../../docs/game/card-sample.json', import.meta.url), 'utf8')).cards;
const cards = n => sample.slice(0, n).map((c, i) => ({ tokenId: `tok${i}`, pageId: c.pageId, title: c.title, revisionId: c.revisionId,
url: `https://en.wikipedia.org/wiki/${encodeURIComponent(c.title.replace(/ /g, '_'))}`, category: 'People', height: 10 + i, confirmations: 3 }));
const account = { sub: 'alice-sub', issuer: 'https://dex.eom.dev', name: 'alice', username: 'alice', wallets: [{ address: 'a'.repeat(64), linkedAt: 1 }] };
const match = { matchId: 'm_smoke', modeName: 'Classic', players: [{ name: 'alice' }, { name: 'bob' }], turns: 9,
startedAt: Date.UTC(2026, 10, 17, 23, 40), endedAt: Date.UTC(2026, 10, 18, 0, 2), result: { winner: 0, reason: 'notability' }, strength: [40, 40],
played: [sample.slice(0, 4).map(c => ({ ...c, type: 'Figure' })).concat([{ ...sample[10], type: 'Doctrine' }]), sample.slice(4, 8).map(c => ({ ...c, type: 'Figure' }))],
lost: [[sample[1]], [sample[4], sample[5]]] };
async function page(browser, { width, height, owned }) {
const ctx = await browser.newContext({ viewport: { width, height }, deviceScaleFactor: 2 });
await ctx.addInitScript(a => {
sessionStorage.setItem('wikideck.auth.session', JSON.stringify({ idToken: 'test-token', expiresAt: Date.now() + 3600e3, account: a }));
localStorage.setItem('wikideck-play-tutorial-v1', '1');
}, account);
let lobbyPolls = 0;
await ctx.route('**/api/**', async route => {
const url = new URL(route.request().url());
const p = url.pathname, json = (body, status = 200) => route.fulfill({ status, contentType: 'application/json', body: JSON.stringify(body) });
if (p === '/api/game/collection') return json({ deckSize: 40, copyLimit: 4, cards: cards(owned) });
if (p === '/api/account/me') return json(account);
if (p === '/api/game/modes') return json({ modes: [{ mode: { id: 'classic', name: 'Classic', engine: 'duel', limits: { rated: true } } }] });
if (p.endsWith('/ladder')) return json({ entries: [{ name: 'alice', rating: 1236, wins: 4, losses: 1 }, { name: 'bob', rating: 1188, wins: 1, losses: 3 }, { name: 'carol', rating: 1176, wins: 0, losses: 1 }] });
if (p === '/api/game/modes/classic/lobbies') return json({ lobbyId: 'L1', seat: 0, token: 'seat-tok', invite: 'Inv7Qx2' });
if (p === '/api/game/lobbies/L1') { lobbyPolls++; return json({ lobbyId: 'L1', version: 1, phase: 'waiting', seat: 0, players: [{ name: 'alice' }], outcome: null, invite: 'Inv7Qx2' }); }
if (p === '/api/game/matches/m_smoke') return json(match);
return route.fulfill({ status: 503, contentType: 'application/json', body: '{"error":"stubbed"}' });
});
return { ctx, page: await ctx.newPage(), polls: () => lobbyPolls };
}
const openPlay = async p => {
await p.goto(base + '/');
await p.getByRole('navigation', { name: 'Main navigation' }).getByText('Play', { exact: true }).first().click();
};
const browser = await chromium.launch();
try {
// 1. Under 40 cards: progress + mine, no borrowed decks.
{ const { ctx, page: p } = await page(browser, { width: 360, height: 740, owned: 23 });
await openPlay(p);
await p.getByText('23 / 40 cards. Mine to play').waitFor();
assert.equal(await p.getByRole('button', { name: /Quick match/ }).count(), 0, 'no play buttons below 40 cards');
assert.equal(await p.getByText(/loaner|starter deck/i).count(), 0, 'no loaners offered');
await p.screenshot({ path: `${out}/1-eligibility-360.png`, fullPage: true }); await ctx.close(); }
// 2. 44 cards: PvP hub with quick match, private lobby, join, ladder.
{ const { ctx, page: p, polls } = await page(browser, { width: 360, height: 740, owned: 44 });
await openPlay(p);
await p.getByRole('button', { name: /Quick match/ }).waitFor();
await p.getByText('40 / 40').waitFor();
await p.getByText('1236').waitFor();
await p.screenshot({ path: `${out}/2-pvp-hub-360.png`, fullPage: true });
await p.getByRole('button', { name: 'Edit deck' }).click();
await p.getByText(/Pick exactly 40/).waitFor();
await p.screenshot({ path: `${out}/3-deck-picker-360.png` });
await p.getByRole('button', { name: 'Cancel' }).click();
await p.getByRole('button', { name: /Private lobby/ }).click();
await p.getByLabel('Invite link').getByText(/\/play\/invite\/Inv7Qx2$/).waitFor();
await p.waitForTimeout(2500);
assert.ok(polls() >= 1, 'the lobby polls the host');
await p.screenshot({ path: `${out}/4-private-lobby-360.png`, fullPage: true }); await ctx.close(); }
// 3. Invite deep link.
{ const { ctx, page: p } = await page(browser, { width: 360, height: 740, owned: 44 });
await p.goto(base + '/play/invite/Inv7Qx2');
await p.getByText('You have been invited to a duel.').waitFor();
await p.screenshot({ path: `${out}/5-invite-360.png` }); await ctx.close(); }
// 4. Battle recap at 360 and desktop.
for (const [w, h, name] of [[360, 740, '6-recap-360'], [1280, 900, '7-recap-desktop']]) {
const { ctx, page: p } = await page(browser, { width: w, height: h, owned: 44 });
await p.goto(base + (w === 360 ? '/recap/m_smoke' : '/battle/m_smoke'));
await p.getByRole('heading', { level: 1, name: /^Battle of / }).waitFor();
await p.getByRole('heading', { name: 'References' }).waitFor();
assert.ok(await p.locator('.recap-refs li').count() >= 8);
await p.screenshot({ path: `${out}/${name}.png`, fullPage: true }); await ctx.close();
}
console.log('ui-pvp smoke: OK');
} finally { await browser.close(); }