Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 55 additions & 1 deletion apps/radio/src/lib/platform-item-loader.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import { describe, expect, mock, test } from "bun:test";
import type { SpotifyMetadata } from "@avoid.quest/platforms/spotify";
import type { YouTubeClient } from "@avoid.quest/platforms/youtube";
import {
type YouTubeClient,
YouTubeProviderAggregateError,
YouTubeProviderError,
} from "@avoid.quest/platforms/youtube";
import { resolveSpotifyItem as matchSpotifyItem } from "./platform-client";
import { createPlatformItemLoader } from "./platform-item-loader";

Expand Down Expand Up @@ -82,6 +86,56 @@ describe("browser platform item loader", () => {
});
});

// RADIO-25: DJ pick keeps aggregate provider diagnostics for reporting/UI.
test("keeps YouTube provider aggregate diagnostics on resolution failure", async () => {
const aggregate = new YouTubeProviderAggregateError([
new YouTubeProviderError("YouTube provider returned HTTP 500", {
code: "http",
kind: "piped",
providerId: "piped-wireway",
status: 500,
}),
new YouTubeProviderError(
"YouTube provider is unavailable or blocked by CORS",
{
cause: new TypeError("Failed to fetch"),
code: "network-or-cors",
kind: "invidious",
providerId: "invidious-omada",
}
),
]);
const load = createPlatformItemLoader({
getYouTubeClient: () =>
youtubeClient(mock(() => Promise.reject(aggregate))),
resolvePlatformItem: mock(() => {
throw new Error("Platform server must remain unused");
}),
resolveSpotifyItem: unusedSpotify,
resolveStaticAudio: mock(() =>
Promise.reject(new Error("static audio must remain unused"))
),
});

const result = await load("https://youtube.com/watch?v=abcdefghijk");
expect(result).toMatchObject({
cause: aggregate,
code: "YOUTUBE_CLIENT_RESOLUTION_FAILED",
error: "Every configured YouTube provider failed",
success: false,
});
expect(result.success).toBe(false);
if (result.success) {
throw new Error("Expected YouTube resolution to fail");
}
expect(result.cause).toBe(aggregate);
expect(
(result.cause as YouTubeProviderAggregateError).errors.map(
(entry) => entry.providerId
)
).toEqual(["piped-wireway", "invidious-omada"]);
});

test.each([
{
metadata: {
Expand Down
22 changes: 22 additions & 0 deletions apps/radio/src/lib/platform-stream-refresh.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,28 @@ describe("refreshPlatformStream", () => {
await refreshPlatformStream(youtube, "sound-1", 42, refresh);
expect(refresh.onFailed).toHaveBeenCalledWith(expect.anything(), error);
});

// RADIO-25-related: YouTube resolveStream throws; refresh keeps diagnostics.
test("reports YouTube provider pool exhaustion through onFailed", async () => {
const aggregate = new Error("Every configured YouTube provider failed");
aggregate.name = "YouTubeProviderAggregateError";
const refresh = options({
resolveStream: mock(() => Promise.reject(aggregate)),
});
await refreshPlatformStream(youtube, "sound-1", 12, refresh);
expect(refresh.onUnresolved).not.toHaveBeenCalled();
expect(refresh.onFailed).toHaveBeenCalledWith(
expect.objectContaining({
failureCode: "DJ_YOUTUBE_REFRESH_FAILED",
failureMessage: "Failed to refresh YouTube stream - please reload",
resolution: expect.objectContaining({
platform: "youtube",
videoId: "abc",
}),
}),
aggregate
);
});
});

describe("radioOnTrack", () => {
Expand Down
44 changes: 44 additions & 0 deletions apps/radio/src/lib/youtube/client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,50 @@ describe("curated YouTube client", () => {
).toThrow("YouTube provider pool timeout is invalid");
});

// RADIO-25: synthetic browser pool outage (HTTP 500 + Failed to fetch).
test("aggregates curated resolveItem failures across HTTP 500 and fetch errors", async () => {
const hosts = [
"provider-1.example",
"provider-2.example",
"provider-3.example",
"provider-4.example",
] as const;
const fetchImpl = mock((input: RequestInfo | URL) => {
const { host } = new URL(String(input));
if (host === hosts[0] || host === hosts[2]) {
return Promise.resolve(
new Response("upstream unavailable", { status: 500 })
);
}
return Promise.reject(new TypeError("Failed to fetch"));
});
const client = createCuratedYouTubeClient(
hosts.map((host, index) => ({
baseUrl: `https://${host}`,
id: `provider-${index + 1}`,
kind: index === 0 ? ("piped" as const) : ("invidious" as const),
})),
{
fetchImpl: fetchImpl as unknown as typeof fetch,
verifyMedia: false,
}
);

await expect(
client.resolveItem("https://youtube.com/watch?v=abcdefghijk")
).rejects.toMatchObject({
errors: [
{ code: "http", providerId: "provider-1", status: 500 },
{ code: "network-or-cors", providerId: "provider-2" },
{ code: "http", providerId: "provider-3", status: 500 },
{ code: "network-or-cors", providerId: "provider-4" },
],
message: "Every configured YouTube provider failed",
name: "YouTubeProviderAggregateError",
});
expect(fetchImpl.mock.calls.length).toBeGreaterThanOrEqual(4);
});

test("searches and resolves through a curated browser provider", async () => {
const fetchImpl = mock((input: RequestInfo | URL) => {
const url = String(input);
Expand Down
24 changes: 24 additions & 0 deletions packages/error/src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,30 @@ describe("handled playback and transport policy", () => {
expect(capturedScope?.tags.error_code).toBe(errorCode);
});

// RADIO-25: exhausted YouTube provider pools stay unexpected so ops can see
// curated relay outages; callers keep the aggregate error as the cause.
test("reports YouTube provider pool exhaustion with diagnostics preserved", () => {
const aggregate = new Error("Every configured YouTube provider failed");
aggregate.name = "YouTubeProviderAggregateError";
expect(
capturePlaybackError(aggregate, {
errorCode: "YOUTUBE_CLIENT_RESOLUTION_FAILED",
errorMessage: aggregate.message,
mode: "dj",
streamUrl: "https://youtube.com/watch?v=abcdefghijk",
})
).toBe(EVENT_ID);
expect(captureException).toHaveBeenCalledWith(aggregate, {
data: { appError: expect.any(AppError) },
});
expect(capturedScope?.tags).toMatchObject({
error_code: "YOUTUBE_CLIENT_RESOLUTION_FAILED",
error_expected: "false",
mode: "dj",
stream_host: "youtube.com",
});
});

test("reports distinct failures on one host without sending stream paths or station data", () => {
const payload = {
errorCode: "STREAM_FETCH_FAILED",
Expand Down
139 changes: 139 additions & 0 deletions packages/platforms/src/youtube/provider.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -815,6 +815,145 @@ describe("ordered YouTube provider failover", () => {
}
});

// RADIO-25: curated pool exhausted by HTTP 500 and fetch/CORS failures.
test("fails over HTTP 500 and network failures, then aggregates diagnostics", async () => {
const http500 = mock(() =>
Promise.reject(
new YouTubeProviderError("YouTube provider returned HTTP 500", {
code: "http",
kind: "piped",
providerId: "piped-a",
status: 500,
})
)
);
const corsBlocked = mock(() =>
Promise.reject(
new YouTubeProviderError(
"YouTube provider is unavailable or blocked by CORS",
{
cause: new TypeError("Failed to fetch"),
code: "network-or-cors",
kind: "invidious",
providerId: "invidious-b",
}
)
)
);
const http500Again = mock(() =>
Promise.reject(
new YouTubeProviderError("YouTube provider returned HTTP 500", {
code: "http",
kind: "invidious",
providerId: "invidious-c",
status: 500,
})
)
);
const client = createYouTubeClient([
{
...fakeAdapter("piped-a", () => Promise.reject(new Error("unused"))),
resolveItem: http500,
},
{
...fakeAdapter("invidious-b", () =>
Promise.reject(new Error("unused"))
),
resolveItem: corsBlocked,
},
{
...fakeAdapter("invidious-c", () =>
Promise.reject(new Error("unused"))
),
resolveItem: http500Again,
},
]);

try {
await client.resolveItem(`https://youtube.com/watch?v=${VIDEO_ID}`);
throw new Error("Expected every provider to fail");
} catch (error) {
expect(error).toBeInstanceOf(YouTubeProviderAggregateError);
const aggregate = error as YouTubeProviderAggregateError;
expect(aggregate.message).toBe(
"Every configured YouTube provider failed"
);
expect(aggregate.errors.map((entry) => entry.providerId)).toEqual([
"piped-a",
"invidious-b",
"invidious-c",
]);
expect(aggregate.errors.map((entry) => entry.code)).toEqual([
"http",
"network-or-cors",
"http",
]);
expect(aggregate.errors.every((entry) => entry.retryable)).toBe(true);
expect(aggregate.cause).toBe(aggregate.errors.at(-1));
}
expect(http500).toHaveBeenCalledTimes(1);
expect(corsBlocked).toHaveBeenCalledTimes(1);
expect(http500Again).toHaveBeenCalledTimes(1);
});

test("recovers when a later provider succeeds after HTTP 500 and fetch failure", async () => {
const recovered = {
metadata: {
artist: "Artist",
itemType: "video" as const,
name: "Track",
platform: "youtube" as const,
url: `https://youtube.com/watch?v=${VIDEO_ID}`,
videoId: VIDEO_ID,
},
streamUrl: "https://media.example/audio.webm",
success: true as const,
};
const client = createYouTubeClient([
{
...fakeAdapter("piped-a", () => Promise.reject(new Error("unused"))),
resolveItem: mock(() =>
Promise.reject(
new YouTubeProviderError("YouTube provider returned HTTP 500", {
code: "http",
kind: "piped",
providerId: "piped-a",
status: 500,
})
)
),
},
{
...fakeAdapter("invidious-b", () =>
Promise.reject(new Error("unused"))
),
resolveItem: mock(() =>
Promise.reject(
new YouTubeProviderError(
"YouTube provider is unavailable or blocked by CORS",
{
cause: new TypeError("Failed to fetch"),
code: "network-or-cors",
kind: "invidious",
providerId: "invidious-b",
}
)
)
),
},
{
...fakeAdapter("invidious-c", () =>
Promise.reject(new Error("unused"))
),
resolveItem: mock(() => Promise.resolve(recovered)),
},
]);

await expect(
client.resolveItem(`https://youtube.com/watch?v=${VIDEO_ID}`)
).resolves.toEqual(recovered);
});

test("classifies timeouts", async () => {
const adapter = createBrowserInvidiousAdapter({
baseUrl: "https://slow.test",
Expand Down
Loading