// Keyword and Portal unit tests (DESIGN §10.2, §10.3 — PR #17 @ cf2fceb). Owner: engine-b. import test from 'node:test'; import assert from 'node:assert/strict'; import { DEFAULT_RULESET, FACTION_KEYWORD, KEYWORDS, OPTIONAL_KEYWORDS, PORTALS, V1_KEYWORDS, hiddenFrom, isOnTopic, parseRuleset, } from '../shared/game/index.ts'; import { board, card, cardPower, ctxFor, endOfTurn, place, reveal, topic, atScoring } from './game/build.ts'; test('registry: ids match keys, every entry has name and text, v1 set is one keyword per faction', () => { for (const [id, def] of [...Object.entries(KEYWORDS), ...Object.entries(PORTALS)]) { assert.equal(def.id, id); assert.ok(def.name && def.text, `${id} needs name and text`); } assert.deepEqual([...V1_KEYWORDS].sort(), ['encyclopedic', 'local', 'nostalgic', 'rising', 'translated', 'viral']); assert.equal(new Set(Object.values(FACTION_KEYWORD)).size, 6); for (const id of [...V1_KEYWORDS, ...OPTIONAL_KEYWORDS]) assert.ok(KEYWORDS[id], `${id} implemented`); }); test('the default ruleset (topic pool and params) validates', () => { assert.doesNotThrow(() => parseRuleset(DEFAULT_RULESET)); for (const t of DEFAULT_RULESET.topicPool) if (t.portal) assert.equal(t.portal, t.id, 'portal id = topic id'); assert.equal(DEFAULT_RULESET.topicPool.length, 9); }); // ---- viral ---- test('viral: +1 per other own card revealed earlier this turn, any Topic, capped at +2', () => { const s = board(); place(s, 0, 0, card(), s.turn - 1); // an older card does not count place(s, 1, 1, card()); // nor do the opponent's assert.equal(reveal(s, 0, 1, card({ keyword: 'viral' })).bonus, 0); place(s, 0, 2, card()); // two earlier this turn now (the first viral card and this one) → +2 assert.equal(reveal(s, 0, 2, card({ keyword: 'viral' })).bonus, 2); for (let i = 0; i < 3; i++) place(s, 0, 0, card()); assert.equal(reveal(s, 0, 0, card({ keyword: 'viral' })).bonus, 2, 'cap'); }); test('viral: the §12 example (Mars then Jazz → +1)', () => { const s = board(); place(s, 1, 2, card({ title: 'Mars' })); assert.equal(reveal(s, 1, 1, card({ title: 'Jazz', keyword: 'viral' })).bonus, 1); }); // ---- rising ---- test('rising: +1 at each end of turn, stopping after +2 in total', () => { const s = board(); const r = place(s, 0, 0, card({ power: 2, keyword: 'rising' })); const seen: number[] = []; for (let i = 0; i < 4; i++) { endOfTurn(s, r); seen.push(r.bonus); s.turn++; } assert.deepEqual(seen, [1, 2, 2, 2]); }); test('rising: other bonuses do not count toward its cap', () => { const s = board(); const r = place(s, 0, 0, card({ keyword: 'rising' })); r.bonus = 5; endOfTurn(s, r); s.turn++; endOfTurn(s, r); s.turn++; endOfTurn(s, r); assert.equal(r.bonus, 7); }); test('rising: respects keywordParams amount and cap', () => { const s = board(); s.ruleset.keywordParams.rising = { amount: 2, cap: 3 }; const r = place(s, 0, 0, card({ keyword: 'rising' })); endOfTurn(s, r); s.turn++; endOfTurn(s, r); s.turn++; endOfTurn(s, r); assert.equal(r.bonus, 3); }); // ---- local ---- test('local: +1 only while on-topic, on top of the on-topic bonus', () => { const s = board(); const on = place(s, 0, 1, card({ cost: 4, power: 6, home: 'attention', keyword: 'local' })); const off = place(s, 0, 0, card({ cost: 4, power: 6, home: 'attention', keyword: 'local' })); const plain = place(s, 1, 1, card({ cost: 4, power: 6, home: 'attention' })); assert.equal(cardPower(s, on) - cardPower(s, plain), 1); assert.equal(cardPower(s, off), 6); assert.equal(KEYWORDS.local.ongoing!(ctxFor(s, {}, 1), on, plain), 0, 'only buffs itself'); }); // ---- encyclopedic ---- test('encyclopedic: +2 only into a completely empty Topic (either side)', () => { const s = board(); assert.equal(reveal(s, 0, 0, card({ keyword: 'encyclopedic' })).bonus, 2); place(s, 1, 1, card()); assert.equal(reveal(s, 0, 1, card({ keyword: 'encyclopedic' })).bonus, 0, 'opponent card here'); place(s, 0, 2, card()); assert.equal(reveal(s, 0, 2, card({ keyword: 'encyclopedic' })).bonus, 0, 'own card here'); }); // ---- nostalgic ---- test('nostalgic: +2 when strictly losing, counted before this card is added', () => { const s = board(); place(s, 0, 2, card({ power: 3, home: 'attention' })); place(s, 1, 2, card({ power: 9, home: 'attention' })); const n = reveal(s, 0, 2, card({ power: 8, home: 'depth', keyword: 'nostalgic' })); assert.equal(n.bonus, 2); assert.equal(s.lanes[2].cards[0].indexOf(n), 1, 'card is back in place, in reveal order'); }); test('nostalgic: a tie is not losing, and neither is an empty Topic', () => { const s = board(); place(s, 0, 2, card({ power: 4 })); place(s, 1, 2, card({ power: 4 })); assert.equal(reveal(s, 0, 2, card({ keyword: 'nostalgic' })).bonus, 0); assert.equal(reveal(s, 1, 0, card({ keyword: 'nostalgic' })).bonus, 0); }); // ---- translated ---- test('translated: +1 to your most recently revealed card in each other Topic', () => { const s = board(); const first = place(s, 0, 0, card()); const latest = place(s, 0, 0, card()); const enemy = place(s, 1, 2, card()); const t = reveal(s, 0, 1, card({ keyword: 'translated' })); assert.deepEqual([first.bonus, latest.bonus, enemy.bonus, t.bonus], [0, 1, 0, 0]); }); test('translated: the §12 example (Jupiter → Jazz in T2 and Pop music in T3)', () => { const s = board(); const haiku = place(s, 1, 1, card({ title: 'Haiku' })); const jazz = place(s, 1, 1, card({ title: 'Jazz' })); const mars = place(s, 1, 2, card({ title: 'Mars' })); const pop = place(s, 1, 2, card({ title: 'Pop music' })); reveal(s, 1, 0, card({ title: 'Jupiter', keyword: 'translated' })); assert.deepEqual([haiku.bonus, jazz.bonus, mars.bonus, pop.bonus], [0, 1, 0, 1]); }); // ---- optional keywords ---- test('edition: +2 per other copy of the same pageId on your side (when enabled)', () => { const s = board(); const a0 = place(s, 0, 1, card({ pageId: 41, power: 5, home: 'reach', keyword: 'edition' })); place(s, 0, 2, card({ pageId: 41, power: 5, home: 'depth', keyword: 'edition' })); assert.equal(cardPower(s, a0), 5, 'disabled by default'); s.ruleset.keywordsEnabled = [...s.ruleset.keywordsEnabled, 'edition']; assert.equal(cardPower(s, a0), 7); s.lanes[1].cards[0] = []; s.lanes[2].cards[0] = []; const a = place(s, 0, 0, card({ pageId: 42, power: 5, home: 'reach', keyword: 'edition' })); assert.equal(cardPower(s, a), 5); place(s, 0, 2, card({ pageId: 42, power: 5, home: 'depth', keyword: 'edition' })); place(s, 1, 0, card({ pageId: 42 })); assert.equal(cardPower(s, a), 7); }); test('wildcard: its home stat becomes the Topic stat on reveal', () => { const s = board(); s.ruleset.keywordsEnabled = [...s.ruleset.keywordsEnabled, 'wildcard']; const w = reveal(s, 0, 2, card({ home: 'attention', keyword: 'wildcard' })); assert.equal(w.card.home, 'reach'); assert.ok(isOnTopic(s, w)); }); test('obscure: hidden only from the opponent, only while running, only when enabled', () => { const s = board(); const o = place(s, 0, 1, card({ keyword: 'obscure' })); (s.ruleset as unknown as { keywordsEnabled: string[] }).keywordsEnabled = ['obscure']; assert.equal(hiddenFrom(s, o, 1), true); assert.equal(hiddenFrom(s, o, 0), false); (s.ruleset as unknown as { keywordsEnabled: string[] }).keywordsEnabled = [...V1_KEYWORDS]; assert.equal(hiddenFrom(s, o, 1), false, 'disabled by default'); (s.ruleset as unknown as { keywordsEnabled: string[] }).keywordsEnabled = ['obscure']; s.phase = 'over'; assert.equal(hiddenFrom(s, o, 1), false); }); // ---- Portals (§10.3) ---- test('isOnTopic: home stat matches the Topic stat; Disambiguation makes everything on-topic once revealed', () => { const s = board([topic('depth'), topic('reach', 'disambiguation'), topic('reach')]); assert.ok(isOnTopic(s, place(s, 0, 0, card({ home: 'depth' })))); assert.ok(!isOnTopic(s, place(s, 0, 2, card({ home: 'depth' })))); const d = place(s, 0, 1, card({ home: 'attention' })); assert.ok(isOnTopic(s, d)); s.lanes[1].portalRevealed = false; assert.ok(!isOnTopic(s, d), 'Portal not active while face down'); }); test('Main Page: +1 per card at scoring', () => { const s = board([topic('depth'), topic('attention', 'main-page'), topic('reach')]); place(s, 0, 1, card()); place(s, 0, 1, card()); place(s, 1, 1, card()); assert.deepEqual(atScoring(s, 1), [2, 1]); }); test('Trending: trend arrows count double', () => { const s = board([topic('depth'), topic('attention', 'trending'), topic('reach')]); const up = place(s, 0, 1, card({ trend: 1 })); const down = place(s, 1, 1, card({ trend: -1 })); const flat = place(s, 1, 1, card({ trend: 0 })); const portal = PORTALS.trending; const ctx = ctxFor(s, s.ruleset.portalParams.trending, 1); assert.deepEqual([portal.ongoing!(ctx, up), portal.ongoing!(ctx, down), portal.ongoing!(ctx, flat)], [1, -1, 0]); }); test('Featured Article: +2 to the owner of the single highest card at scoring; tie → nobody', () => { const s = board([topic('depth', 'featured-article'), topic('attention'), topic('reach')]); place(s, 0, 0, card({ power: 6, home: 'reach' })); const bo = place(s, 1, 0, card({ power: 5, home: 'reach' })); assert.deepEqual(atScoring(s, 0), [2, 0]); bo.bonus = 1; assert.deepEqual(atScoring(s, 0), [0, 0], 'tied highest'); bo.bonus = 3; assert.deepEqual(atScoring(s, 0), [0, 2]); assert.deepEqual(atScoring(board([topic('depth', 'featured-article')]), 0), [0, 0], 'empty'); }); test('Stub: lane capacity 2', () => { const s = board([topic('depth', 'stub'), topic('attention'), topic('reach')]); assert.equal(PORTALS.stub.capacity!(ctxFor(s, s.ruleset.portalParams.stub, 0)), 2); }); test('Interwiki: +1 Topic power to a player with a card in every Topic', () => { const s = board([topic('depth'), topic('attention'), topic('reach', 'interwiki')]); for (const lane of [0, 1, 2]) place(s, 0, lane, card()); place(s, 1, 2, card()); assert.deepEqual(atScoring(s, 2), [1, 0]); assert.deepEqual(atScoring(s, 2), [1, 0]); }); test('no keyword or Portal hook moves or removes cards (§10 v1 constraint)', () => { const s = board([topic('depth', 'featured-article'), topic('attention', 'main-page'), topic('reach', 'interwiki')]); for (const lane of [0, 1, 2]) { place(s, 0, lane, card()); place(s, 1, lane, card()); } const count = () => s.lanes.map(l => l.cards.map(side => side.map(c => c.card.cardId))); for (const id of [...V1_KEYWORDS, ...OPTIONAL_KEYWORDS]) { const before = count(); const c = reveal(s, 0, 1, card({ keyword: id })); endOfTurn(s, c); s.lanes[1].cards[0].pop(); assert.deepEqual(count(), before, id); } });