2022-09-24 16:20:54 +08:00
|
|
|
import "@testing-library/jest-dom";
|
|
|
|
import axios from "axios";
|
|
|
|
import MockAdapter from "axios-mock-adapter";
|
2022-09-24 20:10:06 +08:00
|
|
|
import { calculateRank } from "../src/calculateRank.js";
|
|
|
|
import { fetchStats } from "../src/fetchers/stats-fetcher.js";
|
2023-07-03 14:54:19 +08:00
|
|
|
import { expect, it, describe, beforeEach, afterEach } from "@jest/globals";
|
2020-07-12 00:40:13 +08:00
|
|
|
|
2023-01-22 01:32:37 +08:00
|
|
|
// Test parameters.
|
|
|
|
const data_stats = {
|
2020-07-12 00:40:13 +08:00
|
|
|
data: {
|
|
|
|
user: {
|
|
|
|
name: "Anurag Hazra",
|
|
|
|
repositoriesContributedTo: { totalCount: 61 },
|
2020-07-31 16:07:39 +08:00
|
|
|
contributionsCollection: {
|
|
|
|
totalCommitContributions: 100,
|
2023-06-15 15:35:09 +08:00
|
|
|
totalPullRequestReviewContributions: 50,
|
2020-07-31 16:07:39 +08:00
|
|
|
},
|
2020-07-12 00:40:13 +08:00
|
|
|
pullRequests: { totalCount: 300 },
|
2021-10-23 16:20:33 +08:00
|
|
|
openIssues: { totalCount: 100 },
|
|
|
|
closedIssues: { totalCount: 100 },
|
2020-07-13 22:11:47 +08:00
|
|
|
followers: { totalCount: 100 },
|
2023-06-19 11:51:58 +08:00
|
|
|
repositoryDiscussions: { totalCount: 10 },
|
|
|
|
repositoryDiscussionComments: { totalCount: 40 },
|
2020-07-12 00:40:13 +08:00
|
|
|
repositories: {
|
2020-07-13 22:11:47 +08:00
|
|
|
totalCount: 5,
|
2020-07-12 00:40:13 +08:00
|
|
|
nodes: [
|
2022-09-16 19:09:19 +08:00
|
|
|
{ name: "test-repo-1", stargazers: { totalCount: 100 } },
|
|
|
|
{ name: "test-repo-2", stargazers: { totalCount: 100 } },
|
|
|
|
{ name: "test-repo-3", stargazers: { totalCount: 100 } },
|
2022-10-04 19:10:50 +08:00
|
|
|
],
|
|
|
|
pageInfo: {
|
|
|
|
hasNextPage: true,
|
2023-05-06 16:07:03 +08:00
|
|
|
endCursor: "cursor",
|
2022-10-04 19:10:50 +08:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2023-01-22 01:32:37 +08:00
|
|
|
const data_repo = {
|
2022-10-04 19:10:50 +08:00
|
|
|
data: {
|
|
|
|
user: {
|
|
|
|
repositories: {
|
|
|
|
nodes: [
|
2022-09-16 19:09:19 +08:00
|
|
|
{ name: "test-repo-4", stargazers: { totalCount: 50 } },
|
|
|
|
{ name: "test-repo-5", stargazers: { totalCount: 50 } },
|
2020-07-12 00:40:13 +08:00
|
|
|
],
|
2022-10-04 19:10:50 +08:00
|
|
|
pageInfo: {
|
|
|
|
hasNextPage: false,
|
2023-05-06 16:07:03 +08:00
|
|
|
endCursor: "cursor",
|
2022-10-04 19:10:50 +08:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2023-01-22 01:32:37 +08:00
|
|
|
const data_repo_zero_stars = {
|
2022-10-04 19:10:50 +08:00
|
|
|
data: {
|
|
|
|
user: {
|
|
|
|
repositories: {
|
|
|
|
nodes: [
|
|
|
|
{ name: "test-repo-1", stargazers: { totalCount: 100 } },
|
|
|
|
{ name: "test-repo-2", stargazers: { totalCount: 100 } },
|
|
|
|
{ name: "test-repo-3", stargazers: { totalCount: 100 } },
|
|
|
|
{ name: "test-repo-4", stargazers: { totalCount: 0 } },
|
|
|
|
{ name: "test-repo-5", stargazers: { totalCount: 0 } },
|
|
|
|
],
|
|
|
|
pageInfo: {
|
|
|
|
hasNextPage: true,
|
2023-05-06 16:07:03 +08:00
|
|
|
endCursor: "cursor",
|
2022-10-04 19:10:50 +08:00
|
|
|
},
|
2020-07-12 00:40:13 +08:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
const error = {
|
|
|
|
errors: [
|
|
|
|
{
|
|
|
|
type: "NOT_FOUND",
|
|
|
|
path: ["user"],
|
|
|
|
locations: [],
|
|
|
|
message: "Could not resolve to a User with the login of 'noname'.",
|
|
|
|
},
|
|
|
|
],
|
|
|
|
};
|
|
|
|
|
|
|
|
const mock = new MockAdapter(axios);
|
|
|
|
|
2022-10-04 19:10:50 +08:00
|
|
|
beforeEach(() => {
|
2023-01-22 01:32:37 +08:00
|
|
|
process.env.FETCH_MULTI_PAGE_STARS = "false"; // Set to `false` to fetch only one page of stars.
|
2023-05-06 16:07:03 +08:00
|
|
|
mock.onPost("https://api.github.com/graphql").reply((cfg) => {
|
|
|
|
return [
|
|
|
|
200,
|
|
|
|
cfg.data.includes("contributionsCollection") ? data_stats : data_repo,
|
|
|
|
];
|
|
|
|
});
|
2022-10-04 19:10:50 +08:00
|
|
|
});
|
|
|
|
|
2020-07-12 00:40:13 +08:00
|
|
|
afterEach(() => {
|
|
|
|
mock.reset();
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("Test fetchStats", () => {
|
|
|
|
it("should fetch correct stats", async () => {
|
|
|
|
let stats = await fetchStats("anuraghazra");
|
2020-07-13 22:11:47 +08:00
|
|
|
const rank = calculateRank({
|
2023-05-26 21:39:35 +08:00
|
|
|
all_commits: false,
|
|
|
|
commits: 100,
|
2020-07-13 22:11:47 +08:00
|
|
|
prs: 300,
|
|
|
|
issues: 200,
|
2023-05-26 21:39:35 +08:00
|
|
|
repos: 5,
|
|
|
|
stars: 300,
|
|
|
|
followers: 100,
|
2020-07-13 22:11:47 +08:00
|
|
|
});
|
|
|
|
|
2020-07-12 00:40:13 +08:00
|
|
|
expect(stats).toStrictEqual({
|
|
|
|
contributedTo: 61,
|
|
|
|
name: "Anurag Hazra",
|
|
|
|
totalCommits: 100,
|
|
|
|
totalIssues: 200,
|
|
|
|
totalPRs: 300,
|
2023-06-15 15:35:09 +08:00
|
|
|
totalReviews: 50,
|
2023-01-22 01:32:37 +08:00
|
|
|
totalStars: 300,
|
2023-06-19 11:51:58 +08:00
|
|
|
totalDiscussionsStarted: 10,
|
|
|
|
totalDiscussionsAnswered: 40,
|
2020-07-13 22:11:47 +08:00
|
|
|
rank,
|
2020-07-12 00:40:13 +08:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2022-10-04 19:10:50 +08:00
|
|
|
it("should stop fetching when there are repos with zero stars", async () => {
|
|
|
|
mock.reset();
|
|
|
|
mock
|
|
|
|
.onPost("https://api.github.com/graphql")
|
2023-01-22 01:32:37 +08:00
|
|
|
.replyOnce(200, data_stats)
|
2022-10-04 19:10:50 +08:00
|
|
|
.onPost("https://api.github.com/graphql")
|
2023-01-22 01:32:37 +08:00
|
|
|
.replyOnce(200, data_repo_zero_stars);
|
2022-10-04 19:10:50 +08:00
|
|
|
|
|
|
|
let stats = await fetchStats("anuraghazra");
|
|
|
|
const rank = calculateRank({
|
2023-05-26 21:39:35 +08:00
|
|
|
all_commits: false,
|
|
|
|
commits: 100,
|
2022-10-04 19:10:50 +08:00
|
|
|
prs: 300,
|
|
|
|
issues: 200,
|
2023-05-26 21:39:35 +08:00
|
|
|
repos: 5,
|
|
|
|
stars: 300,
|
|
|
|
followers: 100,
|
2022-10-04 19:10:50 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
expect(stats).toStrictEqual({
|
|
|
|
contributedTo: 61,
|
|
|
|
name: "Anurag Hazra",
|
|
|
|
totalCommits: 100,
|
|
|
|
totalIssues: 200,
|
|
|
|
totalPRs: 300,
|
2023-06-15 15:35:09 +08:00
|
|
|
totalReviews: 50,
|
2022-10-04 19:10:50 +08:00
|
|
|
totalStars: 300,
|
2023-06-19 11:51:58 +08:00
|
|
|
totalDiscussionsStarted: 10,
|
|
|
|
totalDiscussionsAnswered: 40,
|
2022-10-04 19:10:50 +08:00
|
|
|
rank,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2020-07-12 00:40:13 +08:00
|
|
|
it("should throw error", async () => {
|
2022-10-04 19:10:50 +08:00
|
|
|
mock.reset();
|
2020-07-12 00:40:13 +08:00
|
|
|
mock.onPost("https://api.github.com/graphql").reply(200, error);
|
|
|
|
|
|
|
|
await expect(fetchStats("anuraghazra")).rejects.toThrow(
|
2020-09-25 00:08:14 +08:00
|
|
|
"Could not resolve to a User with the login of 'noname'.",
|
2020-07-12 00:40:13 +08:00
|
|
|
);
|
|
|
|
});
|
2020-07-24 22:04:38 +08:00
|
|
|
|
2020-07-31 16:07:39 +08:00
|
|
|
it("should fetch total commits", async () => {
|
|
|
|
mock
|
|
|
|
.onGet("https://api.github.com/search/commits?q=author:anuraghazra")
|
|
|
|
.reply(200, { total_count: 1000 });
|
|
|
|
|
2023-06-07 16:21:28 +08:00
|
|
|
let stats = await fetchStats("anuraghazra", true);
|
2020-07-31 16:07:39 +08:00
|
|
|
const rank = calculateRank({
|
2023-05-26 21:39:35 +08:00
|
|
|
all_commits: true,
|
|
|
|
commits: 1000,
|
2020-07-31 16:07:39 +08:00
|
|
|
prs: 300,
|
|
|
|
issues: 200,
|
2023-05-26 21:39:35 +08:00
|
|
|
repos: 5,
|
|
|
|
stars: 300,
|
|
|
|
followers: 100,
|
2020-07-31 16:07:39 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
expect(stats).toStrictEqual({
|
|
|
|
contributedTo: 61,
|
|
|
|
name: "Anurag Hazra",
|
2023-05-26 21:39:35 +08:00
|
|
|
totalCommits: 1000,
|
2020-07-31 16:07:39 +08:00
|
|
|
totalIssues: 200,
|
|
|
|
totalPRs: 300,
|
2023-06-15 15:35:09 +08:00
|
|
|
totalReviews: 50,
|
2023-01-22 01:32:37 +08:00
|
|
|
totalStars: 300,
|
2023-06-19 11:51:58 +08:00
|
|
|
totalDiscussionsStarted: 10,
|
|
|
|
totalDiscussionsAnswered: 40,
|
2020-07-31 16:07:39 +08:00
|
|
|
rank,
|
|
|
|
});
|
|
|
|
});
|
2022-09-16 19:09:19 +08:00
|
|
|
|
|
|
|
it("should exclude stars of the `test-repo-1` repository", async () => {
|
|
|
|
mock
|
|
|
|
.onGet("https://api.github.com/search/commits?q=author:anuraghazra")
|
|
|
|
.reply(200, { total_count: 1000 });
|
|
|
|
|
2023-06-07 16:21:28 +08:00
|
|
|
let stats = await fetchStats("anuraghazra", true, ["test-repo-1"]);
|
2022-09-16 19:09:19 +08:00
|
|
|
const rank = calculateRank({
|
2023-05-26 21:39:35 +08:00
|
|
|
all_commits: true,
|
|
|
|
commits: 1000,
|
2022-09-16 19:09:19 +08:00
|
|
|
prs: 300,
|
|
|
|
issues: 200,
|
2023-05-26 21:39:35 +08:00
|
|
|
repos: 5,
|
|
|
|
stars: 200,
|
|
|
|
followers: 100,
|
2022-09-16 19:09:19 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
expect(stats).toStrictEqual({
|
|
|
|
contributedTo: 61,
|
|
|
|
name: "Anurag Hazra",
|
2023-05-26 21:39:35 +08:00
|
|
|
totalCommits: 1000,
|
2022-09-16 19:09:19 +08:00
|
|
|
totalIssues: 200,
|
|
|
|
totalPRs: 300,
|
2023-06-15 15:35:09 +08:00
|
|
|
totalReviews: 50,
|
2023-01-22 01:32:37 +08:00
|
|
|
totalStars: 200,
|
2023-06-19 11:51:58 +08:00
|
|
|
totalDiscussionsStarted: 10,
|
|
|
|
totalDiscussionsAnswered: 40,
|
2023-01-22 01:32:37 +08:00
|
|
|
rank,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should fetch two pages of stars if 'FETCH_MULTI_PAGE_STARS' env variable is set to `true`", async () => {
|
|
|
|
process.env.FETCH_MULTI_PAGE_STARS = true;
|
|
|
|
|
|
|
|
let stats = await fetchStats("anuraghazra");
|
|
|
|
const rank = calculateRank({
|
2023-05-26 21:39:35 +08:00
|
|
|
all_commits: false,
|
|
|
|
commits: 100,
|
2023-01-22 01:32:37 +08:00
|
|
|
prs: 300,
|
|
|
|
issues: 200,
|
2023-05-26 21:39:35 +08:00
|
|
|
repos: 5,
|
|
|
|
stars: 400,
|
|
|
|
followers: 100,
|
2023-01-22 01:32:37 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
expect(stats).toStrictEqual({
|
|
|
|
contributedTo: 61,
|
|
|
|
name: "Anurag Hazra",
|
|
|
|
totalCommits: 100,
|
|
|
|
totalIssues: 200,
|
|
|
|
totalPRs: 300,
|
2023-06-15 15:35:09 +08:00
|
|
|
totalReviews: 50,
|
2023-01-22 01:32:37 +08:00
|
|
|
totalStars: 400,
|
2023-06-19 11:51:58 +08:00
|
|
|
totalDiscussionsStarted: 10,
|
|
|
|
totalDiscussionsAnswered: 40,
|
2023-01-22 01:32:37 +08:00
|
|
|
rank,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should fetch one page of stars if 'FETCH_MULTI_PAGE_STARS' env variable is set to `false`", async () => {
|
|
|
|
process.env.FETCH_MULTI_PAGE_STARS = "false";
|
|
|
|
|
|
|
|
let stats = await fetchStats("anuraghazra");
|
|
|
|
const rank = calculateRank({
|
2023-05-26 21:39:35 +08:00
|
|
|
all_commits: false,
|
|
|
|
commits: 100,
|
2023-01-22 01:32:37 +08:00
|
|
|
prs: 300,
|
|
|
|
issues: 200,
|
2023-05-26 21:39:35 +08:00
|
|
|
repos: 5,
|
|
|
|
stars: 300,
|
|
|
|
followers: 100,
|
2023-01-22 01:32:37 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
expect(stats).toStrictEqual({
|
|
|
|
contributedTo: 61,
|
|
|
|
name: "Anurag Hazra",
|
|
|
|
totalCommits: 100,
|
|
|
|
totalIssues: 200,
|
|
|
|
totalPRs: 300,
|
2023-06-15 15:35:09 +08:00
|
|
|
totalReviews: 50,
|
2023-01-22 01:32:37 +08:00
|
|
|
totalStars: 300,
|
2023-06-19 11:51:58 +08:00
|
|
|
totalDiscussionsStarted: 10,
|
|
|
|
totalDiscussionsAnswered: 40,
|
2023-01-22 01:32:37 +08:00
|
|
|
rank,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should fetch one page of stars if 'FETCH_MULTI_PAGE_STARS' env variable is not set", async () => {
|
|
|
|
process.env.FETCH_MULTI_PAGE_STARS = undefined;
|
|
|
|
|
|
|
|
let stats = await fetchStats("anuraghazra");
|
|
|
|
const rank = calculateRank({
|
2023-05-26 21:39:35 +08:00
|
|
|
all_commits: false,
|
|
|
|
commits: 100,
|
2023-01-22 01:32:37 +08:00
|
|
|
prs: 300,
|
|
|
|
issues: 200,
|
2023-05-26 21:39:35 +08:00
|
|
|
repos: 5,
|
|
|
|
stars: 300,
|
|
|
|
followers: 100,
|
2023-01-22 01:32:37 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
expect(stats).toStrictEqual({
|
|
|
|
contributedTo: 61,
|
|
|
|
name: "Anurag Hazra",
|
|
|
|
totalCommits: 100,
|
|
|
|
totalIssues: 200,
|
|
|
|
totalPRs: 300,
|
2023-06-15 15:35:09 +08:00
|
|
|
totalReviews: 50,
|
2023-01-22 01:32:37 +08:00
|
|
|
totalStars: 300,
|
2023-06-19 11:51:58 +08:00
|
|
|
totalDiscussionsStarted: 10,
|
|
|
|
totalDiscussionsAnswered: 40,
|
2022-09-16 19:09:19 +08:00
|
|
|
rank,
|
|
|
|
});
|
|
|
|
});
|
2020-07-31 16:07:39 +08:00
|
|
|
});
|