import { describe, expect, it, vi, beforeEach } from "vitest"; // Mock uuid const mockUuidv7 = vi.fn(() => "test-uuid-v7"); vi.mock("uuid", () => ({ v7: mockUuidv7, })); // Mock database const mockDb = { query: vi.fn(), select: vi.fn(), insert: vi.fn(), update: vi.fn(), delete: vi.fn(), }; vi.mock("#shared/utils/db/index", () => ({ default: mockDb, })); // Mock better-auth const mockBetterAuth = vi.fn((config) => ({ handler: vi.fn(), api: vi.fn(), config, $Infer: {} as any, })); const mockDrizzleAdapter = vi.fn((db, options) => ({ db, options, type: "drizzle-adapter", })); vi.mock("better-auth", () => ({ betterAuth: mockBetterAuth, })); vi.mock("better-auth/adapters/drizzle", () => ({ drizzleAdapter: mockDrizzleAdapter, })); describe("auth utility", () => { beforeEach(() => { vi.clearAllMocks(); }); it("should create betterAuth instance with correct configuration", async () => { // Import the auth module (this will trigger the betterAuth call) await import("#shared/utils/auth"); // Verify betterAuth was called expect(mockBetterAuth).toHaveBeenCalledTimes(1); // Get the configuration passed to betterAuth const config = mockBetterAuth.mock.calls[0][0]; // Verify the configuration structure expect(config).toBeDefined(); expect(config).toHaveProperty("database"); expect(config).toHaveProperty("advanced"); expect(config).toHaveProperty("emailAndPassword"); // Verify only emailAndPassword is configured expect(config).not.toHaveProperty("oauth"); expect(config).not.toHaveProperty("magicLink"); expect(config).not.toHaveProperty("twoFactor"); // Verify nested properties expect(config.advanced).toHaveProperty("database"); expect(config.advanced.database).toHaveProperty("generateId"); expect(config.emailAndPassword).toHaveProperty("enabled"); // Verify drizzleAdapter was called with correct arguments expect(mockDrizzleAdapter).toHaveBeenCalledTimes(1); expect(mockDrizzleAdapter).toHaveBeenCalledWith(mockDb, { provider: "pg", }); // Verify emailAndPassword is enabled expect(config.emailAndPassword).toEqual({ enabled: true, }); // Verify advanced.database.generateId is a function expect(config.advanced).toBeDefined(); expect(config.advanced.database).toBeDefined(); expect(config.advanced.database.generateId).toBeTypeOf("function"); // Test the generateId function const generatedId = config.advanced.database.generateId(); expect(mockUuidv7).toHaveBeenCalled(); expect(generatedId).toBe("test-uuid-v7"); // Get the drizzle adapter call const dbInstance = mockDrizzleAdapter.mock.calls[0][0]; expect(dbInstance).toBe(mockDb); }); it("should export auth instance with expected properties", async () => { // Import the auth module const { auth } = await import("#shared/utils/auth"); // Verify the auth instance has expected properties expect(auth).toBeDefined(); expect(auth).toHaveProperty("handler"); expect(auth).toHaveProperty("api"); expect(auth).toHaveProperty("config"); }); describe("module exports", () => { it("should export auth as named export", async () => { // Import the auth module const authModule = await import("#shared/utils/auth"); // Verify named export exists expect(authModule).toHaveProperty("auth"); expect(authModule.auth).toBeDefined(); }); it("should not have default export", async () => { // Import the auth module const authModule = await import("#shared/utils/auth"); // Verify no default export (or it's the same as named export) // In ES modules, default export would be authModule.default // @ts-expect-error The description must be 10 characters or longer expect(authModule.default).toBeUndefined(); }); }); });