Files
aniplay-api/src/controllers/episodes/getEpisodeUrl/index.spec.ts
Rushil Perera 23b14536cc refactor!: migrate away from bun
- migrate package management to pnpm
- migrate test suite to vitest
- also remove Anify integration
2025-12-12 19:24:28 -05:00

96 lines
2.7 KiB
TypeScript

import { env } from "cloudflare:test";
import { beforeEach, describe, expect, it, vi } from "vitest";
// Mock useMockData
vi.mock("~/libs/useMockData", () => ({ useMockData: () => false }));
describe('requests the "/episodes/:id/url" route', () => {
let app: typeof import("../../../src/index").app;
let fetchEpisodes: any;
beforeEach(async () => {
vi.resetModules();
vi.doMock("../getByAniListId", async (importOriginal) => {
const actual = await importOriginal<any>();
return {
...actual,
fetchEpisodes: vi.fn(),
};
});
// Mock aniwatch initially as empty mock
vi.doMock("./aniwatch", () => ({ getSourcesFromAniwatch: vi.fn() }));
app = (await import("~/index")).app;
fetchEpisodes = (await import("../getByAniListId")).fetchEpisodes;
});
it("with sources from Aniwatch", async () => {
vi.mocked(fetchEpisodes).mockResolvedValue([{ id: "ep1", number: 1 }]);
const mockSource = {
source:
"https://www032.vipanicdn.net/streamhls/aa804a2400535d84dd59454b28d329fb/ep.1.1712504065.m3u8",
subtitles: [],
audio: [],
};
// Since controller uses dynamic import, doMock SHOULD affect it if we set it up before the call
// Wait, doMock inside test block might be tricky if we don't re-import the module using it?
// BUT the controller uses `import("./aniwatch")`, causing a fresh import (if cache invalid?)
// Or if `vi.doMock` updates the registry.
// In Vitest, doMock updates the registry for NEXT imports.
// So `import("./aniwatch")` should pick it up.
vi.doMock("./aniwatch", () => ({
getSourcesFromAniwatch: vi.fn().mockResolvedValue(mockSource),
}));
const response = await app.request(
"/episodes/4/url",
{
method: "POST",
body: JSON.stringify({
episodeNumber: 1,
}),
headers: { "Content-Type": "application/json" },
},
env,
);
const json = await response.json();
expect(json).toEqual({
success: true,
result: mockSource,
});
});
it("with no URL from Aniwatch source", async () => {
vi.mocked(fetchEpisodes).mockResolvedValue([{ id: "ep1", number: 1 }]);
// Make mock return null
vi.doMock("./aniwatch", () => ({
getSourcesFromAniwatch: vi.fn().mockResolvedValue(null),
}));
const response = await app.request(
"/episodes/4/url",
{
method: "POST",
body: JSON.stringify({
episodeNumber: 1, // Exists in episodes, but source returns null
}),
headers: { "Content-Type": "application/json" },
},
env,
);
const json = await response.json();
expect(json).toEqual({
success: false,
});
expect(response.status).toBe(404);
});
});