|
| 1 | +import { describe, expect, it } from 'vitest' |
| 2 | +import { toPlainText } from '@/app/workspace/[workspaceId]/components/message-actions/message-actions' |
| 3 | + |
| 4 | +describe('toPlainText', () => { |
| 5 | + it('preserves language-tagged fenced code in copied messages', () => { |
| 6 | + const message = [ |
| 7 | + "Sure — here's a useful example:", |
| 8 | + '', |
| 9 | + '```python', |
| 10 | + 'import time', |
| 11 | + 'from functools import wraps', |
| 12 | + '', |
| 13 | + 'def retry(fn):', |
| 14 | + ' def wrapper(*args, **kwargs):', |
| 15 | + ' marker = `<thinking>keep this code</thinking>`', |
| 16 | + ' return fn(*args, **kwargs)', |
| 17 | + '```', |
| 18 | + ].join('\n') |
| 19 | + |
| 20 | + expect(toPlainText(message)).toBe( |
| 21 | + [ |
| 22 | + "Sure — here's a useful example:", |
| 23 | + '', |
| 24 | + 'import time', |
| 25 | + 'from functools import wraps', |
| 26 | + '', |
| 27 | + 'def retry(fn):', |
| 28 | + ' def wrapper(*args, **kwargs):', |
| 29 | + ' marker = `<thinking>keep this code</thinking>`', |
| 30 | + ' return fn(*args, **kwargs)', |
| 31 | + ].join('\n') |
| 32 | + ) |
| 33 | + }) |
| 34 | + |
| 35 | + it.each([ |
| 36 | + ['tilde', '~~~typescript', '~~~'], |
| 37 | + ['longer closing', '```typescript', '````'], |
| 38 | + ])('preserves code in %s fences', (_name, openingFence, closingFence) => { |
| 39 | + const message = ['Before', '', openingFence, 'const value = **raw**', closingFence, '', 'After'] |
| 40 | + |
| 41 | + expect(toPlainText(message.join('\n'))).toBe( |
| 42 | + ['Before', '', 'const value = **raw**', '', 'After'].join('\n') |
| 43 | + ) |
| 44 | + }) |
| 45 | + |
| 46 | + it.each(['```python', '~~~python'])( |
| 47 | + 'preserves an unclosed %s fenced code block through the end of the message', |
| 48 | + (openingFence) => { |
| 49 | + const message = [ |
| 50 | + 'Before', |
| 51 | + '', |
| 52 | + openingFence, |
| 53 | + 'def example(*args, **kwargs):', |
| 54 | + ' return `raw`', |
| 55 | + ] |
| 56 | + |
| 57 | + expect(toPlainText(message.join('\n'))).toBe( |
| 58 | + ['Before', '', 'def example(*args, **kwargs):', ' return `raw`'].join('\n') |
| 59 | + ) |
| 60 | + } |
| 61 | + ) |
| 62 | + |
| 63 | + it('preserves multiple code blocks while cleaning surrounding message content', () => { |
| 64 | + const message = [ |
| 65 | + '**Before** `inline`', |
| 66 | + '', |
| 67 | + '<credential>remove this UI payload</credential>', |
| 68 | + '', |
| 69 | + '```typescript', |
| 70 | + 'const marker = `<thinking>keep this code</thinking>`', |
| 71 | + '```', |
| 72 | + '', |
| 73 | + 'Between **blocks**', |
| 74 | + '', |
| 75 | + '~~~python', |
| 76 | + 'def example():', |
| 77 | + ' return *args, **kwargs', |
| 78 | + '~~~', |
| 79 | + '', |
| 80 | + 'After', |
| 81 | + ].join('\n') |
| 82 | + |
| 83 | + expect(toPlainText(message)).toBe( |
| 84 | + [ |
| 85 | + 'Before inline', |
| 86 | + '', |
| 87 | + 'const marker = `<thinking>keep this code</thinking>`', |
| 88 | + '', |
| 89 | + 'Between blocks', |
| 90 | + '', |
| 91 | + 'def example():', |
| 92 | + ' return *args, **kwargs', |
| 93 | + '', |
| 94 | + 'After', |
| 95 | + ].join('\n') |
| 96 | + ) |
| 97 | + }) |
| 98 | +}) |
0 commit comments