@leofcoin/peernet 1.1.78 → 1.1.80

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,159 @@
1
+ import Peernet from '../exports/peernet.js'
2
+ import Identity from '../exports/identity.js'
3
+ import { expect } from 'chai'
4
+ import sinon from 'sinon'
5
+ import networks from '@leofcoin/networks'
6
+
7
+ const network = networks.leofcoin.peach
8
+
9
+ let options
10
+ let password
11
+ options = {
12
+ network: 'leofcoin:peach',
13
+ stars: network.stars,
14
+ root: '.testnet',
15
+ version: '1.0.0',
16
+ storePrefix: 'test'
17
+ }
18
+ password = 'password'
19
+ const peernet = await new Peernet(options, password)
20
+
21
+ describe('Peernet', async () => {
22
+ // beforeEach(async () => {
23
+ // })
24
+
25
+ it('should initialize with the correct network', () => {
26
+ expect(peernet.network).to.equal('leofcoin:peach')
27
+ })
28
+
29
+ it('should initialize with the correct version', () => {
30
+ expect(peernet.version).to.equal('1.0.0')
31
+ })
32
+
33
+ it('should initialize with the correct store prefix', () => {
34
+ expect(peernet.storePrefix).to.equal('test')
35
+ })
36
+
37
+ it('should have an identity instance', () => {
38
+ expect(peernet.identity).to.be.instanceOf(Identity)
39
+ })
40
+
41
+ it('should have a DHT instance', () => {
42
+ expect(peernet.dht).to.not.be.undefined
43
+ })
44
+
45
+ it('should start the client', async () => {
46
+ const startSpy = sinon.spy(peernet, 'start')
47
+ await peernet.start()
48
+ expect(startSpy.calledOnce).to.be.true
49
+ })
50
+
51
+ it('should add a proto', () => {
52
+ const protoName = 'test-proto'
53
+ const proto = {}
54
+ peernet.addProto(protoName, proto)
55
+ expect(globalThis.peernet.protos[protoName]).to.equal(proto)
56
+ })
57
+
58
+ it('should add a codec', () => {
59
+ const codec = { name: 'test-codec' }
60
+ const addCodecSpy = sinon.spy(peernet, 'addCodec')
61
+ peernet.addCodec(codec)
62
+ expect(addCodecSpy.calledOnceWith(codec)).to.be.true
63
+ })
64
+
65
+ it('should select an account', async () => {
66
+ const account = 'test-account'
67
+ const selectAccountSpy = sinon.spy(peernet.identity, 'selectAccount')
68
+ await peernet.selectAccount(account)
69
+ expect(selectAccountSpy.calledOnceWith(account)).to.be.true
70
+ })
71
+
72
+ it('should prepare a message', () => {
73
+ const data = { message: 'test' }
74
+ const prepareMessageSpy = sinon.spy(peernet._messageHandler, 'prepareMessage')
75
+ peernet.prepareMessage(data)
76
+ expect(prepareMessageSpy.calledOnceWith(data)).to.be.true
77
+ })
78
+
79
+ it('should get peers', () => {
80
+ const peers = peernet.peers
81
+ expect(peers).to.be.an('array')
82
+ })
83
+
84
+ it('should get connections', () => {
85
+ const connections = peernet.connections
86
+ expect(connections).to.be.an('object')
87
+ })
88
+
89
+ it('should get a connection by id', () => {
90
+ const id = 'test-id'
91
+ const connection = peernet.getConnection(id)
92
+ expect(connection).to.be.undefined // Assuming no connections are established in the test
93
+ })
94
+
95
+ // it('should handle DHT', async () => {
96
+ // const peer = {}
97
+ // const id = 'test-id'
98
+ // const proto = { decoded: { hash: 'test-hash', store: 'test-store' } }
99
+ // const handleDHTSpy = sinon.spy(peernet, 'handleDHT')
100
+ // await peernet.handleDHT(peer, id, proto)
101
+ // expect(handleDHTSpy.calledOnceWith(peer, id, proto)).to.be.true
102
+ // })
103
+
104
+ it('should handle data', async () => {
105
+ const peer = {}
106
+ const id = 'test-id'
107
+ const proto = { decoded: { hash: 'test-hash', store: 'test-store' } }
108
+ const handleDataSpy = sinon.spy(peernet, 'handleData')
109
+ await peernet.handleData(peer, id, proto)
110
+ expect(handleDataSpy.calledOnceWith(peer, id, proto)).to.be.true
111
+ })
112
+
113
+ it('should handle request', async () => {
114
+ const peer = {}
115
+ const id = 'test-id'
116
+ const proto = { decoded: { request: 'test-request' } }
117
+ const handleRequestSpy = sinon.spy(peernet, 'handleRequest')
118
+ await peernet.handleRequest(peer, id, proto)
119
+ expect(handleRequestSpy.calledOnceWith(peer, id, proto)).to.be.true
120
+ })
121
+
122
+ it('should walk the network', async () => {
123
+ const hash = 'test-hash'
124
+ const walkSpy = sinon.spy(peernet, 'walk')
125
+ await peernet.walk(hash)
126
+ expect(walkSpy.calledOnceWith(hash)).to.be.true
127
+ })
128
+
129
+ // it('should find providers for a hash', async () => {
130
+ // const hash = 'test-hash'
131
+ // const providers = await peernet.providersFor(hash)
132
+ // expect(providers).to.be.undefined // Assuming no providers are found in the test
133
+ // })
134
+
135
+ // it('should request data', async () => {
136
+ // const hash = 'test-hash'
137
+ // const requestDataSpy = sinon.spy(peernet, 'requestData')
138
+ // await peernet.requestData(hash, 'data')
139
+ // expect(requestDataSpy.calledOnceWith(hash, 'data')).to.be.true
140
+ // })
141
+
142
+ it('should publish data', async () => {
143
+ const topic = 'test-topic'
144
+ const data = 'test-data'
145
+ const publishSpy = sinon.spy(peernet, 'publish')
146
+ await peernet.publish(topic, data)
147
+ expect(publishSpy.calledOnceWith(topic, data)).to.be.true
148
+ })
149
+
150
+ it('should subscribe to a topic', async () => {
151
+ const topic = 'test-topic'
152
+ const callback = sinon.spy()
153
+ const subscribeSpy = sinon.spy(peernet, 'subscribe')
154
+ await peernet.subscribe(topic, callback)
155
+ expect(subscribeSpy.calledOnceWith(topic, callback)).to.be.true
156
+
157
+ process.exit()
158
+ })
159
+ })
package/tsconfig.json CHANGED
@@ -7,7 +7,5 @@
7
7
  "moduleResolution": "NodeNext",
8
8
  "allowJs": true
9
9
  },
10
- "include": [
11
- "./src/**/*"
12
- ]
13
- }
10
+ "include": ["./src/**/*"]
11
+ }