1
- import { login , logout , session , contest } from "../../src/commands" ;
1
+ import { login , logout , session , contest , task , tasks } from "../../src/commands" ;
2
2
import { AtCoder } from "../../src/atcoder" ;
3
3
import { getAtCoder } from "../../src/di" ;
4
- import { Contest , ContestProject , findProjectJSON , Task } from "../../src/project"
4
+ import { Contest , ContestProject , findProjectJSON , Task , detectTaskByPath } from "../../src/project"
5
5
jest . mock ( "../../src/di" ) ;
6
6
jest . mock ( "../../src/project" )
7
7
@@ -152,6 +152,30 @@ describe("Commands", () => {
152
152
expect ( atcoder . contest ) . toBeCalledTimes ( 1 ) ;
153
153
expect ( atcoder . contest ) . toBeCalledWith ( "aic987" ) ;
154
154
} ) ;
155
+ test ( "contest found, try login and success" , async ( ) => {
156
+ atcoder . checkSession = jest . fn ( async ( ) => false ) ;
157
+ atcoder . login = jest . fn ( async ( ) => true ) ;
158
+ atcoder . contest = jest . fn ( async _ => dummy_contest ) ;
159
+ await contest ( "aic987" , { } ) ;
160
+ expect ( log ) . toMatchSnapshot ( ) ;
161
+ expect ( elog ) . toMatchSnapshot ( ) ;
162
+ expect ( findProjectJSON ) . toBeCalledTimes ( 0 ) ;
163
+ expect ( atcoder . login ) . toBeCalledTimes ( 1 ) ;
164
+ expect ( atcoder . contest ) . toBeCalledTimes ( 1 ) ;
165
+ expect ( atcoder . contest ) . toBeCalledWith ( "aic987" ) ;
166
+ } ) ;
167
+ test ( "try login and fail, but (public) contest found" , async ( ) => {
168
+ atcoder . checkSession = jest . fn ( async ( ) => false ) ;
169
+ atcoder . login = jest . fn ( async ( ) => false ) ;
170
+ atcoder . contest = jest . fn ( async _ => dummy_contest ) ;
171
+ await contest ( "aic987" , { } ) ;
172
+ expect ( log ) . toMatchSnapshot ( ) ;
173
+ expect ( elog ) . toMatchSnapshot ( ) ;
174
+ expect ( findProjectJSON ) . toBeCalledTimes ( 0 ) ;
175
+ expect ( atcoder . login ) . toBeCalledTimes ( 1 ) ;
176
+ expect ( atcoder . contest ) . toBeCalledTimes ( 1 ) ;
177
+ expect ( atcoder . contest ) . toBeCalledWith ( "aic987" ) ;
178
+ } ) ;
155
179
test ( "contest not found" , async ( ) => {
156
180
atcoder . checkSession = jest . fn ( async ( ) => true ) ;
157
181
atcoder . contest = jest . fn ( async _ => {
@@ -166,4 +190,185 @@ describe("Commands", () => {
166
190
} ) ;
167
191
} ) ;
168
192
} ) ;
193
+ describe ( "task()" , ( ) => {
194
+ describe ( "contest id and task id are not given" , ( ) => {
195
+ test ( "task found, without id" , async ( ) => {
196
+ // @ts -ignore: dynamically added method for test
197
+ detectTaskByPath . mockResolvedValueOnce ( { contest : dummy_contest , task : dummy_task01 } ) ;
198
+ await task ( undefined , undefined , { } ) ;
199
+ expect ( log ) . toMatchSnapshot ( ) ;
200
+ expect ( elog ) . toMatchSnapshot ( ) ;
201
+ expect ( detectTaskByPath ) . toBeCalledTimes ( 1 ) ;
202
+ } )
203
+ test ( "task found, with id" , async ( ) => {
204
+ // @ts -ignore: dynamically added method for test
205
+ detectTaskByPath . mockResolvedValueOnce ( { contest : dummy_contest , task : dummy_task01 } ) ;
206
+ await task ( undefined , undefined , { id : true } ) ;
207
+ expect ( log ) . toMatchSnapshot ( ) ;
208
+ expect ( elog ) . toMatchSnapshot ( ) ;
209
+ expect ( detectTaskByPath ) . toBeCalledTimes ( 1 ) ;
210
+ } )
211
+ test ( "task not found" , async ( ) => {
212
+ // @ts -ignore: dynamically added method for test
213
+ detectTaskByPath . mockResolvedValueOnce ( { contest : null , task : null } ) ;
214
+ await task ( undefined , undefined , { } ) ;
215
+ expect ( log ) . toMatchSnapshot ( ) ;
216
+ expect ( elog ) . toMatchSnapshot ( ) ;
217
+ expect ( detectTaskByPath ) . toBeCalledTimes ( 1 ) ;
218
+ } ) ;
219
+ test ( "error occured in searching path" , async ( ) => {
220
+ // TODO: this path seems to be never reached
221
+ // @ts -ignore: dynamically added method for test
222
+ detectTaskByPath . mockRejectedValueOnce ( new Error ( "something wrong" ) ) ;
223
+ await task ( undefined , undefined , { } ) ;
224
+ expect ( log ) . toMatchSnapshot ( ) ;
225
+ expect ( elog ) . toMatchSnapshot ( ) ;
226
+ expect ( detectTaskByPath ) . toBeCalledTimes ( 1 ) ;
227
+ } ) ;
228
+ } ) ;
229
+ describe ( "contest id and task id are given" , ( ) => {
230
+ test ( "task found, without id" , async ( ) => {
231
+ atcoder . checkSession = jest . fn ( async ( ) => true ) ;
232
+ atcoder . task = jest . fn ( async ( contest_id , task_id ) => dummy_task01 ) ;
233
+ await task ( "aic987" , "aic987_a" , { } ) ;
234
+ expect ( log ) . toMatchSnapshot ( ) ;
235
+ expect ( elog ) . toMatchSnapshot ( ) ;
236
+ expect ( detectTaskByPath ) . toBeCalledTimes ( 0 ) ;
237
+ expect ( atcoder . task ) . toBeCalledTimes ( 1 ) ;
238
+ expect ( atcoder . task ) . toBeCalledWith ( "aic987" , "aic987_a" ) ;
239
+ } ) ;
240
+ test ( "task found, with id" , async ( ) => {
241
+ atcoder . checkSession = jest . fn ( async ( ) => true ) ;
242
+ atcoder . task = jest . fn ( async ( contest_id , task_id ) => dummy_task01 ) ;
243
+ await task ( "aic987" , "aic987_a" , { id : true } ) ;
244
+ expect ( log ) . toMatchSnapshot ( ) ;
245
+ expect ( elog ) . toMatchSnapshot ( ) ;
246
+ expect ( detectTaskByPath ) . toBeCalledTimes ( 0 ) ;
247
+ expect ( atcoder . task ) . toBeCalledTimes ( 1 ) ;
248
+ expect ( atcoder . task ) . toBeCalledWith ( "aic987" , "aic987_a" ) ;
249
+ } ) ;
250
+ test ( "contest found, try login and success" , async ( ) => {
251
+ atcoder . checkSession = jest . fn ( async ( ) => false ) ;
252
+ atcoder . login = jest . fn ( async ( ) => true ) ;
253
+ atcoder . task = jest . fn ( async ( contest_id , task_id ) => dummy_task01 ) ;
254
+ await task ( "aic987" , "aic987_a" , { } ) ;
255
+ expect ( log ) . toMatchSnapshot ( ) ;
256
+ expect ( elog ) . toMatchSnapshot ( ) ;
257
+ expect ( detectTaskByPath ) . toBeCalledTimes ( 0 ) ;
258
+ expect ( atcoder . login ) . toBeCalledTimes ( 1 ) ;
259
+ expect ( atcoder . task ) . toBeCalledTimes ( 1 ) ;
260
+ expect ( atcoder . task ) . toBeCalledWith ( "aic987" , "aic987_a" ) ;
261
+ } ) ;
262
+ test ( "try login and fail, but (public) task found" , async ( ) => {
263
+ atcoder . checkSession = jest . fn ( async ( ) => false ) ;
264
+ atcoder . login = jest . fn ( async ( ) => false ) ;
265
+ atcoder . task = jest . fn ( async ( contest_id , task_id ) => dummy_task01 ) ;
266
+ await task ( "aic987" , "aic987_a" , { } ) ;
267
+ expect ( log ) . toMatchSnapshot ( ) ;
268
+ expect ( elog ) . toMatchSnapshot ( ) ;
269
+ expect ( detectTaskByPath ) . toBeCalledTimes ( 0 ) ;
270
+ expect ( atcoder . login ) . toBeCalledTimes ( 1 ) ;
271
+ expect ( atcoder . task ) . toBeCalledTimes ( 1 ) ;
272
+ expect ( atcoder . task ) . toBeCalledWith ( "aic987" , "aic987_a" ) ;
273
+ } ) ;
274
+ test ( "contest not found" , async ( ) => {
275
+ atcoder . checkSession = jest . fn ( async ( ) => true ) ;
276
+ atcoder . task = jest . fn ( async _ => {
277
+ throw new Error ( ) ;
278
+ } ) ;
279
+ await task ( "aic987" , "aic987_z" , { } ) ;
280
+ expect ( log ) . toMatchSnapshot ( ) ;
281
+ expect ( elog ) . toMatchSnapshot ( ) ;
282
+ expect ( detectTaskByPath ) . toBeCalledTimes ( 0 ) ;
283
+ expect ( atcoder . task ) . toBeCalledTimes ( 1 ) ;
284
+ expect ( atcoder . task ) . toBeCalledWith ( "aic987" , "aic987_z" ) ;
285
+ } ) ;
286
+ } ) ;
287
+ } ) ;
288
+ describe ( "tasks()" , ( ) => {
289
+ describe ( "contest id and task id are not given" , ( ) => {
290
+ test ( "tasks found, without id" , async ( ) => {
291
+ // @ts -ignore: dynamically added method for test
292
+ findProjectJSON . mockResolvedValueOnce ( { path : "/dummy/path" , data : dummy_contest_project } ) ;
293
+ await tasks ( undefined , { } ) ;
294
+ expect ( log ) . toMatchSnapshot ( ) ;
295
+ expect ( elog ) . toMatchSnapshot ( ) ;
296
+ expect ( findProjectJSON ) . toBeCalledTimes ( 1 ) ;
297
+ } )
298
+ test ( "tasks found, with id" , async ( ) => {
299
+ // @ts -ignore: dynamically added method for test
300
+ findProjectJSON . mockResolvedValueOnce ( { path : "/dummy/path" , data : dummy_contest_project } ) ;
301
+ await tasks ( undefined , { id : true } ) ;
302
+ expect ( log ) . toMatchSnapshot ( ) ;
303
+ expect ( elog ) . toMatchSnapshot ( ) ;
304
+ expect ( findProjectJSON ) . toBeCalledTimes ( 1 ) ;
305
+ } )
306
+ test ( "tasks not found" , async ( ) => {
307
+ // @ts -ignore: dynamically added method for test
308
+ findProjectJSON . mockRejectedValueOnce ( new Error ( "contest.acc.json not found." ) ) ;
309
+ await tasks ( undefined , { } ) ;
310
+ expect ( log ) . toMatchSnapshot ( ) ;
311
+ expect ( elog ) . toMatchSnapshot ( ) ;
312
+ expect ( findProjectJSON ) . toBeCalledTimes ( 1 ) ;
313
+ } ) ;
314
+ } ) ;
315
+ describe ( "contest id and task id are given" , ( ) => {
316
+ test ( "tasks found, without id" , async ( ) => {
317
+ atcoder . checkSession = jest . fn ( async ( ) => true ) ;
318
+ atcoder . tasks = jest . fn ( async ( contest_id ) => [ dummy_task01 , dummy_task02 , dummy_task03 , dummy_task04 ] ) ;
319
+ await tasks ( "aic987" , { } ) ;
320
+ expect ( log ) . toMatchSnapshot ( ) ;
321
+ expect ( elog ) . toMatchSnapshot ( ) ;
322
+ expect ( findProjectJSON ) . toBeCalledTimes ( 0 ) ;
323
+ expect ( atcoder . tasks ) . toBeCalledTimes ( 1 ) ;
324
+ expect ( atcoder . tasks ) . toBeCalledWith ( "aic987" ) ;
325
+ } ) ;
326
+ test ( "task found, with id" , async ( ) => {
327
+ atcoder . checkSession = jest . fn ( async ( ) => true ) ;
328
+ atcoder . tasks = jest . fn ( async ( contest_id ) => [ dummy_task01 , dummy_task02 , dummy_task03 , dummy_task04 ] ) ;
329
+ await tasks ( "aic987" , { id : true } ) ;
330
+ expect ( log ) . toMatchSnapshot ( ) ;
331
+ expect ( elog ) . toMatchSnapshot ( ) ;
332
+ expect ( findProjectJSON ) . toBeCalledTimes ( 0 ) ;
333
+ expect ( atcoder . tasks ) . toBeCalledTimes ( 1 ) ;
334
+ expect ( atcoder . tasks ) . toBeCalledWith ( "aic987" ) ;
335
+ } ) ;
336
+ test ( "contest found, try login and success" , async ( ) => {
337
+ atcoder . checkSession = jest . fn ( async ( ) => false ) ;
338
+ atcoder . login = jest . fn ( async ( ) => true ) ;
339
+ atcoder . tasks = jest . fn ( async ( contest_id ) => [ dummy_task01 , dummy_task02 , dummy_task03 , dummy_task04 ] ) ;
340
+ await tasks ( "aic987" , { } ) ;
341
+ expect ( log ) . toMatchSnapshot ( ) ;
342
+ expect ( elog ) . toMatchSnapshot ( ) ;
343
+ expect ( findProjectJSON ) . toBeCalledTimes ( 0 ) ;
344
+ expect ( atcoder . login ) . toBeCalledTimes ( 1 ) ;
345
+ expect ( atcoder . tasks ) . toBeCalledTimes ( 1 ) ;
346
+ expect ( atcoder . tasks ) . toBeCalledWith ( "aic987" ) ;
347
+ } ) ;
348
+ test ( "try login and fail, but (public) task found" , async ( ) => {
349
+ atcoder . checkSession = jest . fn ( async ( ) => false ) ;
350
+ atcoder . login = jest . fn ( async ( ) => false ) ;
351
+ atcoder . tasks = jest . fn ( async ( contest_id ) => [ dummy_task01 , dummy_task02 , dummy_task03 , dummy_task04 ] ) ;
352
+ await tasks ( "aic987" , { } ) ;
353
+ expect ( log ) . toMatchSnapshot ( ) ;
354
+ expect ( elog ) . toMatchSnapshot ( ) ;
355
+ expect ( findProjectJSON ) . toBeCalledTimes ( 0 ) ;
356
+ expect ( atcoder . login ) . toBeCalledTimes ( 1 ) ;
357
+ expect ( atcoder . tasks ) . toBeCalledTimes ( 1 ) ;
358
+ expect ( atcoder . tasks ) . toBeCalledWith ( "aic987" ) ;
359
+ } ) ;
360
+ test ( "contest not found" , async ( ) => {
361
+ atcoder . checkSession = jest . fn ( async ( ) => true ) ;
362
+ atcoder . tasks = jest . fn ( async _ => {
363
+ throw new Error ( ) ;
364
+ } ) ;
365
+ await tasks ( "aic987" , { } ) ;
366
+ expect ( log ) . toMatchSnapshot ( ) ;
367
+ expect ( elog ) . toMatchSnapshot ( ) ;
368
+ expect ( findProjectJSON ) . toBeCalledTimes ( 0 ) ;
369
+ expect ( atcoder . tasks ) . toBeCalledTimes ( 1 ) ;
370
+ expect ( atcoder . tasks ) . toBeCalledWith ( "aic987" ) ;
371
+ } ) ;
372
+ } ) ;
373
+ } ) ;
169
374
} ) ;
0 commit comments