files-tcg/sim/test/cards.test.ts
2026-09-28 01:56:25 -04:00

53 lines
2.1 KiB
TypeScript

import assert from 'node:assert/strict';
import { readdirSync, readFileSync } from 'node:fs';
import { test } from 'node:test';
import { loadCards } from '../src/cards.ts';
import { randomDeck, validateDeck } from '../src/deck.ts';
import { makeRng } from '../src/rng.ts';
import { DEFAULT_RULES } from '../src/rules.ts';
import { staleScripts } from '../src/scripts.ts';
const pool = loadCards();
test('cards.csv loads every playable card', () => {
const count = (kind: string) => pool.all.filter((c) => c.kind === kind).length;
assert.equal(count('avatar'), 8);
assert.equal(count('server'), 13);
assert.equal(count('program'), 28);
assert.equal(count('hardware'), 16);
assert.equal(count('execution'), 18);
assert.equal(pool.byName.get('Broken Bits')?.cores, 4);
assert.equal(pool.byName.get('Carbon NanoBots')?.slots, 4);
assert.equal(pool.byName.get('Quantum Coprocessor')?.ram, 9);
});
test('every card has a script written for its current text', () => {
assert.deepEqual(staleScripts(pool), []);
});
test('every card has its own test', () => {
const dir = new URL('.', import.meta.url);
const tested = new Set<string>();
for (const file of readdirSync(dir).filter((f) => f.endsWith('.test.ts'))) {
for (const m of readFileSync(new URL(file, dir), 'utf8').matchAll(/\bcard\('([^']+)'/g)) tested.add(m[1]);
}
assert.deepEqual(pool.all.map((c) => c.name).filter((n) => !tested.has(n)), []);
});
test('random decks are legal', () => {
const rng = makeRng(3);
for (let i = 0; i < 50; i++) {
assert.deepEqual(validateDeck(pool, randomDeck(pool, rng, DEFAULT_RULES), DEFAULT_RULES), []);
}
});
test('deck validation catches size, kind and copy problems', () => {
const deck = randomDeck(pool, makeRng(1), DEFAULT_RULES);
deck.main[0] = deck.main[1] = deck.main[2] = 'Execute: Fork';
deck.servers.pop();
deck.avatars[0] = 'Cloud Compute';
const errors = validateDeck(pool, deck, DEFAULT_RULES).join('\n');
assert.match(errors, /Servers: 19 cards/);
assert.match(errors, /"Cloud Compute" is a server/);
assert.match(errors, /copies of "Execute: Fork"/);
});