Skip to content

Commit

Permalink
refactor: move common util functions to a package (@Miodec) (#5894)
Browse files Browse the repository at this point in the history
Also moves esbuild to a package.
  • Loading branch information
Miodec authored Sep 20, 2024
1 parent 0b854af commit 1509a67
Show file tree
Hide file tree
Showing 65 changed files with 836 additions and 675 deletions.
282 changes: 0 additions & 282 deletions backend/__tests__/utils/misc.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,6 @@ describe("Misc Utils", () => {
vi.useRealTimers();
});

it("getCurrentDayTimestamp", () => {
vi.useFakeTimers();
vi.setSystemTime(1652743381);

const currentDay = misc.getCurrentDayTimestamp();
expect(currentDay).toBe(1641600000);
});

it("matchesAPattern", () => {
const testCases = {
"eng.*": {
Expand Down Expand Up @@ -280,280 +272,6 @@ describe("Misc Utils", () => {
expect(misc.getOrdinalNumberString(input)).toEqual(output);
});
});

it("getStartOfWeekTimestamp", () => {
const testCases = [
{
input: 1662400184017, // Mon Sep 05 2022 17:49:44 GMT+0000
expected: 1662336000000, // Mon Sep 05 2022 00:00:00 GMT+0000
},
{
input: 1559771456000, // Wed Jun 05 2019 21:50:56 GMT+0000
expected: 1559520000000, // Mon Jun 03 2019 00:00:00 GMT+0000
},
{
input: 1465163456000, // Sun Jun 05 2016 21:50:56 GMT+0000
expected: 1464566400000, // Mon May 30 2016 00:00:00 GMT+0000
},
{
input: 1491515456000, // Thu Apr 06 2017 21:50:56 GMT+0000
expected: 1491177600000, // Mon Apr 03 2017 00:00:00 GMT+0000
},
{
input: 1462507200000, // Fri May 06 2016 04:00:00 GMT+0000
expected: 1462147200000, // Mon May 02 2016 00:00:00 GMT+0000
},
{
input: 1231218000000, // Tue Jan 06 2009 05:00:00 GMT+0000,
expected: 1231113600000, // Mon Jan 05 2009 00:00:00 GMT+0000
},
{
input: 1709420681000, // Sat Mar 02 2024 23:04:41 GMT+0000
expected: 1708905600000, // Mon Feb 26 2024 00:00:00 GMT+0000
},
];

testCases.forEach(({ input, expected }) => {
expect(misc.getStartOfWeekTimestamp(input)).toEqual(expected);
});
});

it("getCurrentWeekTimestamp", () => {
Date.now = vi.fn(() => 825289481000); // Sun Feb 25 1996 23:04:41 GMT+0000

const currentWeek = misc.getCurrentWeekTimestamp();
expect(currentWeek).toBe(824688000000); // Mon Feb 19 1996 00:00:00 GMT+0000
});

it("getStartOfDayTimestamp", () => {
const testCases = [
{
input: new Date("2023/06/16 15:00 UTC").getTime(),
offset: 0,
expected: new Date("2023/06/16 00:00 UTC").getTime(), // Mon Sep 05 2022 00:00:00 GMT+0000
},
{
input: new Date("2023/06/16 15:00 UTC").getTime(),
offset: 1,
expected: new Date("2023/06/16 01:00 UTC").getTime(), // Mon Sep 05 2022 00:00:00 GMT+0000
},
{
input: new Date("2023/06/16 15:00 UTC").getTime(),
offset: -1,
expected: new Date("2023/06/15 23:00 UTC").getTime(), // Mon Sep 05 2022 00:00:00 GMT+0000
},
{
input: new Date("2023/06/16 15:00 UTC").getTime(),
offset: -4,
expected: new Date("2023/06/15 20:00 UTC").getTime(), // Mon Sep 05 2022 00:00:00 GMT+0000
},
{
input: new Date("2023/06/16 15:00 UTC").getTime(),
offset: 4,
expected: new Date("2023/06/16 04:00 UTC").getTime(), // Mon Sep 05 2022 00:00:00 GMT+0000
},
{
input: new Date("2023/06/17 03:00 UTC").getTime(),
offset: 4,
expected: new Date("2023/06/16 04:00 UTC").getTime(), // Mon Sep 05 2022 00:00:00 GMT+0000
},
{
input: new Date("2023/06/16 15:00 UTC").getTime(),
offset: 3,
expected: new Date("2023/06/16 03:00 UTC").getTime(), // Mon Sep 05 2022 00:00:00 GMT+0000
},
{
input: new Date("2023/06/17 01:00 UTC").getTime(),
offset: 3,
expected: new Date("2023/06/16 03:00 UTC").getTime(), // Mon Sep 05 2022 00:00:00 GMT+0000
},
];

testCases.forEach(({ input, offset, expected }) => {
expect(misc.getStartOfDayTimestamp(input, offset * 3600000)).toEqual(
expected
);
});
});

it("isToday", () => {
const testCases = [
{
now: new Date("2023/06/16 15:00 UTC").getTime(),
input: new Date("2023/06/16 15:00 UTC").getTime(),
offset: 0,
expected: true,
},
{
now: new Date("2023/06/16 15:00 UTC").getTime(),
input: new Date("2023/06/17 1:00 UTC").getTime(),
offset: 0,
expected: false,
},
{
now: new Date("2023/06/16 15:00 UTC").getTime(),
input: new Date("2023/06/16 01:00 UTC").getTime(),
offset: 1,
expected: true,
},
{
now: new Date("2023/06/16 15:00 UTC").getTime(),
input: new Date("2023/06/17 01:00 UTC").getTime(),
offset: 2,
expected: true,
},
{
now: new Date("2023/06/16 15:00 UTC").getTime(),
input: new Date("2023/06/16 01:00 UTC").getTime(),
offset: 2,
expected: false,
},
{
now: new Date("2023/06/17 01:00 UTC").getTime(),
input: new Date("2023/06/16 15:00 UTC").getTime(),
offset: 2,
expected: true,
},
{
now: new Date("2023/06/17 01:00 UTC").getTime(),
input: new Date("2023/06/17 02:00 UTC").getTime(),
offset: 2,
expected: false,
},
];

testCases.forEach(({ now, input, offset, expected }) => {
Date.now = vi.fn(() => now);
expect(misc.isToday(input, offset)).toEqual(expected);
});
});

it("isYesterday", () => {
const testCases = [
{
now: new Date("2023/06/15 15:00 UTC").getTime(),
input: new Date("2023/06/14 15:00 UTC").getTime(),
offset: 0,
expected: true,
},
{
now: new Date("2023/06/15 15:00 UTC").getTime(),
input: new Date("2023/06/15 15:00 UTC").getTime(),
offset: 0,
expected: false,
},
{
now: new Date("2023/06/15 15:00 UTC").getTime(),
input: new Date("2023/06/16 15:00 UTC").getTime(),
offset: 0,
expected: false,
},
{
now: new Date("2023/06/15 15:00 UTC").getTime(),
input: new Date("2023/06/13 15:00 UTC").getTime(),
offset: 0,
expected: false,
},
{
now: new Date("2023/06/16 02:00 UTC").getTime(),
input: new Date("2023/06/15 02:00 UTC").getTime(),
offset: 4,
expected: true,
},
{
now: new Date("2023/06/16 02:00 UTC").getTime(),
input: new Date("2023/06/16 01:00 UTC").getTime(),
offset: 4,
expected: false,
},
{
now: new Date("2023/06/16 02:00 UTC").getTime(),
input: new Date("2023/06/15 22:00 UTC").getTime(),
offset: 4,
expected: false,
},
{
now: new Date("2023/06/16 04:00 UTC").getTime(),
input: new Date("2023/06/16 03:00 UTC").getTime(),
offset: 4,
expected: true,
},
{
now: new Date("2023/06/16 14:00 UTC").getTime(),
input: new Date("2023/06/16 12:00 UTC").getTime(),
offset: -11,
expected: true,
},
];

testCases.forEach(({ now, input, offset, expected }) => {
Date.now = vi.fn(() => now);
expect(misc.isYesterday(input, offset)).toEqual(expected);
});
});

it("mapRange", () => {
const testCases = [
{
input: {
value: 123,
inMin: 0,
inMax: 200,
outMin: 0,
outMax: 1000,
clamp: false,
},
expected: 615,
},
{
input: {
value: 123,
inMin: 0,
inMax: 200,
outMin: 1000,
outMax: 0,
clamp: false,
},
expected: 385,
},
{
input: {
value: 10001,
inMin: 0,
inMax: 10000,
outMin: 0,
outMax: 1000,
clamp: false,
},
expected: 1000.1,
},
{
input: {
value: 10001,
inMin: 0,
inMax: 10000,
outMin: 0,
outMax: 1000,
clamp: true,
},
expected: 1000,
},
];

testCases.forEach(({ input, expected }) => {
expect(
misc.mapRange(
input.value,
input.inMin,
input.inMax,
input.outMin,
input.outMax,
input.clamp
)
).toEqual(expected);
});
});

it("formatSeconds", () => {
const testCases = [
{
Expand Down
1 change: 1 addition & 0 deletions backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
},
"dependencies": {
"@date-fns/utc": "1.2.0",
"@monkeytype/util": "workspace:*",
"@monkeytype/contracts": "workspace:*",
"@ts-rest/core": "3.51.0",
"@ts-rest/express": "3.51.0",
Expand Down
5 changes: 4 additions & 1 deletion backend/scripts/openapi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ function addAuth(

function getRequiredPermissions(
metadata: EndpointMetadata | undefined
): Permission[] | undefined {
): PermissionId[] | undefined {
if (metadata === undefined || metadata.requirePermission === undefined)
return undefined;

Expand All @@ -216,11 +216,13 @@ function addRateLimit(
metadata: EndpointMetadata | undefined
): void {
if (metadata === undefined || metadata.rateLimit === undefined) return;
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
const okResponse = operation.responses["200"];
if (okResponse === undefined) return;

operation.description += getRateLimitDescription(metadata.rateLimit);

// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
okResponse["headers"] = {
...okResponse["headers"],
"x-ratelimit-limit": {
Expand Down Expand Up @@ -275,6 +277,7 @@ function addRequiredConfiguration(
if (metadata === undefined || metadata.requireConfiguration === undefined)
return;

//@ts-expect-error
operation.description += `**Required configuration:** This operation can only be called if the [configuration](#tag/configuration/operation/configuration.get) for \`${metadata.requireConfiguration.path}\` is \`true\`.\n\n`;
}

Expand Down
2 changes: 1 addition & 1 deletion backend/src/api/controllers/dev.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import Logger from "../../utils/logger";
import * as DateUtils from "date-fns";
import { UTCDate } from "@date-fns/utc";
import * as ResultDal from "../../dal/result";
import { roundTo2 } from "../../utils/misc";
import { ObjectId } from "mongodb";
import * as LeaderboardDal from "../../dal/leaderboards";
import MonkeyError from "../../utils/error";
Expand All @@ -19,6 +18,7 @@ import {
GenerateDataRequest,
GenerateDataResponse,
} from "@monkeytype/contracts/dev";
import { roundTo2 } from "@monkeytype/util/numbers";

const CREATE_RESULT_DEFAULT_OPTIONS = {
firstTestTimestamp: DateUtils.startOfDay(new UTCDate(Date.now())).valueOf(),
Expand Down
10 changes: 5 additions & 5 deletions backend/src/api/controllers/leaderboard.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
import _ from "lodash";
import {
getCurrentDayTimestamp,
MILLISECONDS_IN_DAY,
getCurrentWeekTimestamp,
} from "../../utils/misc";
import { MonkeyResponse } from "../../utils/monkey-response";
import * as LeaderboardsDAL from "../../dal/leaderboards";
import MonkeyError from "../../utils/error";
Expand All @@ -22,6 +17,11 @@ import {
LanguageAndModeQuery,
} from "@monkeytype/contracts/leaderboards";
import { Configuration } from "@monkeytype/contracts/schemas/configuration";
import {
getCurrentDayTimestamp,
getCurrentWeekTimestamp,
MILLISECONDS_IN_DAY,
} from "@monkeytype/util/date-and-time";

export async function getLeaderboard(
req: MonkeyTypes.Request<GetLeaderboardQuery>
Expand Down
Loading

0 comments on commit 1509a67

Please sign in to comment.