1
1
/* globals describe, it */
2
- import sinon from 'sinon'
3
2
import * as codec from 'multiformats/codecs/json'
4
3
import * as dagPB from '@ipld/dag-pb'
5
4
import { sha256 as hasher } from 'multiformats/hashes/sha2'
6
5
import * as main from 'multiformats/block'
7
- import { walk } from 'multiformats/traverse'
6
+ import { walk } from 'multiformats/traversal'
7
+ import { deepStrictEqual as same } from 'assert'
8
8
9
9
const test = it
10
10
const { createLink, createNode } = dagPB
11
11
12
- describe ( 'traverse ' , ( ) => {
12
+ describe ( 'traversal ' , ( ) => {
13
13
describe ( 'walk' , async ( ) => {
14
14
// Forming the following DAG for testing
15
15
// A
@@ -61,18 +61,25 @@ describe('traverse', () => {
61
61
throw new Error ( 'unmatched CID' )
62
62
}
63
63
64
+ const loadWrapper = ( load , arr = [ ] ) => ( cid ) => {
65
+ arr . push ( cid . toString ( ) )
66
+ return load ( cid )
67
+ }
68
+
64
69
test ( 'block with no links' , async ( ) => {
65
70
// Test Case 1
66
71
// Input DAG
67
72
// D
68
73
//
69
74
// Expect load to be called with D
70
-
71
- const spy = sinon . spy ( load )
72
-
73
- await walk ( { cid : cidD , load : spy } )
74
- sinon . assert . callCount ( spy , 1 )
75
- sinon . assert . calledOnceWithExactly ( spy , cidD )
75
+ const expectedCallArray = [ cidD . toString ( ) ]
76
+ const callArray = [ ]
77
+
78
+ await walk ( { cid : cidD , load : loadWrapper ( load , callArray ) } )
79
+
80
+ expectedCallArray . forEach ( ( value , index ) => {
81
+ same ( value , callArray [ index ] )
82
+ } )
76
83
} )
77
84
test ( 'block with links' , async ( ) => {
78
85
// Test Case 2
@@ -82,15 +89,14 @@ describe('traverse', () => {
82
89
// D E
83
90
//
84
91
// Expect load to be called with C, then D, then E
85
-
86
- const spy = sinon . spy ( load )
87
-
88
- await walk ( { cid : cidC , load : spy } )
89
-
90
- sinon . assert . callCount ( spy , 3 )
91
- sinon . assert . calledWith ( spy . firstCall , cidC )
92
- sinon . assert . calledWith ( spy . secondCall , cidD )
93
- sinon . assert . calledWith ( spy . thirdCall , cidE )
92
+ const expectedCallArray = [ cidC . toString ( ) , cidD . toString ( ) , cidE . toString ( ) ]
93
+ const callArray = [ ]
94
+
95
+ await walk ( { cid : cidC , load : loadWrapper ( load , callArray ) } )
96
+
97
+ expectedCallArray . forEach ( ( value , index ) => {
98
+ same ( value , callArray [ index ] )
99
+ } )
94
100
} )
95
101
test ( 'block with matching links' , async ( ) => {
96
102
// Test Case 3
@@ -100,14 +106,14 @@ describe('traverse', () => {
100
106
// D D
101
107
//
102
108
// Expect load to be called with B, then D
103
-
104
- const spy = sinon . spy ( load )
105
-
106
- await walk ( { cid : cidB , load : spy } )
107
-
108
- sinon . assert . callCount ( spy , 2 )
109
- sinon . assert . calledWith ( spy . firstCall , cidB )
110
- sinon . assert . calledWith ( spy . secondCall , cidD )
109
+ const expectedCallArray = [ cidB . toString ( ) , cidD . toString ( ) ]
110
+ const callArray = [ ]
111
+
112
+ await walk ( { cid : cidB , load : loadWrapper ( load , callArray ) } )
113
+
114
+ expectedCallArray . forEach ( ( value , index ) => {
115
+ same ( value , callArray [ index ] )
116
+ } )
111
117
} )
112
118
test ( 'depth first with duplicated block' , async ( ) => {
113
119
// Test Case 4
@@ -117,18 +123,22 @@ describe('traverse', () => {
117
123
// B C
118
124
// / \ / \
119
125
// D D D E
126
+ //
120
127
// Expect load to be called with A, then B, then D, then C, then E
121
-
122
- const spy = sinon . spy ( load )
123
-
124
- await walk ( { cid : cidA , load : spy } )
125
-
126
- sinon . assert . callCount ( spy , 5 )
127
- sinon . assert . calledWith ( spy . firstCall , cidA )
128
- sinon . assert . calledWith ( spy . secondCall , cidB )
129
- sinon . assert . calledWith ( spy . thirdCall , cidD )
130
- sinon . assert . calledWith ( spy . getCall ( - 2 ) , cidC )
131
- sinon . assert . calledWith ( spy . lastCall , cidE )
128
+ const expectedCallArray = [
129
+ cidA . toString ( ) ,
130
+ cidB . toString ( ) ,
131
+ cidD . toString ( ) ,
132
+ cidC . toString ( ) ,
133
+ cidE . toString ( ) ,
134
+ ]
135
+ const callArray = [ ]
136
+
137
+ await walk ( { cid : cidA , load : loadWrapper ( load , callArray ) } )
138
+
139
+ expectedCallArray . forEach ( ( value , index ) => {
140
+ same ( value , callArray [ index ] )
141
+ } )
132
142
} )
133
143
} )
134
144
} )
0 commit comments