-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathtest.js
73 lines (67 loc) · 2.28 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
/* eslint-env mocha */
import * as chai from 'chai'
import { sha256 } from 'multiformats/hashes/sha2'
import * as Block from 'multiformats/block'
import { codecs } from './codecs.js'
import {
fixtureDirectories,
negativeFixtureCodecs,
negativeFixturesEncode,
negativeFixturesDecode,
loadFixture
} from './util.js'
import { bytes } from 'multiformats'
const { assert } = chai
const utfEncoder = new TextEncoder()
describe('Codec fixtures', () => {
for (const { name, url } of fixtureDirectories()) {
it(name, async () => {
const data = await loadFixture(url)
for (const [fromCodec, { bytes }] of Object.entries(data)) {
const value = codecs[fromCodec].codec.decode(bytes)
for (const [toCodec, { cid }] of Object.entries(data)) {
const block = await Block.encode({ value, codec: codecs[toCodec].codec, hasher: sha256 })
assert.equal(block.cid.toString(), cid, `CIDs match for data decoded from ${fromCodec} encoded as ${toCodec}`)
}
}
})
}
})
describe.only('Codec negative fixtures', () => {
for (const codec of negativeFixtureCodecs()) {
describe(codec, () => {
const { encode, decode } = codecs[codec].codec
for (const fixtures of negativeFixturesEncode(codec)) {
for (const fixture of fixtures) {
it(fixture.name, () => {
const { name, error } = fixture
if (!'dag-json' in fixture) {
// TODO: when we need it, probably hex decode for others
assert.fail('can\'t deal with fixture that doesn\'t have dag-json input')
}
const obj = codecs['dag-json'].codec.decode(utfEncoder.encode(JSON.stringify(fixture['dag-json'])))
try {
encode(obj)
assert.fail('did not error')
} catch (e) {
assert.strictEqual(e.message, error)
}
})
}
}
for (const fixtures of negativeFixturesDecode(codec)) {
for (const { name, hex, error } of fixtures) {
it(name, () => {
const byts = bytes.fromHex(hex)
try {
decode(byts)
assert.fail('did not error')
} catch (e) {
assert.include(e.message, error)
}
})
}
}
})
}
})