|
1 |
| -# OrbitDB Test Utils |
| 1 | +# OrbitDB Test Utils _(orbit-db-test-utils)_ |
| 2 | +> Shared test utilities for OrbitDB-related projects |
2 | 3 |
|
3 | 4 | [](https://gitter.im/orbitdb/Lobby) [](https://riot.permaweb.io/#/room/#orbitdb:permaweb.io) [](https://discord.gg/cscuf5T)
|
4 | 5 |
|
5 |
| -- Readme TBD |
| 6 | +This repository contains utilities to spin up IPFS nodes and swarms, as well |
| 7 | +as different `abstract-leveldown` implementations to use with `orbit-db-keystore` |
| 8 | +and `orbit-db-cache`. |
| 9 | + |
| 10 | +For examples on how this would be used, see the `test` folder in this repo, or |
| 11 | +read on! |
| 12 | + |
| 13 | +## Install |
| 14 | + |
| 15 | +```bash |
| 16 | +$ npm install orbit-db-test-utils -D |
| 17 | +``` |
| 18 | + |
| 19 | +## Usage |
| 20 | + |
| 21 | +### Spawn a single IPFS instance |
| 22 | + |
| 23 | +```JavaScript |
| 24 | +const { |
| 25 | + connectPeers, |
| 26 | + startIpfs, |
| 27 | + stopIpfs, |
| 28 | + testApis, |
| 29 | + getIpfsPeerId, |
| 30 | + waitForPeers |
| 31 | +} = require('../') |
| 32 | + |
| 33 | +;(async () => { |
| 34 | + // Create JS and Go nodes |
| 35 | + const ipfsd1 = await startIpfs('js-ipfs') |
| 36 | + const ipfsd2 = await startIpfs('go-ipfs') |
| 37 | + |
| 38 | + // Get the peer IDs |
| 39 | + const id1 = await getIpfsPeerId(ipfsd1.api) |
| 40 | + const id2 = await getIpfsPeerId(ipfsd2.api) |
| 41 | + |
| 42 | + // Helper function to connect the nodes |
| 43 | + await connectPeers(ipfsd1.api, ipfsd2.api) |
| 44 | + |
| 45 | + // Test that the nodes are pubsubbing with each other |
| 46 | + const topic = 'test-topic' |
| 47 | + await ipfsd1.api.pubsub.subscribe(topic, () => {}) |
| 48 | + await ipfsd2.api.pubsub.subscribe(topic, () => {}) |
| 49 | + await waitForPeers(ipfsd1.api, [id2], topic) |
| 50 | + |
| 51 | + // stop the nodes |
| 52 | + await stopIpfs(ipfsd1) |
| 53 | + await stopIpfs(ipfsd2) |
| 54 | +})() |
| 55 | +``` |
| 56 | + |
| 57 | +**Note:** You may run into issues with `stopIpfs` hanging the script due to a `js-ipfs` bug |
| 58 | + |
| 59 | +### Spawn a swarm of connected IPFS instances |
| 60 | + |
| 61 | +```JavaScript |
| 62 | +const { swarm } = require('orbit-db-test-utils') |
| 63 | + |
| 64 | +// Enter an array of the node types you want, either 'js' or 'go' |
| 65 | +const nodeTypes = ['js', 'go', 'js'] |
| 66 | + |
| 67 | +swarm(nodeTypes).then(nodes => /* ... do stuff ... */) |
| 68 | +``` |
| 69 | + |
| 70 | +## Contributing |
| 71 | + |
| 72 | +Issues and PRs are welcome. |
| 73 | + |
| 74 | +## License |
| 75 | + |
| 76 | +[MIT](./LICENSE) © OrbitDB Community |
0 commit comments