From 93d9b5425a87456b83c80d02f2d2887511076e69 Mon Sep 17 00:00:00 2001 From: Andre Staltz Date: Fri, 14 Apr 2023 16:13:58 +0300 Subject: [PATCH] add Tangle.has --- lib/tangle.js | 8 ++++++++ test/tangle.test.js | 19 +++++++++++++++---- 2 files changed, 23 insertions(+), 4 deletions(-) diff --git a/lib/tangle.js b/lib/tangle.js index 3b97bd2..8db8ffb 100644 --- a/lib/tangle.js +++ b/lib/tangle.js @@ -129,6 +129,14 @@ class Tangle { return this.#getAllAtDepth(lipmaaDepth) } + /** + * @param {string} msgHash + * @returns {boolean} + */ + has(msgHash) { + return this.#depth.has(msgHash) + } + #shortestPathToRoot(msgHash) { const path = [] let current = msgHash diff --git a/test/tangle.test.js b/test/tangle.test.js index c635e52..5892221 100644 --- a/test/tangle.test.js +++ b/test/tangle.test.js @@ -83,6 +83,19 @@ test('setup', async (t) => { reply3Hi = reply3B.localeCompare(reply3C) < 0 ? reply3C : reply3B }) +test('Tangle.has', (t) => { + const tangle = new Tangle(rootPost, peer.db.records()) + + t.true(tangle.has(rootPost), 'has rootPost') + t.true(tangle.has(reply1Lo), 'has reply1Lo') + t.true(tangle.has(reply1Hi), 'has reply1Hi') + t.true(tangle.has(reply2A), 'has reply2A') + t.true(tangle.has(reply3Lo), 'has reply3Lo') + t.true(tangle.has(reply3Hi), 'has reply3Hi') + t.false(tangle.has('nonsense'), 'does not have nonsense') + t.end() +}) + test('Tangle.topoSort', (t) => { const tangle = new Tangle(rootPost, peer.db.records()) const sorted = tangle.topoSort() @@ -95,7 +108,6 @@ test('Tangle.topoSort', (t) => { reply3Lo, reply3Hi, ]) - console.log(sorted); t.end() }) @@ -121,7 +133,7 @@ test('Tangle.getLipmaa', (t) => { t.end() }) -test('Tangle.getDeletablesAndEmptyables basic', t => { +test('Tangle.getDeletablesAndEmptyables basic', (t) => { const tangle = new Tangle(rootPost, peer.db.records()) const { deletables, emptyables } = tangle.getDeletablesAndEmptyables(reply2A) @@ -130,8 +142,7 @@ test('Tangle.getDeletablesAndEmptyables basic', t => { t.end() }) - -test('Tangle.getDeletablesAndEmptyables with lipmaa', t => { +test('Tangle.getDeletablesAndEmptyables with lipmaa', (t) => { const tangle = new Tangle(rootPost, peer.db.records()) const { deletables, emptyables } = tangle.getDeletablesAndEmptyables(reply3Lo)