@@ -177,6 +177,64 @@ describe("PublicAPIConverter", () => {
177
177
} ) ;
178
178
} ) ;
179
179
180
+ describe ( "toDuration" , ( ) => {
181
+ it ( "should convert with 0" , ( ) => {
182
+ expect ( converter . toDuration ( "" ) . seconds ) . to . equal ( BigInt ( 0 ) ) ;
183
+ expect ( converter . toDuration ( " " ) . seconds ) . to . equal ( BigInt ( 0 ) ) ;
184
+ expect ( converter . toDuration ( "0" ) . seconds ) . to . equal ( BigInt ( 0 ) ) ;
185
+ expect ( converter . toDuration ( "0ms" ) . seconds ) . to . equal ( BigInt ( 0 ) ) ;
186
+ expect ( converter . toDuration ( "0s" ) . seconds ) . to . equal ( BigInt ( 0 ) ) ;
187
+ expect ( converter . toDuration ( "0m" ) . seconds ) . to . equal ( BigInt ( 0 ) ) ;
188
+ expect ( converter . toDuration ( "0h" ) . seconds ) . to . equal ( BigInt ( 0 ) ) ;
189
+ } ) ;
190
+ it ( "should convert with hours" , ( ) => {
191
+ expect ( converter . toDuration ( "1h" ) . seconds ) . to . equal ( BigInt ( 3600 ) ) ;
192
+ expect ( converter . toDuration ( "24h" ) . seconds ) . to . equal ( BigInt ( 3600 * 24 ) ) ;
193
+ expect ( converter . toDuration ( "25h" ) . seconds ) . to . equal ( BigInt ( 3600 * 25 ) ) ;
194
+ } ) ;
195
+ it ( "should convert with minutes" , ( ) => {
196
+ expect ( converter . toDuration ( "1m" ) . seconds ) . to . equal ( BigInt ( 60 ) ) ;
197
+ expect ( converter . toDuration ( "120m" ) . seconds ) . to . equal ( BigInt ( 120 * 60 ) ) ;
198
+ } ) ;
199
+ it ( "should convert with seconds" , ( ) => {
200
+ expect ( converter . toDuration ( "1s" ) . seconds ) . to . equal ( BigInt ( 1 ) ) ;
201
+ expect ( converter . toDuration ( "120s" ) . seconds ) . to . equal ( BigInt ( 120 ) ) ;
202
+ } ) ;
203
+ it ( "should convert with mixed" , ( ) => {
204
+ expect ( converter . toDuration ( "1h20m" ) . seconds ) . to . equal ( BigInt ( 3600 + 20 * 60 ) ) ;
205
+ expect ( converter . toDuration ( "1h0m0s" ) . seconds ) . to . equal ( BigInt ( 3600 ) ) ;
206
+ expect ( converter . toDuration ( "20m1h" ) . seconds ) . to . equal ( BigInt ( 3600 + 20 * 60 ) ) ;
207
+ expect ( converter . toDuration ( "20m25h1s" ) . seconds ) . to . equal ( BigInt ( 25 * 3600 + 20 * 60 + 1 ) ) ;
208
+ } ) ;
209
+ } ) ;
210
+
211
+ describe ( "toDurationString" , ( ) => {
212
+ it ( "should convert with 0" , ( ) => {
213
+ expect ( converter . toDurationString ( new Duration ( ) ) ) . to . equal ( "0" ) ;
214
+ expect ( converter . toDurationString ( new Duration ( { seconds : BigInt ( 0 ) } ) ) ) . to . equal ( "0" ) ;
215
+ expect ( converter . toDurationString ( new Duration ( { nanos : 0 } ) ) ) . to . equal ( "0" ) ;
216
+ } ) ;
217
+ it ( "should convert with hours" , ( ) => {
218
+ expect ( converter . toDurationString ( new Duration ( { seconds : BigInt ( 3600 ) } ) ) ) . to . equal ( "1h" ) ;
219
+ expect ( converter . toDurationString ( new Duration ( { seconds : BigInt ( 3600 * 24 ) } ) ) ) . to . equal ( "24h" ) ;
220
+ expect ( converter . toDurationString ( new Duration ( { seconds : BigInt ( 3600 * 25 ) } ) ) ) . to . equal ( "25h" ) ;
221
+ } ) ;
222
+ it ( "should convert with minutes" , ( ) => {
223
+ expect ( converter . toDurationString ( new Duration ( { seconds : BigInt ( 60 ) } ) ) ) . to . equal ( "1m" ) ;
224
+ expect ( converter . toDurationString ( new Duration ( { seconds : BigInt ( 2 * 60 * 60 ) } ) ) ) . to . equal ( "2h" ) ;
225
+ } ) ;
226
+ it ( "should convert with seconds" , ( ) => {
227
+ expect ( converter . toDurationString ( new Duration ( { seconds : BigInt ( 1 ) } ) ) ) . to . equal ( "1s" ) ;
228
+ expect ( converter . toDurationString ( new Duration ( { seconds : BigInt ( 120 ) } ) ) ) . to . equal ( "2m" ) ;
229
+ } ) ;
230
+ it ( "should convert with mixed" , ( ) => {
231
+ expect ( converter . toDurationString ( new Duration ( { seconds : BigInt ( 3600 + 20 * 60 ) } ) ) ) . to . equal ( "1h20m" ) ;
232
+ expect ( converter . toDurationString ( new Duration ( { seconds : BigInt ( 25 * 3600 + 20 * 60 + 1 ) } ) ) ) . to . equal (
233
+ "25h20m1s" ,
234
+ ) ;
235
+ } ) ;
236
+ } ) ;
237
+
180
238
describe ( "errors" , ( ) => {
181
239
it ( "USER_BLOCKED" , ( ) => {
182
240
const connectError = converter . toError ( new ApplicationError ( ErrorCodes . USER_BLOCKED , "user blocked" ) ) ;
0 commit comments