From c89c587c8e591af70a94620e49bed2f4a67afa0a Mon Sep 17 00:00:00 2001 From: Tatamo Date: Sun, 15 Aug 2021 22:55:29 +0900 Subject: [PATCH 1/6] add tests for commands.login() --- .../__tests__/__snapshots__/commands.ts.snap | 11 ++++++ tests/__tests__/commands.ts | 37 +++++++++++++++++++ 2 files changed, 48 insertions(+) create mode 100644 tests/__tests__/__snapshots__/commands.ts.snap create mode 100644 tests/__tests__/commands.ts diff --git a/tests/__tests__/__snapshots__/commands.ts.snap b/tests/__tests__/__snapshots__/commands.ts.snap new file mode 100644 index 0000000..67df85a --- /dev/null +++ b/tests/__tests__/__snapshots__/commands.ts.snap @@ -0,0 +1,11 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`login() login() login fail 1`] = ` +"login failed +" +`; + +exports[`login() login() login success 1`] = ` +"OK +" +`; diff --git a/tests/__tests__/commands.ts b/tests/__tests__/commands.ts new file mode 100644 index 0000000..4347547 --- /dev/null +++ b/tests/__tests__/commands.ts @@ -0,0 +1,37 @@ +import { login } from "../../src/commands"; +import { AtCoder } from "../../src/atcoder"; +import { getAtCoder } from "../../src/di"; +jest.mock("../../src/di"); + +describe("login()", () => { + let atcoder: AtCoder; + beforeAll(async () => { + atcoder = jest.genMockFromModule<{ AtCoder: AtCoder }>("../../src/atcoder").AtCoder; + // @ts-ignore: dynamically added method for test + getAtCoder.mockResolvedValue(atcoder); + }); + beforeEach(() => { + // clear call histories of mock functions + jest.clearAllMocks(); + }); + describe("login()", () => { + test("login success", async () => { + let log = ""; + const spy_console_log = jest.spyOn(console, "log").mockImplementation(s => log += `${s}\n`); + atcoder.login = jest.fn(async () => true); + + await login(); + expect(log).toMatchSnapshot(); + spy_console_log.mockRestore(); + }) + test("login fail", async () => { + let log = ""; + const spy_console_log = jest.spyOn(console, "log").mockImplementation(s => log += `${s}\n`); + atcoder.login = jest.fn(async () => false); + + await login(); + expect(log).toMatchSnapshot(); + spy_console_log.mockRestore(); + }) + }); +}); From b8e334a232a2c1eb25762cb61c4678844c57bc6a Mon Sep 17 00:00:00 2001 From: Tatamo Date: Sun, 15 Aug 2021 23:02:12 +0900 Subject: [PATCH 2/6] add tests for commands.logout() and commands.session() --- .../__tests__/__snapshots__/commands.ts.snap | 17 ++++++++++ tests/__tests__/commands.ts | 33 ++++++++++++++++++- 2 files changed, 49 insertions(+), 1 deletion(-) diff --git a/tests/__tests__/__snapshots__/commands.ts.snap b/tests/__tests__/__snapshots__/commands.ts.snap index 67df85a..7a8ff3d 100644 --- a/tests/__tests__/__snapshots__/commands.ts.snap +++ b/tests/__tests__/__snapshots__/commands.ts.snap @@ -9,3 +9,20 @@ exports[`login() login() login success 1`] = ` "OK " `; + +exports[`login() logout() logout 1`] = ` +"login session aborted. +" +`; + +exports[`login() session() logged-in 1`] = ` +"check login status... +OK +" +`; + +exports[`login() session() not logged-in 1`] = ` +"check login status... +not login +" +`; diff --git a/tests/__tests__/commands.ts b/tests/__tests__/commands.ts index 4347547..95ce530 100644 --- a/tests/__tests__/commands.ts +++ b/tests/__tests__/commands.ts @@ -1,4 +1,4 @@ -import { login } from "../../src/commands"; +import { login, logout, session } from "../../src/commands"; import { AtCoder } from "../../src/atcoder"; import { getAtCoder } from "../../src/di"; jest.mock("../../src/di"); @@ -34,4 +34,35 @@ describe("login()", () => { spy_console_log.mockRestore(); }) }); + describe("logout()", () => { + test("logout", async () => { + let log = ""; + const spy_console_log = jest.spyOn(console, "log").mockImplementation(s => log += `${s}\n`); + atcoder.logout = jest.fn(); + + await logout(); + expect(log).toMatchSnapshot(); + spy_console_log.mockRestore(); + }) + }); + describe("session()", () => { + test("logged-in", async () => { + let log = ""; + const spy_console_log = jest.spyOn(console, "log").mockImplementation(s => log += `${s}\n`); + atcoder.checkSession = jest.fn(async () => true); + + await session(); + expect(log).toMatchSnapshot(); + spy_console_log.mockRestore(); + }) + test("not logged-in", async () => { + let log = ""; + const spy_console_log = jest.spyOn(console, "log").mockImplementation(s => log += `${s}\n`); + atcoder.checkSession = jest.fn(async () => false); + + await session(); + expect(log).toMatchSnapshot(); + spy_console_log.mockRestore(); + }) + }); }); From 47688c87abb5653d68934b66d495a03d16080b8d Mon Sep 17 00:00:00 2001 From: Tatamo Date: Sun, 15 Aug 2021 23:14:07 +0900 Subject: [PATCH 3/6] add call time check --- tests/__tests__/commands.ts | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/tests/__tests__/commands.ts b/tests/__tests__/commands.ts index 95ce530..f51ffc8 100644 --- a/tests/__tests__/commands.ts +++ b/tests/__tests__/commands.ts @@ -22,6 +22,7 @@ describe("login()", () => { await login(); expect(log).toMatchSnapshot(); + expect(atcoder.login).toBeCalledTimes(1); spy_console_log.mockRestore(); }) test("login fail", async () => { @@ -31,6 +32,7 @@ describe("login()", () => { await login(); expect(log).toMatchSnapshot(); + expect(atcoder.login).toBeCalledTimes(1); spy_console_log.mockRestore(); }) }); @@ -42,6 +44,7 @@ describe("login()", () => { await logout(); expect(log).toMatchSnapshot(); + expect(atcoder.logout).toBeCalledTimes(1); spy_console_log.mockRestore(); }) }); @@ -53,6 +56,7 @@ describe("login()", () => { await session(); expect(log).toMatchSnapshot(); + expect(atcoder.checkSession).toBeCalledTimes(1); spy_console_log.mockRestore(); }) test("not logged-in", async () => { @@ -62,6 +66,7 @@ describe("login()", () => { await session(); expect(log).toMatchSnapshot(); + expect(atcoder.checkSession).toBeCalledTimes(1); spy_console_log.mockRestore(); }) }); From 5099cc788e9dea5939a4633bbdd39588abee2f66 Mon Sep 17 00:00:00 2001 From: Tatamo Date: Thu, 19 Aug 2021 01:56:22 +0900 Subject: [PATCH 4/6] change section name --- tests/__tests__/__snapshots__/commands.ts.snap | 10 +++++----- tests/__tests__/commands.ts | 2 +- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/tests/__tests__/__snapshots__/commands.ts.snap b/tests/__tests__/__snapshots__/commands.ts.snap index 7a8ff3d..85f7395 100644 --- a/tests/__tests__/__snapshots__/commands.ts.snap +++ b/tests/__tests__/__snapshots__/commands.ts.snap @@ -1,27 +1,27 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP -exports[`login() login() login fail 1`] = ` +exports[`Commands login() login fail 1`] = ` "login failed " `; -exports[`login() login() login success 1`] = ` +exports[`Commands login() login success 1`] = ` "OK " `; -exports[`login() logout() logout 1`] = ` +exports[`Commands logout() logout 1`] = ` "login session aborted. " `; -exports[`login() session() logged-in 1`] = ` +exports[`Commands session() logged-in 1`] = ` "check login status... OK " `; -exports[`login() session() not logged-in 1`] = ` +exports[`Commands session() not logged-in 1`] = ` "check login status... not login " diff --git a/tests/__tests__/commands.ts b/tests/__tests__/commands.ts index f51ffc8..00c6b97 100644 --- a/tests/__tests__/commands.ts +++ b/tests/__tests__/commands.ts @@ -3,7 +3,7 @@ import { AtCoder } from "../../src/atcoder"; import { getAtCoder } from "../../src/di"; jest.mock("../../src/di"); -describe("login()", () => { +describe("Commands", () => { let atcoder: AtCoder; beforeAll(async () => { atcoder = jest.genMockFromModule<{ AtCoder: AtCoder }>("../../src/atcoder").AtCoder; From 64bc21ac313665529436321f7aee0f88ecfd5163 Mon Sep 17 00:00:00 2001 From: Tatamo Date: Fri, 20 Aug 2021 01:04:21 +0900 Subject: [PATCH 5/6] add tests for commands.contest() --- .../__tests__/__snapshots__/commands.ts.snap | 46 +++++++ tests/__tests__/commands.ts | 128 +++++++++++++++--- 2 files changed, 158 insertions(+), 16 deletions(-) diff --git a/tests/__tests__/__snapshots__/commands.ts.snap b/tests/__tests__/__snapshots__/commands.ts.snap index 85f7395..c4b6ea6 100644 --- a/tests/__tests__/__snapshots__/commands.ts.snap +++ b/tests/__tests__/__snapshots__/commands.ts.snap @@ -1,5 +1,47 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP +exports[`Commands contest() contest id is given contest found, with id 1`] = ` +"aic987 AtCoder Imaginary Contest 987 https://atcoder.jp/contests/aic987 +" +`; + +exports[`Commands contest() contest id is given contest found, with id 2`] = `""`; + +exports[`Commands contest() contest id is given contest found, without id 1`] = ` +"AtCoder Imaginary Contest 987 https://atcoder.jp/contests/aic987 +" +`; + +exports[`Commands contest() contest id is given contest found, without id 2`] = `""`; + +exports[`Commands contest() contest id is given contest not found 1`] = `""`; + +exports[`Commands contest() contest id is given contest not found 2`] = ` +"contest \\"aic987654321\\" not found. +" +`; + +exports[`Commands contest() contest id is not given find project json found, with id 1`] = ` +"aic987 AtCoder Imaginary Contest 987 https://atcoder.jp/contests/aic987 +" +`; + +exports[`Commands contest() contest id is not given find project json found, with id 2`] = `""`; + +exports[`Commands contest() contest id is not given find project json found, without id 1`] = ` +"AtCoder Imaginary Contest 987 https://atcoder.jp/contests/aic987 +" +`; + +exports[`Commands contest() contest id is not given find project json found, without id 2`] = `""`; + +exports[`Commands contest() contest id is not given project json not found 1`] = `""`; + +exports[`Commands contest() contest id is not given project json not found 2`] = ` +"contest.acc.json not found. +" +`; + exports[`Commands login() login fail 1`] = ` "login failed " @@ -21,8 +63,12 @@ OK " `; +exports[`Commands session() logged-in 2`] = `""`; + exports[`Commands session() not logged-in 1`] = ` "check login status... not login " `; + +exports[`Commands session() not logged-in 2`] = `""`; diff --git a/tests/__tests__/commands.ts b/tests/__tests__/commands.ts index 00c6b97..b5869c6 100644 --- a/tests/__tests__/commands.ts +++ b/tests/__tests__/commands.ts @@ -1,11 +1,56 @@ -import { login, logout, session } from "../../src/commands"; +import { login, logout, session, contest } from "../../src/commands"; import { AtCoder } from "../../src/atcoder"; import { getAtCoder } from "../../src/di"; +import { Contest, ContestProject, findProjectJSON, Task } from "../../src/project" jest.mock("../../src/di"); +jest.mock("../../src/project") + +const dummy_contest: Contest = { + id: "aic987", + title: "AtCoder Imaginary Contest 987", + url: AtCoder.getContestURL("aic987") +}; + +const dummy_task01: Task = { + id: "aic987_a", + label: "A", + title: "This is Problem", + url: AtCoder.getTaskURL("aic987", "aic987_a") +}; + +const dummy_task02: Task = { + id: "aic987_b", + label: "B", + title: "Next Problem", + url: AtCoder.getTaskURL("aic987", "aic987_b") +}; + +const dummy_task03: Task = { + id: "aic987_c", + label: "C", + title: "The Test Cases", + url: AtCoder.getTaskURL("aic987", "aic987_c") +}; + +const dummy_task04: Task = { + id: "aic987_d", + label: "D", + title: "Imaginary Problem", + url: AtCoder.getTaskURL("aic987", "aic987_d") +}; + +const dummy_contest_project: ContestProject = { + contest: dummy_contest, + tasks: [dummy_task01, dummy_task02, dummy_task03, dummy_task04] +}; describe("Commands", () => { let atcoder: AtCoder; + let log = ""; + let elog = ""; beforeAll(async () => { + jest.spyOn(console, "log").mockImplementation(s => log += `${s}\n`); + jest.spyOn(console, "error").mockImplementation(s => elog += `${s}\n`); atcoder = jest.genMockFromModule<{ AtCoder: AtCoder }>("../../src/atcoder").AtCoder; // @ts-ignore: dynamically added method for test getAtCoder.mockResolvedValue(atcoder); @@ -13,61 +58,112 @@ describe("Commands", () => { beforeEach(() => { // clear call histories of mock functions jest.clearAllMocks(); + log = ""; + elog = ""; }); describe("login()", () => { test("login success", async () => { - let log = ""; - const spy_console_log = jest.spyOn(console, "log").mockImplementation(s => log += `${s}\n`); atcoder.login = jest.fn(async () => true); await login(); expect(log).toMatchSnapshot(); expect(atcoder.login).toBeCalledTimes(1); - spy_console_log.mockRestore(); }) test("login fail", async () => { - let log = ""; - const spy_console_log = jest.spyOn(console, "log").mockImplementation(s => log += `${s}\n`); atcoder.login = jest.fn(async () => false); await login(); expect(log).toMatchSnapshot(); expect(atcoder.login).toBeCalledTimes(1); - spy_console_log.mockRestore(); }) }); describe("logout()", () => { test("logout", async () => { - let log = ""; - const spy_console_log = jest.spyOn(console, "log").mockImplementation(s => log += `${s}\n`); atcoder.logout = jest.fn(); await logout(); expect(log).toMatchSnapshot(); expect(atcoder.logout).toBeCalledTimes(1); - spy_console_log.mockRestore(); }) }); describe("session()", () => { test("logged-in", async () => { - let log = ""; - const spy_console_log = jest.spyOn(console, "log").mockImplementation(s => log += `${s}\n`); atcoder.checkSession = jest.fn(async () => true); await session(); expect(log).toMatchSnapshot(); + expect(elog).toMatchSnapshot(); expect(atcoder.checkSession).toBeCalledTimes(1); - spy_console_log.mockRestore(); }) test("not logged-in", async () => { - let log = ""; - const spy_console_log = jest.spyOn(console, "log").mockImplementation(s => log += `${s}\n`); atcoder.checkSession = jest.fn(async () => false); await session(); expect(log).toMatchSnapshot(); + expect(elog).toMatchSnapshot(); expect(atcoder.checkSession).toBeCalledTimes(1); - spy_console_log.mockRestore(); }) }); + describe("contest()", () => { + describe("contest id is not given", () => { + test("find project json found, without id", async () => { + // @ts-ignore: dynamically added method for test + findProjectJSON.mockResolvedValueOnce({ path: "/dummy/path", data: dummy_contest_project }); + await contest(undefined, {}); + expect(log).toMatchSnapshot(); + expect(elog).toMatchSnapshot(); + expect(findProjectJSON).toBeCalledTimes(1); + }) + test("find project json found, with id", async () => { + // @ts-ignore: dynamically added method for test + findProjectJSON.mockResolvedValueOnce({ path: "/dummy/path", data: dummy_contest_project }); + await contest(undefined, { id: true }); + expect(log).toMatchSnapshot(); + expect(elog).toMatchSnapshot(); + expect(findProjectJSON).toBeCalledTimes(1); + }) + test("project json not found", async () => { + // @ts-ignore: dynamically added method for test + findProjectJSON.mockRejectedValueOnce(new Error("contest.acc.json not found.")); + await contest(undefined, {}); + expect(log).toMatchSnapshot(); + expect(elog).toMatchSnapshot(); + expect(findProjectJSON).toBeCalledTimes(1); + }); + }); + describe("contest id is given", () => { + test("contest found, without id", async () => { + atcoder.checkSession = jest.fn(async () => true); + atcoder.contest = jest.fn(async _ => dummy_contest); + await contest("aic987", {}); + expect(log).toMatchSnapshot(); + expect(elog).toMatchSnapshot(); + expect(findProjectJSON).toBeCalledTimes(0); + expect(atcoder.contest).toBeCalledTimes(1); + expect(atcoder.contest).toBeCalledWith("aic987"); + }); + test("contest found, with id", async () => { + atcoder.checkSession = jest.fn(async () => true); + atcoder.contest = jest.fn(async _ => dummy_contest); + await contest("aic987", { id: true }); + expect(log).toMatchSnapshot(); + expect(elog).toMatchSnapshot(); + expect(findProjectJSON).toBeCalledTimes(0); + expect(atcoder.contest).toBeCalledTimes(1); + expect(atcoder.contest).toBeCalledWith("aic987"); + }); + test("contest not found", async () => { + atcoder.checkSession = jest.fn(async () => true); + atcoder.contest = jest.fn(async _ => { + throw new Error(); + }); + await contest("aic987654321", {}); + expect(log).toMatchSnapshot(); + expect(elog).toMatchSnapshot(); + expect(findProjectJSON).toBeCalledTimes(0); + expect(atcoder.contest).toBeCalledTimes(1); + expect(atcoder.contest).toBeCalledWith("aic987654321"); + }); + }); + }); }); From 3a452028082ef65138d71c13688d8bf89d30145d Mon Sep 17 00:00:00 2001 From: Tatamo Date: Sat, 21 Aug 2021 00:16:09 +0900 Subject: [PATCH 6/6] add tests for commands.test() and commands.tests() --- .../__tests__/__snapshots__/commands.ts.snap | 151 +++++++++++++ tests/__tests__/commands.ts | 209 +++++++++++++++++- 2 files changed, 358 insertions(+), 2 deletions(-) diff --git a/tests/__tests__/__snapshots__/commands.ts.snap b/tests/__tests__/__snapshots__/commands.ts.snap index c4b6ea6..243f1d7 100644 --- a/tests/__tests__/__snapshots__/commands.ts.snap +++ b/tests/__tests__/__snapshots__/commands.ts.snap @@ -1,5 +1,12 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP +exports[`Commands contest() contest id is given contest found, try login and success 1`] = ` +"AtCoder Imaginary Contest 987 https://atcoder.jp/contests/aic987 +" +`; + +exports[`Commands contest() contest id is given contest found, try login and success 2`] = `""`; + exports[`Commands contest() contest id is given contest found, with id 1`] = ` "aic987 AtCoder Imaginary Contest 987 https://atcoder.jp/contests/aic987 " @@ -21,6 +28,13 @@ exports[`Commands contest() contest id is given contest not found 2`] = ` " `; +exports[`Commands contest() contest id is given try login and fail, but (public) contest found 1`] = ` +"AtCoder Imaginary Contest 987 https://atcoder.jp/contests/aic987 +" +`; + +exports[`Commands contest() contest id is given try login and fail, but (public) contest found 2`] = `""`; + exports[`Commands contest() contest id is not given find project json found, with id 1`] = ` "aic987 AtCoder Imaginary Contest 987 https://atcoder.jp/contests/aic987 " @@ -72,3 +86,140 @@ not login `; exports[`Commands session() not logged-in 2`] = `""`; + +exports[`Commands task() contest id and task id are given contest found, try login and success 1`] = ` +"A This is Problem https://atcoder.jp/contests/aic987/tasks/aic987_a +" +`; + +exports[`Commands task() contest id and task id are given contest found, try login and success 2`] = `""`; + +exports[`Commands task() contest id and task id are given contest not found 1`] = `""`; + +exports[`Commands task() contest id and task id are given contest not found 2`] = ` +"task \\"aic987_z\\" of contest \\"aic987\\" not found. +" +`; + +exports[`Commands task() contest id and task id are given task found, with id 1`] = ` +"aic987_a A This is Problem https://atcoder.jp/contests/aic987/tasks/aic987_a +" +`; + +exports[`Commands task() contest id and task id are given task found, with id 2`] = `""`; + +exports[`Commands task() contest id and task id are given task found, without id 1`] = ` +"A This is Problem https://atcoder.jp/contests/aic987/tasks/aic987_a +" +`; + +exports[`Commands task() contest id and task id are given task found, without id 2`] = `""`; + +exports[`Commands task() contest id and task id are given try login and fail, but (public) task found 1`] = ` +"A This is Problem https://atcoder.jp/contests/aic987/tasks/aic987_a +" +`; + +exports[`Commands task() contest id and task id are given try login and fail, but (public) task found 2`] = `""`; + +exports[`Commands task() contest id and task id are not given error occured in searching path 1`] = `""`; + +exports[`Commands task() contest id and task id are not given error occured in searching path 2`] = ` +"something wrong +" +`; + +exports[`Commands task() contest id and task id are not given task found, with id 1`] = ` +"aic987_a A This is Problem https://atcoder.jp/contests/aic987/tasks/aic987_a +" +`; + +exports[`Commands task() contest id and task id are not given task found, with id 2`] = `""`; + +exports[`Commands task() contest id and task id are not given task found, without id 1`] = ` +"A This is Problem https://atcoder.jp/contests/aic987/tasks/aic987_a +" +`; + +exports[`Commands task() contest id and task id are not given task found, without id 2`] = `""`; + +exports[`Commands task() contest id and task id are not given task not found 1`] = `""`; + +exports[`Commands task() contest id and task id are not given task not found 2`] = ` +"failed to find the task. +" +`; + +exports[`Commands tasks() contest id and task id are given contest found, try login and success 1`] = ` +"A This is Problem https://atcoder.jp/contests/aic987/tasks/aic987_a +B Next Problem https://atcoder.jp/contests/aic987/tasks/aic987_b +C The Test Cases https://atcoder.jp/contests/aic987/tasks/aic987_c +D Imaginary Problem https://atcoder.jp/contests/aic987/tasks/aic987_d +" +`; + +exports[`Commands tasks() contest id and task id are given contest found, try login and success 2`] = `""`; + +exports[`Commands tasks() contest id and task id are given contest not found 1`] = `""`; + +exports[`Commands tasks() contest id and task id are given contest not found 2`] = ` +"contest \\"aic987\\" not found. +" +`; + +exports[`Commands tasks() contest id and task id are given task found, with id 1`] = ` +"aic987_a A This is Problem https://atcoder.jp/contests/aic987/tasks/aic987_a +aic987_b B Next Problem https://atcoder.jp/contests/aic987/tasks/aic987_b +aic987_c C The Test Cases https://atcoder.jp/contests/aic987/tasks/aic987_c +aic987_d D Imaginary Problem https://atcoder.jp/contests/aic987/tasks/aic987_d +" +`; + +exports[`Commands tasks() contest id and task id are given task found, with id 2`] = `""`; + +exports[`Commands tasks() contest id and task id are given tasks found, without id 1`] = ` +"A This is Problem https://atcoder.jp/contests/aic987/tasks/aic987_a +B Next Problem https://atcoder.jp/contests/aic987/tasks/aic987_b +C The Test Cases https://atcoder.jp/contests/aic987/tasks/aic987_c +D Imaginary Problem https://atcoder.jp/contests/aic987/tasks/aic987_d +" +`; + +exports[`Commands tasks() contest id and task id are given tasks found, without id 2`] = `""`; + +exports[`Commands tasks() contest id and task id are given try login and fail, but (public) task found 1`] = ` +"A This is Problem https://atcoder.jp/contests/aic987/tasks/aic987_a +B Next Problem https://atcoder.jp/contests/aic987/tasks/aic987_b +C The Test Cases https://atcoder.jp/contests/aic987/tasks/aic987_c +D Imaginary Problem https://atcoder.jp/contests/aic987/tasks/aic987_d +" +`; + +exports[`Commands tasks() contest id and task id are given try login and fail, but (public) task found 2`] = `""`; + +exports[`Commands tasks() contest id and task id are not given tasks found, with id 1`] = ` +"aic987_a A This is Problem https://atcoder.jp/contests/aic987/tasks/aic987_a +aic987_b B Next Problem https://atcoder.jp/contests/aic987/tasks/aic987_b +aic987_c C The Test Cases https://atcoder.jp/contests/aic987/tasks/aic987_c +aic987_d D Imaginary Problem https://atcoder.jp/contests/aic987/tasks/aic987_d +" +`; + +exports[`Commands tasks() contest id and task id are not given tasks found, with id 2`] = `""`; + +exports[`Commands tasks() contest id and task id are not given tasks found, without id 1`] = ` +"A This is Problem https://atcoder.jp/contests/aic987/tasks/aic987_a +B Next Problem https://atcoder.jp/contests/aic987/tasks/aic987_b +C The Test Cases https://atcoder.jp/contests/aic987/tasks/aic987_c +D Imaginary Problem https://atcoder.jp/contests/aic987/tasks/aic987_d +" +`; + +exports[`Commands tasks() contest id and task id are not given tasks found, without id 2`] = `""`; + +exports[`Commands tasks() contest id and task id are not given tasks not found 1`] = `""`; + +exports[`Commands tasks() contest id and task id are not given tasks not found 2`] = ` +"contest.acc.json not found. +" +`; diff --git a/tests/__tests__/commands.ts b/tests/__tests__/commands.ts index b5869c6..f6dd7f1 100644 --- a/tests/__tests__/commands.ts +++ b/tests/__tests__/commands.ts @@ -1,7 +1,7 @@ -import { login, logout, session, contest } from "../../src/commands"; +import { login, logout, session, contest, task, tasks } from "../../src/commands"; import { AtCoder } from "../../src/atcoder"; import { getAtCoder } from "../../src/di"; -import { Contest, ContestProject, findProjectJSON, Task } from "../../src/project" +import { Contest, ContestProject, findProjectJSON, Task, detectTaskByPath } from "../../src/project" jest.mock("../../src/di"); jest.mock("../../src/project") @@ -152,6 +152,30 @@ describe("Commands", () => { expect(atcoder.contest).toBeCalledTimes(1); expect(atcoder.contest).toBeCalledWith("aic987"); }); + test("contest found, try login and success", async () => { + atcoder.checkSession = jest.fn(async () => false); + atcoder.login = jest.fn(async () => true); + atcoder.contest = jest.fn(async _ => dummy_contest); + await contest("aic987", {}); + expect(log).toMatchSnapshot(); + expect(elog).toMatchSnapshot(); + expect(findProjectJSON).toBeCalledTimes(0); + expect(atcoder.login).toBeCalledTimes(1); + expect(atcoder.contest).toBeCalledTimes(1); + expect(atcoder.contest).toBeCalledWith("aic987"); + }); + test("try login and fail, but (public) contest found", async () => { + atcoder.checkSession = jest.fn(async () => false); + atcoder.login = jest.fn(async () => false); + atcoder.contest = jest.fn(async _ => dummy_contest); + await contest("aic987", {}); + expect(log).toMatchSnapshot(); + expect(elog).toMatchSnapshot(); + expect(findProjectJSON).toBeCalledTimes(0); + expect(atcoder.login).toBeCalledTimes(1); + expect(atcoder.contest).toBeCalledTimes(1); + expect(atcoder.contest).toBeCalledWith("aic987"); + }); test("contest not found", async () => { atcoder.checkSession = jest.fn(async () => true); atcoder.contest = jest.fn(async _ => { @@ -166,4 +190,185 @@ describe("Commands", () => { }); }); }); + describe("task()", () => { + describe("contest id and task id are not given", () => { + test("task found, without id", async () => { + // @ts-ignore: dynamically added method for test + detectTaskByPath.mockResolvedValueOnce({ contest: dummy_contest, task: dummy_task01 }); + await task(undefined, undefined, {}); + expect(log).toMatchSnapshot(); + expect(elog).toMatchSnapshot(); + expect(detectTaskByPath).toBeCalledTimes(1); + }) + test("task found, with id", async () => { + // @ts-ignore: dynamically added method for test + detectTaskByPath.mockResolvedValueOnce({ contest: dummy_contest, task: dummy_task01 }); + await task(undefined, undefined, { id: true }); + expect(log).toMatchSnapshot(); + expect(elog).toMatchSnapshot(); + expect(detectTaskByPath).toBeCalledTimes(1); + }) + test("task not found", async () => { + // @ts-ignore: dynamically added method for test + detectTaskByPath.mockResolvedValueOnce({ contest: null, task: null }); + await task(undefined, undefined, {}); + expect(log).toMatchSnapshot(); + expect(elog).toMatchSnapshot(); + expect(detectTaskByPath).toBeCalledTimes(1); + }); + test("error occured in searching path", async () => { + // TODO: this path seems to be never reached + // @ts-ignore: dynamically added method for test + detectTaskByPath.mockRejectedValueOnce(new Error("something wrong")); + await task(undefined, undefined, {}); + expect(log).toMatchSnapshot(); + expect(elog).toMatchSnapshot(); + expect(detectTaskByPath).toBeCalledTimes(1); + }); + }); + describe("contest id and task id are given", () => { + test("task found, without id", async () => { + atcoder.checkSession = jest.fn(async () => true); + atcoder.task = jest.fn(async (contest_id, task_id) => dummy_task01); + await task("aic987", "aic987_a", {}); + expect(log).toMatchSnapshot(); + expect(elog).toMatchSnapshot(); + expect(detectTaskByPath).toBeCalledTimes(0); + expect(atcoder.task).toBeCalledTimes(1); + expect(atcoder.task).toBeCalledWith("aic987", "aic987_a"); + }); + test("task found, with id", async () => { + atcoder.checkSession = jest.fn(async () => true); + atcoder.task = jest.fn(async (contest_id, task_id) => dummy_task01); + await task("aic987", "aic987_a", { id: true }); + expect(log).toMatchSnapshot(); + expect(elog).toMatchSnapshot(); + expect(detectTaskByPath).toBeCalledTimes(0); + expect(atcoder.task).toBeCalledTimes(1); + expect(atcoder.task).toBeCalledWith("aic987", "aic987_a"); + }); + test("contest found, try login and success", async () => { + atcoder.checkSession = jest.fn(async () => false); + atcoder.login = jest.fn(async () => true); + atcoder.task = jest.fn(async (contest_id, task_id) => dummy_task01); + await task("aic987", "aic987_a", {}); + expect(log).toMatchSnapshot(); + expect(elog).toMatchSnapshot(); + expect(detectTaskByPath).toBeCalledTimes(0); + expect(atcoder.login).toBeCalledTimes(1); + expect(atcoder.task).toBeCalledTimes(1); + expect(atcoder.task).toBeCalledWith("aic987", "aic987_a"); + }); + test("try login and fail, but (public) task found", async () => { + atcoder.checkSession = jest.fn(async () => false); + atcoder.login = jest.fn(async () => false); + atcoder.task = jest.fn(async (contest_id, task_id) => dummy_task01); + await task("aic987", "aic987_a", {}); + expect(log).toMatchSnapshot(); + expect(elog).toMatchSnapshot(); + expect(detectTaskByPath).toBeCalledTimes(0); + expect(atcoder.login).toBeCalledTimes(1); + expect(atcoder.task).toBeCalledTimes(1); + expect(atcoder.task).toBeCalledWith("aic987", "aic987_a"); + }); + test("contest not found", async () => { + atcoder.checkSession = jest.fn(async () => true); + atcoder.task = jest.fn(async _ => { + throw new Error(); + }); + await task("aic987", "aic987_z", {}); + expect(log).toMatchSnapshot(); + expect(elog).toMatchSnapshot(); + expect(detectTaskByPath).toBeCalledTimes(0); + expect(atcoder.task).toBeCalledTimes(1); + expect(atcoder.task).toBeCalledWith("aic987", "aic987_z"); + }); + }); + }); + describe("tasks()", () => { + describe("contest id and task id are not given", () => { + test("tasks found, without id", async () => { + // @ts-ignore: dynamically added method for test + findProjectJSON.mockResolvedValueOnce({ path: "/dummy/path", data: dummy_contest_project }); + await tasks(undefined, {}); + expect(log).toMatchSnapshot(); + expect(elog).toMatchSnapshot(); + expect(findProjectJSON).toBeCalledTimes(1); + }) + test("tasks found, with id", async () => { + // @ts-ignore: dynamically added method for test + findProjectJSON.mockResolvedValueOnce({ path: "/dummy/path", data: dummy_contest_project }); + await tasks(undefined, { id: true }); + expect(log).toMatchSnapshot(); + expect(elog).toMatchSnapshot(); + expect(findProjectJSON).toBeCalledTimes(1); + }) + test("tasks not found", async () => { + // @ts-ignore: dynamically added method for test + findProjectJSON.mockRejectedValueOnce(new Error("contest.acc.json not found.")); + await tasks(undefined, {}); + expect(log).toMatchSnapshot(); + expect(elog).toMatchSnapshot(); + expect(findProjectJSON).toBeCalledTimes(1); + }); + }); + describe("contest id and task id are given", () => { + test("tasks found, without id", async () => { + atcoder.checkSession = jest.fn(async () => true); + atcoder.tasks = jest.fn(async (contest_id) => [dummy_task01, dummy_task02, dummy_task03, dummy_task04]); + await tasks("aic987", {}); + expect(log).toMatchSnapshot(); + expect(elog).toMatchSnapshot(); + expect(findProjectJSON).toBeCalledTimes(0); + expect(atcoder.tasks).toBeCalledTimes(1); + expect(atcoder.tasks).toBeCalledWith("aic987"); + }); + test("task found, with id", async () => { + atcoder.checkSession = jest.fn(async () => true); + atcoder.tasks = jest.fn(async (contest_id) => [dummy_task01, dummy_task02, dummy_task03, dummy_task04]); + await tasks("aic987", { id: true }); + expect(log).toMatchSnapshot(); + expect(elog).toMatchSnapshot(); + expect(findProjectJSON).toBeCalledTimes(0); + expect(atcoder.tasks).toBeCalledTimes(1); + expect(atcoder.tasks).toBeCalledWith("aic987"); + }); + test("contest found, try login and success", async () => { + atcoder.checkSession = jest.fn(async () => false); + atcoder.login = jest.fn(async () => true); + atcoder.tasks = jest.fn(async (contest_id) => [dummy_task01, dummy_task02, dummy_task03, dummy_task04]); + await tasks("aic987", {}); + expect(log).toMatchSnapshot(); + expect(elog).toMatchSnapshot(); + expect(findProjectJSON).toBeCalledTimes(0); + expect(atcoder.login).toBeCalledTimes(1); + expect(atcoder.tasks).toBeCalledTimes(1); + expect(atcoder.tasks).toBeCalledWith("aic987"); + }); + test("try login and fail, but (public) task found", async () => { + atcoder.checkSession = jest.fn(async () => false); + atcoder.login = jest.fn(async () => false); + atcoder.tasks = jest.fn(async (contest_id) => [dummy_task01, dummy_task02, dummy_task03, dummy_task04]); + await tasks("aic987", {}); + expect(log).toMatchSnapshot(); + expect(elog).toMatchSnapshot(); + expect(findProjectJSON).toBeCalledTimes(0); + expect(atcoder.login).toBeCalledTimes(1); + expect(atcoder.tasks).toBeCalledTimes(1); + expect(atcoder.tasks).toBeCalledWith("aic987"); + }); + test("contest not found", async () => { + atcoder.checkSession = jest.fn(async () => true); + atcoder.tasks = jest.fn(async _ => { + throw new Error(); + }); + await tasks("aic987", {}); + expect(log).toMatchSnapshot(); + expect(elog).toMatchSnapshot(); + expect(findProjectJSON).toBeCalledTimes(0); + expect(atcoder.tasks).toBeCalledTimes(1); + expect(atcoder.tasks).toBeCalledWith("aic987"); + }); + }); + }); }); pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy