This repository was archived by the owner on Oct 30, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.spec.ts
99 lines (83 loc) · 2.82 KB
/
index.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
/* eslint-env mocha */
import { createEd25519PeerId } from '@libp2p/peer-id-factory'
import { expect } from 'aegir/chai'
import all from 'it-all'
import { CID } from 'multiformats/cid'
import { type ReframeV1ResponseItem, reframeContentRouting } from '../src/index.js'
if (process.env.ECHO_SERVER == null) {
throw new Error('Echo server not configured correctly')
}
const serverUrl = process.env.ECHO_SERVER
const cid = CID.parse('QmUNLLsPACCz1vLxQVkXqqLX5R1X345qqfHbsf67hvA3Nn')
describe('ReframeContentRouting', function () {
it('should find providers', async () => {
const providers: ReframeV1ResponseItem[] = [{
Protocol: 'transport-bitswap',
Schema: 'bitswap',
ID: (await createEd25519PeerId()).toString(),
Addrs: ['/ip4/41.41.41.41/tcp/1234']
}, {
Protocol: 'transport-bitswap',
Schema: 'bitswap',
ID: (await createEd25519PeerId()).toString(),
Addrs: ['/ip4/42.42.42.42/tcp/1234']
}]
// load providers for the router to fetch
await fetch(`${process.env.ECHO_SERVER}/add-providers/${cid.toString()}`, {
method: 'POST',
body: JSON.stringify({
Providers: providers
})
})
const routing = reframeContentRouting(serverUrl)()
const provs = await all(routing.findProviders(cid))
expect(provs.map(prov => ({
id: prov.id.toString(),
addrs: prov.multiaddrs.map(ma => ma.toString())
}))).to.deep.equal(providers.map(prov => ({
id: prov.ID,
addrs: prov.Addrs
})))
})
it('should handle non-json input', async () => {
// load providers for the router to fetch
await fetch(`${process.env.ECHO_SERVER}/add-providers/${cid.toString()}`, {
method: 'POST',
body: 'not json'
})
const routing = reframeContentRouting(serverUrl)()
const provs = await all(routing.findProviders(cid))
expect(provs).to.be.empty()
})
it('should handle bad input providers', async () => {
const providers = [{
Protocol: 'transport-bitswap',
Schema: 'bitswap',
Bad: 'field'
}, {
Protocol: 'transport-bitswap',
Schema: 'bitswap',
ID: 'not a peer id'
}, {
Protocol: 'transport-bitswap',
Schema: 'bitswap',
ID: (await createEd25519PeerId()).toString(),
Addrs: ['not a multiaddr']
}]
// load providers for the router to fetch
await fetch(`${process.env.ECHO_SERVER}/add-providers/${cid.toString()}`, {
method: 'POST',
body: JSON.stringify({
Providers: providers
})
})
const routing = reframeContentRouting(serverUrl)()
const provs = await all(routing.findProviders(cid))
expect(provs).to.be.empty()
})
it('should handle empty input', async () => {
const routing = reframeContentRouting(serverUrl)()
const provs = await all(routing.findProviders(cid))
expect(provs).to.be.empty()
})
})