@@ -10,35 +10,98 @@ test('SeamPaginator: creates a SeamPaginator', async (t) => {
1010 t . true ( pages instanceof SeamPaginator )
1111} )
1212
13- test ( 'SeamPaginator: fetches an array of devices ' , async ( t ) => {
13+ test ( 'SeamPaginator: cannot paginate a request with an empty response ' , async ( t ) => {
1414 const { seed, endpoint } = await getTestServer ( t )
1515 const seam = SeamHttp . fromApiKey ( seed . seam_apikey1_token , { endpoint } )
16- const pages = seam . createPaginator ( seam . devices . list ( ) )
1716
17+ // @ts -expect-error Testing validation
18+ t . throws ( ( ) => seam . createPaginator ( seam . devices . update ( ) ) , {
19+ message : / d o e s n o t s u p p o r t p a g i n a t i o n / ,
20+ } )
21+ } )
22+
23+ // TODO: Validate the request supports pagination by extending SeamHttpRequest with this knowledge via codegen.
24+ test . failing (
25+ 'SeamPaginator: cannot paginate an request that does not return pagination data' ,
26+ async ( t ) => {
27+ const { seed, endpoint } = await getTestServer ( t )
28+ const seam = SeamHttp . fromApiKey ( seed . seam_apikey1_token , { endpoint } )
29+
30+ t . throws ( ( ) => seam . createPaginator ( seam . workspaces . list ( ) ) , {
31+ message : / d o e s n o t s u p p o r t p a g i n a t i o n / ,
32+ } )
33+ } ,
34+ )
35+
36+ test ( 'SeamPaginator: firstPage returns the first page' , async ( t ) => {
37+ const { seed, endpoint } = await getTestServer ( t )
38+ const seam = SeamHttp . fromApiKey ( seed . seam_apikey1_token , { endpoint } )
39+ const pages = seam . createPaginator ( seam . devices . list ( { limit : 2 } ) )
40+ const [ devices , pagination ] = await pages . firstPage ( )
41+ t . is ( devices . length , 2 )
42+ t . true ( pagination . hasNextPage )
43+ t . truthy ( pagination . nextPageCursor )
44+ t . truthy ( pagination . nextPageUrl )
45+ } )
46+
47+ test ( 'SeamPaginator: nextPage returns the next page' , async ( t ) => {
48+ const { seed, endpoint } = await getTestServer ( t )
49+ const seam = SeamHttp . fromApiKey ( seed . seam_apikey1_token , { endpoint } )
50+ const pages = seam . createPaginator ( seam . devices . list ( { limit : 2 } ) )
51+ const [ devices , { hasNextPage, nextPageCursor } ] = await pages . firstPage ( )
52+ t . is ( devices . length , 2 )
53+ t . true ( hasNextPage )
54+ const [ moreDevices ] = await pages . nextPage ( nextPageCursor )
55+ t . is ( moreDevices . length , 2 )
56+ } )
57+
58+ test ( 'SeamPaginator: nextPage requires the nextPageCursor' , async ( t ) => {
59+ const { seed, endpoint } = await getTestServer ( t )
60+ const seam = SeamHttp . fromApiKey ( seed . seam_apikey1_token , { endpoint } )
61+ const pages = seam . createPaginator ( seam . devices . list ( { limit : 2 } ) )
62+ await t . throwsAsync ( async ( ) => await pages . nextPage ( null ) , {
63+ message : / n e x t P a g e C u r s o r / ,
64+ } )
65+ } )
66+
67+ test ( 'SeamPaginator: toArray returns an array of devices' , async ( t ) => {
68+ const { seed, endpoint } = await getTestServer ( t )
69+ const seam = SeamHttp . fromApiKey ( seed . seam_apikey1_token , { endpoint } )
70+ const allDevices = await seam . devices . list ( )
71+ const pages = seam . createPaginator ( seam . devices . list ( { limit : 1 } ) )
1872 const devices = await pages . toArray ( )
1973 t . true ( devices . length > 1 )
74+ t . is ( devices . length , allDevices . length )
2075} )
2176
22- test ( 'SeamPaginator: flattens an array of devices' , async ( t ) => {
77+ test ( 'SeamPaginator: flatten allows iteration over all devices' , async ( t ) => {
2378 const { seed, endpoint } = await getTestServer ( t )
2479 const seam = SeamHttp . fromApiKey ( seed . seam_apikey1_token , { endpoint } )
25- const pages = seam . createPaginator ( seam . devices . list ( ) )
80+ const allDevices = await seam . devices . list ( )
81+ const pages = seam . createPaginator ( seam . devices . list ( { limit : 1 } ) )
2682
2783 const devices = [ ]
2884 for await ( const device of pages . flatten ( ) ) {
2985 devices . push ( device )
3086 }
3187 t . true ( devices . length > 1 )
88+ t . is ( devices . length , allDevices . length )
3289} )
3390
34- test ( 'SeamPaginator: Fetches an array of pages' , async ( t ) => {
91+ test ( 'SeamPaginator: instance allows iteration over all pages' , async ( t ) => {
3592 const { seed, endpoint } = await getTestServer ( t )
3693 const seam = SeamHttp . fromApiKey ( seed . seam_apikey1_token , { endpoint } )
37- const pages = seam . createPaginator ( seam . devices . list ( ) )
94+ const allDevices = await seam . devices . list ( )
95+ const pages = seam . createPaginator ( seam . devices . list ( { limit : 1 } ) )
3896
3997 const devices = [ ]
98+ const allPages = [ ]
4099 for await ( const page of pages ) {
41- devices . push ( page )
100+ t . is ( page . length , 1 )
101+ allPages . push ( page )
102+ devices . push ( ...page )
42103 }
43- t . true ( devices . length > 0 )
104+ t . true ( allPages . length > 1 )
105+ t . true ( devices . length > 1 )
106+ t . is ( devices . length , allDevices . length )
44107} )
0 commit comments