forked from skygragon/leetcode-cli
-
Notifications
You must be signed in to change notification settings - Fork 51
/
Copy pathtest_retry.js
94 lines (76 loc) · 2.09 KB
/
test_retry.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
var assert = require('chai').assert;
var rewire = require('rewire');
var log = require('../../lib/log');
var config = rewire('../../lib/config');
var session = rewire('../../lib/session');
var plugin = rewire('../../lib/plugins/retry');
describe('plugin:retry', function() {
var USER = {};
var NEXT = {};
var PROBLEMS = [{id: 0, name: 'name0'}];
before(function() {
log.init();
config.init();
plugin.init();
session.getUser = function() {
return USER;
};
plugin.__set__('config', config);
plugin.__set__('session', session);
plugin.setNext(NEXT);
});
it('should fail if auto login disabled', function(done) {
config.autologin.enable = false;
NEXT.getProblems = function(cb) {
return cb(session.errors.EXPIRED);
};
plugin.getProblems(function(e, problems) {
assert.equal(e, session.errors.EXPIRED);
done();
});
});
it('should retry if session expired', function(done) {
config.autologin.enable = true;
var n = 0;
NEXT.getProblems = function(cb) {
++n;
if (n === 1) return cb(session.errors.EXPIRED);
return cb(null, PROBLEMS);
};
NEXT.login = function(user, cb) {
return cb(null, user);
};
plugin.getProblems(function(e, problems) {
assert.equal(e, null);
assert.equal(problems, PROBLEMS);
done();
});
});
it('should fail if user expired locally', function(done) {
config.autologin.enable = true;
var n = 0;
NEXT.getProblems = function(cb) {
++n;
if (n === 1) return cb(session.errors.EXPIRED);
return cb(null, PROBLEMS);
};
session.getUser = function() {
return null;
};
plugin.getProblems(function(e, problems) {
assert.equal(e, null);
assert.equal(problems, PROBLEMS);
done();
});
});
it('should fail if other errors', function(done) {
config.autologin.enable = true;
NEXT.getProblems = function(cb) {
return cb('unknown error');
};
plugin.getProblems(function(e, problems) {
assert.equal(e, 'unknown error');
done();
});
});
});