From 8ff4e69df5e5861b31a7c116ca73c86d4b46c3bd Mon Sep 17 00:00:00 2001 From: saul Date: Thu, 28 Mar 2024 12:25:39 +1300 Subject: [PATCH] Nuke the daemon tests. --- packages/daemon/test/modules/argv.spec.ts | 33 - packages/daemon/test/modules/base.spec.ts | 147 ----- packages/daemon/test/modules/config.spec.ts | 54 -- .../daemon/test/modules/downloader.spec.ts | 236 ------- .../daemon/test/modules/filesystem.spec.ts | 618 ------------------ packages/daemon/test/modules/groups.spec.ts | 311 --------- packages/daemon/test/modules/mock-argv.ts | 12 - packages/daemon/test/modules/mock-base.ts | 20 - packages/daemon/test/modules/mock-config.ts | 12 - packages/daemon/test/modules/network.spec.ts | 313 --------- .../daemon/test/modules/revisions.spec.ts | 460 ------------- packages/daemon/test/modules/rpc.spec.ts | 61 -- packages/daemon/test/modules/sigint.spec.ts | 15 - packages/daemon/test/modules/tick.spec.ts | 69 -- packages/daemon/test/utils.spec.ts | 114 ---- packages/daemon/test/utils/blocks.ts | 46 -- packages/daemon/test/utils/create-group.ts | 17 - packages/daemon/test/utils/dag.ts | 44 -- packages/daemon/test/utils/generate-key.ts | 14 - packages/daemon/test/utils/paths.ts | 6 - packages/daemon/tsconfig.json | 3 +- 21 files changed, 2 insertions(+), 2603 deletions(-) delete mode 100644 packages/daemon/test/modules/argv.spec.ts delete mode 100644 packages/daemon/test/modules/base.spec.ts delete mode 100644 packages/daemon/test/modules/config.spec.ts delete mode 100644 packages/daemon/test/modules/downloader.spec.ts delete mode 100644 packages/daemon/test/modules/filesystem.spec.ts delete mode 100644 packages/daemon/test/modules/groups.spec.ts delete mode 100644 packages/daemon/test/modules/mock-argv.ts delete mode 100644 packages/daemon/test/modules/mock-base.ts delete mode 100644 packages/daemon/test/modules/mock-config.ts delete mode 100644 packages/daemon/test/modules/network.spec.ts delete mode 100644 packages/daemon/test/modules/revisions.spec.ts delete mode 100644 packages/daemon/test/modules/rpc.spec.ts delete mode 100644 packages/daemon/test/modules/sigint.spec.ts delete mode 100644 packages/daemon/test/modules/tick.spec.ts delete mode 100644 packages/daemon/test/utils.spec.ts delete mode 100644 packages/daemon/test/utils/blocks.ts delete mode 100644 packages/daemon/test/utils/create-group.ts delete mode 100644 packages/daemon/test/utils/dag.ts delete mode 100644 packages/daemon/test/utils/generate-key.ts delete mode 100644 packages/daemon/test/utils/paths.ts diff --git a/packages/daemon/test/modules/argv.spec.ts b/packages/daemon/test/modules/argv.spec.ts deleted file mode 100644 index 483f2fa8..00000000 --- a/packages/daemon/test/modules/argv.spec.ts +++ /dev/null @@ -1,33 +0,0 @@ -import assert from 'assert/strict' -import Path from 'path' -import argv from '../../src/modules/argv/index.js' -import { projectPath } from '@/utils.js' - -describe('argv', () => { - it('returns defaults for every argv parameter', async () => { - const m = await argv() - - assert.equal(m.key, Path.join(projectPath, 'config/key.json')) - assert.equal(m.config, Path.join(projectPath, 'config/config.json')) - assert.equal(m.socket, '/tmp/server.socket') - }) - - it('returns the value for every argv parameter', async () => { - const key = '/key.json' - const config = '/config.json' - const socket = '/socket' - - process.argv.push('--key') - process.argv.push(key) - process.argv.push('--config') - process.argv.push(config) - process.argv.push('--socket') - process.argv.push(socket) - - const m = await argv() - - assert.equal(m.key, key) - assert.equal(m.config, config) - assert.equal(m.socket, socket) - }) -}) diff --git a/packages/daemon/test/modules/base.spec.ts b/packages/daemon/test/modules/base.spec.ts deleted file mode 100644 index 81f9c180..00000000 --- a/packages/daemon/test/modules/base.spec.ts +++ /dev/null @@ -1,147 +0,0 @@ -import assert from 'assert/strict' -import fs from 'fs/promises' -import Path from 'path' -import { MemoryBlockstore } from 'blockstore-core' -import { FsBlockstore } from 'blockstore-fs' -import { MemoryDatastore } from 'datastore-core' -import { FsDatastore } from 'datastore-fs' -import { Key } from 'interface-datastore' -import { CID } from 'multiformats/cid' -import { fromString as uint8ArrayFromString } from 'uint8arrays' -import base from '../../src/modules/base/index.js' -import { mkTestPath } from '../utils/paths.js' -import mockArgv from './mock-argv.js' -import mockConfig from './mock-config.js' -import type { Provides as Argv } from '../../src/modules/argv/index.js' -import { extendDatastore } from '@/utils.js' - -const parseStr = (data: string): Uint8Array => uint8ArrayFromString(data, 'base64') -const testPath = mkTestPath('base') - -describe('base', () => { - let argv: Argv - - before(async () => { - argv = mockArgv(testPath) - - await fs.mkdir(Path.join(argv.key, '..'), { recursive: true }) - await fs.mkdir(testPath, { recursive: true }) - - await fs.writeFile(argv.key, JSON.stringify({ - key: '5TP9VimJU1WdSoTxZGLhSuPKqCpXirPHDK4ZjHxzetex-8zAV14C4oLe4dytUSVzznTuQ659pY1dSMG8HAQenDqVQ', - psk: '/key/swarm/psk/1.0.0/\n/base16/\n56d3c18282f1f1b1b3e04e40dd5d8bf44cafa8bc9c9bc7c57716a7766fa2c550' - })) - }) - - after(async () => { - await fs.rm(testPath, { recursive: true }) - }) - - it('returns the key manager', async () => { - const m = await base({ - config: mockConfig({ storage: ':memory:' }), - argv - }) - - assert.deepEqual( - new Uint8Array(m.keyManager.aesKey), - parseStr('knUGn6uUeQcoxfM1qAtg3F/Njm4bp+GcZK257NZ5AtE') - ) - - assert.deepEqual( - new Uint8Array(m.keyManager.hmacKey), - parseStr('KZkJfNz3bRrn6XHvWYtD4+dXvmhdT4TBhBIkWn8y3jY') - ) - - assert.deepEqual( - (await m.keyManager.getWeloIdentity()).id, - parseStr('CAISIQPSvjmKINqJY5SA/3c+kadFmIsHeTXtTJYlrooZ53DTUg') - ) - - assert.deepEqual( - (await m.keyManager.getPeerId()).toBytes(), - parseStr('ACUIAhIhA5m2/DfXxqi0i+fyYixRaWGirDEVemxUEv8WMZPwFPZB') - ) - - assert.deepEqual( - m.keyManager.getPskKey(), - parseStr('L2tleS9zd2FybS9wc2svMS4wLjAvCi9iYXNlMTYvCjU2ZDNjMTgyODJmMWYxYjFiM2UwNGU0MGRkNWQ4YmY0NGNhZmE4YmM5YzliYzdjNTc3MTZhNzc2NmZhMmM1NTA') - ) - }) - - it('uses memory blockstore when memory is specified', async () => { - const m = await base({ - config: mockConfig({ storage: ':memory:' }), - argv - }) - - assert(m.blockstore instanceof MemoryBlockstore) - }) - - it('uses memory datastore when memory is specified', async () => { - const m = await base({ - config: mockConfig({ storage: ':memory:' }), - argv - }) - - assert(m.datastore instanceof MemoryDatastore) - }) - - it('uses fs blockstore when a path is specified', async () => { - const blockstorePath = Path.join(testPath, 'blockstore') - const testData = uint8ArrayFromString('test') - - const m = await base({ - config: mockConfig({ storage: testPath }), - argv - }) - - assert(m.blockstore instanceof FsBlockstore) - - await m.blockstore.put(CID.parse('QmaCpDMGvV2BGHeYERUEnRQAwe3N8SzbUtfsmvsqQLuvuJ'), testData) - - const out = await fs.readdir(blockstorePath, { recursive: true }) - - assert.deepEqual(out, [ - '7I', - '7I/BCIQLASSX2QHMUE4IBHYTTJ3LCICEGM6DOQBZDSN7DTU5RYQ2PEQQX7I.data' - ]) - - const blockData = await fs.readFile(Path.join(blockstorePath, '7I/BCIQLASSX2QHMUE4IBHYTTJ3LCICEGM6DOQBZDSN7DTU5RYQ2PEQQX7I.data')) - - assert.deepEqual(new Uint8Array(blockData), testData) - }) - - it('uses fs datastore when a path is specified', async () => { - const datastorePath = Path.join(testPath, 'datastore') - - const m = await base({ - config: mockConfig({ storage: testPath }), - argv - }) - - assert(m.datastore instanceof FsDatastore) - - await m.datastore.put(new Key('key'), uint8ArrayFromString('value')) - - const subDatastore = extendDatastore(extendDatastore(m.datastore, 'a'), 'b/c') - await subDatastore.put(new Key('d/e'), uint8ArrayFromString('test')) - - const out = await fs.readdir(datastorePath, { recursive: true }) - - assert.deepEqual(out, [ - 'a', - 'key.data', - 'a/b', - 'a/b/c', - 'a/b/c/d', - 'a/b/c/d/e.data' - ]) - - const data1 = await fs.readFile(Path.join(datastorePath, 'key.data')) - const data2 = await fs.readFile(Path.join(datastorePath, 'a/b/c/d/e.data')) - - assert.deepEqual(new Uint8Array(data1), uint8ArrayFromString('value')) - assert.deepEqual(new Uint8Array(data2), uint8ArrayFromString('test')) - }) -}) diff --git a/packages/daemon/test/modules/config.spec.ts b/packages/daemon/test/modules/config.spec.ts deleted file mode 100644 index 26c93b57..00000000 --- a/packages/daemon/test/modules/config.spec.ts +++ /dev/null @@ -1,54 +0,0 @@ -import assert from 'assert/strict' -import fs from 'fs/promises' -import Path from 'path' -import { z } from 'zod' -import config from '../../src/modules/config/index.js' -import { mkTestPath } from '../utils/paths.js' - -const testPath = mkTestPath('config') -const configPath = Path.join(testPath, 'config.json') - -const configData = { - private: true, - tickInterval: 1000, - storage: ':memory:', - addresses: [ - '/ip4/0.0.0.0/tcp/0' - ], - bootstrap: ['/dnsaddr/bootstrap.libp2p.io/p2p/QmNnooDu7bfjPFoTZYxMNLWUQJyrVwtbZg5gBMjTezGAJN'] -} - -before(async () => { - await fs.mkdir(testPath, { recursive: true }) - await fs.writeFile(configPath, JSON.stringify(configData)) -}) - -after(async () => { - await fs.rm(testPath, { recursive: true }) -}) - -describe('config', () => { - it('returns parsed config from the file', async () => { - const m = await config({ - argv: { config: configPath, key: '', socket: '' } - }) - - assert.deepEqual(m.config, configData) - }) - - it('gets config from schema', async () => { - const m = await config({ - argv: { config: configPath, key: '', socket: '' } - }) - - assert.deepEqual( - m.get(z.object({ bootstrap: z.array(z.string()) })), - { bootstrap: configData.bootstrap } - ) - - assert.deepEqual( - m.get(z.object({ tickInterval: z.number(), private: z.boolean() })), - { tickInterval: configData.tickInterval, private: configData.private } - ) - }) -}) diff --git a/packages/daemon/test/modules/downloader.spec.ts b/packages/daemon/test/modules/downloader.spec.ts deleted file mode 100644 index 5c459870..00000000 --- a/packages/daemon/test/modules/downloader.spec.ts +++ /dev/null @@ -1,236 +0,0 @@ -import assert from 'assert/strict' -import fs from 'fs/promises' -import Path from 'path' -import { KeyManager } from '@organicdesign/db-key-manager' -import { createNetClient } from '@organicdesign/net-rpc' -import { MemoryBlockstore } from 'blockstore-core' -import { CID } from 'multiformats/cid' -import createDownloader from '../../src/modules/downloader/index.js' -import createNetwork from '../../src/modules/network/index.js' -import createRpc from '../../src/modules/rpc/index.js' -import createSigint from '../../src/modules/sigint/index.js' -import { createDag } from '../utils/dag.js' -import { generateKey } from '../utils/generate-key.js' -import { mkTestPath } from '../utils/paths.js' -import mockArgv from './mock-argv.js' -import mockBase from './mock-base.js' -import mockConfig from './mock-config.js' -import type { - Requires as DownloaderComponents, - Provides as DownloaderProvides -} from '../../src/modules/downloader/index.js' - -describe('downloader', () => { - const testPath = mkTestPath('groups') - - const create = async (name?: string): Promise & { - argv: ReturnType - config: ReturnType - rpc: Awaited> - base: ReturnType - network: Awaited> - downloader: DownloaderProvides - }> => { - const path = name == null ? testPath : Path.join(testPath, name) - const keyManager = new KeyManager(await generateKey()) - - await fs.mkdir(path, { recursive: true }) - - const argv = mockArgv(path) - const config = mockConfig({ storage: ':memory:' }) - const sigint = await createSigint() - const rpc = await createRpc({ argv, sigint }) - const base = mockBase({ keyManager }) - const network = await createNetwork({ config, sigint, base, rpc }) - - const downloader = await createDownloader({ - sigint, - base, - rpc, - network, - config - }) - - return { - argv, - config, - sigint, - rpc, - base, - network, - downloader - } - } - - before(async () => { - await fs.mkdir(testPath, { recursive: true }) - }) - - after(async () => { - await fs.rm(testPath, { recursive: true }) - }) - - it('has 20 slots by default', async () => { - const { downloader: m, sigint } = await create() - - assert.equal(m.config.slots, 20) - - await sigint.interupt() - }) - - it('rpc - set priority updates local priority', async () => { - const group = 'QmNnooDu7bfjPFoTZYxMNLWUQJyrVwtbZg5gBMjTezGAJN' - const path = '/test.txt' - const priority = 50 - const { downloader: m, sigint, argv } = await create() - const client = createNetClient(argv.socket) - const key = Path.join('/', group, path) - - await m.pinManager.put(key, { priority: 1, cid: CID.parse(group) }) - - const response = await client.rpc.request('set-priority', { group, path, priority }) - - assert.equal(response, null) - - const pinData = await m.pinManager.get(key) - - assert(pinData != null) - assert.equal(pinData.priority, priority) - - client.close() - await sigint.interupt() - }) - - it('rpc - get speed', async () => { - const { downloader: m, network, sigint, argv } = await create() - const blockstore = new MemoryBlockstore() - const dag = await createDag({ blockstore }, 2, 2) - const group = 'QmNnooDu7bfjPFoTZYxMNLWUQJyrVwtbZg5gBMjTezGAJN' - const path = '/test.txt' - const range = 500 - const client = createNetClient(argv.socket) - const key = Path.join('/', group, path) - - await m.pinManager.put(key, { priority: 1, cid: dag[0] }) - - const speed1 = await client.rpc.request('get-speeds', { - cids: [dag[0].toString()], - range - }) - - assert.deepEqual(speed1, [{ cid: dag[0].toString(), speed: 0 }]) - - const value = await blockstore.get(dag[0]) - - await network.helia.blockstore.put(dag[0], value) - await new Promise(resolve => setTimeout(resolve, range / 2)) - - const speed2 = await client.rpc.request('get-speeds', { - cids: [dag[0].toString()], - range - }) - - assert.deepEqual(speed2, [{ cid: dag[0].toString(), speed: value.length / range }]) - - await new Promise(resolve => setTimeout(resolve, range / 2)) - - const values = await Promise.all([ - blockstore.get(dag[1]), - blockstore.get(dag[4]) - ]) - - await Promise.all([ - network.helia.blockstore.put(dag[1], values[0]), - network.helia.blockstore.put(dag[4], values[1]) - ]) - - await new Promise(resolve => setTimeout(resolve, range / 2)) - - const speed3 = await client.rpc.request('get-speeds', { - cids: [dag[0].toString()], - range - }) - - assert.deepEqual(speed3, [{ - cid: dag[0].toString(), - speed: values.reduce((a, c) => c.length + a, 0) / range - }]) - - client.close() - await sigint.interupt() - }) - - it('rpc - get status', async () => { - const { downloader: m, network, sigint, argv } = await create() - const blockstore = new MemoryBlockstore() - const dag = await createDag({ blockstore }, 2, 2) - const group = 'QmNnooDu7bfjPFoTZYxMNLWUQJyrVwtbZg5gBMjTezGAJN' - const path = '/test.txt' - const client = createNetClient(argv.socket) - const key = Path.join('/', group, path) - - const status1 = await client.rpc.request('get-status', { - cids: [dag[0].toString()] - }) - - assert.deepEqual(status1, [{ - cid: dag[0].toString(), - blocks: 0, - size: 0, - state: 'NOTFOUND' - }]) - - await m.pinManager.put(key, { priority: 1, cid: dag[0] }) - - const status2 = await client.rpc.request('get-status', { - cids: [dag[0].toString()] - }) - - assert.deepEqual(status2, [{ - cid: dag[0].toString(), - blocks: 0, - size: 0, - state: 'DOWNLOADING' - }]) - - const value = await blockstore.get(dag[0]) - - await network.helia.blockstore.put(dag[0], value) - await new Promise(resolve => setTimeout(resolve, 100)) - - const status3 = await client.rpc.request('get-status', { - cids: [dag[0].toString()] - }) - - assert.deepEqual(status3, [{ - cid: dag[0].toString(), - blocks: 1, - size: value.length, - state: 'DOWNLOADING' - }]) - - const values = await Promise.all(dag.map(async cid => { - const value = await blockstore.get(cid) - - await network.helia.blockstore.put(cid, value) - - return value.length - })) - - await new Promise(resolve => setTimeout(resolve, 500)) - - const status4 = await client.rpc.request('get-status', { - cids: [dag[0].toString()] - }) - - assert.deepEqual(status4, [{ - cid: dag[0].toString(), - blocks: dag.length, - size: values.reduce((a, c) => a + c, 0), - state: 'COMPLETED' - }]) - - client.close() - await sigint.interupt() - }) -}) diff --git a/packages/daemon/test/modules/filesystem.spec.ts b/packages/daemon/test/modules/filesystem.spec.ts deleted file mode 100644 index 2c1d8896..00000000 --- a/packages/daemon/test/modules/filesystem.spec.ts +++ /dev/null @@ -1,618 +0,0 @@ -import assert from 'assert/strict' -import fs from 'fs/promises' -import Path from 'path' -import { unixfs } from '@helia/unixfs' -import { KeyManager } from '@organicdesign/db-key-manager' -import * as testData from '@organicdesign/db-test-utils' -import { importer } from '@organicdesign/db-utils' -import { createNetClient } from '@organicdesign/net-rpc' -import { MemoryBlockstore } from 'blockstore-core' -import all from 'it-all' -import { CID } from 'multiformats/cid' -import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string' -import { toString as uint8ArrayToString } from 'uint8arrays/to-string' -import createDownloader from '../../src/modules/downloader/index.js' -import createFilesystem from '../../src/modules/filesystem/index.js' -import createGroups from '../../src/modules/groups/index.js' -import createNetwork from '../../src/modules/network/index.js' -import createRpc from '../../src/modules/rpc/index.js' -import createSigint from '../../src/modules/sigint/index.js' -import createTick from '../../src/modules/tick/index.js' -import { createGroup } from '../utils/create-group.js' -import { createDag } from '../utils/dag.js' -import { generateKey } from '../utils/generate-key.js' -import { mkTestPath } from '../utils/paths.js' -import mockArgv from './mock-argv.js' -import mockBase from './mock-base.js' -import mockConfig from './mock-config.js' -import type { Provides as FSProvides } from '../../src/modules/filesystem/index.js' - -describe('filesystem', () => { - const testPath = mkTestPath('filesystem') - - const create = async (name?: string): Promise<{ - argv: ReturnType - config: ReturnType - rpc: Awaited> - base: ReturnType - network: Awaited> - groups: Awaited> - filesystem: FSProvides - sigint: Awaited> - tick: Awaited> - }> => { - const path = name == null ? testPath : Path.join(testPath, name) - - const keyManager = new KeyManager(await generateKey()) - - await fs.mkdir(path, { recursive: true }) - - const argv = mockArgv(path) - const config = mockConfig({ storage: ':memory:' }) - const sigint = await createSigint() - const rpc = await createRpc({ argv, sigint }) - const base = mockBase({ keyManager }) - const network = await createNetwork({ config, sigint, base, rpc }) - - const groups = await createGroups({ - sigint, - base, - rpc, - network - }) - - const downloader = await createDownloader({ - sigint, - base, - rpc, - network, - config - }) - - const tick = await createTick({ config, sigint }) - - const filesystem = await createFilesystem({ - sigint, - base, - rpc, - network, - groups, - downloader, - tick, - config - }) - - return { - argv, - config, - sigint, - rpc, - base, - network, - groups, - filesystem, - tick - } - } - - before(async () => { - await fs.mkdir(testPath, { recursive: true }) - }) - - after(async () => { - await fs.rm(testPath, { recursive: true }) - }) - - it('uses all as the default revision strategy', async () => { - const { filesystem: m, sigint } = await create() - - assert.equal(m.config.defaultRevisionStrategy, 'all') - - await sigint.interupt() - }) - - it('returns null when wraping a group that doesn\'t exist', async () => { - const { filesystem: m, sigint } = await create() - const fs = m.getFileSystem(CID.parse('QmNnooDu7bfjPFoTZYxMNLWUQJyrVwtbZg5gBMjTezGAJN')) - - assert.equal(fs, null) - - await sigint.interupt() - }) - - it('wraps a group in a filesystem', async () => { - const { filesystem: m, groups, sigint } = await create() - const group = await createGroup(groups, 'test') - const fs = m.getFileSystem(group) - - assert.notEqual(fs, null) - - await sigint.interupt() - }) - - it('uploads show in filesystem', async () => { - const { filesystem: m, groups, network, sigint } = await create() - const group = await createGroup(groups, 'test') - const fs = m.getFileSystem(group) - const dag = await createDag(network.helia, 2, 2) - const path = '/test' - - assert(fs != null) - - const before = Date.now() - - await m.uploads.add('put', [group.bytes, path, { - cid: dag[0].bytes, - encrypted: false, - revisionStrategy: 'all' as const, - priority: 1 - }]) - - const entry = await fs.get(path) - const values = await Promise.all(dag.map(async d => network.helia.blockstore.get(d))) - const size = values.reduce((a, c) => a + c.length, 0) - - assert(entry != null) - assert.deepEqual(entry.author, groups.welo.identity.id) - assert.equal(entry.blocks, dag.length) - assert.deepEqual(entry.cid, dag[0]) - assert.equal(entry.encrypted, false) - assert.equal(entry.priority, 1) - assert.equal(entry.revisionStrategy, 'all') - assert.equal(entry.sequence, 0) - assert.equal(entry.size, size) - assert(entry.timestamp >= before) - assert(entry.timestamp <= Date.now()) - - await sigint.interupt() - }) - - it('emits the file:added event when the FS was written to', async () => { - const { filesystem: m, groups, network, sigint } = await create() - const group = await createGroup(groups, 'test') - const dag = await createDag(network.helia, 2, 2) - const path = '/test' - - const promise = new Promise((resolve, reject) => { - setTimeout(() => { reject(new Error('timeout')) }, 100) - - m.events.addEventListener('file:added', () => { resolve() }, { once: true }) - }) - - await m.uploads.add('put', [group.bytes, path, { - cid: dag[0].bytes, - encrypted: false, - revisionStrategy: 'all' as const, - priority: 1 - }]) - - await promise - - await sigint.interupt() - }) - - it('local settings change FS output', async () => { - const { filesystem: m, groups, network, sigint } = await create() - const group = await createGroup(groups, 'test') - const fs = m.getFileSystem(group) - const dag = await createDag(network.helia, 2, 2) - const path = '/test' - - assert(fs != null) - - await m.uploads.add('put', [group.bytes, path, { - cid: dag[0].bytes, - encrypted: false, - revisionStrategy: 'all' as const, - priority: 1 - }]) - - await m.localSettings.set(group, path, { - priority: 100 - }) - - const entry = await fs.get(path) - - assert(entry != null) - assert.equal(entry.priority, 100) - - await sigint.interupt() - }) - - it('rpc - delete (file)', async () => { - const { filesystem: m, groups, network, sigint, argv } = await create() - const client = createNetClient(argv.socket) - const group = await createGroup(groups, 'test') - const fs = m.getFileSystem(group) - const dag = await createDag(network.helia, 2, 2) - const path = '/test' - - assert(fs != null) - - await m.uploads.add('put', [group.bytes, path, { - cid: dag[0].bytes, - encrypted: false, - revisionStrategy: 'all' as const, - priority: 1 - }]) - - const response = await client.rpc.request('delete', { group: group.toString(), path }) - - assert.deepEqual(response, [{ path, cid: dag[0].toString() }]) - - const entry = await fs.get(path) - - assert.equal(entry, null) - - client.close() - await sigint.interupt() - }) - - it('rpc - delete (directory)', async () => { - const { filesystem: m, groups, network, sigint, argv } = await create() - const client = createNetClient(argv.socket) - const group = await createGroup(groups, 'test') - const fs = m.getFileSystem(group) - const dag = await createDag(network.helia, 2, 2) - const rootPath = '/test' - const paths = [`${rootPath}/file1`, `${rootPath}/file2`, `${rootPath}/sub/file3`] - - assert(fs != null) - - await Promise.all(paths.map(async path => - m.uploads.add('put', [group.bytes, path, { - cid: dag[0].bytes, - encrypted: false, - revisionStrategy: 'all' as const, - priority: 1 - }]) - )) - - const response = await client.rpc.request('delete', { group: group.toString(), path: rootPath }) - - assert.deepEqual(response, paths.map(path => ({ path, cid: dag[0].toString() }))) - - const entries = await all(fs.getDir(rootPath)) - - assert.deepEqual(entries, []) - - client.close() - await sigint.interupt() - }) - - it('rpc - edit priority', async () => { - const { filesystem: m, groups, sigint, argv } = await create() - const client = createNetClient(argv.socket) - const group = await createGroup(groups, 'test') - const fs = m.getFileSystem(group) - const path = '/test' - const priority = 50 - - assert(fs != null) - - const response = await client.rpc.request('edit', { group: group.toString(), path, priority }) - - assert.equal(response, null) - - const localSettings = await m.localSettings.get(group, path) - - assert.equal(localSettings.priority, priority) - - client.close() - await sigint.interupt() - }) - - it('rpc - export (file)', async () => { - const { filesystem: m, network, groups, sigint, argv } = await create() - const client = createNetClient(argv.socket) - const group = await createGroup(groups, 'test') - const fs = m.getFileSystem(group) - const rootPath = '/test' - const outPath = Path.join(testPath, 'export-file') - - assert(fs != null) - - await Promise.all(testData.data.map(async data => { - const result = await all(importer(network.helia.blockstore, data.path)) - - await m.uploads.add('put', [group.bytes, data.generatePath(rootPath), { - cid: result[0].cid.bytes, - encrypted: false, - revisionStrategy: 'all', - priority: 1 - }]) - })) - - for (const data of testData.data) { - const exportPath = data.generatePath(outPath) - - const response = await client.rpc.request('export', { - group: group.toString(), - path: data.generatePath(rootPath), - outPath: exportPath - }) - - const valid = await data.validate(exportPath) - - assert.equal(response, null) - assert.equal(valid, true) - } - - client.close() - await sigint.interupt() - }) - - it('rpc - export (directory)', async () => { - const { filesystem: m, network, groups, sigint, argv } = await create() - const client = createNetClient(argv.socket) - const group = await createGroup(groups, 'test') - const fs = m.getFileSystem(group) - const rootPath = '/test' - const outPath = Path.join(testPath, 'export-directory') - - assert(fs != null) - - await Promise.all(testData.data.map(async data => { - const result = await all(importer(network.helia.blockstore, data.path)) - - await m.uploads.add('put', [group.bytes, data.generatePath(rootPath), { - cid: result[0].cid.bytes, - encrypted: false, - revisionStrategy: 'all', - priority: 1 - }]) - })) - - const response = await client.rpc.request('export', { - group: group.toString(), - path: rootPath, - outPath - }) - - assert.equal(response, null) - - for (const data of testData.data) { - const exportPath = data.generatePath(outPath) - const valid = await data.validate(exportPath) - - assert.equal(valid, true) - } - - client.close() - await sigint.interupt() - }) - - it('rpc - import (file)', async () => { - const { filesystem: m, groups, sigint, argv } = await create() - const client = createNetClient(argv.socket) - const group = await createGroup(groups, 'test') - const fs = m.getFileSystem(group) - const rootPath = '/test' - - assert(fs != null) - - await Promise.all(testData.data.map(async data => { - const virtualPath = data.generatePath(rootPath) - - const response = await client.rpc.request('import', { - group: group.toString(), - path: virtualPath, - inPath: data.path - }) - - assert.deepEqual(response, [{ - path: virtualPath, - inPath: data.path, - cid: data.cid.toString() - }]) - - const result = await fs.get(virtualPath) - - assert(result != null) - assert.deepEqual(result.cid, data.cid) - })) - - client.close() - await sigint.interupt() - }) - - it('rpc - import (directory)', async () => { - const { filesystem: m, groups, sigint, argv } = await create() - const client = createNetClient(argv.socket) - const group = await createGroup(groups, 'test') - const fs = m.getFileSystem(group) - const rootPath = '/test' - - assert(fs != null) - - const response = await client.rpc.request('import', { - group: group.toString(), - path: rootPath, - inPath: testData.root - }) - - assert(Array.isArray(response)) - assert.equal(response.length, testData.data.length) - - for (const data of response) { - const dataFile = testData.getDataFile(data.inPath) - - assert(dataFile != null) - assert.equal(data.path, dataFile.generatePath(rootPath)) - assert.equal(data.cid, dataFile.cid.toString()) - } - - const fsResult = await all(fs.getDir(rootPath)) - - assert(Array.isArray(fsResult)) - assert.equal(fsResult.length, testData.data.length) - - for (const dataFile of testData.data) { - const virtualPath = dataFile.generatePath(rootPath) - const r = fsResult.find(r => r.key === virtualPath) - - assert(r != null) - assert.deepEqual(r.value.cid, dataFile.cid) - } - - client.close() - await sigint.interupt() - }) - - it('rpc - list', async () => { - const { filesystem: m, groups, network, sigint, argv } = await create() - const client = createNetClient(argv.socket) - const group = await createGroup(groups, 'test') - const fs = m.getFileSystem(group) - const dag = await createDag(network.helia, 2, 2) - const rootPath = '/test' - const paths = [`${rootPath}/file1`, `${rootPath}/file2`, `${rootPath}/sub/file3`] - - assert(fs != null) - - const before = Date.now() - - await Promise.all(paths.map(async path => - m.uploads.add('put', [group.bytes, path, { - cid: dag[0].bytes, - encrypted: false, - revisionStrategy: 'all' as const, - priority: 1 - }]) - )) - - const response = await client.rpc.request('list', { group: group.toString(), path: '/' }) - - assert(Array.isArray(response)) - - const values = await Promise.all(dag.map(async d => network.helia.blockstore.get(d))) - const size = values.reduce((a, c) => a + c.length, 0) - - for (const entry of response) { - assert(entry != null) - assert.equal(entry.author, uint8ArrayToString(groups.welo.identity.id, 'base58btc')) - assert.equal(entry.blocks, dag.length) - assert.deepEqual(entry.cid, dag[0].toString()) - assert.equal(entry.encrypted, false) - assert.equal(entry.priority, 1) - assert.equal(entry.revisionStrategy, 'all') - assert.equal(entry.size, size) - assert(entry.timestamp >= before) - assert(entry.timestamp <= Date.now()) - } - - client.close() - await sigint.interupt() - }) - - it('rpc - read', async () => { - const { filesystem: m, groups, network, sigint, argv } = await create() - const client = createNetClient(argv.socket) - const group = await createGroup(groups, 'test') - const ufs = unixfs(network.helia) - const path = '/test' - const data = 'test-data' - - const cid = await ufs.addBytes(uint8ArrayFromString(data)) - - await m.uploads.add('put', [group.bytes, path, { - cid: cid.bytes, - encrypted: false, - revisionStrategy: 'all' as const, - priority: 1 - }]) - - const read1 = await client.rpc.request('read', { group: group.toString(), path }) - - assert.deepEqual(read1, data) - - const read2 = await client.rpc.request('read', { group: group.toString(), path, position: 1 }) - - assert.deepEqual(read2, data.slice(1)) - - const read3 = await client.rpc.request('read', { group: group.toString(), path, length: 3 }) - - assert.deepEqual(read3, data.slice(0, 3)) - - const read4 = await client.rpc.request('read', { group: group.toString(), path, position: 1, length: 3 }) - - assert.deepEqual(read4, data.slice(1, 3 + 1)) - - client.close() - await sigint.interupt() - }) - - it('rpc - write', async () => { - const { filesystem: m, base, groups, sigint, argv } = await create() - const client = createNetClient(argv.socket) - const group = await createGroup(groups, 'test') - const fs = m.getFileSystem(group) - const ufs = unixfs({ blockstore: new MemoryBlockstore() }) - const path = '/test' - const data = 'test-data' - const cid = await ufs.addBytes(uint8ArrayFromString(data)) - - assert(fs != null) - - const before = Date.now() - - const write1 = await client.rpc.request('write', { group: group.toString(), path, data }) - - assert.equal(write1, data.length) - - const entry1 = await fs.get(path) - - assert(entry1 != null) - assert.deepEqual(entry1.author, groups.welo.identity.id) - assert.equal(entry1.blocks, 1) - assert.deepEqual(entry1.cid, cid) - assert.equal(entry1.encrypted, false) - assert.equal(entry1.priority, 100) - assert.equal(entry1.revisionStrategy, 'all') - assert.equal(entry1.size, data.length) - assert(entry1.timestamp >= before) - assert(entry1.timestamp <= Date.now()) - - const newData2 = 'your-data-long' - const write2 = await client.rpc.request('write', { group: group.toString(), path, data: newData2 }) - - assert.equal(write2, newData2.length) - - const entry2 = await fs.get(path) - - assert(entry2 != null) - - const value2 = await base.blockstore.get(entry2.cid) - - assert.deepEqual(value2, uint8ArrayFromString(newData2)) - - const newData3 = 'test' - const write3 = await client.rpc.request('write', { group: group.toString(), path, data: newData3, length: newData3.length }) - - assert.equal(write3, newData3.length) - - const entry3 = await fs.get(path) - - assert(entry3 != null) - - const value3 = await base.blockstore.get(entry3.cid) - - assert.deepEqual(value3, uint8ArrayFromString('test-data-long')) - - const newData4 = 'long' - const write4 = await client.rpc.request('write', { group: group.toString(), path, data: newData4, length: newData4.length, position: 5 }) - - assert.equal(write4, newData4.length) - - const entry4 = await fs.get(path) - - assert(entry4 != null) - - const value4 = await base.blockstore.get(entry4.cid) - - assert.deepEqual(value4, uint8ArrayFromString('test-long-long')) - - client.close() - await sigint.interupt() - }) -}) diff --git a/packages/daemon/test/modules/groups.spec.ts b/packages/daemon/test/modules/groups.spec.ts deleted file mode 100644 index f94d84f9..00000000 --- a/packages/daemon/test/modules/groups.spec.ts +++ /dev/null @@ -1,311 +0,0 @@ -import assert from 'assert/strict' -import fs from 'fs/promises' -import Path from 'path' -import { KeyManager, parseKeyData } from '@organicdesign/db-key-manager' -import { createNetClient } from '@organicdesign/net-rpc' -import * as cborg from 'cborg' -import all from 'it-all' -import { CID } from 'multiformats/cid' -import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string' -import { toString as uint8ArrayToString } from 'uint8arrays/to-string' -import createGroups from '../../src/modules/groups/index.js' -import createNetwork from '../../src/modules/network/index.js' -import createRpc from '../../src/modules/rpc/index.js' -import createSigint from '../../src/modules/sigint/index.js' -import { mkTestPath } from '../utils/paths.js' -import mockArgv from './mock-argv.js' -import mockBase from './mock-base.js' -import mockConfig from './mock-config.js' -import type { - Requires as GroupsComponents, - Provides as GroupsProvides -} from '../../src/modules/groups/index.js' - -describe('groups', () => { - const testPath = mkTestPath('groups') - - const mkGroup = async (m: GroupsProvides, name: string, peers: Uint8Array[] = []): Promise => { - const manifest = await m.welo.determine({ - name, - meta: { type: 'group' }, - access: { - protocol: '/hldb/access/static', - config: { write: [m.welo.identity.id, ...peers] } - } - }) - - await m.groups.add(manifest) - - return manifest.address.cid - } - - const create = async (name?: string): Promise & { - argv: ReturnType - config: ReturnType - rpc: Awaited> - base: ReturnType - network: Awaited> - groups: GroupsProvides - }> => { - const path = name == null ? testPath : Path.join(testPath, name) - - const keyManager = name == null - ? undefined - : new KeyManager(parseKeyData({ - key: 'DpGbLiAX4wK4HHtG3DQb8cA6FG2ibv93X4ooZJ5LmMJJ-12FmenN8dbWysuYnzEHzmEF1hod4RGK8NfKFu1SEZ7XM', - psk: '/key/swarm/psk/1.0.0/\n/base16/\n023330a98e30315e2233d4a31a6dc65d741a89f7ce6248e7de40c73995d23157' - })) - - await fs.mkdir(path, { recursive: true }) - - const argv = mockArgv(path) - const config = mockConfig({ storage: ':memory:' }) - const sigint = await createSigint() - const rpc = await createRpc({ argv, sigint }) - const base = mockBase({ keyManager }) - const network = await createNetwork({ config, sigint, base, rpc }) - - const groups = await createGroups({ - sigint, - base, - rpc, - network - }) - - return { - argv, - config, - sigint, - rpc, - base, - network, - groups - } - } - - before(async () => { - await fs.mkdir(testPath, { recursive: true }) - }) - - after(async () => { - await fs.rm(testPath, { recursive: true }) - }) - - it('creates a group', async () => { - const { groups: m, sigint } = await create() - const group = await mkGroup(m, 'test') - - assert(group) - - await sigint.interupt() - }) - - it('tracker puts and checks a group\'s content', async () => { - const { groups: m, sigint } = await create() - const group = await mkGroup(m, 'test') - const database = m.groups.get(group) - - if (database == null) { - throw new Error('group creation failed') - } - - const tracker = m.getTracker(database) - - const key = 'test' - const put = database.store.creators.put(key, 'my-data') - - await database.replica.write(put) - - assert.equal(await tracker.validate(key, put), false) - - await tracker.put(key, put) - - assert.equal(await tracker.validate(key, put), true) - - await sigint.interupt() - }) - - it('tracker processes a group\'s content', async () => { - const { groups: m, sigint } = await create() - const group = await mkGroup(m, 'test') - const database = m.groups.get(group) - - if (database == null) { - throw new Error('group creation failed') - } - - const tracker = m.getTracker(database) - const key = '/test' - const value = 'my-data' - const put = database.store.creators.put(key, value) - - let entries = await all(tracker.process()) - assert.deepEqual(entries, []) - - await database.replica.write(put) - entries = await all(tracker.process()) - assert.deepEqual(entries, [{ key, value: cborg.encode(value) }]) - - entries = await all(tracker.process()) - assert.deepEqual(entries, []) - - await sigint.interupt() - }) - - it('tracker is scope limited', async () => { - const { groups: m, sigint } = await create() - const group = await mkGroup(m, 'test') - const database = m.groups.get(group) - - if (database == null) { - throw new Error('group creation failed') - } - - const tracker = m.getTracker(database) - const key = '/test' - const value = 'my-data' - const put = database.store.creators.put(key, value) - - await database.replica.write(put) - - let entries = await all(tracker.process('/another-key')) - assert.deepEqual(entries, []) - - entries = await all(tracker.process(key)) - assert.deepEqual(entries, [{ key, value: cborg.encode(value) }]) - - await sigint.interupt() - }) - - it('uses the identity from base in welo', async () => { - const { groups: m, sigint, base } = await create() - - assert.deepEqual(m.welo.identity, await base.keyManager.getWeloIdentity()) - - await sigint.interupt() - }) - - it('rpc - id returns the base58btc formatted welo id', async () => { - const { groups: m, sigint, argv } = await create() - const client = createNetClient(argv.socket) - - const id = await client.rpc.request('id', {}) - - assert.equal(uint8ArrayToString(m.welo.identity.id, 'base58btc'), id) - - client.close() - await sigint.interupt() - }) - - it('rpc - create groups creates a group without other peers', async () => { - const { groups: m, sigint, argv } = await create() - const client = createNetClient(argv.socket) - const name = 'test' - - const cid = await client.rpc.request('create-group', { name, peers: [] }) - const group = CID.parse(cid) - const database = m.groups.get(group) - - assert(database != null) - assert.equal(database.manifest.name, name) - assert.deepEqual(database.manifest.access.config?.write, [m.welo.identity.id]) - - client.close() - await sigint.interupt() - }) - - it('rpc - create groups creates a group with other peers', async () => { - const { groups: m, sigint, argv } = await create() - const client = createNetClient(argv.socket) - const name = 'test' - const otherPeer = 'GZsJqUjmbVqZCUMbJoe5ye4xfdKZVPVwBoFFQiyCZYesq6Us5b' - - const cid = await client.rpc.request('create-group', { name, peers: [otherPeer] }) - const group = CID.parse(cid) - const database = m.groups.get(group) - - assert(database != null) - assert.equal(database.manifest.name, name) - assert.deepEqual(database.manifest.access.config?.write, [ - m.welo.identity.id, - uint8ArrayFromString(otherPeer, 'base58btc') - ]) - - client.close() - await sigint.interupt() - }) - - it('rpc - joins an external group', async () => { - const components = await Promise.all([create(), create('server-join-group')]) - const client = createNetClient(components[0].argv.socket) - const name = 'test' - const group = await mkGroup(components[1].groups, name) - - await components[0].network.libp2p.dial(components[1].network.libp2p.getMultiaddrs()) - const res = await client.rpc.request('join-group', { group: group.toString() }) - - assert.equal(res, null) - - const database = components[0].groups.groups.get(group) - - assert(database) - assert.equal(database.manifest.name, name) - - client.close() - await Promise.all(components.map(async c => c.sigint.interupt())) - }) - - it('rpc - list groups', async () => { - const { groups: m, sigint, argv } = await create() - const client = createNetClient(argv.socket) - const name = 'test' - - let groups = await client.rpc.request('list-groups', {}) - - assert.deepEqual(groups, []) - - const group = await mkGroup(m, name) - - groups = await client.rpc.request('list-groups', {}) - - assert.deepEqual(groups, [{ group: group.toString(), name }]) - - client.close() - await sigint.interupt() - }) - - // This fails it github too - seems to think the `server-sync-groups` socket is in use? - it.skip('rpc - sync groups', async () => { - const components = await Promise.all([create(), create('server-sync-groups')]) - const client = createNetClient(components[0].argv.socket) - const key = '/test' - const value = 'test-value' - - const groups = await client.rpc.request('list-groups', {}) - - assert.deepEqual(groups, []) - - const group = await mkGroup(components[1].groups, 'test') - const database = components[1].groups.groups.get(group) - - if (database == null) { - throw new Error('database creation failed') - } - - const put = database.store.creators.put(key, value) - - await database.replica.write(put) - - await components[0].network.libp2p.dial(components[1].network.libp2p.getMultiaddrs()) - await client.rpc.request('join-group', { group: group.toString() }) - await client.rpc.request('sync', {}) - - const index = await database.store.latest() - const result = await database.store.selectors.get(index)(key) - - assert.deepEqual(result, value) - - client.close() - await Promise.all(components.map(async c => c.sigint.interupt())) - }) -}) diff --git a/packages/daemon/test/modules/mock-argv.ts b/packages/daemon/test/modules/mock-argv.ts deleted file mode 100644 index c6da9904..00000000 --- a/packages/daemon/test/modules/mock-argv.ts +++ /dev/null @@ -1,12 +0,0 @@ -import Path from 'path' -import type { Provides } from '../../src/modules/argv/index.js' - -const provs = (path: string): Provides => { - return { - key: Path.join(path, 'key.json'), - config: Path.join(path, 'config.json'), - socket: Path.join(path, 'server.socket') - } -} - -export default provs diff --git a/packages/daemon/test/modules/mock-base.ts b/packages/daemon/test/modules/mock-base.ts deleted file mode 100644 index a139b417..00000000 --- a/packages/daemon/test/modules/mock-base.ts +++ /dev/null @@ -1,20 +0,0 @@ -import Path from 'path' -import { KeyManager, parseKeyData } from '@organicdesign/db-key-manager' -import { MemoryBlockstore } from 'blockstore-core' -import { FsBlockstore } from 'blockstore-fs' -import { MemoryDatastore } from 'datastore-core' -import { FsDatastore } from 'datastore-fs' -import type { Provides } from '../../src/modules/base/index.js' - -const provs = (config?: { path?: string, keyManager?: KeyManager }): Provides => { - return { - keyManager: config?.keyManager ?? new KeyManager(parseKeyData({ - key: '5TP9VimJU1WdSoTxZGLhSuPKqCpXirPHDK4ZjHxzetex-8zAV14C4oLe4dytUSVzznTuQ659pY1dSMG8HAQenDqVQ', - psk: '/key/swarm/psk/1.0.0/\n/base16/\n56d3c18282f1f1b1b3e04e40dd5d8bf44cafa8bc9c9bc7c57716a7766fa2c550' - })), - datastore: config?.path == null ? new MemoryDatastore() : new FsDatastore(Path.join(config?.path, 'datastore')), - blockstore: config?.path == null ? new MemoryBlockstore() : new FsBlockstore(Path.join(config?.path, 'blockstore')) - } -} - -export default provs diff --git a/packages/daemon/test/modules/mock-config.ts b/packages/daemon/test/modules/mock-config.ts deleted file mode 100644 index 45bf3666..00000000 --- a/packages/daemon/test/modules/mock-config.ts +++ /dev/null @@ -1,12 +0,0 @@ -import { type z } from 'zod' -import type { Provides } from '../../src/modules/config/index.js' - -const provs = (config: Record): Provides => ({ - config, - - get (schema: T): z.infer { - return schema.parse(config) - } -}) - -export default provs diff --git a/packages/daemon/test/modules/network.spec.ts b/packages/daemon/test/modules/network.spec.ts deleted file mode 100644 index 5b83c21d..00000000 --- a/packages/daemon/test/modules/network.spec.ts +++ /dev/null @@ -1,313 +0,0 @@ -import assert from 'assert/strict' -import fs from 'fs/promises' -import { unixfs } from '@helia/unixfs' -import { createNetClient } from '@organicdesign/net-rpc' -import { createHelia } from 'helia' -import { Key } from 'interface-datastore' -import all from 'it-all' -import { CID } from 'multiformats/cid' -import network from '../../src/modules/network/index.js' -import createRpc from '../../src/modules/rpc/index.js' -import createSigint from '../../src/modules/sigint/index.js' -import { mkTestPath } from '../utils/paths.js' -import mockArgv from './mock-argv.js' -import mockBase from './mock-base.js' -import mockConfig from './mock-config.js' -import type { Requires as NetworkComponents } from '../../src/modules/network/index.js' -import type { Libp2p } from '@libp2p/interface' -import createLibp2p from '@/modules/network/libp2p.js' - -describe('network', () => { - const testPath = mkTestPath('network') - let components: NetworkComponents & { argv: ReturnType } - - before(async () => { - await fs.mkdir(testPath, { recursive: true }) - - const sigint = await createSigint() - const config = mockConfig({ storage: ':memory:' }) - const argv = mockArgv(testPath) - const base = mockBase() - const rpc = await createRpc({ argv, sigint }) - - components = { - argv, - sigint, - config, - base, - rpc - } - }) - - after(async () => { - await components.sigint.interupt() - await new Promise(resolve => setTimeout(resolve, 1000)) - await fs.rm(testPath, { recursive: true }) - }) - - it('provides defaults for the config options', async () => { - const m = await network(components) - - assert.deepEqual(m.config.addresses, [ - '/ip4/127.0.0.1/tcp/0', - '/ip4/127.0.0.1/tcp/0/ws' - ]) - - assert.deepEqual(m.config.bootstrap, []) - assert.equal(m.config.private, false) - assert.equal(m.config.serverMode, false) - }) - - it('uses the stores derived form base for helia', async () => { - const m = await network(components) - const cid = CID.parse('QmaCpDMGvV2BGHeYERUEnRQAwe3N8SzbUtfsmvsqQLuvuJ') - - assert(!(await m.helia.blockstore.has(cid))) - - await components.base.blockstore.put(cid, new Uint8Array()) - - assert(await m.helia.blockstore.has(cid)) - - assert(!(await m.helia.datastore.has(new Key('/test')))) - - await components.base.datastore.put(new Key('/helia/datastore/test'), new Uint8Array()) - - assert(await m.helia.datastore.has(new Key('/test'))) - }) - - it('is not connectable from outside when private is set to true', async () => { - const config = mockConfig({ private: true }) - - const m = await network({ - ...components, - config - }) - - const libp2p = await createLibp2p({}) - - const mkSignal = (): AbortSignal => AbortSignal.timeout(1000) - - const dialTo = libp2p.dial(m.libp2p.getMultiaddrs(), { signal: mkSignal() }) - const dialFrom = m.libp2p.dial(libp2p.getMultiaddrs(), { signal: mkSignal() }) - - await Promise.all([ - assert.rejects(async () => dialTo), - assert.rejects(async () => dialFrom) - ]) - - await libp2p.stop() - }) - - it('is connectable from outside when private is set to false', async () => { - const config = mockConfig({ private: false }) - - const m = await network({ - ...components, - config - }) - - const [libp2p1, libp2p2] = await Promise.all([createLibp2p({}), createLibp2p({})]) - - const mkSignal = (): AbortSignal => AbortSignal.timeout(1000) - - await Promise.all([ - libp2p1.dial(m.libp2p.getMultiaddrs(), { signal: mkSignal() }), - m.libp2p.dial(libp2p2.getMultiaddrs(), { signal: mkSignal() }) - ]) - - await Promise.all([ - libp2p1.stop(), - libp2p2.stop() - ]) - }) - - it('is connectable from inside when private is set to true', async () => { - const config = mockConfig({ private: true }) - - const m = await network({ - ...components, - config - }) - - const create = async (): Promise => createLibp2p({ - psk: components.base.keyManager.getPskKey() - }) - - const [libp2p1, libp2p2] = await Promise.all([create(), create()]) - - const mkSignal = (): AbortSignal => AbortSignal.timeout(1000) - - await Promise.all([ - libp2p1.dial(m.libp2p.getMultiaddrs(), { signal: mkSignal() }), - m.libp2p.dial(libp2p2.getMultiaddrs(), { signal: mkSignal() }) - ]) - - await Promise.all([ - libp2p1.stop(), - libp2p2.stop() - ]) - }) - - it('bootstraps when a bootstrap peer is set', async () => { - const libp2p = await createLibp2p({}) - - const config = mockConfig({ - private: false, - bootstrap: libp2p.getMultiaddrs().map(a => a.toString()) - }) - - const m = await network({ - ...components, - config - }) - - const peer = await new Promise((resolve, reject) => { - setTimeout(() => { reject(new Error('timeout')) }, 5000) - - m.libp2p.addEventListener('peer:connect', (a) => { - resolve(a.detail.toBytes()) - }) - }) - - assert.deepEqual(peer, libp2p.peerId.toBytes()) - - await libp2p.stop() - }) - - // Something is failing inside websockets... - it.skip('relays when server mode is set', async () => { - const [libp2p1, libp2p2] = await Promise.all([ - createLibp2p({ addresses: ['/ip4/127.0.0.1/tcp/0'] }), - createLibp2p({ addresses: ['/ip4/127.0.0.1/tcp/0/ws'] }) - ]) - - const m = await network({ - ...components, - config: mockConfig({ - private: false, - serverMode: true - }) - }) - - await Promise.all([ - libp2p1.dial(m.libp2p.getMultiaddrs()), - libp2p2.dial(m.libp2p.getMultiaddrs()) - ]) - - await new Promise(resolve => setTimeout(resolve, 5000)) - - await libp2p1.dial(libp2p2.getMultiaddrs()) - - await Promise.all([ - libp2p1.stop(), - libp2p2.stop() - ]) - }) - - it('libp2p remembers peers with persistant storage', async () => { - const libp2p = await createLibp2p({}) - const sigints = await Promise.all([createSigint(), createSigint()]) - - const create = async (index: number): ReturnType => network({ - ...components, - - sigint: sigints[index], - - base: mockBase({ path: testPath }), - - config: mockConfig({ - private: false - }) - }) - - const m1 = await create(0) - - await libp2p.dial(m1.libp2p.getMultiaddrs()) - - const [peer] = m1.libp2p.getPeers() - - await m1.libp2p.peerStore.save(peer, peer) - - await sigints[0].interupt() - - const m2 = await create(1) - - const peers = await m2.libp2p.peerStore.all() - - assert.deepEqual(peers[0].id.toBytes(), peer.toBytes()) - - await sigints[1].interupt() - await libp2p.stop() - }) - - it('rpc - addresses returns the peers addresses', async () => { - const m = await network(components) - const client = createNetClient(components.argv.socket) - const addresses = await client.rpc.request('addresses', {}) - - assert.deepEqual(addresses, m.libp2p.getMultiaddrs().map(a => a.toString())) - - client.close() - }) - - it('rpc - connections returns the peers connections', async () => { - const libp2p = await createLibp2p({}) - const m = await network(components) - const client = createNetClient(components.argv.socket) - - assert.deepEqual(await client.rpc.request('connections', {}), []) - - await libp2p.dial(m.libp2p.getMultiaddrs()) - - const connections = await client.rpc.request('connections', {}) - - assert.equal(connections.length, 1) - - assert.deepEqual( - connections, - m.libp2p.getConnections().map(a => a.remoteAddr.toString()) - ) - - await libp2p.stop() - client.close() - }) - - it('rpc - connection connects to another peer', async () => { - const libp2p = await createLibp2p({}) - const m = await network(components) - const client = createNetClient(components.argv.socket) - - await client.rpc.request('connect', { address: libp2p.getMultiaddrs()[0] }) - - const connections = m.libp2p.getConnections() - - assert.equal(connections.length, 1) - assert.deepEqual(connections[0].remotePeer.toBytes(), libp2p.peerId.toBytes()) - - await libp2p.stop() - client.close() - }) - - // This should pass but sometimes github workflows can be a bit flakey in terms of peer discovery. - it.skip('rpc - get peers returns a peer hosting content', async () => { - const data = new Uint8Array([0, 1, 2, 3]) - const libp2p = await createLibp2p({}) - const helia = await createHelia({ libp2p }) - const ufs = unixfs(helia) - const m = await network(components) - const client = createNetClient(components.argv.socket) - - await m.libp2p.dial(helia.libp2p.getMultiaddrs()) - - const cid = await ufs.addBytes(data) - await all(helia.pins.add(cid)) - - const result = await client.rpc.request('count-peers', { cids: [cid.toString()] }) - - assert.deepEqual(result, [{ cid: cid.toString(), peers: 1 }]) - - await helia.stop() - await libp2p.stop() - client.close() - }) -}) diff --git a/packages/daemon/test/modules/revisions.spec.ts b/packages/daemon/test/modules/revisions.spec.ts deleted file mode 100644 index 3392b4bb..00000000 --- a/packages/daemon/test/modules/revisions.spec.ts +++ /dev/null @@ -1,460 +0,0 @@ -import assert from 'assert' -import fs from 'fs/promises' -import Path from 'path' -import { unixfs } from '@helia/unixfs' -import { KeyManager } from '@organicdesign/db-key-manager' -import * as testData from '@organicdesign/db-test-utils' -import { importer } from '@organicdesign/db-utils' -import { createNetClient } from '@organicdesign/net-rpc' -import all from 'it-all' -import { CID } from 'multiformats/cid' -import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string' -import { toString as uint8ArrayToString } from 'uint8arrays/to-string' -import createDownloader from '../../src/modules/downloader/index.js' -import createFilesystem from '../../src/modules/filesystem/index.js' -import createGroups from '../../src/modules/groups/index.js' -import createNetwork from '../../src/modules/network/index.js' -import createRevisions from '../../src/modules/revisions/index.js' -import createRpc from '../../src/modules/rpc/index.js' -import createSigint from '../../src/modules/sigint/index.js' -import createTick from '../../src/modules/tick/index.js' -import { createGroup } from '../utils/create-group.js' -import { createDag } from '../utils/dag.js' -import { generateKey } from '../utils/generate-key.js' -import { mkTestPath } from '../utils/paths.js' -import mockArgv from './mock-argv.js' -import mockBase from './mock-base.js' -import mockConfig from './mock-config.js' - -describe('revisions', () => { - const testPath = mkTestPath('revisions') - - const create = async (name?: string): Promise<{ - argv: ReturnType - config: ReturnType - rpc: Awaited> - base: ReturnType - network: Awaited> - groups: Awaited> - filesystem: Awaited> - sigint: Awaited> - tick: Awaited> - revisions: Awaited> - }> => { - const path = name == null ? testPath : Path.join(testPath, name) - - const keyManager = new KeyManager(await generateKey()) - - await fs.mkdir(path, { recursive: true }) - - const argv = mockArgv(path) - const config = mockConfig({ storage: ':memory:' }) - const sigint = await createSigint() - const rpc = await createRpc({ argv, sigint }) - const base = mockBase({ keyManager }) - const network = await createNetwork({ config, sigint, base, rpc }) - - const groups = await createGroups({ - sigint, - base, - rpc, - network - }) - - const downloader = await createDownloader({ - sigint, - base, - rpc, - network, - config - }) - - const tick = await createTick({ config, sigint }) - - const filesystem = await createFilesystem({ - sigint, - base, - rpc, - network, - groups, - downloader, - tick, - config - }) - - const revisions = await createRevisions({ - base, - network, - rpc, - groups, - filesystem, - config, - downloader, - tick - }) - - return { - argv, - config, - sigint, - rpc, - base, - network, - groups, - filesystem, - tick, - revisions - } - } - - before(async () => { - await fs.mkdir(testPath, { recursive: true }) - }) - - after(async () => { - await fs.rm(testPath, { recursive: true }) - }) - - it('returns null when wraping a group that doesn\'t exist', async () => { - const { revisions: m, sigint } = await create() - const r = m.getRevisions(CID.parse('QmNnooDu7bfjPFoTZYxMNLWUQJyrVwtbZg5gBMjTezGAJN')) - - assert.equal(r, null) - - await sigint.interupt() - }) - - it('wraps a group in revisions', async () => { - const { revisions: m, groups, sigint } = await create() - const group = await createGroup(groups, 'test') - const r = m.getRevisions(group) - - assert.notEqual(r, null) - - await sigint.interupt() - }) - - it('creates a revision when a file is added to the filesystem', async () => { - const { revisions: m, filesystem, network, groups, sigint } = await create() - const group = await createGroup(groups, 'test') - const r = m.getRevisions(group) - const fs = filesystem.getFileSystem(group) - const dag = await createDag(network.helia, 2, 2) - const path = '/test' - - assert(r != null) - assert(fs != null) - - const promise = new Promise((resolve, reject) => { - setTimeout(() => { reject(new Error('timeout')) }, 100) - - filesystem.events.addEventListener('file:added', () => { resolve() }, { once: true }) - }) - - await filesystem.uploads.add('put', [group.bytes, path, { - cid: dag[0].bytes, - encrypted: false, - revisionStrategy: 'all' as const, - priority: 1 - }]) - - const entry = await fs.get(path) - - assert(entry != null) - - await promise - - await new Promise(resolve => setTimeout(resolve, 100)) - - const entries = await all(r.getAll(path)) - - assert.deepEqual(entries, [{ - path, - sequence: 0, - author: groups.welo.identity.id, - - entry: { - cid: entry.cid, - encrypted: entry.encrypted, - timestamp: entry.timestamp, - blocks: entry.blocks, - size: entry.size, - priority: entry.priority - } - }]) - - await sigint.interupt() - }) - - it('rpc - it exports a revision (file)', async () => { - const { network, filesystem, groups, sigint, argv } = await create() - const group = await createGroup(groups, 'test') - const fs = filesystem.getFileSystem(group) - const path = '/test' - const client = createNetClient(argv.socket) - const sequence = 0 - const dataFile = testData.data[0] - const exportPath = dataFile.generatePath(testPath) - - assert(fs != null) - - const [{ cid }] = await all(importer(network.helia.blockstore, dataFile.path)) - - const promise = new Promise((resolve, reject) => { - setTimeout(() => { reject(new Error('timeout')) }, 100) - - filesystem.events.addEventListener('file:added', () => { resolve() }, { once: true }) - }) - - await filesystem.uploads.add('put', [group.bytes, path, { - cid: cid.bytes, - encrypted: false, - revisionStrategy: 'all' as const, - priority: 1 - }]) - - await promise - - await new Promise(resolve => setTimeout(resolve, 100)) - - const response = await client.rpc.request('export-revision', { - group: group.toString(), - path, - author: uint8ArrayToString(groups.welo.identity.id, 'base58btc'), - sequence, - outPath: exportPath - }) - - assert.equal(response, null) - - const valid = await dataFile.validate(exportPath) - - assert.equal(valid, true) - - await sigint.interupt() - }) - - it('rpc - it exports a revision (directory)', async () => { - const { network, filesystem, groups, sigint, argv } = await create() - const group = await createGroup(groups, 'test') - const fs = filesystem.getFileSystem(group) - const rootPath = '/test' - const client = createNetClient(argv.socket) - const sequence = 0 - const outPath = Path.join(testPath, 'export-directory') - - assert(fs != null) - - for (const dataFile of testData.data) { - const virtualPath = dataFile.generatePath(rootPath) - - const [{ cid }] = await all(importer(network.helia.blockstore, dataFile.path)) - - const promise = new Promise((resolve, reject) => { - setTimeout(() => { reject(new Error('timeout')) }, 100) - - filesystem.events.addEventListener('file:added', () => { resolve() }, { once: true }) - }) - - await filesystem.uploads.add('put', [group.bytes, virtualPath, { - cid: cid.bytes, - encrypted: false, - revisionStrategy: 'all' as const, - priority: 1 - }]) - - await promise - } - - await new Promise(resolve => setTimeout(resolve, 100)) - - const response = await client.rpc.request('export-revision', { - group: group.toString(), - path: rootPath, - author: uint8ArrayToString(groups.welo.identity.id, 'base58btc'), - sequence, - outPath - }) - - assert.equal(response, null) - - for (const dataFile of testData.data) { - const exportPath = dataFile.generatePath(outPath) - const valid = await dataFile.validate(exportPath) - - assert.equal(valid, true) - } - - await sigint.interupt() - }) - - it('rpc - lists a revision (file)', async () => { - const { network, filesystem, groups, sigint, argv } = await create() - const group = await createGroup(groups, 'test') - const fs = filesystem.getFileSystem(group) - const path = '/test' - const client = createNetClient(argv.socket) - const dataFile = testData.data[0] - - assert(fs != null) - - const [{ cid }] = await all(importer(network.helia.blockstore, dataFile.path)) - - const promise = new Promise((resolve, reject) => { - setTimeout(() => { reject(new Error('timeout')) }, 100) - - filesystem.events.addEventListener('file:added', () => { resolve() }, { once: true }) - }) - - const before = Date.now() - - await filesystem.uploads.add('put', [group.bytes, path, { - cid: cid.bytes, - encrypted: false, - revisionStrategy: 'all' as const, - priority: 1 - }]) - - await promise - - await new Promise(resolve => setTimeout(resolve, 100)) - - const response = await client.rpc.request('list', { - group: group.toString(), - path - }) - - assert(Array.isArray(response)) - assert.equal(response.length, 1) - assert.equal(response[0].author, uint8ArrayToString(groups.welo.identity.id, 'base58btc')) - assert.equal(response[0].blocks, 1) - assert.equal(response[0].cid, cid.toString()) - assert.equal(response[0].encrypted, false) - assert.equal(response[0].path, path) - assert.equal(response[0].priority, 1) - assert.equal(response[0].revisionStrategy, 'all') - assert.equal(response[0].size, 447) - assert(response[0].timestamp >= before) - assert(response[0].timestamp <= Date.now()) - - await sigint.interupt() - }) - - it('rpc - it lists a revision (directory)', async () => { - const { network, filesystem, groups, sigint, argv } = await create() - const group = await createGroup(groups, 'test') - const fs = filesystem.getFileSystem(group) - const rootPath = '/test' - const client = createNetClient(argv.socket) - - assert(fs != null) - - const before = Date.now() - - for (const dataFile of testData.data) { - const virtualPath = dataFile.generatePath(rootPath) - - const [{ cid }] = await all(importer(network.helia.blockstore, dataFile.path)) - - const promise = new Promise((resolve, reject) => { - setTimeout(() => { reject(new Error('timeout')) }, 100) - - filesystem.events.addEventListener('file:added', () => { resolve() }, { once: true }) - }) - - await filesystem.uploads.add('put', [group.bytes, virtualPath, { - cid: cid.bytes, - encrypted: false, - revisionStrategy: 'all' as const, - priority: 1 - }]) - - await promise - } - - await new Promise(resolve => setTimeout(resolve, 100)) - - const response = await client.rpc.request('list', { - group: group.toString(), - path: rootPath - }) - - assert(Array.isArray(response)) - assert.equal(response.length, 3) - - for (const item of response) { - assert.equal(item.author, uint8ArrayToString(groups.welo.identity.id, 'base58btc')) - assert.equal(item.blocks, 1) - assert.equal(item.encrypted, false) - assert.equal(item.priority, 1) - assert.equal(item.revisionStrategy, 'all') - assert(item.timestamp >= before) - assert(item.timestamp <= Date.now()) - } - - for (const dataFile of testData.data) { - const virtualPath = dataFile.generatePath(rootPath) - const item = response.find(d => d.path === virtualPath) - - assert(item != null) - assert(item.cid === dataFile.cid.toString()) - assert(BigInt(item.size) === dataFile.size) - } - - await sigint.interupt() - }) - - it('rpc - read revision', async () => { - const { filesystem, groups, network, sigint, argv } = await create() - const client = createNetClient(argv.socket) - const group = await createGroup(groups, 'test') - const ufs = unixfs(network.helia) - const path = '/test' - const data = 'test-data' - - const cid = await ufs.addBytes(uint8ArrayFromString(data)) - - const promise = new Promise((resolve, reject) => { - setTimeout(() => { reject(new Error('timeout')) }, 100) - - filesystem.events.addEventListener('file:added', () => { resolve() }, { once: true }) - }) - - await filesystem.uploads.add('put', [group.bytes, path, { - cid: cid.bytes, - encrypted: false, - revisionStrategy: 'all' as const, - priority: 1 - }]) - - await promise - - await new Promise(resolve => setTimeout(resolve, 100)) - - const coreParams = { - group: group.toString(), - path, - sequence: 0, - author: uint8ArrayToString(groups.welo.identity.id, 'base58btc') - } - - const read1 = await client.rpc.request('read', coreParams) - - assert.deepEqual(read1, data) - - const read2 = await client.rpc.request('read', { ...coreParams, position: 1 }) - - assert.deepEqual(read2, data.slice(1)) - - const read3 = await client.rpc.request('read', { ...coreParams, length: 3 }) - - assert.deepEqual(read3, data.slice(0, 3)) - - const read4 = await client.rpc.request('read', { ...coreParams, position: 1, length: 3 }) - - assert.deepEqual(read4, data.slice(1, 3 + 1)) - - client.close() - await sigint.interupt() - }) -}) diff --git a/packages/daemon/test/modules/rpc.spec.ts b/packages/daemon/test/modules/rpc.spec.ts deleted file mode 100644 index da8324cc..00000000 --- a/packages/daemon/test/modules/rpc.spec.ts +++ /dev/null @@ -1,61 +0,0 @@ -import assert from 'assert/strict' -import fs from 'fs/promises' -import { createNetClient } from '@organicdesign/net-rpc' -import rpc from '../../src/modules/rpc/index.js' -import createSigint from '../../src/modules/sigint/index.js' -import { mkTestPath } from '../utils/paths.js' -import mockArgv from './mock-argv.js' -import type { Provides as Argv } from '../../src/modules/argv/index.js' -import type { Provides as Sigint } from '../../src/modules/sigint/index.js' - -const testPath = mkTestPath('rpc') - -describe('rpc', () => { - let argv: Argv - let sigint: Sigint - - before(async () => { - argv = mockArgv(testPath) - sigint = await createSigint() - - await fs.mkdir(testPath, { recursive: true }) - - await fs.writeFile(argv.key, JSON.stringify({ - key: '5TP9VimJU1WdSoTxZGLhSuPKqCpXirPHDK4ZjHxzetex-8zAV14C4oLe4dytUSVzznTuQ659pY1dSMG8HAQenDqVQ', - psk: '/key/swarm/psk/1.0.0/\n/base16/\n56d3c18282f1f1b1b3e04e40dd5d8bf44cafa8bc9c9bc7c57716a7766fa2c550' - })) - }) - - after(async () => { - await fs.rm(testPath, { recursive: true }) - await sigint.interupt() - }) - - it('adds RPC methods', async () => { - const testData = { key: 'value' } - const returnData = { return: 'return-value' } - - const m = await rpc({ - argv, - sigint - }) - - const client = createNetClient(argv.socket) - - const methodPromise = new Promise((resolve, reject) => { - setTimeout(() => { reject(new Error()) }, 50) - - m.addMethod('test', async params => { - resolve(params) - return returnData - }) - }) - - const returnResult = await client.rpc.request('test', testData) - - assert.deepEqual(returnResult, returnData) - assert.deepEqual(await methodPromise, testData) - - client.close() - }) -}) diff --git a/packages/daemon/test/modules/sigint.spec.ts b/packages/daemon/test/modules/sigint.spec.ts deleted file mode 100644 index 2aa2c68a..00000000 --- a/packages/daemon/test/modules/sigint.spec.ts +++ /dev/null @@ -1,15 +0,0 @@ -import assert from 'assert/strict' -import sigint from '../../src/modules/sigint/index.js' - -describe('sigint', () => { - it('calls the interupt method when interupt gets called', async () => { - const m = await sigint() - - let called = 0 - - m.onInterupt(() => called++) - await m.interupt() - - assert.equal(called, 1) - }) -}) diff --git a/packages/daemon/test/modules/tick.spec.ts b/packages/daemon/test/modules/tick.spec.ts deleted file mode 100644 index bf22fe15..00000000 --- a/packages/daemon/test/modules/tick.spec.ts +++ /dev/null @@ -1,69 +0,0 @@ -import assert from 'assert/strict' -import { type z } from 'zod' -import setupSigint from '../../src/modules/sigint/index.js' -import tick from '../../src/modules/tick/index.js' -import mockConfig from './mock-config.js' - -describe('tick', () => { - it('returns default tick interval', async () => { - const sigint = await setupSigint() - - const m = await tick({ - config: { config: {}, get: (schema: z.AnyZodObject) => schema.parse({}) }, - sigint - }) - - assert.deepEqual(m.config, { tickInterval: 600 }) - - await sigint.interupt() - }) - - it('returns config tick interval', async () => { - const tickInterval = 100 - const sigint = await setupSigint() - - const m = await tick({ - config: mockConfig({ tickInterval }), - sigint - }) - - assert.deepEqual(m.config, { tickInterval }) - - await sigint.interupt() - }) - - it('returns ticks every interval', async () => { - const tickInterval = 5 - const checkTimes = 6 - const sigint = await setupSigint() - - const m = await tick({ - config: mockConfig({ tickInterval }), - sigint - }) - - const before = Date.now() - - await new Promise((resolve, reject) => { - setTimeout(() => { reject(new Error('timeout')) }, (checkTimes + 4) * tickInterval) - let timesCalled = 0 - - m.register(() => { - timesCalled++ - - if (timesCalled >= checkTimes) { - resolve() - } - }) - }) - - const after = Date.now() - - const delta = after - before - - assert(delta < tickInterval * (checkTimes + 2)) - assert(delta > tickInterval * (checkTimes - 2)) - - await sigint.interupt() - }) -}) diff --git a/packages/daemon/test/utils.spec.ts b/packages/daemon/test/utils.spec.ts deleted file mode 100644 index 40ec3425..00000000 --- a/packages/daemon/test/utils.spec.ts +++ /dev/null @@ -1,114 +0,0 @@ -import assert from 'assert/strict' -import { MemoryBlockstore } from 'blockstore-core' -import { type CID } from 'multiformats/cid' -import { MEMORY_MAGIC } from '../src/interface.js' -import { isMemory, encodeAny, decodeAny } from '../src/utils.js' -import { createDag } from './utils/dag.js' -import { walkDag, getDagSize } from '@/modules/filesystem/utils.js' - -describe('isMemory', () => { - it('returns true if the memory magic is passed', () => { - assert(isMemory(MEMORY_MAGIC)) - }) - - it('returns false for any other value than the memory magic', () => { - const values = [ - '/my/path', - 'my-directory', - ':memory', - 'memory', - 'memory:', - ':memory:/my-dir' - ] - - for (const value of values) { - assert(!isMemory(value)) - } - }) -}) - -describe('cbor encoding and decoding', () => { - const data = [ - { - decoded: new Uint8Array([0, 1, 2, 3]), - encoded: new Uint8Array([68, 0, 1, 2, 3]) - }, - - { - decoded: 'str', - encoded: new Uint8Array([99, 115, 116, 114]) - }, - - { - decoded: 9999, - encoded: new Uint8Array([25, 39, 15]) - }, - - { - decoded: [{ test: 'value' }], - encoded: new Uint8Array([129, 161, 100, 116, 101, 115, 116, 101, 118, 97, 108, 117, 101]) - } - ] - - it('encodes any data', () => { - for (const { encoded, decoded } of data) { - assert.deepEqual(encoded, encodeAny(decoded)) - } - }) - - it('decodes any data', () => { - for (const { encoded, decoded } of data) { - assert.deepEqual(decodeAny(encoded), decoded) - } - }) -}) - -describe('walkDag', () => { - let dag: CID[] - let blockstore: MemoryBlockstore - - before(async () => { - blockstore = new MemoryBlockstore() - - dag = await createDag({ blockstore }, 3, 3) - }) - - it('walks over every value of the dag', async () => { - let count = 0 - - for await (const getData of walkDag(blockstore, dag[0])) { - const data = await getData() - - assert(dag.find(cid => cid.equals(data.cid))) - - count++ - } - - assert.equal(count, dag.length) - }) -}) - -describe('getDagSize', () => { - let dag: CID[] - let blockstore: MemoryBlockstore - - before(async () => { - blockstore = new MemoryBlockstore() - - dag = await createDag({ blockstore }, 3, 3) - }) - - it('returns the correct block count for the dag', async () => { - const { blocks } = await getDagSize(blockstore, dag[0]) - - assert.equal(blocks, dag.length) - }) - - it('returns the correct size for the dag', async () => { - const blocks = await Promise.all(dag.map(async cid => blockstore.get(cid))) - const totalSize = blocks.reduce((p, c) => p + c.length, 0) - const { size } = await getDagSize(blockstore, dag[0]) - - assert.equal(size, totalSize) - }) -}) diff --git a/packages/daemon/test/utils/blocks.ts b/packages/daemon/test/utils/blocks.ts deleted file mode 100644 index ae97a7fe..00000000 --- a/packages/daemon/test/utils/blocks.ts +++ /dev/null @@ -1,46 +0,0 @@ -import { CID } from 'multiformats/cid' -import * as raw from 'multiformats/codecs/raw' -import { sha256 } from 'multiformats/hashes/sha2' -import type { Helia } from '@helia/interface' -import type { Blockstore } from 'interface-blockstore' - -export const hashBlock = async (block: Uint8Array, codec?: number): Promise => { - const hash = await sha256.digest(block) - - return CID.createV1(codec ?? raw.code, hash) -} - -export const addBlock = async ({ blockstore }: { blockstore: Blockstore }, block: Uint8Array, codec?: number): Promise => { - const cid = await hashBlock(block, codec) - - await blockstore.put(cid, block) - - return cid -} - -export const createBlocks = async (): Promise> => { - const blocks: Array<{ block: Uint8Array, cid: CID }> = [] - - for (let i = 0; i < 100; i++) { - const block = new Uint8Array([i, i, i]) - const cid = await hashBlock(block) - - blocks.push({ block, cid }) - } - - return blocks -} - -export const addBlocks = async ({ blockstore }: { blockstore: Blockstore }): Promise> => { - const blocks = await createBlocks() - - await Promise.all(blocks.map(async b => addBlock({ blockstore }, b.block))) - - return blocks -} - -export const pinBlocks = async (helia: Helia): Promise => { - const blocks = await addBlocks(helia) - - await Promise.all(blocks.map(({ cid }) => helia.pins.add(cid))) -} diff --git a/packages/daemon/test/utils/create-group.ts b/packages/daemon/test/utils/create-group.ts deleted file mode 100644 index b708735d..00000000 --- a/packages/daemon/test/utils/create-group.ts +++ /dev/null @@ -1,17 +0,0 @@ -import { type CID } from 'multiformats/cid' -import type { Provides as GroupsProvides } from '../../src/modules/groups/index.js' - -export const createGroup = async (m: GroupsProvides, name: string, peers: Uint8Array[] = []): Promise => { - const manifest = await m.welo.determine({ - name, - meta: { type: 'group' }, - access: { - protocol: '/hldb/access/static', - config: { write: [m.welo.identity.id, ...peers] } - } - }) - - await m.groups.add(manifest) - - return manifest.address.cid -} diff --git a/packages/daemon/test/utils/dag.ts b/packages/daemon/test/utils/dag.ts deleted file mode 100644 index a45f1616..00000000 --- a/packages/daemon/test/utils/dag.ts +++ /dev/null @@ -1,44 +0,0 @@ -import * as dagPb from '@ipld/dag-pb' -import { compare as compareUint8Arrays } from 'uint8arrays/compare' -import { fromString as uint8arrayFromString } from 'uint8arrays/from-string' -import { addBlock } from './blocks.js' -import type { Blockstore } from 'interface-blockstore' -import type { CID } from 'multiformats/cid' - -const uniqueNumber = (() => { - let i = 0 - - return () => i++ -})() - -export const createDag = async ({ blockstore }: { blockstore: Blockstore }, depth: number, children: number): Promise => { - if (depth === 0) { - const block = dagPb.encode({ Data: uint8arrayFromString(`level-${depth}-${uniqueNumber()}`), Links: [] }) - const cid = await addBlock({ blockstore }, block, dagPb.code) - - return [cid] - } - - const childrenNodes: CID[][] = [] - - for (let i = 0; i < children; i++) { - childrenNodes.push(await createDag({ blockstore }, depth - 1, children)) - } - - childrenNodes.sort((a, b) => compareUint8Arrays( - uint8arrayFromString(a[0].toString()), - uint8arrayFromString(b[0].toString())) - ) - - const block = dagPb.encode({ - Data: uint8arrayFromString(`level-${depth}-${uniqueNumber()}`), - Links: childrenNodes.map(l => ({ Hash: l[0], Name: l[0].toString() })) - }) - - const cid = await addBlock({ blockstore }, block, dagPb.code) - const allBlocks = childrenNodes.reduce((a, b) => [...a, ...b], []) - - allBlocks.unshift(cid) - - return allBlocks -} diff --git a/packages/daemon/test/utils/generate-key.ts b/packages/daemon/test/utils/generate-key.ts deleted file mode 100644 index 30544ebd..00000000 --- a/packages/daemon/test/utils/generate-key.ts +++ /dev/null @@ -1,14 +0,0 @@ -import { - type KeyData, - parseKeyData, - generateKeyData, - generateMnemonic -} from '@organicdesign/db-key-manager' - -export const generateKey = async (): Promise => { - const mnemonic = generateMnemonic() - const name = generateMnemonic().split(' ')[0] - const rawKeyData = await generateKeyData(mnemonic, name) - - return parseKeyData(rawKeyData) -} diff --git a/packages/daemon/test/utils/paths.ts b/packages/daemon/test/utils/paths.ts deleted file mode 100644 index 9adabb0e..00000000 --- a/packages/daemon/test/utils/paths.ts +++ /dev/null @@ -1,6 +0,0 @@ -import Path from 'path' -import { projectPath } from '@/utils.js' - -export const testPath = Path.join(projectPath, 'packages/daemon/test-out/') - -export const mkTestPath = (name: string): string => Path.join(testPath, name) diff --git a/packages/daemon/tsconfig.json b/packages/daemon/tsconfig.json index a94b36f8..f3691caf 100644 --- a/packages/daemon/tsconfig.json +++ b/packages/daemon/tsconfig.json @@ -13,7 +13,8 @@ } }, "include": [ - "src" + "src", + "test" ], "references": [ {