every-plugin 0.0.6 → 0.1.0

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 (50) hide show
  1. package/README.md +77 -13
  2. package/dist/errors.js +10 -10
  3. package/dist/plugin.d.ts +98 -44
  4. package/dist/plugin.js +17 -15
  5. package/dist/runtime/errors.d.ts +22 -1
  6. package/dist/runtime/errors.d.ts.map +1 -1
  7. package/dist/runtime/errors.js +8 -6
  8. package/dist/runtime/index.d.ts +68 -16
  9. package/dist/runtime/index.d.ts.map +1 -1
  10. package/dist/runtime/index.js +126 -27
  11. package/dist/runtime/mf.test.d.ts +2 -0
  12. package/dist/runtime/mf.test.d.ts.map +1 -0
  13. package/dist/runtime/mf.test.js +135 -0
  14. package/dist/runtime/runtime.test.d.ts +1 -1
  15. package/dist/runtime/runtime.test.js +366 -296
  16. package/dist/runtime/services/module-federation.service.d.ts +36 -9
  17. package/dist/runtime/services/module-federation.service.d.ts.map +1 -1
  18. package/dist/runtime/services/module-federation.service.js +122 -99
  19. package/dist/runtime/services/plugin.service.d.ts +66 -13
  20. package/dist/runtime/services/plugin.service.d.ts.map +1 -1
  21. package/dist/runtime/services/plugin.service.js +330 -122
  22. package/dist/runtime/services/secrets.service.d.ts +29 -9
  23. package/dist/runtime/services/secrets.service.d.ts.map +1 -1
  24. package/dist/runtime/services/secrets.service.js +30 -26
  25. package/dist/runtime/services.d.ts +1 -2
  26. package/dist/runtime/services.d.ts.map +1 -1
  27. package/dist/runtime/services.js +0 -1
  28. package/dist/runtime/streaming.d.ts +29 -0
  29. package/dist/runtime/streaming.d.ts.map +1 -0
  30. package/dist/runtime/streaming.js +59 -0
  31. package/dist/runtime/streaming.test.d.ts +2 -0
  32. package/dist/runtime/streaming.test.d.ts.map +1 -0
  33. package/dist/runtime/streaming.test.js +271 -0
  34. package/dist/runtime/test-utils.d.ts +3 -0
  35. package/dist/runtime/test-utils.d.ts.map +1 -0
  36. package/dist/runtime/test-utils.js +13 -0
  37. package/dist/runtime/types.d.ts +27 -27
  38. package/dist/runtime/validation.d.ts +1 -1
  39. package/dist/runtime/validation.d.ts.map +1 -1
  40. package/dist/runtime/validation.js +14 -11
  41. package/dist/source.d.ts +62 -190
  42. package/dist/source.d.ts.map +1 -1
  43. package/dist/source.js +32 -85
  44. package/package.json +7 -3
  45. package/dist/runtime/runtime.d.ts +0 -58
  46. package/dist/runtime/runtime.d.ts.map +0 -1
  47. package/dist/runtime/runtime.js +0 -75
  48. package/dist/runtime/services/plugin-cache.service.d.ts +0 -18
  49. package/dist/runtime/services/plugin-cache.service.d.ts.map +0 -1
  50. package/dist/runtime/services/plugin-cache.service.js +0 -55
@@ -1,308 +1,378 @@
1
- import { describe, it, expect } from "vitest";
2
- import { Effect, Duration } from "effect";
3
- import { PluginRuntime, createPluginRuntime } from "./index";
1
+ import { Duration, Effect } from "effect";
2
+ import { afterEach, beforeEach, describe, expect, it } from "vitest";
3
+ import { createPluginRuntime, PluginRuntime } from "./index";
4
+
4
5
  // Local template plugin for testing
5
6
  const TEST_REGISTRY = {
6
- "test-plugin": {
7
- remoteUrl: "http://localhost:3000/remoteEntry.js",
8
- type: "transformer",
9
- version: "0.0.1",
10
- description: "Local template plugin for testing",
11
- },
7
+ "test-plugin": {
8
+ remoteUrl: "http://localhost:3000/remoteEntry.js",
9
+ type: "transformer",
10
+ version: "0.0.1",
11
+ description: "Local template plugin for testing",
12
+ },
13
+ "invalid-plugin": {
14
+ remoteUrl: "https://invalid-plugin-url-that-does-not-exist.com/plugin.js",
15
+ type: "transformer",
16
+ version: "1.0.0",
17
+ description: "Invalid plugin for testing error handling",
18
+ },
12
19
  };
13
20
  const TEST_CONFIG = {
14
- variables: {
15
- baseUrl: "http://localhost:1337",
16
- timeout: 5000,
17
- },
18
- secrets: {
19
- apiKey: "{{API_KEY}}", // This will be hydrated from secrets config
20
- },
21
+ variables: {
22
+ baseUrl: "http://localhost:1337",
23
+ timeout: 5000,
24
+ },
25
+ secrets: {
26
+ apiKey: "{{API_KEY}}", // This will be hydrated from secrets config
27
+ },
21
28
  };
22
29
  const TEST_INPUT = {
23
- query: "test query for processing",
24
- options: {
25
- limit: 2,
26
- },
30
+ query: "test query for processing",
31
+ options: {
32
+ limit: 2,
33
+ },
27
34
  };
28
35
  // Secrets configuration for hydration
29
36
  const SECRETS_CONFIG = {
30
- API_KEY: "test-api-key-value",
37
+ API_KEY: "test-api-key-value",
31
38
  };
32
39
  describe("Plugin Runtime Integration", () => {
33
- it("should create and execute plugin end-to-end", async () => {
34
- // Create the managed runtime
35
- const runtime = createPluginRuntime({
36
- registry: TEST_REGISTRY,
37
- secrets: SECRETS_CONFIG,
38
- });
39
- // Execute the test within the runtime
40
- const result = await runtime.runPromise(Effect.gen(function* () {
41
- const pluginRuntime = yield* PluginRuntime;
42
- // Create plugin (load → instantiate → initialize)
43
- const initializedPlugin = yield* pluginRuntime.createPlugin("test-plugin", TEST_CONFIG);
44
- // Verify plugin was initialized
45
- expect(initializedPlugin).toBeDefined();
46
- expect(initializedPlugin.metadata.pluginId).toBe("test-plugin");
47
- expect(initializedPlugin.plugin).toBeDefined();
48
- // Execute plugin
49
- const output = yield* pluginRuntime.executePlugin(initializedPlugin, TEST_INPUT);
50
- return output;
51
- }));
52
- // Validate output structure
53
- expect(result).toBeDefined();
54
- expect(typeof result).toBe("object");
55
- const typedResult = result;
56
- // Verify success wrapper structure
57
- expect(typedResult).toHaveProperty("success");
58
- expect(typedResult).toHaveProperty("data");
59
- expect(typedResult.success).toBe(true);
60
- // Verify data content
61
- expect(typedResult.data).toHaveProperty("results");
62
- expect(typedResult.data).toHaveProperty("count");
63
- expect(Array.isArray(typedResult.data.results)).toBe(true);
64
- expect(typedResult.data.count).toBe(2); // Should match the limit we set
65
- expect(typedResult.data.results).toHaveLength(2);
66
- // Verify results contain processed query
67
- expect(typedResult.data.results[0]).toHaveProperty("id");
68
- expect(typedResult.data.results[0]).toHaveProperty("content");
69
- expect(typedResult.data.results[0].content).toContain("test query for processing");
70
- expect(typedResult.data.results[1].content).toContain("test query for processing");
71
- });
72
- it("should handle plugin lifecycle correctly", async () => {
73
- const runtime = createPluginRuntime({
74
- registry: TEST_REGISTRY,
75
- secrets: SECRETS_CONFIG,
76
- });
77
- await runtime.runPromise(Effect.gen(function* () {
78
- const pluginRuntime = yield* PluginRuntime;
79
- // Test individual lifecycle steps
80
- // 1. Load plugin
81
- const pluginConstructor = yield* pluginRuntime.loadPlugin("test-plugin");
82
- expect(pluginConstructor).toBeDefined();
83
- expect(pluginConstructor.metadata.pluginId).toBe("test-plugin");
84
- // 2. Instantiate plugin
85
- const pluginInstance = yield* pluginRuntime.instantiatePlugin(pluginConstructor);
86
- expect(pluginInstance).toBeDefined();
87
- expect(pluginInstance.plugin).toBeDefined();
88
- expect(pluginInstance.metadata.pluginId).toBe("test-plugin");
89
- // 3. Initialize plugin
90
- const initializedPlugin = yield* pluginRuntime.initializePlugin(pluginInstance, TEST_CONFIG);
91
- expect(initializedPlugin).toBeDefined();
92
- expect(initializedPlugin.config).toBeDefined();
93
- // 4. Execute plugin
94
- const output = yield* pluginRuntime.executePlugin(initializedPlugin, TEST_INPUT);
95
- expect(output).toBeDefined();
96
- }));
97
- });
98
- it("should handle secret hydration", async () => {
99
- const configWithSecrets = {
100
- variables: {
101
- baseUrl: "http://localhost:1337",
102
- timeout: 10000,
103
- },
104
- secrets: {
105
- apiKey: "{{API_KEY}}", // This should be hydrated
106
- },
107
- };
108
- const secretsConfig = {
109
- API_KEY: "hydrated-api-key-value",
110
- };
111
- const runtime = createPluginRuntime({
112
- registry: TEST_REGISTRY,
113
- secrets: secretsConfig,
114
- });
115
- await runtime.runPromise(Effect.gen(function* () {
116
- const pluginRuntime = yield* PluginRuntime;
117
- const initializedPlugin = yield* pluginRuntime.createPlugin("test-plugin", configWithSecrets);
118
- // Verify secrets were hydrated in the config
119
- expect(initializedPlugin.config).toBeDefined();
120
- // Verify the config structure and that secrets were hydrated
121
- const config = initializedPlugin.config;
122
- expect(config).toHaveProperty("secrets");
123
- expect(config.secrets).toHaveProperty("apiKey");
124
- expect(config.secrets.apiKey).toBe("hydrated-api-key-value");
125
- // Verify variables are also present
126
- expect(config).toHaveProperty("variables");
127
- expect(config.variables.baseUrl).toBe("http://localhost:1337");
128
- expect(config.variables.timeout).toBe(10000);
129
- // Verify the plugin was successfully initialized with hydrated secrets
130
- // by executing it to ensure it works with the hydrated config
131
- const output = yield* pluginRuntime.executePlugin(initializedPlugin, TEST_INPUT);
132
- expect(output).toBeDefined();
133
- }));
134
- });
135
- it("should handle validation error for missing apiKey", async () => {
136
- const configWithoutApiKey = {
137
- variables: {
138
- baseUrl: "http://localhost:1337",
139
- timeout: 5000,
140
- },
141
- secrets: {
142
- // Missing required apiKey
143
- },
144
- };
145
- const runtime = createPluginRuntime({
146
- registry: TEST_REGISTRY,
147
- secrets: SECRETS_CONFIG,
148
- });
149
- const result = await runtime.runPromise(Effect.gen(function* () {
150
- const pluginRuntime = yield* PluginRuntime;
151
- return yield* pluginRuntime.createPlugin("test-plugin", configWithoutApiKey).pipe(Effect.catchTag("PluginRuntimeError", (error) => {
152
- // Should catch validation error
153
- expect(error.operation).toBe("validate-config");
154
- expect(error.retryable).toBe(false);
155
- expect(error.pluginId).toBe("test-plugin");
156
- expect(error.cause).toBeDefined();
157
- // The error should mention the missing apiKey
158
- const errorMessage = error.cause?.message || "";
159
- // Zod validation errors might be in JSON format, so check for both formats
160
- const lowerMessage = errorMessage.toLowerCase();
161
- const hasApiKeyError = lowerMessage.includes("api key") ||
162
- lowerMessage.includes("apikey") ||
163
- lowerMessage.includes("required");
164
- expect(hasApiKeyError).toBe(true);
165
- console.log("Caught expected validation error:", {
166
- operation: error.operation,
167
- pluginId: error.pluginId,
168
- retryable: error.retryable,
169
- cause: error.cause?.message,
170
- });
171
- return Effect.succeed("validation-error-handled-properly");
172
- }), Effect.catchAll((unexpectedError) => {
173
- console.error("Unexpected error type:", unexpectedError);
174
- expect.fail(`Expected PluginRuntimeError but got: ${unexpectedError._tag || typeof unexpectedError}`);
175
- return Effect.succeed("should-not-reach-here");
176
- }));
177
- }));
178
- expect(result).toBe("validation-error-handled-properly");
179
- });
180
- it("should handle runtime shutdown gracefully", async () => {
181
- const runtime = createPluginRuntime({
182
- registry: TEST_REGISTRY,
183
- secrets: SECRETS_CONFIG,
184
- });
185
- await runtime.runPromise(Effect.gen(function* () {
186
- const pluginRuntime = yield* PluginRuntime;
187
- // Test shutdown
188
- yield* pluginRuntime.shutdown();
189
- // Shutdown should complete without errors
190
- expect(true).toBe(true); // If we reach here, shutdown was successful
191
- }));
192
- });
193
- it("should handle invalid remoteUrl with proper error handling", async () => {
194
- const INVALID_REGISTRY = {
195
- "invalid-plugin": {
196
- remoteUrl: "https://invalid-plugin-url-that-does-not-exist.com/plugin.js",
197
- type: "transformer",
198
- version: "1.0.0",
199
- description: "Invalid plugin for testing error handling",
200
- },
201
- };
202
- const runtime = createPluginRuntime({
203
- registry: INVALID_REGISTRY,
204
- secrets: {},
205
- });
206
- const result = await runtime.runPromise(Effect.gen(function* () {
207
- const pluginRuntime = yield* PluginRuntime;
208
- return yield* pluginRuntime.createPlugin("invalid-plugin", {}).pipe(Effect.catchTag("PluginRuntimeError", (error) => {
209
- expect(error.operation).toBe("register-remote");
210
- expect(error.retryable).toBe(true);
211
- expect(error.pluginId).toBe("invalid-plugin");
212
- expect(error.cause).toBeDefined();
213
- console.debug("Caught expected PluginRuntimeError:", {
214
- operation: error.operation,
215
- pluginId: error.pluginId,
216
- retryable: error.retryable,
217
- cause: error.cause?.message,
218
- });
219
- return Effect.succeed("error-handled-properly");
220
- }), Effect.catchAll((unexpectedError) => {
221
- // If any other error type is thrown, fail the test
222
- console.error("Unexpected error type:", unexpectedError);
223
- expect.fail(`Expected PluginRuntimeError but got: ${unexpectedError._tag || typeof unexpectedError}`);
224
- return Effect.succeed("should-not-reach-here");
225
- }));
226
- }));
227
- expect(result).toBe("error-handled-properly");
228
- });
229
- it("should handle plugin not found in registry", async () => {
230
- // Empty registry to test missing plugin
231
- const EMPTY_REGISTRY = {};
232
- const runtime = createPluginRuntime({
233
- registry: EMPTY_REGISTRY,
234
- secrets: {},
235
- });
236
- const result = await runtime.runPromise(Effect.gen(function* () {
237
- const pluginRuntime = yield* PluginRuntime;
238
- return yield* pluginRuntime.loadPlugin("non-existent-plugin").pipe(Effect.catchTag("PluginRuntimeError", (error) => {
239
- // Validate error properties
240
- expect(error.operation).toBe("load-plugin");
241
- expect(error.retryable).toBe(false);
242
- expect(error.pluginId).toBe("non-existent-plugin");
243
- expect(error.cause).toBeDefined();
244
- expect(error.cause?.message).toContain("not found in registry");
245
- // Log error details for debugging
246
- console.log("Caught expected PluginRuntimeError for missing plugin:", {
247
- operation: error.operation,
248
- pluginId: error.pluginId,
249
- retryable: error.retryable,
250
- cause: error.cause?.message,
251
- });
252
- // Return success to make test pass
253
- return Effect.succeed("plugin-not-found-handled-properly");
254
- }), Effect.catchAll((unexpectedError) => {
255
- // If any other error type is thrown, fail the test
256
- console.error("Unexpected error type:", unexpectedError);
257
- expect.fail(`Expected PluginRuntimeError but got: ${unexpectedError._tag || typeof unexpectedError}`);
258
- return Effect.succeed("should-not-reach-here");
259
- }));
260
- }));
261
- expect(result).toBe("plugin-not-found-handled-properly");
262
- });
263
- it("should handle constructor instantiation failure", async () => {
264
- // Registry with real remote that should fail during instantiation/initialization
265
- const REAL_REMOTE_REGISTRY = {
266
- "simple-transform": {
267
- remoteUrl: "https://unpkg.com/@curatedotfun/simple-transform@latest/dist/remoteEntry.js",
268
- type: "transformer",
269
- version: "latest",
270
- description: "Real remote plugin for testing constructor failure",
271
- },
272
- };
273
- const runtime = createPluginRuntime({
274
- registry: REAL_REMOTE_REGISTRY,
275
- secrets: {},
276
- });
277
- const result = await runtime.runPromise(Effect.gen(function* () {
278
- const pluginRuntime = yield* PluginRuntime;
279
- return yield* pluginRuntime.createPlugin("simple-transform", {}).pipe(Effect.timeout(Duration.seconds(1)), // Timeout after 3 seconds
280
- Effect.catchTag("PluginRuntimeError", (error) => {
281
- // Validate error properties
282
- expect(["load-remote"]).toContain(error.operation);
283
- expect(error.retryable).toBe(true);
284
- expect(error.pluginId).toBe("simple-transform");
285
- expect(error.cause).toBeDefined();
286
- // Log error details for debugging
287
- console.log("Caught expected PluginRuntimeError for constructor failure:", {
288
- operation: error.operation,
289
- pluginId: error.pluginId,
290
- retryable: error.retryable,
291
- cause: error.cause?.message,
292
- });
293
- // Return success to make test pass
294
- return Effect.succeed("constructor-failure-handled-properly");
295
- }), Effect.catchTag("TimeoutException", () => {
296
- // Handle timeout as expected failure - remote is too slow
297
- console.log("Plugin creation timed out - treating as expected failure");
298
- return Effect.succeed("timeout-handled-properly");
299
- }), Effect.catchAll((unexpectedError) => {
300
- // If any other error type is thrown, fail the test
301
- console.error("Unexpected error type:", unexpectedError);
302
- expect.fail(`Expected PluginRuntimeError or TimeoutException but got: ${unexpectedError._tag || typeof unexpectedError}`);
303
- return Effect.succeed("should-not-reach-here");
304
- }));
305
- }));
306
- expect(["constructor-failure-handled-properly", "timeout-handled-properly"]).toContain(result);
307
- });
40
+ let runtime;
41
+ beforeEach(() => {
42
+ runtime = createPluginRuntime({
43
+ registry: TEST_REGISTRY,
44
+ secrets: SECRETS_CONFIG,
45
+ });
46
+ });
47
+ afterEach(async () => {
48
+ if (runtime) {
49
+ await runtime.dispose();
50
+ }
51
+ });
52
+ it("should create and execute plugin end-to-end", async () => {
53
+ // Execute the test within the runtime
54
+ const result = await runtime.runPromise(
55
+ Effect.gen(function* () {
56
+ const pluginRuntime = yield* PluginRuntime;
57
+ // Create plugin (load → instantiate → initialize)
58
+ const initializedPlugin = yield* pluginRuntime.usePlugin(
59
+ "test-plugin",
60
+ TEST_CONFIG,
61
+ );
62
+ // Verify plugin was initialized
63
+ expect(initializedPlugin).toBeDefined();
64
+ expect(initializedPlugin.metadata.pluginId).toBe("test-plugin");
65
+ expect(initializedPlugin.plugin).toBeDefined();
66
+ // Execute plugin
67
+ const output = yield* pluginRuntime.executePlugin(
68
+ initializedPlugin,
69
+ TEST_INPUT,
70
+ );
71
+ return output;
72
+ }),
73
+ );
74
+ // Validate output structure
75
+ expect(result).toBeDefined();
76
+ expect(typeof result).toBe("object");
77
+ const typedResult = result;
78
+ // Verify success wrapper structure
79
+ expect(typedResult).toHaveProperty("success");
80
+ expect(typedResult).toHaveProperty("data");
81
+ expect(typedResult.success).toBe(true);
82
+ // Verify data content
83
+ expect(typedResult.data).toHaveProperty("results");
84
+ expect(typedResult.data).toHaveProperty("count");
85
+ expect(Array.isArray(typedResult.data.results)).toBe(true);
86
+ expect(typedResult.data.count).toBe(2); // Should match the limit we set
87
+ expect(typedResult.data.results).toHaveLength(2);
88
+ // Verify results contain processed query
89
+ expect(typedResult.data.results[0]).toHaveProperty("id");
90
+ expect(typedResult.data.results[0]).toHaveProperty("content");
91
+ expect(typedResult.data.results[0].content).toContain(
92
+ "test query for processing",
93
+ );
94
+ expect(typedResult.data.results[1].content).toContain(
95
+ "test query for processing",
96
+ );
97
+ });
98
+ it("should handle plugin lifecycle correctly", async () => {
99
+ await runtime.runPromise(
100
+ Effect.gen(function* () {
101
+ const pluginRuntime = yield* PluginRuntime;
102
+ // Test individual lifecycle steps
103
+ // 1. Load plugin
104
+ const pluginConstructor =
105
+ yield* pluginRuntime.loadPlugin("test-plugin");
106
+ expect(pluginConstructor).toBeDefined();
107
+ expect(pluginConstructor.metadata.pluginId).toBe("test-plugin");
108
+ // 2. Instantiate plugin
109
+ const pluginInstance =
110
+ yield* pluginRuntime.instantiatePlugin(pluginConstructor);
111
+ expect(pluginInstance).toBeDefined();
112
+ expect(pluginInstance.plugin).toBeDefined();
113
+ expect(pluginInstance.metadata.pluginId).toBe("test-plugin");
114
+ // 3. Initialize plugin
115
+ const initializedPlugin = yield* pluginRuntime.initializePlugin(
116
+ pluginInstance,
117
+ TEST_CONFIG,
118
+ );
119
+ expect(initializedPlugin).toBeDefined();
120
+ expect(initializedPlugin.config).toBeDefined();
121
+ // 4. Execute plugin
122
+ const output = yield* pluginRuntime.executePlugin(
123
+ initializedPlugin,
124
+ TEST_INPUT,
125
+ );
126
+ expect(output).toBeDefined();
127
+ }),
128
+ );
129
+ });
130
+ it("should handle secret hydration", async () => {
131
+ const configWithSecrets = {
132
+ variables: {
133
+ baseUrl: "http://localhost:1337",
134
+ timeout: 10000,
135
+ },
136
+ secrets: {
137
+ apiKey: "{{API_KEY}}", // This should be hydrated
138
+ },
139
+ };
140
+ const secretsConfig = {
141
+ API_KEY: "hydrated-api-key-value",
142
+ };
143
+ const runtime = createPluginRuntime({
144
+ registry: TEST_REGISTRY,
145
+ secrets: secretsConfig,
146
+ });
147
+ await runtime.runPromise(
148
+ Effect.gen(function* () {
149
+ const pluginRuntime = yield* PluginRuntime;
150
+ const initializedPlugin = yield* pluginRuntime.usePlugin(
151
+ "test-plugin",
152
+ configWithSecrets,
153
+ );
154
+ // Verify secrets were hydrated in the config
155
+ expect(initializedPlugin.config).toBeDefined();
156
+ // Verify the config structure and that secrets were hydrated
157
+ const config = initializedPlugin.config;
158
+ expect(config).toHaveProperty("secrets");
159
+ expect(config.secrets).toHaveProperty("apiKey");
160
+ expect(config.secrets.apiKey).toBe("hydrated-api-key-value");
161
+ // Verify variables are also present
162
+ expect(config).toHaveProperty("variables");
163
+ expect(config.variables.baseUrl).toBe("http://localhost:1337");
164
+ expect(config.variables.timeout).toBe(10000);
165
+ // Verify the plugin was successfully initialized with hydrated secrets
166
+ // by executing it to ensure it works with the hydrated config
167
+ const output = yield* pluginRuntime.executePlugin(
168
+ initializedPlugin,
169
+ TEST_INPUT,
170
+ );
171
+ expect(output).toBeDefined();
172
+ }),
173
+ );
174
+ });
175
+ it("should handle validation error for missing apiKey", async () => {
176
+ const configWithoutApiKey = {
177
+ variables: {
178
+ baseUrl: "http://localhost:1337",
179
+ timeout: 5000,
180
+ },
181
+ secrets: {
182
+ // Missing required apiKey
183
+ },
184
+ };
185
+ const result = await runtime.runPromise(
186
+ Effect.gen(function* () {
187
+ const pluginRuntime = yield* PluginRuntime;
188
+ return yield* pluginRuntime
189
+ .usePlugin("test-plugin", configWithoutApiKey)
190
+ .pipe(
191
+ Effect.catchTag("PluginRuntimeError", (error) => {
192
+ // Should catch validation error
193
+ expect(error.operation).toBe("validate-config");
194
+ expect(error.retryable).toBe(false);
195
+ expect(error.pluginId).toBe("test-plugin");
196
+ expect(error.cause).toBeDefined();
197
+ // The error should mention the missing apiKey
198
+ const errorMessage = error.cause?.message || "";
199
+ // Zod validation errors might be in JSON format, so check for both formats
200
+ const lowerMessage = errorMessage.toLowerCase();
201
+ const hasApiKeyError =
202
+ lowerMessage.includes("api key") ||
203
+ lowerMessage.includes("apikey") ||
204
+ lowerMessage.includes("required");
205
+ expect(hasApiKeyError).toBe(true);
206
+ console.debug("Caught expected validation error:", {
207
+ operation: error.operation,
208
+ pluginId: error.pluginId,
209
+ retryable: error.retryable,
210
+ cause: error.cause?.message,
211
+ });
212
+ return Effect.succeed("validation-error-handled-properly");
213
+ }),
214
+ Effect.catchAll((unexpectedError) => {
215
+ console.error("Unexpected error type:", unexpectedError);
216
+ expect.fail(
217
+ `Expected PluginRuntimeError but got: ${unexpectedError._tag || typeof unexpectedError}`,
218
+ );
219
+ return Effect.succeed("should-not-reach-here");
220
+ }),
221
+ );
222
+ }),
223
+ );
224
+ expect(result).toBe("validation-error-handled-properly");
225
+ });
226
+ it("should handle runtime shutdown gracefully", async () => {
227
+ const runtime = createPluginRuntime({
228
+ registry: TEST_REGISTRY,
229
+ secrets: SECRETS_CONFIG,
230
+ });
231
+ await runtime.runPromise(
232
+ Effect.gen(function* () {
233
+ const pluginRuntime = yield* PluginRuntime;
234
+ // Test shutdown
235
+ yield* pluginRuntime.shutdown();
236
+ // Shutdown should complete without errors
237
+ expect(true).toBe(true); // If we reach here, shutdown was successful
238
+ }),
239
+ );
240
+ });
241
+ it("should handle invalid remoteUrl with proper error handling", async () => {
242
+ const result = await runtime.runPromise(
243
+ Effect.gen(function* () {
244
+ const pluginRuntime = yield* PluginRuntime;
245
+ return yield* pluginRuntime.usePlugin("invalid-plugin", {}).pipe(
246
+ Effect.catchTag("PluginRuntimeError", (error) => {
247
+ expect(error.operation).toBe("register-remote");
248
+ expect(error.retryable).toBe(true);
249
+ expect(error.pluginId).toBe("invalid-plugin");
250
+ expect(error.cause).toBeDefined();
251
+ console.debug("Caught expected PluginRuntimeError:", {
252
+ operation: error.operation,
253
+ pluginId: error.pluginId,
254
+ retryable: error.retryable,
255
+ cause: error.cause?.message,
256
+ });
257
+ return Effect.succeed("error-handled-properly");
258
+ }),
259
+ Effect.catchAll((unexpectedError) => {
260
+ // If any other error type is thrown, fail the test
261
+ console.error("Unexpected error type:", unexpectedError);
262
+ expect.fail(
263
+ `Expected PluginRuntimeError but got: ${unexpectedError._tag || typeof unexpectedError}`,
264
+ );
265
+ return Effect.succeed("should-not-reach-here");
266
+ }),
267
+ );
268
+ }),
269
+ );
270
+ expect(result).toBe("error-handled-properly");
271
+ });
272
+ it("should handle plugin not found in registry", async () => {
273
+ // Empty registry to test missing plugin
274
+ const EMPTY_REGISTRY = {};
275
+ const runtime = createPluginRuntime({
276
+ registry: EMPTY_REGISTRY,
277
+ secrets: {},
278
+ });
279
+ const result = await runtime.runPromise(
280
+ Effect.gen(function* () {
281
+ const pluginRuntime = yield* PluginRuntime;
282
+ return yield* pluginRuntime.loadPlugin("non-existent-plugin").pipe(
283
+ Effect.catchTag("PluginRuntimeError", (error) => {
284
+ // Validate error properties
285
+ expect(error.operation).toBe("load-plugin");
286
+ expect(error.retryable).toBe(false);
287
+ expect(error.pluginId).toBe("non-existent-plugin");
288
+ expect(error.cause).toBeDefined();
289
+ expect(error.cause?.message).toContain("not found in registry");
290
+ // Log error details for debugging
291
+ console.debug(
292
+ "Caught expected PluginRuntimeError for missing plugin:",
293
+ {
294
+ operation: error.operation,
295
+ pluginId: error.pluginId,
296
+ retryable: error.retryable,
297
+ cause: error.cause?.message,
298
+ },
299
+ );
300
+ // Return success to make test pass
301
+ return Effect.succeed("plugin-not-found-handled-properly");
302
+ }),
303
+ Effect.catchAll((unexpectedError) => {
304
+ // If any other error type is thrown, fail the test
305
+ console.error("Unexpected error type:", unexpectedError);
306
+ expect.fail(
307
+ `Expected PluginRuntimeError but got: ${unexpectedError._tag || typeof unexpectedError}`,
308
+ );
309
+ return Effect.succeed("should-not-reach-here");
310
+ }),
311
+ );
312
+ }),
313
+ );
314
+ expect(result).toBe("plugin-not-found-handled-properly");
315
+ });
316
+ it("should handle constructor instantiation failure", async () => {
317
+ // Registry with real remote that should fail during instantiation/initialization
318
+ const REAL_REMOTE_REGISTRY = {
319
+ "simple-transform": {
320
+ remoteUrl:
321
+ "https://unpkg.com/@curatedotfun/simple-transform@latest/dist/remoteEntry.js",
322
+ type: "transformer",
323
+ version: "latest",
324
+ description: "Real remote plugin for testing constructor failure",
325
+ },
326
+ };
327
+ const runtime = createPluginRuntime({
328
+ registry: REAL_REMOTE_REGISTRY,
329
+ secrets: {},
330
+ });
331
+ const result = await runtime.runPromise(
332
+ Effect.gen(function* () {
333
+ const pluginRuntime = yield* PluginRuntime;
334
+ return yield* pluginRuntime.usePlugin("simple-transform", {}).pipe(
335
+ Effect.timeout(Duration.seconds(1)), // Timeout after 3 seconds
336
+ Effect.catchTag("PluginRuntimeError", (error) => {
337
+ // Validate error properties
338
+ expect(["load-remote"]).toContain(error.operation);
339
+ expect(error.retryable).toBe(false);
340
+ expect(error.pluginId).toBe("simple-transform");
341
+ expect(error.cause).toBeDefined();
342
+ // Log error details for debugging
343
+ console.debug(
344
+ "Caught expected PluginRuntimeError for constructor failure:",
345
+ {
346
+ operation: error.operation,
347
+ pluginId: error.pluginId,
348
+ retryable: error.retryable,
349
+ cause: error.cause?.message,
350
+ },
351
+ );
352
+ // Return success to make test pass
353
+ return Effect.succeed("constructor-failure-handled-properly");
354
+ }),
355
+ Effect.catchTag("TimeoutException", () => {
356
+ // Handle timeout as expected failure - remote is too slow
357
+ console.debug(
358
+ "Plugin creation timed out - treating as expected failure",
359
+ );
360
+ return Effect.succeed("timeout-handled-properly");
361
+ }),
362
+ Effect.catchAll((unexpectedError) => {
363
+ // If any other error type is thrown, fail the test
364
+ console.error("Unexpected error type:", unexpectedError);
365
+ expect.fail(
366
+ `Expected PluginRuntimeError or TimeoutException but got: ${unexpectedError._tag || typeof unexpectedError}`,
367
+ );
368
+ return Effect.succeed("should-not-reach-here");
369
+ }),
370
+ );
371
+ }),
372
+ );
373
+ expect([
374
+ "constructor-failure-handled-properly",
375
+ "timeout-handled-properly",
376
+ ]).toContain(result);
377
+ });
308
378
  });