Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
39 changes: 39 additions & 0 deletions backend/__tests__/__integration__/dal/connections.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -362,4 +362,43 @@ describe("ConnectionsDal", () => {
).toEqual([decoy]);
});
});

describe("getFriendsUids", () => {
it("should return friend uids", async () => {
//GIVE
const uid = new ObjectId().toHexString();
const friendOne = await createConnection({
initiatorUid: uid,
status: "accepted",
});
const friendTwo = await createConnection({
receiverUid: uid,
status: "accepted",
});
const friendThree = await createConnection({
receiverUid: uid,
status: "accepted",
});
const _pending = await createConnection({
initiatorUid: uid,
status: "pending",
});
const _blocked = await createConnection({
initiatorUid: uid,
status: "blocked",
});
const _decoy = await createConnection({});

//WHEN
const friendUids = await ConnectionsDal.getFriendsUids(uid);

//THEN
expect(friendUids).toEqual([
uid,
friendOne.receiverUid,
friendTwo.initiatorUid,
friendThree.initiatorUid,
]);
});
});
});
177 changes: 167 additions & 10 deletions backend/__tests__/__integration__/dal/leaderboards.isolated.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,12 @@ describe("LeaderboardsDal", () => {

//WHEN
await LeaderboardsDal.update("time", "15", "english");
const result = await LeaderboardsDal.get("time", "15", "english", 0, 50);
const results = await LeaderboardsDal.get("time", "15", "english", 0, 50);

//THEN
expect(result).toHaveLength(1);
expect(results).toHaveLength(1);
expect(
(result as LeaderboardsDal.DBLeaderboardEntry[])[0]
(results as LeaderboardsDal.DBLeaderboardEntry[])[0]
).toHaveProperty("uid", applicableUser.uid);
});

Expand All @@ -50,7 +50,7 @@ describe("LeaderboardsDal", () => {

//WHEN
await LeaderboardsDal.update("time", "15", "english");
const result = (await LeaderboardsDal.get(
const results = (await LeaderboardsDal.get(
"time",
"15",
"english",
Expand All @@ -59,7 +59,7 @@ describe("LeaderboardsDal", () => {
)) as DBLeaderboardEntry[];

//THEN
const lb = result.map((it) => _.omit(it, ["_id"]));
const lb = results.map((it) => _.omit(it, ["_id"]));

expect(lb).toEqual([
expectedLbEntry("15", { rank: 1, user: rank1 }),
Expand All @@ -77,7 +77,7 @@ describe("LeaderboardsDal", () => {

//WHEN
await LeaderboardsDal.update("time", "60", "english");
const result = (await LeaderboardsDal.get(
const results = (await LeaderboardsDal.get(
"time",
"60",
"english",
Expand All @@ -86,7 +86,7 @@ describe("LeaderboardsDal", () => {
)) as LeaderboardsDal.DBLeaderboardEntry[];

//THEN
const lb = result.map((it) => _.omit(it, ["_id"]));
const lb = results.map((it) => _.omit(it, ["_id"]));

expect(lb).toEqual([
expectedLbEntry("60", { rank: 1, user: rank1 }),
Expand Down Expand Up @@ -262,7 +262,7 @@ describe("LeaderboardsDal", () => {

//WHEN
await LeaderboardsDal.update("time", "15", "english");
const result = (await LeaderboardsDal.get(
const results = (await LeaderboardsDal.get(
"time",
"15",
"english",
Expand All @@ -272,14 +272,169 @@ describe("LeaderboardsDal", () => {
)) as DBLeaderboardEntry[];

//THEN
expect(result[0]?.isPremium).toBeUndefined();
expect(results[0]?.isPremium).toBeUndefined();
});
});

describe("get", () => {
it("should get for page", async () => {
//GIVEN
const _rank1 = await createUser(lbBests(pb(90), pb(105, 90, 2)));
const _rank2 = await createUser(lbBests(undefined, pb(100, 90, 1)));
const rank3 = await createUser(lbBests(undefined, pb(95, 80, 2)));
const rank4 = await createUser(lbBests(undefined, pb(90, 100, 1)));
await LeaderboardsDal.update("time", "60", "english");

//WHEN

const results = (await LeaderboardsDal.get(
"time",
"60",
"english",
1,
2,
true
)) as LeaderboardsDal.DBLeaderboardEntry[];

//THEN
const lb = results.map((it) => _.omit(it, ["_id"]));

expect(lb).toEqual([
expectedLbEntry("60", { rank: 3, user: rank3 }),
expectedLbEntry("60", { rank: 4, user: rank4 }),
]);
});
it("should get for friends only", async () => {
//GIVEN
const rank1 = await createUser(lbBests(pb(90), pb(100, 90, 2)));
const _rank2 = await createUser(lbBests(undefined, pb(100, 90, 1)));
const _rank3 = await createUser(lbBests(undefined, pb(100, 80, 2)));
const rank4 = await createUser(lbBests(undefined, pb(90, 100, 1)));
await LeaderboardsDal.update("time", "60", "english");

//WHEN

const results = (await LeaderboardsDal.get(
"time",
"60",
"english",
0,
50,
false,
[rank1.uid, rank4.uid]
)) as LeaderboardsDal.DBLeaderboardEntry[];

//THEN
const lb = results.map((it) => _.omit(it, ["_id"]));

expect(lb).toEqual([
expectedLbEntry("60", { rank: 1, user: rank1, friendsRank: 1 }),
expectedLbEntry("60", { rank: 4, user: rank4, friendsRank: 2 }),
]);
});
it("should get for friends only with page", async () => {
//GIVEN
const rank1 = await createUser(lbBests(pb(90), pb(105, 90, 2)));
const rank2 = await createUser(lbBests(undefined, pb(100, 90, 1)));
const _rank3 = await createUser(lbBests(undefined, pb(95, 80, 2)));
const rank4 = await createUser(lbBests(undefined, pb(90, 100, 1)));
await LeaderboardsDal.update("time", "60", "english");

//WHEN
const results = (await LeaderboardsDal.get(
"time",
"60",
"english",
1,
2,
false,
[rank1.uid, rank2.uid, rank4.uid]
)) as LeaderboardsDal.DBLeaderboardEntry[];

//THEN
const lb = results.map((it) => _.omit(it, ["_id"]));

expect(lb).toEqual([
expectedLbEntry("60", { rank: 4, user: rank4, friendsRank: 3 }),
]);
});
it("should return empty list if no friends", async () => {
//GIVEN

//WHEN
const results = (await LeaderboardsDal.get(
"time",
"60",
"english",
1,
2,
false,
[]
)) as LeaderboardsDal.DBLeaderboardEntry[];
//THEN
expect(results).toEqual([]);
});
});
describe("getCount / getRank", () => {
it("should get count", async () => {
//GIVEN
await createUser(lbBests(undefined, pb(105)));
await createUser(lbBests(undefined, pb(100)));
const me = await createUser(lbBests(undefined, pb(95)));
await createUser(lbBests(undefined, pb(90)));
await LeaderboardsDal.update("time", "60", "english");

//WHEN / THEN

expect(await LeaderboardsDal.getCount("time", "60", "english")) //
.toEqual(4);
expect(await LeaderboardsDal.getRank("time", "60", "english", me.uid)) //
.toEqual(
expect.objectContaining({
wpm: 95,
rank: 3,
name: me.name,
uid: me.uid,
})
);
});
it("should get for friends only", async () => {
//GIVEN
const friendOne = await createUser(lbBests(undefined, pb(105)));
await createUser(lbBests(undefined, pb(100)));
await createUser(lbBests(undefined, pb(95)));
const friendTwo = await createUser(lbBests(undefined, pb(90)));
const me = await createUser(lbBests(undefined, pb(99)));

console.log("me", me.uid);

await LeaderboardsDal.update("time", "60", "english");

const friends = [friendOne.uid, friendTwo.uid, me.uid];

//WHEN / THEN

expect(await LeaderboardsDal.getCount("time", "60", "english", friends)) //
.toEqual(3);
expect(
await LeaderboardsDal.getRank("time", "60", "english", me.uid, friends)
) //
.toEqual(
expect.objectContaining({
wpm: 99,
rank: 3,
friendsRank: 2,
name: me.name,
uid: me.uid,
})
);
});
});
});

function expectedLbEntry(
time: string,
{ rank, user, badgeId, isPremium }: ExpectedLbEntry
{ rank, user, badgeId, isPremium, friendsRank }: ExpectedLbEntry
) {
// @ts-expect-error
const lbBest: PersonalBest =
Expand All @@ -299,6 +454,7 @@ function expectedLbEntry(
discordAvatar: user.discordAvatar,
badgeId,
isPremium,
friendsRank,
};
}

Expand Down Expand Up @@ -351,4 +507,5 @@ type ExpectedLbEntry = {
user: UserDal.DBUser;
badgeId?: number;
isPremium?: boolean;
friendsRank?: number;
};
Loading