forked from skygragon/leetcode-cli
-
Notifications
You must be signed in to change notification settings - Fork 52
/
Copy pathtest_leetcode.js
727 lines (623 loc) · 22.4 KB
/
test_leetcode.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
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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
'use strict';
const _ = require('underscore');
const assert = require('chai').assert;
const nock = require('nock');
const rewire = require('rewire');
const config = require('../../lib/config');
const chalk = require('../../lib/chalk');
const log = require('../../lib/log');
const plugin = rewire('../../lib/plugins/leetcode');
const session = rewire('../../lib/session');
describe('plugin:leetcode', function() {
const USER = {hash: 'abcdef'};
const PROBLEM = {
id: 389,
name: 'Find the Difference',
slug: 'find-the-difference',
link: 'https://leetcode.com/problems/find-the-difference',
locked: false,
file: '/dev/null'
};
const SUBMISSION = {
id: '73790064',
lang: 'cpp',
runtime: '9 ms',
path: '/submissions/detail/73790064/',
state: 'Accepted'
};
before(function() {
log.init();
config.init();
chalk.init();
plugin.init();
session.getUser = () => USER;
session.saveUser = () => {};
plugin.__set__('session', session);
});
describe('#login', function() {
it('should ok', function(done) {
nock('https://leetcode.com')
.get('/accounts/login/')
.reply(200, '', { 'Set-Cookie': [
'csrftoken=LOGIN_CSRF_TOKEN; Max-Age=31449600; Path=/; secure'
]});
nock('https://leetcode.com')
.post('/accounts/login/')
.reply(302, '', {
'Set-Cookie': [
'csrftoken=SESSION_CSRF_TOKEN; Max-Age=31449600; Path=/; secure',
'LEETCODE_SESSION=SESSION_ID; Max-Age=31449600; Path=/; secure'
]});
nock('https://leetcode.com')
.get('/list/api/questions')
.reply(200, JSON.stringify({
user_name: 'Eric',
favorites: {
private_favorites: [{id_hash: 'abcdef', name: 'Favorite'}]
}
}));
plugin.login({}, function(e, user) {
assert.equal(e, null);
assert.equal(user.loginCSRF, 'LOGIN_CSRF_TOKEN');
assert.equal(user.sessionCSRF, 'SESSION_CSRF_TOKEN');
assert.equal(user.sessionId, 'SESSION_ID');
assert.equal(user.name, 'Eric');
assert.equal(user.hash, 'abcdef');
done();
});
});
it('should fail if http error', function(done) {
nock('https://leetcode.com')
.get('/accounts/login/')
.reply(200, '', {
'Set-Cookie': [
'csrftoken=LOGIN_CSRF_TOKEN; Max-Age=31449600; Path=/; secure'
]});
nock('https://leetcode.com')
.post('/accounts/login/')
.replyWithError('unknown error!');
plugin.login({}, function(e, user) {
assert.equal(e.message, 'unknown error!');
done();
});
});
it('should fail if http error, 2nd', function(done) {
nock('https://leetcode.com')
.get('/accounts/login/')
.replyWithError('unknown error!');
plugin.login({}, function(e, user) {
assert.equal(e.message, 'unknown error!');
done();
});
});
}); // #login
describe('#getProblems', function() {
it('should ok', function(done) {
nock('https://leetcode.com')
.get('/api/problems/algorithms/')
.replyWithFile(200, './test/mock/problems.json.20160911');
nock('https://leetcode.com')
.get('/api/problems/database/')
.replyWithFile(200, './test/mock/problems.json.20160911');
nock('https://leetcode.com')
.get('/api/problems/shell/')
.replyWithFile(200, './test/mock/problems.json.20160911');
nock('https://leetcode.com')
.get('/api/problems/concurrency/')
.replyWithFile(200, './test/mock/problems.json.20160911');
plugin.getProblems(false, function(e, problems) {
assert.equal(e, null);
assert.equal(problems.length, 377 * 4);
done();
});
});
it('should fail if error occurs', function(done) {
nock('https://leetcode.com')
.get('/api/problems/algorithms/')
.replyWithFile(200, './test/mock/problems.json.20160911');
nock('https://leetcode.com')
.get('/api/problems/database/')
.replyWithError('unknown error');
nock('https://leetcode.com')
.get('/api/problems/shell/')
.replyWithFile(200, './test/mock/problems.json.20160911');
nock('https://leetcode.com')
.get('/api/problems/concurrency/')
.replyWithFile(200, './test/mock/problems.json.20160911');
plugin.getProblems(false, function(e, problems) {
assert.equal(e.message, 'unknown error');
done();
});
});
}); // #getProblems
describe('#getCategoryProblems', function() {
it('should ok', function(done) {
nock('https://leetcode.com')
.get('/api/problems/algorithms/')
.replyWithFile(200, './test/mock/problems.json.20160911');
plugin.getCategoryProblems('algorithms', function(e, problems) {
assert.equal(e, null);
assert.equal(problems.length, 377);
done();
});
});
it('should fail if not login', function(done) {
config.autologin.enable = false;
nock('https://leetcode.com')
.get('/api/problems/algorithms/')
.replyWithFile(200, './test/mock/problems.nologin.json.20161015');
plugin.getCategoryProblems('algorithms', function(e, problems) {
assert.deepEqual(e, session.errors.EXPIRED);
done();
});
});
}); // #getCategoryProblems
describe('#getProblem', function() {
beforeEach(function() {
PROBLEM.locked = false;
});
it('should ok', function(done) {
nock('https://leetcode.com')
.post('/graphql')
.replyWithFile(200, './test/mock/find-the-difference.json.20171216');
plugin.getProblem(PROBLEM, false, function(e, problem) {
assert.equal(e, null);
assert.equal(problem.totalAC, '89.7K');
assert.equal(problem.totalSubmit, '175.7K');
assert.equal(problem.desc,
[
'<p>',
'Given two strings <b><i>s</i></b> and <b><i>t</i></b> which consist of only lowercase letters.</p>',
'',
'<p>String <b><i>t</i></b> is generated by random shuffling string <b><i>s</i></b> and then add one more letter at a random position.</p>',
'',
'<p>Find the letter that was added in <b><i>t</i></b>.</p>',
'',
'<p><b>Example:</b>',
'<pre>',
'Input:',
's = "abcd"',
't = "abcde"',
'',
'Output:',
'e',
'',
'Explanation:',
"'e' is the letter that was added.",
'</pre>'
].join('\r\n'));
assert.equal(problem.templates.length, 12);
assert.equal(problem.templates[0].value, 'cpp');
assert.equal(problem.templates[0].text, 'C++');
assert.equal(problem.templates[0].defaultCode,
[
'class Solution {',
'public:',
' char findTheDifference(string s, string t) {',
' ',
' }',
'};'
].join('\r\n'));
assert.equal(problem.templates[1].value, 'java');
assert.equal(problem.templates[1].text, 'Java');
assert.equal(problem.templates[1].defaultCode,
[
'class Solution {',
' public char findTheDifference(String s, String t) {',
' ',
' }',
'}'
].join('\r\n'));
assert.equal(problem.templates[2].value, 'python');
assert.equal(problem.templates[2].text, 'Python');
assert.equal(problem.templates[2].defaultCode,
[
'class Solution(object):',
' def findTheDifference(self, s, t):',
' """',
' :type s: str',
' :type t: str',
' :rtype: str',
' """',
' '
].join('\r\n'));
assert.equal(problem.templates[3].value, 'python3');
assert.equal(problem.templates[3].text, 'Python3');
assert.equal(problem.templates[3].defaultCode,
[
'class Solution:',
' def findTheDifference(self, s, t):',
' """',
' :type s: str',
' :type t: str',
' :rtype: str',
' """',
' '
].join('\r\n'));
assert.equal(problem.templates[4].value, 'c');
assert.equal(problem.templates[4].text, 'C');
assert.equal(problem.templates[4].defaultCode,
[
'char findTheDifference(char* s, char* t) {',
' ',
'}'
].join('\r\n'));
assert.equal(problem.templates[5].value, 'csharp');
assert.equal(problem.templates[5].text, 'C#');
assert.equal(problem.templates[5].defaultCode,
[
'public class Solution {',
' public char FindTheDifference(string s, string t) {',
' ',
' }',
'}'
].join('\r\n'));
assert.equal(problem.templates[6].value, 'javascript');
assert.equal(problem.templates[6].text, 'JavaScript');
assert.equal(problem.templates[6].defaultCode,
[
'/**',
' * @param {string} s',
' * @param {string} t',
' * @return {character}',
' */',
'var findTheDifference = function(s, t) {',
' ',
'};'
].join('\r\n'));
assert.equal(problem.templates[7].value, 'ruby');
assert.equal(problem.templates[7].text, 'Ruby');
assert.equal(problem.templates[7].defaultCode,
[
'# @param {String} s',
'# @param {String} t',
'# @return {Character}',
'def find_the_difference(s, t)',
' ',
'end'
].join('\r\n'));
assert.equal(problem.templates[8].value, 'swift');
assert.equal(problem.templates[8].text, 'Swift');
assert.equal(problem.templates[8].defaultCode,
[
'class Solution {',
' func findTheDifference(_ s: String, _ t: String) -> Character {',
' ',
' }',
'}'
].join('\r\n'));
assert.equal(problem.templates[9].value, 'golang');
assert.equal(problem.templates[9].text, 'Go');
assert.equal(problem.templates[9].defaultCode,
[
'func findTheDifference(s string, t string) byte {',
' ',
'}'
].join('\r\n'));
assert.equal(problem.templates[10].value, 'scala');
assert.equal(problem.templates[10].text, 'Scala');
assert.equal(problem.templates[10].defaultCode,
[
'object Solution {',
' def findTheDifference(s: String, t: String): Char = {',
' ',
' }',
'}'
].join('\n'));
assert.equal(problem.templates[11].value, 'kotlin');
assert.equal(problem.templates[11].text, 'Kotlin');
assert.equal(problem.templates[11].defaultCode,
[
'class Solution {',
' fun findTheDifference(s: String, t: String): Char {',
' ',
' }',
'}'
].join('\n'));
done();
});
});
it('should fail if no permission for locked', function(done) {
PROBLEM.locked = true;
plugin.getProblem(PROBLEM, false, function(e, problem) {
assert.equal(e, 'failed to load locked problem!');
done();
});
});
it('should fail if session expired', function(done) {
nock('https://leetcode.com').post('/graphql').reply(403);
plugin.getProblem(PROBLEM, false, function(e, problem) {
assert.equal(e, session.errors.EXPIRED);
done();
});
});
it('should fail if http error', function(done) {
nock('https://leetcode.com').post('/graphql').reply(500);
plugin.getProblem(PROBLEM, false, function(e, problem) {
assert.deepEqual(e, {msg: 'http error', statusCode: 500});
done();
});
});
it('should fail if unknown error', function(done) {
nock('https://leetcode.com').post('/graphql').replyWithError('unknown error!');
plugin.getProblem(PROBLEM, false, function(e, problem) {
assert.equal(e.message, 'unknown error!');
done();
});
});
}); // #getProblem
describe('#testProblem', function() {
it('should ok', function(done) {
nock('https://leetcode.com')
.post('/problems/find-the-difference/interpret_solution/')
.reply(200, '{"interpret_expected_id": "id1", "interpret_id": "id2"}');
nock('https://leetcode.com')
.get('/submissions/detail/id1/check/')
.reply(200, '{"state": "SUCCESS", "run_success": true, "status_msg": "Accepted", "submission_id": "interpret_expected_id1"}');
nock('https://leetcode.com')
.get('/submissions/detail/id2/check/')
.reply(200, '{"state": "SUCCESS", "run_success": false, "status_msg": "Runtime Error", "submission_id": "interpret_id2"}');
plugin.testProblem(PROBLEM, function(e, results) {
assert.equal(e, null);
assert.equal(results[0].id, 'id2');
assert.equal(results[0].ok, false);
assert.equal(results[1].id, 'id1');
assert.equal(results[1].ok, true);
done();
});
});
it('should fail if http error', function(done) {
nock('https://leetcode.com')
.post('/problems/find-the-difference/interpret_solution/')
.replyWithError('unknown error!');
plugin.testProblem(PROBLEM, function(e, results) {
assert.equal(e.message, 'unknown error!');
done();
});
});
}); // #testProblem
describe('#submitProblem', function() {
it('should ok', function(done) {
nock('https://leetcode.com')
.post('/problems/find-the-difference/submit/')
.reply(200, '{"submission_id": "id1"}');
nock('https://leetcode.com')
.get('/submissions/detail/id1/check/')
.reply(200, '{"state": "SUCCESS", "run_success": true, "status_msg": "Accepted", "submission_id": "id1"}');
plugin.submitProblem(PROBLEM, function(e, results) {
assert.equal(e, null);
assert.equal(results[0].id, 'id1');
assert.equal(results[0].ok, true);
done();
});
});
it('should ok after delay', function(done) {
nock('https://leetcode.com')
.post('/problems/find-the-difference/submit/')
.reply(200, '{"error": "You run code too soon"}');
nock('https://leetcode.com')
.post('/problems/find-the-difference/submit/')
.reply(200, '{"submission_id": "id1"}');
nock('https://leetcode.com')
.get('/submissions/detail/id1/check/')
.reply(200, '{"state": "STARTED"}');
nock('https://leetcode.com')
.get('/submissions/detail/id1/check/')
.reply(200, '{"state": "SUCCESS", "run_success": true, "status_msg": "Accepted", "submission_id": "id1"}');
plugin.submitProblem(PROBLEM, function(e, results) {
assert.equal(e, null);
assert.equal(results[0].id, 'id1');
assert.equal(results[0].ok, true);
done();
});
}).timeout(5000);
it('should fail if server error', function(done) {
nock('https://leetcode.com')
.post('/problems/find-the-difference/submit/')
.reply(200, '{"error": "maybe internal error?"}');
plugin.submitProblem(PROBLEM, function(e, results) {
assert.equal(e, 'maybe internal error?');
done();
});
});
it('should fail if server error in check result', function(done) {
nock('https://leetcode.com')
.post('/problems/find-the-difference/submit/')
.reply(200, '{"submission_id": "id1"}');
nock('https://leetcode.com')
.get('/submissions/detail/id1/check/')
.replyWithError('unknown error!');
plugin.submitProblem(PROBLEM, function(e, results) {
assert.equal(e.message, 'unknown error!');
done();
});
});
}); // #submitProblem
describe('#starProblem', function() {
it('should star ok', function(done) {
nock('https://leetcode.com')
.post('/graphql')
.replyWithFile(200, './test/mock/find-the-difference-star.json.20200821');
plugin.starProblem(PROBLEM, true, function(e, starred) {
assert.equal(e, null);
assert.equal(starred, true);
done();
});
});
it('should unstar ok', function(done) {
nock('https://leetcode.com')
.post('/graphql')
.replyWithFile(200, './test/mock/find-the-difference-unstar.json.20200821');
plugin.starProblem(PROBLEM, false, function(e, starred) {
assert.equal(e, null);
assert.equal(starred, false);
done();
});
});
it('should star fail if http error', function(done) {
nock('https://leetcode.com')
.post('/graphql')
.replyWithError('unknown error!');
plugin.starProblem(PROBLEM, true, function(e, starred) {
assert.equal(e.message, 'unknown error!');
done();
});
});
}); // #starProblem
describe('#getSubmissions', function() {
it('should ok', function(done) {
const problem = {
id: 1,
name: 'Two Sum',
slug: 'two-sum',
link: 'https://leetcode.com/problems/two-sum',
locked: false
};
nock('https://leetcode.com')
.get('/api/submissions/two-sum')
.replyWithFile(200, './test/mock/two-sum.submissions.json.20170425');
plugin.getSubmissions(problem, function(e, submissions) {
assert.equal(e, null);
assert.equal(submissions.length, 20);
assert.deepEqual(submissions[0], {
id: '95464136',
title: 'Two Sum',
is_pending: false,
lang: 'cpp',
time: '1 month, 3 weeks',
runtime: '12 ms',
url: '/submissions/detail/95464136/',
status_display: 'Accepted'
});
assert.deepEqual(submissions[1], {
id: '78502271',
title: 'Two Sum',
is_pending: false,
lang: 'cpp',
time: '6 months, 1 week',
runtime: '13 ms',
url: '/submissions/detail/78502271/',
status_display: 'Accepted'
});
done();
});
});
it('should fail if http error', function(done) {
nock('https://leetcode.com')
.get('/api/submissions/find-the-difference')
.replyWithError('unknown error!');
plugin.getSubmissions(PROBLEM, function(e, submissions) {
assert.equal(e.message, 'unknown error!');
done();
});
});
}); // #getSubmissions
describe('#getSubmission', function() {
it('should ok', function(done) {
nock('https://leetcode.com')
.get('/submissions/detail/73790064/')
.replyWithFile(200, './test/mock/two-sum.submission.73790064.html.20161006');
plugin.getSubmission(_.clone(SUBMISSION), function(e, submission) {
assert.equal(e, null);
assert.deepEqual(submission.code,
[
'class Solution {',
'public:',
' vector<int> twoSum(vector<int>& nums, int target) {',
' return res;',
' }',
'};',
''
].join('\r\n'));
done();
});
});
it('should fail if http error', function(done) {
nock('https://leetcode.com')
.get('/submissions/detail/73790064/')
.replyWithError('unknown error!');
plugin.getSubmission(_.clone(SUBMISSION), function(e, submission) {
assert.equal(e.message, 'unknown error!');
done();
});
});
it('should fail if no matching submission', function(done) {
nock('https://leetcode.com')
.get('/submissions/detail/73790064/')
.replyWithFile(200, './test/mock/locked.html.20161015');
plugin.getSubmission(_.clone(SUBMISSION), function(e, submission) {
assert.equal(e, null);
assert.equal(submission.code, null);
done();
});
});
}); // #getSubmission
describe('#getFavorites', function() {
it('should ok', function(done) {
nock('https://leetcode.com')
.get('/list/api/questions')
.replyWithFile(200, './test/mock/favorites.json.20170716');
plugin.getFavorites(function(e, favorites) {
assert.equal(e, null);
const my = favorites.favorites.private_favorites;
assert.equal(my.length, 1);
assert.equal(my[0].name, 'Favorite');
assert.equal(my[0].id_hash, 'abcdefg');
done();
});
});
}); // #getFavorites
describe('#session', function() {
const DATA = {sessions: []};
it('should getSessions ok', function(done) {
nock('https://leetcode.com')
.post('/session/')
.reply(200, JSON.stringify(DATA));
plugin.getSessions(function(e, sessions) {
assert.notExists(e);
assert.deepEqual(sessions, []);
done();
});
});
it('should activateSessions ok', function(done) {
nock('https://leetcode.com')
.put('/session/', {func: 'activate', target: 1})
.reply(200, JSON.stringify(DATA));
plugin.activateSession({id: 1}, function(e, sessions) {
assert.notExists(e);
assert.deepEqual(sessions, []);
done();
});
});
it('should createSessions ok', function(done) {
nock('https://leetcode.com')
.put('/session/', {func: 'create', name: 's1'})
.reply(200, JSON.stringify(DATA));
plugin.createSession('s1', function(e, sessions) {
assert.notExists(e);
assert.deepEqual(sessions, []);
done();
});
});
it('should deleteSessions ok', function(done) {
nock('https://leetcode.com')
.delete('/session/', {target: 1})
.reply(200, JSON.stringify(DATA));
plugin.deleteSession({id: 1}, function(e, sessions) {
assert.notExists(e);
assert.deepEqual(sessions, []);
done();
});
});
it('should fail if 302 returned', function(done) {
nock('https://leetcode.com')
.post('/session/')
.reply(302);
plugin.getSessions(function(e, sessions) {
assert.deepEqual(e, session.errors.EXPIRED);
assert.notExists(sessions);
done();
});
});
}); // #session
});