From a16ad5b8b5edeb0fb24806549a298bc745c78fc9 Mon Sep 17 00:00:00 2001 From: Sidney Swift <158200036+sidneyswift@users.noreply.github.com> Date: Wed, 18 Nov 2026 09:27:48 -0500 Subject: [PATCH] Respect explicit Spotify configuration availability --- app/listen/[id]/route.ts | 7 +- .../__tests__/playerCapabilities.test.ts | 148 ++++++++++++++++++ lib/players/getPlayerConfig.ts | 22 +++ .../__tests__/spotifyFreeHandoff.test.ts | 21 +++ public/sites-runtime.js | 3 + 5 files changed, 200 insertions(+), 1 deletion(-) create mode 100644 lib/players/__tests__/playerCapabilities.test.ts diff --git a/app/listen/[id]/route.ts b/app/listen/[id]/route.ts index 442c15370..33c472c98 100644 --- a/app/listen/[id]/route.ts +++ b/app/listen/[id]/route.ts @@ -15,8 +15,13 @@ export async function GET( if (value) campaign.set(key, value.slice(0, 100)); } const suffix = campaign.size ? `?${escape(campaign.toString())}` : ""; + const spotifyUnconfigured = + config.capabilities?.spotify.clientConfiguration === "unconfigured"; + const spotifyLabel = spotifyUnconfigured + ? "Open player" + : "Sign in with Spotify"; return new Response( - `${escape(config.name)} · Recoup
${config.artwork ? `${escape(config.name)} artwork` : ""}

${escape(config.name)}

Choose where to listen.

${config.spotifyUrl ? `Sign in with SpotifyOpen Spotify` : ""}${config.appleUrl ? `Sign in with AppleOpen Apple Music` : ""}
`, + `${escape(config.name)} · Recoup
${config.artwork ? `${escape(config.name)} artwork` : ""}

${escape(config.name)}

Choose where to listen.

${config.spotifyUrl ? `${spotifyUnconfigured ? "

Spotify sign-in is unavailable.

" : ""}${!spotifyUnconfigured || config.freePlayback === "audio" ? `${spotifyLabel}` : ""}Open Spotify` : ""}${config.appleUrl ? `Sign in with AppleOpen Apple Music` : ""}
`, { headers: { "Content-Type": "text/html; charset=utf-8", diff --git a/lib/players/__tests__/playerCapabilities.test.ts b/lib/players/__tests__/playerCapabilities.test.ts new file mode 100644 index 000000000..f8617d10c --- /dev/null +++ b/lib/players/__tests__/playerCapabilities.test.ts @@ -0,0 +1,148 @@ +import { afterEach, beforeEach, expect, it, vi } from "vitest"; +import { getPlayerConfig } from "../getPlayerConfig"; +import { GET } from "@/app/listen/[id]/route"; +import { spotifyRuntimeHarness } from "@/lib/sites/__tests__/fixtures/spotifyRuntimeHarness"; +const id = "10000000-0000-4000-8000-000000000001"; +const capabilities = (configured = false) => ({ + version: 1, + spotify: { + clientConfiguration: configured ? "configured" : "unconfigured", + releaseConfigured: true, + premiumEligibility: "unknown", + tokenStatus: "unknown", + scopeStatus: "unknown", + playbackSupport: "unknown", + }, +}); +const base = { + playerId: id, + name: "Release ", + spotifyUrl: "https://open.spotify.com/track/abc", + appleUrl: "https://music.apple.com/us/album/song/123", + revision: 1, + freePlayback: "spotify", +}; +beforeEach(() => vi.stubGlobal("fetch", vi.fn())); +afterEach(() => { + vi.unstubAllGlobals(); + vi.unstubAllEnvs(); +}); +function serve(value: unknown) { + vi.mocked(fetch).mockImplementation(async () => Response.json(value)); +} +it("retains a validated v1 observation without exposing unexpected properties", async () => { + serve({ + ...base, + capabilities: { ...capabilities(), privateToken: "never" }, + }); + expect(await getPlayerConfig(id, new URLSearchParams())).toMatchObject({ + capabilities: capabilities(), + }); + const result = await getPlayerConfig(id, new URLSearchParams()); + expect(result).not.toHaveProperty("capabilities.privateToken"); +}); +it.each([ + undefined, + { version: 2, spotify: { clientConfiguration: "unconfigured" } }, +])("ignores legacy/future capability shapes (%j)", async (value) => { + serve({ ...base, capabilities: value }); + expect( + (await getPlayerConfig(id, new URLSearchParams())).capabilities, + ).toBeUndefined(); +}); +it.each([ + { version: 1 }, + { version: 1, spotify: { ...capabilities().spotify, tokenStatus: "valid" } }, + { + version: 1, + spotify: { ...capabilities().spotify, releaseConfigured: "yes" }, + }, +])("rejects malformed v1 (%j)", async (value) => { + serve({ ...base, capabilities: value }); + await expect(getPlayerConfig(id, new URLSearchParams())).rejects.toThrow(); +}); +it.each(["spotify", "audio"])( + "avoids an impossible sign-in invitation while retaining %s fallback", + async (mode) => { + serve({ ...base, freePlayback: mode, capabilities: capabilities() }); + const response = await GET( + new Request( + "https://app.recoupable.dev/listen/" + + id + + "?campaign=%22%3E%3Cscript%3E", + ), + { params: Promise.resolve({ id }) }, + ); + const html = await response.text(); + expect(html).toContain("Spotify sign-in is unavailable."); + expect(html).not.toContain("Sign in with Spotify"); + expect(html).toContain("Open Spotify"); + expect(html.includes("Open player")).toBe(mode === "audio"); + expect(html).toContain("Sign in with Apple"); + expect(html).toContain("Release <img src=x>"); + expect(html).not.toContain("