import { env } from "cloudflare:workers"; import { GraphQLError } from "graphql"; import { describe, expect, it, vi } from "vitest"; import type { GraphQLContext } from "~/context"; import { home } from "./home"; enum HomeCategory { WATCHING, PLANNING, } describe("home resolver", () => { const mockContext = { user: { name: "testuser" }, aniListToken: "test-token", } as GraphQLContext; it("should fetch WATCHING titles using CURRENT status filter", async () => { const mockResponse = { some: "data" }; const mockStub = { getTitles: vi.fn().mockResolvedValue(mockResponse), }; // @ts-expect-error - Partial mock env.ANILIST_DO = { getByName: vi.fn().mockResolvedValue(mockStub), }; const result = await home( null, { category: HomeCategory.WATCHING }, mockContext, ); expect(result).toEqual(mockResponse); expect(env.ANILIST_DO.getByName).toHaveBeenCalledWith("GLOBAL"); expect(mockStub.getTitles).toHaveBeenCalledWith( "testuser", 1, ["CURRENT"], "test-token", ); }); it("should fetch PLANNING titles using PLANNING, PAUSED, REPEATING status filters", async () => { const mockResponse = { some: "data" }; const mockStub = { getTitles: vi.fn().mockResolvedValue(mockResponse), }; // @ts-expect-error - Partial mock env.ANILIST_DO = { getByName: vi.fn().mockResolvedValue(mockStub), }; const result = await home( null, { category: HomeCategory.PLANNING, page: 2 }, mockContext, ); expect(result).toEqual(mockResponse); expect(mockStub.getTitles).toHaveBeenCalledWith( "testuser", 2, ["PLANNING", "PAUSED", "REPEATING"], "test-token", ); }); it("should throw GraphQLError if Durable Object response is null", async () => { const mockStub = { getTitles: vi.fn().mockResolvedValue(null), }; // @ts-expect-error - Partial mock env.ANILIST_DO = { getByName: vi.fn().mockResolvedValue(mockStub), }; await expect( home(null, { category: HomeCategory.WATCHING }, mockContext), ).rejects.toThrow( new GraphQLError("Failed to fetch 0 titles", { extensions: { code: "INTERNAL_SERVER_ERROR" }, }), ); }); });