@@ -14,7 +14,9 @@ const mockCacheSet = vi.fn();
1414// Mock undici fetch
1515const mockFetch = vi . fn ( ) ;
1616vi . mock ( 'undici' , ( ) => ( {
17- Agent : vi . fn ( ) . mockImplementation ( ( ) => ( { } ) ) ,
17+ Agent : vi . fn ( function MockAgent ( ) {
18+ return { } ;
19+ } ) ,
1820 fetch : mockFetch ,
1921} ) ) ;
2022
@@ -233,6 +235,38 @@ describe('IPTV Channels API', () => {
233235 expect ( data . limit ) . toBe ( 10 ) ;
234236 } ) ;
235237
238+ it . each ( [
239+ [ 'negative' , '-5' , '-20' ] ,
240+ [ 'partially numeric' , '10items' , '20items' ] ,
241+ ] ) ( 'uses pagination defaults for %s values' , async ( _label , limit , offset ) => {
242+ const allChannels = Array . from ( { length : 100 } , ( _ , i ) => ( {
243+ id : String ( i ) ,
244+ name : `Channel ${ i } ` ,
245+ url : `https://example.com/ch${ i } .m3u8` ,
246+ group : 'General' ,
247+ } ) ) ;
248+ mockCacheGet . mockResolvedValue ( {
249+ channels : allChannels ,
250+ groups : [ 'General' ] ,
251+ fetchedAt : Date . now ( ) ,
252+ m3uUrl : 'http://example.com/playlist.m3u' ,
253+ } ) ;
254+ vi . mocked ( searchChannels ) . mockReturnValue ( allChannels ) ;
255+
256+ const request = new NextRequest (
257+ `http://localhost/api/iptv/channels?m3uUrl=http://example.com/playlist.m3u&limit=${ limit } &offset=${ offset } `
258+ ) ;
259+
260+ const response = await GET ( request ) ;
261+ const data = await response . json ( ) ;
262+
263+ expect ( response . status ) . toBe ( 200 ) ;
264+ expect ( data . channels ) . toHaveLength ( 50 ) ;
265+ expect ( data . limit ) . toBe ( 50 ) ;
266+ expect ( data . offset ) . toBe ( 0 ) ;
267+ expect ( data . channels [ 0 ] . id ) . toBe ( '0' ) ;
268+ } ) ;
269+
236270 it ( 'returns 502 when upstream fetch fails' , async ( ) => {
237271 mockCacheGet . mockResolvedValue ( null ) ;
238272
0 commit comments