Skip to content

Commit

Permalink
Rework e2e tests
Browse files Browse the repository at this point in the history
  • Loading branch information
haadcode committed Aug 1, 2024
1 parent 38e28c0 commit 270a1de
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 20 deletions.
6 changes: 4 additions & 2 deletions .github/workflows/run-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@ jobs:
run: npm ci
- name: Run linter
run: npm run lint
- name: Run an Orbiter instance in the background
run: npm run start:orbiter:background
- name: Run an Orbiter 1 instance in the background
run: npm run start:orbiter1:background
- name: Run an Orbiter 2 instance in the background
run: npm run start:orbiter2:background
- name: Run tests
run: npm run test:ci
- name: Run browser tests
Expand Down
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
node_modules
coverage
voyager
bundle.js*
bundle.js*
orbiter1/
orbiter2/
8 changes: 6 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,12 @@
"test:ci": "c8 npm run test",
"lint": "standard",
"lint:fix": "standard --fix",
"start:orbiter": "mkdir -p ./voyager/orbiter && cp -Rf test/fixtures/keystore ./voyager/orbiter && node ./src/bin/cli.js daemon -p 54321 --allow -vv",
"start:orbiter:background": "mkdir -p ./voyager/orbiter && cp -Rf test/fixtures/keystore ./voyager/orbiter && node ./src/bin/cli.js daemon -p 54321 --allow &",
"copy:fixtures1": "rm -rf ./orbiter1 && mkdir -p ./orbiter1/voyager/orbiter/keystore && cp -Rf test/fixtures/keystore1/** ./orbiter1/voyager/orbiter/keystore/",
"copy:fixtures2": "rm -rf ./orbiter2 && mkdir -p ./orbiter2/voyager/orbiter/keystore && cp -Rf test/fixtures/keystore2/** ./orbiter2/voyager/orbiter/keystore/",
"start:orbiter1": "npm run copy:fixtures1 && node ./src/bin/cli.js daemon -p 54321 --allow --directory ./orbiter1 -vv",
"start:orbiter2": "npm run copy:fixtures2 && node ./src/bin/cli.js daemon -p 54322 --allow --directory ./orbiter2 -vv",
"start:orbiter1:background": "npm run copy:fixtures1 && node ./src/bin/cli.js daemon -p 54321 --allow --directory ./orbiter1 &",
"start:orbiter2:background": "npm run copy:fixtures2 && node ./src/bin/cli.js daemon -p 54322 --allow --directory ./orbiter2 &",
"test:browser": "npm run build:tests && ./node_modules/.bin/playwright-test test/browser/bundle.js --runner mocha"
},
"standard": {
Expand Down
98 changes: 83 additions & 15 deletions test/e2e-browser.test.js
Original file line number Diff line number Diff line change
@@ -1,36 +1,57 @@
import { multiaddr } from '@multiformats/multiaddr'
import { peerIdFromString } from '@libp2p/peer-id'
import { strictEqual, deepStrictEqual } from 'assert'
import { rimraf } from 'rimraf'
import { launchLander } from './utils/launch-lander.js'
import waitFor from './utils/wait-for.js'

describe('End-to-End Browser Test', function () {
this.timeout(100000)
this.timeout(10000)

const peerId1 = peerIdFromString('16Uiu2HAmBzKcgCfpJ4j4wJSLkKLbCVvnNBWPnhexrnJWJf1fDu5y')
const peerId2 = peerIdFromString('16Uiu2HAmATMovCwY46yyJib7bGZF2f2XLRar7d7R3NJCSJtuyQLt')
const peerAddress1 = multiaddr('/ip4/127.0.0.1/tcp/54321/ws/p2p/16Uiu2HAmBzKcgCfpJ4j4wJSLkKLbCVvnNBWPnhexrnJWJf1fDu5y')
const peerAddress2 = multiaddr('/ip4/127.0.0.1/tcp/54322/ws/p2p/16Uiu2HAmATMovCwY46yyJib7bGZF2f2XLRar7d7R3NJCSJtuyQLt')

let orbiter1
let orbiter2

let orbiter
let lander1
let lander2
let lander3

beforeEach(async function () {
const peerId = peerIdFromString('16Uiu2HAmBzKcgCfpJ4j4wJSLkKLbCVvnNBWPnhexrnJWJf1fDu5y')
const peerAddress = multiaddr('/ip4/127.0.0.1/tcp/54321/ws/p2p/16Uiu2HAmBzKcgCfpJ4j4wJSLkKLbCVvnNBWPnhexrnJWJf1fDu5y')
orbiter1 = {
orbitdb: {
ipfs: {
libp2p: {
peerId: peerId1
}
}
}
}

orbiter = {
orbiter2 = {
orbitdb: {
ipfs: {
libp2p: {
peerId
peerId: peerId2
}
}
}
}

orbiter.orbitdb.ipfs.libp2p.getMultiaddrs = () => {
return [peerAddress]
orbiter1.orbitdb.ipfs.libp2p.getMultiaddrs = () => {
return [peerAddress1]
}

orbiter2.orbitdb.ipfs.libp2p.getMultiaddrs = () => {
return [peerAddress2]
}

lander1 = await launchLander({ orbiter, directory: 'lander1' })
lander2 = await launchLander({ orbiter, directory: 'lander2' })
lander1 = await launchLander({ orbiter: orbiter1, directory: 'lander1' })
lander2 = await launchLander({ orbiter: orbiter1, directory: 'lander2' })
lander3 = await launchLander({ orbiter: orbiter2, directory: 'lander3' })
})

afterEach(async function () {
Expand All @@ -40,28 +61,35 @@ describe('End-to-End Browser Test', function () {
if (lander2) {
await lander2.shutdown()
}
if (lander3) {
await lander3.shutdown()
}
await rimraf('./lander1')
await rimraf('./lander2')
await rimraf('./lander3')
})

it('pin a db', async function () {
const entryAmount = 1000
it('pin and replicate a database - lander1->orbiter1->lander2', async function () {
const entryAmount = 100
let replicated = false

const db1 = await lander1.orbitdb.open('my-db')

console.log('write')
for (let i = 0; i < entryAmount; i++) {
await db1.add('hello world ' + i)
}

const expected = await db1.all()

console.log('pin')
console.time('pin')
await lander1.pin([db1])
console.timeEnd('pin')
await lander1.shutdown()

console.log('replicate')
console.time('pin')
await lander2.pin([db1])
console.timeEnd('pin')

console.time('replicate')
const db2 = await lander2.orbitdb.open(db1.address)

Expand All @@ -80,4 +108,44 @@ describe('End-to-End Browser Test', function () {
strictEqual(res.length, entryAmount)
deepStrictEqual(expected, res)
})

it('pin and replicate a database - lander1->orbiter1->orbiter2->lander3', async function () {
const entryAmount = 100
let replicated = false

const db1 = await lander1.orbitdb.open('my-db2')

for (let i = 0; i < entryAmount; i++) {
await db1.add('hello world ' + i)
}

const expected = await db1.all()

console.time('pin')
await lander1.pin([db1])
console.timeEnd('pin')
await lander1.shutdown()

console.time('pin')
await lander3.pin([db1])
console.timeEnd('pin')

console.time('replicate')
const db2 = await lander3.orbitdb.open(db1.address)

const onConnected = (peerId, heads) => {
replicated = true
}

db2.events.on('join', onConnected)

await waitFor(() => replicated, () => true)
console.timeEnd('replicate')

const res = await db2.all()

strictEqual(expected.length, entryAmount)
strictEqual(res.length, entryAmount)
deepStrictEqual(expected, res)
})
})

0 comments on commit 270a1de

Please sign in to comment.