|
1 | | -import { TITLE_GENERATION_SYSTEM_PROMPT } from '@/qoder/prompt/title-generation'; |
| 1 | +import { getLocale, setLocale } from '@/i18n/i18n'; |
| 2 | +import { |
| 3 | + buildTitleGenerationPrompt, |
| 4 | + getTitleGenerationSystemPrompt, |
| 5 | + parseTitleGenerationResponse, |
| 6 | +} from '@/qoder/prompt/title-generation'; |
2 | 7 |
|
3 | 8 | describe('titleGeneration', () => { |
| 9 | + let previousLocale: ReturnType<typeof getLocale>; |
| 10 | + |
| 11 | + beforeEach(() => { |
| 12 | + previousLocale = getLocale(); |
| 13 | + setLocale('en'); |
| 14 | + }); |
| 15 | + |
| 16 | + afterEach(() => { |
| 17 | + setLocale(previousLocale); |
| 18 | + }); |
| 19 | + |
4 | 20 | it('exports a non-empty system prompt string', () => { |
5 | | - expect(typeof TITLE_GENERATION_SYSTEM_PROMPT).toBe('string'); |
6 | | - expect(TITLE_GENERATION_SYSTEM_PROMPT.length).toBeGreaterThan(0); |
| 21 | + const prompt = getTitleGenerationSystemPrompt(); |
| 22 | + expect(typeof prompt).toBe('string'); |
| 23 | + expect(prompt.length).toBeGreaterThan(0); |
7 | 24 | }); |
8 | 25 |
|
9 | 26 | it('includes the max character constraint', () => { |
10 | | - expect(TITLE_GENERATION_SYSTEM_PROMPT).toContain('max 50 chars'); |
| 27 | + expect(getTitleGenerationSystemPrompt()).toContain('max 50 chars'); |
11 | 28 | }); |
12 | 29 |
|
13 | | - it('instructs to start with a strong verb', () => { |
14 | | - expect(TITLE_GENERATION_SYSTEM_PROMPT).toContain('strong verb'); |
| 30 | + it('instructs to start with a strong verb in the output language', () => { |
| 31 | + expect(getTitleGenerationSystemPrompt()).toContain('strong verb'); |
| 32 | + expect(getTitleGenerationSystemPrompt()).toContain('output language'); |
15 | 33 | }); |
16 | 34 |
|
17 | 35 | it('instructs to return only the raw title text', () => { |
18 | | - expect(TITLE_GENERATION_SYSTEM_PROMPT).toContain('ONLY the raw title text'); |
| 36 | + expect(getTitleGenerationSystemPrompt()).toContain('ONLY the raw title text'); |
| 37 | + }); |
| 38 | + |
| 39 | + it('uses one language rule without English verb or title examples', () => { |
| 40 | + const prompt = getTitleGenerationSystemPrompt(); |
| 41 | + |
| 42 | + expect(prompt).toContain("same language as the user's request"); |
| 43 | + expect(prompt).toContain('UI locale (`en`)'); |
| 44 | + expect(prompt).toContain('proper nouns, product names, file names, paths, code identifiers, and code snippets unchanged'); |
| 45 | + expect(prompt).not.toContain('Create, Fix, Debug, Explain, Analyze'); |
| 46 | + expect(prompt).not.toContain('Debug Python script'); |
| 47 | + expect(prompt).not.toContain('Refactor React hook'); |
| 48 | + }); |
| 49 | + |
| 50 | + it('falls back to the current UI locale', () => { |
| 51 | + setLocale('zh-CN'); |
| 52 | + |
| 53 | + expect(getTitleGenerationSystemPrompt()).toContain('UI locale (`zh-CN`)'); |
| 54 | + }); |
| 55 | + |
| 56 | + it.each([ |
| 57 | + ['Chinese', '请修复 useReducer 的状态更新'], |
| 58 | + ['English', 'Fix useReducer state updates'], |
| 59 | + ['mixed code identifiers', '请修复 Qoderian 中 notes/demo.md 的 useReducer'], |
| 60 | + ])('preserves %s request text and identifiers', (_language, request) => { |
| 61 | + expect(buildTitleGenerationPrompt(request)).toContain(request); |
| 62 | + }); |
| 63 | + |
| 64 | + it('preserves a Chinese title and its code identifiers', () => { |
| 65 | + expect(parseTitleGenerationResponse('修复 Qoderian 的 useReducer')).toBe('修复 Qoderian 的 useReducer'); |
19 | 66 | }); |
20 | 67 | }); |
0 commit comments