From 63130f98343b85cb0cbafeb90e414c4eb06958da Mon Sep 17 00:00:00 2001 From: Marco Date: Fri, 1 Nov 2024 23:59:21 +1100 Subject: [PATCH 01/10] rename --- test/01-smoke.test.js | 36 ++++++++++++++++++------------------ 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/test/01-smoke.test.js b/test/01-smoke.test.js index a7d0d5c14..084c17a34 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,16 +12,16 @@ 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 versions = await _run({ dir: versionsDir, ok, comment, teardown }) + is(JSON.parse(versions.result).app.key, versions.key, 'app version matches staged key') - 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 dhtBootstrap = await _run({ dir: dhtBootstrapDir, ok, comment, teardown }) + alike(JSON.parse(dhtBootstrap.result), Pear.config.dht.bootstrap, 'dht bootstrap matches Pear.config.dth.bootstrap') - await Helper.untilClose(versionsRun.pipe) + await Helper.untilClose(versions.run.pipe) ok(true, 'ended') - await Helper.untilClose(dhtBootstrapRun.pipe) + await Helper.untilClose(dhtBootstrap.run.pipe) ok(true, 'ended') }) @@ -29,10 +29,10 @@ test('app with assets', async function ({ ok, is, plan, comment, teardown, timeo timeout(180000) plan(5) - const { pipe, result } = await run({ dir: requireAssets, ok, comment, teardown }) + const { run, result } = await _run({ dir: requireAssets, ok, comment, teardown }) 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 +40,21 @@ 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 }) + const { run, 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) + await Helper.untilClose(run.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 } +async function _run ({ dir, ok, comment, teardown }) { + const build = await _build({ dir, ok, comment, teardown }) + const run = await Helper.run({ link: build.link }) + const result = await Helper.untilResult(run.pipe) + return { build, run, result } } -async function build ({ dir, ok, comment, teardown }) { +async function _build ({ dir, ok, comment, teardown }) { const helper = new Helper() teardown(() => helper.close(), { order: Infinity }) await helper.ready() From da85d6193fe56509753df2293cb41398a273fe57 Mon Sep 17 00:00:00 2001 From: Marco Date: Sat, 2 Nov 2024 00:01:07 +1100 Subject: [PATCH 02/10] parse --- test/01-smoke.test.js | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/test/01-smoke.test.js b/test/01-smoke.test.js index 084c17a34..1e5ba1fe3 100644 --- a/test/01-smoke.test.js +++ b/test/01-smoke.test.js @@ -12,11 +12,11 @@ test('smoke', async function ({ ok, is, alike, plan, comment, teardown, timeout timeout(180000) plan(10) - const versions = await _run({ dir: versionsDir, ok, comment, teardown }) - is(JSON.parse(versions.result).app.key, versions.key, 'app version matches staged key') + const versions = await _run({ dir: versionsDir, ok, comment, teardown, parse: true }) + is(versions.result.app.key, versions.key, 'app version matches staged key') - const dhtBootstrap = await _run({ dir: dhtBootstrapDir, ok, comment, teardown }) - alike(JSON.parse(dhtBootstrap.result), Pear.config.dht.bootstrap, 'dht bootstrap matches Pear.config.dth.bootstrap') + const dhtBootstrap = await _run({ dir: dhtBootstrapDir, ok, comment, teardown, parse: true }) + alike(dhtBootstrap.result, Pear.config.dht.bootstrap, 'dht bootstrap matches Pear.config.dth.bootstrap') await Helper.untilClose(versions.run.pipe) ok(true, 'ended') @@ -47,10 +47,11 @@ test('app with assets in sub dep', async function ({ ok, is, plan, comment, tear ok(true, 'ended') }) -async function _run ({ dir, ok, comment, teardown }) { +async function _run ({ dir, ok, comment, teardown, parse }) { const build = await _build({ dir, ok, comment, teardown }) const run = await Helper.run({ link: build.link }) - const result = await Helper.untilResult(run.pipe) + const rawResult = await Helper.untilResult(run.pipe) + const result = parse ? JSON.parse(rawResult) : rawResult return { build, run, result } } From 861c4d8b34f178f37b72ce6f45aeb2cd36b0aef1 Mon Sep 17 00:00:00 2001 From: Marco Date: Sat, 2 Nov 2024 00:10:55 +1100 Subject: [PATCH 03/10] unexpected closed --- test/helper.js | 1 + 1 file changed, 1 insertion(+) diff --git a/test/helper.js b/test/helper.js index 13eefd108..eb3ce5ff1 100644 --- a/test/helper.js +++ b/test/helper.js @@ -155,6 +155,7 @@ class Helper extends IPC { clearTimeout(timeoutId) resolve(data.toString()) }) + pipe.on('close', () => reject(new Error('unexpected closed'))) }) pipe.write('start') return res From 2e95af022be6a9bfb44067dfbdb96d810c0f2ed2 Mon Sep 17 00:00:00 2001 From: Marco Date: Sat, 2 Nov 2024 00:12:28 +1100 Subject: [PATCH 04/10] pipe end --- test/fixtures/dht-bootstrap/index.js | 9 ++++++++- test/fixtures/require-assets/index.js | 2 +- test/fixtures/sub-dep-require-assets/index.js | 2 +- test/fixtures/versions/index.js | 3 ++- 4 files changed, 12 insertions(+), 4 deletions(-) diff --git a/test/fixtures/dht-bootstrap/index.js b/test/fixtures/dht-bootstrap/index.js index b40cf475a..c57013b5a 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) + pipe.end() + } +}) diff --git a/test/fixtures/require-assets/index.js b/test/fixtures/require-assets/index.js index 288f2113a..21f4a269f 100644 --- a/test/fixtures/require-assets/index.js +++ b/test/fixtures/require-assets/index.js @@ -6,6 +6,6 @@ pipe.on('data', () => { pipe.write(fs.readFileSync(require.asset('./text-file.txt'), 'utf8')) } catch (err) { console.error(err) - pipe.write('failed to read asset') + pipe.end() } }) diff --git a/test/fixtures/sub-dep-require-assets/index.js b/test/fixtures/sub-dep-require-assets/index.js index b9c4e1536..8dc166f72 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') + pipe.end() } }) diff --git a/test/fixtures/versions/index.js b/test/fixtures/versions/index.js index 904c6250d..2aaaa5ea8 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) + pipe.end() }) }) From 5349484d5e58cf37cbfe256fe532482b54bf948f Mon Sep 17 00:00:00 2001 From: Marco Date: Sat, 2 Nov 2024 00:14:58 +1100 Subject: [PATCH 05/10] clearTimeout --- test/helper.js | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/test/helper.js b/test/helper.js index eb3ce5ff1..101561750 100644 --- a/test/helper.js +++ b/test/helper.js @@ -155,7 +155,10 @@ class Helper extends IPC { clearTimeout(timeoutId) resolve(data.toString()) }) - pipe.on('close', () => reject(new Error('unexpected closed'))) + pipe.on('close', () => { + clearTimeout(timeoutId) + reject(new Error('unexpected closed')) + }) }) pipe.write('start') return res From f186cbf71b2215ba43119892bc572a3b8b1d97db Mon Sep 17 00:00:00 2001 From: Marco Date: Sat, 2 Nov 2024 00:31:25 +1100 Subject: [PATCH 06/10] unexpected ended --- test/fixtures/dht-bootstrap/index.js | 2 +- test/fixtures/require-assets/index.js | 2 +- test/fixtures/sub-dep-require-assets/index.js | 2 +- test/fixtures/versions/index.js | 2 +- test/helper.js | 4 ++++ 5 files changed, 8 insertions(+), 4 deletions(-) diff --git a/test/fixtures/dht-bootstrap/index.js b/test/fixtures/dht-bootstrap/index.js index c57013b5a..9a9477366 100644 --- a/test/fixtures/dht-bootstrap/index.js +++ b/test/fixtures/dht-bootstrap/index.js @@ -4,6 +4,6 @@ pipe.on('data', () => { pipe.write(JSON.stringify(Pear.config.dht.bootstrap)) } catch (err) { console.error(err) - pipe.end() + Pear.exit() } }) diff --git a/test/fixtures/require-assets/index.js b/test/fixtures/require-assets/index.js index 21f4a269f..afec40328 100644 --- a/test/fixtures/require-assets/index.js +++ b/test/fixtures/require-assets/index.js @@ -6,6 +6,6 @@ pipe.on('data', () => { pipe.write(fs.readFileSync(require.asset('./text-file.txt'), 'utf8')) } catch (err) { console.error(err) - pipe.end() + Pear.exit() } }) diff --git a/test/fixtures/sub-dep-require-assets/index.js b/test/fixtures/sub-dep-require-assets/index.js index 8dc166f72..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.end() + Pear.exit() } }) diff --git a/test/fixtures/versions/index.js b/test/fixtures/versions/index.js index 2aaaa5ea8..69ebc295e 100644 --- a/test/fixtures/versions/index.js +++ b/test/fixtures/versions/index.js @@ -4,6 +4,6 @@ pipe.on('data', () => { pipe.write(JSON.stringify(versions)) }).catch((err) => { console.error(err) - pipe.end() + Pear.exit() }) }) diff --git a/test/helper.js b/test/helper.js index 101561750..72fe30449 100644 --- a/test/helper.js +++ b/test/helper.js @@ -159,6 +159,10 @@ class Helper extends IPC { clearTimeout(timeoutId) reject(new Error('unexpected closed')) }) + pipe.on('end', () => { + clearTimeout(timeoutId) + reject(new Error('unexpected ended')) + }) }) pipe.write('start') return res From f66c6e30604528c1ff9a13ecdf1dabf58bcb6627 Mon Sep 17 00:00:00 2001 From: Marco Date: Sat, 2 Nov 2024 00:42:47 +1100 Subject: [PATCH 07/10] key --- test/01-smoke.test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/01-smoke.test.js b/test/01-smoke.test.js index 1e5ba1fe3..f02d723a5 100644 --- a/test/01-smoke.test.js +++ b/test/01-smoke.test.js @@ -13,7 +13,7 @@ test('smoke', async function ({ ok, is, alike, plan, comment, teardown, timeout plan(10) const versions = await _run({ dir: versionsDir, ok, comment, teardown, parse: true }) - is(versions.result.app.key, versions.key, 'app version matches staged key') + is(versions.result.app.key, versions.build.key, 'app version matches staged key') const dhtBootstrap = await _run({ dir: dhtBootstrapDir, ok, comment, teardown, parse: true }) alike(dhtBootstrap.result, Pear.config.dht.bootstrap, 'dht bootstrap matches Pear.config.dth.bootstrap') From ddd8addb87e6408a4e7db6abf9fabd50c4d8d877 Mon Sep 17 00:00:00 2001 From: Marco Date: Sat, 2 Nov 2024 00:44:15 +1100 Subject: [PATCH 08/10] ended --- test/helper.js | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/test/helper.js b/test/helper.js index 72fe30449..91076b1f7 100644 --- a/test/helper.js +++ b/test/helper.js @@ -175,6 +175,10 @@ class Helper extends IPC { clearTimeout(timeoutId) resolve('closed') }) + pipe.on('end', () => { + clearTimeout(timeoutId) + resolve('ended') + }) }) pipe.end() return res From 8c4c96ad1ff5f3aa13abfbc1dbb42aa7b05727a5 Mon Sep 17 00:00:00 2001 From: Marco Date: Sat, 2 Nov 2024 01:12:50 +1100 Subject: [PATCH 09/10] buffer --- test/fixtures/require-assets/index.js | 2 +- test/fixtures/sub-dep-require-assets/lib/utils.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/test/fixtures/require-assets/index.js b/test/fixtures/require-assets/index.js index afec40328..9699ee397 100644 --- a/test/fixtures/require-assets/index.js +++ b/test/fixtures/require-assets/index.js @@ -3,7 +3,7 @@ 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) 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')) From d880b50d3be1c752ea36c08b98ae893c7584e547 Mon Sep 17 00:00:00 2001 From: Marco Date: Sat, 2 Nov 2024 01:23:17 +1100 Subject: [PATCH 10/10] inline test --- test/01-smoke.test.js | 132 +++++++++++++++++++++++++++++++++--------- 1 file changed, 106 insertions(+), 26 deletions(-) diff --git a/test/01-smoke.test.js b/test/01-smoke.test.js index f02d723a5..a5f1d17e7 100644 --- a/test/01-smoke.test.js +++ b/test/01-smoke.test.js @@ -12,24 +12,113 @@ test('smoke', async function ({ ok, is, alike, plan, comment, teardown, timeout timeout(180000) plan(10) - const versions = await _run({ dir: versionsDir, ok, comment, teardown, parse: true }) - is(versions.result.app.key, versions.build.key, 'app version matches staged key') + const testVersions = async () => { + const dir = versionsDir - const dhtBootstrap = await _run({ dir: dhtBootstrapDir, ok, comment, teardown, parse: true }) - alike(dhtBootstrap.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(versions.run.pipe) - ok(true, 'ended') + const id = Math.floor(Math.random() * 10000) - await Helper.untilClose(dhtBootstrap.run.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 { run, 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(run.pipe) @@ -40,22 +129,8 @@ test('app with assets in sub dep', async function ({ ok, is, plan, comment, tear timeout(180000) plan(5) - const { run, 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(run.pipe) - ok(true, 'ended') -}) + const dir = subDepRequireAssets -async function _run ({ dir, ok, comment, teardown, parse }) { - const build = await _build({ dir, ok, comment, teardown }) - const run = await Helper.run({ link: build.link }) - const rawResult = await Helper.untilResult(run.pipe) - const result = parse ? JSON.parse(rawResult) : rawResult - return { build, run, result } -} - -async function _build ({ dir, ok, comment, teardown }) { const helper = new Helper() teardown(() => helper.close(), { order: Infinity }) await helper.ready() @@ -79,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') +})