Files
sendou.ink/app/features/dev-tools/core/Scenarios.server.test.ts
2026-11-15 17:40:33 +02:00

187 lines
6.0 KiB
TypeScript

import { describe, expect, test } from "vitest";
import { db } from "~/db/sql";
import { broadcastProgramSearchParams } from "~/features/broadcast/broadcast-search-params";
import {
findProgram,
isBroadcastProducer,
} from "~/features/broadcast/broadcast-utils.server";
import * as LobbySet from "~/features/lobbies/core/LobbySet";
import * as LobbyMatchRepository from "~/features/lobbies/LobbyMatchRepository.server";
import { tournamentFromDB } from "~/features/tournament-bracket/core/Tournament.server";
import * as Scenarios from "./Scenarios.server";
const userExists = async (userId: number) =>
Boolean(
await db
.selectFrom("User")
.select("User.id")
.where("User.id", "=", userId)
.executeTakeFirst(),
);
const lobbyIdOf = (path: string) => Number(path.split("/").at(-1));
const paramVariants = Scenarios.list().flatMap((scenario) =>
scenario.params.flatMap((param) =>
param.kind === "select"
? param.options.map((option) => ({
scenarioId: scenario.id,
params: { [param.key]: option.value },
}))
: [],
),
);
describe("Scenarios.run", () => {
test.each(Scenarios.list().map((scenario) => scenario.id))(
"%s with default params lands an existing user on a page",
async (scenarioId) => {
const outcome = await Scenarios.run(scenarioId);
if (outcome.status !== "ok") throw new Error(outcome.status);
expect(outcome.result.path).toMatch(/^\//);
expect(outcome.result.path).not.toMatch(/undefined|null|NaN/);
expect(await userExists(outcome.result.userId)).toBe(true);
},
);
test.each(paramVariants)(
"$scenarioId runs with $params",
async ({ scenarioId, params }) => {
const outcome = await Scenarios.run(scenarioId, params);
expect(outcome.status).toBe("ok");
},
);
test.each(["single_elimination", "round_robin"])(
"tournament-player lands on a match with an opponent when the team count leaves BYEs (%s)",
async (format) => {
const outcome = await Scenarios.run("tournament-player", {
teams: 5,
format,
});
if (outcome.status !== "ok") throw new Error(outcome.status);
const matchId = Number(outcome.result.path.split("/").at(-1));
const match = await db
.selectFrom("TournamentMatch")
.select(["opponentOne", "opponentTwo"])
.where("id", "=", matchId)
.executeTakeFirstOrThrow();
expect(typeof match.opponentOne?.id).toBe("number");
expect(typeof match.opponentTwo?.id).toBe("number");
},
);
test.each(["organizer", "streamer"])(
"tournament-broadcast lands a producer on the dashboard of a channel their OBS URL shows (%s)",
async (role) => {
const outcome = await Scenarios.run("tournament-broadcast", { role });
if (outcome.status !== "ok") throw new Error(outcome.status);
const [, , tournamentId, , slug] = outcome.result.path.split("/");
const tournament = await tournamentFromDB(Number(tournamentId));
const { key: programKey } = broadcastProgramSearchParams.parse(
new URL(outcome.result.links?.[0]?.path ?? "", "http://localhost"),
);
const program = await findProgram(programKey);
expect(
isBroadcastProducer(tournament, { id: outcome.result.userId }),
).toBe(true);
expect(program?.userId).toBe(outcome.result.userId);
expect(program?.tournament?.ctx.id).toBe(tournament.ctx.id);
expect(program?.account).toBe(slug);
},
);
test.each([
{ state: "reporting", can: LobbySet.canReport },
{ state: "awaiting-confirmation", can: LobbySet.canConfirm },
{ state: "disputed", can: LobbySet.canUndo },
])(
"lobby-set lands a player on the side the set waits on ($state)",
async ({ state, can }) => {
const outcome = await Scenarios.run("lobby-set", { state });
if (outcome.status !== "ok") throw new Error(outcome.status);
const set = (
await LobbyMatchRepository.findAllSetsByLobbyId(
lobbyIdOf(outcome.result.path),
)
).at(-1)!;
expect(can(set, outcome.result.userId)).toBe(true);
},
);
test("lobby-set sitting out lands a room member without a side", async () => {
const outcome = await Scenarios.run("lobby-set", { you: "sitting-out" });
if (outcome.status !== "ok") throw new Error(outcome.status);
const set = (
await LobbyMatchRepository.findAllSetsByLobbyId(
lobbyIdOf(outcome.result.path),
)
).at(-1)!;
expect(LobbySet.sideOf(set, outcome.result.userId)).toBeNull();
});
test("lobby-open-spot asks the viewer whether they are ready", async () => {
const outcome = await Scenarios.run("lobby-open-spot", { position: 2 });
if (outcome.status !== "ok") throw new Error(outcome.status);
const readyCheckUser = await db
.selectFrom("LobbyReadyCheckUser")
.innerJoin(
"LobbyReadyCheck",
"LobbyReadyCheck.id",
"LobbyReadyCheckUser.readyCheckId",
)
.select("LobbyReadyCheckUser.confirmedAt")
.where("LobbyReadyCheck.lobbyId", "=", lobbyIdOf(outcome.result.path))
.where("LobbyReadyCheckUser.userId", "=", outcome.result.userId)
.executeTakeFirst();
expect(readyCheckUser).toEqual({ confirmedAt: null });
});
test("accepts numbers given as strings, like the CLI sends them", async () => {
const outcome = await Scenarios.run("tournament-registration", {
registeredTeams: "2",
});
expect(outcome.status).toBe("ok");
});
test.each([
{ why: "unknown param", params: { nope: 1 } },
{ why: "number out of range", params: { teams: 1000 } },
{ why: "not a whole number", params: { teams: "8.5" } },
{ why: "unknown option", params: { format: "ladder" } },
])("rejects $why", async ({ params }) => {
const outcome = await Scenarios.run("tournament-organizer", params);
expect(outcome.status).toBe("invalid-params");
});
test("returns unknown-scenario for an unknown id", async () => {
expect(await Scenarios.run("no-such-scenario")).toEqual({
status: "unknown-scenario",
});
});
test("records the run as the most recent one", async () => {
const outcome = await Scenarios.run("new-user");
if (outcome.status !== "ok") throw new Error(outcome.status);
expect(Scenarios.recentRuns()[0]).toMatchObject({
scenarioId: "new-user",
userId: outcome.result.userId,
});
});
});