Skip to content

Commit 1009791

Browse files
authored
refactor: remove lodash from backend (@fehmer) (#6953)
1 parent 13b75f4 commit 1009791

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+915
-496
lines changed
Lines changed: 102 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,107 @@
1-
import { describe, it, expect } from "vitest";
1+
import { describe, it, expect, vi, beforeEach } from "vitest";
22
import { ObjectId } from "mongodb";
3-
import { addApeKey } from "../../../src/dal/ape-keys";
3+
import {
4+
addApeKey,
5+
DBApeKey,
6+
editApeKey,
7+
getApeKey,
8+
updateLastUsedOn,
9+
} from "../../../src/dal/ape-keys";
410

511
describe("ApeKeysDal", () => {
6-
it("should be able to add a new ape key", async () => {
7-
const apeKey = {
8-
_id: new ObjectId(),
9-
uid: "123",
10-
name: "test",
11-
hash: "12345",
12-
createdOn: Date.now(),
13-
modifiedOn: Date.now(),
14-
lastUsedOn: Date.now(),
15-
useCount: 0,
16-
enabled: true,
17-
};
18-
19-
const apeKeyId = await addApeKey(apeKey);
20-
21-
expect(apeKeyId).toBe(apeKey._id.toHexString());
12+
beforeEach(() => {
13+
vi.useFakeTimers();
14+
});
15+
16+
describe("addApeKey", () => {
17+
it("should be able to add a new ape key", async () => {
18+
const apeKey = buildApeKey();
19+
20+
const apeKeyId = await addApeKey(apeKey);
21+
22+
expect(apeKeyId).toBe(apeKey._id.toHexString());
23+
24+
const read = await getApeKey(apeKeyId);
25+
expect(read).toEqual({
26+
...apeKey,
27+
});
28+
});
29+
});
30+
31+
describe("editApeKey", () => {
32+
it("should edit name of an existing ape key", async () => {
33+
//GIVEN
34+
const apeKey = buildApeKey({ useCount: 5, enabled: true });
35+
const apeKeyId = await addApeKey(apeKey);
36+
37+
//WHEN
38+
const newName = "new name";
39+
await editApeKey(apeKey.uid, apeKeyId, newName, undefined);
40+
41+
//THENa
42+
const readAfterEdit = (await getApeKey(apeKeyId)) as DBApeKey;
43+
expect(readAfterEdit).toEqual({
44+
...apeKey,
45+
name: newName,
46+
modifiedOn: Date.now(),
47+
});
48+
});
49+
50+
it("should edit enabled of an existing ape key", async () => {
51+
//GIVEN
52+
const apeKey = buildApeKey({ useCount: 5, enabled: true });
53+
const apeKeyId = await addApeKey(apeKey);
54+
55+
//WHEN
56+
57+
await editApeKey(apeKey.uid, apeKeyId, undefined, false);
58+
59+
//THEN
60+
const readAfterEdit = (await getApeKey(apeKeyId)) as DBApeKey;
61+
expect(readAfterEdit).toEqual({
62+
...apeKey,
63+
enabled: false,
64+
modifiedOn: Date.now(),
65+
});
66+
});
67+
});
68+
69+
describe("updateLastUsedOn", () => {
70+
it("should update lastUsedOn and increment useCount when editing with lastUsedOn", async () => {
71+
//GIVEN
72+
const apeKey = buildApeKey({
73+
useCount: 5,
74+
lastUsedOn: 42,
75+
});
76+
const apeKeyId = await addApeKey(apeKey);
77+
78+
//WHEN
79+
await updateLastUsedOn(apeKey.uid, apeKeyId);
80+
await updateLastUsedOn(apeKey.uid, apeKeyId);
81+
82+
//THENa
83+
const readAfterEdit = (await getApeKey(apeKeyId)) as DBApeKey;
84+
expect(readAfterEdit).toEqual({
85+
...apeKey,
86+
modifiedOn: readAfterEdit.modifiedOn,
87+
lastUsedOn: Date.now(),
88+
useCount: 5 + 2,
89+
});
90+
});
2291
});
2392
});
93+
94+
function buildApeKey(overrides: Partial<DBApeKey> = {}): DBApeKey {
95+
return {
96+
_id: new ObjectId(),
97+
uid: "123",
98+
name: "test",
99+
hash: "12345",
100+
createdOn: Date.now(),
101+
modifiedOn: Date.now(),
102+
lastUsedOn: Date.now(),
103+
useCount: 0,
104+
enabled: true,
105+
...overrides,
106+
};
107+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import { ObjectId } from "mongodb";
2+
import { describe, expect, it } from "vitest";
3+
import * as ConfigDal from "../../../src/dal/config";
4+
5+
const getConfigCollection = ConfigDal.__testing.getConfigCollection;
6+
7+
describe("ConfigDal", () => {
8+
describe("saveConfig", () => {
9+
it("should save and update user configuration correctly", async () => {
10+
//GIVEN
11+
const uid = new ObjectId().toString();
12+
await getConfigCollection().insertOne({
13+
uid,
14+
config: {
15+
ads: "on",
16+
time: 60,
17+
quickTab: true, //legacy value
18+
},
19+
} as any);
20+
21+
//WHEN
22+
await ConfigDal.saveConfig(uid, {
23+
ads: "on",
24+
difficulty: "normal",
25+
} as any);
26+
27+
//WHEN
28+
await ConfigDal.saveConfig(uid, { ads: "off" });
29+
30+
//THEN
31+
const savedConfig = (await ConfigDal.getConfig(
32+
uid
33+
)) as ConfigDal.DBConfig;
34+
35+
expect(savedConfig.config.ads).toBe("off");
36+
expect(savedConfig.config.time).toBe(60);
37+
38+
//should remove legacy values
39+
expect((savedConfig.config as any)["quickTab"]).toBeUndefined();
40+
});
41+
});
42+
});

backend/__tests__/__integration__/dal/leaderboards.isolated.spec.ts

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
import { describe, it, expect, vi, afterEach } from "vitest";
2-
import _ from "lodash";
1+
import { describe, it, expect, afterEach, vi } from "vitest";
32
import { ObjectId } from "mongodb";
43
import * as UserDal from "../../../src/dal/user";
54
import * as LeaderboardsDal from "../../../src/dal/leaderboards";
@@ -12,6 +11,7 @@ import { LbPersonalBests } from "../../../src/utils/pb";
1211

1312
import { pb } from "../../__testData__/users";
1413
import { createConnection } from "../../__testData__/connections";
14+
import { omit } from "../../../src/utils/misc";
1515

1616
describe("LeaderboardsDal", () => {
1717
afterEach(async () => {
@@ -60,7 +60,8 @@ describe("LeaderboardsDal", () => {
6060
)) as DBLeaderboardEntry[];
6161

6262
//THEN
63-
const lb = results.map((it) => _.omit(it, ["_id"]));
63+
64+
const lb = results.map((it) => omit(it, ["_id"]));
6465

6566
expect(lb).toEqual([
6667
expectedLbEntry("15", { rank: 1, user: rank1 }),
@@ -87,7 +88,7 @@ describe("LeaderboardsDal", () => {
8788
)) as LeaderboardsDal.DBLeaderboardEntry[];
8889

8990
//THEN
90-
const lb = results.map((it) => _.omit(it, ["_id"]));
91+
const lb = results.map((it) => omit(it, ["_id"]));
9192

9293
expect(lb).toEqual([
9394
expectedLbEntry("60", { rank: 1, user: rank1 }),
@@ -201,7 +202,7 @@ describe("LeaderboardsDal", () => {
201202
)) as DBLeaderboardEntry[];
202203

203204
//THEN
204-
const lb = result.map((it) => _.omit(it, ["_id"]));
205+
const lb = result.map((it) => omit(it, ["_id"]));
205206

206207
expect(lb).toEqual([
207208
expectedLbEntry("15", { rank: 1, user: noBadge }),
@@ -240,7 +241,7 @@ describe("LeaderboardsDal", () => {
240241
)) as DBLeaderboardEntry[];
241242

242243
//THEN
243-
const lb = result.map((it) => _.omit(it, ["_id"]));
244+
const lb = result.map((it) => omit(it, ["_id"]));
244245

245246
expect(lb).toEqual([
246247
expectedLbEntry("15", { rank: 1, user: noPremium }),
@@ -298,7 +299,7 @@ describe("LeaderboardsDal", () => {
298299
)) as LeaderboardsDal.DBLeaderboardEntry[];
299300

300301
//THEN
301-
const lb = results.map((it) => _.omit(it, ["_id"]));
302+
const lb = results.map((it) => omit(it, ["_id"]));
302303

303304
expect(lb).toEqual([
304305
expectedLbEntry("60", { rank: 3, user: rank3 }),
@@ -337,7 +338,7 @@ describe("LeaderboardsDal", () => {
337338
)) as LeaderboardsDal.DBLeaderboardEntry[];
338339

339340
//THEN
340-
const lb = results.map((it) => _.omit(it, ["_id"]));
341+
const lb = results.map((it) => omit(it, ["_id"]));
341342

342343
expect(lb).toEqual([
343344
expectedLbEntry("60", { rank: 1, user: rank1, friendsRank: 1 }),
@@ -376,7 +377,7 @@ describe("LeaderboardsDal", () => {
376377
)) as LeaderboardsDal.DBLeaderboardEntry[];
377378

378379
//THEN
379-
const lb = results.map((it) => _.omit(it, ["_id"]));
380+
const lb = results.map((it) => omit(it, ["_id"]));
380381

381382
expect(lb).toEqual([
382383
expectedLbEntry("60", { rank: 4, user: rank4, friendsRank: 3 }),

backend/__tests__/__integration__/dal/preset.spec.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { describe, it, expect } from "vitest";
22
import { ObjectId } from "mongodb";
33
import * as PresetDal from "../../../src/dal/preset";
4-
import _ from "lodash";
54

65
describe("PresetDal", () => {
76
describe("readPreset", () => {

backend/__tests__/__integration__/dal/result.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ async function createDummyData(
5050
tags: [],
5151
consistency: 100,
5252
keyConsistency: 100,
53-
chartData: { wpm: [], raw: [], err: [] },
53+
chartData: { wpm: [], burst: [], err: [] },
5454
uid,
5555
keySpacingStats: { average: 0, sd: 0 },
5656
keyDurationStats: { average: 0, sd: 0 },

0 commit comments

Comments
 (0)