-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.ts
299 lines (266 loc) · 10.9 KB
/
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
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
import IpfsApiHelper from './index';
import * as IPFS from 'ipfs';
import { expect } from 'chai';
import * as rimraf from 'rimraf';
describe('ipfs-connector-utils', function () {
let helperInstance: IpfsApiHelper;
let provider: any;
const longText = `Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been` +
`the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and` +
`scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into` +
`electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of` +
`Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like` +
`Aldus PageMaker including versions of Lorem Ipsum.`;
this.timeout(100000);
before(function (done) {
provider = new IPFS({ repo: 'test-repo', start: true });
provider.on('start', () => {
helperInstance = new IpfsApiHelper(provider);
});
setTimeout(() => {
expect(helperInstance).to.exist;
done();
}, 9500);
});
it('adds an object to ipfs', function () {
return helperInstance.add({ data: '{}' })
.then((cid) => {
expect(cid).to.exist;
expect(cid).to.have.property('multihash');
expect(cid).to.have.property('codec');
expect(cid).to.have.property('version');
});
});
it('transforms object to buffer', function () {
const x = { a: 1 };
const expected = Buffer.from(JSON.stringify(x));
const actual = IpfsApiHelper.toDataBuffer(x);
expect(actual.toString()).to.equal(expected.toString());
});
it('preserves buffer', function () {
const initial = Buffer.from(JSON.stringify({ q: 1 }));
const actual = IpfsApiHelper.toDataBuffer(initial);
expect(actual.toString()).to.equal(initial.toString());
});
it('gets data from node', function() {
return helperInstance.add({q: {v: 1}})
.then((cid1) => helperInstance.get(cid1, '/q/v'))
.then(result => {
expect(result).to.exist;
expect(result).to.have.property('value');
expect(result.value).to.equal(1);
});
});
it('creates a cid with a linked property from data', function() {
return helperInstance.addLinkFrom({firstName: 'John', lastName: 'Doe'}, 'name')
.then(cid => {
expect(cid).to.exist;
expect(cid).to.have.property('multihash');
expect(cid).to.have.property('codec');
expect(cid).to.have.property('version');
return helperInstance.get(cid, '/name/firstName');
})
.then(result => {
expect(result.value).to.equal('John');
})
.catch(err => expect(err).to.not.exist);
});
it('creates a cid link from encoded string', function() {
return helperInstance.addLink('zdpuB2CAUgUbyJ4s8SmqcGGaGfs7bG337dDNty8NSCR3fhgRw', 'details')
.then(cid => {
expect(cid).to.exist;
expect(cid).to.have.property('multihash');
expect(cid).to.have.property('codec');
expect(cid).to.have.property('version');
return helperInstance.get(cid, '/details/lastName');
})
.then(result => {
expect(result.value).to.equal('Doe');
})
.catch(err => {
console.log('error', err);
expect(err).to.not.exist;
});
});
it('fails to create a cid link from bad encoded string', function() {
return helperInstance.addLink('zdpuAzm672f1UYgfPuP3fDifw2wb4DiJVaheg3VMXeFuZxLxM', 'details')
.then(cid => {
expect(cid).to.not.exist;
})
.catch(err => { expect(err).to.exist; });
});
/*
it('adds buffer instance to ipfs', function () {
const actual = Buffer.from(JSON.stringify({ a: 1, b: 2 }));
return helperInstance.add(actual).then(node => {
expect(node).to.have.property('hash');
});
});
it('adds plain text to ipfs', function () {
return helperInstance.add(Buffer.from('testing test'))
.then((node) => {
expect(node).to.have.property('hash');
return helperInstance.get(node.hash)
.then((data: Buffer) => {
expect(data).to.exist;
})
})
});
it('adds raw buffer using api.addFile', function () {
const buf = Buffer.from(JSON.stringify({ a: 1, b: 2, c: 3 }));
return helperInstance.addFile(buf).then((node: any) => {
expect(node).to.have.property('hash');
});
});
it('updates from existing object', function () {
const initialObj = { a: 1, b: 2 };
return helperInstance.add(initialObj)
.then((node) => {
const patchAttr = { b: 3 };
ipfsHash1 = node.hash;
expect(node).to.exist;
return helperInstance.updateObject(node.hash, patchAttr).then((result: any) => {
result.data = JSON.parse(result.data);
expect(result.data.a).to.equal(initialObj.a);
expect(result.data.b).to.equal(patchAttr.b);
expect(result.multihash).to.exist;
});
});
});
it('constructs object link from hash', function () {
return helperInstance.add({ a: 1, b: 2 })
.then((node) => {
expect(node).to.exist;
return helperInstance
.addLinkFrom({ coco: 1 }, 'testLink', node.hash)
.then((result) => {
expect(result.links.length).to.be.above(0);
});
});
});
it('creates node with links', function () {
const links = [{ name: 'testFile', size: 12, multihash: 'QmPMH5GFmLP2oU8dK7i4iWJyX6FpgeK3gT6ZC6xLLZQ9cW' },
{ name: 'testLink', size: 12, multihash: ipfsHash1 }];
return helperInstance
.createNode({ test: 2 }, links)
.then((result: any) => {
ipfsHash = result.hash;
expect(result).to.have.property('hash');
})
});
it('gets object links', function () {
return helperInstance.getLinks(ipfsHash)
.then((result: any) => {
expect(result.length).to.equal(2);
rawObject = result;
});
});
it('transforms raw object', function () {
return helperInstance.DAGNode.createAsync(Buffer.from(JSON.stringify({ test: 'test' })))
.then((rawObject: any) => {
const result = IpfsApiHelper.fromRawObject(rawObject);
expect(result).to.have.property('data');
})
});
it('gets data from raw object', function () {
return helperInstance.DAGNode.createAsync(Buffer.from(JSON.stringify({ test: 'test1' })))
.then((rawObject: any) => {
const result = IpfsApiHelper.fromRawData(rawObject);
expect(result).to.have.property('test');
})
});
it('resolves a given link path', function () {
return helperInstance
.addLinkFrom({ test: true }, 'firstLink', ipfsHash)
.then((result) => {
expect(result).to.exist;
return helperInstance.addLink(
{ name: 'lastLink', hash: result.multihash, size: result.size },
ipfsHash
).then((patched: any) => {
return helperInstance.findLinkPath(patched.multihash, ['lastLink', 'firstLink'])
.then((final: any) => {
expect(final).to.exist;
return helperInstance.get(final[0].multihash)
.then((finalData: any) => {
expect(finalData).to.have.property('test');
})
});
});
});
});
it('fails to find link path', function () {
return helperInstance.findLinkPath(ipfsHash, [])
.then((data: any) => {
expect(data).to.not.exist;
})
.catch((err: any) => {
expect(err).to.exist;
})
});
it('should force to file api add', function () {
return helperInstance.addFile(Buffer.from('TEST'))
.then((node: any) => {
return helperInstance.getFile(node.hash)
.then((result) => {
expect(result.toString()).to.eql('TEST');
});
});
});
it('should have a default encoding', function () {
expect(helperInstance.ENCODING).to.exist;
});
it('should have access to DAGNode and DAGLink', function () {
expect(helperInstance.DAGNode).to.have.property('createAsync');
expect(helperInstance.DAGLink).to.have.property('createAsync');
});
it('sets max ipfs object size', function () {
helperInstance.setObjectMaxSize(100);
expect(helperInstance.OBJECT_MAX_SIZE).to.equal(100);
});
it('sets response timeout', function () {
helperInstance.setRequestTimeout(3000);
expect(helperInstance.REQUEST_TIMEOUT).to.equal(3000);
});
it('adds object using file api', function () {
return helperInstance.add({ q: longText })
.then((node) => {
expect(node).to.have.property('hash');
return helperInstance.get(node.hash, true)
.then((result: Buffer) => {
expect(JSON.parse(result.toString())).deep.equal({ q: longText });
});
});
});
it.skip('gets hash stats', function () {
return helperInstance
.getStats(statHash)
.then((stats: any) => {
console.log(stats);
expect(stats).to.have.property('DataSize');
});
});
it('returns same hash on add if checkIfHash is present', function () {
return helperInstance
.add(statHash, false, true)
.then((result: any) => {
console.log(statHash, result);
expect(result).to.have.property('hash');
expect(result).to.have.property('size');
});
});
it('sets encoding for ipfs hash', function (done) {
helperInstance.setEncoding('base64');
expect(helperInstance.ENCODING).to.equal('base64');
provider.stop(() => done());
});*/
after(function (done) {
rimraf('./test-repo', (err: any) => {
if (err) {
console.log(err);
}
done();
process.exit(); // weird hang
});
});
});