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,117 @@
import assert from 'node:assert/strict'
import test from 'node:test'
import type { Card } from '../src/cards.js'
import { cleanTranslation, TranslationError, translateCard } from '../src/effects/translate.js'
const CARD: Card = {
oracle_id: '00000000-0000-4000-8000-000000000002',
name: 'Test Spell',
layout: 'normal',
cmc: 2,
type_line: 'Sorcery',
colors: ['U'],
color_identity: ['U'],
keywords: [],
oracle_text: 'Draw two cards.',
}
interface RequestBody {
model: string
messages: Array<{ role: string; content: string }>
temperature: number
max_tokens: number
}
function fakeOpenRouter(content: string) {
let body: RequestBody | undefined
let headers: Record<string, string> | undefined
const fetchImpl = async (_input: Parameters<typeof fetch>[0], init?: Parameters<typeof fetch>[1]) => {
body = JSON.parse(String(init?.body)) as RequestBody
headers = init?.headers as Record<string, string>
return new Response(JSON.stringify({ choices: [{ message: { content } }] }), {
status: 200,
headers: { 'Content-Type': 'application/json' },
})
}
return {
fetchImpl: fetchImpl as typeof fetch,
get body() {
assert.ok(body)
return body
},
get headers() {
assert.ok(headers)
return headers
},
}
}
test('translation asks for pseudocode only and validates the answer', async () => {
const fake = fakeOpenRouter('spell:\n draw(you, 2)')
const source = await translateCard(CARD, {
apiKey: 'test-key',
model: 'test/model',
fetch: fake.fetchImpl,
})
assert.equal(source, 'spell:\n draw(you, 2)')
assert.equal(fake.body.model, 'test/model')
assert.equal(fake.body.temperature, 0)
assert.equal(fake.body.max_tokens, 700)
assert.equal(fake.headers.Authorization, 'Bearer test-key')
assert.match(fake.body.messages[0].content, /pseudocode only/i)
assert.match(fake.body.messages[0].content, /Never invent an operation/)
assert.match(fake.body.messages[1].content, /Draw two cards\./)
})
test('a model code fence is removed before saving', async () => {
const fake = fakeOpenRouter('```\nspell:\n draw(you, 2)\n```')
const source = await translateCard(CARD, {
apiKey: 'test-key',
model: 'test/model',
fetch: fake.fetchImpl,
})
assert.equal(source, 'spell:\n draw(you, 2)')
assert.equal(cleanTranslation(' spell:\n draw(you, 2) '), 'spell:\n draw(you, 2)')
})
test('prose instead of pseudocode is rejected', async () => {
const fake = fakeOpenRouter('Here is the translated card. It draws two cards.')
await assert.rejects(
translateCard(CARD, { apiKey: 'test-key', model: 'test/model', fetch: fake.fetchImpl }),
(error: unknown) => error instanceof TranslationError && /valid pseudocode/.test(error.message),
)
})
test('missing API key is reported before any request', async () => {
let called = false
const fetchImpl = async () => {
called = true
return new Response('{}')
}
await assert.rejects(
translateCard(CARD, { apiKey: '', fetch: fetchImpl as typeof fetch }),
(error: unknown) => error instanceof TranslationError && error.status === 503,
)
assert.equal(called, false)
})
test('multi-face cards receive their face names in the prompt', async () => {
const faces: Card = {
...CARD,
layout: 'transform',
card_faces: [
{ name: 'Front', type_line: 'Creature', oracle_text: 'When this enters, draw a card.', colors: [] },
{ name: 'Back', type_line: 'Creature', oracle_text: 'Flying', colors: [] },
],
}
const fake = fakeOpenRouter('## Front\non enters(this):\n draw(you, 1)\n\n## Back\nkeyword Flying')
const source = await translateCard(faces, {
apiKey: 'test-key',
model: 'test/model',
fetch: fake.fetchImpl,
})
assert.equal(source, '## Front\non enters(this):\n draw(you, 1)\n\n## Back\nkeyword Flying')
assert.match(fake.body.messages[1].content, /## Front/)
assert.match(fake.body.messages[1].content, /## Back/)
})