Skip to content

Commit 754bd47

Browse files
fix(cdk): optimize test suite via shared CDK template synthesis (#195)
* perf(cdk): share CDK templates in task-api tests for 61% faster suite Refactors task-api.test.ts to synthesize CDK templates once per describe block via beforeAll instead of per-test. Reduces CDK synth calls from 41 to 11 while preserving all 40 test assertions unchanged. Before: 371s total Jest / task-api.test 370s wall clock After: 145s total Jest / task-api.test 89s wall clock Closes #194 Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> * perf(cdk): share CDK templates in orchestrator and ECS cluster tests Applies the same beforeAll template-sharing pattern to: - task-orchestrator.test.ts: 33 → 12 synths (4 shared + 8 unique) - ecs-agent-cluster.test.ts: 10 → 2 synths (1 shared + 1 unique) Combined with task-api.test.ts, total CDK synths across the three heaviest suites drop from 84 to 25 (70% fewer). Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> --------- Co-authored-by: bgagent <345885+scottschreckengaust@users.noreply.github.com> Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent dc480dd commit 754bd47

3 files changed

Lines changed: 149 additions & 236 deletions

File tree

cdk/test/constructs/ecs-agent-cluster.test.ts

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,14 @@ function createStack(overrides?: { memoryId?: string }): { stack: Stack; templat
6666
}
6767

6868
describe('EcsAgentCluster construct', () => {
69+
let baseTemplate: Template;
70+
71+
beforeAll(() => {
72+
baseTemplate = createStack().template;
73+
});
74+
6975
test('creates an ECS Cluster with container insights', () => {
70-
const { template } = createStack();
71-
template.hasResourceProperties('AWS::ECS::Cluster', {
76+
baseTemplate.hasResourceProperties('AWS::ECS::Cluster', {
7277
ClusterSettings: Match.arrayWith([
7378
Match.objectLike({
7479
Name: 'containerInsights',
@@ -79,8 +84,7 @@ describe('EcsAgentCluster construct', () => {
7984
});
8085

8186
test('creates a Fargate task definition with 2 vCPU and 4 GB', () => {
82-
const { template } = createStack();
83-
template.hasResourceProperties('AWS::ECS::TaskDefinition', {
87+
baseTemplate.hasResourceProperties('AWS::ECS::TaskDefinition', {
8488
Cpu: '2048',
8589
Memory: '4096',
8690
RequiresCompatibilities: ['FARGATE'],
@@ -92,8 +96,7 @@ describe('EcsAgentCluster construct', () => {
9296
});
9397

9498
test('creates a security group with TCP 443 egress only', () => {
95-
const { template } = createStack();
96-
template.hasResourceProperties('AWS::EC2::SecurityGroup', {
99+
baseTemplate.hasResourceProperties('AWS::EC2::SecurityGroup', {
97100
GroupDescription: 'ECS Agent Tasks - egress TCP 443 only',
98101
SecurityGroupEgress: Match.arrayWith([
99102
Match.objectLike({
@@ -107,20 +110,17 @@ describe('EcsAgentCluster construct', () => {
107110
});
108111

109112
test('creates a CloudWatch log group with 3-month retention and CDK-generated name', () => {
110-
const { template } = createStack();
111-
template.hasResourceProperties('AWS::Logs::LogGroup', {
113+
baseTemplate.hasResourceProperties('AWS::Logs::LogGroup', {
112114
RetentionInDays: 90,
113115
});
114-
// Verify no hardcoded log group name — CDK auto-generates a unique name
115-
const logGroups = template.findResources('AWS::Logs::LogGroup');
116+
const logGroups = baseTemplate.findResources('AWS::Logs::LogGroup');
116117
for (const [, lg] of Object.entries(logGroups)) {
117118
expect((lg as any).Properties).not.toHaveProperty('LogGroupName');
118119
}
119120
});
120121

121122
test('task role has DynamoDB read/write permissions', () => {
122-
const { template } = createStack();
123-
template.hasResourceProperties('AWS::IAM::Policy', {
123+
baseTemplate.hasResourceProperties('AWS::IAM::Policy', {
124124
PolicyDocument: {
125125
Statement: Match.arrayWith([
126126
Match.objectLike({
@@ -136,8 +136,7 @@ describe('EcsAgentCluster construct', () => {
136136
});
137137

138138
test('task role has Secrets Manager read permission', () => {
139-
const { template } = createStack();
140-
template.hasResourceProperties('AWS::IAM::Policy', {
139+
baseTemplate.hasResourceProperties('AWS::IAM::Policy', {
141140
PolicyDocument: {
142141
Statement: Match.arrayWith([
143142
Match.objectLike({
@@ -152,8 +151,7 @@ describe('EcsAgentCluster construct', () => {
152151
});
153152

154153
test('task role has Bedrock InvokeModel permissions', () => {
155-
const { template } = createStack();
156-
template.hasResourceProperties('AWS::IAM::Policy', {
154+
baseTemplate.hasResourceProperties('AWS::IAM::Policy', {
157155
PolicyDocument: {
158156
Statement: Match.arrayWith([
159157
Match.objectLike({
@@ -170,8 +168,7 @@ describe('EcsAgentCluster construct', () => {
170168
});
171169

172170
test('container has required environment variables', () => {
173-
const { template } = createStack();
174-
template.hasResourceProperties('AWS::ECS::TaskDefinition', {
171+
baseTemplate.hasResourceProperties('AWS::ECS::TaskDefinition', {
175172
ContainerDefinitions: Match.arrayWith([
176173
Match.objectLike({
177174
Name: 'AgentContainer',

0 commit comments

Comments
 (0)