monkeytype/backend/setup-tests.ts
Bruce Berrios 0ef52ed9da
Add daily leaderboards (#3023) bruception
* Setup daily leaderboards backend (#2987) bruception

* Setup daily leaderboards backend

* Add enabled checks

* Consistent naming

* Add initial unit tests

* Use more flexible daily leaderboard rule logic

* Fix seed rule

* Add LRU Cache + Rank Calculation

* Use native functions

* Optional daily leaderboard rank

* Proper status code for invalid lb mode

* Add result criteria check

* Make daily leaderboard cache size configurable

* Add Ape endpoint for daily leaderboard (#2997)

* support for switching to viewing daily lbs

* test

* buttons

* only checking daily if user has more than 2 hours typed

* updated structure

* setting rank if its undefined

* only when daily

* storing uid

* fixed media queries

* Daily leaderboards pagination (#3006)

* Pagination

* Remove with scores

* Add daily leaderboard rank (#3014) Bruception

* Add daily leaderboard rank

* Remove unused import

* Use object instead

* Add client logic

* Add limit checks

* Announce top daily leaderboard results (#3017)

* Add rank in daily leaderboard results (#3022)

* not showing lb memory and top % on daily

* Fix rank pagination

* Actual fix

* showing new rank

Co-authored-by: Miodec <bartnikjack@gmail.com>
2022-05-26 16:30:11 +02:00

85 lines
1.8 KiB
TypeScript

import { Collection, Db, MongoClient, WithId } from "mongodb";
process.env.MODE = "dev";
jest.mock("./src/init/db", () => ({
__esModule: true,
getDb: (): Db => db,
collection: <T>(name: string): Collection<WithId<T>> =>
db.collection<WithId<T>>(name),
}));
jest.mock("./src/utils/logger", () => ({
__esModule: true,
default: {
error: console.error,
warning: console.warn,
info: console.info,
success: console.info,
logToDb: console.info,
},
}));
jest.mock("swagger-stats", () => ({
getMiddleware:
() =>
(_: unknown, __: unknown, next: () => unknown): void => {
next();
},
}));
if (!process.env.REDIS_URI) {
// use mock if not set
process.env.REDIS_URI = "redis://mock";
jest.mock("ioredis", () => require("ioredis-mock"));
}
// TODO: better approach for this when needed
// https://firebase.google.com/docs/rules/unit-tests#run_local_unit_tests_with_the_version_9_javascript_sdk
jest.mock("firebase-admin", () => ({
__esModule: true,
default: {
auth: (): unknown => ({
verifyIdToken: (
_token: string,
_checkRevoked: boolean
): unknown /* Promise<DecodedIdToken> */ =>
Promise.resolve({
aud: "mockFirebaseProjectId",
auth_time: 123,
exp: 1000,
uid: "mockUid",
}),
}),
},
}));
const collectionsForCleanUp = ["users"];
let db: Db;
let connection: MongoClient;
beforeAll(async () => {
connection = await MongoClient.connect(global.__MONGO_URI__);
db = connection.db();
});
beforeEach(async () => {
if (global.__MONGO_URI__) {
await Promise.all(
collectionsForCleanUp.map((collection) =>
db.collection(collection).deleteMany({})
)
);
}
});
const realDateNow = Date.now;
afterEach(() => {
Date.now = realDateNow;
});
afterAll(async () => {
await connection.close();
});