feat: expose media playback routes

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
MythEclipse
2026-05-15 17:43:53 +07:00
parent b00def2d4d
commit 94e497b7a6
2 changed files with 119 additions and 0 deletions

View File

@@ -0,0 +1,68 @@
import type { Request, Response } from "express";
import { describe, expect, it, vi } from "vitest";
import { createMediaRoutes } from "../../src/routes/mediaRoutes";
function getHandler(router: ReturnType<typeof createMediaRoutes>, path: string, method: string) {
const layer = router.stack.find((item) => item.route?.path === path);
return layer?.route?.stack.find((item) => item.method === method)?.handle;
}
describe("createMediaRoutes", () => {
it("returns media status", async () => {
const controller = {
getState: vi.fn(() => ({ playing: false, current: null, queue: [] })),
queue: vi.fn(),
skip: vi.fn(),
stop: vi.fn(),
};
const handler = getHandler(createMediaRoutes(controller), "/media/status", "get");
const json = vi.fn();
await handler?.({} as Request, { json } as unknown as Response, vi.fn());
expect(json).toHaveBeenCalledWith({ playing: false, current: null, queue: [] });
});
it("queues a source", async () => {
const state = { playing: true, current: null, queue: [] };
const controller = {
getState: vi.fn(),
queue: vi.fn(async () => state),
skip: vi.fn(),
stop: vi.fn(),
};
const handler = getHandler(createMediaRoutes(controller), "/media/queue", "post");
const json = vi.fn();
await handler?.(
{ body: { source: "https://example.com/song.mp3" } } as Request,
{ json } as unknown as Response,
vi.fn(),
);
expect(controller.queue).toHaveBeenCalledWith("https://example.com/song.mp3");
expect(json).toHaveBeenCalledWith(state);
});
it("passes missing source errors to Express", async () => {
const controller = {
getState: vi.fn(),
queue: vi.fn(),
skip: vi.fn(),
stop: vi.fn(),
};
const handler = getHandler(createMediaRoutes(controller), "/media/queue", "post");
const next = vi.fn();
await handler?.(
{ body: {} } as Request,
{ json: vi.fn() } as unknown as Response,
next,
);
expect(next.mock.calls[0][0]).toMatchObject({
code: "MISSING_MEDIA_SOURCE",
statusCode: 400,
});
});
});