Files
wikideck/tests/wikimedia-client.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

237 lines
12 KiB
TypeScript

import test from 'node:test';
import assert from 'node:assert/strict';
import { WikimediaClient, WikimediaUnavailable, configFromEnv, type ClientConfig } from '../server/wikimedia/client.ts';
import { WikimediaCache, cacheKey } from '../server/wikimedia/cache.ts';
import { warnIfNoContact } from '../server/wikimedia-user-agent.ts';
import { FakeClock, flush, json, recorder } from './wikimedia-helpers.ts';
const API = 'https://en.wikipedia.org/w/api.php?action=query&format=json&pageids=1';
const REST = 'https://wikimedia.org/api/rest_v1/metrics/pageviews/per-article/x';
function setup(handler: Parameters<typeof recorder>[1] = () => json({ ok: true }), config: Partial<ClientConfig> = {},
cache?: WikimediaCache) {
const clock = new FakeClock();
const rec = recorder(clock, handler);
const client = new WikimediaClient({ fetch: rec.fetcher, clock, config, cache, userAgent: () => 'Test/1 (+x; y)' });
return { clock, client, ...rec };
}
test('token bucket: burst of 3, then 1 request per second, one in flight per host', async () => {
const { clock, client, calls } = setup();
const jobs = Array.from({ length: 6 }, () => client.request(new URL(API), { lane: 'interactive', timeoutMs: 1000 })
.then(r => r.response.body?.cancel()));
await clock.advance(0);
assert.equal(calls.length, 3);
await clock.advance(999);
assert.equal(calls.length, 3);
await clock.advance(1);
assert.equal(calls.length, 4);
await clock.advance(2000);
await Promise.all(jobs);
assert.deepEqual(calls.map(c => c.at - calls[0].at), [0, 0, 0, 1000, 2000, 3000]);
// Different hosts have separate buckets.
await client.request(new URL(REST), { lane: 'interactive', timeoutMs: 1000 });
assert.equal(calls.at(-1)!.url.hostname, 'wikimedia.org');
});
test('only one request is in flight per host, even with tokens to spare', async () => {
const releases: (() => void)[] = [];
const { clock, client, calls } = setup(() => new Promise(resolve => releases.push(() => resolve(json({})))));
const a = client.request(new URL(API), { lane: 'interactive', timeoutMs: 1000 });
const b = client.request(new URL(API), { lane: 'interactive', timeoutMs: 1000 });
await clock.advance(0);
assert.equal(calls.length, 1);
releases.shift()!();
await a;
await clock.advance(0);
assert.equal(calls.length, 2);
releases.shift()!();
await b;
});
test('priority lanes: mint, then interactive, then background', async () => {
const { clock, client, calls } = setup(url => json({ lane: url.searchParams.get('n') }), { burst: 1 });
await client.request(new URL(`${API}&n=warm`), { lane: 'background', timeoutMs: 1000 }); // Uses the only token.
const order: string[] = [];
const go = (lane: 'mint' | 'interactive' | 'background', n: string) =>
client.request(new URL(`${API}&n=${n}`), { lane, timeoutMs: 1000 }).then(() => order.push(n));
const jobs = [go('background', 'b1'), go('interactive', 'i1'), go('background', 'b2'), go('mint', 'm1'), go('interactive', 'i2')];
await clock.advance(5000);
await Promise.all(jobs);
assert.deepEqual(order, ['m1', 'i1', 'i2', 'b1', 'b2']);
assert.equal(calls.length, 6);
});
test('maxlag=5 is added to Action API requests only, and a maxlag reply waits for Retry-After', async () => {
let lagged = 1;
const { clock, client, calls } = setup(url => url.pathname === '/w/api.php' && lagged-- > 0
? json({ error: { code: 'maxlag' } }, { 'MediaWiki-API-Error': 'maxlag', 'Retry-After': '5' })
: json({ ok: true }));
const job = client.request(new URL(API), { lane: 'background', timeoutMs: 1000 });
await clock.advance(0);
assert.equal(calls.length, 1);
assert.equal(calls[0].url.searchParams.get('maxlag'), '5');
await clock.advance(4999);
assert.equal(calls.length, 1);
await clock.advance(1);
const { response } = await job;
assert.deepEqual(await response.json(), { ok: true });
assert.equal(calls.length, 2);
await client.request(new URL(REST), { lane: 'background', timeoutMs: 1000 });
assert.equal(calls[2].url.searchParams.has('maxlag'), false);
assert.equal(calls[0].headers.get('user-agent'), 'Test/1 (+x; y)');
});
test('429 honours Retry-After by pausing the host; three in a row open the circuit for 10 minutes', async () => {
let status = 429;
const { clock, client, calls } = setup(() => status === 200 ? json({}) : new Response('slow down',
{ status, headers: { 'Retry-After': '2' } }));
const req = (lane: 'mint' | 'background' = 'background') => client.request(new URL(API), { lane, timeoutMs: 1000 });
await assert.rejects(req(), (e: unknown) => e instanceof WikimediaUnavailable && e.status === 429);
// The next request waits out Retry-After (2 s) instead of going straight back.
const second = req().catch(e => e);
await clock.advance(1999);
assert.equal(calls.length, 1);
await clock.advance(1);
assert.equal(((await second) as WikimediaUnavailable).status, 429);
const third = req().catch(e => e);
await clock.advance(10_000);
assert.equal(((await third) as WikimediaUnavailable).status, 429);
assert.equal(calls.length, 3);
assert.equal(client.status().hosts['en.wikipedia.org'].circuitOpen, true);
// Open circuit: refused at once without touching Wikimedia, even on the mint lane.
status = 200;
await assert.rejects(req('mint'), /circuit open/);
assert.equal(calls.length, 3);
await clock.advance(10 * 60_000);
await req('mint');
assert.equal(calls.length, 4);
assert.equal(client.status().counters['breaker.open'], 1);
});
test('a success resets the consecutive-429 count', async () => {
const replies = [429, 200, 429, 200, 429];
const { clock, client } = setup(() => {
const status = replies.shift()!;
return status === 200 ? json({}) : new Response('', { status });
});
for (let i = 0; i < 5; i++) {
const job = client.request(new URL(API), { lane: 'background', timeoutMs: 1000 }).catch(() => {});
await clock.advance(60_000);
await job;
}
assert.equal(client.status().hosts['en.wikipedia.org'].circuitOpen, false);
});
test('daily cap: background stops at 80 %, interactive at 100 %, mint keeps a 10 % reserve; resets at UTC midnight', async () => {
const { clock, client } = setup(() => json({}), { dailyCap: 10, requestsPerSecond: 1000, burst: 1000 });
const req = (lane: 'mint' | 'interactive' | 'background') => client.request(new URL(API), { lane, timeoutMs: 1000 });
for (let i = 0; i < 8; i++) await req('background');
await assert.rejects(req('background'), /daily cap/);
await req('interactive'); await req('interactive');
await assert.rejects(req('interactive'), /daily cap/);
await req('mint');
await assert.rejects(req('mint'), /daily cap/);
await clock.advance(24 * 3600_000);
await req('background');
});
test('nodes sharing one egress IP split the rate and the daily cap', () => {
const config = configFromEnv({ WIKIDECK_WIKIMEDIA_RPS: '0.9', WIKIDECK_WIKIMEDIA_NODES_PER_IP: '3',
WIKIDECK_WIKIMEDIA_DAILY_CAP: '3000' });
const client = new WikimediaClient({ fetch: async () => json({}), config });
assert.ok(Math.abs(client.status().requestsPerSecond - 0.3) < 1e-9);
assert.equal(client.status().dailyCap, 1000);
assert.equal(configFromEnv({ WIKIDECK_WIKIMEDIA_RPS: 'abc' }).requestsPerSecond, 1);
});
test('a full queue sheds background work first and a mint request evicts a background waiter', async () => {
const { clock, client } = setup(() => json({}), { queueLimit: 2, burst: 1 });
await client.request(new URL(API), { lane: 'interactive', timeoutMs: 1000 });
const b1 = client.request(new URL(API), { lane: 'background', timeoutMs: 1000 }).then(() => 'ok', e => e.message);
const b2 = client.request(new URL(API), { lane: 'background', timeoutMs: 1000 }).then(() => 'ok', e => e.message);
await assert.rejects(client.request(new URL(API), { lane: 'background', timeoutMs: 1000 }), /queue full/);
const mint = client.request(new URL(API), { lane: 'mint', timeoutMs: 1000 });
await clock.advance(5000);
await mint;
assert.deepEqual([await b1, await b2], ['ok', 'Wikimedia unavailable: queue full']);
});
test('waiting longer than the lane allows fails instead of queueing forever', async () => {
const { clock, client } = setup(() => json({}), { burst: 1, requestsPerSecond: 0.01, maxWaitMs: { interactive: 3000 } as ClientConfig['maxWaitMs'] });
await client.request(new URL(API), { lane: 'interactive', timeoutMs: 1000 });
const late = client.request(new URL(API), { lane: 'interactive', timeoutMs: 1000 }).catch(e => e);
await clock.advance(3000);
assert.match(String(await late), /queue wait/);
});
test('disk cache: ETag revalidation with If-None-Match, 304 refresh, stale-while-revalidate on the background lane', async () => {
const cache = new WikimediaCache(':memory:');
let body = { v: 1 };
const { clock, client, calls } = setup((_url, headers) => headers.get('if-none-match') === '"v1"' && body.v === 1
? new Response(null, { status: 304 }) : json(body, { ETag: `"v${body.v}"` }), {}, cache);
const policy = { ttlMs: 1000, staleWhileRevalidate: false };
const read = async (lane: 'interactive' | 'background' = 'interactive', cachePolicy = policy) =>
(await (await client.request(new URL(API), { lane, timeoutMs: 1000, cache: cachePolicy })).response.json());
assert.deepEqual(await read(), { v: 1 });
assert.deepEqual(await read(), { v: 1 });
assert.equal(calls.length, 1); // Fresh hit.
await clock.advance(1000);
assert.deepEqual(await read(), { v: 1 });
assert.equal(calls.length, 2);
assert.equal(calls[1].headers.get('if-none-match'), '"v1"'); // Conditional request, answered 304.
assert.equal(client.status().counters['cache.revalidated'], 1);
// Stale-while-revalidate: the stale copy is served at once; the refresh happens on the background lane.
await clock.advance(1000);
body = { v: 2 };
assert.deepEqual(await read('interactive', { ttlMs: 1000, staleWhileRevalidate: true }), { v: 1 });
await clock.advance(0); await flush();
assert.equal(calls.length, 3);
assert.equal(client.status().counters['lane.background'], 1);
assert.deepEqual(await read(), { v: 2 });
assert.equal(calls.length, 3);
// The key ignores maxlag and parameter order.
assert.equal(cacheKey(new URL('https://h.org/p?b=2&a=1&maxlag=5')), cacheKey(new URL('https://h.org/p?a=1&b=2')));
});
test('requests without a cache policy (mint) neither read nor write the cache', async () => {
const cache = new WikimediaCache(':memory:');
const { client, calls } = setup(() => json({ n: calls.length }), {}, cache);
await client.request(new URL(API), { lane: 'interactive', timeoutMs: 1000, cache: { ttlMs: 60_000 } });
const minted = await client.request(new URL(API), { lane: 'mint', timeoutMs: 1000 });
assert.equal(minted.fromCache, false);
assert.equal(calls.length, 2);
assert.equal(cache.size(), 1);
});
test('a stale copy is served when the upstream refuses, and non-JSON replies are never cached', async () => {
const cache = new WikimediaCache(':memory:');
let fail = false, html = false;
const { clock, client } = setup(() => fail ? new Response('', { status: 429 })
: html ? new Response('<html>', { headers: { 'Content-Type': 'text/html' } }) : json({ v: 1 }), {}, cache);
const policy = { ttlMs: 1000 };
await client.request(new URL(API), { lane: 'interactive', timeoutMs: 1000, cache: policy });
await clock.advance(2000);
fail = true;
const stale = await client.request(new URL(API), { lane: 'interactive', timeoutMs: 1000, cache: policy });
assert.equal(stale.fromCache, true);
fail = false; html = true;
await clock.advance(60_000);
await client.request(new URL(`${API}&other=1`), { lane: 'interactive', timeoutMs: 1000, cache: policy });
assert.equal(cache.size(), 1);
});
test('start-up warns when there is no operator contact', () => {
const messages: string[] = [];
assert.equal(warnIfNoContact({}, m => messages.push(m)), true);
assert.match(messages[0], /WIKIDECK_OPERATOR_CONTACT/);
assert.equal(warnIfNoContact({ WIKIDECK_OPERATOR_CONTACT: 'ops@example.org' }, m => messages.push(m)), false);
assert.equal(warnIfNoContact({ WIKIDECK_USER_AGENT: 'X/1 (+https://x; y)' }, m => messages.push(m)), false);
assert.equal(messages.length, 1);
});
test('only HTTPS is allowed', async () => {
const { client } = setup();
await assert.rejects(client.request(new URL('http://en.wikipedia.org/w/api.php'), { lane: 'mint', timeoutMs: 1 }), RangeError);
});