-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathselector.spec.ts
59 lines (43 loc) · 2.06 KB
/
selector.spec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
/* eslint-env mocha */
import { generateKeyPair } from '@libp2p/crypto/keys'
import { peerIdFromKeys } from '@libp2p/peer-id'
import { expect } from 'aegir/chai'
import * as ipns from '../src/index.js'
import { ipnsSelector } from '../src/selector.js'
import { marshal, peerIdToRoutingKey } from '../src/utils.js'
import type { PeerId } from '@libp2p/interface'
describe('selector', function () {
this.timeout(20 * 1000)
const contentPath = '/ipfs/bafkqae3imvwgy3zamzzg63janjzs22lqnzzqu'
let peerId: PeerId
before(async () => {
const rsa = await generateKeyPair('RSA', 2048)
peerId = await peerIdFromKeys(rsa.public.bytes, rsa.bytes)
})
it('should use validator.select to select the record with the highest sequence number', async () => {
const sequence = 0
const lifetime = 1000000
const record = await ipns.create(peerId, contentPath, sequence, lifetime)
const newRecord = await ipns.create(peerId, contentPath, (sequence + 1), lifetime)
const marshalledData = marshal(record)
const marshalledNewData = marshal(newRecord)
const key = peerIdToRoutingKey(peerId)
let valid = ipnsSelector(key, [marshalledNewData, marshalledData])
expect(valid).to.equal(0) // new data is the selected one
valid = ipnsSelector(key, [marshalledData, marshalledNewData])
expect(valid).to.equal(1) // new data is the selected one
})
it('should use validator.select to select the record with the longest validity', async () => {
const sequence = 0
const lifetime = 1000000
const record = await ipns.create(peerId, contentPath, sequence, lifetime)
const newRecord = await ipns.create(peerId, contentPath, sequence, (lifetime + 1))
const marshalledData = marshal(record)
const marshalledNewData = marshal(newRecord)
const key = peerIdToRoutingKey(peerId)
let valid = ipnsSelector(key, [marshalledNewData, marshalledData])
expect(valid).to.equal(0) // new data is the selected one
valid = ipnsSelector(key, [marshalledData, marshalledNewData])
expect(valid).to.equal(1) // new data is the selected one
})
})