|
| 1 | +import { describe, expect, test } from 'vitest' |
| 2 | +import { generatePromptId } from '@/content-render/lib/prompt-id' |
| 3 | + |
| 4 | +describe('generatePromptId', () => { |
| 5 | + test('generates consistent IDs for same content', () => { |
| 6 | + const content = 'example prompt text' |
| 7 | + const id1 = generatePromptId(content) |
| 8 | + const id2 = generatePromptId(content) |
| 9 | + expect(id1).toBe(id2) |
| 10 | + }) |
| 11 | + |
| 12 | + test('generates different IDs for different content', () => { |
| 13 | + const id1 = generatePromptId('prompt one') |
| 14 | + const id2 = generatePromptId('prompt two') |
| 15 | + expect(id1).not.toBe(id2) |
| 16 | + }) |
| 17 | + |
| 18 | + test('generates numeric string IDs', () => { |
| 19 | + const id = generatePromptId('test prompt') |
| 20 | + expect(typeof id).toBe('string') |
| 21 | + expect(Number.isNaN(Number(id))).toBe(false) |
| 22 | + }) |
| 23 | + |
| 24 | + test('handles empty strings', () => { |
| 25 | + const id = generatePromptId('') |
| 26 | + expect(typeof id).toBe('string') |
| 27 | + expect(id.length).toBeGreaterThan(0) |
| 28 | + }) |
| 29 | + |
| 30 | + test('handles special characters', () => { |
| 31 | + const id1 = generatePromptId('prompt with\nnewlines') |
| 32 | + const id2 = generatePromptId('prompt with\ttabs') |
| 33 | + const id3 = generatePromptId('prompt with "quotes"') |
| 34 | + expect(typeof id1).toBe('string') |
| 35 | + expect(typeof id2).toBe('string') |
| 36 | + expect(typeof id3).toBe('string') |
| 37 | + expect(id1).not.toBe(id2) |
| 38 | + expect(id2).not.toBe(id3) |
| 39 | + }) |
| 40 | + |
| 41 | + test('generates deterministic IDs (regression test)', () => { |
| 42 | + // These specific values ensure the hash function remains consistent |
| 43 | + expect(generatePromptId('hello world')).toBe('1730621824') |
| 44 | + expect(generatePromptId('test')).toBe('4180565944') |
| 45 | + }) |
| 46 | + |
| 47 | + test('handles prompts with code context (ref pattern)', () => { |
| 48 | + // When ref= is used, the prompt includes referenced code + prompt text separated by newline |
| 49 | + const codeContext = |
| 50 | + 'function logPersonAge(name, age, revealAge) {\n if (revealAge) {\n console.log(name);\n }\n}' |
| 51 | + const promptText = 'Improve the variable names in this function' |
| 52 | + const combinedPrompt = `${codeContext}\n${promptText}` |
| 53 | + |
| 54 | + const id = generatePromptId(combinedPrompt) |
| 55 | + expect(typeof id).toBe('string') |
| 56 | + expect(id.length).toBeGreaterThan(0) |
| 57 | + |
| 58 | + // Should be different from just the prompt text alone |
| 59 | + expect(id).not.toBe(generatePromptId(promptText)) |
| 60 | + }) |
| 61 | + |
| 62 | + test('handles very long prompts', () => { |
| 63 | + // Real-world prompts can include entire code blocks (100+ lines) |
| 64 | + const longCode = 'x\n'.repeat(500) // 500 lines |
| 65 | + const id = generatePromptId(longCode) |
| 66 | + expect(typeof id).toBe('string') |
| 67 | + expect(id.length).toBeGreaterThan(0) |
| 68 | + }) |
| 69 | + |
| 70 | + test('handles prompts with backticks and template literals', () => { |
| 71 | + // Prompts often include inline code with backticks |
| 72 | + const prompt = "In JavaScript I'd write: `The ${numCats === 1 ? 'cat is' : 'cats are'} hungry.`" |
| 73 | + const id = generatePromptId(prompt) |
| 74 | + expect(typeof id).toBe('string') |
| 75 | + expect(id.length).toBeGreaterThan(0) |
| 76 | + }) |
| 77 | + |
| 78 | + test('handles prompts with placeholders', () => { |
| 79 | + // Content uses placeholders like NEW-LANGUAGE, OWNER/REPOSITORY |
| 80 | + const id1 = generatePromptId('What is NEW-LANGUAGE best suited for?') |
| 81 | + const id2 = generatePromptId('In OWNER/REPOSITORY, create a feature request') |
| 82 | + expect(id1).not.toBe(id2) |
| 83 | + expect(typeof id1).toBe('string') |
| 84 | + expect(typeof id2).toBe('string') |
| 85 | + }) |
| 86 | + |
| 87 | + test('handles unicode and international characters', () => { |
| 88 | + // May encounter non-ASCII characters in prompts |
| 89 | + const id1 = generatePromptId('Explique-moi le code en français') |
| 90 | + const id2 = generatePromptId('コードを説明してください') |
| 91 | + const id3 = generatePromptId('Объясните этот код') |
| 92 | + expect(typeof id1).toBe('string') |
| 93 | + expect(typeof id2).toBe('string') |
| 94 | + expect(typeof id3).toBe('string') |
| 95 | + expect(id1).not.toBe(id2) |
| 96 | + expect(id2).not.toBe(id3) |
| 97 | + }) |
| 98 | +}) |
0 commit comments