2026-05-15 17:43:53 +07:00
|
|
|
import type { Request, Response } from "express";
|
|
|
|
|
import { describe, expect, it, vi } from "vitest";
|
|
|
|
|
import { createMediaRoutes } from "../../src/routes/mediaRoutes";
|
|
|
|
|
|
2026-05-15 18:04:39 +07:00
|
|
|
function getHandler(
|
|
|
|
|
router: ReturnType<typeof createMediaRoutes>,
|
|
|
|
|
path: string,
|
|
|
|
|
method: string,
|
|
|
|
|
) {
|
2026-05-15 17:43:53 +07:00
|
|
|
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(),
|
|
|
|
|
};
|
2026-05-15 18:04:39 +07:00
|
|
|
const handler = getHandler(
|
|
|
|
|
createMediaRoutes(controller),
|
|
|
|
|
"/media/status",
|
|
|
|
|
"get",
|
|
|
|
|
);
|
2026-05-15 17:43:53 +07:00
|
|
|
const json = vi.fn();
|
|
|
|
|
|
|
|
|
|
await handler?.({} as Request, { json } as unknown as Response, vi.fn());
|
|
|
|
|
|
2026-05-15 18:04:39 +07:00
|
|
|
expect(json).toHaveBeenCalledWith({
|
|
|
|
|
playing: false,
|
|
|
|
|
current: null,
|
|
|
|
|
queue: [],
|
|
|
|
|
});
|
2026-05-15 17:43:53 +07:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
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(),
|
|
|
|
|
};
|
2026-05-15 18:04:39 +07:00
|
|
|
const handler = getHandler(
|
|
|
|
|
createMediaRoutes(controller),
|
|
|
|
|
"/media/queue",
|
|
|
|
|
"post",
|
|
|
|
|
);
|
2026-05-15 17:43:53 +07:00
|
|
|
const json = vi.fn();
|
|
|
|
|
|
|
|
|
|
await handler?.(
|
|
|
|
|
{ body: { source: "https://example.com/song.mp3" } } as Request,
|
|
|
|
|
{ json } as unknown as Response,
|
|
|
|
|
vi.fn(),
|
|
|
|
|
);
|
|
|
|
|
|
2026-05-15 18:04:39 +07:00
|
|
|
expect(controller.queue).toHaveBeenCalledWith(
|
|
|
|
|
"https://example.com/song.mp3",
|
|
|
|
|
);
|
2026-05-15 17:43:53 +07:00
|
|
|
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(),
|
|
|
|
|
};
|
2026-05-15 18:04:39 +07:00
|
|
|
const handler = getHandler(
|
|
|
|
|
createMediaRoutes(controller),
|
|
|
|
|
"/media/queue",
|
|
|
|
|
"post",
|
|
|
|
|
);
|
2026-05-15 17:43:53 +07:00
|
|
|
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,
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
});
|