11/*!
2- * Copyright (c) 2021-2023 Digital Bazaar, Inc. All rights reserved.
2+ * Copyright (c) 2021-2025 Digital Bazaar, Inc. All rights reserved.
33 */
44import {
55 CapabilityDelegation ,
@@ -132,13 +132,15 @@ export class ZcapClient {
132132 * @param {string|Array } [options.allowedActions] - Optional list of allowed
133133 * actions or string specifying allowed delegated action. Default: [] -
134134 * delegate all actions.
135+ * @param {number } [options.now] - Optional timestamp for the current time;
136+ * useful for testing.
135137 *
136138 * @returns {Promise<object> } - A promise that resolves to a delegated
137139 * capability.
138140 */
139141 async delegate ( {
140142 capability, controller, invocationTarget, expires,
141- allowedActions
143+ allowedActions, now = Date . now ( )
142144 } = { } ) {
143145 if ( ! ( typeof controller === 'string' && controller . includes ( ':' ) ) ) {
144146 throw new Error (
@@ -161,7 +163,7 @@ export class ZcapClient {
161163
162164 if ( expires === undefined ) {
163165 // default expiration is 5 minutes in the future
164- expires = new Date ( Date . now ( ) + 5 * 60 * 1000 ) ;
166+ expires = new Date ( now + 5 * 60 * 1000 ) ;
165167 } else if ( expires instanceof Date || typeof expires === 'string' ) {
166168 // ensure expires is a valid date
167169 const time = Date . parse ( expires ) ;
@@ -242,7 +244,7 @@ export class ZcapClient {
242244
243245 // ensure delegation date will not be at least after parent delegation date
244246 const [ parentProof ] = _getDelegationProofs ( { capability} ) ;
245- let date = new Date ( ) ;
247+ let date = new Date ( now ) ;
246248 if ( parentProof ) {
247249 const parentDelegationDate = new Date ( parentProof . created ) ;
248250 if ( date < parentDelegationDate ) {
0 commit comments