forked from madlabsinc/teachcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfetchtask.test.js
129 lines (117 loc) · 4.01 KB
/
fetchtask.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
'use strict';
const path = require('path');
const execa = require('execa');
const test = require('ava');
const fs = require('fs');
const rootCommand = path.join(process.cwd(), 'bin/index.js');
const configFilePath = path.join(process.cwd(), 'config.json');
const learningTracksInfo = [
{ trackName: 'Python', fileExtension: 'py' },
{ trackName: 'JavaScript', fileExtension: 'js' },
];
const userDefaultConfig = {
learningTrack: '',
userName: 'testConfig',
taskCount: 0,
keys: [],
userSubmittedFiles: [],
};
const createUserConfig = (
trackName,
fileExtension,
keysNumber,
filesNumber,
) => {
let userConfig = userDefaultConfig;
userConfig['learningTrack'] = trackName;
for (let i = 0; i < keysNumber; i++) {
userConfig['keys'].push(`testKey${i + 1}`);
}
for (let i = 0; i < filesNumber; i++) {
userConfig['userSubmittedFiles'].push(`task${i + 1}.${fileExtension}`);
}
userConfig['taskCount'] = filesNumber;
return userConfig;
};
test.serial('no config file for fetchtask', async t => {
if (fs.existsSync(configFilePath)) fs.unlinkSync(configFilePath);
const { stdout } = await execa(rootCommand, ['fetchtask'], {
reject: false,
});
t.snapshot(stdout);
});
test.serial('incorrect key for fetchtask', async t => {
let snap = null;
t.assert(learningTracksInfo.length > 0);
for (let index in learningTracksInfo) {
let { trackName, fileExtension } = learningTracksInfo[index];
const userConfig = createUserConfig(trackName, fileExtension, 30, 30);
fs.writeFileSync(configFilePath, JSON.stringify(userConfig));
const { stdout } = await execa(
rootCommand,
['fetchtask', 'incorrectTestKey'],
{ reject: false },
);
fs.unlinkSync(configFilePath);
if (snap) {
t.true(snap === stdout);
} else {
snap = stdout;
}
}
t.snapshot(snap);
});
test.serial('display completed task with fetchtask key', async t => {
t.assert(learningTracksInfo.length > 0);
for (let index in learningTracksInfo) {
let { trackName, fileExtension } = learningTracksInfo[index];
let userConfig = createUserConfig(trackName, fileExtension, 6, 5);
fs.writeFileSync(configFilePath, JSON.stringify(userConfig));
const { stdout } = await execa(rootCommand, ['fetchtask', 'testKey2']);
t.snapshot(stdout);
fs.unlinkSync(configFilePath);
}
});
test.serial('display incomplete task with fetchtask key', async t => {
t.assert(learningTracksInfo.length > 0);
for (let index in learningTracksInfo) {
let { trackName, fileExtension } = learningTracksInfo[index];
let userConfig = createUserConfig(trackName, fileExtension, 6, 5);
fs.writeFileSync(configFilePath, JSON.stringify(userConfig));
const { stdout } = await execa(rootCommand, ['fetchtask', 'testKey6']);
t.snapshot(stdout);
fs.unlinkSync(configFilePath);
fs.unlinkSync(path.join(process.cwd(), `task6.${fileExtension}`));
}
});
test.serial('no more tasks available', async t => {
let snap = null;
t.assert(learningTracksInfo.length > 0);
for (let index in learningTracksInfo) {
let { trackName, fileExtension } = learningTracksInfo[index];
const userConfig = createUserConfig(trackName, fileExtension, 30, 30);
fs.writeFileSync(configFilePath, JSON.stringify(userConfig));
const { stdout } = await execa(rootCommand, ['fetchtask'], {
reject: false,
});
if (snap) {
t.assert(snap === stdout);
} else {
snap = stdout;
}
fs.unlinkSync(configFilePath);
}
t.snapshot(snap);
});
test.serial('display next task with fetchtask', async t => {
t.assert(learningTracksInfo.length > 0);
for (let index in learningTracksInfo) {
let { trackName, fileExtension } = learningTracksInfo[index];
const userConfig = createUserConfig(trackName, fileExtension, 6, 5);
fs.writeFileSync(configFilePath, JSON.stringify(userConfig));
const { stdout } = await execa(rootCommand, ['fetchtask']);
t.snapshot(stdout);
fs.unlinkSync(configFilePath);
fs.unlinkSync(path.join(process.cwd(), `task6.${fileExtension}`));
}
});