-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
executable file
·339 lines (254 loc) · 8.25 KB
/
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
import { IPLDURLSystem, IPLDURL } from './index.js'
import { create } from 'ipfs-core'
import { fromDSL } from '@ipld/schema/from-dsl.js'
import test from 'tape'
const node = await create({
silent: true,
config: {
Bootstrap: []
},
libp2p: {
nat: {
enabled: false
}
}
})
test.onFinish(() => {
node.stop()
})
test('Load simple value from URL', async (t) => {
const system = new IPLDURLSystem({ getNode, saveNode })
const cid = await put({ hello: 'world' })
const url = `ipld://${cid}/hello`
const resolved = await system.resolve(url)
t.equal(resolved, 'world', 'resolved expected value')
})
test('Interpret root data via schema type', async (t) => {
const system = new IPLDURLSystem({ getNode, saveNode })
const schemaCID = await addSchema(`
type Example {String:String} representation listpairs
`)
const dataCID = await put([
['Hello', 'World'],
['Goodbye', 'Cyberspace']
])
const url = `ipld://${dataCID};schema=${schemaCID};type=Example/`
const resolved = await system.resolve(url)
t.deepEqual(resolved, {
Hello: 'World',
Goodbye: 'Cyberspace'
}, 'Parsed data into expected structure')
})
test('Interpret nested data via schema type', async (t) => {
const system = new IPLDURLSystem({ getNode, saveNode })
const schemaCID = await addSchema(`
type Example {String:String} representation listpairs
`)
const dataCID = await put({
example: [
['Hello', 'World'],
['Goodbye', 'Cyberspace']
]
})
const url = `ipld://${dataCID}/example;schema=${schemaCID};type=Example`
const resolved = await system.resolve(url)
t.deepEqual(resolved, {
Hello: 'World',
Goodbye: 'Cyberspace'
}, 'Parsed data into expected structure')
})
test('Apply schema for sub-nodes during pathing', async (t) => {
const system = new IPLDURLSystem({ getNode, saveNode })
const schemaCID = await addSchema(`
type Example struct {
Hello String
Goodbye NestedExample
} representation tuple
type NestedExample struct {
region String
} representation tuple
`)
const dataCID = await put(
['Hello', ['Cyberspace']]
)
const expected = {
region: 'Cyberspace'
}
const url = `ipld://${dataCID};schema=${schemaCID};type=Example/Goodbye/`
const resolved = await system.resolve(url)
t.deepEqual(resolved, expected, 'Parsed data into expected structure')
})
test('Traverse over links', async (t) => {
const system = new IPLDURLSystem({ getNode, saveNode })
const cid1 = await put('Hello, World?')
const cid2 = await put({
example: cid1
})
const rootURL = `ipld://${cid2}/`
const expected1 = {
example: cid1
}
const resolved1 = await system.resolve(rootURL)
t.deepEqual(resolved1?.example?.bytes, expected1?.example?.bytes, 'Resolved data with CID present')
const subURL = rootURL + 'example/'
const expected2 = 'Hello, World?'
const resolved2 = await system.resolve(subURL)
t.deepEqual(resolved2, expected2, 'Resolved data pointed to by link')
})
test('Preserve schema type when traversing Links', async (t) => {
const system = new IPLDURLSystem({ getNode, saveNode })
const schemaCID = await addSchema(`
type Example struct {
Hello String
Goodbye &NestedExample
} representation tuple
type NestedExample struct {
region String
} representation tuple
`)
const cid1 = await put(['Cyberspace'])
const cid2 = await put(['Hello', cid1])
const expected = {
region: 'Cyberspace'
}
const url = `ipld://${cid2};schema=${schemaCID};type=Example/Goodbye/`
const resolved = await system.resolve(url)
t.deepEqual(resolved, expected, 'Parsed data into expected structure')
})
test('Traverse segments with / in the name', async (t) => {
const system = new IPLDURLSystem({ getNode, saveNode })
const weirdPath = 'hello/world'
const cid = await put({
[weirdPath]: 'Fancy!'
})
const url = `ipld://${cid}/${encodeURIComponent(weirdPath)}/`
const resolved = await system.resolve(url)
t.equal(resolved, 'Fancy!', 'Resolved data at nested path')
})
test('Patch, add and move on root', async (t) => {
const cid = await put({
hello: ['world']
})
const patches = [
{ op: 'add', path: '/hello/0', value: 'cruel' },
{ op: 'move', path: '/goodbye', from: '/hello' }
]
const url = `ipld://${cid}/`
const system = new IPLDURLSystem({ getNode, saveNode })
const updatedURL = await system.patch(url, patches)
// It's determenistic! 🤯
const expectedURL = 'ipld://bafyreiaigmnxp4ehbvt4nptoof2w7dixyanblnq3lfvxslulsrzkcpk3ni/'
t.equal(updatedURL, expectedURL, 'Got expected result URL')
const resolved = await system.resolve(updatedURL)
const expected = {
goodbye: ['cruel', 'world']
}
t.deepEqual(resolved, expected, 'Got expected structure')
})
test('Patch accross Link boundry', async (t) => {
const cid1 = await put({
hello: ['world']
})
const cid2 = await put({
example: cid1
})
const patches = [
{ op: 'add', path: '/example/hello/0', value: 'cruel' },
{ op: 'move', path: '/example/goodbye', from: '/example/hello' }
]
const url = `ipld://${cid2}/`
const system = new IPLDURLSystem({ getNode, saveNode })
const updatedURL = await system.patch(url, patches)
// It's determenistic! 🤯
const expectedURL = 'ipld://bafyreiaeh5ftdg5qmvaxsj54dm25ja5kd5446hpst3zy3u7qcx42v54f5a/'
t.equal(updatedURL, expectedURL, 'Got expected result URL')
const resolved = await system.resolve(updatedURL + 'example/')
const expected = {
goodbye: ['cruel', 'world']
}
t.deepEqual(resolved, expected, 'Got expected structure')
})
test('Patch over schema', async (t) => {
const system = new IPLDURLSystem({ getNode, saveNode })
const schemaCID = await addSchema(`
type Example {String:String} representation listpairs
`)
const dataCID = await put({
example: [
['Hello', 'World'],
['Goodbye', 'Cyberspace']
]
})
const url = `ipld://${dataCID}/example;schema=${schemaCID};type=Example`
const patches = [
{ op: 'replace', path: '/Goodbye', value: 'Cruel World' }
]
const updatedURL = await system.patch(url, patches)
// It's determenistic! 🤯
const expectedURL = 'ipld://bafyreihg26bnrowvbnm4tqismqubtjowpnoeh364iifnlsgcnr7unmnc7u/example;schema=bafyreideyq75einxa57fkeqkgguwcbsxmuloxzmtdtzn6ft4nsijybbu6q;type=Example'
t.equal(updatedURL, expectedURL, 'Got expected result URL')
const resolved = await system.resolve(updatedURL)
const expected = {
Hello: 'World',
Goodbye: 'Cruel World'
}
t.deepEqual(resolved, expected, 'Got expected structure')
const expectedRaw = {
example: [
['Hello', 'World'],
['Goodbye', 'Cruel World']
]
}
const { cid: updatedCid } = new IPLDURL(updatedURL)
const updatedRawURL = `ipld://${updatedCid}/`
const resolvedRaw = await system.resolve(updatedRawURL)
t.deepEqual(resolvedRaw, expectedRaw, 'Got expected raw structure')
})
test('Patch over schema with link', async (t) => {
const system = new IPLDURLSystem({ getNode, saveNode })
const schemaCID = await addSchema(`
type Example struct {
Hello String
Goodbye &NestedExample
} representation tuple
type NestedExample struct {
region String
} representation tuple
`)
const cid1 = await put(['Cyberspace'])
const cid2 = await put(['Hello', cid1])
const patches = [
{ op: 'replace', path: '/Goodbye/region', value: 'Cruel World' }
]
const url = `ipld://${cid2};schema=${schemaCID};type=Example/`
const updatedURL = await system.patch(url, patches)
const expected = {
region: 'Cruel World'
}
const changedValueURL = new URL('Goodbye/',updatedURL)
const resolved = await system.resolve(changedValueURL)
t.deepEqual(resolved, expected, 'Got expected data after patching')
})
test.skip('Patch over ADL')
test.skip('Path over link that links to a link')
async function addSchema (dslString) {
// Convert to DMT
const dmt = fromDSL(dslString)
// Add to IPFS node
const cid = put(dmt)
// Return CID
return cid
}
async function put (data) {
return node.dag.put(data)
}
async function getNode (cid) {
const { value } = await node.dag.get(cid)
return value
}
async function saveNode (data, { encoding = 'dag-cbor', ...opts } = {}) {
return node.dag.put(data, {
storeCodec: encoding,
...opts
})
}