feat: Add unit tests for resolvers and services, update dependencies, and remove unused scripts.

This commit is contained in:
2025-12-07 05:18:23 -05:00
parent 83732913f7
commit 4b3354a5d6
22 changed files with 1204 additions and 275 deletions

View File

@@ -0,0 +1,54 @@
import { env } from "cloudflare:workers";
import { beforeEach, describe, expect, it, vi } from "vitest";
import { getUser } from "./getUser";
describe("getUser service", () => {
const mockStub = {
getUserProfile: vi.fn(),
};
beforeEach(() => {
vi.clearAllMocks();
// @ts-expect-error - Partial mock
env.ANILIST_DO = {
idFromName: vi.fn().mockReturnValue("global-id"),
get: vi.fn().mockReturnValue(mockStub),
};
});
it("should fetch user profile from Durable Object", async () => {
const mockUser = { id: 1, name: "User", statistics: { anime: {} } };
mockStub.getUserProfile.mockResolvedValue(mockUser);
const result = await getUser("token");
expect(result).toEqual({
...mockUser,
statistics: mockUser.statistics.anime,
});
expect(mockStub.getUserProfile).toHaveBeenCalledWith("token");
});
it("should return null if DO throws 401 error", async () => {
mockStub.getUserProfile.mockRejectedValue(new Error("401 Unauthorized"));
const result = await getUser("token");
expect(result).toBeNull();
});
it("should rethrow other DO errors", async () => {
mockStub.getUserProfile.mockRejectedValue(new Error("Other Error"));
await expect(getUser("token")).rejects.toThrow("Other Error");
});
it("should return null if DO returns null", async () => {
mockStub.getUserProfile.mockResolvedValue(null);
const result = await getUser("token");
expect(result).toBeNull();
});
});