@omnitron-dev/testing 0.1.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.
Files changed (182) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +19 -0
  3. package/dist/async/deferred.d.ts +15 -0
  4. package/dist/async/deferred.js +35 -0
  5. package/dist/async/deferred.js.map +1 -0
  6. package/dist/async/events.d.ts +32 -0
  7. package/dist/async/events.js +176 -0
  8. package/dist/async/events.js.map +1 -0
  9. package/dist/async/index.d.ts +5 -0
  10. package/dist/async/index.js +6 -0
  11. package/dist/async/index.js.map +1 -0
  12. package/dist/async/mock-timer.d.ts +19 -0
  13. package/dist/async/mock-timer.js +87 -0
  14. package/dist/async/mock-timer.js.map +1 -0
  15. package/dist/async/retry.d.ts +8 -0
  16. package/dist/async/retry.js +36 -0
  17. package/dist/async/retry.js.map +1 -0
  18. package/dist/async/wait.d.ts +10 -0
  19. package/dist/async/wait.js +35 -0
  20. package/dist/async/wait.js.map +1 -0
  21. package/dist/docker/database-manager.d.ts +22 -0
  22. package/dist/docker/database-manager.js +97 -0
  23. package/dist/docker/database-manager.js.map +1 -0
  24. package/dist/docker/docker-test-manager.d.ts +44 -0
  25. package/dist/docker/docker-test-manager.js +812 -0
  26. package/dist/docker/docker-test-manager.js.map +1 -0
  27. package/dist/docker/index.d.ts +4 -0
  28. package/dist/docker/index.js +4 -0
  29. package/dist/docker/index.js.map +1 -0
  30. package/dist/docker/redis-manager.d.ts +12 -0
  31. package/dist/docker/redis-manager.js +452 -0
  32. package/dist/docker/redis-manager.js.map +1 -0
  33. package/dist/docker/types.d.ts +103 -0
  34. package/dist/docker/types.js +2 -0
  35. package/dist/docker/types.js.map +1 -0
  36. package/dist/errors.d.ts +15 -0
  37. package/dist/errors.js +28 -0
  38. package/dist/errors.js.map +1 -0
  39. package/dist/helpers/assertions.d.ts +6 -0
  40. package/dist/helpers/assertions.js +37 -0
  41. package/dist/helpers/assertions.js.map +1 -0
  42. package/dist/helpers/console.d.ts +1 -0
  43. package/dist/helpers/console.js +23 -0
  44. package/dist/helpers/console.js.map +1 -0
  45. package/dist/helpers/index.d.ts +3 -0
  46. package/dist/helpers/index.js +4 -0
  47. package/dist/helpers/index.js.map +1 -0
  48. package/dist/helpers/temp-dir.d.ts +2 -0
  49. package/dist/helpers/temp-dir.js +13 -0
  50. package/dist/helpers/temp-dir.js.map +1 -0
  51. package/dist/index.d.ts +18 -0
  52. package/dist/index.js +22 -0
  53. package/dist/index.js.map +1 -0
  54. package/dist/performance/index.d.ts +2 -0
  55. package/dist/performance/index.js +3 -0
  56. package/dist/performance/index.js.map +1 -0
  57. package/dist/performance/memory-detector.d.ts +9 -0
  58. package/dist/performance/memory-detector.js +61 -0
  59. package/dist/performance/memory-detector.js.map +1 -0
  60. package/dist/performance/perf-timer.d.ts +9 -0
  61. package/dist/performance/perf-timer.js +49 -0
  62. package/dist/performance/perf-timer.js.map +1 -0
  63. package/dist/runtime/bun-adapter.d.ts +1 -0
  64. package/dist/runtime/bun-adapter.js +100 -0
  65. package/dist/runtime/bun-adapter.js.map +1 -0
  66. package/dist/runtime/deno-adapter.d.ts +27 -0
  67. package/dist/runtime/deno-adapter.js +221 -0
  68. package/dist/runtime/deno-adapter.js.map +1 -0
  69. package/dist/runtime/node-adapter.d.ts +16 -0
  70. package/dist/runtime/node-adapter.js +30 -0
  71. package/dist/runtime/node-adapter.js.map +1 -0
  72. package/dist/runtime/test-adapter.d.ts +22 -0
  73. package/dist/runtime/test-adapter.js +93 -0
  74. package/dist/runtime/test-adapter.js.map +1 -0
  75. package/dist/titan/container-utils.d.ts +20 -0
  76. package/dist/titan/container-utils.js +78 -0
  77. package/dist/titan/container-utils.js.map +1 -0
  78. package/dist/titan/database/database-testing.module.d.ts +82 -0
  79. package/dist/titan/database/database-testing.module.js +433 -0
  80. package/dist/titan/database/database-testing.module.js.map +1 -0
  81. package/dist/titan/database/index.d.ts +3 -0
  82. package/dist/titan/database/index.js +4 -0
  83. package/dist/titan/database/index.js.map +1 -0
  84. package/dist/titan/database/test-utilities.d.ts +31 -0
  85. package/dist/titan/database/test-utilities.js +146 -0
  86. package/dist/titan/database/test-utilities.js.map +1 -0
  87. package/dist/titan/index.d.ts +14 -0
  88. package/dist/titan/index.js +10 -0
  89. package/dist/titan/index.js.map +1 -0
  90. package/dist/titan/nexus/index.d.ts +3 -0
  91. package/dist/titan/nexus/index.js +4 -0
  92. package/dist/titan/nexus/index.js.map +1 -0
  93. package/dist/titan/nexus/mock-provider.d.ts +41 -0
  94. package/dist/titan/nexus/mock-provider.js +127 -0
  95. package/dist/titan/nexus/mock-provider.js.map +1 -0
  96. package/dist/titan/nexus/test-container.d.ts +120 -0
  97. package/dist/titan/nexus/test-container.js +559 -0
  98. package/dist/titan/nexus/test-container.js.map +1 -0
  99. package/dist/titan/nexus/test-utilities.d.ts +135 -0
  100. package/dist/titan/nexus/test-utilities.js +484 -0
  101. package/dist/titan/nexus/test-utilities.js.map +1 -0
  102. package/dist/titan/pm/index.d.ts +4 -0
  103. package/dist/titan/pm/index.js +4 -0
  104. package/dist/titan/pm/index.js.map +1 -0
  105. package/dist/titan/pm/mock-process-spawner.d.ts +61 -0
  106. package/dist/titan/pm/mock-process-spawner.js +343 -0
  107. package/dist/titan/pm/mock-process-spawner.js.map +1 -0
  108. package/dist/titan/pm/mock-spawner.d.ts +28 -0
  109. package/dist/titan/pm/mock-spawner.js +101 -0
  110. package/dist/titan/pm/mock-spawner.js.map +1 -0
  111. package/dist/titan/pm/test-process-manager.d.ts +49 -0
  112. package/dist/titan/pm/test-process-manager.js +192 -0
  113. package/dist/titan/pm/test-process-manager.js.map +1 -0
  114. package/dist/titan/redis-test-utils.d.ts +33 -0
  115. package/dist/titan/redis-test-utils.js +415 -0
  116. package/dist/titan/redis-test-utils.js.map +1 -0
  117. package/dist/titan/test-application.d.ts +21 -0
  118. package/dist/titan/test-application.js +90 -0
  119. package/dist/titan/test-application.js.map +1 -0
  120. package/dist/titan/test-fixtures.d.ts +97 -0
  121. package/dist/titan/test-fixtures.js +223 -0
  122. package/dist/titan/test-fixtures.js.map +1 -0
  123. package/dist/titan/test-mocks.d.ts +30 -0
  124. package/dist/titan/test-mocks.js +262 -0
  125. package/dist/titan/test-mocks.js.map +1 -0
  126. package/dist/titan/test-module.d.ts +47 -0
  127. package/dist/titan/test-module.js +120 -0
  128. package/dist/titan/test-module.js.map +1 -0
  129. package/package.json +146 -0
  130. package/runtime/bun.d.ts +2 -0
  131. package/runtime/bun.js +5 -0
  132. package/runtime/bun.ts +7 -0
  133. package/runtime/deno.d.ts +2 -0
  134. package/runtime/deno.js +4 -0
  135. package/runtime/vitest.d.ts +2 -0
  136. package/runtime/vitest.js +16 -0
  137. package/runtime/vitest.ts +7 -0
  138. package/src/async/README.md +336 -0
  139. package/src/async/deferred.ts +98 -0
  140. package/src/async/events.ts +386 -0
  141. package/src/async/index.ts +23 -0
  142. package/src/async/mock-timer.ts +220 -0
  143. package/src/async/retry.ts +115 -0
  144. package/src/async/wait.ts +148 -0
  145. package/src/docker/database-manager.ts +242 -0
  146. package/src/docker/docker-test-manager.ts +1198 -0
  147. package/src/docker/index.ts +48 -0
  148. package/src/docker/redis-manager.ts +741 -0
  149. package/src/docker/types.ts +216 -0
  150. package/src/errors.ts +38 -0
  151. package/src/globals.d.ts +24 -0
  152. package/src/helpers/assertions.ts +168 -0
  153. package/src/helpers/console.ts +74 -0
  154. package/src/helpers/index.ts +15 -0
  155. package/src/helpers/temp-dir.ts +50 -0
  156. package/src/index.ts +43 -0
  157. package/src/performance/index.ts +11 -0
  158. package/src/performance/memory-detector.ts +166 -0
  159. package/src/performance/perf-timer.ts +146 -0
  160. package/src/runtime/bun-adapter.ts +127 -0
  161. package/src/runtime/deno-adapter.ts +286 -0
  162. package/src/runtime/node-adapter.ts +40 -0
  163. package/src/runtime/test-adapter.test.ts +98 -0
  164. package/src/runtime/test-adapter.ts +122 -0
  165. package/src/titan/container-utils.ts +128 -0
  166. package/src/titan/database/database-testing.module.ts +851 -0
  167. package/src/titan/database/index.ts +30 -0
  168. package/src/titan/database/test-utilities.ts +340 -0
  169. package/src/titan/index.ts +128 -0
  170. package/src/titan/nexus/index.ts +3 -0
  171. package/src/titan/nexus/mock-provider.ts +189 -0
  172. package/src/titan/nexus/test-container.ts +895 -0
  173. package/src/titan/nexus/test-utilities.ts +797 -0
  174. package/src/titan/pm/index.ts +22 -0
  175. package/src/titan/pm/mock-process-spawner.ts +534 -0
  176. package/src/titan/pm/mock-spawner.ts +162 -0
  177. package/src/titan/pm/test-process-manager.ts +352 -0
  178. package/src/titan/redis-test-utils.ts +567 -0
  179. package/src/titan/test-application.ts +154 -0
  180. package/src/titan/test-fixtures.ts +304 -0
  181. package/src/titan/test-mocks.ts +336 -0
  182. package/src/titan/test-module.ts +233 -0
@@ -0,0 +1,115 @@
1
+ /**
2
+ * Retry and timeout utilities for async testing
3
+ */
4
+
5
+ import { TimeoutError } from '../errors.js';
6
+
7
+ /**
8
+ * Options for retry behavior
9
+ */
10
+ export interface RetryOptions {
11
+ /**
12
+ * Maximum number of retry attempts (default: 3)
13
+ */
14
+ retries?: number;
15
+
16
+ /**
17
+ * Initial delay between retries in milliseconds (default: 100)
18
+ */
19
+ delay?: number;
20
+
21
+ /**
22
+ * Backoff multiplier for exponential backoff (default: 2)
23
+ */
24
+ backoff?: number;
25
+
26
+ /**
27
+ * Callback invoked on each retry attempt
28
+ */
29
+ onRetry?: (error: Error, attempt: number) => void;
30
+ }
31
+
32
+ /**
33
+ * Retry an async operation with exponential backoff
34
+ *
35
+ * Useful for testing operations that may temporarily fail,
36
+ * such as network requests or resource availability checks.
37
+ *
38
+ * @param fn - Async function to retry
39
+ * @param options - Retry configuration options
40
+ * @returns Promise resolving to the function's result
41
+ * @throws Last error if all retries are exhausted
42
+ *
43
+ * @example
44
+ * ```ts
45
+ * const result = await retry(
46
+ * async () => fetchData(),
47
+ * { retries: 5, delay: 200, backoff: 2 }
48
+ * );
49
+ * ```
50
+ */
51
+ export async function retry<T>(fn: () => Promise<T>, options: RetryOptions = {}): Promise<T> {
52
+ const { retries = 3, delay = 100, backoff = 2, onRetry } = options;
53
+
54
+ let lastError: Error;
55
+
56
+ for (let i = 0; i < retries; i++) {
57
+ try {
58
+ return await fn();
59
+ } catch (error) {
60
+ lastError = error as Error;
61
+
62
+ if (i < retries - 1) {
63
+ if (onRetry) {
64
+ onRetry(lastError, i + 1);
65
+ }
66
+
67
+ const waitTime = delay * Math.pow(backoff, i);
68
+ await new Promise((resolve) => setTimeout(resolve, waitTime));
69
+ }
70
+ }
71
+ }
72
+
73
+ throw lastError!;
74
+ }
75
+
76
+ /**
77
+ * Wait for an async operation with timeout
78
+ *
79
+ * Races the provided promise against a timeout, throwing a TimeoutError
80
+ * if the timeout is reached first.
81
+ *
82
+ * @param promise - Promise to race against timeout
83
+ * @param timeout - Timeout in milliseconds (default: 5000)
84
+ * @param errorMessage - Custom error message for timeout
85
+ * @returns Promise resolving to the original promise's result
86
+ * @throws TimeoutError if timeout is reached
87
+ *
88
+ * @example
89
+ * ```ts
90
+ * const result = await withTimeout(
91
+ * fetchData(),
92
+ * 3000,
93
+ * 'Data fetch timed out'
94
+ * );
95
+ * ```
96
+ */
97
+ export async function withTimeout<T>(
98
+ promise: Promise<T>,
99
+ timeout = 5000,
100
+ errorMessage = 'Operation timed out'
101
+ ): Promise<T> {
102
+ let timeoutId: NodeJS.Timeout;
103
+
104
+ const timeoutPromise = new Promise<never>((_, reject) => {
105
+ timeoutId = setTimeout(() => {
106
+ reject(new TimeoutError(errorMessage, timeout));
107
+ }, timeout);
108
+ });
109
+
110
+ try {
111
+ return await Promise.race([promise, timeoutPromise]);
112
+ } finally {
113
+ clearTimeout(timeoutId!);
114
+ }
115
+ }
@@ -0,0 +1,148 @@
1
+ /**
2
+ * Wait utilities for async testing
3
+ */
4
+
5
+ import { TimeoutError } from '../errors.js';
6
+
7
+ /**
8
+ * Options for wait operations
9
+ */
10
+ export interface WaitOptions {
11
+ /**
12
+ * Timeout in milliseconds (default: 5000)
13
+ */
14
+ timeout?: number;
15
+
16
+ /**
17
+ * Polling interval in milliseconds (default: 50)
18
+ */
19
+ interval?: number;
20
+
21
+ /**
22
+ * Custom error message
23
+ */
24
+ message?: string;
25
+ }
26
+
27
+ /**
28
+ * Wait for a condition to be true
29
+ *
30
+ * Polls the condition function at regular intervals until it returns true
31
+ * or the timeout is reached.
32
+ *
33
+ * @param condition - Function that returns true when condition is met
34
+ * @param options - Wait configuration options
35
+ * @throws TimeoutError if timeout is reached before condition is met
36
+ *
37
+ * @example
38
+ * ```ts
39
+ * await waitFor(
40
+ * () => element.isVisible(),
41
+ * { timeout: 3000, interval: 100 }
42
+ * );
43
+ * ```
44
+ */
45
+ export async function waitFor(condition: () => boolean | Promise<boolean>, options: WaitOptions = {}): Promise<void> {
46
+ const { timeout = 5000, interval = 50, message = 'Condition not met' } = options;
47
+ const start = Date.now();
48
+
49
+ while (Date.now() - start < timeout) {
50
+ const result = await condition();
51
+ if (result) {
52
+ return;
53
+ }
54
+ await delay(interval);
55
+ }
56
+
57
+ throw new TimeoutError('waitFor: ' + message, timeout);
58
+ }
59
+
60
+ /**
61
+ * Wait for a condition to be true with timeout (alternative naming)
62
+ *
63
+ * This is an alias for waitFor with slightly different parameter signature
64
+ * for backward compatibility.
65
+ *
66
+ * @param condition - Function that returns true when condition is met
67
+ * @param timeout - Timeout in milliseconds (default: 5000)
68
+ * @param interval - Polling interval in milliseconds (default: 50)
69
+ * @throws TimeoutError if timeout is reached before condition is met
70
+ *
71
+ * @example
72
+ * ```ts
73
+ * await waitForCondition(
74
+ * () => counter > 10,
75
+ * 5000,
76
+ * 100
77
+ * );
78
+ * ```
79
+ */
80
+ export async function waitForCondition(
81
+ condition: () => boolean | Promise<boolean>,
82
+ timeout = 5000,
83
+ interval = 50
84
+ ): Promise<void> {
85
+ const start = Date.now();
86
+
87
+ while (Date.now() - start < timeout) {
88
+ const result = await condition();
89
+ if (result) {
90
+ return;
91
+ }
92
+ await new Promise((resolve) => setTimeout(resolve, interval));
93
+ }
94
+
95
+ throw new TimeoutError('waitForCondition', timeout);
96
+ }
97
+
98
+ /**
99
+ * Delay execution for specified milliseconds
100
+ *
101
+ * Simple async sleep utility for introducing delays in tests.
102
+ *
103
+ * @param ms - Milliseconds to delay
104
+ *
105
+ * @example
106
+ * ```ts
107
+ * await delay(1000); // Wait 1 second
108
+ * ```
109
+ */
110
+ export function delay(ms: number): Promise<void> {
111
+ return new Promise((resolve) => setTimeout(resolve, ms));
112
+ }
113
+
114
+ /**
115
+ * Wait for next tick in the event loop
116
+ *
117
+ * Useful for allowing pending async operations to complete
118
+ * before proceeding with test assertions.
119
+ *
120
+ * @example
121
+ * ```ts
122
+ * emitter.emit('event');
123
+ * await nextTick(); // Let handlers execute
124
+ * expect(handlerCalled).toBe(true);
125
+ * ```
126
+ */
127
+ export function nextTick(): Promise<void> {
128
+ return new Promise((resolve) => setImmediate(resolve));
129
+ }
130
+
131
+ /**
132
+ * Flush all pending promises in the event loop
133
+ *
134
+ * Ensures all microtasks and next-tick callbacks are executed
135
+ * before proceeding. Useful for testing promise chains and
136
+ * async operations.
137
+ *
138
+ * @example
139
+ * ```ts
140
+ * Promise.resolve().then(() => { executed = true });
141
+ * await flushPromises();
142
+ * expect(executed).toBe(true);
143
+ * ```
144
+ */
145
+ export async function flushPromises(): Promise<void> {
146
+ await new Promise((resolve) => setImmediate(resolve));
147
+ await new Promise((resolve) => process.nextTick(resolve));
148
+ }
@@ -0,0 +1,242 @@
1
+ /**
2
+ * Database Test Manager
3
+ *
4
+ * Provides specialized helpers for creating and managing database containers
5
+ * in tests. Supports PostgreSQL and MySQL with sensible defaults.
6
+ */
7
+
8
+ import { DockerTestManager } from './docker-test-manager.js';
9
+ import type { DockerContainer } from './types.js';
10
+
11
+ /**
12
+ * Database Test Manager
13
+ *
14
+ * Provides convenient methods for creating PostgreSQL and MySQL containers
15
+ * with proper health checks and connection strings.
16
+ *
17
+ * @example
18
+ * ```typescript
19
+ * // Create a PostgreSQL container
20
+ * const pg = await DatabaseTestManager.createPostgresContainer({
21
+ * database: 'mydb',
22
+ * user: 'myuser',
23
+ * password: 'mypass',
24
+ * });
25
+ *
26
+ * // Use with helper
27
+ * await DatabaseTestManager.withPostgres(async (container, connStr) => {
28
+ * // Run tests with database
29
+ * });
30
+ * ```
31
+ */
32
+ export class DatabaseTestManager {
33
+ private static _dockerManager: DockerTestManager | null = null;
34
+
35
+ /**
36
+ * Get the Docker manager instance
37
+ * @private
38
+ */
39
+ private static getDockerManager(): DockerTestManager {
40
+ if (!DatabaseTestManager._dockerManager) {
41
+ DatabaseTestManager._dockerManager = DockerTestManager.getInstance();
42
+ }
43
+ return DatabaseTestManager._dockerManager;
44
+ }
45
+
46
+ /**
47
+ * Create a PostgreSQL container
48
+ *
49
+ * @param options - PostgreSQL configuration options
50
+ * @returns Promise resolving to DockerContainer
51
+ *
52
+ * @example
53
+ * ```typescript
54
+ * const container = await DatabaseTestManager.createPostgresContainer({
55
+ * database: 'testdb',
56
+ * user: 'testuser',
57
+ * password: 'testpass',
58
+ * });
59
+ *
60
+ * const port = container.ports.get(5432)!;
61
+ * const connStr = `postgresql://testuser:testpass@localhost:${port}/testdb`;
62
+ * ```
63
+ */
64
+ static async createPostgresContainer(options?: {
65
+ /** Container name (auto-generated if not provided) */
66
+ name?: string;
67
+ /** Host port or 'auto' for dynamic allocation (default: 'auto') */
68
+ port?: number | 'auto';
69
+ /** Database name (default: 'testdb') */
70
+ database?: string;
71
+ /** Database user (default: 'testuser') */
72
+ user?: string;
73
+ /** Database password (default: 'testpass') */
74
+ password?: string;
75
+ }): Promise<DockerContainer> {
76
+ const port = options?.port || 'auto';
77
+ const database = options?.database || 'testdb';
78
+ const user = options?.user || 'testuser';
79
+ const password = options?.password || 'testpass';
80
+
81
+ return DatabaseTestManager.getDockerManager().createContainer({
82
+ name: options?.name,
83
+ image: 'postgres:16-alpine',
84
+ ports: { 5432: port },
85
+ environment: {
86
+ POSTGRES_DB: database,
87
+ POSTGRES_USER: user,
88
+ POSTGRES_PASSWORD: password,
89
+ POSTGRES_HOST_AUTH_METHOD: 'trust',
90
+ },
91
+ healthcheck: {
92
+ test: ['CMD-SHELL', `pg_isready -U ${user}`],
93
+ interval: '1s',
94
+ timeout: '5s',
95
+ retries: 30,
96
+ startPeriod: '5s',
97
+ },
98
+ waitFor: {
99
+ healthcheck: true,
100
+ timeout: 45000,
101
+ },
102
+ });
103
+ }
104
+
105
+ /**
106
+ * Create a MySQL container
107
+ *
108
+ * @param options - MySQL configuration options
109
+ * @returns Promise resolving to DockerContainer
110
+ *
111
+ * @example
112
+ * ```typescript
113
+ * const container = await DatabaseTestManager.createMySQLContainer({
114
+ * database: 'testdb',
115
+ * user: 'testuser',
116
+ * password: 'testpass',
117
+ * });
118
+ *
119
+ * const port = container.ports.get(3306)!;
120
+ * const connStr = `mysql://testuser:testpass@localhost:${port}/testdb`;
121
+ * ```
122
+ */
123
+ static async createMySQLContainer(options?: {
124
+ /** Container name (auto-generated if not provided) */
125
+ name?: string;
126
+ /** Host port or 'auto' for dynamic allocation (default: 'auto') */
127
+ port?: number | 'auto';
128
+ /** Database name (default: 'testdb') */
129
+ database?: string;
130
+ /** Database user (default: 'testuser') */
131
+ user?: string;
132
+ /** Database password (default: 'testpass') */
133
+ password?: string;
134
+ /** Root password (default: 'rootpass') */
135
+ rootPassword?: string;
136
+ }): Promise<DockerContainer> {
137
+ const port = options?.port || 'auto';
138
+ const database = options?.database || 'testdb';
139
+ const user = options?.user || 'testuser';
140
+ const password = options?.password || 'testpass';
141
+ const rootPassword = options?.rootPassword || 'rootpass';
142
+
143
+ return DatabaseTestManager.getDockerManager().createContainer({
144
+ name: options?.name,
145
+ image: 'mysql:8.0',
146
+ ports: { 3306: port },
147
+ environment: {
148
+ MYSQL_DATABASE: database,
149
+ MYSQL_USER: user,
150
+ MYSQL_PASSWORD: password,
151
+ MYSQL_ROOT_PASSWORD: rootPassword,
152
+ MYSQL_ALLOW_EMPTY_PASSWORD: 'yes',
153
+ },
154
+ healthcheck: {
155
+ test: ['CMD', 'mysqladmin', 'ping', '-h', 'localhost'],
156
+ interval: '2s',
157
+ timeout: '10s',
158
+ retries: 25,
159
+ startPeriod: '20s',
160
+ },
161
+ waitFor: {
162
+ healthcheck: true,
163
+ timeout: 90000, // MySQL 8.0 can take longer to initialize
164
+ },
165
+ });
166
+ }
167
+
168
+ /**
169
+ * Run tests with a PostgreSQL container
170
+ *
171
+ * Creates a PostgreSQL container, runs the test function, and cleans up.
172
+ *
173
+ * @param testFn - Test function that receives container and connection string
174
+ * @param options - PostgreSQL configuration options
175
+ * @returns Promise resolving to test function result
176
+ *
177
+ * @example
178
+ * ```typescript
179
+ * await DatabaseTestManager.withPostgres(async (container, connectionString) => {
180
+ * const client = new Client({ connectionString });
181
+ * await client.connect();
182
+ * // Run tests...
183
+ * await client.end();
184
+ * });
185
+ * ```
186
+ */
187
+ static async withPostgres<T>(
188
+ testFn: (container: DockerContainer, connectionString: string) => Promise<T>,
189
+ options?: Parameters<typeof DatabaseTestManager.createPostgresContainer>[0]
190
+ ): Promise<T> {
191
+ const container = await DatabaseTestManager.createPostgresContainer(options);
192
+
193
+ const port = container.ports.get(5432)!;
194
+ const database = options?.database || 'testdb';
195
+ const user = options?.user || 'testuser';
196
+ const password = options?.password || 'testpass';
197
+ const connectionString = `postgresql://${user}:${password}@localhost:${port}/${database}`;
198
+
199
+ try {
200
+ return await testFn(container, connectionString);
201
+ } finally {
202
+ await container.cleanup();
203
+ }
204
+ }
205
+
206
+ /**
207
+ * Run tests with a MySQL container
208
+ *
209
+ * Creates a MySQL container, runs the test function, and cleans up.
210
+ *
211
+ * @param testFn - Test function that receives container and connection string
212
+ * @param options - MySQL configuration options
213
+ * @returns Promise resolving to test function result
214
+ *
215
+ * @example
216
+ * ```typescript
217
+ * await DatabaseTestManager.withMySQL(async (container, connectionString) => {
218
+ * const connection = await mysql.createConnection(connectionString);
219
+ * // Run tests...
220
+ * await connection.end();
221
+ * });
222
+ * ```
223
+ */
224
+ static async withMySQL<T>(
225
+ testFn: (container: DockerContainer, connectionString: string) => Promise<T>,
226
+ options?: Parameters<typeof DatabaseTestManager.createMySQLContainer>[0]
227
+ ): Promise<T> {
228
+ const container = await DatabaseTestManager.createMySQLContainer(options);
229
+
230
+ const port = container.ports.get(3306)!;
231
+ const database = options?.database || 'testdb';
232
+ const user = options?.user || 'testuser';
233
+ const password = options?.password || 'testpass';
234
+ const connectionString = `mysql://${user}:${password}@localhost:${port}/${database}`;
235
+
236
+ try {
237
+ return await testFn(container, connectionString);
238
+ } finally {
239
+ await container.cleanup();
240
+ }
241
+ }
242
+ }