grada-run 0.0.2 → 0.32.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 (231) hide show
  1. package/.github/workflows/deploy-docs.yml +37 -0
  2. package/.github/workflows/iac-validation.yml +303 -0
  3. package/.github/workflows/publish.yml +68 -0
  4. package/.github/workflows/sync-bedrock-models.yml +57 -0
  5. package/.github/workflows/test.yml +43 -0
  6. package/.muserules +31 -0
  7. package/LICENSE +21 -0
  8. package/README.md +193 -3
  9. package/apps/docs/.astro/collections/docs.schema.json +644 -0
  10. package/apps/docs/.astro/content-assets.mjs +4 -0
  11. package/apps/docs/.astro/content-modules.mjs +4 -0
  12. package/apps/docs/.astro/content.d.ts +179 -0
  13. package/apps/docs/.astro/data-store.json +1 -0
  14. package/apps/docs/.astro/dev.json +14 -0
  15. package/apps/docs/.astro/settings.json +5 -0
  16. package/apps/docs/.astro/types.d.ts +2 -0
  17. package/apps/docs/astro.config.mjs +97 -0
  18. package/apps/docs/package.json +17 -0
  19. package/apps/docs/src/content/docs/adrs/0001-s3-native-state-locking.md +37 -0
  20. package/apps/docs/src/content/docs/adrs/0002-eject-mechanism-pure-iac.md +39 -0
  21. package/apps/docs/src/content/docs/adrs/0003-sync-ai-context-strategy.md +48 -0
  22. package/apps/docs/src/content/docs/adrs/0004-iac-driven-diagnostic-context.md +37 -0
  23. package/apps/docs/src/content/docs/adrs/0005-ecs-fargate-alb-runtime-target.md +38 -0
  24. package/apps/docs/src/content/docs/adrs/0006-github-oidc-no-stored-keys.md +37 -0
  25. package/apps/docs/src/content/docs/adrs/0007-framework-detection-with-fallback.md +37 -0
  26. package/apps/docs/src/content/docs/adrs/0008-secrets-names-in-git-values-in-aws.md +37 -0
  27. package/apps/docs/src/content/docs/adrs/0009-regenerate-with-backup-on-rerun.md +37 -0
  28. package/apps/docs/src/content/docs/adrs/0010-advisory-only-security-scans.md +37 -0
  29. package/apps/docs/src/content/docs/cli/add.md +84 -0
  30. package/apps/docs/src/content/docs/cli/apply.md +32 -0
  31. package/apps/docs/src/content/docs/cli/db.md +200 -0
  32. package/apps/docs/src/content/docs/cli/destroy.md +31 -0
  33. package/apps/docs/src/content/docs/cli/diagnose.md +37 -0
  34. package/apps/docs/src/content/docs/cli/doctor.md +28 -0
  35. package/apps/docs/src/content/docs/cli/domain.md +57 -0
  36. package/apps/docs/src/content/docs/cli/drift.md +40 -0
  37. package/apps/docs/src/content/docs/cli/eject.md +29 -0
  38. package/apps/docs/src/content/docs/cli/exec.md +49 -0
  39. package/apps/docs/src/content/docs/cli/gc.md +37 -0
  40. package/apps/docs/src/content/docs/cli/init.md +72 -0
  41. package/apps/docs/src/content/docs/cli/logs.md +39 -0
  42. package/apps/docs/src/content/docs/cli/rollback.md +51 -0
  43. package/apps/docs/src/content/docs/cli/secrets.md +73 -0
  44. package/apps/docs/src/content/docs/cli/sleep.md +53 -0
  45. package/apps/docs/src/content/docs/cli/status.md +34 -0
  46. package/apps/docs/src/content/docs/cli/sync-ai.md +27 -0
  47. package/apps/docs/src/content/docs/guides/architecture.md +87 -0
  48. package/apps/docs/src/content/docs/guides/aws-credentials.md +72 -0
  49. package/apps/docs/src/content/docs/guides/background-workers.md +45 -0
  50. package/apps/docs/src/content/docs/guides/cicd-pipeline.md +64 -0
  51. package/apps/docs/src/content/docs/guides/database-connections.md +64 -0
  52. package/apps/docs/src/content/docs/guides/docker-compose.md +37 -0
  53. package/apps/docs/src/content/docs/guides/dockerfiles.md +46 -0
  54. package/apps/docs/src/content/docs/guides/ephemeral-pr-previews.md +39 -0
  55. package/apps/docs/src/content/docs/guides/examples.md +50 -0
  56. package/apps/docs/src/content/docs/guides/frameworks.md +88 -0
  57. package/apps/docs/src/content/docs/guides/headless.md +75 -0
  58. package/apps/docs/src/content/docs/guides/quickstart.md +52 -0
  59. package/apps/docs/src/content/docs/guides/rerun-init.md +43 -0
  60. package/apps/docs/src/content/docs/guides/secrets-management.md +83 -0
  61. package/apps/docs/src/content/docs/guides/understanding-your-bill.md +63 -0
  62. package/apps/docs/src/content/docs/index.mdx +103 -0
  63. package/apps/docs/src/content/docs/migrations/astro-vercel-to-aws.md +55 -0
  64. package/apps/docs/src/content/docs/migrations/heroku-procfile-to-aws.md +41 -0
  65. package/apps/docs/src/content/docs/migrations/nextjs-vercel-to-aws.md +51 -0
  66. package/apps/docs/src/content/docs/migrations/sveltekit-vercel-to-aws.md +63 -0
  67. package/apps/docs/src/content/docs/roadmap.md +97 -0
  68. package/apps/docs/src/content/docs/testing-strategy.md +32 -0
  69. package/apps/docs/src/content.config.ts +7 -0
  70. package/apps/docs/src/custom.css +14 -0
  71. package/apps/docs/tsconfig.json +6 -0
  72. package/bin/cli.js +140 -0
  73. package/package.json +105 -7
  74. package/scripts/sync-bedrock-models.js +22 -0
  75. package/scripts/test-iac.js +261 -0
  76. package/specs/add-redis-sqs-bedrock.md +128 -0
  77. package/specs/add-storage-dynamodb.md +106 -0
  78. package/specs/bedrock-model-catalog.md +131 -0
  79. package/specs/ci-pipeline.md +17 -0
  80. package/specs/cost-transparency.md +115 -0
  81. package/specs/custom-domains-and-ses.md +153 -0
  82. package/specs/database-suite-expansion.md +151 -0
  83. package/specs/db-connect.md +69 -0
  84. package/specs/db-lifecycle-migrations.md +159 -0
  85. package/specs/dependency-aware-init.md +176 -0
  86. package/specs/deploy-stack-to-grada-run-rebrand.md +45 -0
  87. package/specs/deployment-safety.md +170 -0
  88. package/specs/diagnose.md +16 -0
  89. package/specs/docs-hub.md +16 -0
  90. package/specs/dx-polish.md +46 -0
  91. package/specs/exec.md +25 -0
  92. package/specs/finops-cron-drift.md +161 -0
  93. package/specs/gc.md +26 -0
  94. package/specs/integration-suite.md +16 -0
  95. package/specs/logs.md +32 -0
  96. package/specs/rollback-live-polling.md +40 -0
  97. package/specs/secrets-pull-audit.md +51 -0
  98. package/specs/serverless-lambda-target.md +133 -0
  99. package/specs/status.md +31 -0
  100. package/specs/telemetry-and-spawn-hardening.md +69 -0
  101. package/specs/telemetry-hardening.md +35 -0
  102. package/src/commands/add.js +1111 -0
  103. package/src/commands/apply.js +214 -0
  104. package/src/commands/db/backup.js +229 -0
  105. package/src/commands/db/connect.js +304 -0
  106. package/src/commands/db/enable-vector.js +344 -0
  107. package/src/commands/db/import.js +604 -0
  108. package/src/commands/db/migrate.js +477 -0
  109. package/src/commands/db/restore.js +361 -0
  110. package/src/commands/db.js +87 -0
  111. package/src/commands/destroy.js +217 -0
  112. package/src/commands/diagnose.js +460 -0
  113. package/src/commands/doctor.js +109 -0
  114. package/src/commands/domain.js +685 -0
  115. package/src/commands/drift.js +243 -0
  116. package/src/commands/eject.js +127 -0
  117. package/src/commands/exec.js +222 -0
  118. package/src/commands/gc.js +250 -0
  119. package/src/commands/init.js +649 -0
  120. package/src/commands/logs.js +256 -0
  121. package/src/commands/rollback.js +323 -0
  122. package/src/commands/secrets.js +485 -0
  123. package/src/commands/sleep.js +347 -0
  124. package/src/commands/status.js +309 -0
  125. package/src/commands/sync-ai.js +115 -0
  126. package/src/commands/wake.js +337 -0
  127. package/src/core/parser.js +126 -0
  128. package/src/core/telemetry.js +244 -0
  129. package/src/data/bedrock-models.json +896 -0
  130. package/src/utils/addons.js +126 -0
  131. package/src/utils/ai-rules.js +59 -0
  132. package/src/utils/args.js +91 -0
  133. package/src/utils/aws.js +178 -0
  134. package/src/utils/backup.js +69 -0
  135. package/src/utils/bedrock-catalog.js +511 -0
  136. package/src/utils/capabilities.js +500 -0
  137. package/src/utils/command.js +65 -0
  138. package/src/utils/db-tunnel.js +164 -0
  139. package/src/utils/detector.js +298 -0
  140. package/src/utils/dockerCompose.js +65 -0
  141. package/src/utils/domains.js +73 -0
  142. package/src/utils/ecs-runner.js +289 -0
  143. package/src/utils/ecs.js +92 -0
  144. package/src/utils/frameworks.js +55 -0
  145. package/src/utils/generator.js +527 -0
  146. package/src/utils/hcl.js +426 -0
  147. package/src/utils/lambda-ecr.js +185 -0
  148. package/src/utils/prompts.js +278 -0
  149. package/src/utils/rds.js +131 -0
  150. package/src/utils/resolvers.js +174 -0
  151. package/src/utils/sleep-state.js +140 -0
  152. package/src/utils/sleep-targets.js +139 -0
  153. package/src/utils/system.js +42 -0
  154. package/src/utils/terraform.js +70 -0
  155. package/src/utils/visualizer.js +381 -0
  156. package/src/utils/warnings.js +49 -0
  157. package/templates/README.md +150 -0
  158. package/templates/docker/django.Dockerfile +40 -0
  159. package/templates/docker/go.Dockerfile +23 -0
  160. package/templates/docker/nestjs.Dockerfile +33 -0
  161. package/templates/docker/nextjs.Dockerfile +55 -0
  162. package/templates/docker/node.Dockerfile +24 -0
  163. package/templates/docker/nuxt.Dockerfile +47 -0
  164. package/templates/docker/python.Dockerfile +38 -0
  165. package/templates/docker/rails.Dockerfile +59 -0
  166. package/templates/docker/static.Dockerfile +32 -0
  167. package/templates/docker/svelte.Dockerfile +52 -0
  168. package/templates/github/deploy-lambda.yml +120 -0
  169. package/templates/github/deploy.yml +138 -0
  170. package/templates/github/drift.yml +112 -0
  171. package/templates/github/preview-lambda.yml +86 -0
  172. package/templates/github/preview.yml +69 -0
  173. package/templates/github/teardown.yml +43 -0
  174. package/templates/terraform/addons/bedrock.tf +34 -0
  175. package/templates/terraform/addons/cron-lambda.tf +78 -0
  176. package/templates/terraform/addons/cron.tf +101 -0
  177. package/templates/terraform/addons/dynamodb.tf +73 -0
  178. package/templates/terraform/addons/redis.tf +64 -0
  179. package/templates/terraform/addons/s3.tf +143 -0
  180. package/templates/terraform/addons/ses.tf +73 -0
  181. package/templates/terraform/addons/sqs.tf +67 -0
  182. package/templates/terraform/backend.tf +22 -0
  183. package/templates/terraform/cloudfront-lambda.tf +80 -0
  184. package/templates/terraform/cloudfront.tf +80 -0
  185. package/templates/terraform/database-aurora-postgresql.tf +92 -0
  186. package/templates/terraform/database-mysql.tf +72 -0
  187. package/templates/terraform/database.tf +71 -0
  188. package/templates/terraform/main-lambda.tf +229 -0
  189. package/templates/terraform/main.tf +296 -0
  190. package/templates/terraform/network.tf +95 -0
  191. package/templates/terraform/oidc.tf +64 -0
  192. package/templates/terraform/secrets.tf +31 -0
  193. package/templates/terraform/worker.tf +69 -0
  194. package/tests/__snapshots__/generator.test.js.snap +9633 -0
  195. package/tests/add.test.js +2037 -0
  196. package/tests/ai.test.js +94 -0
  197. package/tests/apply.test.js +488 -0
  198. package/tests/args.test.js +86 -0
  199. package/tests/aws.test.js +244 -0
  200. package/tests/capabilities.test.js +307 -0
  201. package/tests/cli.test.js +29 -0
  202. package/tests/command.test.js +100 -0
  203. package/tests/commands-import.test.js +74 -0
  204. package/tests/db.test.js +2704 -0
  205. package/tests/destroy.test.js +391 -0
  206. package/tests/detector.test.js +79 -0
  207. package/tests/diagnose.test.js +779 -0
  208. package/tests/doctor.test.js +202 -0
  209. package/tests/domain.test.js +899 -0
  210. package/tests/drift.test.js +243 -0
  211. package/tests/ecs.test.js +130 -0
  212. package/tests/eject.test.js +65 -0
  213. package/tests/exec.test.js +380 -0
  214. package/tests/gc.test.js +496 -0
  215. package/tests/generator.test.js +794 -0
  216. package/tests/headless.test.js +562 -0
  217. package/tests/lambda-ecr.test.js +185 -0
  218. package/tests/logs.test.js +447 -0
  219. package/tests/parser.test.js +160 -0
  220. package/tests/rds.test.js +244 -0
  221. package/tests/resolvers.test.js +282 -0
  222. package/tests/rollback.test.js +692 -0
  223. package/tests/secrets.test.js +752 -0
  224. package/tests/sleep-wake.test.js +1016 -0
  225. package/tests/status.test.js +370 -0
  226. package/tests/system.test.js +70 -0
  227. package/tests/telemetry.test.js +520 -0
  228. package/tests/terraform.test.js +84 -0
  229. package/tests/visualizer.test.js +496 -0
  230. package/vitest.config.js +9 -0
  231. package/index.js +0 -2
@@ -0,0 +1,380 @@
1
+ import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
2
+ import fs from 'fs';
3
+ import os from 'os';
4
+ import path from 'path';
5
+ import { EventEmitter } from 'events';
6
+ import {
7
+ runExec,
8
+ parseExecArgs,
9
+ buildExecuteCommandArgs,
10
+ hasSessionManagerPlugin,
11
+ findRunningTask,
12
+ resolveContainer,
13
+ DEFAULT_SHELL,
14
+ } from '../src/commands/exec.js';
15
+ import {
16
+ resolveCluster,
17
+ resolveService,
18
+ } from '../src/utils/resolvers.js';
19
+ import { hasAwsCli } from '../src/utils/aws.js';
20
+ import { trackEvent } from '../src/core/telemetry.js';
21
+
22
+ vi.mock('@clack/prompts', () => ({
23
+ intro: vi.fn(),
24
+ outro: vi.fn(),
25
+ spinner: () => ({ start: vi.fn(), stop: vi.fn(), message: vi.fn() }),
26
+ }));
27
+
28
+ vi.mock('../src/core/telemetry.js', () => {
29
+ const trackEvent = vi.fn();
30
+ const flushTelemetry = vi.fn(() => Promise.resolve());
31
+ // Mirrors the real trackSuccess delegation so success-path assertions
32
+ // keep observing trackEvent (the real helper is unit-tested separately).
33
+ const trackSuccess = vi.fn(async (event, properties) => {
34
+ trackEvent(event, { ...properties, success: true });
35
+ await flushTelemetry();
36
+ });
37
+ const trackFailure = vi.fn(async (event, properties) => {
38
+ trackEvent(event, { ...properties, success: false });
39
+ await flushTelemetry();
40
+ });
41
+ return { trackEvent, flushTelemetry, trackSuccess, trackFailure };
42
+ });
43
+
44
+ const {
45
+ MockListTasksCommand,
46
+ MockDescribeTasksCommand,
47
+ } = vi.hoisted(() => ({
48
+ MockListTasksCommand: vi.fn(function (input) { Object.assign(this, input); }),
49
+ MockDescribeTasksCommand: vi.fn(function (input) { Object.assign(this, input); }),
50
+ }));
51
+
52
+ vi.mock('@aws-sdk/client-ecs', () => ({
53
+ ECSClient: vi.fn(function () { this.send = vi.fn(); }),
54
+ ListTasksCommand: MockListTasksCommand,
55
+ DescribeTasksCommand: MockDescribeTasksCommand,
56
+ }));
57
+
58
+ function mockEcsClient({ taskArns = [], tasks = [] } = {}) {
59
+ return {
60
+ send: vi.fn((cmd) => {
61
+ if (cmd instanceof MockListTasksCommand) return Promise.resolve({ taskArns });
62
+ if (cmd instanceof MockDescribeTasksCommand) return Promise.resolve({ tasks });
63
+ return Promise.resolve({});
64
+ }),
65
+ };
66
+ }
67
+
68
+ function mockSpawnImpl(calls, exitCode = 0) {
69
+ return vi.fn((cmd, args, opts) => {
70
+ calls.push({ cmd, args, opts });
71
+ const child = new EventEmitter();
72
+ queueMicrotask(() => child.emit('close', exitCode));
73
+ return child;
74
+ });
75
+ }
76
+
77
+ describe('exec: CLI args', () => {
78
+ it('parses cluster/service/container/command/region flags', () => {
79
+ expect(parseExecArgs(['exec', '--cluster', 'c1', '--service', 's1', '--container', 'web', '--command', '/bin/bash', '--region', 'eu-west-1'])).toEqual({
80
+ cluster: 'c1',
81
+ service: 's1',
82
+ container: 'web',
83
+ command: '/bin/bash',
84
+ region: 'eu-west-1',
85
+ });
86
+ });
87
+
88
+ it('supports = syntax and bare exec', () => {
89
+ expect(parseExecArgs(['exec', '--cluster=c1', '--region=us-west-2'])).toEqual({ cluster: 'c1', region: 'us-west-2' });
90
+ expect(parseExecArgs(['exec'])).toEqual({});
91
+ });
92
+
93
+ it('resolves names from project directory with env overrides', () => {
94
+ expect(resolveCluster({ projectName: 'myapp' }, '/tmp')).toBe('myapp-cluster');
95
+ expect(resolveService({ projectName: 'myapp' }, '/tmp')).toBe('myapp-service');
96
+ expect(resolveContainer({ projectName: 'myapp' }, '/tmp')).toBe('myapp-container');
97
+ });
98
+ });
99
+
100
+ describe('exec: AWS CLI command construction', () => {
101
+ it('builds the exact execute-command arguments', () => {
102
+ const args = buildExecuteCommandArgs({
103
+ cluster: 'myapp-cluster',
104
+ taskArn: 'arn:aws:ecs:us-east-2:123:task/myapp-cluster/abc',
105
+ container: 'myapp-container',
106
+ command: '/bin/sh',
107
+ region: 'us-east-2',
108
+ });
109
+ expect(args).toEqual([
110
+ 'ecs', 'execute-command',
111
+ '--cluster', 'myapp-cluster',
112
+ '--task', 'arn:aws:ecs:us-east-2:123:task/myapp-cluster/abc',
113
+ '--container', 'myapp-container',
114
+ '--interactive',
115
+ '--command', '/bin/sh',
116
+ '--region', 'us-east-2',
117
+ ]);
118
+ });
119
+
120
+ it('defaults to /bin/sh', () => {
121
+ const args = buildExecuteCommandArgs({ cluster: 'c', taskArn: 't', container: 'web' });
122
+ expect(args).toContain('/bin/sh');
123
+ expect(DEFAULT_SHELL).toBe('/bin/sh');
124
+ });
125
+
126
+ it('detects a missing AWS CLI via spawnSync', () => {
127
+ expect(hasAwsCli({ spawnSyncImpl: () => { throw new Error('ENOENT'); } })).toBe(false);
128
+ expect(hasAwsCli({ spawnSyncImpl: () => ({ status: 0 }) })).toBe(true);
129
+ expect(hasAwsCli({ spawnSyncImpl: () => ({ status: 1 }) })).toBe(false);
130
+ });
131
+
132
+ it('detects a missing Session Manager plugin via spawnSync', () => {
133
+ expect(hasSessionManagerPlugin({ spawnSyncImpl: () => { throw new Error('ENOENT'); } })).toBe(false);
134
+ const enoent = new Error('spawnSync session-manager-plugin ENOENT');
135
+ enoent.code = 'ENOENT';
136
+ expect(hasSessionManagerPlugin({ spawnSyncImpl: () => ({ error: enoent }) })).toBe(false);
137
+ expect(hasSessionManagerPlugin({ spawnSyncImpl: () => ({}) })).toBe(true);
138
+ });
139
+ });
140
+
141
+ describe('Command: exec (mocked ECS + spawn)', () => {
142
+ let exitSpy;
143
+
144
+ beforeEach(() => {
145
+ vi.clearAllMocks();
146
+ exitSpy = vi.spyOn(process, 'exit').mockImplementation(() => { });
147
+ });
148
+
149
+ afterEach(() => {
150
+ exitSpy.mockRestore();
151
+ });
152
+
153
+ it('spawns aws ecs execute-command with inherited stdio on success', async () => {
154
+ const taskArn = 'arn:aws:ecs:us-east-2:123:task/myapp-cluster/abc123';
155
+ const ecsClient = mockEcsClient({
156
+ taskArns: [taskArn],
157
+ tasks: [{ taskArn, containers: [{ name: 'myapp-container', lastStatus: 'RUNNING' }] }],
158
+ });
159
+ const calls = [];
160
+ const spawnImpl = mockSpawnImpl(calls, 0);
161
+ const consoleSpy = vi.spyOn(console, 'log').mockImplementation(() => { });
162
+
163
+ try {
164
+ const result = await runExec({
165
+ projectName: 'myapp',
166
+ region: 'us-east-2',
167
+ ecsClient,
168
+ spawnImpl,
169
+ hasAwsCli: true,
170
+ hasSsmPlugin: true,
171
+ });
172
+ expect(result.ok).toBe(true);
173
+ expect(result.taskArn).toBe(taskArn);
174
+ expect(exitSpy).not.toHaveBeenCalled();
175
+ expect(spawnImpl).toHaveBeenCalledTimes(1);
176
+ const [cmd, args, opts] = spawnImpl.mock.calls[0];
177
+ expect(cmd).toBe('aws');
178
+ expect(args).toEqual(expect.arrayContaining([
179
+ 'ecs', 'execute-command',
180
+ '--cluster', 'myapp-cluster',
181
+ '--task', taskArn,
182
+ '--container', 'myapp-container',
183
+ '--interactive',
184
+ '--command', '/bin/sh',
185
+ ]));
186
+ expect(opts).toEqual(expect.objectContaining({ stdio: 'inherit' }));
187
+ expect(calls).toHaveLength(1);
188
+ } finally {
189
+ consoleSpy.mockRestore();
190
+ }
191
+ });
192
+
193
+ it('exits gracefully when no tasks are running', async () => {
194
+ const ecsClient = mockEcsClient({ taskArns: [], tasks: [] });
195
+ const spawnImpl = mockSpawnImpl([], 0);
196
+ const output = [];
197
+ const consoleSpy = vi.spyOn(console, 'log').mockImplementation((...args) => {
198
+ output.push(args.join(' '));
199
+ });
200
+
201
+ try {
202
+ const result = await runExec({
203
+ projectName: 'myapp',
204
+ region: 'us-east-2',
205
+ ecsClient,
206
+ spawnImpl,
207
+ hasAwsCli: true,
208
+ hasSsmPlugin: true,
209
+ });
210
+ expect(result.ok).toBe(false);
211
+ expect(spawnImpl).not.toHaveBeenCalled();
212
+ expect(output.join('\n')).toMatch(/running container is required|No running containers/i);
213
+ expect(exitSpy).toHaveBeenCalledWith(1);
214
+ } finally {
215
+ consoleSpy.mockRestore();
216
+ }
217
+ });
218
+
219
+ it('fails fast with install guidance when AWS CLI is missing', async () => {
220
+ const ecsClient = mockEcsClient({ taskArns: ['t'], tasks: [] });
221
+ const spawnImpl = mockSpawnImpl([], 0);
222
+ const output = [];
223
+ const consoleSpy = vi.spyOn(console, 'log').mockImplementation((...args) => {
224
+ output.push(args.join(' '));
225
+ });
226
+
227
+ try {
228
+ const result = await runExec({
229
+ projectName: 'myapp',
230
+ region: 'us-east-2',
231
+ ecsClient,
232
+ spawnImpl,
233
+ hasAwsCli: false,
234
+ });
235
+ expect(result.ok).toBe(false);
236
+ expect(spawnImpl).not.toHaveBeenCalled();
237
+ expect(ecsClient.send).not.toHaveBeenCalled();
238
+ expect(output.join('\n')).toMatch(/AWS CLI not found/);
239
+ expect(output.join('\n')).toMatch(/https:\/\//);
240
+ expect(exitSpy).toHaveBeenCalledWith(1);
241
+ } finally {
242
+ consoleSpy.mockRestore();
243
+ }
244
+ });
245
+
246
+ it('fails fast with platform guidance when the Session Manager plugin is missing', async () => {
247
+ const ecsClient = mockEcsClient({ taskArns: ['t'], tasks: [] });
248
+ const spawnImpl = mockSpawnImpl([], 0);
249
+ const output = [];
250
+ const consoleSpy = vi.spyOn(console, 'log').mockImplementation((...args) => {
251
+ output.push(args.join(' '));
252
+ });
253
+
254
+ try {
255
+ const result = await runExec({
256
+ projectName: 'myapp',
257
+ region: 'us-east-2',
258
+ ecsClient,
259
+ spawnImpl,
260
+ hasAwsCli: true,
261
+ hasSsmPlugin: false,
262
+ });
263
+ expect(result.ok).toBe(false);
264
+ expect(result.reason).toBe('ssm-plugin-missing');
265
+ expect(spawnImpl).not.toHaveBeenCalled();
266
+ expect(ecsClient.send).not.toHaveBeenCalled();
267
+ expect(output.join('\n')).toMatch(/Session Manager plugin not found/);
268
+ expect(exitSpy).toHaveBeenCalledWith(1);
269
+ } finally {
270
+ consoleSpy.mockRestore();
271
+ }
272
+ });
273
+
274
+ it('finds a running task via ListTasks + DescribeTasks', async () => {
275
+ const taskArn = 'arn:task/1';
276
+ const ecsClient = mockEcsClient({
277
+ taskArns: [taskArn],
278
+ tasks: [{ taskArn, containers: [{ name: 'myapp-container' }] }],
279
+ });
280
+ const found = await findRunningTask(ecsClient, { cluster: 'myapp-cluster', service: 'myapp-service' });
281
+ expect(found.taskArn).toBe(taskArn);
282
+ expect(ecsClient.send).toHaveBeenCalledTimes(2);
283
+ });
284
+
285
+ it('returns null when no RUNNING tasks exist', async () => {
286
+ const ecsClient = mockEcsClient({ taskArns: [] });
287
+ await expect(findRunningTask(ecsClient, { cluster: 'c', service: 's' })).resolves.toBeNull();
288
+ });
289
+
290
+ it('is wired into bin/cli.js', () => {
291
+ const cliPath = path.resolve(__dirname, '../bin/cli.js');
292
+ const content = fs.readFileSync(cliPath, 'utf8');
293
+ expect(content).toContain('runExec');
294
+ expect(content).toContain("'exec'");
295
+ });
296
+
297
+ it('enables ECS Exec in Terraform templates with SSM permissions on the task role', () => {
298
+ const mainTf = fs.readFileSync(path.resolve(__dirname, '../templates/terraform/main.tf'), 'utf8');
299
+ expect(mainTf).toContain('enable_execute_command = true');
300
+ for (const perm of [
301
+ 'ssmmessages:CreateControlChannel',
302
+ 'ssmmessages:CreateDataChannel',
303
+ 'ssmmessages:OpenControlChannel',
304
+ 'ssmmessages:OpenDataChannel',
305
+ ]) {
306
+ expect(mainTf).toContain(perm);
307
+ }
308
+ expect(mainTf).toMatch(/resource\s+"aws_iam_role_policy"\s+"ecs_exec"/);
309
+ expect(mainTf).toMatch(/aws_iam_role\.task_role/);
310
+ });
311
+ });
312
+
313
+ describe('exec: fuzzer hardening', () => {
314
+ let exitSpy;
315
+ let consoleSpy;
316
+
317
+ beforeEach(() => {
318
+ vi.clearAllMocks();
319
+ exitSpy = vi.spyOn(process, 'exit').mockImplementation(() => { });
320
+ consoleSpy = vi.spyOn(console, 'log').mockImplementation(() => { });
321
+ });
322
+
323
+ afterEach(() => {
324
+ exitSpy.mockRestore();
325
+ consoleSpy.mockRestore();
326
+ });
327
+
328
+ it('routes unresolvable projects through PROJECT_NOT_INITIALIZED', async () => {
329
+ const cwdSpy = vi.spyOn(process, 'cwd').mockImplementation(() => { throw new Error('deleted'); });
330
+ try {
331
+ const result = await runExec(null);
332
+ expect(result).toEqual({ ok: false, reason: 'project-not-initialized' });
333
+ expect(exitSpy).toHaveBeenCalledWith(1);
334
+ expect(trackEvent).toHaveBeenCalledWith('exec_run', expect.objectContaining({
335
+ success: false,
336
+ error_code: 'PROJECT_NOT_INITIALIZED',
337
+ }));
338
+ } finally {
339
+ cwdSpy.mockRestore();
340
+ }
341
+ });
342
+
343
+ it.each([null, 42, true, { port: 'string' }])('parseExecArgs(%s) returns defaults', (bad) => {
344
+ expect(parseExecArgs(bad)).toEqual({});
345
+ });
346
+ });
347
+
348
+ describe('exec on --target lambda projects', () => {
349
+ let exitSpy;
350
+ let logSpy;
351
+
352
+ beforeEach(() => {
353
+ vi.clearAllMocks();
354
+ exitSpy = vi.spyOn(process, 'exit').mockImplementation(() => { });
355
+ logSpy = vi.spyOn(console, 'log').mockImplementation(() => { });
356
+ });
357
+
358
+ afterEach(() => {
359
+ exitSpy.mockRestore();
360
+ logSpy.mockRestore();
361
+ });
362
+
363
+ it('fails gracefully with logs guidance instead of ECS Exec', async () => {
364
+ const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'exec-lambda-test-'));
365
+ try {
366
+ fs.mkdirSync(path.join(dir, 'terraform'), { recursive: true });
367
+ fs.writeFileSync(
368
+ path.join(dir, 'terraform', 'main.tf'),
369
+ 'locals {\n app_name = "myapp${local.env_suffix}"\n}\nresource "aws_lambda_function" "app" {}\n'
370
+ );
371
+ const result = await runExec({ cwd: dir, hasAwsCli: true, hasSsmPlugin: true });
372
+ expect(result).toMatchObject({ ok: false, reason: 'lambda-target-unsupported' });
373
+ expect(exitSpy).toHaveBeenCalledWith(1);
374
+ expect(trackEvent).toHaveBeenCalledWith('exec_run', expect.objectContaining({ error_code: 'LAMBDA_TARGET_UNSUPPORTED' }));
375
+ expect(logSpy.mock.calls.map((call) => String(call[0])).join('\n')).toContain('npx grada-run logs');
376
+ } finally {
377
+ fs.rmSync(dir, { recursive: true, force: true });
378
+ }
379
+ });
380
+ });