Files
magik/scripts/verify-cards.mjs
masi 14daed4c54 Add market prices to the card index and the deck total
Prices were already in the Scryfall bulk download, so this adds no
dependency and no new fetch: the work is deciding what a card's price
means, given that the index keeps one row per card and a card has many
printings.

It is the median of the ordinary printings. The kept printing underprices
about a third of the legal pool because it is chosen for art and identity
and is usually a cheap reprint; the maximum is worse, since premium and
bundled listings put a $870 Sol Ring in the data. The median lands where a
player recognises the number and ignores the outliers. It is a market
value, not a build cost.

Two filter decisions are deliberate and easy to undo by accident: Reserved
List printings are included, because that is where Commander's banned
Power 9 live and excluding them priced Ancestral Recall and Time Walk at
nothing, while memorabilia is excluded because a Collectors' Edition box is
not a single card. Banned cards fall back to their banned printings.

deckValue counts unpriced cards instead of treating them as free, so a
total is always labelled with what it left out.

Also implements scripts/verify-cards.mjs. package.json and the index skill
both documented `pnpm cards:verify` but the script did not exist; it now
runs in deploy and fails on a card priced at exactly zero, which is the
failure mode the missing-price guard exists to prevent.

Co-Authored-By: Claude Code <noreply@anthropic.com>
2026-10-02 13:17:09 -03:00

88 lines
3.7 KiB
JavaScript

/**
* Sanity-check data/cards.db.
*
* The build is the thing that can go quietly wrong: a partial download still
* produces a valid-looking index (see the skill's download note), and a price
* column is easy to poison with zeros. This asserts the invariants a correct
* build holds, so a bad one fails loudly instead of shipping a subtly wrong
* number to a deck total.
*
* pnpm cards:verify
*/
import { DatabaseSync } from 'node:sqlite'
import { existsSync } from 'node:fs'
import { resolve, dirname } from 'node:path'
import { fileURLToPath } from 'node:url'
const ROOT = resolve(dirname(fileURLToPath(import.meta.url)), '..')
const DB_PATH = process.env.CARDS_DB ?? resolve(ROOT, 'data/cards.db')
if (!existsSync(DB_PATH)) {
console.error(`card index missing at ${DB_PATH} — run \`pnpm cards:build\` first`)
process.exit(1)
}
const db = new DatabaseSync(DB_PATH, { readOnly: true })
const scalar = (sql, ...args) => db.prepare(sql).get(...args)
const failures = []
const check = (label, ok, detail) => {
console.log(`${ok ? 'ok ' : 'FAIL'} ${label}${detail ? ` ${detail}` : ''}`)
if (!ok) failures.push(label)
}
const total = scalar('SELECT COUNT(*) AS n FROM cards').n
check('index is populated', total > 30000, `${total} cards`)
check('every row has a name', scalar('SELECT COUNT(*) AS n FROM cards WHERE name IS NULL OR name = \'\'').n === 0)
const legal = scalar('SELECT COUNT(*) AS n FROM cards WHERE commander_legality = \'legal\'').n
check('legal cards were indexed', legal > 25000, `${legal} legal`)
const commanders = scalar('SELECT COUNT(*) AS n FROM cards WHERE commander_eligible = 1').n
check('commanders were identified', commanders > 500, `${commanders} commander-eligible`)
// A banned card must stay in the index: the validator names it and the
// commander picker explains why it is unavailable.
const banned = scalar('SELECT COUNT(*) AS n FROM cards WHERE commander_legality = \'banned\'').n
check('banned cards are kept for the validator', banned > 0, `${banned} banned`)
/**
* Prices.
*
* The zero check is the one that matters. Scryfall spells a missing price as
* null and `Number(null)` is 0, so a build that converts before testing writes
* a real $0.00 into the database — and a deck total then reports a famous card
* as free, which is the one price error that changes what a player does.
*/
const zeroPriced = scalar('SELECT COUNT(*) AS n FROM cards WHERE price_usd = 0').n
check('no card is priced at exactly zero', zeroPriced === 0, zeroPriced ? `${zeroPriced} at $0.00` : '')
const negative = scalar('SELECT COUNT(*) AS n FROM cards WHERE price_usd < 0').n
check('no card has a negative price', negative === 0)
const priced = scalar('SELECT COUNT(*) AS n FROM cards WHERE price_usd IS NOT NULL').n
const coverage = (priced / total) * 100
check('most cards carry a price', coverage > 95, `${priced} of ${total} (${coverage.toFixed(1)}%)`)
// An unpriced card is legitimate — Un-cards have no open-market price — but it
// must be the exception, not the rule. A bulk-download truncation or a filter
// mistake shows up here as a sudden collapse in coverage.
const unpriced = total - priced
check('unpriced cards are the exception', unpriced < total * 0.05, `${unpriced} unpriced`)
// A median over ordinary printings cannot be absurd. Anything past this is a
// premium/bundled listing leaking through the filter.
const outlier = db
.prepare('SELECT name, price_usd FROM cards WHERE price_usd > 100000')
.all()
check('no absurd outlier prices', outlier.length === 0, outlier.map((c) => c.name).join(', '))
db.close()
if (failures.length) {
console.error(`\n${failures.length} check(s) failed: ${failures.join('; ')}`)
process.exit(1)
}
console.log('\ncard index looks healthy')