@@ -113,4 +113,53 @@ describe('/internal Routes', () => {
113
113
expect ( list . objects . length ) . toBe ( 0 ) ;
114
114
} ) ;
115
115
} ) ;
116
+
117
+ describe ( '/internal/count-objects route' , ( ) => {
118
+ beforeEach ( ( ) => {
119
+ workerEnv = getMiniflareBindings ( ) ;
120
+ ctx = new ExecutionContext ( ) ;
121
+ } ) ;
122
+
123
+ afterEach ( ( ) => {
124
+ vi . restoreAllMocks ( ) ;
125
+ } ) ;
126
+
127
+ test ( 'should return the number of objects in R2 when bucket is empty' , async ( ) => {
128
+ const request = new Request ( 'http://localhost/internal/count-objects' , {
129
+ method : 'GET' ,
130
+ headers : {
131
+ 'Content-Type' : 'application/json' ,
132
+ Authorization : `Bearer ${ workerEnv . TURBO_TOKEN } ` ,
133
+ } ,
134
+ } ) ;
135
+ const response = await app . fetch ( request , workerEnv , ctx ) ;
136
+ expect ( response . status ) . toBe ( 200 ) ;
137
+ expect ( await response . json ( ) ) . toEqual ( { count : 0 } ) ;
138
+ } ) ;
139
+
140
+ test ( 'should return the number of objects in R2 when bucket is not empty' , async ( ) => {
141
+ await workerEnv . R2_STORE . put ( 'key' , 'value' ) ;
142
+ const request = new Request ( 'http://localhost/internal/count-objects' , {
143
+ method : 'GET' ,
144
+ headers : {
145
+ 'Content-Type' : 'application/json' ,
146
+ Authorization : `Bearer ${ workerEnv . TURBO_TOKEN } ` ,
147
+ } ,
148
+ } ) ;
149
+ const response = await app . fetch ( request , workerEnv , ctx ) ;
150
+ expect ( response . status ) . toBe ( 200 ) ;
151
+ expect ( await response . json ( ) ) . toEqual ( { count : 1 } ) ;
152
+ } ) ;
153
+
154
+ test ( 'should return 401 if no auth token is provided' , async ( ) => {
155
+ const request = new Request ( 'http://localhost/internal/count-objects' , {
156
+ method : 'GET' ,
157
+ headers : {
158
+ 'Content-Type' : 'application/json' ,
159
+ } ,
160
+ } ) ;
161
+ const response = await app . fetch ( request , workerEnv , ctx ) ;
162
+ expect ( response . status ) . toBe ( 401 ) ;
163
+ } ) ;
164
+ } ) ;
116
165
} ) ;
0 commit comments