|
| 1 | +import { cleanup_api_response } from './cleanup-api-response' |
| 2 | + |
| 3 | +describe('cleanup_api_response', () => { |
| 4 | + it('should return plain text as is', () => { |
| 5 | + const content = 'This is some plain text.' |
| 6 | + expect(cleanup_api_response({ content })).toBe('This is some plain text.') |
| 7 | + }) |
| 8 | + |
| 9 | + it('should trim whitespace from the final result', () => { |
| 10 | + const content = ' \n some text \n ' |
| 11 | + expect(cleanup_api_response({ content })).toBe('some text') |
| 12 | + }) |
| 13 | + |
| 14 | + it('should handle empty string input', () => { |
| 15 | + const content = '' |
| 16 | + expect(cleanup_api_response({ content })).toBe('') |
| 17 | + }) |
| 18 | + |
| 19 | + describe('wrapper removal', () => { |
| 20 | + it('should remove markdown code block wrappers', () => { |
| 21 | + const content = '```\nconst a = 1;\n```' |
| 22 | + expect(cleanup_api_response({ content })).toBe('const a = 1;') |
| 23 | + }) |
| 24 | + |
| 25 | + it('should remove markdown code block wrappers with language specifier', () => { |
| 26 | + const content = '```javascript\nconst a = 1;\n```' |
| 27 | + expect(cleanup_api_response({ content })).toBe('const a = 1;') |
| 28 | + }) |
| 29 | + |
| 30 | + it('should extract a markdown code block with surrounding text', () => { |
| 31 | + const content = |
| 32 | + 'Here is the code you requested:\n```javascript\nconst a = 1;\n```\nThanks!' |
| 33 | + expect(cleanup_api_response({ content })).toBe('const a = 1;') |
| 34 | + }) |
| 35 | + |
| 36 | + it('should remove <file> tags', () => { |
| 37 | + const content = '<file path="test.ts">\nconst a = 1;\n</file>' |
| 38 | + expect(cleanup_api_response({ content })).toBe('const a = 1;') |
| 39 | + }) |
| 40 | + |
| 41 | + it('should remove <files> tags', () => { |
| 42 | + const content = '<files>\nconst a = 1;\n</files>' |
| 43 | + expect(cleanup_api_response({ content })).toBe('const a = 1;') |
| 44 | + }) |
| 45 | + |
| 46 | + it('should remove CDATA wrappers', () => { |
| 47 | + const content = '<![CDATA[\nconst a = 1;\n]]>' |
| 48 | + expect(cleanup_api_response({ content })).toBe('const a = 1;') |
| 49 | + }) |
| 50 | + |
| 51 | + it('should handle incomplete CDATA closing tag', () => { |
| 52 | + const content = '<![CDATA[const a = 1;]]' |
| 53 | + expect(cleanup_api_response({ content })).toBe('const a = 1;') |
| 54 | + }) |
| 55 | + |
| 56 | + it('should remove DOCTYPE declaration', () => { |
| 57 | + const content = '<!DOCTYPE html>\n<html></html>' |
| 58 | + expect(cleanup_api_response({ content })).toBe('<html></html>') |
| 59 | + }) |
| 60 | + |
| 61 | + it('should handle nested wrappers iteratively', () => { |
| 62 | + const content = |
| 63 | + '```typescript\n<file path="test.ts">\n<![CDATA[\nconsole.log("hello");\n]]>\n</file>\n```' |
| 64 | + expect(cleanup_api_response({ content })).toBe('console.log("hello");') |
| 65 | + }) |
| 66 | + |
| 67 | + it('should handle content with only wrappers', () => { |
| 68 | + const content = '```\n<file path="test.ts">\n</file>\n```' |
| 69 | + expect(cleanup_api_response({ content })).toBe('') |
| 70 | + }) |
| 71 | + |
| 72 | + it('should not remove wrappers from the middle of the content', () => { |
| 73 | + const content = 'Some text\n```\ncode\n```\nSome other text' |
| 74 | + expect(cleanup_api_response({ content })).toBe( |
| 75 | + 'Some text\n```\ncode\n```\nSome other text' |
| 76 | + ) |
| 77 | + }) |
| 78 | + }) |
| 79 | + |
| 80 | + describe('special tag removal', () => { |
| 81 | + it('should remove <think> block from the beginning', () => { |
| 82 | + const content = '<think>I should write some code.</think>\nconst a = 1;' |
| 83 | + expect(cleanup_api_response({ content })).toBe('const a = 1;') |
| 84 | + }) |
| 85 | + |
| 86 | + it('should remove <thought> block from the beginning', () => { |
| 87 | + const content = |
| 88 | + '<thought>I should write some code.</thought>\nconst a = 1;' |
| 89 | + expect(cleanup_api_response({ content })).toBe('const a = 1;') |
| 90 | + }) |
| 91 | + |
| 92 | + it('should handle <think> block with newlines', () => { |
| 93 | + const content = |
| 94 | + '<think>\nI should write some code.\n</think>\nconst a = 1;' |
| 95 | + expect(cleanup_api_response({ content })).toBe('const a = 1;') |
| 96 | + }) |
| 97 | + |
| 98 | + it('should only remove <think> block if it is at the start', () => { |
| 99 | + const content = 'const a = 1;\n<think>I should write some code.</think>' |
| 100 | + expect(cleanup_api_response({ content })).toBe( |
| 101 | + 'const a = 1;\n<think>I should write some code.</think>' |
| 102 | + ) |
| 103 | + }) |
| 104 | + }) |
| 105 | +}) |
0 commit comments