generated from nhcarrigan/template
a36c8e72a5
## Summary
- Add comprehensive try/catch error handling across all API routes, middleware, and the Hono global error handler, piping every unhandled error to the `@nhcarrigan/logger` service to prevent silent crashes and unhandled Promise rejections
- Add a `logError` utility on the frontend that forwards errors through the overridden `console.error` to the backend telemetry endpoint; apply it to every silent `catch {}` block in the game context, sound, notification, and clipboard utilities, and wrap the React tree in an `ErrorBoundary`
- Add Plausible analytics, Open Graph + Twitter Card meta tags, Tree-Nation widget, and Google Ads to `index.html`
- Make the game sidebar sticky with a `--resource-bar-height` CSS custom property offset so it stays viewport-height without overlapping the resource bar; reset sticky behaviour in the mobile responsive override
## Test plan
- [ ] Lint passes: `pnpm lint`
- [ ] Build passes: `pnpm build`
- [ ] Verify errors thrown in API routes appear in the logger service rather than crashing the process
- [ ] Verify frontend errors appear in the `/api/fe/error` backend log
- [ ] Verify Open Graph tags render correctly when sharing the URL
- [ ] Verify Plausible analytics fires on page load
- [ ] Verify Tree-Nation badge renders in the sidebar
- [ ] Verify sidebar stays fixed while the main content scrolls on desktop
- [ ] Verify mobile layout is unaffected
✨ This issue was created with help from Hikari~ 🌸
Reviewed-on: #44
Co-authored-by: Hikari <hikari@nhcarrigan.com>
Co-committed-by: Hikari <hikari@nhcarrigan.com>
127 lines
5.6 KiB
TypeScript
127 lines
5.6 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");
|
|
});
|
|
|
|
it("redirects with error when callback throws a non-Error value", async () => {
|
|
const { app, exchangeCode } = await makeApp();
|
|
exchangeCode.mockRejectedValueOnce("raw string error");
|
|
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");
|
|
});
|
|
});
|
|
});
|