refactor!: migrate away from bun
- migrate package management to pnpm - migrate test suite to vitest - also remove Anify integration
This commit is contained in:
@@ -1,12 +1,52 @@
|
||||
import { describe, expect, it } from "bun:test";
|
||||
import { env } from "cloudflare:test";
|
||||
import { beforeEach, describe, expect, it, vi } from "vitest";
|
||||
|
||||
import app from "~/index";
|
||||
import { server } from "~/mocks";
|
||||
|
||||
server.listen();
|
||||
// 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",
|
||||
{
|
||||
@@ -16,32 +56,40 @@ describe('requests the "/episodes/:id/url" route', () => {
|
||||
}),
|
||||
headers: { "Content-Type": "application/json" },
|
||||
},
|
||||
{
|
||||
ENABLE_ANIFY: "true",
|
||||
},
|
||||
env,
|
||||
);
|
||||
|
||||
expect(response.json()).resolves.toEqual({
|
||||
const json = await response.json();
|
||||
expect(json).toEqual({
|
||||
success: true,
|
||||
result: {
|
||||
source:
|
||||
"https://www032.vipanicdn.net/streamhls/aa804a2400535d84dd59454b28d329fb/ep.1.1712504065.m3u8",
|
||||
subtitles: [],
|
||||
audio: [],
|
||||
},
|
||||
result: mockSource,
|
||||
});
|
||||
});
|
||||
|
||||
it("with no URL from Aniwatch source", async () => {
|
||||
const response = await app.request("/episodes/-1/url", {
|
||||
method: "POST",
|
||||
body: JSON.stringify({
|
||||
episodeNumber: -1,
|
||||
}),
|
||||
headers: { "Content-Type": "application/json" },
|
||||
});
|
||||
vi.mocked(fetchEpisodes).mockResolvedValue([{ id: "ep1", number: 1 }]);
|
||||
|
||||
expect(response.json()).resolves.toEqual({ success: false });
|
||||
// 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);
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user