Files
magik/tests/prices.test.js
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

101 lines
4.4 KiB
JavaScript

import { test } from 'node:test'
import assert from 'node:assert/strict'
import { isMarketPrinting, medianPrice, samplePrice } from '../scripts/build-cards.mjs'
/**
* Price aggregation.
*
* The card index keeps one row per card, so a card's many printing prices have
* to be collapsed into one number. The two obvious choices are both wrong, and
* both are the kind of thing that looks like a harmless simplification later:
*
* - the maximum, which is dragged up by premium and bundled listings (a
* Sol Ring that reads as $870 is not a market price)
* - the cheapest, which understates what the card is actually worth and so
* makes a deck look cheaper than it is
*
* These pin the median and the printing filter that feeds it.
*/
/** A printing that would otherwise be sampled. */
const printing = (over = {}) => ({
lang: 'en',
set_type: 'expansion',
nonfoil: true,
promo: false,
digital: false,
reserved: false,
...over,
})
test('the median is unmoved by a single absurd printing', () => {
// Sol Ring: one premium listing at $869.99 and one cheap reprint. A mean or
// a max would carry the outlier; the median should not move at all.
const prices = [1.33, 2.1, 2.1, 869.99, 1.9]
assert.equal(medianPrice(prices), 2.1)
})
test('an even number of printings averages the middle pair', () => {
// (0.32 + 0.81) / 2 = 0.565, stored to the cent so a price never carries
// float noise into the database.
assert.equal(medianPrice([0.09, 0.32, 0.81, 42.42]), 0.57)
assert.equal(medianPrice([0.09, 0.32]), 0.21)
})
test('a single printing is its own median', () => {
assert.equal(medianPrice([4.98]), 4.98)
})
test('no sampled printing means no price, not zero', () => {
assert.equal(medianPrice([]), null)
assert.equal(medianPrice(undefined), null)
})
test('promo, digital, token and sealed-product printings are not market', () => {
assert.equal(isMarketPrinting(printing()), true)
assert.equal(isMarketPrinting(printing({ promo: true })), false)
assert.equal(isMarketPrinting(printing({ digital: true })), false)
assert.equal(isMarketPrinting(printing({ set_type: 'token' })), false)
// Sealed product is priced as the box it is, not as the card inside it.
assert.equal(isMarketPrinting(printing({ set_type: 'memorabilia' })), false)
// A non-English printing carries its own regional market's USD price.
assert.equal(isMarketPrinting(printing({ lang: 'ja' })), false)
// Foil-only printings have no non-foil price to read.
assert.equal(isMarketPrinting(printing({ nonfoil: false })), false)
})
test('a Reserved List printing is still the market', () => {
// Reserved excludes a card from reprinting, not from trading. Commander's
// banned Power 9 are only ever printed here, so excluding this set left
// Ancestral Recall and Time Walk with no price at all.
assert.equal(isMarketPrinting(printing({ reserved: true })), true)
assert.equal(isMarketPrinting(printing({ reserved: true, set_type: 'core' })), true)
})
test('a premium printing is still sampled when it is an ordinary release', () => {
// The filter keys off what a printing *is* (promo, digital, token, sealed
// product), not off how expensive it is. Expensive-but-normal printings must
// stay in the sample, or the median drifts toward the cheap reprints.
assert.equal(isMarketPrinting(printing({ set_type: 'masters' })), true)
})
test('a missing price is not a free card', () => {
// The bug this pins: Scryfall spells a missing price as null, and
// Number(null) is 0. Converting before testing let priceless printings into
// the median as $0.00, which zeroed out Ancestral Recall, Time Walk and
// Mox Jet — every one of their priced printings surrounded by unpriced ones.
assert.equal(samplePrice(printing({ prices: { usd: null } })), null)
assert.equal(samplePrice(printing({ prices: {} })), null)
assert.equal(samplePrice(printing({ prices: { usd: '' } })), null)
assert.equal(samplePrice(printing({ prices: { usd: 'not a number' } })), null)
assert.equal(samplePrice({ lang: 'en' }), null)
})
test('a real price is read from the string Scryfall sends', () => {
assert.equal(samplePrice(printing({ prices: { usd: '2.10' } })), 2.1)
assert.equal(samplePrice(printing({ prices: { usd: 0.05 } })), 0.05)
// A printing that is not market-contributing is not a price, even when
// Scryfall has one for it.
assert.equal(samplePrice(printing({ promo: true, prices: { usd: '1.00' } })), null)
})