|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const fs = require('node:fs'); |
| 4 | +const path = require('node:path'); |
| 5 | +const expect = require('chai').expect; |
| 6 | + |
| 7 | +const templatePath = path.join(__dirname, '.serverless', 'cloudformation-template-update-stack.json'); |
| 8 | + |
| 9 | +const findStateMachine = (resources, namePrefix) => { |
| 10 | + const entry = Object.values(resources).find( |
| 11 | + (r) => r.Type === 'AWS::StepFunctions::StateMachine' |
| 12 | + && typeof r.Properties.StateMachineName === 'string' |
| 13 | + && r.Properties.StateMachineName.startsWith(namePrefix), |
| 14 | + ); |
| 15 | + return entry || null; |
| 16 | +}; |
| 17 | + |
| 18 | +const parseDefinition = (stateMachine) => { |
| 19 | + const ds = stateMachine.Properties.DefinitionString; |
| 20 | + if (typeof ds === 'string') return JSON.parse(ds); |
| 21 | + const sub = ds['Fn::Sub']; |
| 22 | + return JSON.parse(Array.isArray(sub) ? sub[0] : sub); |
| 23 | +}; |
| 24 | + |
| 25 | +describe('configure-task-timeouts fixture — CloudFormation template', () => { |
| 26 | + let resources; |
| 27 | + |
| 28 | + before(() => { |
| 29 | + const template = JSON.parse(fs.readFileSync(templatePath, 'utf8')); |
| 30 | + resources = template.Resources; |
| 31 | + }); |
| 32 | + |
| 33 | + describe('autoTimeoutsMachine (configureTaskTimeouts: true)', () => { |
| 34 | + let definition; |
| 35 | + |
| 36 | + before(() => { |
| 37 | + const sm = findStateMachine(resources, 'integration-configure-task-timeouts-'); |
| 38 | + expect(sm, 'auto-timeouts state machine should exist').to.not.equal(null); |
| 39 | + definition = parseDefinition(sm); |
| 40 | + }); |
| 41 | + |
| 42 | + it('injects TimeoutSeconds for legacy direct invoke (Resource: Fn::GetAtt) using function.timeout', () => { |
| 43 | + expect(definition.States.DirectInvoke.TimeoutSeconds).to.equal(12); |
| 44 | + }); |
| 45 | + |
| 46 | + it('injects TimeoutSeconds for service-integration lambda:invoke using function.timeout', () => { |
| 47 | + expect(definition.States.ServiceIntegration.TimeoutSeconds).to.equal(90); |
| 48 | + }); |
| 49 | + |
| 50 | + it('preserves a user-set TimeoutSeconds and does not overwrite it', () => { |
| 51 | + expect(definition.States.UserSetTimeout.TimeoutSeconds).to.equal(5); |
| 52 | + }); |
| 53 | + |
| 54 | + it('falls back to the Serverless Framework default (6s) when the function has no timeout configured', () => { |
| 55 | + const nested = definition.States.ParallelStep.Branches[0].States.NestedTask; |
| 56 | + expect(nested.TimeoutSeconds).to.equal(6); |
| 57 | + }); |
| 58 | + }); |
| 59 | + |
| 60 | + describe('backCompatMachine (configureTaskTimeouts unset)', () => { |
| 61 | + it('does not inject TimeoutSeconds when the flag is off', () => { |
| 62 | + const sm = findStateMachine(resources, 'integration-back-compat-task-timeouts-'); |
| 63 | + expect(sm, 'back-compat state machine should exist').to.not.equal(null); |
| 64 | + const definition = parseDefinition(sm); |
| 65 | + expect(definition.States.NoInjection.TimeoutSeconds).to.equal(undefined); |
| 66 | + }); |
| 67 | + }); |
| 68 | +}); |
0 commit comments