siegebreak/services/test/matchmakingSubscribe.test.ts
caelum0x e8e48c28bc feat: real-time WebSocket matchmaking push (replaces poll-only)
Backend: on a match the matcher publishes a match-found event to a
Redis channel; modules/matchmaking/notifier.ts runs a per-instance
subscriber that forwards to locally-connected sockets, so fan-out is
correct across multiple API instances. New WS /matchmaking/subscribe
route (@fastify/websocket) authenticates via ?token= (WS handshakes
can't carry Authorization), registers the socket, and replays the
current ticket on connect to close the queue<->connect race.

Web: openMatchSocket() opens the subscribe socket as an accelerator and
keeps the ~1s poll as the reliable backstop (and the only path where
WebSocket is unavailable, e.g. jsdom). Pure matchSocketUrl builder tested.

10 new backend tests (notifier registry/dispatch/publish, token helper,
WS route registration) + matcher publish assertions; 3 new web tests.
108 backend / 21 web tests green.
2026-07-27 17:46:41 +03:00

40 lines
1.4 KiB
TypeScript

import { describe, it, expect, beforeAll, afterAll } from "vitest";
import type { FastifyInstance } from "fastify";
import { buildApp } from "../src/app";
import { tokenFromQuery } from "../src/modules/matchmaking";
describe("tokenFromQuery", () => {
it("extracts a non-empty token from the query object", () => {
expect(tokenFromQuery({ token: "abc" })).toBe("abc");
});
it("returns null when the token is missing, empty, or wrong-typed", () => {
expect(tokenFromQuery({})).toBeNull();
expect(tokenFromQuery({ token: "" })).toBeNull();
expect(tokenFromQuery({ token: 123 })).toBeNull();
expect(tokenFromQuery(null)).toBeNull();
expect(tokenFromQuery(undefined)).toBeNull();
});
});
describe("WebSocket route registration", () => {
let app: FastifyInstance;
beforeAll(async () => {
app = buildApp();
await app.ready();
});
afterAll(async () => {
await app.close();
});
// The @fastify/websocket plugin is registered and the subscribe route builds
// without error. A plain HTTP GET (no Upgrade header) to a websocket route is
// rejected by the plugin, which is the correct guard for non-WS callers.
it("builds the app with the subscribe route and rejects a non-upgrade GET", async () => {
const res = await app.inject({ method: "GET", url: "/matchmaking/subscribe" });
expect(res.statusCode).toBeGreaterThanOrEqual(400);
});
});