|
| 1 | +/* globals describe, it */ |
| 2 | +import * as codec from 'multiformats/codecs/json' |
| 3 | +import * as dagPB from '@ipld/dag-pb' |
| 4 | +import { sha256 as hasher } from 'multiformats/hashes/sha2' |
| 5 | +import * as main from 'multiformats/block' |
| 6 | +import { walk } from 'multiformats/traversal' |
| 7 | +import { deepStrictEqual as same } from 'assert' |
| 8 | + |
| 9 | +const test = it |
| 10 | +const { createLink, createNode } = dagPB |
| 11 | + |
| 12 | +describe('traversal', () => { |
| 13 | + describe('walk', async () => { |
| 14 | + // Forming the following DAG for testing |
| 15 | + // A |
| 16 | + // / \ |
| 17 | + // B C |
| 18 | + // / \ / \ |
| 19 | + // D D D E |
| 20 | + const linksE = [] |
| 21 | + const valueE = createNode(Uint8Array.from('string E qacdswa'), linksE) |
| 22 | + const blockE = await main.encode({ value: valueE, codec, hasher }) |
| 23 | + const cidE = blockE.cid |
| 24 | + |
| 25 | + const linksD = [] |
| 26 | + const valueD = createNode(Uint8Array.from('string D zasa'), linksD) |
| 27 | + const blockD = await main.encode({ value: valueD, codec, hasher }) |
| 28 | + const cidD = blockD.cid |
| 29 | + |
| 30 | + const linksC = [createLink('link1', 100, cidD), createLink('link2', 100, cidE)] |
| 31 | + const valueC = createNode(Uint8Array.from('string C zxc'), linksC) |
| 32 | + const blockC = await main.encode({ value: valueC, codec, hasher }) |
| 33 | + const cidC = blockC.cid |
| 34 | + |
| 35 | + const linksB = [createLink('link1', 100, cidD), createLink('link2', 100, cidD)] |
| 36 | + const valueB = createNode(Uint8Array.from('string B lpokjiasd'), linksB) |
| 37 | + const blockB = await main.encode({ value: valueB, codec, hasher }) |
| 38 | + const cidB = blockB.cid |
| 39 | + |
| 40 | + const linksA = [createLink('link1', 100, cidB), createLink('link2', 100, cidC)] |
| 41 | + const valueA = createNode(Uint8Array.from('string A qwertcfdgshaa'), linksA) |
| 42 | + const blockA = await main.encode({ value: valueA, codec, hasher }) |
| 43 | + const cidA = blockA.cid |
| 44 | + |
| 45 | + const load = async (cid) => { |
| 46 | + if (cid.equals(cidE)) { |
| 47 | + return blockE |
| 48 | + } |
| 49 | + if (cid.equals(cidD)) { |
| 50 | + return blockD |
| 51 | + } |
| 52 | + if (cid.equals(cidC)) { |
| 53 | + return blockC |
| 54 | + } |
| 55 | + if (cid.equals(cidB)) { |
| 56 | + return blockB |
| 57 | + } |
| 58 | + if (cid.equals(cidA)) { |
| 59 | + return blockA |
| 60 | + } |
| 61 | + return null |
| 62 | + } |
| 63 | + |
| 64 | + const loadWrapper = (load, arr = []) => (cid) => { |
| 65 | + arr.push(cid.toString()) |
| 66 | + return load(cid) |
| 67 | + } |
| 68 | + |
| 69 | + test('block with no links', async () => { |
| 70 | + // Test Case 1 |
| 71 | + // Input DAG |
| 72 | + // D |
| 73 | + // |
| 74 | + // Expect load to be called with D |
| 75 | + const expectedCallArray = [cidD.toString()] |
| 76 | + const callArray = [] |
| 77 | + |
| 78 | + await walk({ cid: cidD, load: loadWrapper(load, callArray) }) |
| 79 | + |
| 80 | + expectedCallArray.forEach((value, index) => { |
| 81 | + same(value, callArray[index]) |
| 82 | + }) |
| 83 | + }) |
| 84 | + |
| 85 | + test('block with links', async () => { |
| 86 | + // Test Case 2 |
| 87 | + // Input |
| 88 | + // C |
| 89 | + // / \ |
| 90 | + // D E |
| 91 | + // |
| 92 | + // Expect load to be called with C, then D, then E |
| 93 | + const expectedCallArray = [cidC.toString(), cidD.toString(), cidE.toString()] |
| 94 | + const callArray = [] |
| 95 | + |
| 96 | + await walk({ cid: cidC, load: loadWrapper(load, callArray) }) |
| 97 | + |
| 98 | + expectedCallArray.forEach((value, index) => { |
| 99 | + same(value, callArray[index]) |
| 100 | + }) |
| 101 | + }) |
| 102 | + |
| 103 | + test('block with matching links', async () => { |
| 104 | + // Test Case 3 |
| 105 | + // Input |
| 106 | + // B |
| 107 | + // / \ |
| 108 | + // D D |
| 109 | + // |
| 110 | + // Expect load to be called with B, then D |
| 111 | + const expectedCallArray = [cidB.toString(), cidD.toString()] |
| 112 | + const callArray = [] |
| 113 | + |
| 114 | + await walk({ cid: cidB, load: loadWrapper(load, callArray) }) |
| 115 | + |
| 116 | + expectedCallArray.forEach((value, index) => { |
| 117 | + same(value, callArray[index]) |
| 118 | + }) |
| 119 | + }) |
| 120 | + |
| 121 | + test('depth first with duplicated block', async () => { |
| 122 | + // Test Case 4 |
| 123 | + // Input |
| 124 | + // A |
| 125 | + // / \ |
| 126 | + // B C |
| 127 | + // / \ / \ |
| 128 | + // D D D E |
| 129 | + // |
| 130 | + // Expect load to be called with A, then B, then D, then C, then E |
| 131 | + const expectedCallArray = [ |
| 132 | + cidA.toString(), |
| 133 | + cidB.toString(), |
| 134 | + cidD.toString(), |
| 135 | + cidC.toString(), |
| 136 | + cidE.toString() |
| 137 | + ] |
| 138 | + const callArray = [] |
| 139 | + |
| 140 | + await walk({ cid: cidA, load: loadWrapper(load, callArray) }) |
| 141 | + |
| 142 | + expectedCallArray.forEach((value, index) => { |
| 143 | + same(value, callArray[index]) |
| 144 | + }) |
| 145 | + }) |
| 146 | + |
| 147 | + test('null return', async () => { |
| 148 | + const links = [] |
| 149 | + const value = createNode(Uint8Array.from('test'), links) |
| 150 | + const block = await main.encode({ value: value, codec, hasher }) |
| 151 | + const cid = block.cid |
| 152 | + const expectedCallArray = [cid.toString()] |
| 153 | + const callArray = [] |
| 154 | + |
| 155 | + await walk({ cid, load: loadWrapper(load, callArray) }) |
| 156 | + |
| 157 | + expectedCallArray.forEach((value, index) => { |
| 158 | + same(value, callArray[index]) |
| 159 | + }) |
| 160 | + }) |
| 161 | + }) |
| 162 | +}) |
0 commit comments