-
Notifications
You must be signed in to change notification settings - Fork 47
/
test-block.spec.js
154 lines (136 loc) · 5.47 KB
/
test-block.spec.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
/* globals describe, it */
import { assert } from 'aegir/chai'
import * as main from '../src/block.js'
import * as codec from '../src/codecs/json.js'
import { sha256 as hasher } from '../src/hashes/sha2.js'
import { CID, bytes } from '../src/index.js'
const fixture = { hello: 'world' }
const link = CID.parse('bafyreidykglsfhoixmivffc5uwhcgshx4j465xwqntbmu43nb2dzqwfvae')
const buff = bytes.fromString('sadf')
describe('block', () => {
it('basic encode/decode roundtrip', async () => {
const block = await main.encode({ value: fixture, codec, hasher })
const block2 = await main.decode({ bytes: block.bytes, codec, hasher })
assert.deepStrictEqual(block.cid.equals(block2.cid), true)
assert.deepStrictEqual(block.cid.equals(block2.cid), true)
assert.deepStrictEqual(fixture, block2.value)
const block3 = await main.create({ bytes: block.bytes, cid: block.cid, codec, hasher })
assert.deepStrictEqual(block3.cid.equals(block2.cid), true)
})
it('createUnsafe', async () => {
const block = await main.encode({ value: fixture, codec, hasher })
const block2 = main.createUnsafe({ bytes: block.bytes, cid: block.cid, codec })
assert.deepStrictEqual(block.cid.equals(block2.cid), true)
})
describe('reader', () => {
const value = {
link,
nope: 'skip',
arr: [link],
obj: { arr: [{ obj: {} }] },
// @ts-expect-error - 'string' is not assignable to parameter of type 'ArrayLike<number>'
bytes: Uint8Array.from('1234')
}
// @ts-expect-error - 'boolean' is not assignable to type 'CID'
const block = main.createUnsafe({ value, codec, hasher, cid: true, bytes: true })
it('links', () => {
const expected = ['link', 'arr/0']
for (const [path, cid] of block.links()) {
assert.deepStrictEqual(path, expected.shift())
assert.deepStrictEqual(cid.toString(), link.toString())
}
})
it('tree', () => {
const expected = ['link', 'nope', 'arr', 'arr/0', 'obj', 'obj/arr', 'obj/arr/0', 'obj/arr/0/obj', 'bytes']
for (const path of block.tree()) {
assert.deepStrictEqual(path, expected.shift())
}
})
it('get', () => {
let ret = block.get('link/test')
assert.deepStrictEqual(ret.remaining, 'test')
assert.deepStrictEqual(String(ret.value), link.toString())
ret = block.get('nope')
assert.deepStrictEqual(ret, { value: 'skip' })
})
it('null links/tree', () => {
const block = main.createUnsafe({
value: null,
codec,
hasher,
// @ts-expect-error - 'boolean' is not assignable to type 'ByteView<unknown>'
bytes: true,
// @ts-expect-error - 'boolean' is not assignable to type 'CID'
cid: true
})
// eslint-disable-next-line
for (const x of block.tree()) {
throw new Error(`tree should have nothing, got "${x}"`)
}
// eslint-disable-next-line
for (const x of block.links()) {
throw new Error(`links should have nothing, got "${x}"`)
}
})
})
it('links of a block that is a CID', async () => {
const block = await main.encode({ value: link, codec, hasher })
const links = []
for (const link of block.links()) {
links.push(link)
}
assert.equal(links.length, 1)
assert.equal(links[0][0], '')
assert.equal(links[0][1].toString(), link.toString())
})
it('kitchen sink', () => {
const sink = { one: { two: { arr: [true, false, null], three: 3, buff, link } } }
const block = main.createUnsafe({
value: sink,
codec,
// @ts-expect-error - 'boolean' is not assignable to type 'ByteView<unknown>'
bytes: true,
// @ts-expect-error - 'boolean' is not assignable to type 'CID'
cid: true
})
assert.deepStrictEqual(sink, block.value)
})
describe('errors', () => {
it('constructor missing args', () => {
assert.throws(
// @ts-expect-error - missing properties
() => new main.Block({}),
'Missing required argument'
)
})
it('encode', async () => {
// @ts-expect-error
await assert.isRejected(main.encode({}), 'Missing required argument "value"')
// @ts-expect-error
await assert.isRejected(main.encode({ value: true }), 'Missing required argument: codec or hasher')
})
it('decode', async () => {
// @ts-expect-error
await assert.isRejected(main.decode({}), 'Missing required argument "bytes"')
// @ts-expect-error
await assert.isRejected(main.decode({ bytes: true }), 'Missing required argument: codec or hasher')
})
it('createUnsafe', async () => {
// @ts-expect-error
assert.throws(() => main.createUnsafe({}), 'Missing required argument, must either provide "value" or "codec"')
})
it('create', async () => {
// @ts-expect-error
await assert.isRejected(main.create({}), 'Missing required argument "bytes"')
// @ts-expect-error
await assert.isRejected(main.create({ bytes: true }), 'Missing required argument "hasher"')
const block = await main.encode({ value: fixture, codec, hasher })
const block2 = await main.encode({ value: { ...fixture, test: 'blah' }, codec, hasher })
await assert.isRejected(main.create({ bytes: block.bytes, cid: block2.cid, codec, hasher }), 'CID hash does not match bytes')
})
it('get', async () => {
const block = await main.encode({ value: fixture, codec, hasher })
assert.throws(() => block.get('/asd/fs/dfasd/f'), 'Object has no property at ["asd"]')
})
})
})