15
15
* You should have received a copy of the GNU Affero General Public License
16
16
* along with this program. If not, see <http://www.gnu.org/licenses/>.
17
17
*/
18
- import chai , { expect } from 'chai' ;
19
18
import fs from 'node:fs' ;
20
- import sinon , { SinonSandbox } from 'sinon ' ;
21
- import sinonChai from 'sinon-chai ' ;
19
+ import { strict as assert } from 'node:assert ' ;
20
+ import { afterEach , before , describe , it , mock } from 'node:test ' ;
22
21
import * as winston from 'winston' ;
23
22
24
23
import { ArweaveChunkSourceStub } from '../../test/stubs.js' ;
@@ -27,43 +26,32 @@ import { FsChunkDataStore } from '../store/fs-chunk-data-store.js';
27
26
import { Chunk , ChunkData , ChunkDataStore } from '../types.js' ;
28
27
import { ReadThroughChunkDataCache } from './read-through-chunk-data-cache.js' ;
29
28
30
- chai . use ( sinonChai ) ;
31
29
const B64_DATA_ROOT = 'wRq6f05oRupfTW_M5dcYBtwK5P8rSNYu20vC6D_o-M4' ;
32
30
const TX_SIZE = 256000 ;
33
31
const ABSOLUTE_OFFSET = 51530681327863 ;
34
32
const RELATIVE_OFFSET = 0 ;
35
33
36
- describe ( 'ReadThroughChunkDataCache' , ( ) => {
37
- let log : winston . Logger ;
38
- let chunkSource : ArweaveChunkSourceStub ;
39
- let chunkDataStore : ChunkDataStore ;
40
- let chunkCache : ReadThroughChunkDataCache ;
41
- let sandbox : SinonSandbox ;
34
+ let log : winston . Logger ;
35
+ let chunkSource : ArweaveChunkSourceStub ;
36
+ let chunkDataStore : ChunkDataStore ;
37
+ let chunkCache : ReadThroughChunkDataCache ;
42
38
43
- before ( ( ) => {
44
- log = winston . createLogger ( { silent : true } ) ;
45
- chunkSource = new ArweaveChunkSourceStub ( ) ;
46
- chunkDataStore = new FsChunkDataStore ( {
47
- log,
48
- baseDir : 'data/chunks' ,
49
- } ) ;
50
- chunkCache = new ReadThroughChunkDataCache ( {
51
- log,
52
- chunkSource,
53
- chunkDataStore,
54
- } ) ;
39
+ before ( ( ) => {
40
+ log = winston . createLogger ( { silent : true } ) ;
41
+ chunkSource = new ArweaveChunkSourceStub ( ) ;
42
+ chunkDataStore = new FsChunkDataStore ( {
43
+ log,
44
+ baseDir : 'data/chunks' ,
55
45
} ) ;
56
-
57
- beforeEach ( ( ) => {
58
- sandbox = sinon . createSandbox ( ) ;
59
- } ) ;
60
-
61
- afterEach ( ( ) => {
62
- sandbox . restore ( ) ;
46
+ chunkCache = new ReadThroughChunkDataCache ( {
47
+ log,
48
+ chunkSource,
49
+ chunkDataStore,
63
50
} ) ;
51
+ } ) ;
64
52
53
+ describe ( 'ReadThroughChunkDataCache' , ( ) => {
65
54
// TODO remove mocks from tests
66
-
67
55
describe ( 'getChunkDataByAny' , ( ) => {
68
56
let mockedChunk : Chunk ;
69
57
let mockedChunkData : ChunkData ;
@@ -76,9 +64,9 @@ describe('ReadThroughChunkDataCache', () => {
76
64
) ,
77
65
) ;
78
66
const txPath = fromB64Url ( jsonChunk . tx_path ) ;
79
- const dataRootBuffer = txPath . slice ( - 64 , - 32 ) ;
67
+ const dataRootBuffer = txPath . subarray ( - 64 , - 32 ) ;
80
68
const dataPath = fromB64Url ( jsonChunk . data_path ) ;
81
- const hash = dataPath . slice ( - 64 , - 32 ) ;
69
+ const hash = dataPath . subarray ( - 64 , - 32 ) ;
82
70
mockedChunk = {
83
71
tx_path : txPath ,
84
72
data_root : dataRootBuffer ,
@@ -97,55 +85,68 @@ describe('ReadThroughChunkDataCache', () => {
97
85
} ;
98
86
} ) ;
99
87
88
+ afterEach ( ( ) => {
89
+ mock . restoreAll ( ) ;
90
+ } ) ;
91
+
100
92
it ( 'should fetch chunk data from cache when available' , async ( ) => {
101
- // mock the file exists
102
- const storeGetSpy = sandbox
103
- . stub ( chunkDataStore , 'get' )
104
- . resolves ( mockedChunkData ) ;
105
- const networkSpy = sandbox . spy ( chunkSource , 'getChunkByAny' ) ;
93
+ mock . method ( chunkDataStore , 'get' , async ( ) => mockedChunkData ) ;
94
+ mock . method ( chunkSource , 'getChunkByAny' ) ;
95
+
106
96
await chunkCache . getChunkDataByAny (
107
97
TX_SIZE ,
108
98
ABSOLUTE_OFFSET ,
109
99
B64_DATA_ROOT ,
110
100
0 ,
111
101
) ;
112
- expect ( networkSpy ) . not . to . have . been . called ;
113
- expect ( storeGetSpy ) . to . have . been . called ;
102
+
103
+ assert . deepEqual ( ( chunkSource . getChunkByAny as any ) . mock . callCount ( ) , 0 ) ;
104
+ assert . deepEqual ( ( chunkDataStore . get as any ) . mock . callCount ( ) , 1 ) ;
114
105
} ) ;
115
106
116
107
it ( 'should fetch chunk data from network when not in local cache' , async ( ) => {
117
- // mock file does not exist
118
- const storeHasSpy = sandbox . stub ( chunkDataStore , 'has' ) . resolves ( false ) ;
119
- const storeGetSpy = sandbox . spy ( chunkDataStore , 'get' ) ;
120
- const networkSpy = sandbox
121
- . stub ( chunkSource , 'getChunkByAny' )
122
- . resolves ( mockedChunk ) ;
108
+ const chuunkDataStoreHasSpy = mock . method (
109
+ chunkDataStore ,
110
+ 'has' ,
111
+ async ( ) => false ,
112
+ ) ;
113
+ const chunkDataStoreGetSpy = mock . method ( chunkDataStore , 'get' ) ;
114
+ const networkSpy = mock . method (
115
+ chunkSource ,
116
+ 'getChunkByAny' ,
117
+ async ( ) => mockedChunk ,
118
+ ) ;
123
119
await chunkCache . getChunkDataByAny (
124
120
TX_SIZE ,
125
121
ABSOLUTE_OFFSET ,
126
122
B64_DATA_ROOT ,
127
123
0 ,
128
124
) ;
129
- expect ( storeGetSpy ) . to . have . been . called ;
130
- expect ( storeHasSpy ) . to . have . been . called ;
131
- expect ( networkSpy ) . to . have . been . called ;
125
+ assert . deepEqual ( chunkDataStoreGetSpy . mock . callCount ( ) , 1 ) ;
126
+ assert . deepEqual ( chuunkDataStoreHasSpy . mock . callCount ( ) , 1 ) ;
127
+ assert . deepEqual ( networkSpy . mock . callCount ( ) , 1 ) ;
132
128
} ) ;
133
129
134
130
it ( 'should fetch chunk data from network when an error occurs fetching from local cache' , async ( ) => {
135
- const storeHasSpy = sandbox . stub ( chunkDataStore , 'has' ) . rejects ( ) ;
136
- const storeGetSpy = sandbox . spy ( chunkDataStore , 'get' ) ;
137
- const networkSpy = sandbox
138
- . stub ( chunkSource , 'getChunkByAny' )
139
- . resolves ( mockedChunk ) ;
131
+ const storeHasSpy = mock . method ( chunkDataStore , 'has' , async ( ) => {
132
+ throw new Error ( 'Error' ) ;
133
+ } ) ;
134
+ const storeGetSpy = mock . method ( chunkDataStore , 'get' ) ;
135
+ const networkSpy = mock . method (
136
+ chunkSource ,
137
+ 'getChunkByAny' ,
138
+ async ( ) => mockedChunk ,
139
+ ) ;
140
140
await chunkCache . getChunkDataByAny (
141
141
TX_SIZE ,
142
142
ABSOLUTE_OFFSET ,
143
143
B64_DATA_ROOT ,
144
144
0 ,
145
145
) ;
146
- expect ( storeGetSpy ) . to . have . been . called ;
147
- expect ( storeHasSpy ) . to . have . been . called ;
148
- expect ( networkSpy ) . to . have . been . called ;
146
+
147
+ assert . deepEqual ( storeGetSpy . mock . callCount ( ) , 1 ) ;
148
+ assert . deepEqual ( storeHasSpy . mock . callCount ( ) , 1 ) ;
149
+ assert . deepEqual ( networkSpy . mock . callCount ( ) , 1 ) ;
149
150
} ) ;
150
151
} ) ;
151
152
} ) ;
0 commit comments