|
| 1 | +import { describe, expect, it } from 'vitest' |
| 2 | +import { statsFor } from '../../../src/lib/enhancers/draftStats' |
| 3 | + |
| 4 | +describe('statsFor', () => { |
| 5 | + it('should handle empty markdown', () => { |
| 6 | + expect(statsFor('')).toMatchInlineSnapshot(` |
| 7 | + { |
| 8 | + "charCount": 0, |
| 9 | + "codeBlocks": [], |
| 10 | + "images": [], |
| 11 | + "links": [], |
| 12 | + } |
| 13 | + `) |
| 14 | + }) |
| 15 | + |
| 16 | + it('should count characters', () => { |
| 17 | + expect(statsFor('Hello world')).toMatchInlineSnapshot(` |
| 18 | + { |
| 19 | + "charCount": 11, |
| 20 | + "codeBlocks": [], |
| 21 | + "images": [], |
| 22 | + "links": [], |
| 23 | + } |
| 24 | + `) |
| 25 | + }) |
| 26 | + |
| 27 | + it('should extract images with alt text', () => { |
| 28 | + expect(statsFor('')).toMatchInlineSnapshot(` |
| 29 | + { |
| 30 | + "charCount": 42, |
| 31 | + "codeBlocks": [], |
| 32 | + "images": [ |
| 33 | + { |
| 34 | + "alt": "Alt text", |
| 35 | + "url": "https://example.com/image.png", |
| 36 | + }, |
| 37 | + ], |
| 38 | + "links": [], |
| 39 | + } |
| 40 | + `) |
| 41 | + }) |
| 42 | + |
| 43 | + it('should extract images without alt text', () => { |
| 44 | + expect(statsFor('')).toMatchInlineSnapshot(` |
| 45 | + { |
| 46 | + "charCount": 34, |
| 47 | + "codeBlocks": [], |
| 48 | + "images": [ |
| 49 | + { |
| 50 | + "url": "https://example.com/image.png", |
| 51 | + }, |
| 52 | + ], |
| 53 | + "links": [], |
| 54 | + } |
| 55 | + `) |
| 56 | + }) |
| 57 | + |
| 58 | + it('should extract links', () => { |
| 59 | + expect(statsFor('[Link text](https://example.com)')).toMatchInlineSnapshot(` |
| 60 | + { |
| 61 | + "charCount": 32, |
| 62 | + "codeBlocks": [], |
| 63 | + "images": [], |
| 64 | + "links": [ |
| 65 | + { |
| 66 | + "text": "Link text", |
| 67 | + "url": "https://example.com", |
| 68 | + }, |
| 69 | + ], |
| 70 | + } |
| 71 | + `) |
| 72 | + }) |
| 73 | + |
| 74 | + it('should extract code blocks with language', () => { |
| 75 | + expect(statsFor('```javascript\nconsole.log("hello")\n```')).toMatchInlineSnapshot(` |
| 76 | + { |
| 77 | + "charCount": 38, |
| 78 | + "codeBlocks": [ |
| 79 | + { |
| 80 | + "code": "console.log("hello") |
| 81 | + ", |
| 82 | + "language": "javascript", |
| 83 | + }, |
| 84 | + ], |
| 85 | + "images": [], |
| 86 | + "links": [], |
| 87 | + } |
| 88 | + `) |
| 89 | + }) |
| 90 | + |
| 91 | + it('should extract code blocks without language', () => { |
| 92 | + expect(statsFor('```\nconsole.log("hello")\n```')).toMatchInlineSnapshot(` |
| 93 | + { |
| 94 | + "charCount": 28, |
| 95 | + "codeBlocks": [ |
| 96 | + { |
| 97 | + "code": "console.log("hello") |
| 98 | + ", |
| 99 | + }, |
| 100 | + ], |
| 101 | + "images": [], |
| 102 | + "links": [], |
| 103 | + } |
| 104 | + `) |
| 105 | + }) |
| 106 | + |
| 107 | + it('should handle complex markdown with multiple elements', () => { |
| 108 | + const markdown = `# Title |
| 109 | +
|
| 110 | +Here's some text with a [link](https://example.com) and an . |
| 111 | +
|
| 112 | +\`\`\`typescript |
| 113 | +function hello() { |
| 114 | + return "world" |
| 115 | +} |
| 116 | +\`\`\` |
| 117 | +
|
| 118 | +More text with another  and [another link](https://test.com). |
| 119 | +
|
| 120 | +\`\`\` |
| 121 | +plain code block |
| 122 | +\`\`\`` |
| 123 | + |
| 124 | + expect(statsFor(markdown)).toMatchInlineSnapshot(` |
| 125 | + { |
| 126 | + "charCount": 293, |
| 127 | + "codeBlocks": [ |
| 128 | + { |
| 129 | + "code": "function hello() { |
| 130 | + return "world" |
| 131 | + } |
| 132 | + ", |
| 133 | + "language": "typescript", |
| 134 | + }, |
| 135 | + { |
| 136 | + "code": "plain code block |
| 137 | + ", |
| 138 | + }, |
| 139 | + ], |
| 140 | + "images": [ |
| 141 | + { |
| 142 | + "alt": "image", |
| 143 | + "url": "https://example.com/img.png", |
| 144 | + }, |
| 145 | + { |
| 146 | + "alt": "alt text", |
| 147 | + "url": "https://example.com/img2.jpg", |
| 148 | + }, |
| 149 | + ], |
| 150 | + "links": [ |
| 151 | + { |
| 152 | + "text": "link", |
| 153 | + "url": "https://example.com", |
| 154 | + }, |
| 155 | + { |
| 156 | + "text": "another link", |
| 157 | + "url": "https://test.com", |
| 158 | + }, |
| 159 | + ], |
| 160 | + } |
| 161 | + `) |
| 162 | + }) |
| 163 | +}) |
0 commit comments