-
Notifications
You must be signed in to change notification settings - Fork 5
/
signalling_test.go
218 lines (204 loc) · 7.06 KB
/
signalling_test.go
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
package unixfsnode_test
import (
"fmt"
"strings"
"testing"
"github.com/ipfs/go-unixfsnode"
"github.com/ipld/go-ipld-prime"
"github.com/ipld/go-ipld-prime/codec/dagjson"
"github.com/ipld/go-ipld-prime/traversal/selector/builder"
selectorparse "github.com/ipld/go-ipld-prime/traversal/selector/parse"
"github.com/stretchr/testify/require"
)
// Selectors are tested against JSON expected forms; this doesn't necessarily
// validate that they work as advertised. It's just a sanity check that the
// selectors are being built as expected.
var exploreAllJson = mustDagJson(selectorparse.CommonSelector_ExploreAllRecursively)
// explore interpret-as (~), next (>), match (.), interpreted as unixfs-preload
var matchUnixfsPreloadJson = `{"~":{">":{".":{}},"as":"unixfs-preload"}}`
// explore interpret-as (~), next (>), union (|) of match (.) and explore recursive (R) edge (@) with a depth of 1, interpreted as unixfs
var matchUnixfsEntityJson = `{"~":{">":{"|":[{".":{}},{"R":{":>":{"a":{">":{"@":{}}}},"l":{"depth":1}}}]},"as":"unixfs"}}`
// match interpret-as (~), next (>), match (.), interpreted as unixfs
var matchUnixfsJson = `{"~":{">":{".":{}},"as":"unixfs"}}`
func TestUnixFSPathSelector(t *testing.T) {
testCases := []struct {
name string
path string
expextedSelector string
}{
{
name: "empty path",
path: "",
expextedSelector: matchUnixfsJson,
},
{
name: "single field",
path: "/foo",
expextedSelector: jsonFields(matchUnixfsJson, "foo"),
},
{
name: "multiple fields",
path: "/foo/bar",
expextedSelector: jsonFields(matchUnixfsJson, "foo", "bar"),
},
{
name: "leading slash optional",
path: "foo/bar",
expextedSelector: jsonFields(matchUnixfsJson, "foo", "bar"),
},
{
name: "trailing slash optional",
path: "/foo/bar/",
expextedSelector: jsonFields(matchUnixfsJson, "foo", "bar"),
},
{
// a go-ipld-prime specific thing, not clearly specified by path spec (?)
name: ".. is a field named ..",
path: "/foo/../bar/",
expextedSelector: jsonFields(matchUnixfsJson, "foo", "..", "bar"),
},
{
// a go-ipld-prime specific thing, not clearly specified by path spec
name: "redundant slashes ignored",
path: "foo///bar",
expextedSelector: jsonFields(matchUnixfsJson, "foo", "bar"),
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
sel := unixfsnode.UnixFSPathSelector(tc.path)
require.Equal(t, tc.expextedSelector, mustDagJson(sel))
})
}
}
func TestUnixFSPathSelectorBuilder(t *testing.T) {
testCases := []struct {
name string
path string
target builder.SelectorSpec
matchPath bool
expextedSelector string
}{
{
name: "empty path",
path: "",
target: unixfsnode.ExploreAllRecursivelySelector,
expextedSelector: exploreAllJson,
},
{
name: "empty path shallow (preload)",
path: "",
target: unixfsnode.MatchUnixFSPreloadSelector,
expextedSelector: matchUnixfsPreloadJson,
},
{
name: "empty path shallow (entity)",
path: "",
target: unixfsnode.MatchUnixFSEntitySelector,
expextedSelector: matchUnixfsEntityJson,
},
{
name: "single field",
path: "/foo",
expextedSelector: jsonFields(exploreAllJson, "foo"),
target: unixfsnode.ExploreAllRecursivelySelector,
},
{
name: "single field, match path",
path: "/foo",
expextedSelector: jsonFieldsMatchPoint(exploreAllJson, "foo"),
target: unixfsnode.ExploreAllRecursivelySelector,
matchPath: true,
},
{
name: "single field shallow (preload)",
path: "/foo",
expextedSelector: jsonFields(matchUnixfsPreloadJson, "foo"),
target: unixfsnode.MatchUnixFSPreloadSelector,
},
{
name: "single field shallow (entity)",
path: "/foo",
expextedSelector: jsonFields(matchUnixfsEntityJson, "foo"),
target: unixfsnode.MatchUnixFSEntitySelector,
},
{
name: "multiple fields",
path: "/foo/bar",
expextedSelector: jsonFields(exploreAllJson, "foo", "bar"),
target: unixfsnode.ExploreAllRecursivelySelector,
},
{
name: "multiple fields, match path",
path: "/foo/bar",
expextedSelector: jsonFieldsMatchPoint(exploreAllJson, "foo", "bar"),
target: unixfsnode.ExploreAllRecursivelySelector,
matchPath: true,
},
{
name: "multiple fields shallow",
path: "/foo/bar",
expextedSelector: jsonFields(matchUnixfsPreloadJson, "foo", "bar"),
target: unixfsnode.MatchUnixFSPreloadSelector,
},
{
name: "leading slash optional",
path: "foo/bar",
expextedSelector: jsonFields(exploreAllJson, "foo", "bar"),
target: unixfsnode.ExploreAllRecursivelySelector,
},
{
name: "trailing slash optional",
path: "/foo/bar/",
expextedSelector: jsonFields(exploreAllJson, "foo", "bar"),
target: unixfsnode.ExploreAllRecursivelySelector,
},
// a go-ipld-prime specific thing, not clearly specified by path spec (?)
{
name: ".. is a field named ..",
path: "/foo/../bar/",
expextedSelector: jsonFields(exploreAllJson, "foo", "..", "bar"),
target: unixfsnode.ExploreAllRecursivelySelector,
},
{
// a go-ipld-prime specific thing, not clearly specified by path spec
name: "redundant slashes ignored",
path: "foo///bar",
expextedSelector: jsonFields(exploreAllJson, "foo", "bar"),
target: unixfsnode.ExploreAllRecursivelySelector,
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
sel := unixfsnode.UnixFSPathSelectorBuilder(tc.path, tc.target, tc.matchPath)
require.Equal(t, tc.expextedSelector, mustDagJson(sel))
})
}
}
func jsonFields(target string, fields ...string) string {
var sb strings.Builder
for _, n := range fields {
// explore interpret-as (~) next (>), explore field (f) + specific field (f>), with field name
sb.WriteString(fmt.Sprintf(`{"~":{">":{"f":{"f>":{"%s":`, n))
}
sb.WriteString(target)
sb.WriteString(strings.Repeat(`}}},"as":"unixfs"}}`, len(fields)))
return sb.String()
}
func jsonFieldsMatchPoint(target string, fields ...string) string {
var sb strings.Builder
for _, n := range fields {
// union (|) of match (.) and explore interpret-as (~) next (>), explore field (f) + specific field (f>), with field name
sb.WriteString(fmt.Sprintf(`{"|":[{".":{}},{"~":{">":{"f":{"f>":{"%s":`, n))
}
sb.WriteString(target)
sb.WriteString(strings.Repeat(`}}},"as":"unixfs"}}]}`, len(fields)))
return sb.String()
}
func mustDagJson(n ipld.Node) string {
byts, err := ipld.Encode(n, dagjson.Encode)
if err != nil {
panic(err)
}
return string(byts)
}