sendou.ink/app/utils/Test.ts
Kalle 7dec8c572e
SendouQ Season 2 changes (#1542)
* Initial

* Saves preferences

* Include TW

* mapModePreferencesToModeList

* mapPoolFromPreferences initial

* Preference to map pool

* Adjust seed

* q.looking tests

* adds about created map preferences to memento in the correct spot (two preferrers)

* Failing test about modes

* Mode preferences to memento

* Remove old Plus Voting code

* Fix seeding

* find match by id via kysely

* View map memento

* Fix up map list generation logic

* Mode memento info

* Future match modes

* Add TODO

* Migration number

* Migrate test DB

* Remove old map pool code

* createGroupFromPrevious new

* Settings styling

* VC to settings

* Weapon pool

* Add TODOs

* Progress

* Adjust mode exclusion policy

* Progress

* Progress

* Progress

* Notes in progress

* Note feedback after submit

* Textarea styling

* Unskip tests

* Note sorting failing test

* Private note in Q

* Ownerpicksmaps later

* New bottom section

* Mobile layout initial

* Add basic match meta

* Tabs initial

* Sticky tab

* Unseen messages in match page

* Front page i18n

* Settings i18n

* Looking 18n

* Chat i18n

* Progress

* Tranfer weapon pools script

* Sticky on match page

* Match page translations

* i18n - tiers page

* Preparing page i18n

* Icon

* Show add note right after report
2023-11-30 20:57:06 +02:00

122 lines
3.0 KiB
TypeScript

import type { ActionArgs, LoaderArgs } from "@remix-run/node";
import type { z } from "zod";
import { ADMIN_ID } from "~/constants";
import { NZAP_TEST_ID } from "~/db/seed/constants";
import { db, sql } from "~/db/sql";
import { SESSION_KEY } from "~/features/auth/core/authenticator.server";
import { authSessionStorage } from "~/features/auth/core/session.server";
export function arrayContainsSameItems<T>(arr1: T[], arr2: T[]) {
return (
arr1.length === arr2.length && arr1.every((item) => arr2.includes(item))
);
}
export function wrappedAction<T extends z.ZodTypeAny>({
action,
params = {},
}: {
// TODO: strongly type this
action: (args: ActionArgs) => any;
params?: ActionArgs["params"];
}) {
return async (
args: z.infer<T>,
{ user }: { user?: "admin" | "regular" } = {},
) => {
const body = new URLSearchParams(args);
const request = new Request("http://app.com/path", {
method: "POST",
body,
headers: await authHeader(user),
});
try {
const response = await action({
request,
context: {},
params,
});
return response;
} catch (thrown) {
if (thrown instanceof Response) {
// it was a redirect
if (thrown.status === 302) return thrown;
throw new Error(`Response thrown with status code: ${thrown.status}`);
}
throw thrown;
}
};
}
export function wrappedLoader<T>({
loader,
}: {
// TODO: strongly type this
loader: (args: LoaderArgs) => any;
}) {
return async ({ user }: { user?: "admin" | "regular" } = {}) => {
const request = new Request("http://app.com/path", {
method: "GET",
headers: await authHeader(user),
});
try {
const data = await loader({
request,
params: {},
context: {},
});
return data as T;
} catch (thrown) {
if (thrown instanceof Response) {
throw new Error(`Response thrown with status code: ${thrown.status}`);
}
throw thrown;
}
};
}
async function authHeader(user?: "admin" | "regular"): Promise<HeadersInit> {
if (!user) return [];
const session = await authSessionStorage.getSession();
session.set(SESSION_KEY, user === "admin" ? ADMIN_ID : NZAP_TEST_ID);
return [["Cookie", await authSessionStorage.commitSession(session)]];
}
export const database = {
reset: () => {
const tables = sql
.prepare(
"SELECT name FROM sqlite_master WHERE type='table' AND name NOT LIKE 'sqlite_%' AND name NOT LIKE 'migrations';",
)
.all() as { name: string }[];
sql.prepare("PRAGMA foreign_keys = OFF").run();
for (const table of tables) {
sql.prepare(`DELETE FROM "${table.name}"`).run();
}
sql.prepare("PRAGMA foreign_keys = ON").run();
},
insertUsers: (count: number) =>
db
.insertInto("User")
.values(
Array.from({ length: count }).map((_, i) => ({
id: i + 1,
discordName: `user${i + 1}`,
discordDiscriminator: "0",
discordId: String(i),
})),
)
.execute(),
};