diff --git a/.gitignore b/.gitignore index 274511cf..8cc5f2a6 100644 --- a/.gitignore +++ b/.gitignore @@ -19,7 +19,7 @@ pids lib-cov # Coverage directory used by tools like istanbul -coverage +.coverage # Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) .grunt diff --git a/package.json b/package.json index 9e84d49d..cd72c78f 100644 --- a/package.json +++ b/package.json @@ -131,8 +131,12 @@ "lint": "aegir lint", "build": "aegir build", "generate": "protons ./src/message/dht.proto", - "test": "aegir test -t node", - "test:node": "aegir test -t node", + "test": "aegir test", + "test:node": "aegir test -t node -f './dist/test/**/*.{node,spec}.js' --cov", + "test:chrome": "aegir test -t browser -f './dist/test/**/*.spec.js' --cov", + "test:chrome-webworker": "aegir test -t webworker -f './dist/test/**/*.spec.js'", + "test:firefox": "aegir test -t browser -f './dist/test/**/*.spec.js' -- --browser firefox", + "test:firefox-webworker": "aegir test -t webworker -f './dist/test/**/*.spec.js' -- --browser firefox", "dep-check": "aegir dep-check", "release": "aegir release" }, @@ -159,6 +163,7 @@ "any-signal": "^3.0.0", "datastore-core": "^8.0.1", "err-code": "^3.0.1", + "events": "^3.3.0", "hashlru": "^2.3.0", "interface-datastore": "^7.0.0", "it-all": "^1.0.6", @@ -191,7 +196,7 @@ "@types/lodash.range": "^3.2.6", "@types/varint": "^6.0.0", "@types/which": "^2.0.1", - "aegir": "^37.0.8", + "aegir": "^37.3.0", "datastore-level": "^9.0.0", "delay": "^5.0.0", "execa": "^6.0.0", diff --git a/test/generate-peers/generate-peers.spec.ts b/test/generate-peers/generate-peers.node.ts similarity index 100% rename from test/generate-peers/generate-peers.spec.ts rename to test/generate-peers/generate-peers.node.ts diff --git a/test/message.node.ts b/test/message.node.ts new file mode 100644 index 00000000..24ee27d0 --- /dev/null +++ b/test/message.node.ts @@ -0,0 +1,31 @@ +/* eslint-env mocha */ + +import { expect } from 'aegir/chai' +import range from 'lodash.range' +import fs from 'fs' +import path from 'path' +import { Message } from '../src/message/index.js' +import { isPeerId } from '@libp2p/interface-peer-id' + +describe('Message', () => { + it('go-interop', () => { + range(1, 9).forEach((i) => { + const raw = fs.readFileSync( + path.join(process.cwd(), 'test', 'fixtures', `msg-${i}`) + ) + + const msg = Message.deserialize(raw) + + expect(msg.clusterLevel).to.gte(0) + if (msg.record != null) { + expect(msg.record.key).to.be.a('Uint8Array') + } + + if (msg.providerPeers.length > 0) { + msg.providerPeers.forEach((p) => { + expect(isPeerId(p.id)).to.be.true() + }) + } + }) + }) +}) diff --git a/test/message.spec.ts b/test/message.spec.ts index d9b9f770..71f2f3dd 100644 --- a/test/message.spec.ts +++ b/test/message.spec.ts @@ -2,15 +2,11 @@ import { expect } from 'aegir/chai' import { multiaddr } from '@multiformats/multiaddr' -import range from 'lodash.range' -import random from 'lodash.random' +import random from 'lodash/random.js' import { Libp2pRecord } from '@libp2p/record' -import fs from 'fs' -import path from 'path' import { Message, MESSAGE_TYPE } from '../src/message/index.js' import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string' import { createEd25519PeerId } from '@libp2p/peer-id-factory' -import { isPeerId } from '@libp2p/interface-peer-id' describe('Message', () => { it('create', () => { @@ -88,25 +84,4 @@ describe('Message', () => { msg.clusterLevel = 10 expect(msg.clusterLevel).to.eql(9) }) - - it('go-interop', () => { - range(1, 9).forEach((i) => { - const raw = fs.readFileSync( - path.join(process.cwd(), 'test', 'fixtures', `msg-${i}`) - ) - - const msg = Message.deserialize(raw) - - expect(msg.clusterLevel).to.gte(0) - if (msg.record != null) { - expect(msg.record.key).to.be.a('Uint8Array') - } - - if (msg.providerPeers.length > 0) { - msg.providerPeers.forEach((p) => { - expect(isPeerId(p.id)).to.be.true() - }) - } - }) - }) }) diff --git a/test/providers.node.ts b/test/providers.node.ts new file mode 100644 index 00000000..6cbcf86e --- /dev/null +++ b/test/providers.node.ts @@ -0,0 +1,63 @@ +/* eslint-env mocha */ + +import { LevelDatastore } from 'datastore-level' +import path from 'path' +import os from 'os' +import { Providers } from '../src/providers.js' +import { createValues } from './utils/create-values.js' +import { createPeerIds } from './utils/create-peer-id.js' +import { MemoryDatastore } from 'datastore-core/memory' + +describe('Providers', () => { + let providers: Providers + + before(async function () { + this.timeout(10 * 1000) + }) + + afterEach(async () => { + await providers?.stop() + }) + + // slooow so only run when you need to + it.skip('many', async function () { + const p = path.join( + os.tmpdir(), (Math.random() * 100).toString() + ) + const store = new LevelDatastore(p) + await store.open() + providers = new Providers({ + datastore: new MemoryDatastore() + }, { + cacheSize: 10 + }) + + console.log('starting') // eslint-disable-line no-console + const [createdValues, createdPeers] = await Promise.all([ + createValues(100), + createPeerIds(600) + ]) + + console.log('got values and peers') // eslint-disable-line no-console + const total = Date.now() + + for (const v of createdValues) { + for (const p of createdPeers) { + await providers.addProvider(v.cid, p) + } + } + + console.log('addProvider %s peers %s cids in %sms', createdPeers.length, createdValues.length, Date.now() - total) // eslint-disable-line no-console + console.log('starting profile with %s peers and %s cids', createdPeers.length, createdValues.length) // eslint-disable-line no-console + + for (let i = 0; i < 3; i++) { + const start = Date.now() + for (const v of createdValues) { + await providers.getProviders(v.cid) + console.log('query %sms', (Date.now() - start)) // eslint-disable-line no-console + } + } + + await store.close() + }) +}) diff --git a/test/providers.spec.ts b/test/providers.spec.ts index 005f1f4d..cc3eab64 100644 --- a/test/providers.spec.ts +++ b/test/providers.spec.ts @@ -4,12 +4,8 @@ import { expect } from 'aegir/chai' import { MemoryDatastore } from 'datastore-core/memory' import { CID } from 'multiformats/cid' import { sha256 } from 'multiformats/hashes/sha2' -import { LevelDatastore } from 'datastore-level' -import path from 'path' -import os from 'os' import { Providers } from '../src/providers.js' import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string' -import { createValues } from './utils/create-values.js' import { createPeerIds } from './utils/create-peer-id.js' import type { PeerId } from '@libp2p/interface-peer-id' import delay from 'delay' @@ -114,46 +110,4 @@ describe('Providers', () => { expect(provsAfter).to.have.length(0) await providers.stop() }) - - // slooow so only run when you need to - it.skip('many', async function () { - const p = path.join( - os.tmpdir(), (Math.random() * 100).toString() - ) - const store = new LevelDatastore(p) - await store.open() - providers = new Providers({ - datastore: new MemoryDatastore() - }, { - cacheSize: 10 - }) - - console.log('starting') // eslint-disable-line no-console - const [createdValues, createdPeers] = await Promise.all([ - createValues(100), - createPeerIds(600) - ]) - - console.log('got values and peers') // eslint-disable-line no-console - const total = Date.now() - - for (const v of createdValues) { - for (const p of createdPeers) { - await providers.addProvider(v.cid, p) - } - } - - console.log('addProvider %s peers %s cids in %sms', createdPeers.length, createdValues.length, Date.now() - total) // eslint-disable-line no-console - console.log('starting profile with %s peers and %s cids', createdPeers.length, createdValues.length) // eslint-disable-line no-console - - for (let i = 0; i < 3; i++) { - const start = Date.now() - for (const v of createdValues) { - await providers.getProviders(v.cid) - console.log('query %sms', (Date.now() - start)) // eslint-disable-line no-console - } - } - - await store.close() - }) }) diff --git a/test/rpc/index.spec.ts b/test/rpc/index.node.ts similarity index 98% rename from test/rpc/index.spec.ts rename to test/rpc/index.node.ts index 76e794d9..02fbf7ef 100644 --- a/test/rpc/index.spec.ts +++ b/test/rpc/index.node.ts @@ -97,11 +97,11 @@ describe('rpc', () => { } } - await rpc.onIncomingStream({ + rpc.onIncomingStream({ stream: mockStream(duplexStream), connection: stubInterface() }) - return await defer.promise + await defer.promise }) }) diff --git a/tsconfig.json b/tsconfig.json index 13a35996..5d1f9eb1 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -1,6 +1,7 @@ { "extends": "aegir/src/config/tsconfig.aegir.json", "compilerOptions": { + "esModuleInterop": true, "outDir": "dist" }, "include": [