This commit is contained in:
2026-10-29 15:21:05 +01:00
parent bb85881cac
commit bc05b63083
36 changed files with 4654 additions and 5080 deletions

View File

@@ -46,40 +46,40 @@ function fakeOpenRouter(content: string) {
}
}
test('translation asks for pseudocode only and validates the answer', async () => {
const fake = fakeOpenRouter('spell:\n draw(you, 2)')
test('translation asks for card source only and validates the answer', async () => {
const fake = fakeOpenRouter('export default [spell([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(source, 'export default [spell([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.body.max_tokens, 1200)
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, /card source 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 fake = fakeOpenRouter('```\nexport default [spell([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)')
assert.equal(source, 'export default [spell([draw(you, 2)])]')
assert.equal(cleanTranslation(' export default [spell([draw(you, 2)])] '), 'export default [spell([draw(you, 2)])]')
})
test('prose instead of pseudocode is rejected', async () => {
test('prose instead of card source 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),
(error: unknown) => error instanceof TranslationError && /valid card source/.test(error.message),
)
})
@@ -105,13 +105,13 @@ test('multi-face cards receive their face names in the prompt', async () => {
{ 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 fake = fakeOpenRouter("export default faces(face('Front', [triggered(when.enters(self), [draw(you, 1)])]), face('Back', [keyword('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.equal(source, "export default faces(face('Front', [triggered(when.enters(self), [draw(you, 1)])]), face('Back', [keyword('Flying')]))")
assert.match(fake.body.messages[1].content, /## Front/)
assert.match(fake.body.messages[1].content, /## Back/)
})