This repository has been archived by the owner on Feb 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy pathpublish.js
88 lines (67 loc) · 2.25 KB
/
publish.js
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
/* eslint-env mocha */
import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
import { nanoid } from 'nanoid'
import { getTopic } from './utils.js'
import { expect } from 'aegir/chai'
import { getDescribe, getIt } from '../utils/mocha.js'
import pWaitFor from 'p-wait-for'
/**
* @typedef {import('ipfsd-ctl').Factory} Factory
* @typedef {import('ipfs-core-types').IPFS} IPFS
*/
/**
* @param {string} topic
* @param {IPFS} ipfs
* @param {IPFS} remote
*/
async function waitForRemoteToBeSubscribed (topic, ipfs, remote) {
await remote.pubsub.subscribe(topic, () => {})
const remoteId = await remote.id()
// wait for remote to be subscribed to topic
await pWaitFor(async () => {
const peers = await ipfs.pubsub.peers(topic)
return peers.map(p => p.toString()).includes(remoteId.id.toString())
})
}
/**
* @param {Factory} factory
* @param {object} options
*/
export function testPublish (factory, options) {
const describe = getDescribe(options)
const it = getIt(options)
describe('.pubsub.publish', function () {
this.timeout(80 * 1000)
/** @type {IPFS} */
let ipfs
/** @type {IPFS} */
let remote
before(async () => {
ipfs = (await factory.spawn()).api
remote = (await factory.spawn()).api
// ensure we have peers to allow publishing
const remoteId = await remote.id()
await ipfs.swarm.connect(remoteId.addresses[0])
})
after(() => factory.clean())
it('should fail with undefined msg', async () => {
const topic = getTopic()
await waitForRemoteToBeSubscribed(topic, ipfs, remote)
// @ts-expect-error invalid parameter
await expect(ipfs.pubsub.publish(topic)).to.eventually.be.rejected()
})
it('should publish message from buffer', async () => {
const topic = getTopic()
await waitForRemoteToBeSubscribed(topic, ipfs, remote)
return ipfs.pubsub.publish(topic, uint8ArrayFromString(nanoid()))
})
it('should publish 10 times within time limit', async () => {
const count = 10
const topic = getTopic()
await waitForRemoteToBeSubscribed(topic, ipfs, remote)
for (let i = 0; i < count; i++) {
await ipfs.pubsub.publish(topic, uint8ArrayFromString(nanoid()))
}
})
})
}