60fcb494a7
按 jingtingxiang 拍板将 prompt 回到 v4 基线:模板正文恢复内嵌的 格式/语气/句式/词汇/标点全量约束与 11 组正误例(量词规则按术语表 现行裁定 package→包 写作「由三个包构成的 seam」),不再注入 translation-rules.md——该文件约束人和 agent,不进模板;占位符收敛 为 source_lang/target_lang/terminology 三个,切换行由模型按文档 自身拼写。渲染器、解析器、conformance 门禁与单测同步回 v4 契约: 三段裸 XML(translation/review/final 顺序唯一),容忍整体 ```xml 围栏回显;saxes 依赖随 CDATA 协议一并移除。
52 lines
2.5 KiB
TypeScript
52 lines
2.5 KiB
TypeScript
/** Unit tests for the prompt-v4 renderer and three-section response parser. */
|
|
|
|
import { readFileSync } from 'node:fs'
|
|
import { join, resolve } from 'node:path'
|
|
import { describe, expect, it } from 'vitest'
|
|
import {
|
|
parseTranslationResponse,
|
|
renderTranslationPrompt,
|
|
renderTranslationResponse,
|
|
} from './translation-prompt.ts'
|
|
|
|
const root = resolve(import.meta.dirname, '..')
|
|
const document = readFileSync(join(root, 'docs/i18n/translation-prompt.md'), 'utf8')
|
|
const terminology = '| English | 中文 |\n|---|---|\n| agent | agent |'
|
|
|
|
describe('translation prompt rendering', () => {
|
|
it('renders both directions with every placeholder resolved', () => {
|
|
const en = renderTranslationPrompt(document, { sourceLanguage: 'English', terminology })
|
|
expect(en).toContain('from English to Chinese')
|
|
expect(en).toContain(terminology)
|
|
expect(en).not.toContain('{{')
|
|
const zh = renderTranslationPrompt(document, { sourceLanguage: 'Chinese', terminology })
|
|
expect(zh).toContain('from Chinese to English')
|
|
})
|
|
|
|
it('rejects a template with unknown or missing placeholders', () => {
|
|
const alien = document.replaceAll('{{terminology}}', '{{terms_prompt}}')
|
|
expect(() => renderTranslationPrompt(alien, { sourceLanguage: 'English', terminology })).toThrow(/unsupported placeholder/)
|
|
const missing = document.replaceAll('{{terminology}}', '')
|
|
expect(() => renderTranslationPrompt(missing, { sourceLanguage: 'English', terminology })).toThrow(/required placeholder/)
|
|
})
|
|
})
|
|
|
|
describe('translation response sections', () => {
|
|
it('round-trips Markdown bodies', () => {
|
|
const response = { translation: '# 标题\n\n正文 **加粗**。', review: '- [Tone] 修正一处。\n- 无修正', final: '# 标题\n\n定稿。' }
|
|
expect(parseTranslationResponse(renderTranslationResponse(response))).toEqual(response)
|
|
})
|
|
|
|
it('tolerates a fenced xml wrapper around the whole response', () => {
|
|
const fenced = '```xml\n<translation>\nA\n</translation>\n\n<review>\n- 无修正\n</review>\n\n<final>\nA\n</final>\n```'
|
|
expect(parseTranslationResponse(fenced).final).toBe('A')
|
|
})
|
|
|
|
it('rejects missing, unterminated, or duplicated sections', () => {
|
|
expect(() => parseTranslationResponse('<translation>\nA\n</translation>')).toThrow(/missing <review>/)
|
|
expect(() => parseTranslationResponse('<translation>\nA')).toThrow(/unterminated <translation>/)
|
|
const dup = '<translation>\nA\n</translation>\n<review>\nR\n</review>\n<final>\nF\n</final>\n<final>\nG\n</final>'
|
|
expect(() => parseTranslationResponse(dup)).toThrow(/duplicate <final>/)
|
|
})
|
|
})
|