@@ -5,6 +5,7 @@ import type { SchemaDef } from '@zenstackhq/orm/schema';
55import { PolicyPlugin } from '@zenstackhq/plugin-policy' ;
66import { PrismaSchemaGenerator } from '@zenstackhq/sdk' ;
77import SQLite from 'better-sqlite3' ;
8+ import { glob } from 'glob' ;
89import { PostgresDialect , SqliteDialect , type LogEvent } from 'kysely' ;
910import { execSync } from 'node:child_process' ;
1011import { createHash } from 'node:crypto' ;
@@ -32,14 +33,55 @@ const TEST_PG_CONFIG = {
3233} ;
3334
3435export type CreateTestClientOptions < Schema extends SchemaDef > = Omit < ClientOptions < Schema > , 'dialect' > & {
36+ /**
37+ * Database provider
38+ */
3539 provider ?: 'sqlite' | 'postgresql' ;
40+
41+ /**
42+ * The main ZModel file. Only used when `usePrismaPush` is true and `schema` is an object.
43+ */
3644 schemaFile ?: string ;
45+
46+ /**
47+ * Database name. If not provided, a name will be generated based on the test name.
48+ */
3749 dbName ?: string ;
50+
51+ /**
52+ * Use `prisma db push` instead of ZenStack's `$pushSchema` for database initialization.
53+ */
3854 usePrismaPush ?: boolean ;
55+
56+ /**
57+ * Extra source files to create and compile.
58+ */
3959 extraSourceFiles ?: Record < string , string > ;
60+
61+ /**
62+ * Working directory for the test client. If not provided, a temporary directory will be created.
63+ */
4064 workDir ?: string ;
65+
66+ /**
67+ * Debug mode.
68+ */
4169 debug ?: boolean ;
70+
71+ /**
72+ * A sqlite database file to be used for the test. Only supported for sqlite provider.
73+ */
4274 dbFile ?: string ;
75+
76+ /**
77+ * PostgreSQL extensions to be added to the datasource. Only supported for postgresql provider.
78+ */
79+ dataSourceExtensions ?: string [ ] ;
80+
81+ /**
82+ * Additional files to be copied to the working directory. The glob pattern is relative to the test file.
83+ */
84+ copyFiles ?: { globPattern : string ; destination : string } [ ] ;
4385} ;
4486
4587export async function createTestClient < Schema extends SchemaDef > (
@@ -95,16 +137,24 @@ export async function createTestClient<Schema extends SchemaDef>(
95137 `datasource db {
96138 provider = '${ provider } '
97139 url = '${ dbUrl } '
140+ ${ options . dataSourceExtensions ? `extensions = [${ options . dataSourceExtensions . join ( ', ' ) } ]` : '' }
98141}` ,
99142 ) ;
100143 }
101- fs . writeFileSync ( path . join ( workDir , 'schema.zmodel' ) , schemaContent ) ;
144+ fs . writeFileSync ( path . join ( workDir ! , 'schema.zmodel' ) , schemaContent ) ;
102145 }
103146 }
104147
105148 invariant ( workDir ) ;
149+
150+ const { plugins, ...rest } = options ?? { } ;
151+ const _options : ClientOptions < Schema > = {
152+ ...rest ,
153+ } as ClientOptions < Schema > ;
154+
106155 if ( options ?. debug ) {
107156 console . log ( `Work directory: ${ workDir } ` ) ;
157+ _options . log = testLogger ;
108158 }
109159
110160 // copy db file to workDir if specified
@@ -115,10 +165,23 @@ export async function createTestClient<Schema extends SchemaDef>(
115165 fs . copyFileSync ( options . dbFile , path . join ( workDir , dbName ) ) ;
116166 }
117167
118- const { plugins, ...rest } = options ?? { } ;
119- const _options : ClientOptions < Schema > = {
120- ...rest ,
121- } as ClientOptions < Schema > ;
168+ // copy additional files if specified
169+ if ( options ?. copyFiles ) {
170+ const state = expect . getState ( ) ;
171+ const currentTestPath = state . testPath ;
172+ if ( ! currentTestPath ) {
173+ throw new Error ( 'Unable to determine current test file path' ) ;
174+ }
175+ for ( const { globPattern, destination } of options . copyFiles ) {
176+ const files = glob . sync ( globPattern , { cwd : path . dirname ( currentTestPath ) } ) ;
177+ for ( const file of files ) {
178+ const src = path . resolve ( path . dirname ( currentTestPath ) , file ) ;
179+ const dest = path . resolve ( workDir , destination , path . basename ( file ) ) ;
180+ fs . mkdirSync ( path . dirname ( dest ) , { recursive : true } ) ;
181+ fs . copyFileSync ( src , dest ) ;
182+ }
183+ }
184+ }
122185
123186 if ( ! options ?. dbFile ) {
124187 if ( options ?. usePrismaPush ) {
0 commit comments