sendou.ink/app/utils/users.test.ts
2026-08-05 15:36:31 +03:00

100 lines
2.4 KiB
TypeScript

import { afterEach, beforeEach, describe, expect, test, vi } from "vitest";
import { queryToUserIdentifier, userDiscordIdIsAged } from "./users";
describe("queryToUserIdentifier()", () => {
test("returns null if no match", () => {
expect(queryToUserIdentifier("foo")).toBe(null);
});
test("gets custom url from url", () => {
expect(queryToUserIdentifier("https://sendou.ink/u/sendou")).toEqual({
customUrl: "sendou",
});
});
test("gets discord id from url", () => {
expect(
queryToUserIdentifier("https://sendou.ink/u/79237403620945920"),
).toEqual({
discordId: "79237403620945920",
});
});
test("gets custom url from url (without https://)", () => {
expect(queryToUserIdentifier("sendou.ink/u/sendou")).toEqual({
customUrl: "sendou",
});
});
test("gets discord id", () => {
expect(queryToUserIdentifier("79237403620945920")).toEqual({
discordId: "79237403620945920",
});
});
test("gets id", () => {
expect(queryToUserIdentifier("1")).toEqual({
id: 1,
});
});
test("gets id from url", () => {
expect(queryToUserIdentifier("https://sendou.ink/u/42")).toEqual({
id: 42,
});
});
test("gets custom url from url with trailing slash", () => {
expect(queryToUserIdentifier("https://sendou.ink/u/sendou/")).toEqual({
customUrl: "sendou",
});
});
test("gets custom url from profile sub-page url", () => {
expect(queryToUserIdentifier("https://sendou.ink/u/sendou/builds")).toEqual(
{
customUrl: "sendou",
},
);
});
test("gets discord id from url with query string", () => {
expect(
queryToUserIdentifier(
"https://sendou.ink/u/79237403620945920?utm_source=discord",
),
).toEqual({
discordId: "79237403620945920",
});
});
});
describe("userDiscordIdIsAged()", () => {
beforeEach(() => {
vi.useFakeTimers();
vi.setSystemTime(new Date("2023-11-25T00:00:00.000Z"));
});
afterEach(() => {
vi.useRealTimers();
});
test("returns false if discord id is not aged", () => {
expect(userDiscordIdIsAged({ discordId: "1177730652641181871" })).toBe(
false,
);
});
test("returns true if discord id is aged", () => {
expect(userDiscordIdIsAged({ discordId: "79237403620945920" })).toBe(true);
});
test("return false if discord id missing", () => {
expect(userDiscordIdIsAged({ discordId: "" })).toBe(false);
});
test("return false if discord id too short", () => {
expect(userDiscordIdIsAged({ discordId: "1234" })).toBe(false);
});
});