@@ -4,9 +4,9 @@ import { randomBytes } from '@libp2p/crypto'
4
4
import { generateKeyPair , publicKeyToProtobuf } from '@libp2p/crypto/keys'
5
5
import { expect } from 'aegir/chai'
6
6
import { toString as uint8ArrayToString } from 'uint8arrays/to-string'
7
- import { InvalidEmbeddedPublicKeyError , RecordTooLargeError , SignatureVerificationError } from '../src/errors.js'
7
+ import { InvalidEmbeddedPublicKeyError , RecordTooLargeError , SignatureVerificationError , UnsupportedValidityError } from '../src/errors.js'
8
8
import { createIPNSRecord , marshalIPNSRecord , multihashToIPNSRoutingKey } from '../src/index.js'
9
- import { ipnsValidator } from '../src/validator.js'
9
+ import { ipnsValidator , validFor } from '../src/validator.js'
10
10
import type { PrivateKey } from '@libp2p/interface'
11
11
12
12
describe ( 'validator' , function ( ) {
@@ -91,4 +91,40 @@ describe('validator', function () {
91
91
await expect ( ipnsValidator ( key , marshalledData ) ) . to . eventually . be . rejected ( )
92
92
. with . property ( 'name' , RecordTooLargeError . name )
93
93
} )
94
+
95
+ describe ( 'validFor' , ( ) => {
96
+ it ( 'should return the number of milliseconds until the record expires' , async ( ) => {
97
+ const record = await createIPNSRecord ( privateKey1 , contentPath , 0 , 1000000 )
98
+ const result = validFor ( record )
99
+ expect ( result ) . to . be . greaterThan ( 0 )
100
+ } )
101
+
102
+ it ( 'should return 0 for expired records' , async ( ) => {
103
+ const record = await createIPNSRecord ( privateKey1 , contentPath , 0 , 0 )
104
+
105
+ expect ( validFor ( record ) ) . to . equal ( 0 )
106
+ } )
107
+
108
+ it ( 'should throw UnsupportedValidityError for non-EOL validity types' , async ( ) => {
109
+ const record = await createIPNSRecord ( privateKey1 , contentPath , 0 , 1000000 )
110
+ record . validityType = 5 as any
111
+
112
+ expect ( ( ) => validFor ( record ) ) . to . throw ( UnsupportedValidityError )
113
+ } )
114
+
115
+ it ( 'should throw UnsupportedValidityError for null validity' , async ( ) => {
116
+ const record = await createIPNSRecord ( privateKey1 , contentPath , 0 , 1000000 )
117
+ record . validityType = null as any
118
+
119
+ expect ( ( ) => validFor ( record ) ) . to . throw ( UnsupportedValidityError )
120
+ } )
121
+
122
+ it ( 'should return correct milliseconds until expiration' , async ( ) => {
123
+ const record = await createIPNSRecord ( privateKey1 , contentPath , 0 , 5000 )
124
+
125
+ const result = validFor ( record )
126
+
127
+ expect ( result ) . to . be . within ( 4900 , 5000 )
128
+ } )
129
+ } )
94
130
} )
0 commit comments