|
| 1 | +/** |
| 2 | + * @vitest-environment node |
| 3 | + */ |
| 4 | +import * as schema from '@sim/db/schema' |
| 5 | +import { withUtcTimestamps } from '@sim/db/timestamps' |
| 6 | +import { generateId } from '@sim/utils/id' |
| 7 | +import type { BetterAuthOptions } from 'better-auth' |
| 8 | +import { drizzleAdapter } from 'better-auth/adapters/drizzle' |
| 9 | +import { organization } from 'better-auth/plugins' |
| 10 | +import { drizzle } from 'drizzle-orm/postgres-js' |
| 11 | +import postgres from 'postgres' |
| 12 | +import { describe, expect, it, vi } from 'vitest' |
| 13 | +import { createSimAuthAdapter } from '@/lib/auth/sim-auth-adapter' |
| 14 | + |
| 15 | +vi.unmock('@sim/db/schema') |
| 16 | +vi.unmock('drizzle-orm') |
| 17 | + |
| 18 | +/** The guard suites cover authorization; this exercises adapter SQL against the real table shape. */ |
| 19 | +vi.mock('@/lib/auth/oauth-provider-adapter-guard', () => ({ |
| 20 | + guardOAuthProviderWrites: (adapter: object) => adapter, |
| 21 | +})) |
| 22 | +vi.mock('@/lib/auth/stripe-adapter-guard', () => ({ |
| 23 | + guardSubscriptionPlanWrites: (adapter: object) => adapter, |
| 24 | +})) |
| 25 | + |
| 26 | +const OPTIONS: BetterAuthOptions = { plugins: [organization()] } |
| 27 | +const databaseUrl = process.env.OAUTH_TOKEN_FAMILY_TEST_DATABASE_URL |
| 28 | +type AdapterSurface = Omit<ReturnType<typeof createSimAuthAdapter>, 'transaction'> |
| 29 | + |
| 30 | +async function exerciseOrganization(adapter: AdapterSurface) { |
| 31 | + const id = generateId() |
| 32 | + const data = { id, name: 'Example', slug: id, createdAt: new Date('2026-01-01T00:00:00Z') } |
| 33 | + const where = [{ field: 'id', value: id }] |
| 34 | + |
| 35 | + await expect( |
| 36 | + adapter.create({ model: 'organization', data, forceAllowId: true }) |
| 37 | + ).resolves.toMatchObject(data) |
| 38 | + await expect(adapter.findOne({ model: 'organization', where })).resolves.toMatchObject(data) |
| 39 | + await expect(adapter.findMany({ model: 'organization', where })).resolves.toEqual([ |
| 40 | + expect.objectContaining(data), |
| 41 | + ]) |
| 42 | + await expect( |
| 43 | + adapter.update({ model: 'organization', where, update: { name: 'Renamed' } }) |
| 44 | + ).resolves.toMatchObject({ id, name: 'Renamed' }) |
| 45 | + await adapter.delete({ model: 'organization', where }) |
| 46 | + await expect(adapter.findOne({ model: 'organization', where })).resolves.toBeNull() |
| 47 | +} |
| 48 | + |
| 49 | +describe.skipIf(!databaseUrl)('Better Auth across the organization column drop', () => { |
| 50 | + it.each([false, true])('preserves CRUD with transaction=%s', async (transaction) => { |
| 51 | + const client = postgres( |
| 52 | + databaseUrl!, |
| 53 | + withUtcTimestamps({ max: 1, prepare: false, fetch_types: false }) |
| 54 | + ) |
| 55 | + try { |
| 56 | + /** The connection-local copy lets other integration suites keep using the public table. */ |
| 57 | + await client`CREATE TEMP TABLE organization (LIKE public.organization INCLUDING ALL)` |
| 58 | + const database = drizzle(client, { schema }) |
| 59 | + const adapter = createSimAuthAdapter(OPTIONS, database) |
| 60 | + const exercise = () => |
| 61 | + transaction |
| 62 | + ? adapter.transaction((tx) => exerciseOrganization(tx)) |
| 63 | + : exerciseOrganization(adapter) |
| 64 | + |
| 65 | + await exercise() |
| 66 | + await client`ALTER TABLE pg_temp.organization DROP COLUMN departed_member_usage` |
| 67 | + |
| 68 | + const unprojected = drizzleAdapter(database, { provider: 'pg', schema })(OPTIONS) |
| 69 | + await expect( |
| 70 | + unprojected.findOne({ model: 'organization', where: [{ field: 'id', value: 'missing' }] }) |
| 71 | + ).rejects.toMatchObject({ cause: { code: '42703' } }) |
| 72 | + |
| 73 | + await exercise() |
| 74 | + } finally { |
| 75 | + await client.end() |
| 76 | + } |
| 77 | + }) |
| 78 | +}) |
0 commit comments