@grest-ts/testkit 0.0.5

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 (200) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +413 -0
  3. package/dist/src/GGBundleTest.d.ts +8 -0
  4. package/dist/src/GGBundleTest.d.ts.map +1 -0
  5. package/dist/src/GGBundleTest.js +75 -0
  6. package/dist/src/GGBundleTest.js.map +1 -0
  7. package/dist/src/GGTest.d.ts +131 -0
  8. package/dist/src/GGTest.d.ts.map +1 -0
  9. package/dist/src/GGTest.js +245 -0
  10. package/dist/src/GGTest.js.map +1 -0
  11. package/dist/src/GGTestContext.d.ts +36 -0
  12. package/dist/src/GGTestContext.d.ts.map +1 -0
  13. package/dist/src/GGTestContext.js +63 -0
  14. package/dist/src/GGTestContext.js.map +1 -0
  15. package/dist/src/GGTestRunner.d.ts +108 -0
  16. package/dist/src/GGTestRunner.d.ts.map +1 -0
  17. package/dist/src/GGTestRunner.js +242 -0
  18. package/dist/src/GGTestRunner.js.map +1 -0
  19. package/dist/src/GGTestRuntime.d.ts +103 -0
  20. package/dist/src/GGTestRuntime.d.ts.map +1 -0
  21. package/dist/src/GGTestRuntime.js +219 -0
  22. package/dist/src/GGTestRuntime.js.map +1 -0
  23. package/dist/src/GGTestRuntimeWorker.d.ts +41 -0
  24. package/dist/src/GGTestRuntimeWorker.d.ts.map +1 -0
  25. package/dist/src/GGTestRuntimeWorker.js +136 -0
  26. package/dist/src/GGTestRuntimeWorker.js.map +1 -0
  27. package/dist/src/GGTestSharedRef.d.ts +35 -0
  28. package/dist/src/GGTestSharedRef.d.ts.map +1 -0
  29. package/dist/src/GGTestSharedRef.js +126 -0
  30. package/dist/src/GGTestSharedRef.js.map +1 -0
  31. package/dist/src/GGTestkitExtensionsDiscovery.d.ts +21 -0
  32. package/dist/src/GGTestkitExtensionsDiscovery.d.ts.map +1 -0
  33. package/dist/src/GGTestkitExtensionsDiscovery.js +24 -0
  34. package/dist/src/GGTestkitExtensionsDiscovery.js.map +1 -0
  35. package/dist/src/IGGLocalDiscoveryServer.d.ts +16 -0
  36. package/dist/src/IGGLocalDiscoveryServer.d.ts.map +1 -0
  37. package/dist/src/IGGLocalDiscoveryServer.js +2 -0
  38. package/dist/src/IGGLocalDiscoveryServer.js.map +1 -0
  39. package/dist/src/callOn/GGCallOnSelector.d.ts +42 -0
  40. package/dist/src/callOn/GGCallOnSelector.d.ts.map +1 -0
  41. package/dist/src/callOn/GGCallOnSelector.js +35 -0
  42. package/dist/src/callOn/GGCallOnSelector.js.map +1 -0
  43. package/dist/src/callOn/GGContractClass.implement.d.ts +8 -0
  44. package/dist/src/callOn/GGContractClass.implement.d.ts.map +1 -0
  45. package/dist/src/callOn/GGContractClass.implement.js +31 -0
  46. package/dist/src/callOn/GGContractClass.implement.js.map +1 -0
  47. package/dist/src/callOn/GGTestActionForLocatorOnCall.d.ts +28 -0
  48. package/dist/src/callOn/GGTestActionForLocatorOnCall.d.ts.map +1 -0
  49. package/dist/src/callOn/GGTestActionForLocatorOnCall.js +118 -0
  50. package/dist/src/callOn/GGTestActionForLocatorOnCall.js.map +1 -0
  51. package/dist/src/callOn/TestableIPC.d.ts +72 -0
  52. package/dist/src/callOn/TestableIPC.d.ts.map +1 -0
  53. package/dist/src/callOn/TestableIPC.js +34 -0
  54. package/dist/src/callOn/TestableIPC.js.map +1 -0
  55. package/dist/src/callOn/callOn.d.ts +113 -0
  56. package/dist/src/callOn/callOn.d.ts.map +1 -0
  57. package/dist/src/callOn/callOn.js +122 -0
  58. package/dist/src/callOn/callOn.js.map +1 -0
  59. package/dist/src/callOn/registerOnCallHandler.d.ts +13 -0
  60. package/dist/src/callOn/registerOnCallHandler.d.ts.map +1 -0
  61. package/dist/src/callOn/registerOnCallHandler.js +111 -0
  62. package/dist/src/callOn/registerOnCallHandler.js.map +1 -0
  63. package/dist/src/index-node.d.ts +35 -0
  64. package/dist/src/index-node.d.ts.map +1 -0
  65. package/dist/src/index-node.js +50 -0
  66. package/dist/src/index-node.js.map +1 -0
  67. package/dist/src/mockable/GGMockable.d.ts +19 -0
  68. package/dist/src/mockable/GGMockable.d.ts.map +1 -0
  69. package/dist/src/mockable/GGMockable.js +2 -0
  70. package/dist/src/mockable/GGMockable.js.map +1 -0
  71. package/dist/src/mockable/GGMockableCall.d.ts +2 -0
  72. package/dist/src/mockable/GGMockableCall.d.ts.map +1 -0
  73. package/dist/src/mockable/GGMockableCall.js +41 -0
  74. package/dist/src/mockable/GGMockableCall.js.map +1 -0
  75. package/dist/src/mockable/GGMockableIPC.d.ts +17 -0
  76. package/dist/src/mockable/GGMockableIPC.d.ts.map +1 -0
  77. package/dist/src/mockable/GGMockableIPC.js +8 -0
  78. package/dist/src/mockable/GGMockableIPC.js.map +1 -0
  79. package/dist/src/mockable/GGMockableInterceptor.d.ts +24 -0
  80. package/dist/src/mockable/GGMockableInterceptor.d.ts.map +1 -0
  81. package/dist/src/mockable/GGMockableInterceptor.js +32 -0
  82. package/dist/src/mockable/GGMockableInterceptor.js.map +1 -0
  83. package/dist/src/mockable/GGMockableInterceptorsServer.d.ts +12 -0
  84. package/dist/src/mockable/GGMockableInterceptorsServer.d.ts.map +1 -0
  85. package/dist/src/mockable/GGMockableInterceptorsServer.js +55 -0
  86. package/dist/src/mockable/GGMockableInterceptorsServer.js.map +1 -0
  87. package/dist/src/mockable/mockable.d.ts +46 -0
  88. package/dist/src/mockable/mockable.d.ts.map +1 -0
  89. package/dist/src/mockable/mockable.js +47 -0
  90. package/dist/src/mockable/mockable.js.map +1 -0
  91. package/dist/src/runner/InlineRunner.d.ts +12 -0
  92. package/dist/src/runner/InlineRunner.d.ts.map +1 -0
  93. package/dist/src/runner/InlineRunner.js +42 -0
  94. package/dist/src/runner/InlineRunner.js.map +1 -0
  95. package/dist/src/runner/IsolatedRunner.d.ts +17 -0
  96. package/dist/src/runner/IsolatedRunner.d.ts.map +1 -0
  97. package/dist/src/runner/IsolatedRunner.js +155 -0
  98. package/dist/src/runner/IsolatedRunner.js.map +1 -0
  99. package/dist/src/runner/RuntimeRunner.d.ts +14 -0
  100. package/dist/src/runner/RuntimeRunner.d.ts.map +1 -0
  101. package/dist/src/runner/RuntimeRunner.js +2 -0
  102. package/dist/src/runner/RuntimeRunner.js.map +1 -0
  103. package/dist/src/runner/WorkerRunner.d.ts +17 -0
  104. package/dist/src/runner/WorkerRunner.d.ts.map +1 -0
  105. package/dist/src/runner/WorkerRunner.js +155 -0
  106. package/dist/src/runner/WorkerRunner.js.map +1 -0
  107. package/dist/src/runner/isolated-loader.mjs +91 -0
  108. package/dist/src/runner/worker-loader.mjs +49 -0
  109. package/dist/src/testers/GGCallInterceptor.d.ts +71 -0
  110. package/dist/src/testers/GGCallInterceptor.d.ts.map +1 -0
  111. package/dist/src/testers/GGCallInterceptor.js +170 -0
  112. package/dist/src/testers/GGCallInterceptor.js.map +1 -0
  113. package/dist/src/testers/GGMockWith.d.ts +30 -0
  114. package/dist/src/testers/GGMockWith.d.ts.map +1 -0
  115. package/dist/src/testers/GGMockWith.js +70 -0
  116. package/dist/src/testers/GGMockWith.js.map +1 -0
  117. package/dist/src/testers/GGSpyWith.d.ts +40 -0
  118. package/dist/src/testers/GGSpyWith.d.ts.map +1 -0
  119. package/dist/src/testers/GGSpyWith.js +90 -0
  120. package/dist/src/testers/GGSpyWith.js.map +1 -0
  121. package/dist/src/testers/GGTestAction.d.ts +126 -0
  122. package/dist/src/testers/GGTestAction.d.ts.map +1 -0
  123. package/dist/src/testers/GGTestAction.js +245 -0
  124. package/dist/src/testers/GGTestAction.js.map +1 -0
  125. package/dist/src/testers/GGTestComponent.d.ts +15 -0
  126. package/dist/src/testers/GGTestComponent.d.ts.map +1 -0
  127. package/dist/src/testers/GGTestComponent.js +2 -0
  128. package/dist/src/testers/GGTestComponent.js.map +1 -0
  129. package/dist/src/testers/GGTestSelector.d.ts +54 -0
  130. package/dist/src/testers/GGTestSelector.d.ts.map +1 -0
  131. package/dist/src/testers/GGTestSelector.js +179 -0
  132. package/dist/src/testers/GGTestSelector.js.map +1 -0
  133. package/dist/src/testers/IGGTestInterceptor.d.ts +8 -0
  134. package/dist/src/testers/IGGTestInterceptor.d.ts.map +1 -0
  135. package/dist/src/testers/IGGTestInterceptor.js +2 -0
  136. package/dist/src/testers/IGGTestInterceptor.js.map +1 -0
  137. package/dist/src/testers/IGGTestWith.d.ts +13 -0
  138. package/dist/src/testers/IGGTestWith.d.ts.map +1 -0
  139. package/dist/src/testers/IGGTestWith.js +2 -0
  140. package/dist/src/testers/IGGTestWith.js.map +1 -0
  141. package/dist/src/testers/RuntimeSelector.d.ts +117 -0
  142. package/dist/src/testers/RuntimeSelector.d.ts.map +1 -0
  143. package/dist/src/testers/RuntimeSelector.js +2 -0
  144. package/dist/src/testers/RuntimeSelector.js.map +1 -0
  145. package/dist/src/tsconfig.json +17 -0
  146. package/dist/src/utils/GGExpectations.d.ts +18 -0
  147. package/dist/src/utils/GGExpectations.d.ts.map +1 -0
  148. package/dist/src/utils/GGExpectations.js +59 -0
  149. package/dist/src/utils/GGExpectations.js.map +1 -0
  150. package/dist/src/utils/GGTestError.d.ts +13 -0
  151. package/dist/src/utils/GGTestError.d.ts.map +1 -0
  152. package/dist/src/utils/GGTestError.js +26 -0
  153. package/dist/src/utils/GGTestError.js.map +1 -0
  154. package/dist/src/utils/captureStack.d.ts +9 -0
  155. package/dist/src/utils/captureStack.d.ts.map +1 -0
  156. package/dist/src/utils/captureStack.js +51 -0
  157. package/dist/src/utils/captureStack.js.map +1 -0
  158. package/dist/tsconfig.publish.tsbuildinfo +1 -0
  159. package/package.json +66 -0
  160. package/src/GGBundleTest.ts +89 -0
  161. package/src/GGTest.ts +318 -0
  162. package/src/GGTestContext.ts +74 -0
  163. package/src/GGTestRunner.ts +308 -0
  164. package/src/GGTestRuntime.ts +265 -0
  165. package/src/GGTestRuntimeWorker.ts +159 -0
  166. package/src/GGTestSharedRef.ts +116 -0
  167. package/src/GGTestkitExtensionsDiscovery.ts +26 -0
  168. package/src/IGGLocalDiscoveryServer.ts +16 -0
  169. package/src/callOn/GGCallOnSelector.ts +61 -0
  170. package/src/callOn/GGContractClass.implement.ts +43 -0
  171. package/src/callOn/GGTestActionForLocatorOnCall.ts +134 -0
  172. package/src/callOn/TestableIPC.ts +81 -0
  173. package/src/callOn/callOn.ts +224 -0
  174. package/src/callOn/registerOnCallHandler.ts +123 -0
  175. package/src/index-node.ts +64 -0
  176. package/src/mockable/GGMockable.ts +22 -0
  177. package/src/mockable/GGMockableCall.ts +45 -0
  178. package/src/mockable/GGMockableIPC.ts +20 -0
  179. package/src/mockable/GGMockableInterceptor.ts +44 -0
  180. package/src/mockable/GGMockableInterceptorsServer.ts +69 -0
  181. package/src/mockable/mockable.ts +71 -0
  182. package/src/runner/InlineRunner.ts +47 -0
  183. package/src/runner/IsolatedRunner.ts +179 -0
  184. package/src/runner/RuntimeRunner.ts +15 -0
  185. package/src/runner/WorkerRunner.ts +179 -0
  186. package/src/runner/isolated-loader.mjs +91 -0
  187. package/src/runner/worker-loader.mjs +49 -0
  188. package/src/testers/GGCallInterceptor.ts +224 -0
  189. package/src/testers/GGMockWith.ts +92 -0
  190. package/src/testers/GGSpyWith.ts +115 -0
  191. package/src/testers/GGTestAction.ts +333 -0
  192. package/src/testers/GGTestComponent.ts +16 -0
  193. package/src/testers/GGTestSelector.ts +223 -0
  194. package/src/testers/IGGTestInterceptor.ts +11 -0
  195. package/src/testers/IGGTestWith.ts +15 -0
  196. package/src/testers/RuntimeSelector.ts +151 -0
  197. package/src/tsconfig.json +17 -0
  198. package/src/utils/GGExpectations.ts +78 -0
  199. package/src/utils/GGTestError.ts +37 -0
  200. package/src/utils/captureStack.ts +54 -0
@@ -0,0 +1,308 @@
1
+ import {GGTestRuntime} from "./GGTestRuntime"
2
+ import {GGLog} from "@grest-ts/logger"
3
+ import {IPCClientRequest, IPCServer} from "@grest-ts/ipc";
4
+ import {GGContext} from "@grest-ts/context";
5
+ import {GG_TRACE} from "@grest-ts/trace";
6
+ import {GGLocatorKey} from "@grest-ts/locator";
7
+ import {GGTestComponent, GGTestComponentType} from "./testers/GGTestComponent";
8
+ import {IGGLocalDiscoveryServer} from "./IGGLocalDiscoveryServer";
9
+ import {TestableIPC} from "./callOn/TestableIPC";
10
+
11
+ export const GG_TEST_RUNNER = new GGLocatorKey<GGTestRunner>("GGTestRunner");
12
+
13
+ /**
14
+ * Interface for test lifecycle hooks.
15
+ * Registered via GGTest.registerHook() to run setup/teardown logic.
16
+ */
17
+ export interface GGTestRunnerHook {
18
+ /** Key name for deduplication (e.g., config key name) */
19
+ keyName: string;
20
+ /** Runs in beforeAll - setup logic */
21
+ beforeAll: () => Promise<void>;
22
+ /** Runs in afterAll - cleanup logic */
23
+ afterAll: () => Promise<void>;
24
+ }
25
+
26
+ /**
27
+ * Symbol for resources that support test operations.
28
+ * Resources implement this to expose operations like clone().
29
+ */
30
+ export const GG_TEST_RESOURCE = Symbol('GG_TEST_RESOURCE');
31
+
32
+ /**
33
+ * Type for objects that expose test resource operations.
34
+ * Used by GGTest.with() to get available operations.
35
+ */
36
+ export interface TestResource<T = any> {
37
+ [GG_TEST_RESOURCE]: T;
38
+ }
39
+
40
+ export interface GGTestConfig {
41
+ serviceStartupTimeout: number
42
+ verboseProxy: boolean
43
+ }
44
+
45
+ export class GGTestRunner {
46
+
47
+ /**
48
+ * Unique identifier for this test context.
49
+ * Used for test resource isolation (e.g., creating isolated DB schemas).
50
+ */
51
+ public readonly testId: string
52
+ public readonly ipcServer: IPCServer;
53
+ public readonly discoveryServer: IGGLocalDiscoveryServer
54
+ public readonly config: GGTestConfig = {
55
+ serviceStartupTimeout: 30000,
56
+ verboseProxy: false
57
+ }
58
+ private _started: boolean = false
59
+
60
+ /**
61
+ * Runtimes added in the describe block (before start).
62
+ * These are managed by beforeAll/afterAll.
63
+ */
64
+ private readonly globalRuntimes: GGTestRuntime[] = []
65
+
66
+ /**
67
+ * Runtimes added within test blocks (after start).
68
+ * These are managed by afterEach and cleared after each test.
69
+ */
70
+ private readonly inTestRuntimes: GGTestRuntime[] = []
71
+
72
+ /**
73
+ * Extension instances - extensions are some "describe block level components".
74
+ * Some examples EventsServer, HttpInterceptorsServer, MockableInterceptorsServer etc.
75
+ */
76
+ private readonly extensionInstances = new Map<GGTestComponentType<any>, GGTestComponent>();
77
+
78
+ private readonly hooks: Map<string, GGTestRunnerHook> = new Map()
79
+
80
+ constructor(ipcServer: IPCServer, discoveryServer: IGGLocalDiscoveryServer, userConfig?: Partial<GGTestConfig>) {
81
+ this.testId = "t" + Math.random().toString(36).substring(2, 8);
82
+ this.config = {...this.config, ...userConfig};
83
+ this.ipcServer = ipcServer;
84
+ this.discoveryServer = discoveryServer;
85
+
86
+ // Register IPC handler for key registration from workers
87
+ this.ipcServer.onFrameworkMessage(TestableIPC.server.registerKeys, async (payload) => {
88
+ const runtime = this.getRuntimeById(payload.runtimeId);
89
+ if (runtime) {
90
+ runtime.registerLocatorKeys(payload.keys);
91
+ GGLog.debug(this, `Registered ${payload.keys.length} keys for runtime ${payload.runtimeId}`);
92
+ } else {
93
+ GGLog.warn(this, `Received key registration for unknown runtime: ${payload.runtimeId}`);
94
+ }
95
+ });
96
+ }
97
+
98
+ // -----------------------------------------------
99
+ // Static component factory registry
100
+ // -----------------------------------------------
101
+
102
+ /**
103
+ * Extension factories - extensions are some "describe block level components".
104
+ * Some examples EventsServer, HttpInterceptorsServer, MockableInterceptorsServer etc.
105
+ */
106
+ private static extensionFactories: GGTestComponentType<any>[] = [];
107
+
108
+ /**
109
+ * Register a component type.
110
+ * Components must accept GGTestRunner as their constructor argument.
111
+ */
112
+ public static registerExtension<T extends GGTestComponent>(type: GGTestComponentType<T>): void {
113
+ this.extensionFactories.push(type);
114
+ }
115
+
116
+ // -----------------------------------------------
117
+ // Component registry
118
+ // -----------------------------------------------
119
+
120
+ /**
121
+ * Get a component by type. Creates it lazily if not yet instantiated.
122
+ */
123
+ public getExtensionInstance<T extends GGTestComponent>(type: GGTestComponentType<T>): T {
124
+ if (!this.extensionInstances.has(type)) {
125
+ const instance = new type(this);
126
+ this.extensionInstances.set(type, instance);
127
+ }
128
+ return this.extensionInstances.get(type) as T;
129
+ }
130
+
131
+
132
+ // -----------------------------------------------
133
+ // Instance methods
134
+ // -----------------------------------------------
135
+
136
+ /**
137
+ * Whether the test has started (services are running).
138
+ */
139
+ public get started(): boolean {
140
+ return this._started;
141
+ }
142
+
143
+ /**
144
+ * Register a test lifecycle hook.
145
+ * Hooks run beforeAll (during start) and afterAll (during teardown).
146
+ * Duplicate registrations with the same keyName are skipped.
147
+ */
148
+ public registerHook(hook: GGTestRunnerHook): void {
149
+ if (this.hooks.has(hook.keyName)) {
150
+ GGLog.debug(this, `Hook already registered for ${hook.keyName}, skipping duplicate`);
151
+ return;
152
+ }
153
+ this.hooks.set(hook.keyName, hook);
154
+ }
155
+
156
+ /**
157
+ * Send a command to ALL runtimes.
158
+ * Used by resource hooks (e.g., DB config) that apply globally.
159
+ */
160
+ public async sendCommand<Payload>(type: IPCClientRequest<Payload, any>, payload: Payload): Promise<void> {
161
+ const promises = this.globalRuntimes.map(runtime => runtime.sendCommand(type, payload));
162
+ await Promise.allSettled(promises)
163
+ }
164
+
165
+
166
+ /**
167
+ * Add a runtime to this test runner.
168
+ * Automatically routes to the appropriate list based on lifecycle stage.
169
+ */
170
+ public addRuntime(runtime: GGTestRuntime): void {
171
+ if (this._started) {
172
+ // Added within a test block - managed by afterEach
173
+ this.inTestRuntimes.push(runtime);
174
+ } else {
175
+ // Added in describe block - managed by beforeAll/afterAll
176
+ this.globalRuntimes.push(runtime);
177
+ }
178
+ }
179
+
180
+ /**
181
+ * Get all active runtimes (both global and in-test).
182
+ */
183
+ public getAllRuntimes(): GGTestRuntime[] {
184
+ return [...this.globalRuntimes, ...this.inTestRuntimes];
185
+ }
186
+
187
+ /**
188
+ * Find a runtime by its unique runtimeId.
189
+ */
190
+ public getRuntimeById(runtimeId: string): GGTestRuntime | undefined {
191
+ return this.globalRuntimes.find(r => r.runtimeId === runtimeId)
192
+ ?? this.inTestRuntimes.find(r => r.runtimeId === runtimeId);
193
+ }
194
+
195
+ public async start(): Promise<void> {
196
+ await new GGContext("Test").run(async () => {
197
+ GG_TRACE.init();
198
+ if (this._started) {
199
+ throw new Error("Already started!");
200
+ }
201
+
202
+ // 0. Initialize extension (testkits already loaded by vitest setup)
203
+ for (const type of GGTestRunner.extensionFactories) {
204
+ // Create extension instance, which registers its IPC handlers
205
+ this.extensionInstances.set(type, new type(this));
206
+ }
207
+
208
+ // 1. Execute all hook beforeAll handlers in parallel
209
+ const hookPromises = Array.from(this.hooks.entries()).map(async ([keyName, hook]) => {
210
+ const startTime = performance.now();
211
+ GGLog.debug(this, `Running beforeAll hook: ${keyName}`);
212
+ await hook.beforeAll();
213
+ const duration = (performance.now() - startTime).toFixed(0);
214
+ GGLog.debug(this, `Completed beforeAll hook: ${keyName} (${duration}ms)`);
215
+ return keyName;
216
+ });
217
+
218
+ const results = await Promise.allSettled(hookPromises);
219
+ const failures = results.filter((r): r is PromiseRejectedResult => r.status === 'rejected');
220
+ if (failures.length > 0) {
221
+ const errorMessages = failures.map(f => {
222
+ const reason = f.reason;
223
+ if (reason instanceof AggregateError) {
224
+ const innerErrors = reason.errors?.map((e: any) => e?.message || String(e)).join(', ');
225
+ return `AggregateError[${innerErrors}]`;
226
+ }
227
+ return reason?.message || String(reason);
228
+ }).join('; ');
229
+ throw new Error(`Failed to run beforeAll hooks: ${errorMessages}`);
230
+ }
231
+
232
+ // 2. Start router
233
+ await this.discoveryServer.start();
234
+
235
+ // 3. Start components
236
+ for (const [type, component] of this.extensionInstances) {
237
+ if (component.start) {
238
+ GGLog.debug(this, `Starting component: ${type.name}`);
239
+ await component.start();
240
+ }
241
+ }
242
+
243
+ // 4. Start runtimes (they will receive commands via env)
244
+ for (const runtime of this.globalRuntimes) {
245
+ await runtime.start()
246
+ }
247
+ this._started = true;
248
+ });
249
+ }
250
+
251
+ public async runAfterEachHooks(): Promise<void> {
252
+ for (const [, component] of this.extensionInstances) {
253
+ await component.afterEach?.();
254
+ }
255
+
256
+ GGLog.debug(this, 'Stopping in-test runtimes...');
257
+ for (const runtime of this.inTestRuntimes) {
258
+ await runtime.shutdown();
259
+ }
260
+ this.inTestRuntimes.length = 0;
261
+ GGLog.debug(this, 'All in-test runtimes stopped!');
262
+ }
263
+
264
+ public async teardown(): Promise<void> {
265
+ await new GGContext("Test").run(async () => {
266
+ GG_TRACE.init();
267
+ // Stop global runtimes (teardown services, keep IPC alive)
268
+ GGLog.debug(this, 'Stopping global runtimes...');
269
+ for (const runtime of this.globalRuntimes) {
270
+ await runtime.stop()
271
+ }
272
+
273
+ // Shutdown global runtimes (terminate workers/processes)
274
+ GGLog.debug(this, 'Shutting down global runtimes...');
275
+ for (const runtime of this.globalRuntimes) {
276
+ await runtime.shutdown()
277
+ }
278
+
279
+ GGLog.debug(this, 'Tearing down router...');
280
+ await this.discoveryServer.teardown();
281
+
282
+ // Teardown components
283
+ for (const [type, component] of this.extensionInstances) {
284
+ if (component.teardown) {
285
+ GGLog.debug(this, `Tearing down component: ${type.name}`);
286
+ await component.teardown();
287
+ }
288
+ }
289
+ this.extensionInstances.clear();
290
+
291
+ GGLog.debug(this, 'Router torn down');
292
+
293
+ // Run afterAll hooks
294
+ GGLog.debug(this, 'Running afterAll hooks...');
295
+ for (const [keyName, hook] of this.hooks) {
296
+ try {
297
+ await hook.afterAll();
298
+ GGLog.debug(this, `Completed afterAll hook: ${keyName}`);
299
+ } catch (error) {
300
+ GGLog.error(this, `Failed afterAll hook ${keyName}:`, error);
301
+ }
302
+ }
303
+
304
+ this.globalRuntimes.length = 0
305
+ this.hooks.clear()
306
+ });
307
+ }
308
+ }
@@ -0,0 +1,265 @@
1
+ import {GGLog} from "@grest-ts/logger";
2
+ import {withTimeout} from "@grest-ts/common";
3
+ import {GGContext} from "@grest-ts/context";
4
+ import {GG_TRACE} from "@grest-ts/trace";
5
+ import {IPCClientRequest} from "@grest-ts/ipc";
6
+ import type {RuntimeRunner} from "./runner/RuntimeRunner";
7
+ import {InlineRunner} from "./runner/InlineRunner";
8
+ import {WorkerRunner} from "./runner/WorkerRunner";
9
+ import {IsolatedRunner} from "./runner/IsolatedRunner";
10
+ import type {GGTestRunner} from "./GGTestRunner";
11
+
12
+ /**
13
+ * Lifecycle state of a runtime instance.
14
+ */
15
+ export enum GGTestRuntimeState {
16
+ /** Initial state, not yet started */
17
+ CREATED = 'created',
18
+ /** Running successfully */
19
+ STARTED = 'started',
20
+ /** Startup failed, but worker/IPC still alive for diagnostics */
21
+ FAILED = 'failed',
22
+ /** Runtime stopped, but worker/IPC still alive for log retrieval */
23
+ STOPPED = 'stopped',
24
+ /** Fully shut down, no IPC available */
25
+ SHUTDOWN = 'shutdown',
26
+ }
27
+
28
+ export interface GGTestEnvConfig {
29
+ executablePath: string;
30
+ className: string;
31
+ testRouterPort: number;
32
+ testId: string;
33
+ runtimeId: string;
34
+ initialCommands: GGTestCommand[];
35
+ /** When true, the runtime runs inline (same process). Affects module loading strategy. */
36
+ inline?: boolean;
37
+ }
38
+
39
+ export interface GGTestCommand<Payload = unknown> {
40
+ method: string;
41
+ payload: Payload;
42
+ }
43
+
44
+ export interface GGTestRuntimeConfig {
45
+ mode?: GGTestMode
46
+ }
47
+
48
+ export enum GGTestMode {
49
+ INLINE = 'INLINE',
50
+ WORKER = 'WORKER',
51
+ ISOLATED = 'ISOLATED'
52
+ }
53
+
54
+ export class GGTestRuntime {
55
+
56
+ public readonly runner: GGTestRunner;
57
+
58
+ /**
59
+ * Unique identifier for this runtime instance.
60
+ * Used for targeted communication (e.g., "checklist-0", "checklist-1").
61
+ */
62
+ public readonly runtimeId: string
63
+
64
+ /**
65
+ * The runtime name used for selector access (e.g., "checklist").
66
+ * This is the static NAME property from the runtime class.
67
+ */
68
+ public readonly name: string
69
+
70
+ /**
71
+ * The class name of the runtime (e.g., "ChecklistRuntime").
72
+ * Used for file matching and logging.
73
+ */
74
+ public readonly className: string
75
+
76
+ /**
77
+ * Lifecycle state of this runtime instance.
78
+ */
79
+ private _state: GGTestRuntimeState = GGTestRuntimeState.CREATED
80
+
81
+ public get state(): GGTestRuntimeState {
82
+ return this._state;
83
+ }
84
+
85
+ /**
86
+ * Locator keys registered by this runtime.
87
+ * Populated via IPC after compose completes.
88
+ */
89
+ private readonly registeredLocatorKeys: Set<string> = new Set()
90
+
91
+ /**
92
+ * Commands to be sent to this runtime on startup.
93
+ * Queued before start(), passed to worker via env.
94
+ */
95
+ private readonly initialCommands: GGTestCommand[] = []
96
+
97
+ private readonly executablePath: string;
98
+ private readonly config: GGTestRuntimeConfig;
99
+ private runtimeRunner?: RuntimeRunner
100
+ /** Factory to create the runtime without dynamic import (used by inline mode) */
101
+ public runtimeFactory?: () => any;
102
+
103
+ /** Counter for generating unique runtime IDs per name */
104
+ private static runtimeCounters: Map<string, number> = new Map();
105
+
106
+ public constructor(runner: GGTestRunner, executablePath: string, className: string, name: string, config?: GGTestRuntimeConfig) {
107
+ this.executablePath = executablePath;
108
+ this.className = className;
109
+ this.name = name;
110
+ this.runtimeId = GGTestRuntime.generateRuntimeId(name);
111
+ this.config = config ?? {}
112
+ this.config.mode ??= GGTestMode.WORKER
113
+ this.runner = runner
114
+ this.runner.addRuntime(this);
115
+ }
116
+
117
+ private static generateRuntimeId(name: string): string {
118
+ const count = GGTestRuntime.runtimeCounters.get(name) ?? 0;
119
+ GGTestRuntime.runtimeCounters.set(name, count + 1);
120
+ return `${name}-${count}`;
121
+ }
122
+
123
+ public async start(): Promise<this> {
124
+ if (this._state !== GGTestRuntimeState.CREATED) {
125
+ throw new Error("Can only start runtimes if they are in CREATED state! Current state: " + this._state);
126
+ }
127
+ await new GGContext("Test").run(async () => {
128
+ GG_TRACE.init();
129
+ GGLog.debug(this, 'Launching ' + this.className + ' in ' + this.config.mode + ' mode...')
130
+
131
+ const config: GGTestEnvConfig = {
132
+ executablePath: this.executablePath,
133
+ className: this.className,
134
+ testRouterPort: this.runner.ipcServer.getPort(),
135
+ testId: this.runner.testId,
136
+ runtimeId: this.runtimeId,
137
+ initialCommands: this.initialCommands
138
+ }
139
+ switch (this.config.mode) {
140
+ case GGTestMode.INLINE:
141
+ config.inline = true;
142
+ this.runtimeRunner = new InlineRunner(config, this.runtimeFactory);
143
+ break;
144
+ case GGTestMode.WORKER:
145
+ this.runtimeRunner = new WorkerRunner(config);
146
+ break;
147
+ case GGTestMode.ISOLATED:
148
+ this.runtimeRunner = new IsolatedRunner(config);
149
+ break;
150
+ default:
151
+ throw new Error(`Unknown test mode: ${this.config.mode}`);
152
+ }
153
+
154
+ const startupTimeout = 30000;
155
+ try {
156
+ await withTimeout(
157
+ this.runtimeRunner.start(),
158
+ startupTimeout,
159
+ 'Service ' + this.className + ' failed to start within ' + startupTimeout + 'ms'
160
+ );
161
+ this._state = GGTestRuntimeState.STARTED;
162
+ GGLog.debug(this, this.className + ' started successfully')
163
+ } catch (error) {
164
+ // Mark as failed but keep runner alive for diagnostics (e.g., log retrieval)
165
+ this._state = GGTestRuntimeState.FAILED;
166
+ throw error;
167
+ }
168
+ });
169
+ return this
170
+ }
171
+
172
+ /**
173
+ * Stop the GGRuntime (teardown services) but keep worker/IPC alive.
174
+ * This allows log retrieval after the runtime has stopped.
175
+ * Idempotent - safe to call multiple times.
176
+ */
177
+ public async stop(): Promise<void> {
178
+ await new GGContext("Test").run(async () => {
179
+ GG_TRACE.init();
180
+ if (this._state === GGTestRuntimeState.STOPPED || this._state === GGTestRuntimeState.SHUTDOWN) {
181
+ // Already stopped or shutdown, nothing to do
182
+ return;
183
+ }
184
+ if (this._state === GGTestRuntimeState.FAILED) {
185
+ // Already failed, just mark as stopped
186
+ this._state = GGTestRuntimeState.STOPPED;
187
+ return;
188
+ }
189
+ if (this._state === GGTestRuntimeState.CREATED) {
190
+ // Never started (e.g., another runtime or hook failed first), nothing to stop
191
+ this._state = GGTestRuntimeState.STOPPED;
192
+ return;
193
+ }
194
+ if (this._state !== GGTestRuntimeState.STARTED) {
195
+ throw new Error("Can only stop runtimes in STARTED state! Current state: " + this._state);
196
+ }
197
+ GGLog.debug(this, 'Stopping ' + this.className + '...')
198
+ try {
199
+ await this.runtimeRunner?.stopRuntime()
200
+ } catch (error) {
201
+ GGLog.error(this, 'Error stopping ' + this.className, error)
202
+ }
203
+ this._state = GGTestRuntimeState.STOPPED;
204
+ GGLog.debug(this, this.className + ' stopped')
205
+ });
206
+ }
207
+
208
+ /**
209
+ * Fully shutdown the runtime and worker. IPC will be disconnected.
210
+ * After this, no commands can be sent.
211
+ */
212
+ public async shutdown(): Promise<void> {
213
+ await new GGContext("Test").run(async () => {
214
+ GG_TRACE.init();
215
+ if (this._state === GGTestRuntimeState.SHUTDOWN) {
216
+ return; // Already shutdown
217
+ }
218
+ GGLog.debug(this, 'Shutting down ' + this.className + '...')
219
+ try {
220
+ await this.runtimeRunner?.shutdown()
221
+ } catch (error) {
222
+ GGLog.error(this, 'Error shutting down ' + this.className, error)
223
+ }
224
+ this.runtimeRunner = undefined
225
+ this._state = GGTestRuntimeState.SHUTDOWN;
226
+ GGLog.debug(this, this.className + ' shut down')
227
+ });
228
+ }
229
+
230
+ public async sendCommand<Payload, Result>(type: IPCClientRequest<Payload, Result>, payload: Payload): Promise<Result> {
231
+ switch (this._state) {
232
+ case GGTestRuntimeState.CREATED:
233
+ this.initialCommands.push({method: type, payload: payload});
234
+ return undefined as Result;
235
+ case GGTestRuntimeState.STARTED:
236
+ case GGTestRuntimeState.FAILED:
237
+ case GGTestRuntimeState.STOPPED:
238
+ // IPC still available in these states
239
+ return await this.runner.ipcServer.sendFrameworkMessage(this.runtimeId, type, payload);
240
+ case GGTestRuntimeState.SHUTDOWN:
241
+ throw new Error(`Cannot send command to shut down runtime ${this.className}`);
242
+ }
243
+ }
244
+
245
+ // -----------
246
+ // Key registration (for callOn routing)
247
+ // -----------
248
+
249
+ /**
250
+ * Register multiple locator keys at once.
251
+ */
252
+ public registerLocatorKeys(keys: string[]): void {
253
+ for (const key of keys) {
254
+ this.registeredLocatorKeys.add(key);
255
+ }
256
+ }
257
+
258
+ /**
259
+ * Check if this runtime has a specific locator key.
260
+ */
261
+ public hasLocatorKey(key: string): boolean {
262
+ return this.registeredLocatorKeys.has(key);
263
+ }
264
+
265
+ }