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

75 lines
3.8 KiB
TypeScript

import test from 'node:test';
import assert from 'node:assert/strict';
import { MintTemplatePool } from '../server/wikimedia/pool.ts';
import type { Article } from '../shared/protocol.ts';
import type { PageEnrichment } from '../shared/enrichment.ts';
function article(pageId: number): Article {
return { pageId, title: `Page ${pageId}`, extract: 'An extract that is comfortably longer than forty characters.',
image: 'https://upload.wikimedia.org/a.jpg', url: `https://en.wikipedia.org/?curid=${pageId}`, revisionId: pageId + 1,
source: 'live', category: 'Culture' };
}
function enrichment(pageId: number): PageEnrichment {
return { pageId, title: `Page ${pageId}`, description: null, wikidataId: null, revisionId: pageId + 1, pageBytes: 10,
languageCount: 1, languagesTruncated: false, categories: [], categoriesTruncated: false, fetchedAt: '2026-11-17T00:00:00Z',
pageviews: { status: 'available', months: [], latestMonth: '2026-10', total: 1, dailyAverage: 1, trendPercent: null },
stats: { attention: 1, depth: 1, reach: 1 } };
}
function fixture(options: { size?: number; maxAgeMs?: number } = {}) {
let next = 1, now = 0, fail = false;
const log: string[] = [];
const sources = {
articles: { random: async () => { log.push('random'); if (fail) throw new Error('Wikimedia unavailable: HTTP 429'); return article(next++); } },
enrichment: { getFresh: async (id: number) => { log.push(`fresh:${id}`); return enrichment(id); } },
};
const pool = new MintTemplatePool(sources, { now: () => now, ...options });
return { pool, log, advance: (ms: number) => { now += ms; }, setFail: (value: boolean) => { fail = value; } };
}
test('every template on one tip shares one article; a new tip gets a different one', async () => {
const { pool, log } = fixture({ size: 1 });
const first = await Promise.all(Array.from({ length: 20 }, () => pool.take('tip-a')));
assert.ok(first.every(pair => pair.article.pageId === first[0].article.pageId));
await pool.refill();
const requestsForTwentyMiners = log.length;
assert.equal(requestsForTwentyMiners, 4); // One direct pair, one prefetched pair: 2 calls each.
const second = await pool.take('tip-b');
assert.notEqual(second.article.pageId, first[0].article.pageId);
assert.equal(second.article.pageId, 2); // Served from the prefetched pair, no wait.
await pool.refill();
assert.equal(log.length, 6); // ...and that use alone triggers the next prefetch.
// The stats snapshot comes from a mint-path read of the same page.
assert.equal(second.statsSnapshot.revisionId, second.article.pageId + 1);
});
test('pairs older than maxAge are dropped, never served', async () => {
const { pool, advance } = fixture({ size: 1, maxAgeMs: 1000 });
const a = await pool.take('t1');
await pool.refill();
advance(1000);
const b = await pool.take('t1'); // Same tip, but its pair expired; the ready pair expired too.
assert.notEqual(b.article.pageId, a.article.pageId);
assert.equal(pool.status().expired, 1);
});
test('an idle pool makes no requests, and a failure rejects the template without a retry loop', async () => {
const { pool, log, setFail } = fixture({ size: 2 });
assert.equal(log.length, 0);
setFail(true);
await assert.rejects(pool.take('t1'), /429/);
await pool.refill();
assert.equal(log.filter(entry => entry === 'random').length, 2); // The direct attempt and one refill attempt.
setFail(false);
assert.ok((await pool.take('t1')).article.pageId > 0);
});
test('callers get copies they cannot mutate', async () => {
const { pool } = fixture();
const a = await pool.take('t');
a.article.title = 'changed';
assert.equal((await pool.take('t')).article.title.startsWith('Page'), true);
assert.throws(() => new MintTemplatePool({ articles: { random: async () => article(1) },
enrichment: { getFresh: async () => enrichment(1) } }, { size: 99 }), RangeError);
});