Skip to content

Commit

Permalink
refactor: remove global type namespaces (@Miodec) (#5907)
Browse files Browse the repository at this point in the history
Remove global types, move types to where they originate from, import
them when needed.
  • Loading branch information
Miodec authored Sep 23, 2024
1 parent d9788a1 commit b6bd5ba
Show file tree
Hide file tree
Showing 198 changed files with 1,608 additions and 1,642 deletions.
6 changes: 4 additions & 2 deletions backend/__migration__/testActivity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@ import * as DB from "../src/init/db";
import { Collection, Db } from "mongodb";

import readlineSync from "readline-sync";
import { DBUser } from "../src/dal/user";
import { DBResult } from "../src/utils/result";

const batchSize = 50;

let appRunning = true;
let db: Db | undefined;
let userCollection: Collection<MonkeyTypes.DBUser>;
let resultCollection: Collection<MonkeyTypes.DBResult>;
let userCollection: Collection<DBUser>;
let resultCollection: Collection<DBResult>;

const filter = { testActivity: { $exists: false } };

Expand Down
3 changes: 2 additions & 1 deletion backend/__tests__/__migration__/testActivity.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import * as Migration from "../../__migration__/testActivity";
import * as UserTestData from "../__testData__/users";
import * as UserDal from "../../src/dal/user";
import * as ResultDal from "../../src/dal/result";
import { DBResult } from "../../src/utils/result";

describe("testActivity migration", () => {
it("migrates users without results", async () => {
Expand Down Expand Up @@ -69,5 +70,5 @@ async function createResult(uid: string, timestamp: number): Promise<void> {
keyConsistency: 0,
chartData: "toolong",
name: "",
} as unknown as ResultDal.DBResult);
} as unknown as DBResult);
}
2 changes: 1 addition & 1 deletion backend/__tests__/__testData__/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export async function mockAuthenticateWithApeKey(
const apiKey = randomBytes(apeKeyBytes).toString("base64url");
const saltyHash = await hash(apiKey, apeKeySaltRounds);

const apeKey: MonkeyTypes.ApeKeyDB = {
const apeKey: ApeKeyDal.DBApeKey = {
_id: new ObjectId(),
name: "bob",
enabled: true,
Expand Down
8 changes: 4 additions & 4 deletions backend/__tests__/__testData__/users.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@ import * as UserDAL from "../../src/dal/user";
import { ObjectId } from "mongodb";

export async function createUser(
user?: Partial<MonkeyTypes.DBUser>
): Promise<MonkeyTypes.DBUser> {
user?: Partial<UserDAL.DBUser>
): Promise<UserDAL.DBUser> {
const uid = new ObjectId().toHexString();
await UserDAL.addUser("user" + uid, uid + "@example.com", uid);
await DB.collection("users").updateOne({ uid }, { $set: { ...user } });
return await UserDAL.getUser(uid, "test");
}

export async function createUserWithoutMigration(
user?: Partial<MonkeyTypes.DBUser>
): Promise<MonkeyTypes.DBUser> {
user?: Partial<UserDAL.DBUser>
): Promise<UserDAL.DBUser> {
const uid = new ObjectId().toHexString();
await UserDAL.addUser("user" + uid, uid + "@example.com", uid);
await DB.collection("users").updateOne({ uid }, { $set: { ...user } });
Expand Down
8 changes: 4 additions & 4 deletions backend/__tests__/api/controllers/admin.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -207,11 +207,11 @@ describe("AdminController", () => {
const reportOne = {
id: "1",
reason: "one",
} as any as MonkeyTypes.Report;
} as any as ReportDal.DBReport;
const reportTwo = {
id: "2",
reason: "two",
} as any as MonkeyTypes.Report;
} as any as ReportDal.DBReport;
getReportsMock.mockResolvedValue([reportOne, reportTwo]);

//WHEN
Expand Down Expand Up @@ -321,11 +321,11 @@ describe("AdminController", () => {
const reportOne = {
id: "1",
reason: "one",
} as any as MonkeyTypes.Report;
} as any as ReportDal.DBReport;
const reportTwo = {
id: "2",
reason: "two",
} as any as MonkeyTypes.Report;
} as any as ReportDal.DBReport;
getReportsMock.mockResolvedValue([reportOne, reportTwo]);

//WHEN
Expand Down
11 changes: 4 additions & 7 deletions backend/__tests__/api/controllers/ape-key.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -337,8 +337,8 @@ describe("ApeKeyController", () => {

function apeKeyDb(
uid: string,
data?: Partial<MonkeyTypes.ApeKeyDB>
): MonkeyTypes.ApeKeyDB {
data?: Partial<ApeKeyDal.DBApeKey>
): ApeKeyDal.DBApeKey {
return {
_id: new ObjectId(),
uid,
Expand All @@ -363,12 +363,9 @@ async function enableApeKeysEndpoints(enabled: boolean): Promise<void> {
);
}

function user(
uid: string,
data: Partial<MonkeyTypes.DBUser>
): MonkeyTypes.DBUser {
function user(uid: string, data: Partial<UserDal.DBUser>): UserDal.DBUser {
return {
uid,
...data,
} as MonkeyTypes.DBUser;
} as UserDal.DBUser;
}
6 changes: 2 additions & 4 deletions backend/__tests__/api/controllers/result.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { DecodedIdToken } from "firebase-admin/lib/auth/token-verifier";
import { ObjectId } from "mongodb";
import { mockAuthenticateWithApeKey } from "../../__testData__/auth";
import { enableRateLimitExpects } from "../../__testData__/rate-limit";
import { DBResult } from "../../../src/utils/result";
const uid = "123456";

const mockDecodedToken: DecodedIdToken = {
Expand Down Expand Up @@ -830,10 +831,7 @@ async function enablePremiumFeatures(premium: boolean): Promise<void> {
mockConfig
);
}
function givenDbResult(
uid: string,
customize?: Partial<MonkeyTypes.DBResult>
): MonkeyTypes.DBResult {
function givenDbResult(uid: string, customize?: Partial<DBResult>): DBResult {
return {
_id: new ObjectId(),
wpm: Math.random() * 100,
Expand Down
28 changes: 14 additions & 14 deletions backend/__tests__/api/controllers/user.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -484,7 +484,7 @@ describe("user controller test", () => {
//given
getUserMock.mockResolvedValue({
testActivity: { "2023": [1, 2, 3], "2024": [4, 5, 6] },
} as Partial<MonkeyTypes.DBUser> as MonkeyTypes.DBUser);
} as Partial<UserDal.DBUser> as UserDal.DBUser);

//when
await mockApp
Expand All @@ -497,7 +497,7 @@ describe("user controller test", () => {
//given
getUserMock.mockResolvedValue({
testActivity: { "2023": [1, 2, 3], "2024": [4, 5, 6] },
} as Partial<MonkeyTypes.DBUser> as MonkeyTypes.DBUser);
} as Partial<UserDal.DBUser> as UserDal.DBUser);
vi.spyOn(UserDal, "checkIfUserIsPremium").mockResolvedValue(true);
await enablePremiumFeatures(true);

Expand Down Expand Up @@ -633,7 +633,7 @@ describe("user controller test", () => {
email: "email",
discordId: "discordId",
banned: true,
} as Partial<MonkeyTypes.DBUser> as MonkeyTypes.DBUser;
} as Partial<UserDal.DBUser> as UserDal.DBUser;
await getUserMock.mockResolvedValue(user);

//WHEN
Expand Down Expand Up @@ -664,7 +664,7 @@ describe("user controller test", () => {
name: "name",
email: "email",
discordId: "discordId",
} as Partial<MonkeyTypes.DBUser> as MonkeyTypes.DBUser;
} as Partial<UserDal.DBUser> as UserDal.DBUser;
getUserMock.mockResolvedValue(user);

//WHEN
Expand Down Expand Up @@ -1555,7 +1555,7 @@ describe("user controller test", () => {
uid,
name: "name",
email: "email",
} as Partial<MonkeyTypes.DBUser> as MonkeyTypes.DBUser;
} as Partial<UserDal.DBUser> as UserDal.DBUser;
getUserMock.mockResolvedValue(user);
blocklistContainsMock.mockResolvedValue(true);

Expand Down Expand Up @@ -2061,12 +2061,12 @@ describe("user controller test", () => {

it("should get tags", async () => {
//GIVEN
const tagOne: MonkeyTypes.DBUserTag = {
const tagOne: UserDal.DBUserTag = {
_id: new ObjectId(),
name: "tagOne",
personalBests: {} as any,
};
const tagTwo: MonkeyTypes.DBUserTag = {
const tagTwo: UserDal.DBUserTag = {
_id: new ObjectId(),
name: "tagOne",
personalBests: {} as any,
Expand Down Expand Up @@ -2171,12 +2171,12 @@ describe("user controller test", () => {
});
it("should get custom themes", async () => {
//GIVEN
const themeOne: MonkeyTypes.DBCustomTheme = {
const themeOne: UserDal.DBCustomTheme = {
_id: new ObjectId(),
name: "themeOne",
colors: new Array(10).fill("#000000") as any,
};
const themeTwo: MonkeyTypes.DBCustomTheme = {
const themeTwo: UserDal.DBCustomTheme = {
_id: new ObjectId(),
name: "themeTwo",
colors: new Array(10).fill("#FFFFFF") as any,
Expand Down Expand Up @@ -2207,7 +2207,7 @@ describe("user controller test", () => {

it("should add ", async () => {
//GIVEN
const addedTheme: MonkeyTypes.DBCustomTheme = {
const addedTheme: UserDal.DBCustomTheme = {
_id: new ObjectId(),
name: "custom",
colors: new Array(10).fill("#000000") as any,
Expand Down Expand Up @@ -2501,7 +2501,7 @@ describe("user controller test", () => {
it("should get stats", async () => {
//GIVEN
const stats: Pick<
MonkeyTypes.DBUser,
UserDal.DBUser,
"startedTests" | "completedTests" | "timeTyping"
> = {
startedTests: 5,
Expand Down Expand Up @@ -2672,7 +2672,7 @@ describe("user controller test", () => {
const checkIfUserIsPremiumMock = vi.spyOn(UserDal, "checkIfUserIsPremium");
const leaderboardGetRankMock = vi.spyOn(LeaderboardDal, "getRank");

const foundUser: Partial<MonkeyTypes.DBUser> = {
const foundUser: Partial<UserDal.DBUser> = {
_id: new ObjectId(),
uid: new ObjectId().toHexString(),
name: "bob",
Expand Down Expand Up @@ -3548,7 +3548,7 @@ describe("user controller test", () => {
testActivity: {
"2024": fillYearWithDay(94),
},
} as Partial<MonkeyTypes.DBUser> as MonkeyTypes.DBUser;
} as Partial<UserDal.DBUser> as UserDal.DBUser;
getUserMock.mockResolvedValue(user);

//WHEN
Expand Down Expand Up @@ -3584,7 +3584,7 @@ describe("user controller test", () => {
maxLength: 1024,
hourOffset: 2,
},
} as Partial<MonkeyTypes.DBUser> as MonkeyTypes.DBUser;
} as Partial<UserDal.DBUser> as UserDal.DBUser;
getUserMock.mockResolvedValue(user);

//WHEN
Expand Down
18 changes: 8 additions & 10 deletions backend/__tests__/dal/leaderboards.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import type { PersonalBest } from "@monkeytype/contracts/schemas/shared";
const configuration = Configuration.getCachedConfiguration();

import * as DB from "../../src/init/db";
import { LbPersonalBests } from "../../src/utils/pb";

describe("LeaderboardsDal", () => {
describe("update", () => {
Expand Down Expand Up @@ -289,14 +290,14 @@ function expectedLbEntry(
}

async function createUser(
lbPersonalBests?: MonkeyTypes.LbPersonalBests,
userProperties?: Partial<MonkeyTypes.DBUser>
): Promise<MonkeyTypes.DBUser> {
lbPersonalBests?: LbPersonalBests,
userProperties?: Partial<UserDal.DBUser>
): Promise<UserDal.DBUser> {
const uid = new ObjectId().toHexString();
await UserDal.addUser("User " + uid, uid + "@example.com", uid);

await DB.getDb()
?.collection<MonkeyTypes.DBUser>("users")
?.collection<UserDal.DBUser>("users")
.updateOne(
{ uid },
{
Expand All @@ -313,11 +314,8 @@ async function createUser(
return await UserDal.getUser(uid, "test");
}

function lbBests(
pb15?: PersonalBest,
pb60?: PersonalBest
): MonkeyTypes.LbPersonalBests {
const result: MonkeyTypes.LbPersonalBests = { time: {} };
function lbBests(pb15?: PersonalBest, pb60?: PersonalBest): LbPersonalBests {
const result: LbPersonalBests = { time: {} };
if (pb15) result.time["15"] = { english: pb15 };
if (pb60) result.time["60"] = { english: pb60 };
return result;
Expand Down Expand Up @@ -355,7 +353,7 @@ function premium(expirationDeltaSeconds: number) {

interface ExpectedLbEntry {
rank: number;
user: MonkeyTypes.DBUser;
user: UserDal.DBUser;
badgeId?: number;
isPremium?: boolean;
}
Expand Down
5 changes: 3 additions & 2 deletions backend/__tests__/dal/result.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as ResultDal from "../../src/dal/result";
import { ObjectId } from "mongodb";
import * as UserDal from "../../src/dal/user";
import { DBResult } from "../../src/utils/result";

let uid: string = "";
const timestamp = Date.now() - 60000;
Expand All @@ -11,7 +12,7 @@ async function createDummyData(
timestamp: number,
tag?: string
): Promise<void> {
const dummyUser: MonkeyTypes.DBUser = {
const dummyUser: UserDal.DBUser = {
_id: new ObjectId(),
uid,
addedAt: 0,
Expand Down Expand Up @@ -56,7 +57,7 @@ async function createDummyData(
language: "english",
isPb: false,
name: "Test",
} as MonkeyTypes.DBResult);
} as DBResult);
}
}
describe("ResultDal", () => {
Expand Down
8 changes: 4 additions & 4 deletions backend/__tests__/dal/user.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -493,7 +493,7 @@ describe("UserDal", () => {

it("should fail if tag not found", async () => {
// given
const tagOne: MonkeyTypes.DBUserTag = {
const tagOne: UserDAL.DBUserTag = {
_id: new ObjectId(),
name: "one",
personalBests: {} as any,
Expand All @@ -510,7 +510,7 @@ describe("UserDal", () => {

it("editTag success", async () => {
// given
const tagOne: MonkeyTypes.DBUserTag = {
const tagOne: UserDAL.DBUserTag = {
_id: new ObjectId(),
name: "one",
personalBests: {} as any,
Expand Down Expand Up @@ -540,7 +540,7 @@ describe("UserDal", () => {

it("should return error if tag is unknown", async () => {
// given
const tagOne: MonkeyTypes.DBUserTag = {
const tagOne: UserDAL.DBUserTag = {
_id: new ObjectId(),
name: "one",
personalBests: {} as any,
Expand Down Expand Up @@ -594,7 +594,7 @@ describe("UserDal", () => {

it("should return error if tag is unknown", async () => {
// given
const tagOne: MonkeyTypes.DBUserTag = {
const tagOne: UserDAL.DBUserTag = {
_id: new ObjectId(),
name: "one",
personalBests: {} as any,
Expand Down
5 changes: 3 additions & 2 deletions backend/__tests__/middlewares/auth.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
RequestAuthenticationOptions,
} from "@monkeytype/contracts/schemas/api";
import * as Prometheus from "../../src/utils/prometheus";
import { TsRestRequestWithContext } from "../../src/api/types";

const mockDecodedToken: DecodedIdToken = {
uid: "123456789",
Expand All @@ -37,7 +38,7 @@ const mockApeKey = {
vi.spyOn(ApeKeys, "getApeKey").mockResolvedValue(mockApeKey);
vi.spyOn(ApeKeys, "updateLastUsedOn").mockResolvedValue();
const isDevModeMock = vi.spyOn(Misc, "isDevEnvironment");
let mockRequest: Partial<Auth.TsRestRequestWithCtx>;
let mockRequest: Partial<TsRestRequestWithContext>;
let mockResponse: Partial<Response>;
let nextFunction: NextFunction;

Expand Down Expand Up @@ -553,7 +554,7 @@ describe("middlewares/auth", () => {
async function authenticate(
request: Partial<Request>,
authenticationOptions?: RequestAuthenticationOptions
): Promise<{ decodedToken: MonkeyTypes.DecodedToken }> {
): Promise<{ decodedToken: Auth.DecodedToken }> {
const mergedRequest = {
...mockRequest,
...request,
Expand Down
Loading

0 comments on commit b6bd5ba

Please sign in to comment.