Files
wikideck/tests/wiki.test.ts
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

86 lines
4.3 KiB
TypeScript
Executable File

import test from 'node:test';
import assert from 'node:assert/strict';
import { WikipediaArticleSource } from '../server/wiki.ts';
function page(pageid: number, patch: Record<string, unknown> = {}) {
return { pageid, ns: 0, title: `Article ${pageid}`, extract: `An encyclopedia article with enough explanatory text for a useful collectible card. ${pageid}.`,
lastrevid: pageid + 100, thumbnail: { source: `https://upload.wikimedia.org/wikipedia/commons/${pageid}.jpg` },
categories: [{ title: 'Category:Science' }], ...patch };
}
function json(value: unknown, headers: Record<string, string> = {}) {
return new Response(JSON.stringify(value), { headers: { 'Content-Type': 'application/json', ...headers } });
}
function mockFetch(handler: (url: URL, init: RequestInit) => Response | Promise<Response>) {
const calls: { url: URL; init: RequestInit }[] = [];
const fetcher: typeof fetch = async (input, init) => {
const url = new URL(String(input));
calls.push({ url, init: init! });
return handler(url, init!);
};
return { fetcher, calls };
}
test('random article lookup retries batches with no pictures and selects a pictured page', async () => {
let attempt = 0;
const { fetcher, calls } = mockFetch(() => {
attempt++;
return attempt === 1
? json({ query: { pages: [page(10, { thumbnail: undefined }), page(11, { thumbnail: undefined })] } })
: json({ query: { pages: [page(12)] } });
});
const article = await new WikipediaArticleSource(fetcher).random();
assert.equal(attempt, 2);
assert.equal(article.pageId, 12);
assert.equal(article.source, 'live');
assert.equal(article.image, 'https://upload.wikimedia.org/wikipedia/commons/12.jpg');
assert.equal(article.url, 'https://en.wikipedia.org/?curid=12');
assert.equal(calls.length, 2);
assert.equal(calls[0].url.origin, 'https://en.wikipedia.org');
assert.equal(calls[0].url.searchParams.get('generator'), 'random');
assert.equal(calls[0].url.searchParams.get('grnlimit'), '10');
assert.equal(calls[0].url.searchParams.get('piprop'), 'thumbnail');
assert.equal(calls[0].init.redirect, 'error');
});
test('only HTTPS Wikimedia pictures are eligible and random lookup has a bounded retry count', async () => {
const { fetcher, calls } = mockFetch(() => json({ query: { pages: [
page(20, { thumbnail: { source: 'https://evil.example/tracker.jpg' } }),
page(21, { thumbnail: { source: 'http://upload.wikimedia.org/image.jpg' } }),
] } }));
await assert.rejects(new WikipediaArticleSource(fetcher).random(), /three attempts/);
assert.equal(calls.length, 3);
});
test('random article fetch rejects unsafe response types, redirects, and oversized payloads without fallback', async () => {
const failures = [
() => new Response('{}', { headers: { 'Content-Type': 'text/html' } }),
() => new Response(null, { status: 302, headers: { Location: 'https://evil.example' } }),
() => new Response(' '.repeat(512 * 1024 + 1), { headers: { 'Content-Type': 'application/json' } }),
];
for (const fail of failures) {
const { fetcher, calls } = mockFetch(() => fail());
await assert.rejects(new WikipediaArticleSource(fetcher).random());
assert.equal(calls.length, 3);
}
// A network failure (or throttling) stops at once instead of retrying against an unreachable upstream.
const { fetcher, calls } = mockFetch(() => { throw new Error('offline'); });
await assert.rejects(new WikipediaArticleSource(fetcher).random(), /Wikimedia unavailable/);
assert.equal(calls.length, 1);
});
test('bad page records are skipped while later valid candidates are considered', async () => {
const { fetcher } = mockFetch(() => json({ query: { pages: [
page(30, { pageid: Number.MAX_SAFE_INTEGER + 1 }), page(31, { extract: 'short' }), page(32),
] } }));
const article = await new WikipediaArticleSource(fetcher).random();
assert.equal(article.pageId, 32);
});
test('a previously selected Wikipedia page may be selected again', async () => {
const { fetcher } = mockFetch(() => json({ query: { pages: [page(40)] } }));
const source = new WikipediaArticleSource(fetcher);
const first = await source.random();
const second = await source.random();
assert.equal(first.pageId, second.pageId);
});