siegebreak/services/test/routes.test.ts
caelum0x 6c7cea7592 feat: build out backend services, web client, and game roster
Backend (services/): make the app testable and fill real gaps.
- Extract buildApp() factory; index.ts owns listen()/matcher tick
- Real Apple/Google identity verification via jose JWKS (iss/aud/sub)
- Guest -> OAuth account linking (POST /auth/link), rebind in place
- POST /matches/:id/complete: record match + participants, update
  leaderboard, idempotent, guarded by server-to-server secret
- Extract pure matchmaking pairing (computePairings); fix ticket
  deckId loss on match; map ZodError -> 400; @fastify/cors
- vitest suite (40 tests), initial Prisma migration, prisma schema path

Web (web/): new Vite + React + TS client.
- Typed API client with one-shot 401 refresh/retry, localStorage auth
- Login (guest), deck builder (slot-0 hero rule), queue polling,
  leaderboard; 11 tests, clean build

Game (Assets/, Unity C#): champion-select layout now fits the roster
size; add Golem and Bandit champions.

Docs: root DEVELOPMENT.md tying game + services + web together.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-07-25 18:15:12 +03:00

96 lines
2.8 KiB
TypeScript

import { describe, it, expect, beforeAll, afterAll } from "vitest";
import type { FastifyInstance } from "fastify";
import { buildApp } from "../src/app";
let app: FastifyInstance;
beforeAll(async () => {
app = buildApp();
await app.ready();
});
afterAll(async () => {
await app.close();
});
describe("GET /health", () => {
it("reports ok", async () => {
const res = await app.inject({ method: "GET", url: "/health" });
expect(res.statusCode).toBe(200);
expect(res.json()).toEqual({ status: "ok" });
});
});
describe("CORS", () => {
it("reflects the request origin so browser clients can call the API", async () => {
const res = await app.inject({
method: "GET",
url: "/health",
headers: { origin: "http://localhost:5173" },
});
expect(res.statusCode).toBe(200);
expect(res.headers["access-control-allow-origin"]).toBe("http://localhost:5173");
});
it("answers CORS preflight for a protected route", async () => {
const res = await app.inject({
method: "OPTIONS",
url: "/players/me/decks",
headers: {
origin: "http://localhost:5173",
"access-control-request-method": "POST",
},
});
expect(res.statusCode).toBeLessThan(300);
expect(res.headers["access-control-allow-origin"]).toBe("http://localhost:5173");
});
});
describe("POST /analytics/event", () => {
it("accepts a well-formed event with 202", async () => {
const res = await app.inject({
method: "POST",
url: "/analytics/event",
payload: { name: "match_started", properties: { region: "eu" } },
});
expect(res.statusCode).toBe(202);
});
it("accepts an event with no properties", async () => {
const res = await app.inject({
method: "POST",
url: "/analytics/event",
payload: { name: "app_open" },
});
expect(res.statusCode).toBe(202);
});
it("rejects a body missing the required name with 400 (Zod error mapped, not 500)", async () => {
const res = await app.inject({
method: "POST",
url: "/analytics/event",
payload: { properties: { region: "eu" } },
});
expect(res.statusCode).toBe(400);
expect(res.json().error).toBe("validation_failed");
});
});
describe("auth on protected routes", () => {
it("rejects a request with no bearer token", async () => {
const res = await app.inject({ method: "GET", url: "/players/me/decks" });
expect(res.statusCode).toBe(401);
expect(res.json().error).toBe("missing_token");
});
it("rejects a malformed bearer token", async () => {
const res = await app.inject({
method: "GET",
url: "/players/me/decks",
headers: { authorization: "Bearer not-a-real-jwt" },
});
expect(res.statusCode).toBe(401);
expect(res.json().error).toBe("invalid_token");
});
});