@geekmidas/cli 0.0.26 → 0.2.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 (121) hide show
  1. package/FUNCTION_CRON_SUPPORT.md +266 -0
  2. package/README.md +84 -17
  3. package/dist/CronGenerator-1PflEYe2.cjs +60 -0
  4. package/dist/CronGenerator-1PflEYe2.cjs.map +1 -0
  5. package/dist/CronGenerator-DXRfHQcV.mjs +54 -0
  6. package/dist/CronGenerator-DXRfHQcV.mjs.map +1 -0
  7. package/dist/EndpointGenerator-BbGrDiCP.cjs +264 -0
  8. package/dist/EndpointGenerator-BbGrDiCP.cjs.map +1 -0
  9. package/dist/EndpointGenerator-BmZ9BxbO.mjs +258 -0
  10. package/dist/EndpointGenerator-BmZ9BxbO.mjs.map +1 -0
  11. package/dist/FunctionGenerator-Clw64SwQ.cjs +59 -0
  12. package/dist/FunctionGenerator-Clw64SwQ.cjs.map +1 -0
  13. package/dist/FunctionGenerator-DOEB_yPh.mjs +53 -0
  14. package/dist/FunctionGenerator-DOEB_yPh.mjs.map +1 -0
  15. package/dist/Generator-CDoEXCDg.cjs +47 -0
  16. package/dist/Generator-CDoEXCDg.cjs.map +1 -0
  17. package/dist/Generator-UanJW0_V.mjs +41 -0
  18. package/dist/Generator-UanJW0_V.mjs.map +1 -0
  19. package/dist/SubscriberGenerator-BfMZCVNy.cjs +204 -0
  20. package/dist/SubscriberGenerator-BfMZCVNy.cjs.map +1 -0
  21. package/dist/SubscriberGenerator-D2u00NI3.mjs +198 -0
  22. package/dist/SubscriberGenerator-D2u00NI3.mjs.map +1 -0
  23. package/dist/build/index.cjs +12 -0
  24. package/dist/build/index.mjs +12 -0
  25. package/dist/build/manifests.cjs +3 -0
  26. package/dist/build/manifests.mjs +3 -0
  27. package/dist/build/providerResolver.cjs +5 -0
  28. package/dist/build/providerResolver.mjs +3 -0
  29. package/dist/build/types.cjs +0 -0
  30. package/dist/build/types.mjs +0 -0
  31. package/dist/build-BBhlEjf5.cjs +89 -0
  32. package/dist/build-BBhlEjf5.cjs.map +1 -0
  33. package/dist/build-kY-lG30Q.mjs +83 -0
  34. package/dist/build-kY-lG30Q.mjs.map +1 -0
  35. package/dist/config-D1EpSGk6.cjs +36 -0
  36. package/dist/config-D1EpSGk6.cjs.map +1 -0
  37. package/dist/config-U-mdW-7Y.mjs +30 -0
  38. package/dist/config-U-mdW-7Y.mjs.map +1 -0
  39. package/dist/config.cjs +1 -1
  40. package/dist/config.mjs +1 -1
  41. package/dist/generators/CronGenerator.cjs +4 -0
  42. package/dist/generators/CronGenerator.mjs +4 -0
  43. package/dist/generators/EndpointGenerator.cjs +4 -0
  44. package/dist/generators/EndpointGenerator.mjs +4 -0
  45. package/dist/generators/FunctionGenerator.cjs +4 -0
  46. package/dist/generators/FunctionGenerator.mjs +4 -0
  47. package/dist/generators/Generator.cjs +3 -0
  48. package/dist/generators/Generator.mjs +3 -0
  49. package/dist/generators/SubscriberGenerator.cjs +4 -0
  50. package/dist/generators/SubscriberGenerator.mjs +4 -0
  51. package/dist/generators/index.cjs +12 -0
  52. package/dist/generators/index.mjs +8 -0
  53. package/dist/generators-CEKtVh81.cjs +0 -0
  54. package/dist/generators-CsLujGXs.mjs +0 -0
  55. package/dist/index.cjs +71 -25
  56. package/dist/index.cjs.map +1 -0
  57. package/dist/index.mjs +71 -25
  58. package/dist/index.mjs.map +1 -0
  59. package/dist/manifests-BrJXpHrf.mjs +21 -0
  60. package/dist/manifests-BrJXpHrf.mjs.map +1 -0
  61. package/dist/manifests-D0saShvH.cjs +27 -0
  62. package/dist/manifests-D0saShvH.cjs.map +1 -0
  63. package/dist/{openapi-CksVdkh2.mjs → openapi-BQx3_JbM.mjs} +8 -6
  64. package/dist/openapi-BQx3_JbM.mjs.map +1 -0
  65. package/dist/{openapi-D4QQJUPY.cjs → openapi-CMLr04cz.cjs} +9 -7
  66. package/dist/openapi-CMLr04cz.cjs.map +1 -0
  67. package/dist/{openapi-react-query-DpT3XHFC.mjs → openapi-react-query-DbrWwQzb.mjs} +5 -3
  68. package/dist/openapi-react-query-DbrWwQzb.mjs.map +1 -0
  69. package/dist/{openapi-react-query-C1JLYUOs.cjs → openapi-react-query-Dvjqx_Eo.cjs} +5 -3
  70. package/dist/openapi-react-query-Dvjqx_Eo.cjs.map +1 -0
  71. package/dist/openapi-react-query.cjs +1 -1
  72. package/dist/openapi-react-query.mjs +1 -1
  73. package/dist/openapi.cjs +4 -3
  74. package/dist/openapi.mjs +4 -3
  75. package/dist/providerResolver-B_TjNF0_.mjs +96 -0
  76. package/dist/providerResolver-B_TjNF0_.mjs.map +1 -0
  77. package/dist/providerResolver-DgvzNfP4.cjs +114 -0
  78. package/dist/providerResolver-DgvzNfP4.cjs.map +1 -0
  79. package/examples/cron-example.ts +45 -0
  80. package/examples/function-example.ts +40 -0
  81. package/examples/gkm.config.json +22 -0
  82. package/examples/gkm.minimal.config.json +7 -0
  83. package/examples/gkm.production.config.json +27 -0
  84. package/examples/logger.ts +1 -1
  85. package/package.json +38 -14
  86. package/src/__tests__/config.spec.ts +110 -0
  87. package/src/__tests__/openapi-react-query.spec.ts +506 -0
  88. package/src/__tests__/openapi.spec.ts +362 -0
  89. package/src/__tests__/test-helpers.ts +180 -0
  90. package/src/build/__tests__/index-new.spec.ts +577 -0
  91. package/src/build/index.ts +197 -0
  92. package/src/build/manifests.ts +35 -0
  93. package/src/build/providerResolver.ts +184 -0
  94. package/src/build/types.ts +37 -0
  95. package/src/config.ts +14 -6
  96. package/src/generators/CronGenerator.ts +98 -0
  97. package/src/generators/EndpointGenerator.ts +389 -0
  98. package/src/generators/FunctionGenerator.ts +97 -0
  99. package/src/generators/Generator.ts +95 -0
  100. package/src/generators/SubscriberGenerator.ts +271 -0
  101. package/src/generators/__tests__/CronGenerator.spec.ts +445 -0
  102. package/src/generators/__tests__/EndpointGenerator.spec.ts +394 -0
  103. package/src/generators/__tests__/FunctionGenerator.spec.ts +256 -0
  104. package/src/generators/__tests__/SubscriberGenerator.spec.ts +341 -0
  105. package/src/generators/index.ts +9 -0
  106. package/src/index.ts +57 -22
  107. package/src/openapi-react-query.ts +2 -1
  108. package/src/openapi.ts +5 -4
  109. package/src/types.ts +91 -2
  110. package/dist/build-BTggTCYL.cjs +0 -176
  111. package/dist/build-Ca4P6_lY.mjs +0 -170
  112. package/dist/build.cjs +0 -5
  113. package/dist/build.mjs +0 -5
  114. package/dist/config-BNqUMsvc.cjs +0 -24
  115. package/dist/config-BciAdY6_.mjs +0 -18
  116. package/dist/loadEndpoints-BBIavB9h.cjs +0 -37
  117. package/dist/loadEndpoints-DAZ53Og2.mjs +0 -31
  118. package/dist/loadEndpoints.cjs +0 -3
  119. package/dist/loadEndpoints.mjs +0 -3
  120. package/src/build.ts +0 -305
  121. package/src/loadEndpoints.ts +0 -48
@@ -0,0 +1,394 @@
1
+ import { mkdir, readFile } from 'node:fs/promises';
2
+ import { join } from 'node:path';
3
+ import type { Endpoint } from '@geekmidas/constructs/endpoints';
4
+ import { itWithDir } from '@geekmidas/testkit/os';
5
+ import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
6
+
7
+ import type { HttpMethod } from '@geekmidas/constructs';
8
+ import {
9
+ createMockBuildContext,
10
+ createMockEndpointFile,
11
+ createTestEndpoint,
12
+ } from '../../__tests__/test-helpers';
13
+ import { EndpointGenerator } from '../EndpointGenerator';
14
+ import type { GeneratedConstruct } from '../Generator';
15
+
16
+ describe('EndpointGenerator', () => {
17
+ let tempDir: string;
18
+ let outputDir: string;
19
+ let generator: EndpointGenerator;
20
+ let context: ReturnType<typeof createMockBuildContext>;
21
+
22
+ beforeEach(async () => {
23
+ generator = new EndpointGenerator();
24
+ context = createMockBuildContext();
25
+ });
26
+
27
+ afterEach(async () => {});
28
+
29
+ it('should identify valid endpoints', () => {
30
+ const testEndpoint = createTestEndpoint('/test', 'GET');
31
+ expect(generator.isConstruct(testEndpoint)).toBe(true);
32
+ });
33
+
34
+ it('should reject invalid constructs', () => {
35
+ expect(generator.isConstruct({})).toBe(false);
36
+ expect(generator.isConstruct('string')).toBe(false);
37
+ expect(generator.isConstruct(null)).toBe(false);
38
+ });
39
+
40
+ const createTestEndpointConstruct = (
41
+ key: string,
42
+ path: string,
43
+ method: HttpMethod,
44
+ dir: string,
45
+ ): GeneratedConstruct<Endpoint<any, any, any, any, any, any>> => ({
46
+ key,
47
+ name: key.toLowerCase(),
48
+ construct: createTestEndpoint(path, method),
49
+ path: {
50
+ absolute: join(dir, `${key}.ts`),
51
+ relative: `./${key}.ts`,
52
+ },
53
+ });
54
+
55
+ itWithDir('should generate a single server app file', async ({ dir }) => {
56
+ const outputDir = join(dir, 'output');
57
+ const routesDir = join(dir, 'routes');
58
+ await mkdir(outputDir, { recursive: true });
59
+
60
+ await Promise.all([
61
+ createMockEndpointFile(
62
+ routesDir,
63
+ 'testEndpoint.ts',
64
+ 'testEndpoint',
65
+ '/test',
66
+ 'GET',
67
+ ),
68
+ createMockEndpointFile(
69
+ routesDir,
70
+ 'anotherEndpoint.ts',
71
+ 'anotherEndpoint',
72
+ '/another',
73
+ 'POST',
74
+ ),
75
+ ]);
76
+
77
+ const constructs = await generator.load('**/routes/*.ts', dir);
78
+
79
+ const routes = await generator.build(context, constructs, outputDir, {
80
+ provider: 'server',
81
+ enableOpenApi: true,
82
+ });
83
+
84
+ expect(routes).toHaveLength(1);
85
+ expect(routes[0]).toMatchObject({
86
+ path: '*',
87
+ method: 'ALL',
88
+ handler: expect.stringContaining('app.ts'),
89
+ });
90
+
91
+ // Check that the app.ts file was created
92
+ const appPath = join(outputDir, 'app.ts');
93
+ const appContent = await readFile(appPath, 'utf-8');
94
+
95
+ expect(appContent).toContain('import { setupEndpoints }');
96
+ expect(appContent).toContain('import { setupSubscribers }');
97
+ expect(appContent).toContain('function createApp');
98
+ expect(appContent).toContain('app?: HonoType');
99
+ expect(appContent).toContain('enableOpenApi: boolean = true');
100
+ expect(appContent).toContain('interface ServerApp');
101
+ expect(appContent).toContain('async start(options');
102
+ expect(appContent).toContain('serve: (app: HonoType, port: number)');
103
+
104
+ // Check that the endpoints.ts file was created with endpoint logic
105
+ const endpointsPath = join(outputDir, 'endpoints.ts');
106
+ const endpointsContent = await readFile(endpointsPath, 'utf-8');
107
+
108
+ expect(endpointsContent).toContain('import { HonoEndpoint }');
109
+ expect(endpointsContent).toContain('import { testEndpoint }');
110
+ expect(endpointsContent).toContain('import { anotherEndpoint }');
111
+ // Function signature always defaults to true
112
+ expect(endpointsContent).toContain('enableOpenApi: boolean = true');
113
+ // OpenAPI options are configured based on the parameter
114
+ expect(endpointsContent).toContain("docsPath: '/docs'");
115
+ });
116
+
117
+ itWithDir(
118
+ 'should generate server app without OpenAPI when disabled',
119
+ async ({ dir }) => {
120
+ const outputDir = join(dir, 'output');
121
+ const routesDir = join(dir, 'routes');
122
+ await mkdir(outputDir, { recursive: true });
123
+
124
+ await createMockEndpointFile(
125
+ routesDir,
126
+ 'testEndpoint.ts',
127
+ 'testEndpoint',
128
+ '/test',
129
+ 'GET',
130
+ );
131
+
132
+ const constructs = await generator.load('**/routes/*.ts', dir);
133
+
134
+ await generator.build(context, constructs, outputDir, {
135
+ provider: 'server',
136
+ enableOpenApi: false,
137
+ });
138
+
139
+ const appPath = join(outputDir, 'app.ts');
140
+ const appContent = await readFile(appPath, 'utf-8');
141
+
142
+ expect(appContent).toContain('function createApp');
143
+
144
+ // Check that the endpoints.ts file defaults to true (but can be overridden)
145
+ const endpointsPath = join(outputDir, 'endpoints.ts');
146
+ const endpointsContent = await readFile(endpointsPath, 'utf-8');
147
+
148
+ // The function signature always defaults to true
149
+ expect(endpointsContent).toContain('enableOpenApi: boolean = true');
150
+ // But the OpenAPI options are configured dynamically based on the parameter
151
+ expect(endpointsContent).toContain('docsPath: false');
152
+ },
153
+ );
154
+
155
+ itWithDir(
156
+ 'should generate individual handlers in routes subdirectory',
157
+ async ({ dir }) => {
158
+ const outputDir = join(dir, 'output');
159
+ const routesDir = join(dir, 'routes');
160
+ await mkdir(outputDir, { recursive: true });
161
+
162
+ await Promise.all([
163
+ createMockEndpointFile(
164
+ routesDir,
165
+ 'testEndpoint.ts',
166
+ 'testEndpoint',
167
+ '/test',
168
+ 'GET',
169
+ ),
170
+ createMockEndpointFile(
171
+ routesDir,
172
+ 'anotherEndpoint.ts',
173
+ 'anotherEndpoint',
174
+ '/another',
175
+ 'POST',
176
+ ),
177
+ ]);
178
+
179
+ const constructs = await generator.load('**/routes/*.ts', dir);
180
+
181
+ const routes = await generator.build(context, constructs, outputDir, {
182
+ provider: 'aws-lambda',
183
+ });
184
+
185
+ expect(routes).toHaveLength(2);
186
+
187
+ // Find routes by their path since order may vary
188
+ const testRoute = routes.find((r) => r.path === '/test');
189
+ const anotherRoute = routes.find((r) => r.path === '/another');
190
+
191
+ expect(testRoute).toMatchObject({
192
+ path: '/test',
193
+ method: 'GET',
194
+ handler: expect.stringContaining('routes/testEndpoint.handler'),
195
+ });
196
+ expect(anotherRoute).toMatchObject({
197
+ path: '/another',
198
+ method: 'POST',
199
+ handler: expect.stringContaining('routes/anotherEndpoint.handler'),
200
+ });
201
+
202
+ // Check that handler files were created
203
+ const testHandlerPath = join(outputDir, 'routes', 'testEndpoint.ts');
204
+ const testHandlerContent = await readFile(testHandlerPath, 'utf-8');
205
+ expect(testHandlerContent).toContain('AmazonApiGatewayV2Endpoint');
206
+ expect(testHandlerContent).toContain('import { testEndpoint }');
207
+
208
+ const anotherHandlerPath = join(
209
+ outputDir,
210
+ 'routes',
211
+ 'anotherEndpoint.ts',
212
+ );
213
+ const anotherHandlerContent = await readFile(anotherHandlerPath, 'utf-8');
214
+ expect(anotherHandlerContent).toContain('AmazonApiGatewayV2Endpoint');
215
+ expect(anotherHandlerContent).toContain('import { anotherEndpoint }');
216
+ },
217
+ );
218
+
219
+ itWithDir(
220
+ 'should generate individual handlers with v1 adapter',
221
+ async ({ dir }) => {
222
+ const outputDir = join(dir, 'output');
223
+ const routesDir = join(dir, 'routes');
224
+ await mkdir(outputDir, { recursive: true });
225
+
226
+ await createMockEndpointFile(
227
+ routesDir,
228
+ 'testEndpoint.ts',
229
+ 'testEndpoint',
230
+ '/test',
231
+ 'GET',
232
+ );
233
+
234
+ const constructs = await generator.load('**/routes/*.ts', dir);
235
+
236
+ const routes = await generator.build(context, constructs, outputDir, {
237
+ provider: 'aws-apigatewayv1',
238
+ });
239
+
240
+ expect(routes).toHaveLength(1);
241
+
242
+ const handlerPath = join(outputDir, 'testEndpoint.ts');
243
+ const handlerContent = await readFile(handlerPath, 'utf-8');
244
+ expect(handlerContent).toContain('AmazonApiGatewayV1Endpoint');
245
+ expect(handlerContent).toContain('import { testEndpoint }');
246
+ },
247
+ );
248
+
249
+ itWithDir(
250
+ 'should generate individual handlers with v2 adapter',
251
+ async ({ dir }) => {
252
+ const outputDir = join(dir, 'output');
253
+ const routesDir = join(dir, 'routes');
254
+ await mkdir(outputDir, { recursive: true });
255
+
256
+ await createMockEndpointFile(
257
+ routesDir,
258
+ 'testEndpoint.ts',
259
+ 'testEndpoint',
260
+ '/test',
261
+ 'GET',
262
+ );
263
+
264
+ const constructs = await generator.load('**/routes/*.ts', dir);
265
+
266
+ const routes = await generator.build(context, constructs, outputDir, {
267
+ provider: 'aws-apigatewayv2',
268
+ });
269
+
270
+ expect(routes).toHaveLength(1);
271
+
272
+ const handlerPath = join(outputDir, 'testEndpoint.ts');
273
+ const handlerContent = await readFile(handlerPath, 'utf-8');
274
+ expect(handlerContent).toContain('AmazonApiGatewayV2Endpoint');
275
+ expect(handlerContent).toContain('import { testEndpoint }');
276
+ },
277
+ );
278
+
279
+ itWithDir(
280
+ 'should return empty array for empty constructs',
281
+ async ({ dir }) => {
282
+ const outputDir = join(dir, 'output');
283
+ const routes = await generator.build(context, [], outputDir);
284
+ expect(routes).toEqual([]);
285
+ },
286
+ );
287
+
288
+ itWithDir(
289
+ 'should use default provider when none specified',
290
+ async ({ dir }) => {
291
+ const outputDir = join(dir, 'output');
292
+ const routesDir = join(dir, 'routes');
293
+ await mkdir(outputDir, { recursive: true });
294
+
295
+ await createMockEndpointFile(
296
+ routesDir,
297
+ 'testEndpoint.ts',
298
+ 'testEndpoint',
299
+ '/test',
300
+ 'GET',
301
+ );
302
+
303
+ const constructs = await generator.load('**/routes/*.ts', dir);
304
+
305
+ const routes = await generator.build(context, constructs, outputDir);
306
+
307
+ expect(routes).toHaveLength(1);
308
+
309
+ // Should use default aws-apigatewayv2
310
+ const handlerPath = join(outputDir, 'testEndpoint.ts');
311
+ const handlerContent = await readFile(handlerPath, 'utf-8');
312
+ expect(handlerContent).toContain('AmazonApiGatewayV2Endpoint');
313
+ },
314
+ );
315
+
316
+ itWithDir('should throw error for unsupported provider', async ({ dir }) => {
317
+ const outputDir = join(dir, 'output');
318
+ const constructs = [
319
+ createTestEndpointConstruct('testEndpoint', '/test', 'GET', dir),
320
+ ];
321
+
322
+ await expect(
323
+ generator.build(context, constructs, outputDir, {
324
+ provider: 'unsupported' as any,
325
+ }),
326
+ ).rejects.toThrow('Unsupported provider: unsupported');
327
+ });
328
+
329
+ itWithDir(
330
+ 'should generate correct import paths for nested files',
331
+ async ({ dir }) => {
332
+ const outputDir = join(dir, 'output');
333
+ const nestedDir = join(dir, 'src', 'api', 'endpoints');
334
+ await mkdir(outputDir, { recursive: true });
335
+
336
+ await createMockEndpointFile(
337
+ nestedDir,
338
+ 'deepEndpoint.ts',
339
+ 'deepEndpoint',
340
+ '/deep',
341
+ 'GET',
342
+ );
343
+
344
+ const constructs = await generator.load('**/src/api/endpoints/*.ts', dir);
345
+
346
+ await generator.build(context, constructs, outputDir, {
347
+ provider: 'aws-apigatewayv2',
348
+ });
349
+
350
+ const handlerPath = join(outputDir, 'deepEndpoint.ts');
351
+ const handlerContent = await readFile(handlerPath, 'utf-8');
352
+
353
+ // Check that relative imports are correct
354
+ expect(handlerContent).toContain(
355
+ "from '../src/api/endpoints/deepEndpoint.js'",
356
+ );
357
+ expect(handlerContent).toMatch(/from ['"]\.\.\/\.\.\/\.\.\/.*\/env['"]/);
358
+ },
359
+ );
360
+
361
+ itWithDir('should log generation progress', async ({ dir }) => {
362
+ const logSpy = vi.spyOn(console, 'log');
363
+ const outputDir = join(dir, 'output');
364
+ const routesDir = join(dir, 'routes');
365
+ await mkdir(outputDir, { recursive: true });
366
+
367
+ await Promise.all([
368
+ createMockEndpointFile(
369
+ routesDir,
370
+ 'endpoint1.ts',
371
+ 'endpoint1',
372
+ '/test1',
373
+ 'GET',
374
+ ),
375
+ createMockEndpointFile(
376
+ routesDir,
377
+ 'endpoint2.ts',
378
+ 'endpoint2',
379
+ '/test2',
380
+ 'POST',
381
+ ),
382
+ ]);
383
+
384
+ const constructs = await generator.load('**/routes/*.ts', dir);
385
+
386
+ await generator.build(context, constructs, outputDir, {
387
+ provider: 'server',
388
+ });
389
+
390
+ expect(logSpy).toHaveBeenCalledWith('Generated server with 2 endpoints');
391
+
392
+ logSpy.mockRestore();
393
+ });
394
+ });
@@ -0,0 +1,256 @@
1
+ import { readFile } from 'node:fs/promises';
2
+ import { join } from 'node:path';
3
+ import { f } from '@geekmidas/constructs/functions';
4
+ import type { Function } from '@geekmidas/constructs/functions';
5
+ import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
6
+ import { z } from 'zod';
7
+ import {
8
+ cleanupDir,
9
+ createMockBuildContext,
10
+ createTempDir,
11
+ } from '../../__tests__/test-helpers';
12
+ import { FunctionGenerator } from '../FunctionGenerator';
13
+ import type { GeneratedConstruct } from '../Generator';
14
+
15
+ describe('FunctionGenerator', () => {
16
+ let tempDir: string;
17
+ let outputDir: string;
18
+ let generator: FunctionGenerator;
19
+ let context: ReturnType<typeof createMockBuildContext>;
20
+
21
+ beforeEach(async () => {
22
+ tempDir = await createTempDir();
23
+ outputDir = join(tempDir, 'output');
24
+ generator = new FunctionGenerator();
25
+ context = createMockBuildContext();
26
+ });
27
+
28
+ afterEach(async () => {
29
+ await cleanupDir(tempDir);
30
+ });
31
+
32
+ describe('isConstruct', () => {
33
+ it('should identify valid functions', () => {
34
+ const testFunction = f
35
+ .input(z.object({ name: z.string() }))
36
+ .output(z.object({ greeting: z.string() }))
37
+ .timeout(30)
38
+ .handle(async ({ input }) => ({
39
+ greeting: `Hello, ${input.name}!`,
40
+ }));
41
+
42
+ expect(generator.isConstruct(testFunction)).toBe(true);
43
+ });
44
+
45
+ it('should reject invalid constructs', () => {
46
+ expect(generator.isConstruct({})).toBe(false);
47
+ expect(generator.isConstruct('string')).toBe(false);
48
+ expect(generator.isConstruct(null)).toBe(false);
49
+ });
50
+ });
51
+
52
+ describe('build', () => {
53
+ const createTestFunctionConstruct = (
54
+ key: string,
55
+ timeout: number = 30,
56
+ ): GeneratedConstruct<Function<any, any, any, any>> => {
57
+ const func = f
58
+ .timeout(timeout)
59
+ .handle(async () => ({ greeting: 'Hello!' }));
60
+
61
+ return {
62
+ key,
63
+ name: key.toLowerCase(),
64
+ construct: func,
65
+ path: {
66
+ absolute: join(tempDir, `${key}.ts`),
67
+ relative: `${key}.ts`,
68
+ },
69
+ };
70
+ };
71
+
72
+ describe('aws-lambda provider', () => {
73
+ it('should generate function handlers', async () => {
74
+ const constructs = [
75
+ createTestFunctionConstruct('processData', 60),
76
+ createTestFunctionConstruct('sendEmail', 30),
77
+ ];
78
+
79
+ const functionInfos = await generator.build(
80
+ context,
81
+ constructs,
82
+ outputDir,
83
+ { provider: 'aws-lambda' },
84
+ );
85
+
86
+ expect(functionInfos).toHaveLength(2);
87
+ expect(functionInfos[0]).toMatchObject({
88
+ name: 'processData',
89
+ handler: expect.stringContaining('functions/processData.handler'),
90
+ timeout: 60,
91
+ });
92
+ expect(functionInfos[1]).toMatchObject({
93
+ name: 'sendEmail',
94
+ handler: expect.stringContaining('functions/sendEmail.handler'),
95
+ timeout: 30,
96
+ });
97
+
98
+ // Check that handler files were created
99
+ const processDataHandlerPath = join(
100
+ outputDir,
101
+ 'functions',
102
+ 'processData.ts',
103
+ );
104
+ const processDataContent = await readFile(
105
+ processDataHandlerPath,
106
+ 'utf-8',
107
+ );
108
+ expect(processDataContent).toContain('AWSLambdaFunction');
109
+ expect(processDataContent).toContain('import { processData }');
110
+ expect(processDataContent).toContain('import envParser');
111
+ expect(processDataContent).toContain('import logger');
112
+
113
+ const sendEmailHandlerPath = join(
114
+ outputDir,
115
+ 'functions',
116
+ 'sendEmail.ts',
117
+ );
118
+ const sendEmailContent = await readFile(sendEmailHandlerPath, 'utf-8');
119
+ expect(sendEmailContent).toContain('AWSLambdaFunction');
120
+ expect(sendEmailContent).toContain('import { sendEmail }');
121
+ });
122
+
123
+ it('should generate correct relative import paths', async () => {
124
+ const construct: GeneratedConstruct<Function<any, any, any, any>> = {
125
+ key: 'deepFunction',
126
+ name: 'deep-function',
127
+ construct: createTestFunctionConstruct('deepFunction', 45).construct,
128
+ path: {
129
+ absolute: join(tempDir, 'src/functions/deep/processor.ts'),
130
+ relative: 'src/functions/deep/processor.ts',
131
+ },
132
+ };
133
+
134
+ await generator.build(context, [construct], outputDir, {
135
+ provider: 'aws-lambda',
136
+ });
137
+
138
+ const handlerPath = join(outputDir, 'functions', 'deepFunction.ts');
139
+ const handlerContent = await readFile(handlerPath, 'utf-8');
140
+
141
+ // Check relative imports are correct - the path will be relative from outputDir
142
+ expect(handlerContent).toMatch(
143
+ /from ['"].*src\/functions\/deep\/processor\.js['"]/,
144
+ );
145
+ expect(handlerContent).toMatch(/from ['"].*\/env['"]/);
146
+ expect(handlerContent).toMatch(/from ['"].*\/logger['"]/);
147
+ });
148
+
149
+ it('should log generation progress', async () => {
150
+ const logSpy = vi.spyOn(console, 'log');
151
+
152
+ const constructs = [createTestFunctionConstruct('testFunction', 30)];
153
+
154
+ await generator.build(context, constructs, outputDir, {
155
+ provider: 'aws-lambda',
156
+ });
157
+
158
+ expect(logSpy).toHaveBeenCalledWith(
159
+ 'Generated function handler: testFunction',
160
+ );
161
+
162
+ logSpy.mockRestore();
163
+ });
164
+ });
165
+
166
+ describe('non aws-lambda provider', () => {
167
+ it('should return empty array for server provider', async () => {
168
+ const constructs = [createTestFunctionConstruct('testFunction', 30)];
169
+
170
+ const functionInfos = await generator.build(
171
+ context,
172
+ constructs,
173
+ outputDir,
174
+ { provider: 'server' },
175
+ );
176
+
177
+ expect(functionInfos).toEqual([]);
178
+ });
179
+
180
+ it('should return empty array for aws-apigatewayv1 provider', async () => {
181
+ const constructs = [createTestFunctionConstruct('testFunction', 30)];
182
+
183
+ const functionInfos = await generator.build(
184
+ context,
185
+ constructs,
186
+ outputDir,
187
+ { provider: 'aws-apigatewayv1' },
188
+ );
189
+
190
+ expect(functionInfos).toEqual([]);
191
+ });
192
+ });
193
+
194
+ it('should return empty array for empty constructs', async () => {
195
+ const functionInfos = await generator.build(context, [], outputDir, {
196
+ provider: 'aws-lambda',
197
+ });
198
+ expect(functionInfos).toEqual([]);
199
+ });
200
+
201
+ it('should use default provider when none specified', async () => {
202
+ const constructs = [createTestFunctionConstruct('defaultFunction', 30)];
203
+
204
+ const functionInfos = await generator.build(
205
+ context,
206
+ constructs,
207
+ outputDir,
208
+ );
209
+
210
+ expect(functionInfos).toHaveLength(1);
211
+ expect(functionInfos[0].name).toBe('defaultFunction');
212
+
213
+ // Check that handler was created (default is aws-lambda)
214
+ const handlerPath = join(outputDir, 'functions', 'defaultFunction.ts');
215
+ const handlerContent = await readFile(handlerPath, 'utf-8');
216
+ expect(handlerContent).toContain('AWSLambdaFunction');
217
+ });
218
+
219
+ it('should handle functions with different timeout values', async () => {
220
+ const constructs = [
221
+ createTestFunctionConstruct('quickFunction', 15),
222
+ createTestFunctionConstruct('slowFunction', 300),
223
+ ];
224
+
225
+ const functionInfos = await generator.build(
226
+ context,
227
+ constructs,
228
+ outputDir,
229
+ { provider: 'aws-lambda' },
230
+ );
231
+
232
+ expect(functionInfos[0].timeout).toBe(15);
233
+ expect(functionInfos[1].timeout).toBe(300);
234
+ });
235
+
236
+ it('should handle functions with custom environment parser patterns', async () => {
237
+ const customContext = {
238
+ ...context,
239
+ envParserImportPattern: '{ customParser as envParser }',
240
+ loggerImportPattern: '{ customLogger as logger }',
241
+ };
242
+
243
+ const constructs = [createTestFunctionConstruct('customFunction', 30)];
244
+
245
+ await generator.build(customContext, constructs, outputDir, {
246
+ provider: 'aws-lambda',
247
+ });
248
+
249
+ const handlerPath = join(outputDir, 'functions', 'customFunction.ts');
250
+ const handlerContent = await readFile(handlerPath, 'utf-8');
251
+
252
+ expect(handlerContent).toContain('import { customParser as envParser }');
253
+ expect(handlerContent).toContain('import { customLogger as logger }');
254
+ });
255
+ });
256
+ });