Skip to content

Commit

Permalink
Merge branch 'smoke-test-1' of https://github.com/holepunchto/pear in…
Browse files Browse the repository at this point in the history
…to encrypted-test
  • Loading branch information
maidh91 committed Nov 1, 2024
2 parents ca3d599 + d880b50 commit 3cbdf25
Show file tree
Hide file tree
Showing 7 changed files with 135 additions and 34 deletions.
137 changes: 109 additions & 28 deletions test/01-smoke.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,58 +3,134 @@ const test = require('brittle')
const path = require('bare-path')
const hypercoreid = require('hypercore-id-encoding')
const Helper = require('./helper')
const versions = path.join(Helper.localDir, 'test', 'fixtures', 'versions')
const dhtBootstrap = path.join(Helper.localDir, 'test', 'fixtures', 'dht-bootstrap')
const versionsDir = path.join(Helper.localDir, 'test', 'fixtures', 'versions')
const dhtBootstrapDir = path.join(Helper.localDir, 'test', 'fixtures', 'dht-bootstrap')
const requireAssets = path.join(Helper.localDir, 'test', 'fixtures', 'require-assets')
const subDepRequireAssets = path.join(Helper.localDir, 'test', 'fixtures', 'sub-dep-require-assets')

test('smoke', async function ({ ok, is, alike, plan, comment, teardown, timeout }) {
timeout(180000)
plan(10)

const versionsRun = await run({ dir: versions, ok, comment, teardown })
is(JSON.parse(versionsRun.result).app.key, versionsRun.key, 'app version matches staged key')
const testVersions = async () => {
const dir = versionsDir

const dhtBootstrapRun = await run({ dir: dhtBootstrap, ok, comment, teardown })
alike(JSON.parse(dhtBootstrapRun.result), Pear.config.dht.bootstrap, 'dht bootstrap matches Pear.config.dth.bootstrap')
const helper = new Helper()
teardown(() => helper.close(), { order: Infinity })
await helper.ready()

await Helper.untilClose(versionsRun.pipe)
ok(true, 'ended')
const id = Math.floor(Math.random() * 10000)

await Helper.untilClose(dhtBootstrapRun.pipe)
ok(true, 'ended')
comment('staging')
const staging = helper.stage({ channel: `test-${id}`, name: `test-${id}`, dir, dryRun: false, bare: true })
teardown(() => Helper.teardownStream(staging))
const staged = await Helper.pick(staging, { tag: 'final' })
ok(staged.success, 'stage succeeded')

comment('seeding')
const seeding = helper.seed({ channel: `test-${id}`, name: `test-${id}`, dir, key: null, cmdArgs: [] })
teardown(() => Helper.teardownStream(seeding))
const until = await Helper.pick(seeding, [{ tag: 'key' }, { tag: 'announced' }])
const announced = await until.announced
ok(announced, 'seeding is announced')

const key = await until.key
ok(hypercoreid.isValid(key), 'app key is valid')

const link = `pear://${key}`
const run = await Helper.run({ link })

const result = await Helper.untilResult(run.pipe)
const versions = JSON.parse(result)
is(versions.app.key, key, 'app version matches staged key')

await Helper.untilClose(run.pipe)
ok(true, 'ended')
}

const testDhtBootstrap = async () => {
const dir = dhtBootstrapDir

const helper = new Helper()
teardown(() => helper.close(), { order: Infinity })
await helper.ready()

const id = Math.floor(Math.random() * 10000)

comment('staging')
const staging = helper.stage({ channel: `test-${id}`, name: `test-${id}`, dir, dryRun: false, bare: true })
teardown(() => Helper.teardownStream(staging))
const staged = await Helper.pick(staging, { tag: 'final' })
ok(staged.success, 'stage succeeded')

comment('seeding')
const seeding = helper.seed({ channel: `test-${id}`, name: `test-${id}`, dir, key: null, cmdArgs: [] })
teardown(() => Helper.teardownStream(seeding))
const until = await Helper.pick(seeding, [{ tag: 'key' }, { tag: 'announced' }])
const announced = await until.announced
ok(announced, 'seeding is announced')

const key = await until.key
ok(hypercoreid.isValid(key), 'app key is valid')

const link = `pear://${key}`
const run = await Helper.run({ link })

const result = await Helper.untilResult(run.pipe)
const dhtBootstrap = JSON.parse(result)
alike(dhtBootstrap, Pear.config.dht.bootstrap, 'dht bootstrap matches Pear.config.dht.bootstrap')

await Helper.untilClose(run.pipe)
ok(true, 'ended')
}

await Promise.all([testVersions(), testDhtBootstrap()])
})

test('app with assets', async function ({ ok, is, plan, comment, teardown, timeout }) {
timeout(180000)
plan(5)

const { pipe, result } = await run({ dir: requireAssets, ok, comment, teardown })
const dir = requireAssets

const helper = new Helper()
teardown(() => helper.close(), { order: Infinity })
await helper.ready()

const id = Math.floor(Math.random() * 10000)

comment('staging')
const staging = helper.stage({ channel: `test-${id}`, name: `test-${id}`, dir, dryRun: false, bare: true })
teardown(() => Helper.teardownStream(staging))
const staged = await Helper.pick(staging, { tag: 'final' })
ok(staged.success, 'stage succeeded')

comment('seeding')
const seeding = helper.seed({ channel: `test-${id}`, name: `test-${id}`, dir, key: null, cmdArgs: [] })
teardown(() => Helper.teardownStream(seeding))
const until = await Helper.pick(seeding, [{ tag: 'key' }, { tag: 'announced' }])
const announced = await until.announced
ok(announced, 'seeding is announced')

const key = await until.key
ok(hypercoreid.isValid(key), 'app key is valid')

const link = `pear://${key}`
const run = await Helper.run({ link })

const result = await Helper.untilResult(run.pipe)
is(result.trim(), 'This is the content of the asset', 'Read asset from entrypoint')

await Helper.untilClose(pipe)
await Helper.untilClose(run.pipe)
ok(true, 'ended')
})

test('app with assets in sub dep', async function ({ ok, is, plan, comment, teardown, timeout }) {
timeout(180000)
plan(5)

const { pipe, result } = await run({ dir: subDepRequireAssets, ok, comment, teardown })
is(result.trim(), 'This is the content of the asset', 'Read asset from entrypoint')

await Helper.untilClose(pipe)
ok(true, 'ended')
})

async function run ({ dir, ok, comment, teardown }) {
const { key, link } = await build({ dir, ok, comment, teardown })
const { pipe } = await Helper.run({ link })
const result = await Helper.untilResult(pipe)
return { key, pipe, result }
}
const dir = subDepRequireAssets

async function build ({ dir, ok, comment, teardown }) {
const helper = new Helper()
teardown(() => helper.close(), { order: Infinity })
await helper.ready()
Expand All @@ -78,6 +154,11 @@ async function build ({ dir, ok, comment, teardown }) {
ok(hypercoreid.isValid(key), 'app key is valid')

const link = `pear://${key}`
const run = await Helper.run({ link })

const result = await Helper.untilResult(run.pipe)
is(result.trim(), 'This is the content of the asset', 'Read asset from entrypoint')

return { key, link }
}
await Helper.untilClose(run.pipe)
ok(true, 'ended')
})
9 changes: 8 additions & 1 deletion test/fixtures/dht-bootstrap/index.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,9 @@
const pipe = Pear.worker.pipe()
pipe.on('data', () => pipe.write(JSON.stringify(Pear.config.dht.bootstrap)))
pipe.on('data', () => {
try {
pipe.write(JSON.stringify(Pear.config.dht.bootstrap))
} catch (err) {
console.error(err)
Pear.exit()
}
})
4 changes: 2 additions & 2 deletions test/fixtures/require-assets/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ const fs = require('bare-fs')
const pipe = Pear.worker.pipe()
pipe.on('data', () => {
try {
pipe.write(fs.readFileSync(require.asset('./text-file.txt'), 'utf8'))
pipe.write(fs.readFileSync(require.asset('./text-file.txt')))
} catch (err) {
console.error(err)
pipe.write('failed to read asset')
Pear.exit()
}
})
2 changes: 1 addition & 1 deletion test/fixtures/sub-dep-require-assets/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ pipe.on('data', () => {
pipe.write(readAsset())
} catch (err) {
console.error(err)
pipe.write('failed to read asset')
Pear.exit()
}
})
2 changes: 1 addition & 1 deletion test/fixtures/sub-dep-require-assets/lib/utils.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
const fs = require('bare-fs')

module.exports = () => fs.readFileSync(require.asset('../text-file.txt'), 'utf8')
module.exports = () => fs.readFileSync(require.asset('../text-file.txt'))
3 changes: 2 additions & 1 deletion test/fixtures/versions/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ pipe.on('data', () => {
Pear.versions().then((versions) => {
pipe.write(JSON.stringify(versions))
}).catch((err) => {
pipe.write(`${err}`)
console.error(err)
Pear.exit()
})
})
12 changes: 12 additions & 0 deletions test/helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,14 @@ class Helper extends IPC {
clearTimeout(timeoutId)
resolve(data.toString())
})
pipe.on('close', () => {
clearTimeout(timeoutId)
reject(new Error('unexpected closed'))
})
pipe.on('end', () => {
clearTimeout(timeoutId)
reject(new Error('unexpected ended'))
})
})
pipe.write('start')
return res
Expand All @@ -167,6 +175,10 @@ class Helper extends IPC {
clearTimeout(timeoutId)
resolve('closed')
})
pipe.on('end', () => {
clearTimeout(timeoutId)
resolve('ended')
})
})
pipe.end()
return res
Expand Down

0 comments on commit 3cbdf25

Please sign in to comment.