First commit

This commit is contained in:
2026-10-27 12:42:57 +01:00
commit 7dd9f4b14d
74 changed files with 33018 additions and 0 deletions

View File

@@ -0,0 +1,89 @@
import assert from 'node:assert/strict'
import { createServer } from 'node:http'
import type { AddressInfo } from 'node:net'
import { mkdtemp, readFile, rm } from 'node:fs/promises'
import { tmpdir } from 'node:os'
import { join } from 'node:path'
import test from 'node:test'
import {
PlaymatError,
cardImage,
deletePlaymatImage,
playmatImage,
scryfallArtUrl,
scryfallImageUrl,
storePlaymatImage,
} from '../src/images.js'
const ART = Buffer.from([0xff, 0xd8, 0xff, 0xd9])
test('only https URLs on the Scryfall image host are accepted', () => {
assert.equal(
scryfallImageUrl('https://cards.scryfall.io/normal/front/a/b/art.jpg?123'),
'https://cards.scryfall.io/normal/front/a/b/art.jpg?123',
)
assert.equal(scryfallImageUrl('http://cards.scryfall.io/art.jpg'), undefined)
assert.equal(scryfallImageUrl('https://example.com/art.jpg'), undefined)
assert.equal(scryfallImageUrl('not a url'), undefined)
assert.equal(scryfallImageUrl(undefined), undefined)
})
test('playmat art swaps the card image for the wide Scryfall crop', () => {
assert.equal(
scryfallArtUrl('https://cards.scryfall.io/normal/front/a/b/art.jpg?123'),
'https://cards.scryfall.io/art_crop/front/a/b/art.jpg?123',
)
assert.equal(scryfallArtUrl(undefined), undefined)
assert.equal(scryfallArtUrl('https://example.com/normal/art.jpg'), undefined)
})
test('uploaded playmats are stored, served and deleted', async () => {
const dir = await mkdtemp(join(tmpdir(), 'commander-playmats-'))
const jpeg = Buffer.concat([Buffer.from([0xff, 0xd8, 0xff]), Buffer.from('pixels')])
try {
const id = await storePlaymatImage(`data:image/jpeg;base64,${jpeg.toString('base64')}`, dir)
assert.deepEqual(await playmatImage(id, dir), jpeg)
await deletePlaymatImage(id, dir)
assert.equal(await playmatImage(id, dir), undefined)
} finally {
await rm(dir, { recursive: true, force: true })
}
})
test('uploaded playmats must be JPEG data URLs with a JPEG signature', async () => {
const dir = await mkdtemp(join(tmpdir(), 'commander-playmats-'))
try {
await assert.rejects(storePlaymatImage('data:image/png;base64,AAAA', dir), PlaymatError)
await assert.rejects(storePlaymatImage('data:image/jpeg;base64,AAAA', dir), PlaymatError)
await assert.rejects(storePlaymatImage('not an image', dir), PlaymatError)
} finally {
await rm(dir, { recursive: true, force: true })
}
})
test('card art is downloaded once and then served from the disk cache', async () => {
let requests = 0
const server = createServer((_request, response) => {
requests++
response.writeHead(200, { 'Content-Type': 'image/jpeg' })
response.end(ART)
})
await new Promise<void>(resolve => server.listen(0, '127.0.0.1', resolve))
const dir = await mkdtemp(join(tmpdir(), 'commander-images-'))
try {
const url = `http://127.0.0.1:${(server.address() as AddressInfo).port}/art.jpg`
assert.deepEqual(await cardImage('card-id', url, dir), ART)
assert.deepEqual(await readFile(join(dir, 'card-id.jpg')), ART)
assert.deepEqual(await cardImage('card-id', url, dir), ART)
assert.equal(requests, 1)
} finally {
await new Promise(resolve => server.close(resolve))
await rm(dir, { recursive: true, force: true })
}
})