forked from WikiDeck/wikideck
257 lines
15 KiB
TypeScript
257 lines
15 KiB
TypeScript
import { test } from 'node:test';
|
||
import assert from 'node:assert/strict';
|
||
import { readFileSync, readdirSync } from 'node:fs';
|
||
import {
|
||
BANNED_WORDS, FOOTER, LOG_TEMPLATES, gloss, logParagraphs, longDate, recap, renderLog,
|
||
type CardType, type MatchRecord, type StoryCard, type StoryEvent,
|
||
} from '../shared/duel/story/index.ts';
|
||
|
||
// Real cards from docs/game/card-sample.json (no network). Types, costs and Portals are the
|
||
// DUEL_DESIGN.md §9 worked examples.
|
||
const sample = JSON.parse(readFileSync(new URL('../docs/game/card-sample.json', import.meta.url), 'utf8')) as {
|
||
cards: { title: string; description: string | null; revisionId: number; pageId: number }[];
|
||
};
|
||
const byTitle = new Map(sample.cards.map(c => [c.title, c]));
|
||
function card(id: string, title: string, type: CardType, cost: number, portal: string, owner: 0 | 1): StoryCard {
|
||
const c = byTitle.get(title);
|
||
if (!c) throw new Error(`not in sample: ${title}`);
|
||
return { id, title, description: c.description ?? '', type, cost, portal, revisionId: c.revisionId, tokenId: `tok-${c.pageId}`, owner };
|
||
}
|
||
const cards: Record<string, StoryCard> = Object.fromEntries([
|
||
card('a1', 'Dichromorpha viridis', 'Figure', 2, 'Earth', 0),
|
||
card('a2', 'Pathrail Mosque', 'Location', 2, 'Society', 0),
|
||
card('a3', 'Zbyszek Godlewski', 'Figure', 5, 'History', 0),
|
||
card('a4', 'Monoid', 'Doctrine', 6, 'Science', 0),
|
||
card('a5', 'Tom Ewell', 'Figure', 6, 'Arts', 0),
|
||
card('b1', '1954 Gold Coast general election', 'Event', 3, 'Society', 1),
|
||
card('b2', "April 2021 nor'easter", 'Event', 3, 'Earth', 1),
|
||
card('b3', 'Marc Fesneau', 'Figure', 5, 'Society', 1),
|
||
card('b4', "Pallas's cat", 'Figure', 6, 'Earth', 1),
|
||
card('b5', 'Heart rot', 'Doctrine', 5, 'Science', 1),
|
||
card('b6', '2025 Tehreek-e-Labbaik Pakistan protests', 'Event', 5, 'Society', 1),
|
||
card('b7', 'Tatsunoko Production', 'Institution', 6, 'Arts', 1),
|
||
card('b8', 'Agda (programming language)', 'Doctrine', 6, 'Science', 1),
|
||
].map(c => [c.id, c]));
|
||
|
||
const T = (turn: number): StoryEvent => ({ kind: 'turnStart', turn, seat: turn % 2 === 1 ? 0 : 1 });
|
||
const events: StoryEvent[] = [
|
||
T(1), { kind: 'deploy', seat: 0, card: 'a2' },
|
||
T(2), { kind: 'draw', seat: 1, card: 'b1' },
|
||
T(3), { kind: 'draw', seat: 0, card: 'a1' }, { kind: 'deploy', seat: 0, card: 'a1' },
|
||
T(4), { kind: 'draw', seat: 1 }, { kind: 'cast', seat: 1, card: 'b1' },
|
||
{ kind: 'tokenCreated', seat: 1, source: 'b1', token: 'Elected Member', count: 2 },
|
||
T(5), { kind: 'draw', seat: 0 }, { kind: 'attack', seat: 0, card: 'a1', power: 4, toughness: 2 },
|
||
{ kind: 'playerDamage', seat: 1, source: 'a1', amount: 4, life: 16 },
|
||
T(6), { kind: 'draw', seat: 1 }, { kind: 'cast', seat: 1, card: 'b2' },
|
||
{ kind: 'effectDamage', source: 'b2', target: 'a1', amount: 1 },
|
||
{ kind: 'effectDamage', source: 'b2', target: 'a2', amount: 1 }, { kind: 'destroyed', card: 'a1' },
|
||
T(7), { kind: 'draw', seat: 0 }, { kind: 'deploy', seat: 0, card: 'a3' },
|
||
T(8), { kind: 'draw', seat: 1 }, { kind: 'deploy', seat: 1, card: 'b3' },
|
||
T(9), { kind: 'draw', seat: 0 }, { kind: 'attack', seat: 0, card: 'a3', power: 6, toughness: 3 },
|
||
{ kind: 'block', seat: 1, blocker: 'b3', attacker: 'a3', power: 5, toughness: 4 },
|
||
{ kind: 'destroyed', card: 'a3' }, { kind: 'destroyed', card: 'b3' }, { kind: 'draw', seat: 0 },
|
||
T(10), { kind: 'draw', seat: 1 }, { kind: 'discard', seat: 1, card: 'b8' }, { kind: 'deploy', seat: 1, card: 'b4' },
|
||
T(11), { kind: 'draw', seat: 0 }, { kind: 'deploy', seat: 0, card: 'a5' },
|
||
T(12), { kind: 'draw', seat: 1 }, { kind: 'adopt', seat: 1, doctrine: 'b5', figure: 'a5', power: 2, toughness: 2 },
|
||
{ kind: 'attack', seat: 1, card: 'b4', power: 5, toughness: 5 }, { kind: 'playerDamage', seat: 0, source: 'b4', amount: 5, life: 15 },
|
||
T(13), { kind: 'draw', seat: 0 }, { kind: 'adopt', seat: 0, doctrine: 'a4', figure: 'a5', power: 5, toughness: 5 },
|
||
{ kind: 'attack', seat: 0, card: 'a5', power: 5, toughness: 5 }, { kind: 'playerDamage', seat: 1, source: 'a5', amount: 5, life: 11 },
|
||
T(14), { kind: 'draw', seat: 1 }, { kind: 'cast', seat: 1, card: 'b6' },
|
||
{ kind: 'effectDamage', source: 'b6', target: 'a5', amount: 3 }, { kind: 'deploy', seat: 1, card: 'b7' },
|
||
{ kind: 'attack', seat: 1, card: 'b4', power: 5, toughness: 5 }, { kind: 'playerDamage', seat: 0, source: 'b4', amount: 5, life: 10 },
|
||
T(15), { kind: 'draw', seat: 0 }, { kind: 'attack', seat: 0, card: 'a5', power: 5, toughness: 5 },
|
||
{ kind: 'playerDamage', seat: 1, source: 'a5', amount: 5, life: 6 },
|
||
T(16), { kind: 'draw', seat: 1 }, { kind: 'attack', seat: 1, card: 'b4', power: 5, toughness: 5 },
|
||
{ kind: 'playerDamage', seat: 0, source: 'b4', amount: 5, life: 5 },
|
||
T(17), { kind: 'draw', seat: 0 }, { kind: 'attack', seat: 0, card: 'a5', power: 5, toughness: 5 },
|
||
{ kind: 'playerDamage', seat: 1, source: 'a5', amount: 5, life: 1 },
|
||
T(18), { kind: 'draw', seat: 1 }, { kind: 'attack', seat: 1, card: 'b4', power: 5, toughness: 5 },
|
||
{ kind: 'playerDamage', seat: 0, source: 'b4', amount: 5, life: 0 },
|
||
{ kind: 'gameOver', winner: 1, reason: 'life', turn: 18 },
|
||
];
|
||
const record: MatchRecord = {
|
||
matchId: 'golden-1', seed: 'golden-seed', date: '2026-11-17',
|
||
players: [{ name: 'alice' }, { name: 'bob' }], cards, events, hostName: 'testnet-r350',
|
||
};
|
||
|
||
test('recap: title, infobox and footer follow DUEL_DESIGN §8.3', () => {
|
||
const r = recap(record);
|
||
assert.equal(r.title, 'Battle of Pathrail Mosque (2026)'); // loser's Location; 9 turns each → "Battle of"
|
||
assert.equal(r.url, '/recap/golden-1');
|
||
assert.equal(r.masthead, 'WikiDeck');
|
||
assert.deepEqual(r.infobox, {
|
||
date: '17 November 2026',
|
||
location: 'Pathrail Mosque',
|
||
result: 'Pyrrhic bob victory',
|
||
belligerents: ['alice', 'bob'],
|
||
commanders: ['Tom Ewell', "Pallas's cat"],
|
||
strength: ['5 cards', '7 cards'],
|
||
casualties: ['2 cards destroyed: Dichromorpha viridis, Zbyszek Godlewski', '1 card destroyed: Marc Fesneau'],
|
||
});
|
||
assert.equal(r.footer, FOOTER);
|
||
assert.match(r.lead, /^The \*\*Battle of Pathrail Mosque \(2026\)\*\* .*17 November 2026.*alice.*bob.*eighteen turns.*a Pyrrhic victory for bob\.\[1\]/);
|
||
assert.match(r.lead, /\*Dichromorpha viridis\* \(Species of grasshopper\).* third turn\.\[2\]$/);
|
||
assert.deepEqual(r.sections.map(s => s.heading), ['Background', 'Course of the battle', 'Aftermath']);
|
||
assert.match(r.sections[2].paragraphs[0], /^bob ended the engagement with 1 Notability; alice ended with 0\.$/);
|
||
});
|
||
|
||
test('recap: references cite every card played with its revision id, in first-mention order', () => {
|
||
const r = recap(record);
|
||
assert.equal(r.references.length, 12);
|
||
assert.deepEqual(r.references.map(x => x.n), r.references.map((_, i) => i + 1));
|
||
assert.equal(r.references[0].title, 'Pathrail Mosque');
|
||
for (const ref of r.references) {
|
||
const c = cards[ref.cardId];
|
||
assert.equal(ref.revisionId, c.revisionId);
|
||
assert.ok(ref.url.endsWith(`&oldid=${c.revisionId}`), ref.url);
|
||
assert.ok(ref.url.startsWith('https://en.wikipedia.org/w/index.php?title='));
|
||
assert.match(ref.text, new RegExp(`Revision ${c.revisionId}\\. Minted as token tok-`));
|
||
}
|
||
// every [n] used in the text exists
|
||
const text = [r.lead, ...r.sections.flatMap(s => s.paragraphs)].join(' ');
|
||
for (const m of text.matchAll(/\[(\d+)\]/g)) assert.ok(Number(m[1]) <= r.references.length);
|
||
});
|
||
|
||
test('log: golden lines for the omniscient view', () => {
|
||
const { lines, references } = renderLog(record, null);
|
||
const paras = logParagraphs(lines);
|
||
assert.equal(paras.length, 18);
|
||
assert.equal(lines[0].text, 'Turn 1.');
|
||
assert.match(lines[1].text, /\*Pathrail Mosque\* \(Mosque in Faridpur, Bangladesh\).*\[1\]$/);
|
||
assert.ok(lines.some(l => /\*Zbyszek Godlewski\* \(Polish protester, 1952–1970\)/.test(l.text)));
|
||
assert.ok(lines.some(l => /two Elected Members/.test(l.text)));
|
||
assert.match(lines[lines.length - 1].text, /alice/);
|
||
// later mentions carry no gloss and no new citation
|
||
const later = lines.filter(l => l.text.includes('*Tom Ewell*'));
|
||
assert.ok(later.length >= 3);
|
||
assert.equal(later.filter(l => l.text.includes('American actor')).length, 1);
|
||
assert.equal(references.length, 13); // 12 played + the discarded Agda
|
||
// snapshot of the whole log text (golden): regenerate deliberately if templates change
|
||
assert.equal(paras.join('\n').length > 1500, true);
|
||
assert.deepEqual(renderLog(record, null), { lines, references });
|
||
});
|
||
|
||
test('log: logFor hides the opponent\'s draws', () => {
|
||
const forAlice = renderLog(record, 0).lines.map(l => l.text);
|
||
const forBob = renderLog(record, 1).lines.map(l => l.text);
|
||
const turn2Alice = forAlice[forAlice.indexOf('Turn 2.') + 1];
|
||
const turn2Bob = forBob[forBob.indexOf('Turn 2.') + 1];
|
||
assert.doesNotMatch(turn2Alice, /Gold Coast/);
|
||
assert.match(turn2Alice, /^bob (drew a card|drew one card|added a card to their hand)\.$/);
|
||
assert.match(turn2Bob, /Gold Coast/);
|
||
// alice sees her own draw of Dichromorpha viridis by name
|
||
assert.ok(forAlice.some(t => /alice.*Dichromorpha viridis|Dichromorpha viridis.*alice/.test(t) && /drew|drawn|hand/.test(t)));
|
||
});
|
||
|
||
test('log: discards are public (Deleted is a public zone)', () => {
|
||
const forAlice = renderLog(record, 0).lines.map(l => l.text);
|
||
assert.ok(forAlice.some(t => /Agda \(programming language\)/.test(t) && /discard|Deleted/.test(t)));
|
||
});
|
||
|
||
test('determinism: same seed gives the same text; another seed varies it', () => {
|
||
assert.deepEqual(recap(record), recap(structuredClone(record)));
|
||
const other = recap({ ...record, seed: 'another-seed' });
|
||
const a = renderLog(record).lines.map(l => l.text).join('\n');
|
||
const b = renderLog({ ...record, seed: 'another-seed' }).lines.map(l => l.text).join('\n');
|
||
assert.notEqual(a, b);
|
||
assert.equal(other.title, recap(record).title); // the name is data, not a variant
|
||
});
|
||
|
||
test('recap naming: Event fallback, Figure campaign, prefixes, and the decisive and withdrawal results', () => {
|
||
const base = { ...record, cards };
|
||
const short: MatchRecord = { ...base, events: [
|
||
T(1), { kind: 'cast', seat: 0, card: 'b6' }, { kind: 'deploy', seat: 0, card: 'a3' },
|
||
T(2), { kind: 'gameOver', winner: 0, reason: 'concede', turn: 2 },
|
||
] };
|
||
const r1 = recap(short);
|
||
assert.equal(r1.title, 'Skirmish at 2025 Tehreek-e-Labbaik Pakistan protests (2026)');
|
||
assert.equal(r1.infobox.result, 'alice victory by withdrawal');
|
||
assert.equal(r1.infobox.location, 'WikiDeck host testnet-r350');
|
||
const figOnly: MatchRecord = { ...base, events: [
|
||
T(1), { kind: 'deploy', seat: 1, card: 'b3' },
|
||
T(2), { kind: 'playerDamage', seat: 0, amount: 20, life: 0 }, { kind: 'gameOver', winner: 1, reason: 'life', turn: 2 },
|
||
] };
|
||
const r2 = recap(figOnly);
|
||
assert.equal(r2.title, 'Marc Fesneau campaign (2026)');
|
||
assert.equal(r2.infobox.result, 'Decisive bob victory');
|
||
const empty: MatchRecord = { ...base, events: [T(1), { kind: 'gameOver', winner: 1, reason: 'timeout', turn: 1 }] };
|
||
assert.equal(recap(empty).title, 'WikiDeck match of 17 November 2026');
|
||
});
|
||
|
||
test('tone lint: templates are deadpan (DUEL_DESIGN §8.1, critic S7)', () => {
|
||
for (const [key, variants] of Object.entries(LOG_TEMPLATES)) {
|
||
assert.ok(variants.length >= 3 && variants.length <= 4, `${key} has ${variants.length} variants`);
|
||
for (const v of variants) {
|
||
assert.doesNotMatch(v, /!/, `${key}: no exclamation marks`);
|
||
for (const w of BANNED_WORDS) assert.doesNotMatch(v.toLowerCase(), new RegExp(`\\b${w}\\b`), `${key}: banned word ${w}`);
|
||
assert.doesNotMatch(v, /[\u{1F300}-\u{1FAFF}]/u, `${key}: no emoji`);
|
||
}
|
||
}
|
||
const r = recap(record);
|
||
const prose = [r.lead, ...r.sections.flatMap(s => s.paragraphs)].join(' ')
|
||
.replace(/\*[^*]+\*/g, 'CARD'); // card titles are data; lint only our words
|
||
for (const w of BANNED_WORDS) assert.doesNotMatch(prose.toLowerCase(), new RegExp(`\\b${w}\\b`));
|
||
assert.doesNotMatch(prose, /!/);
|
||
assert.doesNotMatch(prose, /Wikipedia|Wikimedia/); // branding: only in references and the footer
|
||
});
|
||
|
||
test('every sample description glosses without throwing; dates format', () => {
|
||
for (const c of sample.cards) assert.equal(typeof gloss(c.description ?? ''), 'string');
|
||
assert.equal(gloss('American actor (1909–1994)'), 'American actor, 1909–1994');
|
||
assert.equal(gloss(''), '');
|
||
assert.equal(longDate('2026-01-05'), '5 January 2026');
|
||
});
|
||
|
||
test('story code is pure: no network or clock access', () => {
|
||
const dir = new URL('../shared/duel/story/', import.meta.url);
|
||
for (const f of readdirSync(dir)) {
|
||
const src = readFileSync(new URL(f, dir), 'utf8');
|
||
assert.doesNotMatch(src, /\bfetch\(|XMLHttpRequest|node:http|Date\.now|new Date\(|Math\.random/, f);
|
||
}
|
||
});
|
||
|
||
test('golden: full log and recap prose match tests/duel-design-story.golden.json', () => {
|
||
// To regenerate after a deliberate template change, dump { log, lead, sections } and review the diff.
|
||
const golden = JSON.parse(readFileSync(new URL('./duel-design-story.golden.json', import.meta.url), 'utf8'));
|
||
const r = recap(record);
|
||
assert.deepEqual({ log: logParagraphs(renderLog(record).lines), lead: r.lead, sections: r.sections }, golden);
|
||
});
|
||
|
||
test('tokens are named plainly and never cited; draws are recorded as inconclusive', () => {
|
||
const withToken: MatchRecord = {
|
||
...record,
|
||
cards: { ...cards, 't:1': { id: 't:1', title: 'Elected Member', type: 'Figure', cost: 0, portal: 'Society', owner: 1, token: true } },
|
||
events: [
|
||
T(1), { kind: 'cast', seat: 1, card: 'b1' },
|
||
{ kind: 'tokenCreated', seat: 1, source: 'b1', token: 'Elected Member', count: 1 },
|
||
T(2), { kind: 'attack', seat: 1, card: 't:1', power: 1, toughness: 1 },
|
||
{ kind: 'playerDamage', seat: 0, source: 't:1', amount: 20, life: 0 },
|
||
{ kind: 'playerDamage', seat: 1, amount: 20, life: 0 },
|
||
{ kind: 'gameOver', winner: null, reason: 'life', turn: 2 },
|
||
],
|
||
};
|
||
const { lines, references } = renderLog(withToken);
|
||
assert.ok(lines.some(l => /Elected Member \(1\/1\)/.test(l.text) && !/\*Elected Member\*/.test(l.text)));
|
||
assert.deepEqual(references.map(r => r.title), ['1954 Gold Coast general election']);
|
||
assert.match(lines[lines.length - 1].text, /draw|No winner/);
|
||
const r = recap(withToken);
|
||
assert.equal(r.infobox.result, 'Inconclusive');
|
||
assert.match(r.lead, /ended in a draw\./);
|
||
assert.ok(r.references.every(x => x.revisionId !== undefined));
|
||
});
|
||
|
||
test('final Notability: lifeGain is tracked, and gameOver.life wins when present', () => {
|
||
const ev: StoryEvent[] = [
|
||
T(1), { kind: 'deploy', seat: 0, card: 'a2' },
|
||
{ kind: 'playerDamage', seat: 0, amount: 18, life: 2 },
|
||
{ kind: 'lifeGain', seat: 0, source: 'a2', amount: 10, life: 12 },
|
||
{ kind: 'playerDamage', seat: 1, amount: 20, life: 0 },
|
||
{ kind: 'gameOver', winner: 0, reason: 'life', turn: 1 },
|
||
];
|
||
const r = recap({ ...record, events: ev });
|
||
assert.equal(r.infobox.result, 'Decisive alice victory'); // 12 vs 0, not Pyrrhic at 2
|
||
assert.ok(renderLog({ ...record, events: ev }).lines.some(l => /Notability/.test(l.text) && /12/.test(l.text)));
|
||
const withFinal = recap({ ...record, events: [...ev.slice(0, -1), { kind: 'gameOver', winner: 0, reason: 'life', turn: 1, life: [3, 0] }] });
|
||
assert.equal(withFinal.infobox.result, 'Pyrrhic alice victory');
|
||
});
|