@forklaunch/testing 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024 forklaunch
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,215 @@
1
+ import { StartedTestContainer } from 'testcontainers';
2
+ import { MikroORM } from '@mikro-orm/core';
3
+ import Redis from 'ioredis';
4
+
5
+ type DatabaseType = 'postgres' | 'postgresql' | 'mysql' | 'mariadb' | 'mongodb' | 'mongo' | 'mssql' | 'libsql' | 'sqlite' | 'better-sqlite';
6
+ interface PostgresConfig {
7
+ user?: string;
8
+ password?: string;
9
+ database?: string;
10
+ command?: string[];
11
+ }
12
+ interface MySQLConfig {
13
+ user?: string;
14
+ password?: string;
15
+ database?: string;
16
+ rootPassword?: string;
17
+ }
18
+ interface MongoDBConfig {
19
+ user?: string;
20
+ password?: string;
21
+ database?: string;
22
+ }
23
+ interface MSSQLConfig {
24
+ user?: string;
25
+ password?: string;
26
+ database?: string;
27
+ saPassword?: string;
28
+ }
29
+ interface SQLiteConfig {
30
+ database?: string;
31
+ }
32
+ interface RedisConfig {
33
+ command?: string[];
34
+ }
35
+ type DatabaseConfig = PostgresConfig | MySQLConfig | MongoDBConfig | MSSQLConfig | SQLiteConfig;
36
+ /**
37
+ * Manages test containers (PostgreSQL, MySQL, MongoDB, Redis, etc.) for E2E testing
38
+ */
39
+ declare class TestContainerManager {
40
+ private containers;
41
+ /**
42
+ * Setup database container based on type
43
+ */
44
+ setupDatabaseContainer(type: DatabaseType, config?: DatabaseConfig): Promise<StartedTestContainer | null>;
45
+ /**
46
+ * Normalize database type aliases
47
+ */
48
+ private normalizeDatabaseType;
49
+ /**
50
+ * Setup PostgreSQL test container
51
+ */
52
+ setupPostgresContainer(config?: PostgresConfig): Promise<StartedTestContainer>;
53
+ /**
54
+ * Setup MySQL test container
55
+ */
56
+ setupMySQLContainer(config?: MySQLConfig): Promise<StartedTestContainer>;
57
+ /**
58
+ * Setup MongoDB test container
59
+ */
60
+ setupMongoDBContainer(config?: MongoDBConfig): Promise<StartedTestContainer>;
61
+ /**
62
+ * Setup Microsoft SQL Server test container
63
+ */
64
+ setupMSSQLContainer(config?: MSSQLConfig): Promise<StartedTestContainer>;
65
+ /**
66
+ * Setup Redis test container
67
+ */
68
+ setupRedisContainer(config?: RedisConfig): Promise<StartedTestContainer>;
69
+ /**
70
+ * Cleanup all containers
71
+ */
72
+ cleanup(): Promise<void>;
73
+ }
74
+
75
+ interface TestEnvConfig {
76
+ database: StartedTestContainer | null;
77
+ databaseType: DatabaseType;
78
+ redis?: StartedTestContainer;
79
+ hmacSecret?: string;
80
+ customVars?: Record<string, string>;
81
+ }
82
+ /**
83
+ * Setup test environment variables for a blueprint test
84
+ */
85
+ declare function setupTestEnvironment(config: TestEnvConfig): void;
86
+
87
+ interface MikroOrmTestConfig {
88
+ /**
89
+ * Path to the MikroORM config file (e.g., '../mikro-orm.config')
90
+ */
91
+ mikroOrmConfigPath: string;
92
+ /**
93
+ * Database type (postgres, mysql, mongodb, etc.)
94
+ */
95
+ databaseType: DatabaseType;
96
+ /**
97
+ * Whether to use migrations (true) or schema generation (false)
98
+ * - true: IAM blueprints (uses getMigrator().up())
99
+ * - false: Billing blueprints (uses getSchemaGenerator().createSchema())
100
+ */
101
+ useMigrations?: boolean;
102
+ /**
103
+ * Path to migrations directory (required if useMigrations is true)
104
+ */
105
+ migrationsPath?: string;
106
+ /**
107
+ * Database container instance (null for file-based databases like SQLite)
108
+ */
109
+ container: StartedTestContainer | null;
110
+ }
111
+ /**
112
+ * Setup MikroORM for testing with proper schema/migrations
113
+ */
114
+ declare function setupTestORM(config: MikroOrmTestConfig): Promise<MikroORM>;
115
+ /**
116
+ * Clear all data from the test database
117
+ */
118
+ declare function clearTestDatabase(orm: MikroORM, redis?: Redis): Promise<void>;
119
+
120
+ interface BlueprintTestConfig {
121
+ /**
122
+ * Path to the MikroORM config file (e.g., '../mikro-orm.config')
123
+ */
124
+ mikroOrmConfigPath: string;
125
+ /**
126
+ * Database type (postgres, mysql, mongodb, etc.)
127
+ * @default 'postgres'
128
+ */
129
+ databaseType?: DatabaseType;
130
+ /**
131
+ * Whether to use migrations (true) or schema generation (false)
132
+ */
133
+ useMigrations?: boolean;
134
+ /**
135
+ * Path to migrations directory (required if useMigrations is true)
136
+ */
137
+ migrationsPath?: string;
138
+ /**
139
+ * Whether the blueprint needs Redis
140
+ */
141
+ needsRedis?: boolean;
142
+ /**
143
+ * Custom environment variables to set
144
+ */
145
+ customEnvVars?: Record<string, string>;
146
+ /**
147
+ * Custom setup hook called after containers and ORM are initialized
148
+ */
149
+ onSetup?: (setup: TestSetupResult) => Promise<void>;
150
+ }
151
+ interface TestSetupResult {
152
+ container: StartedTestContainer | null;
153
+ redisContainer?: StartedTestContainer;
154
+ orm: MikroORM;
155
+ redis?: Redis;
156
+ }
157
+ /**
158
+ * Complete test harness for blueprint E2E testing
159
+ *
160
+ * Handles container setup, environment configuration, and database initialization
161
+ *
162
+ * @example
163
+ * ```typescript
164
+ * const harness = new BlueprintTestHarness({
165
+ * mikroOrmConfigPath: '../mikro-orm.config',
166
+ * useMigrations: false,
167
+ * needsRedis: true,
168
+ * customEnvVars: {
169
+ * STRIPE_API_KEY: 'sk_test_...'
170
+ * }
171
+ * });
172
+ *
173
+ * const setup = await harness.setup();
174
+ * // ... run tests
175
+ * await harness.cleanup();
176
+ * ```
177
+ */
178
+ declare class BlueprintTestHarness {
179
+ private config;
180
+ private containers;
181
+ private result?;
182
+ constructor(config: BlueprintTestConfig);
183
+ /**
184
+ * Setup all test infrastructure (containers, ORM, Redis)
185
+ */
186
+ setup(): Promise<TestSetupResult>;
187
+ /**
188
+ * Cleanup all test infrastructure
189
+ */
190
+ cleanup(): Promise<void>;
191
+ /**
192
+ * Clear all data from the database
193
+ */
194
+ clearDatabase(): Promise<void>;
195
+ }
196
+
197
+ /**
198
+ * Standard mock authentication tokens for testing
199
+ */
200
+ declare const TEST_TOKENS: {
201
+ /**
202
+ * Mock Bearer token for testing
203
+ */
204
+ readonly AUTH: "Bearer test-token";
205
+ /**
206
+ * Mock valid HMAC token for testing
207
+ */
208
+ readonly HMAC: "HMAC keyId=test-key ts=1234567890 nonce=test-nonce signature=test-signature";
209
+ /**
210
+ * Mock invalid HMAC token for testing authentication failures
211
+ */
212
+ readonly HMAC_INVALID: "HMAC keyId=invalid-key ts=1234567890 nonce=invalid-nonce signature=invalid-signature";
213
+ };
214
+
215
+ export { type BlueprintTestConfig, BlueprintTestHarness, type DatabaseConfig, type DatabaseType, type MSSQLConfig, type MikroOrmTestConfig, type MongoDBConfig, type MySQLConfig, type PostgresConfig, type RedisConfig, type SQLiteConfig, TEST_TOKENS, TestContainerManager, type TestEnvConfig, type TestSetupResult, clearTestDatabase, setupTestEnvironment, setupTestORM };
package/lib/index.d.ts ADDED
@@ -0,0 +1,215 @@
1
+ import { StartedTestContainer } from 'testcontainers';
2
+ import { MikroORM } from '@mikro-orm/core';
3
+ import Redis from 'ioredis';
4
+
5
+ type DatabaseType = 'postgres' | 'postgresql' | 'mysql' | 'mariadb' | 'mongodb' | 'mongo' | 'mssql' | 'libsql' | 'sqlite' | 'better-sqlite';
6
+ interface PostgresConfig {
7
+ user?: string;
8
+ password?: string;
9
+ database?: string;
10
+ command?: string[];
11
+ }
12
+ interface MySQLConfig {
13
+ user?: string;
14
+ password?: string;
15
+ database?: string;
16
+ rootPassword?: string;
17
+ }
18
+ interface MongoDBConfig {
19
+ user?: string;
20
+ password?: string;
21
+ database?: string;
22
+ }
23
+ interface MSSQLConfig {
24
+ user?: string;
25
+ password?: string;
26
+ database?: string;
27
+ saPassword?: string;
28
+ }
29
+ interface SQLiteConfig {
30
+ database?: string;
31
+ }
32
+ interface RedisConfig {
33
+ command?: string[];
34
+ }
35
+ type DatabaseConfig = PostgresConfig | MySQLConfig | MongoDBConfig | MSSQLConfig | SQLiteConfig;
36
+ /**
37
+ * Manages test containers (PostgreSQL, MySQL, MongoDB, Redis, etc.) for E2E testing
38
+ */
39
+ declare class TestContainerManager {
40
+ private containers;
41
+ /**
42
+ * Setup database container based on type
43
+ */
44
+ setupDatabaseContainer(type: DatabaseType, config?: DatabaseConfig): Promise<StartedTestContainer | null>;
45
+ /**
46
+ * Normalize database type aliases
47
+ */
48
+ private normalizeDatabaseType;
49
+ /**
50
+ * Setup PostgreSQL test container
51
+ */
52
+ setupPostgresContainer(config?: PostgresConfig): Promise<StartedTestContainer>;
53
+ /**
54
+ * Setup MySQL test container
55
+ */
56
+ setupMySQLContainer(config?: MySQLConfig): Promise<StartedTestContainer>;
57
+ /**
58
+ * Setup MongoDB test container
59
+ */
60
+ setupMongoDBContainer(config?: MongoDBConfig): Promise<StartedTestContainer>;
61
+ /**
62
+ * Setup Microsoft SQL Server test container
63
+ */
64
+ setupMSSQLContainer(config?: MSSQLConfig): Promise<StartedTestContainer>;
65
+ /**
66
+ * Setup Redis test container
67
+ */
68
+ setupRedisContainer(config?: RedisConfig): Promise<StartedTestContainer>;
69
+ /**
70
+ * Cleanup all containers
71
+ */
72
+ cleanup(): Promise<void>;
73
+ }
74
+
75
+ interface TestEnvConfig {
76
+ database: StartedTestContainer | null;
77
+ databaseType: DatabaseType;
78
+ redis?: StartedTestContainer;
79
+ hmacSecret?: string;
80
+ customVars?: Record<string, string>;
81
+ }
82
+ /**
83
+ * Setup test environment variables for a blueprint test
84
+ */
85
+ declare function setupTestEnvironment(config: TestEnvConfig): void;
86
+
87
+ interface MikroOrmTestConfig {
88
+ /**
89
+ * Path to the MikroORM config file (e.g., '../mikro-orm.config')
90
+ */
91
+ mikroOrmConfigPath: string;
92
+ /**
93
+ * Database type (postgres, mysql, mongodb, etc.)
94
+ */
95
+ databaseType: DatabaseType;
96
+ /**
97
+ * Whether to use migrations (true) or schema generation (false)
98
+ * - true: IAM blueprints (uses getMigrator().up())
99
+ * - false: Billing blueprints (uses getSchemaGenerator().createSchema())
100
+ */
101
+ useMigrations?: boolean;
102
+ /**
103
+ * Path to migrations directory (required if useMigrations is true)
104
+ */
105
+ migrationsPath?: string;
106
+ /**
107
+ * Database container instance (null for file-based databases like SQLite)
108
+ */
109
+ container: StartedTestContainer | null;
110
+ }
111
+ /**
112
+ * Setup MikroORM for testing with proper schema/migrations
113
+ */
114
+ declare function setupTestORM(config: MikroOrmTestConfig): Promise<MikroORM>;
115
+ /**
116
+ * Clear all data from the test database
117
+ */
118
+ declare function clearTestDatabase(orm: MikroORM, redis?: Redis): Promise<void>;
119
+
120
+ interface BlueprintTestConfig {
121
+ /**
122
+ * Path to the MikroORM config file (e.g., '../mikro-orm.config')
123
+ */
124
+ mikroOrmConfigPath: string;
125
+ /**
126
+ * Database type (postgres, mysql, mongodb, etc.)
127
+ * @default 'postgres'
128
+ */
129
+ databaseType?: DatabaseType;
130
+ /**
131
+ * Whether to use migrations (true) or schema generation (false)
132
+ */
133
+ useMigrations?: boolean;
134
+ /**
135
+ * Path to migrations directory (required if useMigrations is true)
136
+ */
137
+ migrationsPath?: string;
138
+ /**
139
+ * Whether the blueprint needs Redis
140
+ */
141
+ needsRedis?: boolean;
142
+ /**
143
+ * Custom environment variables to set
144
+ */
145
+ customEnvVars?: Record<string, string>;
146
+ /**
147
+ * Custom setup hook called after containers and ORM are initialized
148
+ */
149
+ onSetup?: (setup: TestSetupResult) => Promise<void>;
150
+ }
151
+ interface TestSetupResult {
152
+ container: StartedTestContainer | null;
153
+ redisContainer?: StartedTestContainer;
154
+ orm: MikroORM;
155
+ redis?: Redis;
156
+ }
157
+ /**
158
+ * Complete test harness for blueprint E2E testing
159
+ *
160
+ * Handles container setup, environment configuration, and database initialization
161
+ *
162
+ * @example
163
+ * ```typescript
164
+ * const harness = new BlueprintTestHarness({
165
+ * mikroOrmConfigPath: '../mikro-orm.config',
166
+ * useMigrations: false,
167
+ * needsRedis: true,
168
+ * customEnvVars: {
169
+ * STRIPE_API_KEY: 'sk_test_...'
170
+ * }
171
+ * });
172
+ *
173
+ * const setup = await harness.setup();
174
+ * // ... run tests
175
+ * await harness.cleanup();
176
+ * ```
177
+ */
178
+ declare class BlueprintTestHarness {
179
+ private config;
180
+ private containers;
181
+ private result?;
182
+ constructor(config: BlueprintTestConfig);
183
+ /**
184
+ * Setup all test infrastructure (containers, ORM, Redis)
185
+ */
186
+ setup(): Promise<TestSetupResult>;
187
+ /**
188
+ * Cleanup all test infrastructure
189
+ */
190
+ cleanup(): Promise<void>;
191
+ /**
192
+ * Clear all data from the database
193
+ */
194
+ clearDatabase(): Promise<void>;
195
+ }
196
+
197
+ /**
198
+ * Standard mock authentication tokens for testing
199
+ */
200
+ declare const TEST_TOKENS: {
201
+ /**
202
+ * Mock Bearer token for testing
203
+ */
204
+ readonly AUTH: "Bearer test-token";
205
+ /**
206
+ * Mock valid HMAC token for testing
207
+ */
208
+ readonly HMAC: "HMAC keyId=test-key ts=1234567890 nonce=test-nonce signature=test-signature";
209
+ /**
210
+ * Mock invalid HMAC token for testing authentication failures
211
+ */
212
+ readonly HMAC_INVALID: "HMAC keyId=invalid-key ts=1234567890 nonce=invalid-nonce signature=invalid-signature";
213
+ };
214
+
215
+ export { type BlueprintTestConfig, BlueprintTestHarness, type DatabaseConfig, type DatabaseType, type MSSQLConfig, type MikroOrmTestConfig, type MongoDBConfig, type MySQLConfig, type PostgresConfig, type RedisConfig, type SQLiteConfig, TEST_TOKENS, TestContainerManager, type TestEnvConfig, type TestSetupResult, clearTestDatabase, setupTestEnvironment, setupTestORM };