Skip to content

Commit 0a4f533

Browse files
committed
feat(tests): add tests for Credential and leetcode.cn
1 parent bc93beb commit 0a4f533

File tree

2 files changed

+108
-39
lines changed

2 files changed

+108
-39
lines changed

src/_tests/credential-cn.test.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import { describe, expect, it } from "vitest";
2+
import { Credential } from "../credential-cn";
3+
4+
describe("Credential", () => {
5+
it("should be able to pass session and csrf directly", async () => {
6+
const credential = new Credential({
7+
session: "test_session",
8+
csrf: "test_csrf",
9+
});
10+
expect(credential.csrf).toBe("test_csrf");
11+
expect(credential.session).toBe("test_session");
12+
});
13+
14+
it("should be able to init without session", async () => {
15+
const credential = new Credential();
16+
await credential.init();
17+
expect(credential.csrf).toBeDefined();
18+
expect(credential.session).toBeUndefined();
19+
});
20+
21+
it("should be able to init with session", async () => {
22+
const credential = new Credential();
23+
await credential.init("test_session");
24+
expect(credential.csrf).toBeDefined();
25+
expect(credential.session).toBe("test_session");
26+
});
27+
});

src/_tests/leetcode-cn.test.ts

Lines changed: 81 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1-
import { describe, expect, it } from "vitest";
1+
import dotenv from "dotenv";
2+
import { beforeAll, describe, expect, it } from "vitest";
23
import { Cache } from "../cache";
4+
import Credential from "../credential-cn";
35
import { LeetCodeCN } from "../leetcode-cn";
46

5-
describe("LeetCode", { timeout: 15_000 }, () => {
7+
describe("LeetCodeCN", { timeout: 15_000 }, () => {
68
describe("General", () => {
79
it("should be an instance of LeetCodeCN", () => {
810
const lc = new LeetCodeCN();
@@ -16,6 +18,48 @@ describe("LeetCode", { timeout: 15_000 }, () => {
1618
});
1719
});
1820

21+
describe("Authenticated", () => {
22+
dotenv.config();
23+
const credential = new Credential();
24+
let lc: LeetCodeCN;
25+
26+
beforeAll(async () => {
27+
await credential.init(process.env["TEST_CN_LEETCODE_SESSION"]);
28+
lc = new LeetCodeCN(credential);
29+
});
30+
31+
it.skipIf(!process.env["TEST_CN_LEETCODE_SESSION"])(
32+
"should be able to get own submissions with slug",
33+
async () => {
34+
const submissions = await lc.problem_submissions({
35+
limit: 30,
36+
offset: 0,
37+
slug: "two-sum",
38+
});
39+
expect(Array.isArray(submissions)).toBe(true);
40+
},
41+
);
42+
43+
it.skipIf(!process.env["TEST_CN_LEETCODE_SESSION"])(
44+
"should be able to get user progress questions",
45+
async () => {
46+
const progress = await lc.user_progress_questions({
47+
skip: 0,
48+
limit: 20,
49+
});
50+
expect(progress).toBeDefined();
51+
},
52+
);
53+
54+
it.skipIf(!process.env["TEST_CN_LEETCODE_SESSION"])(
55+
"should be able to get user signed in status",
56+
async () => {
57+
const user = await lc.userStatus();
58+
expect(user.isSignedIn).toBe(true);
59+
},
60+
);
61+
});
62+
1963
describe("Unauthenticated", () => {
2064
const lc = new LeetCodeCN();
2165
lc.limiter.limit = 100;
@@ -26,45 +70,43 @@ describe("LeetCode", { timeout: 15_000 }, () => {
2670

2771
it("should be able to get user profile", async () => {
2872
const user = await lc.user("LeetCode");
29-
expect(user.userProfilePublicProfile.username).toBe("LeetCode");
73+
expect(user.userProfilePublicProfile.profile.realName).toBe("LeetCode");
3074
});
3175

32-
it("should be able to use graphql", async () => {
33-
const { data } = await lc.graphql({
34-
operationName: "data",
35-
variables: { username: "LeetCode" },
36-
query: `
37-
query data($username: String!) {
38-
progress: userProfileUserQuestionProgress(userSlug: $username) {
39-
ac: numAcceptedQuestions { difficulty count }
40-
wa: numFailedQuestions { difficulty count }
41-
un: numUntouchedQuestions { difficulty count }
42-
}
43-
user: userProfilePublicProfile(userSlug: $username) {
44-
username
45-
ranking: siteRanking
46-
profile {
47-
realname: realName
48-
about: aboutMe
49-
avatar: userAvatar
50-
skills: skillTags
51-
country: countryName
52-
}
53-
}
54-
submissions: recentSubmitted(userSlug: $username) {
55-
id: submissionId
56-
status
57-
lang
58-
time: submitTime
59-
question {
60-
title: translatedTitle
61-
slug: titleSlug
62-
}
63-
}
64-
}
65-
`,
66-
});
67-
expect(data.user.username).toBe("LeetCode");
76+
it("should be able to get user's contest info", async () => {
77+
const contest = await lc.user_contest_info("LeetCode");
78+
expect(contest).toBeDefined();
79+
});
80+
81+
it("should be able to get user's recent submissions", async () => {
82+
const submissions = await lc.recent_submissions("LeetCode");
83+
expect(Array.isArray(submissions)).toBe(true);
84+
});
85+
86+
it("should be able to get problems list", async () => {
87+
const problems = await lc.problems({ limit: 10 });
88+
expect(problems.questions.length).toBe(10);
89+
});
90+
91+
it("should be able to get problem by slug", async () => {
92+
const problem = await lc.problem("two-sum");
93+
expect(problem.titleSlug).toBe("two-sum");
94+
});
95+
96+
it("should be able to get daily challenge", async () => {
97+
const daily = await lc.daily();
98+
expect(daily.question).toBeDefined();
99+
});
100+
101+
it("should be able to get user status", async () => {
102+
const user = await lc.userStatus();
103+
expect(user.isSignedIn).toBe(false);
104+
});
105+
106+
it("should throw error when trying to get submissions without slug", async () => {
107+
await expect(lc.problem_submissions({ limit: 30, offset: 0 })).rejects.toThrow(
108+
"LeetCodeCN requires slug parameter for submissions",
109+
);
68110
});
69111

70112
it("should be able to use graphql noj-go", async () => {

0 commit comments

Comments
 (0)