Tofu/scripts/import-recipes/tests/readRecipeResponse.test.js
2026-09-18 01:41:02 +02:00

46 lines
1.5 KiB
JavaScript

import { test } from 'node:test'
import assert from 'node:assert/strict'
import { readRecipeResponse } from '#import-recipes/extraction/readRecipeResponse.js'
test('reads the JSON of the text block', () => {
const response = {
stop_reason: 'end_turn',
content: [
{ type: 'thinking', thinking: '' },
{ type: 'text', text: '{"name":"Gratin de courgettes"}' }
]
}
assert.deepEqual(readRecipeResponse(response), {
ok: true,
recipe: { name: 'Gratin de courgettes' }
})
})
test('reports a refusal', () => {
const result = readRecipeResponse({ stop_reason: 'refusal', content: [] })
assert.equal(result.ok, false)
assert.match(result.reason, /refusal/)
})
test('reports an answer cut off at max_tokens', () => {
const result = readRecipeResponse({ stop_reason: 'max_tokens', content: [] })
assert.equal(result.ok, false)
assert.match(result.reason, /max_tokens/)
})
test('reports an answer with no text block', () => {
const result = readRecipeResponse({ stop_reason: 'end_turn', content: [{ type: 'thinking' }] })
assert.equal(result.ok, false)
assert.match(result.reason, /no text block/)
})
test('reports unreadable JSON instead of throwing', () => {
const response = { stop_reason: 'end_turn', content: [{ type: 'text', text: '{oops' }] }
const result = readRecipeResponse(response)
assert.equal(result.ok, false)
assert.match(result.reason, /unreadable JSON/)
})
test('survives an empty answer', () => {
assert.equal(readRecipeResponse(undefined).ok, false)
})