-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathinstance.test.ts
118 lines (109 loc) · 3.16 KB
/
instance.test.ts
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
import test from "ava"
import {
Schema,
types,
Instance,
values,
encodeInstance,
} from "../src/index.js"
test("encode nano instance", (t) => {
const schema = new Schema({
"http://example.com/foo": types.boolean,
})
const instance = new Instance(schema, {
"http://example.com/foo": [
{ id: 0, value: values.boolean(true) },
{ id: 1, value: values.boolean(false) },
{ id: 2, value: values.boolean(true) },
],
})
t.deepEqual(
encodeInstance(instance),
new Uint8Array([
0x01, // version number
0x03, // number of elements in the class
0x00, // the delta-encoded id of the first element
0x01, // the value of the first element ("true")
0x00, // the delta-encoded id of the second element
0x00, // the value of the second element ("false")
0x00, // the delta-encoded id of the third element
0x01, // the value of the third element ("true")
])
)
})
test("encode micro instance", (t) => {
const schema = new Schema({
"http://example.com/a": types.product({
"http://example.com/a/a": types.u8,
"http://example.com/a/b": types.boolean,
}),
"http://example.com/b": types.coproduct({
"http://example.com/b/a": types.bytes,
"http://example.com/b/b": types.unit,
"http://example.com/b/c": types.uri(),
}),
})
const instance = new Instance(schema, {
"http://example.com/a": [
{
id: 0,
value: values.product({
"http://example.com/a/a": values.u8(0xff),
"http://example.com/a/b": values.boolean(false),
}),
},
],
"http://example.com/b": [
{
id: 0,
value: values.coproduct(
"http://example.com/b/a",
values.bytes(new Uint8Array([0x0f, 0xee, 0x12, 0x00]))
),
},
{
id: 1,
value: values.coproduct("http://example.com/b/b", values.unit()),
},
{
id: 2,
value: values.coproduct("http://example.com/b/b", values.unit()),
},
{
id: 3,
value: values.coproduct(
"http://example.com/b/c",
values.uri(
"dweb:/ipfs/bafybeiczsscdsbs7ffqz55asqdf3smv6klcw3gofszvwlyarci47bgf354"
)
),
},
],
})
// instance.schema
t.deepEqual(
encodeInstance(instance),
new Uint8Array([
0x01, // version number
0x01, // number of elements in the first class
0x00, // the delta-encoded id of the first element
0xff, // the value of the first component (u8)
0x00, // the value of the second component (boolean)
0x04, // the number of elements in the second class
0x00, // the delta-encoded id of the first element
0x00, // the index of option for the first element's value
0x04, // the number of bytes in the binary literal
...[0x0f, 0xee, 0x12, 0x00], // the raw bytes of the binary literal
0x00, // the delta-encoded id of the second element
0x01, // the index of the option for the second element's value
0x00, // the delta-encoded id of the third element
0x01, // the index of the option for the third element's value
0x00, // the delta-encoded id of the fourth element
0x02, // the index of the option for the fourth element's value
0x46, // the length of the URI in bytes (70)s
...new TextEncoder().encode(
"dweb:/ipfs/bafybeiczsscdsbs7ffqz55asqdf3smv6klcw3gofszvwlyarci47bgf354"
),
])
)
})