|
| 1 | +import type { BetterAuthOptions } from "@better-auth/core"; |
1 | 2 | import { describe, expect, it, vi } from "vitest"; |
2 | 3 | import { prismaAdapter } from "./prisma-adapter"; |
3 | 4 |
|
4 | 5 | describe("prisma-adapter", () => { |
| 6 | + const createTestAdapter = (prisma: Record<string, unknown>) => |
| 7 | + prismaAdapter(prisma as never, { |
| 8 | + provider: "sqlite", |
| 9 | + })({} as BetterAuthOptions); |
| 10 | + |
5 | 11 | it("should create prisma adapter", () => { |
6 | 12 | const prisma = { |
7 | 13 | $transaction: vi.fn(), |
8 | | - } as any; |
9 | | - const adapter = prismaAdapter(prisma, { |
| 14 | + }; |
| 15 | + const adapter = prismaAdapter(prisma as never, { |
10 | 16 | provider: "sqlite", |
11 | 17 | }); |
12 | 18 | expect(adapter).toBeDefined(); |
13 | 19 | }); |
| 20 | + |
| 21 | + /** |
| 22 | + * @see https://github.com/better-auth/better-auth/issues/8365 |
| 23 | + */ |
| 24 | + it("should fall back to updateMany for non-unique verification identifiers", async () => { |
| 25 | + const update = vi.fn(); |
| 26 | + const updateMany = vi.fn().mockResolvedValue({ count: 1 }); |
| 27 | + const findFirst = vi.fn().mockResolvedValue({ |
| 28 | + id: "verification-id", |
| 29 | + identifier: "magic-link-token", |
| 30 | + value: "updated-value", |
| 31 | + }); |
| 32 | + const adapter = createTestAdapter({ |
| 33 | + $transaction: vi.fn(), |
| 34 | + verification: { |
| 35 | + findFirst, |
| 36 | + update, |
| 37 | + updateMany, |
| 38 | + }, |
| 39 | + }); |
| 40 | + |
| 41 | + const result = await adapter.update({ |
| 42 | + model: "verification", |
| 43 | + where: [{ field: "identifier", value: "magic-link-token" }], |
| 44 | + update: { value: "updated-value" }, |
| 45 | + }); |
| 46 | + |
| 47 | + expect(update).not.toHaveBeenCalled(); |
| 48 | + expect(updateMany).toHaveBeenCalledWith( |
| 49 | + expect.objectContaining({ |
| 50 | + where: { |
| 51 | + identifier: "magic-link-token", |
| 52 | + }, |
| 53 | + data: expect.objectContaining({ |
| 54 | + value: "updated-value", |
| 55 | + updatedAt: expect.any(Date), |
| 56 | + }), |
| 57 | + }), |
| 58 | + ); |
| 59 | + expect(findFirst).toHaveBeenCalledWith({ |
| 60 | + where: { |
| 61 | + identifier: "magic-link-token", |
| 62 | + }, |
| 63 | + }); |
| 64 | + expect(result).toEqual({ |
| 65 | + id: "verification-id", |
| 66 | + identifier: "magic-link-token", |
| 67 | + value: "updated-value", |
| 68 | + }); |
| 69 | + }); |
| 70 | + |
| 71 | + it("should keep using update for unique non-id fields", async () => { |
| 72 | + const update = vi.fn().mockResolvedValue({ |
| 73 | + id: "session-id", |
| 74 | + token: "session-token", |
| 75 | + userId: "user-id", |
| 76 | + }); |
| 77 | + const updateMany = vi.fn(); |
| 78 | + const findFirst = vi.fn(); |
| 79 | + const adapter = createTestAdapter({ |
| 80 | + $transaction: vi.fn(), |
| 81 | + session: { |
| 82 | + findFirst, |
| 83 | + update, |
| 84 | + updateMany, |
| 85 | + }, |
| 86 | + }); |
| 87 | + |
| 88 | + const result = await adapter.update({ |
| 89 | + model: "session", |
| 90 | + where: [{ field: "token", value: "session-token" }], |
| 91 | + update: { userId: "user-id" }, |
| 92 | + }); |
| 93 | + |
| 94 | + expect(update).toHaveBeenCalledWith( |
| 95 | + expect.objectContaining({ |
| 96 | + where: { |
| 97 | + token: "session-token", |
| 98 | + }, |
| 99 | + data: expect.objectContaining({ |
| 100 | + userId: "user-id", |
| 101 | + updatedAt: expect.any(Date), |
| 102 | + }), |
| 103 | + }), |
| 104 | + ); |
| 105 | + expect(updateMany).not.toHaveBeenCalled(); |
| 106 | + expect(findFirst).not.toHaveBeenCalled(); |
| 107 | + expect(result).toEqual({ |
| 108 | + id: "session-id", |
| 109 | + token: "session-token", |
| 110 | + userId: "user-id", |
| 111 | + }); |
| 112 | + }); |
14 | 113 | }); |
0 commit comments