fix a Tangle.topoSort() corner case

This commit is contained in:
Andre Staltz 2023-04-18 16:55:53 +03:00
parent 656d622271
commit 8288432785
2 changed files with 21 additions and 3 deletions

View File

@ -132,7 +132,6 @@ class Tangle {
const max = this.#maxDepth const max = this.#maxDepth
for (let i = 0; i <= max; i++) { for (let i = 0; i <= max; i++) {
const atDepth = this.#getAllAtDepth(i) const atDepth = this.#getAllAtDepth(i)
if (atDepth.length === 0) break
for (const msgHash of atDepth) { for (const msgHash of atDepth) {
sorted.push(msgHash) sorted.push(msgHash)
} }

View File

@ -96,7 +96,7 @@ test('Tangle.has', (t) => {
t.end() t.end()
}) })
test('Tangle.getDepth', t=> { test('Tangle.getDepth', (t) => {
t.equals(tangle.getDepth(rootPost), 0, 'depth of rootPost is 0') t.equals(tangle.getDepth(rootPost), 0, 'depth of rootPost is 0')
t.equals(tangle.getDepth(reply1Lo), 1, 'depth of reply1Lo is 1') t.equals(tangle.getDepth(reply1Lo), 1, 'depth of reply1Lo is 1')
t.equals(tangle.getDepth(reply1Hi), 1, 'depth of reply1Hi is 1') t.equals(tangle.getDepth(reply1Hi), 1, 'depth of reply1Hi is 1')
@ -106,7 +106,7 @@ test('Tangle.getDepth', t=> {
t.end() t.end()
}) })
test('Tangle.getMaxDepth', t => { test('Tangle.getMaxDepth', (t) => {
t.equals(tangle.getMaxDepth(), 3, 'max depth is 3') t.equals(tangle.getMaxDepth(), 3, 'max depth is 3')
t.end() t.end()
}) })
@ -172,6 +172,25 @@ test('Tangle.getDeletablesAndErasables with lipmaa', (t) => {
t.end() t.end()
}) })
test('Tangle.topoSort after some have been deleted and erased', async (t) => {
const { deletables, erasables } = tangle.getDeletablesAndErasables(reply3Lo)
for (const msgHash of deletables) {
await p(peer.db.del)(msgHash)
}
for (const msgHash of erasables) {
await p(peer.db.erase)(msgHash)
}
const tangle2 = peer.db.getTangle(rootPost)
const sorted = tangle2.topoSort()
t.deepEquals(sorted, [
rootPost,
reply3Lo,
reply3Hi,
])
})
test('teardown', async (t) => { test('teardown', async (t) => {
await p(peer.close)(true) await p(peer.close)(true)
}) })