|
1 | | -import {applyConciergeDraftEvent, getCachedDraft, setCachedDraft} from '@pages/inbox/conciergeDraftState'; |
| 1 | +import {applyConciergeDraftEvent, getCachedDraft, setCachedDraft, stripIncompleteMarkdown} from '@pages/inbox/conciergeDraftState'; |
2 | 2 | import CONST from '@src/CONST'; |
3 | 3 |
|
4 | 4 | const REPORT_ID = '123'; |
@@ -132,6 +132,87 @@ describe('conciergeDraftState', () => { |
132 | 132 | expect(otherReportDraft).toBe(initialDraft); |
133 | 133 | }); |
134 | 134 |
|
| 135 | + describe('stripIncompleteMarkdown', () => { |
| 136 | + it('returns empty/falsy values unchanged', () => { |
| 137 | + expect(stripIncompleteMarkdown('')).toBe(''); |
| 138 | + }); |
| 139 | + |
| 140 | + it('does not alter complete markdown', () => { |
| 141 | + const complete = 'Hello **bold** and [link](https://example.com) and `code`'; |
| 142 | + expect(stripIncompleteMarkdown(complete)).toBe(complete); |
| 143 | + }); |
| 144 | + |
| 145 | + // --- Links / Images --- |
| 146 | + it('strips an incomplete link with only opening bracket', () => { |
| 147 | + expect(stripIncompleteMarkdown('Check out [')).toBe('Check out '); |
| 148 | + }); |
| 149 | + |
| 150 | + it('strips an incomplete link with text but no closing bracket', () => { |
| 151 | + expect(stripIncompleteMarkdown('Check out [this page')).toBe('Check out '); |
| 152 | + }); |
| 153 | + |
| 154 | + it('strips an incomplete link with bracket closed but no URL', () => { |
| 155 | + expect(stripIncompleteMarkdown('Check out [link](')).toBe('Check out '); |
| 156 | + }); |
| 157 | + |
| 158 | + it('strips an incomplete link with partial URL', () => { |
| 159 | + expect(stripIncompleteMarkdown('Check out [link](https://example')).toBe('Check out '); |
| 160 | + }); |
| 161 | + |
| 162 | + it('preserves a complete link followed by an incomplete one', () => { |
| 163 | + expect(stripIncompleteMarkdown('[done](https://a.com) and [broken')).toBe('[done](https://a.com) and '); |
| 164 | + }); |
| 165 | + |
| 166 | + it('strips an incomplete image syntax', () => { |
| 167 | + expect(stripIncompleteMarkdown('Here is ![alt')).toBe('Here is '); |
| 168 | + }); |
| 169 | + |
| 170 | + // --- Bold (**) --- |
| 171 | + it('strips trailing unclosed bold', () => { |
| 172 | + expect(stripIncompleteMarkdown('Hello **world')).toBe('Hello '); |
| 173 | + }); |
| 174 | + |
| 175 | + it('strips bare trailing ** delimiter', () => { |
| 176 | + expect(stripIncompleteMarkdown('Hello **')).toBe('Hello '); |
| 177 | + }); |
| 178 | + |
| 179 | + it('preserves complete bold and strips only the unclosed one', () => { |
| 180 | + expect(stripIncompleteMarkdown('**done** and **broken')).toBe('**done** and '); |
| 181 | + }); |
| 182 | + |
| 183 | + // --- Strikethrough (~~) --- |
| 184 | + it('strips trailing unclosed strikethrough', () => { |
| 185 | + expect(stripIncompleteMarkdown('Hello ~~strike')).toBe('Hello '); |
| 186 | + }); |
| 187 | + |
| 188 | + // --- Code blocks (```) --- |
| 189 | + it('strips an unclosed code block', () => { |
| 190 | + expect(stripIncompleteMarkdown('Here:\n```\ncode')).toBe('Here:\n'); |
| 191 | + }); |
| 192 | + |
| 193 | + it('preserves a complete code block', () => { |
| 194 | + const complete = 'Before\n```\ncode\n```\nAfter'; |
| 195 | + expect(stripIncompleteMarkdown(complete)).toBe(complete); |
| 196 | + }); |
| 197 | + |
| 198 | + // --- Inline code (`) --- |
| 199 | + it('strips trailing unclosed inline code', () => { |
| 200 | + expect(stripIncompleteMarkdown('Run `command')).toBe('Run '); |
| 201 | + }); |
| 202 | + |
| 203 | + it('preserves complete inline code', () => { |
| 204 | + const complete = 'Run `command` now'; |
| 205 | + expect(stripIncompleteMarkdown(complete)).toBe(complete); |
| 206 | + }); |
| 207 | + |
| 208 | + // --- Streaming integration --- |
| 209 | + it('strips incomplete markdown during a streaming draft event', () => { |
| 210 | + const draft = applyConciergeDraftEvent(null, createDraftEvent({bodyMarkdown: 'Check [this link'}), REPORT_ID); |
| 211 | + // The raw '[this link' syntax should NOT appear in the rendered HTML |
| 212 | + expect(getFirstMessageHTML(draft)).not.toContain('[this link'); |
| 213 | + }); |
| 214 | + }); |
| 215 | + |
135 | 216 | describe('draftCache', () => { |
136 | 217 | // Always start clean so tests don't leak state into each other. |
137 | 218 | beforeEach(() => { |
|
0 commit comments