generated from nhcarrigan/template
29c817230d
## Summary This PR represents the full v1 prototype, implementing the core game systems for Elysium. - Full idle/clicker RPG loop: resource collection, crafting, boss fights, exploration, and quests - Adventurer hiring with batch size selector and progressive tier cost scaling - Prestige, transcendence, and apotheosis systems with auto-prestige support - Character sheet, titles, leaderboards, companion system, and daily login bonuses - Auto-quest and auto-boss toggles - Discord webhook notifications on prestige/transcendence/apotheosis - Discord role awarded on apotheosis - Responsive design and overarching story/lore system - In-game sound effects and browser notifications for key events - Support link button in the resource bar - Full test coverage (100% on `apps/api` and `packages/types`) - CI pipeline: lint → build → test ## Closes Closes #1 Closes #2 Closes #3 Closes #4 Closes #5 Closes #6 Closes #7 Closes #8 Closes #9 Closes #10 Closes #11 Closes #12 Closes #13 Closes #14 Closes #16 Closes #19 Closes #20 Closes #21 Closes #22 Closes #23 Closes #24 Closes #25 Closes #26 Closes #27 Closes #29 ✨ This issue was created with help from Hikari~ 🌸 Co-authored-by: Naomi Carrigan <commits@nhcarrigan.com> Reviewed-on: #30 Co-authored-by: Hikari <hikari@nhcarrigan.com> Co-committed-by: Hikari <hikari@nhcarrigan.com>
118 lines
5.1 KiB
TypeScript
118 lines
5.1 KiB
TypeScript
/* eslint-disable max-lines-per-function -- Test suites naturally have many cases */
|
|
/* eslint-disable @typescript-eslint/consistent-type-assertions -- Tests build minimal state objects */
|
|
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
|
import { Hono } from "hono";
|
|
|
|
vi.mock("../../src/db/client.js", () => ({
|
|
prisma: {
|
|
player: {
|
|
findUnique: vi.fn(),
|
|
create: vi.fn(),
|
|
update: vi.fn(),
|
|
},
|
|
gameState: {
|
|
create: vi.fn(),
|
|
},
|
|
},
|
|
}));
|
|
|
|
vi.mock("../../src/services/discord.js", () => ({
|
|
buildOAuthUrl: vi.fn(),
|
|
exchangeCode: vi.fn(),
|
|
fetchDiscordUser: vi.fn(),
|
|
}));
|
|
|
|
vi.mock("../../src/services/jwt.js", () => ({
|
|
signToken: vi.fn().mockReturnValue("test_jwt"),
|
|
}));
|
|
|
|
describe("auth route", () => {
|
|
beforeEach(() => {
|
|
vi.clearAllMocks();
|
|
process.env["CORS_ORIGIN"] = "http://localhost:5173";
|
|
});
|
|
|
|
afterEach(() => {
|
|
delete process.env["CORS_ORIGIN"];
|
|
});
|
|
|
|
const makeApp = async () => {
|
|
const { authRouter } = await import("../../src/routes/auth.js");
|
|
const { buildOAuthUrl, exchangeCode, fetchDiscordUser } = await import("../../src/services/discord.js");
|
|
const { prisma } = await import("../../src/db/client.js");
|
|
const app = new Hono();
|
|
app.route("/auth", authRouter);
|
|
return { app, buildOAuthUrl: vi.mocked(buildOAuthUrl), exchangeCode: vi.mocked(exchangeCode), fetchDiscordUser: vi.mocked(fetchDiscordUser), prisma };
|
|
};
|
|
|
|
describe("GET /url", () => {
|
|
it("returns the OAuth URL when buildOAuthUrl succeeds", async () => {
|
|
const { app, buildOAuthUrl } = await makeApp();
|
|
buildOAuthUrl.mockReturnValueOnce("https://discord.com/oauth2/authorize?...");
|
|
const res = await app.fetch(new Request("http://localhost/auth/url"));
|
|
expect(res.status).toBe(200);
|
|
const body = await res.json() as { url: string };
|
|
expect(body.url).toContain("discord.com");
|
|
});
|
|
|
|
it("returns 500 when buildOAuthUrl throws", async () => {
|
|
const { app, buildOAuthUrl } = await makeApp();
|
|
buildOAuthUrl.mockImplementationOnce(() => { throw new Error("Missing env"); });
|
|
const res = await app.fetch(new Request("http://localhost/auth/url"));
|
|
expect(res.status).toBe(500);
|
|
});
|
|
});
|
|
|
|
describe("GET /callback", () => {
|
|
it("returns 400 when code parameter is missing", async () => {
|
|
const { app } = await makeApp();
|
|
const res = await app.fetch(new Request("http://localhost/auth/callback"));
|
|
expect(res.status).toBe(400);
|
|
});
|
|
|
|
it("redirects with isNew=true for a new user", async () => {
|
|
const { app, exchangeCode, fetchDiscordUser, prisma } = await makeApp();
|
|
exchangeCode.mockResolvedValueOnce({ access_token: "token" });
|
|
fetchDiscordUser.mockResolvedValueOnce({ id: "new_user", username: "Newbie", discriminator: "0", avatar: null });
|
|
vi.mocked(prisma.player.findUnique).mockResolvedValueOnce(null);
|
|
const createdPlayer = {
|
|
discordId: "new_user", username: "Newbie", discriminator: "0", avatar: null,
|
|
characterName: "Newbie", createdAt: 0, lastSavedAt: 0,
|
|
totalGoldEarned: 0, totalClicks: 0, lifetimeGoldEarned: 0, lifetimeClicks: 0,
|
|
lifetimeBossesDefeated: 0, lifetimeQuestsCompleted: 0,
|
|
lifetimeAdventurersRecruited: 0, lifetimeAchievementsUnlocked: 0,
|
|
};
|
|
vi.mocked(prisma.player.create).mockResolvedValueOnce(createdPlayer as never);
|
|
vi.mocked(prisma.gameState.create).mockResolvedValueOnce({} as never);
|
|
const res = await app.fetch(new Request("http://localhost/auth/callback?code=auth_code"));
|
|
expect(res.status).toBe(302);
|
|
const location = res.headers.get("Location") ?? "";
|
|
expect(location).toContain("isNew=true");
|
|
expect(location).toContain("token=test_jwt");
|
|
});
|
|
|
|
it("redirects with isNew=false for an existing user", async () => {
|
|
const { app, exchangeCode, fetchDiscordUser, prisma } = await makeApp();
|
|
exchangeCode.mockResolvedValueOnce({ access_token: "token" });
|
|
fetchDiscordUser.mockResolvedValueOnce({ id: "existing_user", username: "OldTimer", discriminator: "0", avatar: null });
|
|
const existingPlayer = { discordId: "existing_user", username: "OldTimer", discriminator: "0", avatar: null, characterName: "OldTimer" };
|
|
vi.mocked(prisma.player.findUnique).mockResolvedValueOnce(existingPlayer as never);
|
|
const updatedPlayer = { ...existingPlayer, discordId: "existing_user" };
|
|
vi.mocked(prisma.player.update).mockResolvedValueOnce(updatedPlayer as never);
|
|
const res = await app.fetch(new Request("http://localhost/auth/callback?code=auth_code"));
|
|
expect(res.status).toBe(302);
|
|
const location = res.headers.get("Location") ?? "";
|
|
expect(location).toContain("isNew=false");
|
|
});
|
|
|
|
it("redirects with error when callback throws", async () => {
|
|
const { app, exchangeCode } = await makeApp();
|
|
exchangeCode.mockRejectedValueOnce(new Error("OAuth failed"));
|
|
const res = await app.fetch(new Request("http://localhost/auth/callback?code=bad_code"));
|
|
expect(res.status).toBe(302);
|
|
const location = res.headers.get("Location") ?? "";
|
|
expect(location).toContain("error=auth_failed");
|
|
});
|
|
});
|
|
});
|