-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest.js
229 lines (196 loc) · 5.61 KB
/
test.js
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
var Gun = require("gun");
require('gun/sea');
require('gun/lib/unset');
//hide gun output to express the test output
var logShot = {};
Gun.log = (a, b, c, d) => {
if (logShot[a]) return;
logShot[a] = true;
//console.log(a,b,c,d);
};
Gun.log.once = (a, b, c, d) => {
if (logShot[a]) return;
logShot[a] = true;
Gun.log(a, b, c, d);
};
var gun = Gun( /*['https://gunjs.herokuapp.com/gun']*/ );
var GunFS = require("./index.js");
var DBKEY = "testFS1";
var SETKEY = "test10";
var gunfs = new GunFS(gun.get(DBKEY), SETKEY);
var act = {};
var fireCount = 0;
gunfs.on("*", function(a) {
console.log("path watch fired", a.path, a);
fireCount += 1;
});
//a test = writing
act.a = function() {
console.log("a-------------write");
gunfs.write("/test1.txt", "testing4321", function(err, data) {
if (err) throw err;
if (data) {
console.log("write test good");
act.a2();
}
});
};
act.a2 = function() {
console.log("a2-------------write");
gunfs.write("/folder/test2.txt", "testing1234", function(err, data) {
if (err) throw err;
if (data) {
console.log("write test good");
act.b();
}
});
};
//b test = reading
act.b = function() {
console.log("b-------------read");
gunfs.read("/test1.txt", function(err, data) {
if (err) throw err;
if (data) {
if (data != "testing4321")
throw new Error("read test FAIL (file did not match test data)");
console.log("read test good", data);
act.b2();
}
});
};
act.b2 = function() {
console.log("b2-------------read");
gunfs.read("/folder/test2.txt", function(err, data) {
if (err) throw err;
if (data) {
if (data.value != "testing1234")
throw new Error("read test FAIL (file did not match test data)");
console.log("read test good", data);
act.b3();
}
}, true); //<-- get gun ack
};
act.b3 = function() {
console.log("b3-------------read-fail");
gunfs.read("/folder/test3.txt", function(err, data) {
if (data) {
throw new Error("read test on unknown file FAIL (file should not have data)");
}
else if (err) {
console.log("read test on unknown file good");
act.c();
}
else {
throw new Error("read test on unknown file FAIL (err not given)");
}
}, true);
};
//c test = listing
act.c = function() {
console.log("c-------------list");
gunfs.list(function(err, data) {
if (err) throw err;
if (data) {
if (data.length != 2)
throw new Error("list test count did not = 2");
console.log("list test good", data);
act.c2();
}
});
};
act.c2 = function() {
console.log("c2-------------list-path");
gunfs.list("/folder/", function(err, data) {
if (err) throw err;
if (data) {
if (data.length != 1)
throw new Error("list test count did not = 1");
console.log("list test good", data);
act.d();
}
});
};
//d test = deleting
act.d = function() {
console.log("d-------------delete");
gunfs.delete("/folder/test2.txt", function(err, data) {
if (err) throw err;
console.log("del test good", data);
act.e();
});
};
//e test = existing
act.e = function() {
console.log("e-------------exist");
gunfs.exist("/test1.txt", function(err, data) {
if (err && !data) throw err;
console.log("exist test good", data);
act.f();
});
};
//f test = info
act.f = function() {
console.log("f-------------stats");
gunfs.stats("/test1.txt", function(err, data) {
if (err && !data) throw err;
console.log("stats test good", data);
act.f2();
});
};
act.f2 = function() {
console.log("f2-------------write");
gunfs.write("/test1.txt", "testing4321-update", function(err, data) {
if (err) throw err;
if (data) {
console.log("write test good");
act.f3();
}
});
};
act.f3 = function() {
console.log("f3-------------stats");
gunfs.stats("/test1.txt", function(err, data) {
if (err && !data) throw err;
console.log("stats test good", data);
act.g();
});
};
//g test = deleting test data (cleanup test)
act.g = function() {
console.log("g-------------delete");
gunfs.delete("/test1.txt", function(err, data) {
if (err) throw err;
console.log("del test good", data);
act.h();
});
};
//g test = list test empty (cleanup test check)
act.h = function() {
console.log("h-------------list");
gunfs.list(function(err, data) {
if (err) throw err;
if (data) {
if (data.length > 0)
throw new Error("list test fail, (list should be empty)");
console.log("list test good", data);
act.done();
}
});
};
act.done = function(){
console.log("done-------------events");
if (fireCount != 5)// a, a2, d, f2, g
throw new Error("Events count did not match");
console.log("EVENTS FIRED", fireCount);
process.exit();
};
//have no files to start test
gunfs.list(function(err, data) {
if(err) throw err;
if (data)
if (data.length > 0)
throw new Error("list test fail, (list should be empty)");
var testTimer = 3000;
setTimeout(act.a, testTimer);
console.log("test starting in " + (testTimer / 1000) + "sec");
});