Spaces:
Sleeping
Sleeping
File size: 5,634 Bytes
6cda6d8 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 | import assert from 'node:assert/strict'
import test from 'node:test'
import {
extractProblemFramingJson,
normalizeProblemFramingPlan,
parseProblemFramingResponse,
} from '../problem-framing/parser'
test('extracts a JSON object from a fenced model response', () => {
assert.equal(
extractProblemFramingJson('```json\n{"headline":"Quadratic"}\n```'),
'{"headline":"Quadratic"}',
)
})
test('rejects an HTML response before JSON parsing', () => {
assert.throws(
() => extractProblemFramingJson('<!DOCTYPE html><html><body>502</body></html>'),
/HTML, not JSON/,
)
})
test('normalizes aliases and preserves all steps up to twelve', () => {
const plan = normalizeProblemFramingPlan({
mode: 'clarify',
headline: ' Quadratic function ',
summary: ' Show the vertex. ',
steps: [
{ title: ' Step one ', content: ' Locate the axis. ' },
{ title: 'Two', content: 'Complete the square.' },
{ title: 'Three', content: 'Plot the vertex.' },
{ title: 'Four', content: 'Add the focus.' },
{ title: 'Five', content: 'Compare forms.' },
{ title: 'Six', content: 'This step must be preserved.' },
],
visual_motif: ' Coordinate grid ',
designer_hint: ' Animate each transformation. ',
}, 'en-US')
assert.deepEqual(plan, {
mode: 'clarify',
headline: 'Quadratic function',
summary: 'Show the vertex.',
steps: [
{ title: 'Step one', content: 'Locate the axis.' },
{ title: 'Two', content: 'Complete the square.' },
{ title: 'Three', content: 'Plot the vertex.' },
{ title: 'Four', content: 'Add the focus.' },
{ title: 'Five', content: 'Compare forms.' },
{ title: 'Six', content: 'This step must be preserved.' },
],
visualMotif: 'Coordinate grid',
designerHint: 'Animate each transformation.',
})
})
test('parses all twelve tagged steps and every expanded design field without truncation', () => {
const fields = '<question>为什么?</question><answer>因为几何约束。</answer><audience>初中</audience><background_rules>定长</background_rules><selected_route>勾股</selected_route><alternative_routes>不用求导</alternative_routes><out_of_scope>摩擦</out_of_scope><special_cases>避开竖直</special_cases>'
const steps = Array.from({ length: 12 }, (_, i) => `<step><phase>derivation</phase><title>${i + 1}</title><content>点亮第${i + 1}条线。</content></step>`).join('')
const plan = parseProblemFramingResponse(`<plan><mode>clarify</mode><headline>梯子</headline><summary>解释变慢</summary>${fields}<steps>${steps}</steps><visual_motif>梯子</visual_motif><designer_hint>90秒</designer_hint></plan>`, 'zh-CN')
assert.equal(plan.steps.length, 12)
assert.equal(plan.steps[11].content, '点亮第12条线。')
assert.equal(plan.steps[11].phase, 'derivation')
assert.equal(plan.question, '为什么?')
assert.equal(plan.backgroundRules, '定长')
assert.equal(plan.specialCases, '避开竖直')
})
test('rejects thirteen steps rather than silently dropping the ending', () => {
assert.throws(() => normalizeProblemFramingPlan({ headline: '标题', summary: '摘要', steps: Array.from({ length: 13 }, () => ({ title: '一步', content: '动作' })) }, 'zh-CN'), /at most 12/)
})
test('default generation contract requires expanded fields while legacy reading stays supported', () => {
const legacy = JSON.stringify({ headline: '标题', summary: '摘要', steps: Array.from({ length: 3 }, () => ({ title: '一步', content: '动作' })) })
assert.equal(parseProblemFramingResponse(legacy, 'zh-CN').steps.length, 3)
assert.throws(() => parseProblemFramingResponse(legacy, 'zh-CN', true), /Expanded plan requires question/)
})
test('rejects partial expanded decisions and unknown phases', () => {
const base = { headline: '标题', summary: '摘要', steps: Array.from({ length: 3 }, () => ({ title: '一步', content: '动作' })) }
assert.throws(() => normalizeProblemFramingPlan({ ...base, question: '为什么?' }, 'zh-CN'), /answer/)
assert.throws(() => normalizeProblemFramingPlan({ ...base, steps: base.steps.map(step => ({ ...step, phase: 'wrong' })) }, 'zh-CN'), /Step phase/)
})
test('rejects an incomplete plan instead of inventing its content', () => {
assert.throws(
() => normalizeProblemFramingPlan({ steps: [{ content: '保留的步骤' }] }, 'zh-CN'),
/missing required content: headline, summary/,
)
assert.throws(
() => normalizeProblemFramingPlan({
headline: '标题',
summary: '摘要',
steps: [
{ title: '甲', content: '乙' },
{ title: '丙', content: '丁' },
],
}, 'zh-CN'),
/at least 3 complete steps \(received 2\)/,
)
// A step with a title but no content is not a complete step: three of them stay three short.
assert.throws(
() => normalizeProblemFramingPlan({
headline: '标题',
summary: '摘要',
steps: [{ title: '甲' }, { title: '乙' }, { title: '丙' }],
}, 'zh-CN'),
/at least 3 complete steps \(received 0\)/,
)
})
test('keeps a complete plan intact', () => {
const plan = normalizeProblemFramingPlan({
mode: 'invent',
headline: '标题',
summary: '摘要',
steps: [
{ title: '甲', content: '乙' },
{ title: '丙', content: '丁' },
{ title: '戊', content: '己' },
],
}, 'zh-CN')
assert.deepEqual(plan.steps, [
{ title: '甲', content: '乙' },
{ title: '丙', content: '丁' },
{ title: '戊', content: '己' },
])
assert.equal(plan.headline, '标题')
assert.equal(plan.summary, '摘要')
})
|