diff --git a/test/01-smoke.test.js b/test/01-smoke.test.js index a7d0d5c14..a5f1d17e7 100644 --- a/test/01-smoke.test.js +++ b/test/01-smoke.test.js @@ -3,8 +3,8 @@ 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') @@ -12,27 +12,116 @@ 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') }) @@ -40,21 +129,8 @@ test('app with assets in sub dep', async function ({ ok, is, plan, comment, tear 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() @@ -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') +}) diff --git a/test/fixtures/dht-bootstrap/index.js b/test/fixtures/dht-bootstrap/index.js index b40cf475a..9a9477366 100644 --- a/test/fixtures/dht-bootstrap/index.js +++ b/test/fixtures/dht-bootstrap/index.js @@ -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() + } +}) diff --git a/test/fixtures/require-assets/index.js b/test/fixtures/require-assets/index.js index 288f2113a..9699ee397 100644 --- a/test/fixtures/require-assets/index.js +++ b/test/fixtures/require-assets/index.js @@ -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() } }) diff --git a/test/fixtures/sub-dep-require-assets/index.js b/test/fixtures/sub-dep-require-assets/index.js index b9c4e1536..10cec31e2 100644 --- a/test/fixtures/sub-dep-require-assets/index.js +++ b/test/fixtures/sub-dep-require-assets/index.js @@ -6,6 +6,6 @@ pipe.on('data', () => { pipe.write(readAsset()) } catch (err) { console.error(err) - pipe.write('failed to read asset') + Pear.exit() } }) diff --git a/test/fixtures/sub-dep-require-assets/lib/utils.js b/test/fixtures/sub-dep-require-assets/lib/utils.js index dfba6a467..e204d2fea 100644 --- a/test/fixtures/sub-dep-require-assets/lib/utils.js +++ b/test/fixtures/sub-dep-require-assets/lib/utils.js @@ -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')) diff --git a/test/fixtures/versions/index.js b/test/fixtures/versions/index.js index 904c6250d..69ebc295e 100644 --- a/test/fixtures/versions/index.js +++ b/test/fixtures/versions/index.js @@ -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() }) }) diff --git a/test/helper.js b/test/helper.js index 13eefd108..91076b1f7 100644 --- a/test/helper.js +++ b/test/helper.js @@ -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 @@ -167,6 +175,10 @@ class Helper extends IPC { clearTimeout(timeoutId) resolve('closed') }) + pipe.on('end', () => { + clearTimeout(timeoutId) + resolve('ended') + }) }) pipe.end() return res