-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathcreate.test.ts
More file actions
320 lines (260 loc) · 9.29 KB
/
create.test.ts
File metadata and controls
320 lines (260 loc) · 9.29 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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
/*
* Copyright (c) 2025, Salesforce, Inc.
* SPDX-License-Identifier: Apache-2
* For full license text, see the license.txt file in the repo root or http://www.apache.org/licenses/LICENSE-2.0
*/
/* eslint-disable @typescript-eslint/no-explicit-any, unicorn/consistent-function-scoping */
import {expect} from 'chai';
import OdsCreate from '../../../src/commands/ods/create.js';
import {
makeCommandThrowOnError,
stubCommandConfigAndLogger,
stubOdsClient,
stubResolvedConfig,
} from '../../helpers/ods.js';
/**
* Unit tests for ODS create command CLI logic.
* Tests settings building, permission logic, wait/poll logic.
* SDK tests cover the actual API calls.
*/
describe('ods create', () => {
describe('buildSettings', () => {
it('should return undefined when set-permissions is false', () => {
const command = new OdsCreate([], {} as any);
(command as any).flags = {'set-permissions': false};
// Accessing private method for testing
const settings = (command as any).buildSettings(false);
expect(settings).to.be.undefined;
});
it('should return undefined when no client ID is configured', () => {
const command = new OdsCreate([], {} as any);
stubCommandConfigAndLogger(command);
stubResolvedConfig(command, {});
const settings = (command as any).buildSettings(true);
expect(settings).to.be.undefined;
});
it('should build settings with OCAPI and WebDAV permissions', () => {
const command = new OdsCreate([], {} as any);
stubCommandConfigAndLogger(command);
stubResolvedConfig(command, {clientId: 'test-client-id'});
const settings = (command as any).buildSettings(true);
expect(settings).to.exist;
expect(settings).to.have.property('ocapi');
expect(settings).to.have.property('webdav');
expect(settings.ocapi).to.be.an('array').with.length.greaterThan(0);
expect(settings.webdav).to.be.an('array').with.length.greaterThan(0);
expect(settings.ocapi[0]).to.have.property('client_id');
expect(settings.webdav[0]).to.have.property('client_id');
});
it('should include default OCAPI resources', () => {
const command = new OdsCreate([], {} as any);
stubCommandConfigAndLogger(command);
stubResolvedConfig(command, {clientId: 'test-client-id'});
const settings = (command as any).buildSettings(true);
const resources = settings.ocapi[0].resources;
expect(resources).to.be.an('array');
expect(resources.some((r: any) => r.resource_id === '/code_versions')).to.be.true;
expect(resources.some((r: any) => r.resource_id.includes('/jobs/'))).to.be.true;
});
it('should include default WebDAV permissions', () => {
const command = new OdsCreate([], {} as any);
stubCommandConfigAndLogger(command);
stubResolvedConfig(command, {clientId: 'test-client-id'});
const settings = (command as any).buildSettings(true);
const permissions = settings.webdav[0].permissions;
expect(permissions).to.be.an('array');
expect(permissions.some((p: any) => p.path === '/impex')).to.be.true;
expect(permissions.some((p: any) => p.path === '/cartridges')).to.be.true;
});
});
describe('flag defaults', () => {
it('should have correct default TTL', () => {
expect(OdsCreate.flags.ttl.default).to.equal(24);
});
it('should have correct default profile', () => {
expect(OdsCreate.flags.profile.default).to.equal('medium');
});
it('should have correct default for set-permissions', () => {
expect(OdsCreate.flags['set-permissions'].default).to.equal(true);
});
it('should have correct default for auto-scheduled', () => {
expect(OdsCreate.flags['auto-scheduled'].default).to.equal(false);
});
it('should have correct default for wait', () => {
expect(OdsCreate.flags.wait.default).to.equal(false);
});
it('should have correct default poll interval', () => {
expect(OdsCreate.flags['poll-interval'].default).to.equal(10);
});
it('should have correct default timeout', () => {
expect(OdsCreate.flags.timeout.default).to.equal(600);
});
});
describe('profile options', () => {
it('should only allow valid profile values', () => {
const validProfiles = ['medium', 'large', 'xlarge', 'xxlarge'];
expect(OdsCreate.flags.profile.options).to.deep.equal(validProfiles);
});
});
describe('run()', () => {
function setupCreateCommand(): OdsCreate {
const command = new OdsCreate([], {} as any);
stubCommandConfigAndLogger(command);
// Mock log & error
command.log = () => {};
makeCommandThrowOnError(command);
return command;
}
it('should create sandbox successfully without wait', async () => {
const command = setupCreateCommand();
(command as any).flags = {
realm: 'abcd',
ttl: 24,
profile: 'medium',
'auto-scheduled': false,
wait: false,
'set-permissions': false,
json: true,
};
stubOdsClient(command, {
POST: async () => ({
data: {
data: {
id: 'sb-123',
realm: 'abcd',
state: 'creating',
},
},
}),
});
const result = await command.run();
expect(result.id).to.equal('sb-123');
});
it('should throw error when sandbox creation fails', async () => {
const command = setupCreateCommand();
(command as any).flags = {
realm: 'abcd',
ttl: 24,
profile: 'medium',
wait: false,
'set-permissions': false,
};
stubOdsClient(command, {
POST: async () => ({
data: undefined,
error: {
error: {message: 'Invalid realm'},
},
response: {
statusText: 'Bad Request',
},
}),
});
try {
await command.run();
expect.fail('Expected error');
} catch (error: any) {
expect(error.message).to.include('Failed to create sandbox');
}
});
it('should not include settings when set-permissions is false', async () => {
const command = setupCreateCommand();
(command as any).flags = {
realm: 'abcd',
ttl: 24,
profile: 'medium',
wait: false,
'set-permissions': false,
};
let requestBody: any;
stubOdsClient(command, {
async POST(_url: string, options: any) {
requestBody = options.body;
return {
data: {data: {id: 'sb-1', state: 'creating'}},
};
},
});
await command.run();
expect(requestBody.settings).to.be.undefined;
});
describe('waitForSandbox()', () => {
it('should wait until sandbox reaches started state', async () => {
const command = setupCreateCommand();
let calls = 0;
const mockClient = {
async GET() {
calls++;
return {
data: {
data: {
state: calls < 2 ? 'creating' : 'started',
},
},
};
},
};
stubOdsClient(command, mockClient);
const result = await (command as any).waitForSandbox('sb-1', 0, 5);
expect(result.state).to.equal('started');
});
it('should error when sandbox enters failed state', async () => {
const command = setupCreateCommand();
stubOdsClient(command, {
GET: async () => ({
data: {data: {state: 'failed'}},
}),
});
try {
await (command as any).waitForSandbox('sb-1', 0, 5);
expect.fail('Expected error');
} catch (error: any) {
expect(error.message).to.include('Sandbox creation failed');
}
});
it('should error when sandbox is deleted', async () => {
const command = setupCreateCommand();
stubOdsClient(command, {
GET: async () => ({
data: {data: {state: 'deleted'}},
}),
});
try {
await (command as any).waitForSandbox('sb-1', 0, 5);
expect.fail('Expected error');
} catch (error: any) {
expect(error.message).to.include('Sandbox was deleted');
}
});
it('should timeout if sandbox never reaches terminal state', async () => {
const command = setupCreateCommand();
stubOdsClient(command, {
GET: async () => ({
data: {data: {state: 'creating'}},
}),
});
try {
await (command as any).waitForSandbox('sb-1', 0, 1);
expect.fail('Expected timeout');
} catch (error: any) {
expect(error.message).to.include('Timeout waiting for sandbox');
}
});
it('should error if polling API returns no data', async () => {
const command = setupCreateCommand();
stubOdsClient(command, {
GET: async () => ({
data: undefined,
response: {statusText: 'Internal Error'},
}),
});
try {
await (command as any).waitForSandbox('sb-1', 0, 5);
expect.fail('Expected error');
} catch (error: any) {
expect(error.message).to.include('Failed to fetch sandbox status');
}
});
});
});
});