From 270a1de52053a37913aff6a43aa1e3e2cac05f8e Mon Sep 17 00:00:00 2001 From: haad Date: Thu, 1 Aug 2024 08:41:15 +0300 Subject: [PATCH] Rework e2e tests --- .github/workflows/run-test.yml | 6 ++- .gitignore | 4 +- package.json | 8 ++- test/e2e-browser.test.js | 98 ++++++++++++++++++++++++++++------ 4 files changed, 96 insertions(+), 20 deletions(-) diff --git a/.github/workflows/run-test.yml b/.github/workflows/run-test.yml index 653fb58..da41894 100644 --- a/.github/workflows/run-test.yml +++ b/.github/workflows/run-test.yml @@ -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 diff --git a/.gitignore b/.gitignore index 2ea1957..0895d74 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,6 @@ node_modules coverage voyager -bundle.js* \ No newline at end of file +bundle.js* +orbiter1/ +orbiter2/ diff --git a/package.json b/package.json index 670c8de..ff32d88 100644 --- a/package.json +++ b/package.json @@ -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": { diff --git a/test/e2e-browser.test.js b/test/e2e-browser.test.js index 1f84834..c04a864 100644 --- a/test/e2e-browser.test.js +++ b/test/e2e-browser.test.js @@ -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 () { @@ -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) @@ -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) + }) })