@friggframework/devtools 2.0.0--canary.704.6a606c3.0 → 2.0.0--canary.703.12b980b.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 (65) hide show
  1. package/README.md +0 -14
  2. package/frigg-cli/README.md +30 -83
  3. package/frigg-cli/__tests__/unit/commands/deploy-subscriptions-step.test.js +35 -0
  4. package/frigg-cli/__tests__/unit/commands/generate-iam.test.js +0 -18
  5. package/frigg-cli/__tests__/unit/commands/install.test.js +341 -594
  6. package/frigg-cli/__tests__/unit/dependencies.test.js +5 -3
  7. package/frigg-cli/deploy-command/index.js +3 -0
  8. package/frigg-cli/deploy-command/subscriptions-step.js +44 -0
  9. package/frigg-cli/generate-command/index.js +0 -1
  10. package/frigg-cli/generate-iam-command.js +0 -1
  11. package/frigg-cli/index.js +6 -12
  12. package/frigg-cli/init-command/backend-first-handler.js +554 -397
  13. package/frigg-cli/init-command/index.js +35 -82
  14. package/frigg-cli/install-command/commit-changes.js +8 -40
  15. package/frigg-cli/install-command/environment-variables.js +0 -15
  16. package/frigg-cli/install-command/index.js +21 -96
  17. package/frigg-cli/install-command/install-package.js +4 -77
  18. package/frigg-cli/install-command/integration-file.js +11 -20
  19. package/frigg-cli/install-command/template.js +75 -102
  20. package/frigg-cli/install-command/validate-package.js +4 -36
  21. package/frigg-cli/package.json +3 -3
  22. package/frigg-cli/test/init-command.test.js +2 -9
  23. package/infrastructure/README.md +0 -20
  24. package/infrastructure/docs/POSTGRES-CONFIGURATION.md +1 -21
  25. package/infrastructure/domains/admin-scripts/admin-script-builder.js +46 -50
  26. package/infrastructure/domains/admin-scripts/admin-script-builder.test.js +7 -46
  27. package/infrastructure/domains/database/aurora-builder.js +2 -13
  28. package/infrastructure/domains/database/aurora-builder.test.js +0 -22
  29. package/infrastructure/domains/database/migration-builder.js +175 -182
  30. package/infrastructure/domains/database/migration-builder.test.js +5 -36
  31. package/infrastructure/domains/integration/define-integration-provisioning.test.js +59 -0
  32. package/infrastructure/domains/integration/integration-builder.js +80 -65
  33. package/infrastructure/domains/integration/integration-builder.test.js +30 -73
  34. package/infrastructure/domains/scheduler/trigger-builder.js +114 -0
  35. package/infrastructure/domains/scheduler/trigger-builder.test.js +82 -0
  36. package/infrastructure/domains/security/iam-generator.js +5 -112
  37. package/infrastructure/domains/security/iam-generator.test.js +0 -94
  38. package/infrastructure/domains/shared/function-environments.js +9 -1
  39. package/infrastructure/domains/shared/function-environments.test.js +14 -0
  40. package/infrastructure/domains/shared/utilities/base-definition-factory.js +124 -83
  41. package/infrastructure/domains/shared/utilities/base-definition-factory.test.js +26 -116
  42. package/infrastructure/domains/shared/utilities/logging-config.js +4 -3
  43. package/infrastructure/domains/shared/utilities/logging-config.test.js +2 -1
  44. package/infrastructure/infrastructure-composer.js +2 -0
  45. package/infrastructure/infrastructure-composer.test.js +3 -3
  46. package/package.json +8 -13
  47. package/frigg-cli/__tests__/unit/commands/init.test.js +0 -688
  48. package/frigg-cli/__tests__/unit/utils/node-version.test.js +0 -66
  49. package/frigg-cli/init-command/deployment-modes.js +0 -7
  50. package/frigg-cli/init-command/safe-path.js +0 -58
  51. package/frigg-cli/init-command/scaffold-dependencies.js +0 -202
  52. package/frigg-cli/install-command/app-definition.js +0 -174
  53. package/frigg-cli/install-command/resolve-module.js +0 -69
  54. package/frigg-cli/templates/backend/README.md +0 -267
  55. package/frigg-cli/templates/backend/env.example +0 -20
  56. package/frigg-cli/templates/backend/gitignore +0 -10
  57. package/frigg-cli/templates/backend/index.js +0 -114
  58. package/frigg-cli/templates/backend/infrastructure.js +0 -20
  59. package/frigg-cli/utils/node-version.js +0 -51
  60. package/infrastructure/__tests__/package-patterns-composition.test.js +0 -153
  61. package/infrastructure/__tests__/scaffold-default-infrastructure.test.js +0 -148
  62. package/infrastructure/domains/shared/utilities/framework-version.js +0 -14
  63. package/infrastructure/domains/shared/utilities/framework-version.test.js +0 -27
  64. package/infrastructure/domains/shared/utilities/package-patterns.js +0 -35
  65. package/infrastructure/domains/shared/utilities/package-patterns.test.js +0 -112
@@ -20,7 +20,6 @@ const {
20
20
  ValidationResult,
21
21
  } = require('../shared/base-builder');
22
22
  const IntegrationResourceResolver = require('./integration-resolver');
23
- const { toPackagePatterns } = require('../shared/utilities/package-patterns');
24
23
  const {
25
24
  createEmptyDiscoveryResult,
26
25
  ResourceOwnership,
@@ -255,70 +254,67 @@ class IntegrationBuilder extends InfrastructureBuilder {
255
254
  */
256
255
  createFunctionPackageConfig(usePrismaLayer = true, appDefinition = {}) {
257
256
  return {
258
- // osls 4 only reads package.patterns (ADR-050): excludes first, then includes.
259
- patterns: toPackagePatterns({
260
- exclude: [
261
- // Exclude AWS SDK (provided by Lambda runtime)
262
- 'node_modules/aws-sdk/**',
263
- 'node_modules/@aws-sdk/**',
264
-
265
- // Exclude Prisma (provided via Lambda Layer)
266
- ...(usePrismaLayer
267
- ? [
268
- 'node_modules/@prisma/**',
269
- 'node_modules/.prisma/**',
270
- 'node_modules/prisma/**',
271
- 'node_modules/@friggframework/core/generated/**',
272
- ]
273
- : []),
274
-
275
- ...nestedNodeModulesExcludes(appDefinition, usePrismaLayer),
276
-
277
- // Exclude build tools (not needed at runtime)
278
- 'node_modules/esbuild/**',
279
- 'node_modules/@esbuild/**',
280
- 'node_modules/typescript/**',
281
- 'node_modules/webpack/**',
282
- 'node_modules/osls/**',
283
- 'node_modules/serverless-esbuild/**',
284
- 'node_modules/serverless-jetpack/**',
285
- 'node_modules/serverless-offline/**',
286
- 'node_modules/serverless-offline-sqs/**',
287
- 'node_modules/serverless-dotenv-plugin/**',
288
- 'node_modules/serverless-kms-grants/**',
289
- // Note: DO NOT exclude serverless-http - it's a runtime dependency!
290
-
291
- // Exclude dev/test dependencies
292
- 'node_modules/@friggframework/test/**',
293
- 'node_modules/@friggframework/eslint-config/**',
294
- 'node_modules/@friggframework/prettier-config/**',
295
- 'node_modules/jest/**',
296
- 'node_modules/prettier/**',
297
- 'node_modules/eslint/**',
298
-
299
- // Exclude local dev files
300
- 'deploy.log',
301
- '.env.backup',
302
- 'docker-compose.yml',
303
- 'jest.config.js',
304
- 'jest.unit.config.js',
305
- '.eslintrc.json',
306
- '.prettierrc',
307
- '.prettierignore',
308
- '.markdownlintignore',
309
- 'package-lock.json',
310
-
311
- // Exclude development/test files (keep src/ - needed for integrations and api-modules)
312
- 'coverage/**',
313
- 'test/**',
314
- 'layers/**',
315
- // Note: DO NOT exclude src/** - handlers need src/integrations and src/api-modules at runtime
316
- '**/*.test.js',
317
- '**/*.spec.js',
318
- '**/.claude-flow/**',
319
- '**/.swarm/**',
320
- ],
321
- }),
257
+ exclude: [
258
+ // Exclude AWS SDK (provided by Lambda runtime)
259
+ 'node_modules/aws-sdk/**',
260
+ 'node_modules/@aws-sdk/**',
261
+
262
+ // Exclude Prisma (provided via Lambda Layer)
263
+ ...(usePrismaLayer
264
+ ? [
265
+ 'node_modules/@prisma/**',
266
+ 'node_modules/.prisma/**',
267
+ 'node_modules/prisma/**',
268
+ 'node_modules/@friggframework/core/generated/**',
269
+ ]
270
+ : []),
271
+
272
+ ...nestedNodeModulesExcludes(appDefinition, usePrismaLayer),
273
+
274
+ // Exclude build tools (not needed at runtime)
275
+ 'node_modules/esbuild/**',
276
+ 'node_modules/@esbuild/**',
277
+ 'node_modules/typescript/**',
278
+ 'node_modules/webpack/**',
279
+ 'node_modules/osls/**',
280
+ 'node_modules/serverless-esbuild/**',
281
+ 'node_modules/serverless-jetpack/**',
282
+ 'node_modules/serverless-offline/**',
283
+ 'node_modules/serverless-offline-sqs/**',
284
+ 'node_modules/serverless-dotenv-plugin/**',
285
+ 'node_modules/serverless-kms-grants/**',
286
+ // Note: DO NOT exclude serverless-http - it's a runtime dependency!
287
+
288
+ // Exclude dev/test dependencies
289
+ 'node_modules/@friggframework/test/**',
290
+ 'node_modules/@friggframework/eslint-config/**',
291
+ 'node_modules/@friggframework/prettier-config/**',
292
+ 'node_modules/jest/**',
293
+ 'node_modules/prettier/**',
294
+ 'node_modules/eslint/**',
295
+
296
+ // Exclude local dev files
297
+ 'deploy.log',
298
+ '.env.backup',
299
+ 'docker-compose.yml',
300
+ 'jest.config.js',
301
+ 'jest.unit.config.js',
302
+ '.eslintrc.json',
303
+ '.prettierrc',
304
+ '.prettierignore',
305
+ '.markdownlintignore',
306
+ 'package-lock.json',
307
+
308
+ // Exclude development/test files (keep src/ - needed for integrations and api-modules)
309
+ 'coverage/**',
310
+ 'test/**',
311
+ 'layers/**',
312
+ // Note: DO NOT exclude src/** - handlers need src/integrations and src/api-modules at runtime
313
+ '**/*.test.js',
314
+ '**/*.spec.js',
315
+ '**/.claude-flow/**',
316
+ '**/.swarm/**',
317
+ ],
322
318
  };
323
319
  }
324
320
 
@@ -364,6 +360,25 @@ class IntegrationBuilder extends InfrastructureBuilder {
364
360
  method: 'POST',
365
361
  },
366
362
  },
363
+ // A provider (the shared runtime for published integration definitions) also receives the
364
+ // webhooks of every type it provides at that type's own URL. HTTP API cannot match part of a
365
+ // segment, so the whole segment is a parameter; literal routes still win over it.
366
+ ...(integration.IntegrationProvider
367
+ ? [
368
+ {
369
+ httpApi: {
370
+ path: '/api/{integrationType}/webhooks',
371
+ method: 'POST',
372
+ },
373
+ },
374
+ {
375
+ httpApi: {
376
+ path: '/api/{integrationType}/webhooks/{integrationId}',
377
+ method: 'POST',
378
+ },
379
+ },
380
+ ]
381
+ : []),
367
382
  ],
368
383
  };
369
384
  console.log(` ✓ Webhook handler function defined`);
@@ -620,6 +620,28 @@ describe('IntegrationBuilder', () => {
620
620
  ]);
621
621
  });
622
622
 
623
+ it('gives a provider of integration types the webhook routes of every type it provides', async () => {
624
+ const appDefinition = {
625
+ integrations: [
626
+ { Definition: { name: 'stripe', webhooks: true } },
627
+ {
628
+ Definition: { name: 'integration-runtime', webhooks: { enabled: true } },
629
+ IntegrationProvider: { resolve: async () => null, list: async () => [] },
630
+ },
631
+ ],
632
+ };
633
+
634
+ const result = await integrationBuilder.build(appDefinition, {});
635
+
636
+ expect(result.functions['integration-runtimeWebhook'].events.map((e) => e.httpApi.path)).toEqual([
637
+ '/api/integration-runtime-integration/webhooks',
638
+ '/api/integration-runtime-integration/webhooks/{integrationId}',
639
+ '/api/{integrationType}/webhooks',
640
+ '/api/{integrationType}/webhooks/{integrationId}',
641
+ ]);
642
+ expect(result.functions.stripeWebhook.events).toHaveLength(2);
643
+ });
644
+
623
645
  it('emits a dedicated per-binding function (namespaced) for Tier 3 extension routes; the main handler keeps only {proxy+}', async () => {
624
646
  const appDefinition = {
625
647
  integrations: [
@@ -886,8 +908,8 @@ describe('IntegrationBuilder', () => {
886
908
  const result = await integrationBuilder.build(appDefinition, {});
887
909
 
888
910
  expect(result.functions.testWebhook.package).toBeDefined();
889
- expect(result.functions.testWebhook.package.patterns).toContain('!node_modules/aws-sdk/**');
890
- expect(result.functions.testWebhook.package.patterns).toContain('!node_modules/@prisma/**');
911
+ expect(result.functions.testWebhook.package.exclude).toContain('node_modules/aws-sdk/**');
912
+ expect(result.functions.testWebhook.package.exclude).toContain('node_modules/@prisma/**');
891
913
  });
892
914
  });
893
915
 
@@ -982,8 +1004,8 @@ describe('IntegrationBuilder', () => {
982
1004
  expect(result.functions.asana.layers).toBeUndefined();
983
1005
  expect(result.functions.asanaQueueWorker.layers).toBeUndefined();
984
1006
  expect(result.functions.asanaWebhook.layers).toBeUndefined();
985
- expect(result.functions.asana.package.patterns).not.toEqual(
986
- expect.arrayContaining(['!node_modules/@prisma/**'])
1007
+ expect(result.functions.asana.package.exclude).not.toEqual(
1008
+ expect.arrayContaining(['node_modules/@prisma/**'])
987
1009
  );
988
1010
  });
989
1011
  });
@@ -1091,14 +1113,14 @@ describe('IntegrationBuilder nested node_modules (lambda.keepNestedNodeModules)'
1091
1113
  ...(lambda && { lambda }),
1092
1114
  });
1093
1115
  const packagedFunctions = (result) =>
1094
- Object.values(result.functions).filter((fn) => fn.package?.patterns);
1116
+ Object.values(result.functions).filter((fn) => fn.package?.exclude);
1095
1117
 
1096
1118
  it('excludes every nested node_modules by default', async () => {
1097
1119
  const result = await new IntegrationBuilder().build(appDefinition(), {});
1098
1120
 
1099
1121
  expect(packagedFunctions(result).length).toBeGreaterThan(0);
1100
1122
  for (const fn of packagedFunctions(result)) {
1101
- expect(fn.package.patterns).toContain('!node_modules/**/node_modules/**');
1123
+ expect(fn.package.exclude).toContain('node_modules/**/node_modules/**');
1102
1124
  }
1103
1125
  });
1104
1126
 
@@ -1109,73 +1131,8 @@ describe('IntegrationBuilder nested node_modules (lambda.keepNestedNodeModules)'
1109
1131
  );
1110
1132
 
1111
1133
  for (const fn of packagedFunctions(result)) {
1112
- expect(fn.package.patterns).not.toContain('!node_modules/**/node_modules/**');
1113
- expect(fn.package.patterns).toContain('!node_modules/**/node_modules/@friggframework/**');
1114
- }
1115
- });
1116
- });
1117
-
1118
- describe('IntegrationBuilder function package patterns (ADR-050)', () => {
1119
- const appDefinition = {
1120
- integrations: [
1121
- {
1122
- Definition: {
1123
- name: 'hubspot',
1124
- webhooks: true,
1125
- extensions: {
1126
- hubspotWebhooks: {
1127
- extension: {
1128
- name: 'hubspot-webhooks',
1129
- routes: [
1130
- {
1131
- path: '/webhooks',
1132
- method: 'POST',
1133
- event: 'HUBSPOT_WEBHOOK_RECEIVED',
1134
- },
1135
- ],
1136
- events: {
1137
- HUBSPOT_WEBHOOK_RECEIVED: {
1138
- type: 'LIFE_CYCLE_EVENT',
1139
- handler: () => {},
1140
- },
1141
- },
1142
- },
1143
- handlers: {},
1144
- },
1145
- },
1146
- },
1147
- },
1148
- ],
1149
- };
1150
-
1151
- it('emits package.patterns, never include/exclude, on every function it packages', async () => {
1152
- const result = await new IntegrationBuilder().build(appDefinition, {});
1153
-
1154
- const expected = [
1155
- 'hubspot',
1156
- 'hubspotWebhook',
1157
- 'hubspot__hubspotWebhooks',
1158
- 'hubspotQueueWorker',
1159
- 'dlqProcessor',
1160
- ];
1161
- for (const name of expected) {
1162
- const pkg = result.functions[name]?.package;
1163
- expect(pkg).toBeDefined();
1164
- expect(pkg).not.toHaveProperty('include');
1165
- expect(pkg).not.toHaveProperty('exclude');
1166
- // Integration functions only exclude, so every pattern is negated.
1167
- expect(pkg.patterns.length).toBeGreaterThan(0);
1168
- expect(pkg.patterns.every((p) => p.startsWith('!'))).toBe(true);
1169
- expect(pkg.patterns).toEqual(
1170
- expect.arrayContaining([
1171
- '!node_modules/aws-sdk/**',
1172
- '!node_modules/@prisma/**',
1173
- '!node_modules/**/node_modules/**',
1174
- '!node_modules/osls/**',
1175
- '!test/**',
1176
- ])
1177
- );
1178
- expect(pkg.patterns).not.toContain('!src/**');
1134
+ expect(fn.package.exclude).not.toContain('node_modules/**/node_modules/**');
1135
+ expect(fn.package.exclude).toContain('node_modules/**/node_modules/@friggframework/**');
1179
1136
  }
1180
1137
  });
1181
1138
  });
@@ -0,0 +1,114 @@
1
+ /**
2
+ * Trigger Builder (ADR-055 §2)
3
+ *
4
+ * Domain Layer - Hexagonal Architecture
5
+ *
6
+ * One function per integration that declares recurring triggers: `{integration}Triggers`, with the Prisma layer.
7
+ * It gets one serverless `schedule` event per distinct tick of the integration's `every` / `cron` triggers (and of
8
+ * the triggers it resolves per installation, `Definition.instanceTriggers`), plus one EventBridge Scheduler event per
9
+ * `at` trigger, with its time zone. Each tick pages through the ENABLED installations and queues the runs that are
10
+ * due (core: integrations/use-cases/run-trigger-tick.js). Nothing is created per installation, and these resources
11
+ * change only when a Definition does, so they live in CloudFormation and leave with the stack.
12
+ *
13
+ * IAM, and why this builder adds none:
14
+ * - A `rate()` tick is an EventBridge rule; the framework adds the Lambda permission for it.
15
+ * - An `at` trigger uses `method: scheduler`; the framework adds `scheduler.amazonaws.com` to the function role's
16
+ * trust policy and `lambda:InvokeFunction` on this function to it.
17
+ * - The tick reads the integration table (the Prisma layer and the provider's database access, like the queue
18
+ * worker) and sends to its own `<Name>Queue`, which the provider-level `sqs:SendMessage(Batch)` statement on
19
+ * `${service}--${stage}-*Queue` already allows. It never calls EventBridge Scheduler at runtime, so it needs none
20
+ * of SchedulerBuilder's scheduler:* permissions (those stay for one-time jobs).
21
+ */
22
+
23
+ const { InfrastructureBuilder, ValidationResult } = require('../shared/base-builder');
24
+ const { IntegrationBuilder } = require('../integration/integration-builder');
25
+ const {
26
+ hasTriggers,
27
+ provisioningOf,
28
+ durationOf,
29
+ } = require('@friggframework/core/integrations/triggers');
30
+
31
+ /** EventBridge rate() for a tick in minutes: rate(1 minute), rate(5 minutes), rate(1 hour), rate(2 hours), rate(1 day). */
32
+ function rateExpression(minutes) {
33
+ const [count, unit] =
34
+ minutes % 1440 === 0 ? [minutes / 1440, 'day'] : minutes % 60 === 0 ? [minutes / 60, 'hour'] : [minutes, 'minute'];
35
+ return `rate(${count} ${unit}${count === 1 ? '' : 's'})`;
36
+ }
37
+
38
+ /** The function name the trigger tick for an integration runs as. Wire contract with function-environments.js. */
39
+ function triggerFunctionName(integrationName) {
40
+ return `${integrationName}Triggers`;
41
+ }
42
+
43
+ class TriggerBuilder extends InfrastructureBuilder {
44
+ constructor() {
45
+ super();
46
+ this.name = 'TriggerBuilder';
47
+ }
48
+
49
+ shouldExecute(appDefinition) {
50
+ return Array.isArray(appDefinition.integrations) && appDefinition.integrations.some((i) => hasTriggers(i));
51
+ }
52
+
53
+ getDependencies() {
54
+ return ['IntegrationBuilder']; // the tick sends to the integration queue
55
+ }
56
+
57
+ validate(appDefinition) {
58
+ const result = new ValidationResult();
59
+ for (const integration of appDefinition.integrations || []) {
60
+ if (!hasTriggers(integration)) continue;
61
+ try {
62
+ provisioningOf(integration);
63
+ } catch (error) {
64
+ result.addError(error.message);
65
+ }
66
+ }
67
+ return result;
68
+ }
69
+
70
+ async build(appDefinition) {
71
+ console.log(`\n[${this.name}] Configuring recurring triggers...`);
72
+ const result = { functions: {}, resources: {}, environment: {}, custom: {}, iamStatements: [] };
73
+ const usePrismaLayer = appDefinition.usePrismaLambdaLayer !== false;
74
+ const functionPackageConfig = new IntegrationBuilder().createFunctionPackageConfig(usePrismaLayer, appDefinition);
75
+
76
+ for (const integration of appDefinition.integrations || []) {
77
+ if (!hasTriggers(integration)) continue;
78
+ const name = integration.Definition.name;
79
+ const { ticks, at } = provisioningOf(integration);
80
+ const events = [
81
+ ...ticks.map((minutes) => ({
82
+ schedule: {
83
+ rate: [rateExpression(minutes)],
84
+ // The rule's own firing time goes in, so a repeated delivery falls in the same tick slot.
85
+ inputTransformer: {
86
+ inputPathsMap: { time: '$.time' },
87
+ inputTemplate: `{"tick":"${durationOf(minutes)}","time":<time>}`,
88
+ },
89
+ },
90
+ })),
91
+ ...at.map((t) => ({
92
+ schedule: {
93
+ method: 'scheduler',
94
+ rate: [t.at],
95
+ ...(t.timezone ? { timezone: t.timezone } : {}),
96
+ input: { at: t.name, time: '<aws.scheduler.scheduled-time>' },
97
+ },
98
+ })),
99
+ ];
100
+ result.functions[triggerFunctionName(name)] = {
101
+ handler: `node_modules/@friggframework/core/handlers/workers/integration-triggers.handlers.${name}.tick`,
102
+ skipEsbuild: true, // Nested exports in node_modules - skip esbuild bundling
103
+ package: functionPackageConfig,
104
+ ...(usePrismaLayer && { layers: [{ Ref: 'PrismaLambdaLayer' }] }), // reads the integration table
105
+ timeout: 300, // a tick past its deadline hands its page cursor to the queue
106
+ events,
107
+ };
108
+ console.log(` ✓ ${triggerFunctionName(name)}: ticks ${ticks.map(durationOf).join(', ') || 'none'}; at ${at.map((t) => t.name).join(', ') || 'none'}`);
109
+ }
110
+ return result;
111
+ }
112
+ }
113
+
114
+ module.exports = { TriggerBuilder, rateExpression, triggerFunctionName };
@@ -0,0 +1,82 @@
1
+ /**
2
+ * TriggerBuilder (ADR-055 §2): one tick function per integration with triggers, one schedule per distinct tick and
3
+ * one per `at` trigger, never anything per installation.
4
+ */
5
+ const { TriggerBuilder, rateExpression } = require('./trigger-builder');
6
+ const { applyFunctionEnvironments } = require('../shared/function-environments');
7
+
8
+ const poll = { every: '15m', handler: 'poll' };
9
+
10
+ describe('TriggerBuilder', () => {
11
+ beforeEach(() => { jest.spyOn(console, 'log').mockImplementation(() => {}); });
12
+ afterEach(() => { jest.restoreAllMocks(); });
13
+
14
+ it('runs when, and only when, an integration declares triggers (code or per installation)', () => {
15
+ const b = new TriggerBuilder();
16
+ expect(b.shouldExecute({ integrations: [{ Definition: { name: 'a', triggers: { poll } } }] })).toBe(true);
17
+ expect(b.shouldExecute({ integrations: [{ Definition: { name: 'dd', instanceTriggers: { tick: '5m' } } }] })).toBe(true);
18
+ expect(b.shouldExecute({ integrations: [{ Definition: { name: 'a', webhooks: true } }] })).toBe(false);
19
+ expect(b.shouldExecute({ integrations: [{ Definition: { name: 'a', triggers: {} } }] })).toBe(false);
20
+ expect(b.shouldExecute({})).toBe(false);
21
+ expect(b.getDependencies()).toEqual(['IntegrationBuilder']);
22
+ });
23
+
24
+ it('writes rate() in the largest whole unit EventBridge accepts', () => {
25
+ expect([1, 5, 60, 90, 120, 1440].map(rateExpression)).toEqual(['rate(1 minute)', 'rate(5 minutes)', 'rate(1 hour)', 'rate(90 minutes)', 'rate(2 hours)', 'rate(1 day)']);
26
+ });
27
+
28
+ it('emits one function per integration: a schedule per distinct tick and one per at trigger, with its zone', async () => {
29
+ const appDefinition = {
30
+ integrations: [
31
+ { Definition: { name: 'crm', triggers: {
32
+ poll,
33
+ sync: { every: '1h', configurable: { min: '15m' }, handler: 'sync' }, // 15m tick: same schedule as poll
34
+ digest: { cron: '0 8 * * 1-5', timezone: 'America/New_York', handler: 'digest' }, // 5m
35
+ nightly: { at: 'cron(0 3 * * ? *)', timezone: 'Europe/London', handler: 'nightly' },
36
+ } } },
37
+ { Definition: { name: 'plain' } },
38
+ ],
39
+ };
40
+ const result = await new TriggerBuilder().build(appDefinition, {});
41
+ expect(Object.keys(result.functions)).toEqual(['crmTriggers']);
42
+ const fn = result.functions.crmTriggers;
43
+ expect(fn).toMatchObject({
44
+ handler: 'node_modules/@friggframework/core/handlers/workers/integration-triggers.handlers.crm.tick',
45
+ skipEsbuild: true,
46
+ layers: [{ Ref: 'PrismaLambdaLayer' }],
47
+ timeout: 300,
48
+ });
49
+ expect(fn.events).toEqual([
50
+ { schedule: { rate: ['rate(5 minutes)'], inputTransformer: { inputPathsMap: { time: '$.time' }, inputTemplate: '{"tick":"5m","time":<time>}' } } },
51
+ { schedule: { rate: ['rate(15 minutes)'], inputTransformer: { inputPathsMap: { time: '$.time' }, inputTemplate: '{"tick":"15m","time":<time>}' } } },
52
+ { schedule: { method: 'scheduler', rate: ['cron(0 3 * * ? *)'], timezone: 'Europe/London', input: { at: 'nightly', time: '<aws.scheduler.scheduled-time>' } } },
53
+ ]);
54
+ // The tick never calls EventBridge Scheduler at runtime: no scheduler IAM, no resources.
55
+ expect(result.iamStatements).toEqual([]);
56
+ expect(result.resources).toEqual({});
57
+ // The inputTemplate is valid JSON once EventBridge fills <time>.
58
+ expect(JSON.parse(fn.events[0].schedule.inputTransformer.inputTemplate.replace('<time>', '"2026-10-19T12:00:00Z"'))).toEqual({ tick: '5m', time: '2026-10-19T12:00:00Z' });
59
+ });
60
+
61
+ it('leaves the Prisma layer off when the app bundles Prisma', async () => {
62
+ const result = await new TriggerBuilder().build({ usePrismaLambdaLayer: false, integrations: [{ Definition: { name: 'crm', triggers: { poll } } }] }, {});
63
+ expect(result.functions.crmTriggers.layers).toBeUndefined();
64
+ });
65
+
66
+ it('fails validation, naming the integration, when a trigger cannot be provisioned', () => {
67
+ const v = new TriggerBuilder().validate({ integrations: [{ Definition: { name: 'crm', triggers: { bad: { every: 'often', handler: 'x' } } } }] });
68
+ expect(v.valid).toBe(false);
69
+ expect(v.errors[0]).toMatch(/Integration "crm" declares invalid triggers: bad: every "often"/);
70
+ });
71
+
72
+ it('scoped env: the queue URL reaches the tick function, through the shared function-name contract', async () => {
73
+ const { IntegrationBuilder } = require('../integration/integration-builder');
74
+ const integration = { Definition: { name: 'crm', triggers: { poll } } };
75
+ const ib = new IntegrationBuilder();
76
+ const result = { functionEnvironments: {}, environment: {} };
77
+ ib.setQueueUrlEnvironment('crm', { Ref: 'CrmQueue' }, result, { lambda: { scopedEnvironment: true }, integrations: [integration] });
78
+ const functions = { auth: {}, crm: {}, crmQueueWorker: {}, ...(await new TriggerBuilder().build({ integrations: [integration] }, {})).functions };
79
+ applyFunctionEnvironments(functions, result.functionEnvironments);
80
+ expect(functions.crmTriggers.environment).toEqual({ CRM_QUEUE_URL: { Ref: 'CrmQueue' } });
81
+ });
82
+ });