Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
cb04256
feat: add challenges (@fehmer, @ShizukoV)
fehmer Jul 5, 2026
94f32bc
trigger
fehmer Jul 5, 2026
36f5a4d
add some settings to challenges
fehmer Jul 5, 2026
aae4d31
use fromRequirements for config/funbox instead of duplicating it
fehmer Jul 5, 2026
c03aed0
Merge branch 'master' into feature/track-challenges
fehmer Jul 7, 2026
6331b43
Merge branch 'master' into feature/track-challenges
fehmer Jul 10, 2026
da5a7c1
non-autorole note
fehmer Jul 11, 2026
d19175c
style
fehmer Jul 11, 2026
8b24759
move validation to challenges package
fehmer Jul 18, 2026
efaa365
cleaup
fehmer Jul 18, 2026
cf4d9e8
backend validation
fehmer Jul 18, 2026
cd08f3e
fix challenge not verified
fehmer Jul 20, 2026
3e99f02
Merge branch 'master' into feature/track-challenges
fehmer Jul 20, 2026
d391bbf
fix lint issue from master
fehmer Jul 20, 2026
a80039d
always show challenge list on account page
fehmer Jul 20, 2026
a250034
new percentages
fehmer Jul 20, 2026
c7a1c01
new percentages
fehmer Jul 20, 2026
bc6f7f9
cap unlock percentage
fehmer Jul 20, 2026
73bd964
add challenge object only for linked accounts
fehmer Jul 20, 2026
9d23faf
streak roles
fehmer Sep 19, 2026
3cd94a9
chore: updated description from 760 days to 730 days (@ShizukoV) (#8443)
ShizukoV Oct 23, 2026
5a009f3
fix(challenges): updated role ID of the 250 hours challenge to match …
ShizukoV Oct 23, 2026
168945d
fix(challenges): removed the blazeIt challenge as it no longer exists…
ShizukoV Oct 23, 2026
e6dea24
missing dependency
fehmer Nov 17, 2026
6a0f7c2
Merge branch 'master' into feature/track-challenges
fehmer Nov 18, 2026
dc19ecb
chore: sort import
fehmer Nov 18, 2026
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 65 additions & 3 deletions backend/__tests__/__integration__/dal/user.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -755,6 +755,10 @@ describe("UserDal", () => {
const uid = new ObjectId().toHexString();
await UserDAL.addUser("test name", "test email", uid);

await UserDAL.updateChallenge(uid, "69");

await UserDAL.linkDiscord(uid, "testId", "testAvatar");

await UserDAL.updateProfile(
uid,
{
Expand Down Expand Up @@ -793,6 +797,10 @@ describe("UserDal", () => {
lastResultTimestamp: 0,
maxLength: 0,
});

expect(resetUser.discordAvatar).toBeUndefined();
expect(resetUser.discordId).toBeUndefined();
expect(resetUser.challenges).toBeUndefined();
});

it("getInbox should return the user's inbox", async () => {
Expand Down Expand Up @@ -1271,22 +1279,26 @@ describe("UserDal", () => {
describe("linkDiscord", () => {
it("throws for nonexisting user", async () => {
await expect(async () =>
UserDAL.linkDiscord("unknown", "", ""),
UserDAL.linkDiscord("unknown", "", "", {}),
).rejects.toThrow("User not found\nStack: link discord");
});
it("should update", async () => {
//given
const { uid } = await UserTestData.createUser({
discordId: "discordId",
discordAvatar: "discordAvatar",
challenges: {
"100hours": {},
},
});
//when
await UserDAL.linkDiscord(uid, "newId", "newAvatar");
await UserDAL.linkDiscord(uid, "newId", "newAvatar", { "250hours": {} });

//then
const read = await UserDAL.getUser(uid, "read");
expect(read.discordId).toEqual("newId");
expect(read.discordAvatar).toEqual("newAvatar");
expect(read.challenges).toEqual({ "250hours": {} });
});
it("should update without avatar", async () => {
//given
Expand All @@ -1303,6 +1315,22 @@ describe("UserDal", () => {
expect(read.discordId).toEqual("newId");
expect(read.discordAvatar).toEqual("discordAvatar");
});

it("should update without challenges", async () => {
//given
const { uid } = await UserTestData.createUser({
discordId: "discordId",
discordAvatar: "discordAvatar",
});
//creates without challenges
expect((await UserDAL.getUser(uid, "read")).challenges).toBeUndefined();

//when
await UserDAL.linkDiscord(uid, "newId", undefined, undefined);
//then
const read = await UserDAL.getUser(uid, "read");
expect(read.challenges).toEqual({});
});
});
describe("unlinkDiscord", () => {
it("throws for nonexisting user", async () => {
Expand All @@ -1312,9 +1340,13 @@ describe("UserDal", () => {
});
it("should update", async () => {
//given
const { uid } = await UserTestData.createUser({
const { uid, challenges } = await UserTestData.createUser({
discordId: "discordId",
discordAvatar: "discordAvatar",
challenges: {
"100hours": {},
"250hours": { addedAt: Date.now() },
},
});

//when
Expand All @@ -1324,6 +1356,36 @@ describe("UserDal", () => {
const read = await UserDAL.getUser(uid, "read");
expect(read.discordId).toBeUndefined();
expect(read.discordAvatar).toBeUndefined();
expect(read.challenges).toEqual(challenges);
});
});

describe("updateChallenge", () => {
it("throws for nonexisting user", async () => {
await expect(async () =>
UserDAL.updateChallenge("unknown", "69"),
).rejects.toThrow("User not found\nStack: update challenge");
});
it("should update", async () => {
//given
vi.useFakeTimers();
const { uid } = await UserTestData.createUser({
challenges: {
"100hours": {},
"250hours": { addedAt: 1 },
},
});

//when
await UserDAL.updateChallenge(uid, "69");

//then
const read = await UserDAL.getUser(uid, "read");
expect(read.challenges).toEqual({
"100hours": {},
"250hours": { addedAt: 1 },
"69": { addedAt: Date.now() },
});
});
});
describe("updateInbox", () => {
Expand Down
131 changes: 131 additions & 0 deletions backend/__tests__/api/controllers/result.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@ import * as Configuration from "../../../src/init/configuration";
import * as ResultDal from "../../../src/dal/result";
import * as UserDal from "../../../src/dal/user";
import * as PublicDal from "../../../src/dal/public";
import * as GeorgeQueue from "../../../src/queues/george-queue";
import * as LogsDal from "../../../src/dal/logs";
import { ObjectId } from "mongodb";
import { mockAuthenticateWithApeKey } from "../../__testData__/auth";
import { enableRateLimitExpects } from "../../__testData__/rate-limit";
import { DBResult } from "../../../src/utils/result";
import { omit } from "../../../src/utils/misc";
import { CompletedEvent } from "@monkeytype/schemas/results";
import * as ChallengeVerify from "@monkeytype/challenges/verify";

const { mockApp, uid } = setup();
const configuration = Configuration.getCachedConfiguration();
Expand Down Expand Up @@ -552,12 +554,18 @@ describe("result controller test", () => {
const userCheckIfPbMock = vi.spyOn(UserDal, "checkIfPb");
const userIncrementXpMock = vi.spyOn(UserDal, "incrementXp");
const userUpdateTypingStatsMock = vi.spyOn(UserDal, "updateTypingStats");
const userUpdateChallengeMock = vi.spyOn(UserDal, "updateChallenge");
const georgeAwardChallengeMock = vi.spyOn(
GeorgeQueue.default,
"awardChallenge",
);
const resultAddMock = vi.spyOn(ResultDal, "addResult");
const resultGetLastTimestampMock = vi.spyOn(
ResultDal,
"getLastResultTimestamp",
);
const publicUpdateStatsMock = vi.spyOn(PublicDal, "updateStats");
const challengeVerifyMock = vi.spyOn(ChallengeVerify, "verify");

beforeEach(async () => {
await enableResultsSaving(true);
Expand All @@ -570,19 +578,25 @@ describe("result controller test", () => {
userCheckIfPbMock,
userIncrementXpMock,
userUpdateTypingStatsMock,
userUpdateChallengeMock,
georgeAwardChallengeMock,
resultAddMock,
resultGetLastTimestampMock,
publicUpdateStatsMock,
challengeVerifyMock,
].forEach((it) => it.mockClear());

userGetMock.mockResolvedValue({ name: "bob" } as any);
userUpdateStreakMock.mockResolvedValue(0);
userCheckIfTagPbMock.mockResolvedValue([]);
userCheckIfPbMock.mockResolvedValue(true);
userUpdateChallengeMock.mockResolvedValue();
georgeAwardChallengeMock.mockResolvedValue();
resultAddMock.mockResolvedValue({ insertedId });
//a prior result exists so incomplete-test time is credited (not zeroed)
resultGetLastTimestampMock.mockResolvedValue(0);
userIncrementXpMock.mockResolvedValue();
challengeVerifyMock.mockReturnValue({ state: "success" } as any);
});

it("should add result", async () => {
Expand Down Expand Up @@ -664,6 +678,122 @@ describe("result controller test", () => {
15.1 + 2 - 5, //duration + incompleteTestSeconds-afk
);
});
it("should add result with challenge", async () => {
//GIVEN
userGetMock.mockClear();
userGetMock.mockResolvedValue({
uid,
name: "bob",
discordId: "discordId",
} as any);

const completedEvent = buildCompletedEvent({
challenge: "69",
});
//WHEN
await mockApp
.post("/results")
.set("Authorization", `Bearer ${uid}`)
.send({
result: completedEvent,
})
.expect(200);

//THEN
expect(userUpdateChallengeMock).toHaveBeenCalledWith(uid, "69");
expect(georgeAwardChallengeMock).toHaveBeenCalledWith("discordId", "69");
});

it("should not add challenge if not auto-role", async () => {
//GIVEN
userGetMock.mockClear();
userGetMock.mockResolvedValue({
uid,
name: "bob",
discordId: "discordId",
} as any);

const completedEvent = buildCompletedEvent({
challenge: "roleAddict",
});
//WHEN
await mockApp
.post("/results")
.set("Authorization", `Bearer ${uid}`)
.send({
result: completedEvent,
})
.expect(200);

//THEN
expect(userUpdateChallengeMock).not.toHaveBeenCalled();
expect(georgeAwardChallengeMock).not.toHaveBeenCalled();
});
it("should dd challenge without discord connected", async () => {
const completedEvent = buildCompletedEvent({
challenge: "69",
});
//WHEN
await mockApp
.post("/results")
.set("Authorization", `Bearer ${uid}`)
.send({
result: completedEvent,
})
.expect(200);

//THEN
expect(userUpdateChallengeMock).toHaveBeenCalledWith(uid, "69");
expect(georgeAwardChallengeMock).not.toHaveBeenCalled();
});
it("should fail when challenge verification fails", async () => {
//GIVEN
challengeVerifyMock.mockReturnValue({
state: "failed",
reason: "Challenge failed: WPM below 69",
} as any);

//WHEN
const { body } = await mockApp
.post("/results")
.set("Authorization", `Bearer ${uid}`)
.send({
result: buildCompletedEvent({
challenge: "69",
}),
})
.expect(400);

//THEN
expect(body.message).toEqual("Challenge failed: WPM below 69");
expect(userUpdateChallengeMock).not.toHaveBeenCalled();
expect(georgeAwardChallengeMock).not.toHaveBeenCalled();
});

it("should fail when challenge verification has error", async () => {
//GIVEN
challengeVerifyMock.mockReturnValue({
state: "error",
errorMessage: "failed to verify challenge",
} as any);

//WHEN
const { body } = await mockApp
.post("/results")
.set("Authorization", `Bearer ${uid}`)
.send({
result: buildCompletedEvent({
challenge: "69",
}),
})
.expect(500);

//THEN
expect(body.message).toEqual("failed to verify challenge");
expect(userUpdateChallengeMock).not.toHaveBeenCalled();
expect(georgeAwardChallengeMock).not.toHaveBeenCalled();
});

it("should add first result finished before signup within the grace", async () => {
//GIVEN
resultGetLastTimestampMock.mockRejectedValue(
Expand Down Expand Up @@ -702,6 +832,7 @@ describe("result controller test", () => {
expect(body.message).toEqual("Invalid result spacing");
expect(resultAddMock).not.toHaveBeenCalled();
});

it("should fail if result saving is disabled", async () => {
//GIVEN
await enableResultsSaving(false);
Expand Down
Loading
Loading