-
Notifications
You must be signed in to change notification settings - Fork 47
/
test-link.spec.js
149 lines (119 loc) · 3.84 KB
/
test-link.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
/* globals describe, it */
import { assert } from 'aegir/chai'
import { sha256 } from '../src/hashes/sha2.js'
import * as Link from '../src/link.js'
const utf8 = new TextEncoder()
const h1 = 'QmdfTbBqBPQ7VNxZEYEj14VmRuZBkqFbiwReogJgS1zR1n'
const h4 = 'bafyreidykglsfhoixmivffc5uwhcgshx4j465xwqntbmu43nb2dzqwfvae'
const CBOR = 0x71
// eslint-disable-next-line
const SHA256 = sha256.code
const sh1 = /** @type {Link.MultihashDigest<typeof sha256.code>} */ (
Link.parse(h4).multihash
)
describe('Link', () => {
it('isLink', () => {
assert.equal(Link.isLink(0), false)
assert.equal(Link.isLink(false), false)
})
describe('create', () => {
it('create v1', async () => {
const hash = await sha256.digest(utf8.encode('abc'))
const link = Link.create(0x71, hash)
/** @type {0x71} */
const code = link.code
assert.deepStrictEqual(code, 0x71)
/** @type {1} */
const version = link.version
assert.deepEqual(version, 1)
/** @type {Link.MultihashDigest<typeof sha256.code>}> */
const multihash = link.multihash
assert.deepStrictEqual(multihash, hash)
})
it('create v0', async () => {
const hash = await sha256.digest(utf8.encode('abc'))
const link = Link.createLegacy(hash)
/** @type {0x70} */
const code = link.code
assert.deepStrictEqual(code, 0x70)
/** @type {0} */
const version = link.version
assert.deepEqual(version, 0)
/** @type {Link.MultihashDigest<typeof sha256.code>}> */
const multihash = link.multihash
assert.deepStrictEqual(multihash, hash)
})
})
describe('parse', () => {
it('can parse any string', () => {
const link = Link.parse(h1)
/** @type {Link.Link<unknown, typeof CBOR, typeof SHA256, 1>} */
// @ts-expect-error - types can not be inferred
const t1 = link
assert.ok(t1)
// it is possible to manually cast
const t2 = /** @type {Link.LegacyLink<unknown>} */ (link)
assert.ok(t2)
})
it('parse retains type info', () => {
const original = Link.create(CBOR, sh1)
const source = Link.format(original)
const link = Link.parse(source)
assert.equal(original.equals(link), true, 'format -> parse roundtrips')
// ensure that type info is retained
/** @type {Link.Link<unknown, typeof CBOR, typeof SHA256, 1>} */
const t1 = link
assert.ok(t1)
// ensurate that you can't cast incorrectly
const t2 =
// @ts-expect-error - version is 1 not 0
/** @type {Link.Link<unknown, typeof CBOR, typeof SHA256, 0>} */ (link)
assert.ok(t2)
})
})
describe('toJSON', () => {
assert.deepStrictEqual(Link.toJSON(Link.parse(h1)), {
'/': h1
})
assert.deepStrictEqual(Link.toJSON(Link.parse(h4)), {
'/': h4
})
})
describe('fromJSON', () => {
assert.deepStrictEqual(Link.parse(h1), Link.fromJSON({
'/': h1
}))
assert.deepStrictEqual(Link.parse(h1), Link.fromJSON({
'/': h1,
// @ts-expect-error
foo: 1
}))
assert.deepStrictEqual(Link.parse(h4), Link.fromJSON({
'/': h4
}))
})
describe('JSON.stringify', () => {
assert.equal(JSON.stringify(Link.parse(h1)), JSON.stringify({
'/': h1
}))
assert.equal(JSON.stringify(Link.parse(h4)), JSON.stringify({
'/': h4
}))
})
})
describe('decode', () => {
it('decode', async () => {
const hash = await sha256.digest(utf8.encode('abc'))
const { bytes } = Link.create(0x71, hash)
const link = Link.decode(bytes)
/** @type {0x71} */
const code = link.code
assert.deepStrictEqual(code, 0x71)
/** @type {1} */
const version = link.version
assert.deepEqual(version, 1)
/** @type {Link.MultihashDigest<typeof sha256.code>}> */
const multihash = link.multihash
assert.deepStrictEqual(multihash, hash)
})
})