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
89 changes: 89 additions & 0 deletions backend/__tests__/__integration__/dal/connections.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { ObjectId } from "mongodb";

import * as ConnectionsDal from "../../../src/dal/connections";
import { createConnection } from "../../__testData__/connections";
import { createUser } from "../../__testData__/users";

describe("ConnectionsDal", () => {
beforeAll(async () => {
Expand Down Expand Up @@ -401,4 +402,92 @@ describe("ConnectionsDal", () => {
]);
});
});

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

//WHEN
const friendUids = await ConnectionsDal.aggregateWithAcceptedConnections<{
uid: string;
}>({ collectionName: "users", uid }, [{ $project: { uid: true } }]);

//THEN
expect(friendUids.flatMap((it) => it.uid).toSorted()).toEqual([
uid,
friendOne.receiverUid,
friendTwo.initiatorUid,
friendThree.initiatorUid,
]);
});
it("should return friend uids and metaData", async () => {
//GIVE
const me = await createUser();
const friend = await createUser();

const connection = await createConnection({
initiatorUid: me.uid,
receiverUid: friend.uid,
status: "accepted",
});

//WHEN
const friendUids = await ConnectionsDal.aggregateWithAcceptedConnections(
{ collectionName: "users", uid: me.uid, includeMetaData: true },
[
{
$project: {
uid: true,
lastModified: "$connectionMeta.lastModified",
connectionId: "$connectionMeta._id",
},
},
]
);

//THEN
expect(friendUids).toEqual([
{
_id: friend._id,
connectionId: connection._id,
lastModified: connection.lastModified,
uid: friend.uid,
},
{
_id: me._id,
uid: me.uid,
},
]);
});
});
});
60 changes: 46 additions & 14 deletions backend/__tests__/__integration__/dal/leaderboards.isolated.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { describe, it, expect, afterEach, vi } from "vitest";
import { describe, it, expect, vi, afterEach } from "vitest";
import _ from "lodash";
import { ObjectId } from "mongodb";
import * as UserDal from "../../../src/dal/user";
Expand All @@ -11,6 +11,7 @@ import * as DB from "../../../src/init/db";
import { LbPersonalBests } from "../../../src/utils/pb";

import { pb } from "../../__testData__/users";
import { createConnection } from "../../__testData__/connections";

describe("LeaderboardsDal", () => {
afterEach(async () => {
Expand Down Expand Up @@ -307,9 +308,20 @@ describe("LeaderboardsDal", () => {
it("should get for friends only", async () => {
//GIVEN
const rank1 = await createUser(lbBests(pb(90), pb(100, 90, 2)));
const uid = rank1.uid;
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)));

//two friends, one is not on the leaderboard
await createConnection({
initiatorUid: uid,
receiverUid: rank4.uid,
status: "accepted",
});

await createConnection({ initiatorUid: uid, status: "accepted" });

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

//WHEN
Expand All @@ -321,7 +333,7 @@ describe("LeaderboardsDal", () => {
0,
50,
false,
[rank1.uid, rank4.uid]
uid
)) as LeaderboardsDal.DBLeaderboardEntry[];

//THEN
Expand All @@ -335,11 +347,23 @@ describe("LeaderboardsDal", () => {
it("should get for friends only with page", async () => {
//GIVEN
const rank1 = await createUser(lbBests(pb(90), pb(105, 90, 2)));
const uid = rank1.uid;
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");

await createConnection({
initiatorUid: uid,
receiverUid: rank2.uid,
status: "accepted",
});
await createConnection({
initiatorUid: rank4.uid,
receiverUid: uid,
status: "accepted",
});

//WHEN
const results = (await LeaderboardsDal.get(
"time",
Expand All @@ -348,7 +372,7 @@ describe("LeaderboardsDal", () => {
1,
2,
false,
[rank1.uid, rank2.uid, rank4.uid]
uid
)) as LeaderboardsDal.DBLeaderboardEntry[];

//THEN
Expand All @@ -360,6 +384,7 @@ describe("LeaderboardsDal", () => {
});
it("should return empty list if no friends", async () => {
//GIVEN
const uid = new ObjectId().toHexString();

//WHEN
const results = (await LeaderboardsDal.get(
Expand All @@ -369,7 +394,7 @@ describe("LeaderboardsDal", () => {
1,
2,
false,
[]
uid
)) as LeaderboardsDal.DBLeaderboardEntry[];
//THEN
expect(results).toEqual([]);
Expand All @@ -378,10 +403,10 @@ describe("LeaderboardsDal", () => {
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 createUser(lbBests(undefined, pb(105)), { name: "One" });
await createUser(lbBests(undefined, pb(100)), { name: "Two" });
const me = await createUser(lbBests(undefined, pb(95)), { name: "Me" });
await createUser(lbBests(undefined, pb(90)), { name: "Three" });
await LeaderboardsDal.update("time", "60", "english");

//WHEN / THEN
Expand All @@ -405,19 +430,26 @@ describe("LeaderboardsDal", () => {
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];
await createConnection({
initiatorUid: me.uid,
receiverUid: friendOne.uid,
status: "accepted",
});

await createConnection({
initiatorUid: friendTwo.uid,
receiverUid: me.uid,
status: "accepted",
});

//WHEN / THEN

expect(await LeaderboardsDal.getCount("time", "60", "english", friends)) //
expect(await LeaderboardsDal.getCount("time", "60", "english", me.uid)) //
.toEqual(3);
expect(
await LeaderboardsDal.getRank("time", "60", "english", me.uid, friends)
await LeaderboardsDal.getRank("time", "60", "english", me.uid, true)
) //
.toEqual(
expect.objectContaining({
Expand Down
7 changes: 3 additions & 4 deletions backend/__tests__/__testData__/connections.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,8 @@ export async function createConnection(
},
maxPerUser
);
await ConnectionsDal.getCollection().updateOne(
{ _id: result._id },
{ $set: data }
);
await ConnectionsDal.__testing
.getCollection()
.updateOne({ _id: result._id }, { $set: data });
return { ...result, ...data };
}
16 changes: 4 additions & 12 deletions backend/__tests__/api/controllers/leaderboard.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,10 @@ describe("Loaderboard Controller", () => {
describe("get leaderboard", () => {
const getLeaderboardMock = vi.spyOn(LeaderboardDal, "get");
const getLeaderboardCountMock = vi.spyOn(LeaderboardDal, "getCount");
const getFriendsUidsMock = vi.spyOn(ConnectionsDal, "getFriendsUids");

beforeEach(() => {
getLeaderboardMock.mockClear();
getLeaderboardCountMock.mockClear();
getFriendsUidsMock.mockClear();
getLeaderboardCountMock.mockResolvedValue(42);
});

Expand Down Expand Up @@ -154,7 +152,6 @@ describe("Loaderboard Controller", () => {
//GIVEN
await enableConnectionsFeature(true);
getLeaderboardMock.mockResolvedValue([]);
getFriendsUidsMock.mockResolvedValue(["uidOne", "uidTwo"]);
getLeaderboardCountMock.mockResolvedValue(2);

//WHEN
Expand All @@ -180,13 +177,13 @@ describe("Loaderboard Controller", () => {
0,
50,
false,
["uidOne", "uidTwo"]
uid
);
expect(getLeaderboardCountMock).toHaveBeenCalledWith(
"time",
"60",
"english",
["uidOne", "uidTwo"]
uid
);
});

Expand Down Expand Up @@ -286,11 +283,9 @@ describe("Loaderboard Controller", () => {

describe("get rank", () => {
const getLeaderboardRankMock = vi.spyOn(LeaderboardDal, "getRank");
const getFriendsUidsMock = vi.spyOn(ConnectionsDal, "getFriendsUids");

afterEach(() => {
getLeaderboardRankMock.mockClear();
getFriendsUidsMock.mockClear();
});

it("fails withouth authentication", async () => {
Expand Down Expand Up @@ -335,14 +330,12 @@ describe("Loaderboard Controller", () => {
"60",
"english",
uid,
undefined
false
);
});
it("should get for english time 60 friends only", async () => {
//GIVEN
await enableConnectionsFeature(true);
const friends = ["friendOne", "friendTwo"];
getFriendsUidsMock.mockResolvedValue(friends);
getLeaderboardRankMock.mockResolvedValue({} as any);

//WHEN
Expand All @@ -363,9 +356,8 @@ describe("Loaderboard Controller", () => {
"60",
"english",
uid,
friends
true
);
expect(getFriendsUidsMock).toHaveBeenCalledWith(uid);
});
it("should get with ape key", async () => {
await acceptApeKeys(true);
Expand Down
Loading
Loading