|
| 1 | +import type { BetterAuthOptions } from '@better-auth/core'; |
| 2 | +import type { BetterAuthDBSchema } from 'better-auth/db'; |
| 3 | +import fs from 'node:fs'; |
| 4 | +import path from 'node:path'; |
| 5 | +import tmp from 'tmp'; |
| 6 | +import { describe, expect, it } from 'vitest'; |
| 7 | +import { type AdapterConfig, zenstackAdapter } from '../src/adapter'; |
| 8 | +import { generateSchema } from '../src/schema-generator'; |
| 9 | + |
| 10 | +const oauthClientSchema = { |
| 11 | + oauthClient: { |
| 12 | + modelName: 'oauthClient', |
| 13 | + fields: { |
| 14 | + name: { |
| 15 | + type: 'string', |
| 16 | + required: true, |
| 17 | + }, |
| 18 | + scopes: { |
| 19 | + type: 'string[]', |
| 20 | + required: true, |
| 21 | + }, |
| 22 | + retryDelays: { |
| 23 | + type: 'number[]', |
| 24 | + required: false, |
| 25 | + }, |
| 26 | + }, |
| 27 | + }, |
| 28 | +} satisfies BetterAuthDBSchema; |
| 29 | + |
| 30 | +function makeAuthOptions() { |
| 31 | + return { |
| 32 | + plugins: [ |
| 33 | + { |
| 34 | + id: 'oauth-provider', |
| 35 | + schema: oauthClientSchema, |
| 36 | + }, |
| 37 | + ], |
| 38 | + } as unknown as BetterAuthOptions; |
| 39 | +} |
| 40 | + |
| 41 | +function makeDb(captured: { createData?: Record<string, unknown> }) { |
| 42 | + return { |
| 43 | + oauthClient: { |
| 44 | + create: async ({ data }: { data: Record<string, unknown> }) => { |
| 45 | + captured.createData = data; |
| 46 | + return data; |
| 47 | + }, |
| 48 | + }, |
| 49 | + $transaction: async <T>(cb: (tx: unknown) => Promise<T>) => cb(makeDb(captured)), |
| 50 | + }; |
| 51 | +} |
| 52 | + |
| 53 | +async function createOauthClient(config: AdapterConfig) { |
| 54 | + const captured: { createData?: Record<string, unknown> } = {}; |
| 55 | + const adapter = zenstackAdapter(makeDb(captured) as any, config)(makeAuthOptions()); |
| 56 | + |
| 57 | + await adapter.create({ |
| 58 | + model: 'oauthClient', |
| 59 | + data: { |
| 60 | + id: 'client-1', |
| 61 | + name: 'client', |
| 62 | + scopes: ['openid', 'profile'], |
| 63 | + retryDelays: [1, 2], |
| 64 | + }, |
| 65 | + forceAllowId: true, |
| 66 | + }); |
| 67 | + |
| 68 | + return captured.createData; |
| 69 | +} |
| 70 | + |
| 71 | +async function generateOauthClientSchema(config: AdapterConfig) { |
| 72 | + const { name: workDir, removeCallback } = tmp.dirSync({ unsafeCleanup: true }); |
| 73 | + const schemaPath = path.join(workDir, 'schema.zmodel'); |
| 74 | + |
| 75 | + try { |
| 76 | + const result = await generateSchema(schemaPath, oauthClientSchema, config, makeAuthOptions()); |
| 77 | + return result.code; |
| 78 | + } finally { |
| 79 | + if (fs.existsSync(workDir)) { |
| 80 | + removeCallback(); |
| 81 | + } |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +describe('ZenStack Better Auth adapter', () => { |
| 86 | + it('preserves native array inputs for PostgreSQL (#2615)', async () => { |
| 87 | + const data = await createOauthClient({ provider: 'postgresql' }); |
| 88 | + |
| 89 | + expect(data?.scopes).toEqual(['openid', 'profile']); |
| 90 | + expect(data?.retryDelays).toEqual([1, 2]); |
| 91 | + }); |
| 92 | + |
| 93 | + it('serializes array inputs when native arrays are disabled (#2615)', async () => { |
| 94 | + const data = await createOauthClient({ provider: 'postgresql', supportsArrays: false }); |
| 95 | + |
| 96 | + expect(data?.scopes).toBe(JSON.stringify(['openid', 'profile'])); |
| 97 | + expect(data?.retryDelays).toBe(JSON.stringify([1, 2])); |
| 98 | + }); |
| 99 | + |
| 100 | + it('generates native array fields when the adapter supports arrays (#2615)', async () => { |
| 101 | + const schema = await generateOauthClientSchema({ provider: 'postgresql' }); |
| 102 | + |
| 103 | + expect(schema).toMatch(/scopes\s+String\[\]/); |
| 104 | + expect(schema).toMatch(/retryDelays\s+Int\[\]\?/); |
| 105 | + }); |
| 106 | + |
| 107 | + it('generates JSON fields when the adapter does not support arrays (#2615)', async () => { |
| 108 | + const schema = await generateOauthClientSchema({ provider: 'sqlite' }); |
| 109 | + |
| 110 | + expect(schema).toMatch(/scopes\s+Json/); |
| 111 | + expect(schema).toMatch(/retryDelays\s+Json\?/); |
| 112 | + expect(schema).not.toMatch(/scopes\s+String\[\]/); |
| 113 | + expect(schema).not.toMatch(/retryDelays\s+Int\[\]/); |
| 114 | + }); |
| 115 | +}); |
0 commit comments