|
3 | 3 | */ |
4 | 4 |
|
5 | 5 | import { describe, it, expect } from 'vitest'; |
6 | | -import { generateInterceptorJs, generateReadInterceptedJs, generateTapInterceptorJs } from './interceptor.js'; |
| 6 | +import { generateInterceptorJs, generateReadInterceptedJs, generateTapInterceptorJs, generateStreamingInterceptorJs, generateReadStreamJs } from './interceptor.js'; |
7 | 7 |
|
8 | 8 | describe('generateInterceptorJs', () => { |
9 | 9 | it('generates valid JavaScript function source', () => { |
@@ -92,3 +92,132 @@ describe('generateTapInterceptorJs', () => { |
92 | 92 | expect(tap.xhrPatch).toContain('!captured'); |
93 | 93 | }); |
94 | 94 | }); |
| 95 | + |
| 96 | +describe('generateStreamingInterceptorJs', () => { |
| 97 | + it('generates valid JavaScript function source', () => { |
| 98 | + const js = generateStreamingInterceptorJs('"api/stream"'); |
| 99 | + expect(js.trim()).toMatch(/^\(\)\s*=>/); |
| 100 | + expect(js).toContain('"api/stream"'); |
| 101 | + }); |
| 102 | + |
| 103 | + it('initializes all streaming state variables', () => { |
| 104 | + const js = generateStreamingInterceptorJs('"test"'); |
| 105 | + expect(js).toContain('__opencli_stream_text'); |
| 106 | + expect(js).toContain('__opencli_stream_events'); |
| 107 | + expect(js).toContain('__opencli_stream_done'); |
| 108 | + expect(js).toContain('__opencli_stream_errors'); |
| 109 | + expect(js).toContain('__opencli_stream_sse_buf'); |
| 110 | + }); |
| 111 | + |
| 112 | + it('resets SSE buffer on install', () => { |
| 113 | + const js = generateStreamingInterceptorJs('"test"'); |
| 114 | + // SSE buffer should be reset at initialization time |
| 115 | + expect(js).toContain('__opencli_stream_sse_buf'); |
| 116 | + expect(js).toContain("= ''"); |
| 117 | + }); |
| 118 | + |
| 119 | + it('resets all state including SSE buffer on each new matching fetch', () => { |
| 120 | + const js = generateStreamingInterceptorJs('"test"'); |
| 121 | + // Inside the fetch patch, SSE buffer should be cleared for new request |
| 122 | + const fetchResetCount = (js.match(/__opencli_stream_sse_buf/g) || []).length; |
| 123 | + // Should appear at least twice: init + fetch reset + XHR reset |
| 124 | + expect(fetchResetCount).toBeGreaterThanOrEqual(2); |
| 125 | + }); |
| 126 | + |
| 127 | + it('patches both fetch and XHR', () => { |
| 128 | + const js = generateStreamingInterceptorJs('"test"'); |
| 129 | + expect(js).toContain('window.fetch'); |
| 130 | + expect(js).toContain('XMLHttpRequest.prototype'); |
| 131 | + expect(js).toContain('onprogress'); |
| 132 | + expect(js).toContain('readystatechange'); |
| 133 | + }); |
| 134 | + |
| 135 | + it('normalizes CRLF in SSE parsing', () => { |
| 136 | + const js = generateStreamingInterceptorJs('"test"'); |
| 137 | + // SSE parser should normalize \r\n to \n |
| 138 | + expect(js).toContain(String.raw`replace(/\r\n/g, '\n')`); |
| 139 | + }); |
| 140 | + |
| 141 | + it('normalizes CRLF in fetch SSE boundary detection', () => { |
| 142 | + const js = generateStreamingInterceptorJs('"test"'); |
| 143 | + // Fetch path should normalize CRLF before SSE boundary splitting |
| 144 | + expect(js).toContain('sseBuffer'); |
| 145 | + expect(js).toContain(String.raw`replace(/\r\n/g, '\n')`); |
| 146 | + }); |
| 147 | + |
| 148 | + it('normalizes CRLF in XHR SSE boundary detection', () => { |
| 149 | + const js = generateStreamingInterceptorJs('"test"'); |
| 150 | + // XHR progress path should normalize CRLF |
| 151 | + expect(js).toContain(String.raw`chunk.replace(/\r\n/g, '\n')`); |
| 152 | + }); |
| 153 | + |
| 154 | + it('includes SSE parser function', () => { |
| 155 | + const js = generateStreamingInterceptorJs('"test"'); |
| 156 | + expect(js).toContain('__parseSse'); |
| 157 | + expect(js).toContain('event:'); |
| 158 | + expect(js).toContain('data:'); |
| 159 | + }); |
| 160 | + |
| 161 | + it('uses custom prefix and patch guard', () => { |
| 162 | + const js = generateStreamingInterceptorJs('"test"', { |
| 163 | + arrayName: '__my_stream', |
| 164 | + patchGuard: '__my_stream_guard', |
| 165 | + }); |
| 166 | + expect(js).toContain('__my_stream_text'); |
| 167 | + expect(js).toContain('__my_stream_events'); |
| 168 | + expect(js).toContain('__my_stream_guard'); |
| 169 | + expect(js).not.toContain('__opencli_stream'); |
| 170 | + }); |
| 171 | + |
| 172 | + it('uses custom maxChunks', () => { |
| 173 | + const js = generateStreamingInterceptorJs('"test"', { maxChunks: 100 }); |
| 174 | + expect(js).toContain('100'); |
| 175 | + }); |
| 176 | + |
| 177 | + it('XHR readystatechange uses authoritative responseText overwrite', () => { |
| 178 | + const js = generateStreamingInterceptorJs('"test"'); |
| 179 | + // readystatechange readyState=4 should overwrite with full responseText |
| 180 | + expect(js).toContain('readyState === 4'); |
| 181 | + expect(js).toContain('window.__opencli_stream_text = full'); |
| 182 | + }); |
| 183 | + |
| 184 | + it('XHR load event is guarded by settled flag', () => { |
| 185 | + const js = generateStreamingInterceptorJs('"test"'); |
| 186 | + expect(js).toContain('if (settled) return'); |
| 187 | + }); |
| 188 | +}); |
| 189 | + |
| 190 | +describe('generateReadStreamJs', () => { |
| 191 | + it('generates valid JavaScript to read streaming state', () => { |
| 192 | + const js = generateReadStreamJs(); |
| 193 | + expect(js.trim()).toMatch(/^\(\)\s*=>/); |
| 194 | + expect(js).toContain('__opencli_stream_text'); |
| 195 | + expect(js).toContain('__opencli_stream_events'); |
| 196 | + expect(js).toContain('__opencli_stream_done'); |
| 197 | + expect(js).toContain('__opencli_stream_errors'); |
| 198 | + }); |
| 199 | + |
| 200 | + it('clears all state including SSE buffer by default', () => { |
| 201 | + const js = generateReadStreamJs(); |
| 202 | + expect(js).toContain('__opencli_stream_text'); |
| 203 | + expect(js).toContain('__opencli_stream_sse_buf'); |
| 204 | + expect(js).toContain("= ''"); |
| 205 | + expect(js).toContain('= []'); |
| 206 | + }); |
| 207 | + |
| 208 | + it('does not clear state when clear=false (peek mode)', () => { |
| 209 | + const js = generateReadStreamJs('__opencli_stream', false); |
| 210 | + // Should NOT contain clearing statements |
| 211 | + expect(js).not.toContain("window.__opencli_stream_text = ''"); |
| 212 | + expect(js).not.toContain("window.__opencli_stream_sse_buf = ''"); |
| 213 | + // But should still contain the read statements |
| 214 | + expect(js).toContain('__opencli_stream_text'); |
| 215 | + }); |
| 216 | + |
| 217 | + it('uses custom prefix', () => { |
| 218 | + const js = generateReadStreamJs('__my_prefix'); |
| 219 | + expect(js).toContain('__my_prefix_text'); |
| 220 | + expect(js).toContain('__my_prefix_events'); |
| 221 | + expect(js).not.toContain('__opencli_stream'); |
| 222 | + }); |
| 223 | +}); |
0 commit comments