/
resolve-ipld-path.test.js
183 lines (170 loc) · 6.2 KB
/
resolve-ipld-path.test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/* global describe it expect */
// @ts-check
import * as dagCbor from '@ipld/dag-cbor'
import * as dagPb from '@ipld/dag-pb'
import * as raw from 'multiformats/codecs/raw'
import { addDagNodeToHelia } from './helpers'
import resolveIpldPath, { findLinkPath } from './resolve-ipld-path'
import { createHeliaMock } from '../../test/unit/heliaMock'
// #WhenAddingNewCodec
describe('resolveIpldPath', () => {
/**
* @type {import('@helia/interface').Helia}
*/
let helia
beforeEach(async () => {
helia = await createHeliaMock()
})
it('resolves all nodes traversed along a path', async () => {
const node4Cid = await addDagNodeToHelia(helia, 'dag-pb', createDagPbNode('4th node', []))
const node3Cid = await addDagNodeToHelia(helia, 'dag-pb', createDagPbNode('3rd node', [{
name: 'a',
cid: node4Cid.toString(),
size: 101
}]))
const node2Cid = await addDagNodeToHelia(helia, 'dag-pb', createDagPbNode('2nd node', [{
name: 'b',
cid: node3Cid.toString(),
size: 101
}]))
const rootNodeCid = await addDagNodeToHelia(helia, 'dag-pb', createDagPbNode('root node', [{
name: 'a',
cid: node2Cid.toString(),
size: 101
}]))
const res = await resolveIpldPath(helia, rootNodeCid.toString(), '/a/b/a')
expect(res.canonicalPath).toBe(node4Cid.toString())
expect(res.nodes.length).toBe(4)
expect(res.nodes[0].cid).toBe(rootNodeCid.toString())
expect(res.nodes[1].cid).toBe(node2Cid.toString())
expect(res.nodes[2].cid).toBe(node3Cid.toString())
expect(res.nodes[3].cid).toBe(node4Cid.toString())
expect(res.pathBoundaries.length).toBe(3)
expect(res.pathBoundaries[0]).toEqual(expect.objectContaining({
path: 'a',
source: rootNodeCid.toString(),
target: node2Cid.toString()
}))
})
it('resolves thru dag-cbor to dag-pb to dag-pb', async () => {
const path = '/a/b/pb1'
const dagNode3 = await createDagPbNode('the second pb node', [])
const dagNode3CID = await addDagNodeToHelia(helia, 'dag-pb', dagNode3)
const dagNode2 = await createDagPbNode('the first pb node', [{
name: 'pb1',
cid: dagNode3CID.toString(),
size: 101
}])
const dagNode2CID = await addDagNodeToHelia(helia, 'dag-pb', dagNode2)
const dagNode1 = {
a: {
b: dagNode2CID
}
}
const dagNode1Cid = await addDagNodeToHelia(helia, 'dag-cbor', dagNode1)
const res = await resolveIpldPath(helia, dagNode1Cid.toString(), path)
expect(res.targetNode.cid).toEqual(dagNode3CID.toString())
expect(res.canonicalPath).toBe(dagNode3CID.toString())
expect(res.nodes.length).toBe(3)
expect(res.nodes[0].type).toBe(dagCbor.code)
expect(res.nodes[0].cid).toBe(dagNode1Cid.toString())
expect(res.nodes[0].links.length).toBe(1)
expect(res.nodes[1].type).toBe(dagPb.code)
expect(res.nodes[1].cid).toBe(dagNode2CID.toString())
expect(res.nodes[1].links.length).toBe(1)
expect(res.nodes[2].type).toBe(dagPb.code)
expect(res.nodes[2].cid).toBe(dagNode3CID.toString())
expect(res.nodes[2].links.length).toBe(0)
expect(res.pathBoundaries.length).toBe(2)
expect(res.pathBoundaries[0]).toEqual(expect.objectContaining({
path: 'a/b',
source: dagNode1Cid.toString(),
target: dagNode2CID.toString()
}))
expect(res.pathBoundaries[1]).toEqual(expect.objectContaining({
index: 0,
path: 'pb1',
source: dagNode2CID.toString(),
target: dagNode3CID.toString()
}))
})
/**
* This test proves that an error ("Requested IPLD path should end with the remainder path") discovered when viewing
* the dag-cbor example from the start exploring page when loading path
* `bafyreicnokmhmrnlp2wjhyk2haep4tqxiptwfrp2rrs7rzq7uk766chqvq/cheese/0` does not occur.
*
* The CIDs of the below objects should be:
*
* rootNode: bafyreicnokmhmrnlp2wjhyk2haep4tqxiptwfrp2rrs7rzq7uk766chqvq
* childNode: bafkreifvxooyaffa7gy5mhrb46lnpdom34jvf4r42mubf5efbodyvzeujq
*
* but it doesn't necessarily matter, this was just an edgecase discovered accidentally.
*/
it('resolves dag-cbor node with children all pointing to raw node', async () => {
const path = '/cheese/0'
const textEncoder = new TextEncoder()
const childNode = await addDagNodeToHelia(helia, 'raw', textEncoder.encode('foo\n'))
const rootNode = await addDagNodeToHelia(helia, 'dag-cbor', {
cats: 'not cats',
cheese: [
childNode,
childNode,
childNode,
childNode
],
something: childNode
})
const res = await resolveIpldPath(helia, rootNode.toString(), path)
expect(res.targetNode.cid.toString()).toBe(childNode.toString())
expect(res.canonicalPath).toBe(childNode.toString())
expect(res.nodes.length).toBe(2)
expect(res.nodes[0].cid.toString()).toBe(rootNode.toString())
expect(res.nodes[0].type).toBe(dagCbor.code)
expect(res.nodes[0].links.length).toBe(5)
expect(res.nodes[1].cid.toString()).toBe(childNode.toString())
expect(res.nodes[1].type).toBe(raw.code)
expect(res.nodes[1].links.length).toBe(0)
expect(res.pathBoundaries.length).toBe(1)
expect(res.pathBoundaries[0]).toEqual(expect.objectContaining({
path: 'cheese/0',
source: rootNode.toString(),
target: childNode.toString()
}))
})
})
/**
*
* @typedef {object} OldDagPbLinkLiteral
* @property {string} name
* @property {string} cid
* @property {number} size
*/
/**
*
* @param {string|Uint8Array} data
* @param {OldDagPbLinkLiteral[]} links
* @returns {import('@ipld/dag-pb').PBNode}
*/
function createDagPbNode (data, links) {
return dagPb.prepare({
Data: data,
Links: links.map(({ cid, name, size }) => {
return {
Hash: cid,
Name: name,
Tsize: size
}
})
})
}
describe('findLinkPath', () => {
it('finds the linkPath from a fullPath and a remainderPath', () => {
expect(findLinkPath('', '')).toBe(null)
expect(findLinkPath('/', '')).toBe(null)
expect(findLinkPath('/foo/bar', 'bar')).toBe('foo')
expect(findLinkPath('/a/b/c/a', 'b/c/a')).toBe('a')
expect(findLinkPath('/a/b/c/a', '/b/c/a')).toBe('a')
expect(findLinkPath('a/b/c/a', '/b/c/a')).toBe('a')
expect(() => findLinkPath('/foo', 'wat')).toThrow()
})
})