forked from aws/agentcore-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexit-codes.test.ts
More file actions
224 lines (190 loc) · 8.54 KB
/
exit-codes.test.ts
File metadata and controls
224 lines (190 loc) · 8.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
import { ExitCode, getExitCode } from '../exit-codes.js';
import { describe, expect, it } from 'vitest';
describe('exit-codes', () => {
describe('ExitCode', () => {
it('has correct values for all exit codes', () => {
expect(ExitCode.SUCCESS).toBe(0);
expect(ExitCode.GENERAL_ERROR).toBe(1);
expect(ExitCode.INVALID_ARGS).toBe(2);
expect(ExitCode.AUTH_EXPIRED).toBe(3);
expect(ExitCode.ACCESS_DENIED).toBe(4);
expect(ExitCode.AGENT_NOT_FOUND).toBe(5);
expect(ExitCode.DEPLOY_FAILED).toBe(6);
});
it('has all expected keys', () => {
const keys = Object.keys(ExitCode);
expect(keys).toContain('SUCCESS');
expect(keys).toContain('GENERAL_ERROR');
expect(keys).toContain('INVALID_ARGS');
expect(keys).toContain('AUTH_EXPIRED');
expect(keys).toContain('ACCESS_DENIED');
expect(keys).toContain('AGENT_NOT_FOUND');
expect(keys).toContain('DEPLOY_FAILED');
expect(keys).toHaveLength(7);
});
});
describe('getExitCode', () => {
describe('access denied errors → EXIT_ACCESS_DENIED (4)', () => {
it('returns 4 for AccessDeniedException', () => {
expect(getExitCode({ name: 'AccessDeniedException' })).toBe(ExitCode.ACCESS_DENIED);
});
it('returns 4 for AccessDenied', () => {
expect(getExitCode({ name: 'AccessDenied' })).toBe(ExitCode.ACCESS_DENIED);
});
});
describe('expired token errors → EXIT_AUTH_EXPIRED (3)', () => {
const expiredTokenNames = [
'ExpiredToken',
'ExpiredTokenException',
'TokenRefreshRequired',
'CredentialsExpired',
'InvalidIdentityToken',
'UnauthorizedAccess',
'InvalidClientTokenId',
'SignatureDoesNotMatch',
'RequestExpired',
];
it('returns 3 for all expired token error names', () => {
for (const name of expiredTokenNames) {
expect(getExitCode({ name }), `Should return 3 for error.name: ${name}`).toBe(ExitCode.AUTH_EXPIRED);
}
});
it('returns 3 for expired token error Code properties', () => {
for (const Code of expiredTokenNames) {
expect(getExitCode({ Code }), `Should return 3 for error.Code: ${Code}`).toBe(ExitCode.AUTH_EXPIRED);
}
});
it('returns 3 for expired token message patterns', () => {
const patterns = [
'expired token',
'token has expired',
'credentials have expired',
'security token included in the request is expired',
'the security token included in the request is invalid',
];
for (const pattern of patterns) {
expect(getExitCode(new Error(pattern)), `Should return 3 for message: ${pattern}`).toBe(
ExitCode.AUTH_EXPIRED
);
}
});
it('returns 3 for nested expired token errors', () => {
expect(getExitCode({ cause: { name: 'ExpiredToken' } })).toBe(ExitCode.AUTH_EXPIRED);
});
});
describe('no credentials errors → EXIT_AUTH_EXPIRED (3)', () => {
it('returns 3 for AwsCredentialsError', () => {
expect(getExitCode({ name: 'AwsCredentialsError' })).toBe(ExitCode.AUTH_EXPIRED);
});
it('returns 3 for no credentials message patterns', () => {
const patterns = ['no aws credentials found', 'could not load credentials', 'credentials not found'];
for (const pattern of patterns) {
expect(getExitCode(new Error(pattern)), `Should return 3 for message: ${pattern}`).toBe(
ExitCode.AUTH_EXPIRED
);
}
});
});
describe('commander invalid argument errors → EXIT_INVALID_ARGS (2)', () => {
it('returns 2 for commander.invalidArgument code', () => {
const err = Object.assign(new Error('invalid argument'), { code: 'commander.invalidArgument' });
expect(getExitCode(err)).toBe(ExitCode.INVALID_ARGS);
});
it('returns 2 for commander.missingArgument code', () => {
const err = Object.assign(new Error('missing argument'), { code: 'commander.missingArgument' });
expect(getExitCode(err)).toBe(ExitCode.INVALID_ARGS);
});
it('returns 2 for commander.missingMandatoryOptionValue code', () => {
const err = Object.assign(new Error('missing option value'), {
code: 'commander.missingMandatoryOptionValue',
});
expect(getExitCode(err)).toBe(ExitCode.INVALID_ARGS);
});
it('returns 2 for commander.optionMissingArgument code', () => {
const err = Object.assign(new Error('option missing argument'), {
code: 'commander.optionMissingArgument',
});
expect(getExitCode(err)).toBe(ExitCode.INVALID_ARGS);
});
});
describe('deploy failed errors → EXIT_DEPLOY_FAILED (6)', () => {
it('returns 6 for stack in progress errors', () => {
const states = ['UPDATE_IN_PROGRESS', 'CREATE_IN_PROGRESS', 'DELETE_IN_PROGRESS', 'ROLLBACK_IN_PROGRESS'];
for (const state of states) {
expect(getExitCode(new Error(`Stack is in ${state} state`)), `Should return 6 for state: ${state}`).toBe(
ExitCode.DEPLOY_FAILED
);
}
});
it('returns 6 for stack cannot be updated errors', () => {
expect(getExitCode(new Error('Stack is in UPDATE_ROLLBACK_IN_PROGRESS state and cannot be updated'))).toBe(
ExitCode.DEPLOY_FAILED
);
});
it('returns 6 for stack currently being updated', () => {
expect(getExitCode(new Error('stack is currently being updated'))).toBe(ExitCode.DEPLOY_FAILED);
});
it('returns 6 for changeset in progress errors', () => {
expect(
getExitCode(
new Error('InvalidChangeSetStatusException: An operation on this ChangeSet is currently in progress.')
)
).toBe(ExitCode.DEPLOY_FAILED);
});
it('returns 6 for changeset currently in progress', () => {
expect(getExitCode(new Error('ChangeSet is currently in progress'))).toBe(ExitCode.DEPLOY_FAILED);
});
});
describe('agent not found errors → EXIT_AGENT_NOT_FOUND (5)', () => {
it('returns 5 for ResourceNotFoundException error name', () => {
expect(getExitCode({ name: 'ResourceNotFoundException' })).toBe(ExitCode.AGENT_NOT_FOUND);
});
it('returns 5 for agent not found message', () => {
expect(getExitCode(new Error("Agent 'my-agent' not found"))).toBe(ExitCode.AGENT_NOT_FOUND);
});
it('returns 5 for agent not deployed message', () => {
expect(getExitCode(new Error("Agent 'my-agent' is not deployed"))).toBe(ExitCode.AGENT_NOT_FOUND);
});
it('returns 5 for no agents defined message', () => {
expect(getExitCode(new Error('No agents defined in agentcore.json'))).toBe(ExitCode.AGENT_NOT_FOUND);
});
it('returns 5 for agent not found in deployed state', () => {
expect(getExitCode(new Error("Agent 'my-agent' not found in deployed state"))).toBe(ExitCode.AGENT_NOT_FOUND);
});
});
describe('default → EXIT_GENERAL_ERROR (1)', () => {
it('returns 1 for generic errors', () => {
expect(getExitCode(new Error('some random error'))).toBe(ExitCode.GENERAL_ERROR);
});
it('returns 1 for null', () => {
expect(getExitCode(null)).toBe(ExitCode.GENERAL_ERROR);
});
it('returns 1 for undefined', () => {
expect(getExitCode(undefined)).toBe(ExitCode.GENERAL_ERROR);
});
it('returns 1 for string errors', () => {
expect(getExitCode('string error')).toBe(ExitCode.GENERAL_ERROR);
});
it('returns 1 for empty objects', () => {
expect(getExitCode({})).toBe(ExitCode.GENERAL_ERROR);
});
it('returns 1 for number errors', () => {
expect(getExitCode(123)).toBe(ExitCode.GENERAL_ERROR);
});
});
describe('priority ordering', () => {
it('access denied takes priority over message-based matching', () => {
const err = { name: 'AccessDeniedException', message: 'Agent not found' };
expect(getExitCode(err)).toBe(ExitCode.ACCESS_DENIED);
});
it('expired token takes priority over agent not found message matching', () => {
const err = { name: 'ExpiredToken', message: 'Agent not found' };
expect(getExitCode(err)).toBe(ExitCode.AUTH_EXPIRED);
});
it('access denied takes priority over expired token', () => {
expect(getExitCode({ name: 'AccessDeniedException' })).toBe(ExitCode.ACCESS_DENIED);
expect(getExitCode({ name: 'AccessDenied' })).toBe(ExitCode.ACCESS_DENIED);
});
});
});
});