Files
wikideck/tests/testnet-ops-identity.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

91 lines
4.8 KiB
TypeScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// testnet-ops: card identity freeze-at-first-sight and pre-bake. Zero network calls.
import test from 'node:test';
import assert from 'node:assert/strict';
import { mkdtempSync, readFileSync, rmSync } from 'node:fs';
import { tmpdir } from 'node:os';
import { join } from 'node:path';
import { identityStoreFile, IdentityStore, buildIdentity, cachedSource, linksHash, prebake, toProfile,
type ChainArticle, type IdentitySource } from '../server/game/identity.ts';
const sample = JSON.parse(readFileSync(new URL('../docs/game/card-sample.json', import.meta.url), 'utf8')).cards as any[];
const now = () => new Date('2026-11-17T22:00:00Z');
function tmpStore() {
const dir = mkdtempSync(join(tmpdir(), 'identity-'));
return { store: new IdentityStore(join(dir, 'identity.sqlite')), done: () => rmSync(dir, { recursive: true, force: true }) };
}
const art = (c: any, i: number): ChainArticle => ({ tokenId: i.toString(16).padStart(64, '0'), pageId: c.pageId, title: c.title, revisionId: c.revisionId });
test('no fetch: pre-bake from the cached sample freezes every sample card', async () => {
const realFetch = globalThis.fetch;
globalThis.fetch = (() => { throw new Error('network call in test'); }) as typeof fetch;
const { store, done } = tmpStore();
try {
const articles = sample.map(art);
const r = await prebake(store, cachedSource(sample), articles, { now });
assert.deepEqual(r, { seen: sample.length, alreadyFrozen: 0, frozen: sample.length, unknown: 0 });
const salah = store.get(articles[sample.findIndex(c => c.title === 'Salahuddin of Aceh')].tokenId)!;
assert.equal(salah.wikidataId, 'Q4264920');
assert.deepEqual(salah.instanceOf, [{ id: 'Q5', label: 'human' }]);
assert.equal(salah.source, 'cache');
assert.ok(salah.categories.includes('Sultans of Aceh'));
assert.equal(salah.description, 'Sultan of Aceh (1530–1537/9)');
const profile = toProfile(salah);
assert.deepEqual(profile.instanceOf, [{ id: 'Q5', label: 'human' }]);
assert.equal(profile.fetchedRevisionId, 1358732737);
} finally { globalThis.fetch = realFetch; store.close(); done(); }
});
test('freeze at first sight: a later, different profile never overwrites the stored one', async () => {
const { store, done } = tmpStore();
try {
const a = art(sample[0], 1);
const first = store.freeze(buildIdentity(a, { instanceOf: [{ id: 'Q5', label: 'human' }] }, 'cache', now()));
const second = store.freeze(buildIdentity(a, { instanceOf: [{ id: 'Q515', label: 'city' }] }, 'wikimedia', new Date()));
assert.deepEqual(second, first);
assert.equal(store.get(a.tokenId)!.instanceOf[0].id, 'Q5');
const again = await prebake(store, cachedSource(sample), [a, a], { now });
assert.deepEqual(again, { seen: 1, alreadyFrozen: 1, frozen: 0, unknown: 0 });
} finally { store.close(); done(); }
});
test('unknown pages are not frozen, so a later run can retry them', async () => {
const { store, done } = tmpStore();
try {
const stranger: ChainArticle = { tokenId: 'f'.repeat(64), pageId: 999999999, title: 'Not in sample', revisionId: 1 };
const r = await prebake(store, cachedSource(sample), [stranger], { now });
assert.deepEqual(r, { seen: 1, alreadyFrozen: 0, frozen: 0, unknown: 1 });
assert.equal(store.has(stranger.tokenId), false);
} finally { store.close(); done(); }
});
test('sources are asked in bounded batches and identities persist across reopen', async () => {
const dir = mkdtempSync(join(tmpdir(), 'identity-'));
try {
const calls: number[] = [];
const source: IdentitySource = { kind: 'wikimedia', async lookup(batch) {
calls.push(batch.length);
return new Map(batch.map(a => [a.pageId, { links: [7, 3, 3, 1.5], coords: [1, 2] as [number, number], cites: ['Q30', 'bad'], years: [1948, -44], died: true }]));
} };
let store = new IdentityStore(join(dir, 'id.sqlite'));
await prebake(store, source, sample.slice(0, 12).map(art), { batchSize: 5, now });
assert.deepEqual(calls, [5, 5, 2]);
store.close();
store = new IdentityStore(join(dir, 'id.sqlite'));
const one = store.get(art(sample[3], 3).tokenId)!;
assert.deepEqual(one.links, [3, 7]);
assert.equal(one.linksHash, linksHash([3, 7]));
assert.deepEqual(one.cites, ['Q30']);
assert.deepEqual(one.years, [-44, 1948]);
assert.equal(one.died, true);
assert.deepEqual(one.coords, [1, 2]);
assert.equal(one.mintRevisionId, sample[3].revisionId);
assert.equal(store.count(), 12);
store.close();
} finally { rmSync(dir, { recursive: true, force: true }); }
});
test('the store file lives under WIKIDECK_DATA_DIR, next to the ledger', () => {
assert.equal(identityStoreFile({ WIKIDECK_DATA_DIR: '/srv/wd' }), join('/srv/wd', 'card-identity.sqlite'));
assert.ok(identityStoreFile({}).endsWith(join('data', 'card-identity.sqlite')));
});