Skip to content
Draft
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
120 changes: 102 additions & 18 deletions backend/__tests__/__integration__/dal/ape-keys.spec.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,107 @@
import { describe, it, expect } from "vitest";
import { describe, it, expect, vi, beforeEach } from "vitest";
import { ObjectId } from "mongodb";
import { addApeKey } from "../../../src/dal/ape-keys";
import {
addApeKey,
DBApeKey,
editApeKey,
getApeKey,
updateLastUsedOn,
} from "../../../src/dal/ape-keys";

describe("ApeKeysDal", () => {
it("should be able to add a new ape key", async () => {
const apeKey = {
_id: new ObjectId(),
uid: "123",
name: "test",
hash: "12345",
createdOn: Date.now(),
modifiedOn: Date.now(),
lastUsedOn: Date.now(),
useCount: 0,
enabled: true,
};

const apeKeyId = await addApeKey(apeKey);

expect(apeKeyId).toBe(apeKey._id.toHexString());
beforeEach(() => {
vi.useFakeTimers();
});

describe("addApeKey", () => {
it("should be able to add a new ape key", async () => {
const apeKey = buildApeKey();

const apeKeyId = await addApeKey(apeKey);

expect(apeKeyId).toBe(apeKey._id.toHexString());

const read = await getApeKey(apeKeyId);
expect(read).toEqual({
...apeKey,
});
});
});

describe("editApeKey", () => {
it("should edit name of an existing ape key", async () => {
//GIVEN
const apeKey = buildApeKey({ useCount: 5, enabled: true });
const apeKeyId = await addApeKey(apeKey);

//WHEN
const newName = "new name";
await editApeKey(apeKey.uid, apeKeyId, newName, undefined);

//THENa
const readAfterEdit = (await getApeKey(apeKeyId)) as DBApeKey;
expect(readAfterEdit).toEqual({
...apeKey,
name: newName,
modifiedOn: Date.now(),
});
});

it("should edit enabled of an existing ape key", async () => {
//GIVEN
const apeKey = buildApeKey({ useCount: 5, enabled: true });
const apeKeyId = await addApeKey(apeKey);

//WHEN

await editApeKey(apeKey.uid, apeKeyId, undefined, false);

//THEN
const readAfterEdit = (await getApeKey(apeKeyId)) as DBApeKey;
expect(readAfterEdit).toEqual({
...apeKey,
enabled: false,
modifiedOn: Date.now(),
});
});
});

describe("updateLastUsedOn", () => {
it("should update lastUsedOn and increment useCount when editing with lastUsedOn", async () => {
//GIVEN
const apeKey = buildApeKey({
useCount: 5,
lastUsedOn: 42,
});
const apeKeyId = await addApeKey(apeKey);

//WHEN
await updateLastUsedOn(apeKey.uid, apeKeyId);
await updateLastUsedOn(apeKey.uid, apeKeyId);

//THENa
const readAfterEdit = (await getApeKey(apeKeyId)) as DBApeKey;
expect(readAfterEdit).toEqual({
...apeKey,
modifiedOn: readAfterEdit.modifiedOn,
lastUsedOn: Date.now(),
useCount: 5 + 2,
});
});
});
});

function buildApeKey(overrides: Partial<DBApeKey> = {}): DBApeKey {
return {
_id: new ObjectId(),
uid: "123",
name: "test",
hash: "12345",
createdOn: Date.now(),
modifiedOn: Date.now(),
lastUsedOn: Date.now(),
useCount: 0,
enabled: true,
...overrides,
};
}
42 changes: 42 additions & 0 deletions backend/__tests__/__integration__/dal/config.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { ObjectId } from "mongodb";
import { describe, expect, it } from "vitest";
import * as ConfigDal from "../../../src/dal/config";

const getConfigCollection = ConfigDal.__testing.getConfigCollection;

describe("ConfigDal", () => {
describe("saveConfig", () => {
it("should save and update user configuration correctly", async () => {
//GIVEN
const uid = new ObjectId().toString();
await getConfigCollection().insertOne({
uid,
config: {
ads: "on",
time: 60,
quickTab: true, //legacy value
},
} as any);

//WHEN
await ConfigDal.saveConfig(uid, {
ads: "on",
difficulty: "normal",
} as any);

//WHEN
await ConfigDal.saveConfig(uid, { ads: "off" });

//THEN
const savedConfig = (await ConfigDal.getConfig(
uid
)) as ConfigDal.DBConfig;

expect(savedConfig.config.ads).toBe("off");
expect(savedConfig.config.time).toBe(60);

//should remove legacy values
expect((savedConfig.config as any)["quickTab"]).toBeUndefined();
});
});
});
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { describe, it, expect, afterEach, vi } from "vitest";
import _ from "lodash";
import { ObjectId } from "mongodb";
import * as UserDal from "../../../src/dal/user";
import * as LeaderboardsDal from "../../../src/dal/leaderboards";
Expand All @@ -11,6 +10,7 @@ import * as DB from "../../../src/init/db";
import { LbPersonalBests } from "../../../src/utils/pb";

import { pb } from "../../__testData__/users";
import { omit } from "../../../src/utils/misc";

describe("LeaderboardsDal", () => {
afterEach(async () => {
Expand Down Expand Up @@ -59,7 +59,7 @@ describe("LeaderboardsDal", () => {
)) as DBLeaderboardEntry[];

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

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

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

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

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

expect(lb).toEqual([
expectedLbEntry("15", { rank: 1, user: noBadge }),
Expand Down Expand Up @@ -239,7 +239,7 @@ describe("LeaderboardsDal", () => {
)) as DBLeaderboardEntry[];

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

expect(lb).toEqual([
expectedLbEntry("15", { rank: 1, user: noPremium }),
Expand Down
1 change: 0 additions & 1 deletion backend/__tests__/__integration__/dal/preset.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { describe, it, expect } from "vitest";
import { ObjectId } from "mongodb";
import * as PresetDal from "../../../src/dal/preset";
import _ from "lodash";

describe("PresetDal", () => {
describe("readPreset", () => {
Expand Down
2 changes: 1 addition & 1 deletion backend/__tests__/__integration__/dal/result.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ async function createDummyData(
tags: [],
consistency: 100,
keyConsistency: 100,
chartData: { wpm: [], raw: [], err: [] },
chartData: { wpm: [], burst: [], err: [] },
uid,
keySpacingStats: { average: 0, sd: 0 },
keyDurationStats: { average: 0, sd: 0 },
Expand Down
Loading