Skip to content

Commit

Permalink
Refactor test utils.
Browse files Browse the repository at this point in the history
  • Loading branch information
saul-jb committed Apr 1, 2024
1 parent 7b32546 commit 9b89bc4
Show file tree
Hide file tree
Showing 19 changed files with 82 additions and 68 deletions.
15 changes: 10 additions & 5 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 10 additions & 10 deletions packages/daemon/test/modules/filesystem.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import assert from 'assert/strict'
import fs from 'fs/promises'
import Path from 'path'
import { unixfs } from '@helia/unixfs'
import * as testData from '@organicdesign/db-test-utils'
import { createDag } from '@organicdesign/db-test-utils'
import * as testData from '@organicdesign/db-test-utils/data'
import { importer } from '@organicdesign/db-utils'
import { createNetClient } from '@organicdesign/net-rpc'
import { MemoryBlockstore } from 'blockstore-core'
Expand Down Expand Up @@ -250,7 +250,7 @@ describe('filesystem', () => {

assert(fs != null)

await Promise.all(testData.data.map(async data => {
await Promise.all(testData.files.map(async data => {
const result = await all(importer(components.helia.blockstore, data.path))

await filesystem.uploads.add('put', [group.bytes, data.generatePath(rootPath), {
Expand All @@ -261,7 +261,7 @@ describe('filesystem', () => {
}])
}))

for (const data of testData.data) {
for (const data of testData.files) {
const exportPath = data.generatePath(outPath)

const response = await client.rpc.request('export', {
Expand Down Expand Up @@ -290,7 +290,7 @@ describe('filesystem', () => {

assert(fs != null)

await Promise.all(testData.data.map(async data => {
await Promise.all(testData.files.map(async data => {
const result = await all(importer(components.helia.blockstore, data.path))

await filesystem.uploads.add('put', [group.bytes, data.generatePath(rootPath), {
Expand All @@ -309,7 +309,7 @@ describe('filesystem', () => {

assert.equal(response, null)

for (const data of testData.data) {
for (const data of testData.files) {
const exportPath = data.generatePath(outPath)
const valid = await data.validate(exportPath)

Expand All @@ -329,7 +329,7 @@ describe('filesystem', () => {

assert(fs != null)

await Promise.all(testData.data.map(async data => {
await Promise.all(testData.files.map(async data => {
const virtualPath = data.generatePath(rootPath)

const response = await client.rpc.request('import', {
Expand Down Expand Up @@ -370,10 +370,10 @@ describe('filesystem', () => {
})

assert(Array.isArray(response))
assert.equal(response.length, testData.data.length)
assert.equal(response.length, testData.files.length)

for (const data of response) {
const dataFile = testData.getDataFile(data.inPath)
const dataFile = testData.getFile(data.inPath)

assert(dataFile != null)
assert.equal(data.path, dataFile.generatePath(rootPath))
Expand All @@ -383,9 +383,9 @@ describe('filesystem', () => {
const fsResult = await all(fs.getDir(rootPath))

assert(Array.isArray(fsResult))
assert.equal(fsResult.length, testData.data.length)
assert.equal(fsResult.length, testData.files.length)

for (const dataFile of testData.data) {
for (const dataFile of testData.files) {
const virtualPath = dataFile.generatePath(rootPath)
const r = fsResult.find(r => r.key === virtualPath)

Expand Down
14 changes: 7 additions & 7 deletions packages/daemon/test/modules/revisions.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import assert from 'assert'
import fs from 'fs/promises'
import Path from 'path'
import { unixfs } from '@helia/unixfs'
import * as testData from '@organicdesign/db-test-utils'
import { createDag } from '@organicdesign/db-test-utils'
import * as testData from '@organicdesign/db-test-utils/data'
import { importer } from '@organicdesign/db-utils'
import { createNetClient } from '@organicdesign/net-rpc'
import all from 'it-all'
Expand Down Expand Up @@ -122,7 +122,7 @@ describe('revisions', () => {
const path = '/test'
const client = createNetClient(socket)
const sequence = 0
const dataFile = testData.data[0]
const dataFile = testData.files[0]
const exportPath = dataFile.generatePath(testPath)

assert(fs != null)
Expand Down Expand Up @@ -174,7 +174,7 @@ describe('revisions', () => {

assert(fs != null)

for (const dataFile of testData.data) {
for (const dataFile of testData.files) {
const virtualPath = dataFile.generatePath(rootPath)

const [{ cid }] = await all(importer(components.helia.blockstore, dataFile.path))
Expand Down Expand Up @@ -207,7 +207,7 @@ describe('revisions', () => {

assert.equal(response, null)

for (const dataFile of testData.data) {
for (const dataFile of testData.files) {
const exportPath = dataFile.generatePath(outPath)
const valid = await dataFile.validate(exportPath)

Expand All @@ -223,7 +223,7 @@ describe('revisions', () => {
const fs = filesystem.getFileSystem(group)
const path = '/test'
const client = createNetClient(socket)
const dataFile = testData.data[0]
const dataFile = testData.files[0]

assert(fs != null)

Expand Down Expand Up @@ -280,7 +280,7 @@ describe('revisions', () => {

const before = Date.now()

for (const dataFile of testData.data) {
for (const dataFile of testData.files) {
const virtualPath = dataFile.generatePath(rootPath)

const [{ cid }] = await all(importer(components.helia.blockstore, dataFile.path))
Expand Down Expand Up @@ -321,7 +321,7 @@ describe('revisions', () => {
assert(item.timestamp <= Date.now())
}

for (const dataFile of testData.data) {
for (const dataFile of testData.files) {
const virtualPath = dataFile.generatePath(rootPath)
const item = response.find(d => d.path === virtualPath)

Expand Down
12 changes: 6 additions & 6 deletions packages/e2e-tests/test/import-export.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import assert from 'assert/strict'
import fs from 'fs/promises'
import Path from 'path'
import * as testData from '@organicdesign/db-test-utils'
import * as testData from '@organicdesign/db-test-utils/data'
import { testPath } from './utils/paths.js'
import runClient from './utils/run-client.js'
import runNode from './utils/run-node.js'
Expand Down Expand Up @@ -34,7 +34,7 @@ describe('import/export', () => {
})

it('imports a file', async () => {
for (const data of testData.data) {
for (const data of testData.files) {
const virtual = data.generatePath('/test-import-file')
const response = await runClient(node, 'import', group, data.path, virtual)

Expand All @@ -57,10 +57,10 @@ describe('import/export', () => {

assert.equal(response.success, true)
assert(Array.isArray(response.imports))
assert.equal(response.imports.length, testData.data.length)
assert.equal(response.imports.length, testData.files.length)

for (const file of response.imports) {
const dataFile = testData.getDataFile(file.inPath)
const dataFile = testData.getFile(file.inPath)

assert(dataFile != null)
assert.equal(file.cid, dataFile.cid.toString())
Expand All @@ -70,7 +70,7 @@ describe('import/export', () => {
})

it('exports a file', async () => {
for (const data of testData.data) {
for (const data of testData.files) {
const virtual = data.generatePath('/test-import-file')
const exportPath = data.generatePath(outPath)
const response = await runClient(node, 'export', group, virtual, exportPath)
Expand All @@ -91,7 +91,7 @@ describe('import/export', () => {
success: true
})

for (const data of testData.data) {
for (const data of testData.files) {
const exportDir = data.generatePath(outPath)
const valid = await data.validate(exportDir)

Expand Down
4 changes: 2 additions & 2 deletions packages/e2e-tests/test/revisions.spec.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import assert from 'assert/strict'
import * as testData from '@organicdesign/db-test-utils'
import * as testData from '@organicdesign/db-test-utils/data'
import runClient from './utils/run-client.js'
import runNode from './utils/run-node.js'

const node = 'revisions'
const virtualDir = '/data'
const dataFile = testData.data[0]
const dataFile = testData.files[0]

describe('revisions', () => {
let proc: Awaited<ReturnType<typeof runNode>>
Expand Down
6 changes: 3 additions & 3 deletions packages/e2e-tests/test/synchronization.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import assert from 'assert/strict'
import * as testData from '@organicdesign/db-test-utils'
import * as testData from '@organicdesign/db-test-utils/data'
import runClient from './utils/run-client.js'
import runNode from './utils/run-node.js'

Expand Down Expand Up @@ -39,7 +39,7 @@ describe('synchronization', () => {
nodes[0],
'import',
group,
testData.data[0].path,
testData.files[0].path,
virtualPath
)

Expand Down Expand Up @@ -74,7 +74,7 @@ describe('synchronization', () => {
nodes[0],
'import',
group,
testData.data[1].path,
testData.files[1].path,
virtualPath
)

Expand Down
2 changes: 1 addition & 1 deletion packages/e2e-tests/test/utils/run-fuse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export default async (name: string, path: string, group: string): Promise<{ star
const promise = new DeferredPromise<void>()

const listener = (chunk: Uint8Array): void => {
if (uint8ArrayToString(chunk).includes('INIT') === true) {
if (uint8ArrayToString(chunk).includes('INIT')) {
promise.resolve()
}
}
Expand Down
4 changes: 2 additions & 2 deletions packages/e2e-tests/test/utils/run-node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export default async (name: string): Promise<{ start(): Promise<void>, stop(): P
const promise = new DeferredPromise<void>()

const listener = (chunk: Uint8Array): void => {
if (uint8ArrayToString(chunk).includes('started') === true) {
if (uint8ArrayToString(chunk).includes('started')) {
promise.resolve()
}
}
Expand All @@ -56,7 +56,7 @@ export default async (name: string): Promise<{ start(): Promise<void>, stop(): P
const promise = new DeferredPromise<void>()

const listener = (chunk: Uint8Array): void => {
if (uint8ArrayToString(chunk).includes('exiting...') === true) {
if (uint8ArrayToString(chunk).includes('exiting...')) {
promise.resolve()
}
}
Expand Down
3 changes: 2 additions & 1 deletion packages/e2e-tests/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
"esModuleInterop": true,
"outDir": "dist",
"baseUrl": ".",
"module": "ES2022",
"module": "nodenext",
"moduleResolution": "nodenext",
"target": "ES2022"
},
"include": [
Expand Down
4 changes: 4 additions & 0 deletions packages/test-utils/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@
".": {
"types": "./dist/src/index.d.ts",
"import": "./dist/src/index.js"
},
"./data": {
"types": "./dist/src/data/index.d.ts",
"import": "./dist/src/data/index.js"
}
},
"scripts": {
Expand Down
11 changes: 11 additions & 0 deletions packages/test-utils/src/data/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { parse } from './parse.js'
import type { DataFile } from './interface.js'

export * from './interface.js'
export { root } from './parse.js'

export const files = await parse()

export const getFile = (path: string): DataFile | null => {
return files.find(f => f.path === path) ?? null
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { CID } from 'multiformats/cid'

export interface TestData {
export interface DataFile {
cid: CID
name: string
path: string
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { fileURLToPath } from 'url'
import { unixfs } from '@helia/unixfs'
import { BlackHoleBlockstore } from 'blockstore-core'
import { compare as uint8ArrayCompareString } from 'uint8arrays/compare'
import type { TestData } from './interface.js'
import type { DataFile } from './interface.js'

const generateHash = async (path: string): Promise<Uint8Array> => {
const hasher = createHash('sha256')
Expand All @@ -15,14 +15,13 @@ const generateHash = async (path: string): Promise<Uint8Array> => {
return hasher.digest()
}

const blockstore = new BlackHoleBlockstore()
const ufs = unixfs({ blockstore })
const ufs = unixfs({ blockstore: new BlackHoleBlockstore() })

export const root = Path.join(Path.dirname(fileURLToPath(import.meta.url)), '../../data')
export const root = Path.join(Path.dirname(fileURLToPath(import.meta.url)), '../../../data')

export default async (): Promise<TestData[]> => {
export const parse = async (): Promise<DataFile[]> => {
const structure = await fs.readdir(root, { recursive: true, withFileTypes: true })
const paths: TestData[] = []
const paths: DataFile[] = []

for (const dirent of structure) {
if (dirent.isDirectory()) {
Expand Down
12 changes: 1 addition & 11 deletions packages/test-utils/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,3 @@
import generateData from './generate-data.js'
import type { TestData } from './interface.js'

export { root } from './generate-data.js'

export const data = await generateData()

export const getDataFile = (path: string): TestData | null => {
return data.find(p => p.path === path) ?? null
}

export * from './data/index.js'
export * from './blocks.js'
export * from './dag.js'
Loading

0 comments on commit 9b89bc4

Please sign in to comment.