Files
OpenFrontIO/tests/client/components/FeaturedStream.test.ts
T
Josh HarrisandGitHub 2ecdbdfe8d Steam desktop authentication support (SteamSDK, Auth branch, audience config, username seeding) (#4683)
> **Maintainer note:** part of the private OpenFront Steam release
(Milestone 2, tracked in Linear), not a public GitHub issue. Per the
contributor policy this needs an approved/assigned issue or a maintainer
label to avoid auto-close — flagging for a maintainer to handle.

Resolves: _n/a — private Steam-release work (see note above)_

## Description:

Adds the client-side pieces for authenticating the OpenFront **Steam
desktop build** (an Electron shell) against the existing API, mirroring
the established CrazyGames integration. The native Steamworks code lives
in the desktop shell and is exposed to this client through a
`window.openfrontDesktop.steam` bridge; this PR is the renderer half.

- **`SteamSDK.ts`** (new) — thin wrapper over the desktop shell's Steam
bridge (`isOnSteam` / `getTicket` / `getUser`), mirroring
`CrazyGamesSDK`; narrows the loosely-typed `window.openfrontDesktop`
locally rather than re-declaring it.
- **`Auth.ts`** — a Steam branch at the top of `doRefreshJwt()` plus
`doSteamLogin()`, exchanging a Steam Web-API ticket at `POST
/auth/steam` for a session JWT, exactly paralleling `doCrazyGamesLogin`.
Re-exchanges on expiry (the refresh cookie is cross-site-blocked from
the `app://` origin); no ticket / not on Steam falls through to the
existing flow.
- **`Api.ts`** — `getAudience()` now sources from `BOOTSTRAP_CONFIG`
(`ClientEnv.jwtAudience()`) instead of `window.location`, so the desktop
app (running at `app://openfront`) resolves the real API base.
Behavior-preserving on web, where the injected audience already equals
the hostname-derived one.
- **`UsernameInput.ts`** — seeds the in-game display name from the Steam
persona on first launch and persists it (unlike CrazyGames). Writes only
the local display name; never the account username.
- **`SteamLinkSignpost.ts`** (new) — a one-time, dismissible notice
shown on first Steam launch that account linking is coming.

## Please complete the following:

- [ ] I have added screenshots for all UI updates — ⚠️ _the new
first-launch link signpost is a UI addition; it renders only inside the
Steam desktop shell. Screenshots to be added by the maintainer._
- [x] I process any text displayed to the user through translateText()
and I've added it to the en.json file — signpost strings go through
`translateText()` with keys added to `resources/lang/en.json`
(`steam.link_signpost`, `common.got_it`).
- [x] I have added relevant tests to the test directory — unit tests for
`SteamSDK` (incl. bridge-throws degradation), the `Auth.ts` Steam
exchange + fall-through, the `Api.ts` audience change, username seeding,
and the signpost gating. Full suite green (1976) + `tsc --noEmit` clean.

## Please put your Discord username so you can be contacted if a bug or
regression is found:

_⚠️ maintainer to fill_

🤖 Generated with [Claude Code](https://claude.com/claude-code)

https://claude.ai/code/session_01BWxUzYb2uqjcBFQuNSJhMy
2026-07-23 14:13:14 +01:00

142 lines
4.9 KiB
TypeScript

import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import featuredStream from "../../../resources/featured-stream.json";
import { getFeaturedStream } from "../../../src/client/Api";
import { ClientEnv } from "../../../src/client/ClientEnv";
import { cornerFromCenter } from "../../../src/client/FeaturedStream";
import { FeaturedStreamSchema } from "../../../src/core/ApiSchemas";
describe("FeaturedStream", () => {
describe("bundled config (resources/featured-stream.json)", () => {
it("validates against the schema", () => {
expect(FeaturedStreamSchema.safeParse(featuredStream).success).toBe(true);
});
it("is off by default (no channel shown unless OF turns it on)", () => {
const cfg = FeaturedStreamSchema.parse(featuredStream);
expect(cfg.enabled).toBe(false);
expect(cfg.channels).toEqual([]);
});
});
describe("FeaturedStreamSchema", () => {
it("defaults to disabled with no channels", () => {
const cfg = FeaturedStreamSchema.parse({});
expect(cfg.enabled).toBe(false);
expect(cfg.channels).toEqual([]);
});
it("accepts enabled with a channel list", () => {
const cfg = FeaturedStreamSchema.parse({
enabled: true,
channels: ["openfrontmasters", "openfront"],
});
expect(cfg.enabled).toBe(true);
expect(cfg.channels).toEqual(["openfrontmasters", "openfront"]);
});
it("drops bad channel entries instead of failing the whole config", () => {
// Non-strings, too-short, illegal chars, and full URLs are all dropped
// individually so one garbage entry can't silently disable the feature for
// every client; the valid login still comes through.
const cfg = FeaturedStreamSchema.parse({
enabled: true,
channels: [
1,
"ab",
"has space",
"bad!",
"https://twitch.tv/x",
"openfrontmasters",
],
});
expect(cfg.channels).toEqual(["openfrontmasters"]);
});
it("accepts a valid channel login", () => {
expect(
FeaturedStreamSchema.safeParse({ channels: ["eslcs", "es_l_2"] })
.success,
).toBe(true);
});
});
describe("getFeaturedStream", () => {
const off = { enabled: false, channels: [] };
beforeEach(() => {
vi.unstubAllGlobals();
vi.spyOn(console, "warn").mockImplementation(() => {});
// getApiBase() → getAudience() now reads the JWT audience from
// BOOTSTRAP_CONFIG (ClientEnv), so getFeaturedStream needs it present or
// it throws before fetch and silently falls back. The value is irrelevant
// here since fetch is stubbed; we only need a well-formed config.
(window as any).BOOTSTRAP_CONFIG = {
gameEnv: "prod",
numWorkers: 1,
turnstileSiteKey: "x",
jwtAudience: "openfront.io",
instanceId: "desktop",
gitCommit: "test",
};
ClientEnv.reset();
});
afterEach(() => {
vi.restoreAllMocks();
vi.unstubAllGlobals();
delete (window as any).BOOTSTRAP_CONFIG;
ClientEnv.reset();
});
const stubFetch = (impl: () => unknown) =>
vi.stubGlobal("fetch", vi.fn(impl));
it("returns the served config on HTTP 200 with valid JSON", async () => {
stubFetch(() =>
Promise.resolve({
status: 200,
json: async () => ({ enabled: true, channels: ["eslcs"] }),
}),
);
const cfg = await getFeaturedStream();
expect(cfg).toEqual({ enabled: true, channels: ["eslcs"] });
});
it("falls back to the bundled config on a non-200 status", async () => {
stubFetch(() => Promise.resolve({ status: 404, json: async () => ({}) }));
expect(await getFeaturedStream()).toEqual(off);
});
it("drops invalid channels instead of failing the whole config", async () => {
stubFetch(() =>
Promise.resolve({
status: 200,
json: async () => ({
enabled: true,
channels: ["valid_chan", "bad name!", "x"],
}),
}),
);
// "bad name!" (space/!) and "x" (too short) are dropped; the valid one stays,
// so one garbage entry can't silently disable the feature for everyone.
expect(await getFeaturedStream()).toEqual({
enabled: true,
channels: ["valid_chan"],
});
});
it("falls back when the request rejects (network error)", async () => {
stubFetch(() => Promise.reject(new Error("network down")));
expect(await getFeaturedStream()).toEqual(off);
});
});
describe("cornerFromCenter", () => {
it("maps each quadrant to the nearest corner", () => {
expect(cornerFromCenter(100, 100, 1000, 800)).toBe("tl");
expect(cornerFromCenter(900, 100, 1000, 800)).toBe("tr");
expect(cornerFromCenter(100, 700, 1000, 800)).toBe("bl");
expect(cornerFromCenter(900, 700, 1000, 800)).toBe("br");
});
});
});