feat: add extensive test suite

This commit is contained in:
2026-08-30 11:33:53 +07:00 Unverified
parent 061cce00dc
commit 8359c7272e
131 changed files with 8717 additions and 2200 deletions
+67
View File
@@ -0,0 +1,67 @@
import { describe, expect, mock, test } from "bun:test";
import { createChatHandler } from "./handler";
const request = () => new Request("https://example.com/api/chat");
describe("chat handler", () => {
test("fetches the live endpoint without caching and redirects to popout chat", async () => {
const fetch = mock((_input: RequestInfo | URL, _init?: RequestInit) =>
Promise.resolve(
Response.json({ url: "https://youtube.com/watch?v=abc_123&feature=x" }),
),
);
const response = await createChatHandler({ fetch })(request());
expect(fetch).toHaveBeenCalledWith("https://buzz.sudloh.com/api/live", {
cache: "no-store",
});
expect(response.status).toBe(307);
expect(response.headers.get("location")).toBe(
"https://studio.youtube.com/live_chat?is_popout=1&v=abc_123",
);
});
test("returns 502 when the live endpoint fails", async () => {
const handler = createChatHandler({
fetch: mock(() => Promise.resolve(new Response(null, { status: 500 }))),
});
const response = await handler(request());
expect(response.status).toBe(502);
expect(await response.text()).toBe("Failed to get live stream.");
});
test("returns 404 when there is no live stream", async () => {
const handler = createChatHandler({
fetch: mock(() => Promise.resolve(Response.json("none"))),
});
const response = await handler(request());
expect(response.status).toBe(404);
expect(await response.text()).toContain("No live stream");
});
test.each([
{ url: "https://youtube.com/watch?feature=x" },
{ url: "not a url" },
])("returns 502 for a live URL without a usable video id", async (live) => {
const handler = createChatHandler({
fetch: mock(() => Promise.resolve(Response.json(live))),
});
const response = await handler(request());
expect(response.status).toBe(502);
expect(await response.text()).toContain("video id was not found");
});
test("encodes the video id in the redirect URL", async () => {
const handler = createChatHandler({
fetch: mock(() =>
Promise.resolve(
Response.json({ url: "https://youtube.com/watch?v=a%26b" }),
),
),
});
expect((await handler(request())).headers.get("location")).toEndWith(
"v=a%26b",
);
});
});
+40
View File
@@ -0,0 +1,40 @@
type LiveResponse = { url: string } | "none";
type Fetch = (
input: RequestInfo | URL,
init?: RequestInit,
) => Promise<Response>;
export function createChatHandler({ fetch }: { fetch: Fetch }) {
return async function GET(request: Request) {
const liveUrl = new URL(
"https://buzz.sudloh.com/api/live",
request.url,
).toString();
const response = await fetch(liveUrl, { cache: "no-store" });
if (!response.ok)
return new Response("Failed to get live stream.", { status: 502 });
const liveInfo: LiveResponse = await response.json();
if (liveInfo === "none")
return new Response("No live stream found for this channel.", {
status: 404,
});
let videoId: string | null = null;
try {
videoId = new URL(liveInfo.url).searchParams.get("v");
} catch {}
if (!videoId)
return new Response("Live stream video id was not found.", {
status: 502,
});
return Response.redirect(
`https://studio.youtube.com/live_chat?is_popout=1&v=${encodeURIComponent(videoId)}`,
307,
);
};
}
+2 -42
View File
@@ -1,43 +1,3 @@
import { NextResponse } from "next/server";
import { createChatHandler } from "./handler";
type LiveResponse =
| {
url: string;
}
| "none";
export async function GET(request: Request) {
const liveUrl = new URL(
"https://buzz.sudloh.com/api/live",
request.url,
).toString();
const response = await fetch(liveUrl, {
cache: "no-store",
});
if (!response.ok) {
return new NextResponse("Failed to get live stream.", {
status: 502,
});
}
const liveInfo: LiveResponse = await response.json();
if (liveInfo === "none") {
return new NextResponse("No live stream found for this channel.", {
status: 404,
});
}
const videoId = new URL(liveInfo.url).searchParams.get("v");
if (!videoId) {
return new NextResponse("Live stream video id was not found.", {
status: 502,
});
}
return NextResponse.redirect(
`https://studio.youtube.com/live_chat?is_popout=1&v=${videoId}`,
);
}
export const GET = createChatHandler({ fetch });