Skip to content

Commit 18c4bbf

Browse files
committed
adding some tests
1 parent f074db0 commit 18c4bbf

3 files changed

Lines changed: 180 additions & 1 deletion

File tree

packages/b2c-cli/src/commands/ods/create.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ export default class OdsCreate extends OdsCommand<typeof OdsCreate> {
152152
this.logger.info({sandboxId: sandbox.id}, t('commands.ods.create.success', 'Sandbox created successfully'));
153153

154154
if (wait && sandbox.id) {
155-
this.log('');
155+
this.log(t('commands.ods.create.waiting', 'Waiting for sandbox to get started..'));
156156
sandbox = await this.waitForSandbox(sandbox.id, pollInterval, timeout);
157157
}
158158

packages/b2c-cli/test/commands/ods/delete.test.ts

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,15 @@ describe('ods delete', () => {
6969
expect(OdsDelete.flags.force.char).to.equal('f');
7070
});
7171

72+
it('should expose wait/polling flags', () => {
73+
expect(OdsDelete.flags).to.have.property('wait');
74+
expect(OdsDelete.flags.wait.char).to.equal('w');
75+
expect(OdsDelete.flags).to.have.property('poll-interval');
76+
expect(OdsDelete.flags['poll-interval'].dependsOn).to.deep.equal(['wait']);
77+
expect(OdsDelete.flags).to.have.property('timeout');
78+
expect(OdsDelete.flags.timeout.dependsOn).to.deep.equal(['wait']);
79+
});
80+
7281
it('should have correct description', () => {
7382
expect(OdsDelete.description).to.be.a('string');
7483
expect(OdsDelete.description.toLowerCase()).to.include('delete');
@@ -128,6 +137,46 @@ describe('ods delete', () => {
128137
expect(logs.length).to.be.greaterThan(0);
129138
});
130139

140+
it('should poll sandbox state when --wait is true', async () => {
141+
const command = new OdsDelete([], {} as any);
142+
143+
Object.defineProperty(command, 'args', {
144+
value: {sandboxId: 'sandbox-123'},
145+
configurable: true,
146+
});
147+
148+
(command as any).flags = {force: true, wait: true, 'poll-interval': 0, timeout: 1};
149+
stubCommandConfigAndLogger(command);
150+
stubJsonEnabled(command, true);
151+
152+
const getSpy = sinon.spy(async () => {
153+
// First GET is for confirmation/details, subsequent GET is polling
154+
if (getSpy.callCount === 0) {
155+
return {
156+
data: {data: {id: 'sandbox-123', realm: 'zzzv', instance: 'zzzv-001', state: 'started'}},
157+
response: new Response(),
158+
};
159+
}
160+
161+
return {
162+
data: {data: {state: 'deleted'}},
163+
response: new Response(),
164+
};
165+
});
166+
167+
stubOdsClient(command, {
168+
GET: getSpy,
169+
DELETE: async () => ({
170+
data: {data: {}},
171+
response: new Response(null, {status: 202}),
172+
}),
173+
});
174+
175+
await command.run();
176+
177+
expect(getSpy.callCount, 'Expected GET to be called multiple times when --wait is true').to.be.greaterThan(1);
178+
});
179+
131180
it('should log messages in non-JSON mode', async () => {
132181
const command = new OdsDelete([], {} as any);
133182

packages/b2c-cli/test/commands/ods/operations.test.ts

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
*/
66

77
import {expect} from 'chai';
8+
import sinon from 'sinon';
89

910
import OdsStart from '../../../src/commands/ods/start.js';
1011

@@ -66,6 +67,15 @@ describe('ods operations', () => {
6667
expect(OdsStart.enableJsonFlag).to.be.true;
6768
});
6869

70+
it('should expose wait/polling flags', () => {
71+
expect(OdsStart.flags).to.have.property('wait');
72+
expect(OdsStart.flags.wait.char).to.equal('w');
73+
expect(OdsStart.flags).to.have.property('poll-interval');
74+
expect(OdsStart.flags['poll-interval'].dependsOn).to.deep.equal(['wait']);
75+
expect(OdsStart.flags).to.have.property('timeout');
76+
expect(OdsStart.flags.timeout.dependsOn).to.deep.equal(['wait']);
77+
});
78+
6979
it('should return operation data in JSON mode', async () => {
7080
const command = new OdsStart([], {} as any);
7181

@@ -178,6 +188,40 @@ describe('ods operations', () => {
178188
expect(error.message).to.include('Bad request');
179189
}
180190
});
191+
192+
it('should poll sandbox state when --wait is true', async () => {
193+
const command = new OdsStart([], {} as any);
194+
195+
Object.defineProperty(command, 'args', {
196+
value: {sandboxId: 'sandbox-123'},
197+
configurable: true,
198+
});
199+
200+
Object.defineProperty(command, 'flags', {
201+
value: {wait: true, 'poll-interval': 0, timeout: 1},
202+
configurable: true,
203+
});
204+
205+
stubCommandConfigAndLogger(command);
206+
stubJsonEnabled(command, true);
207+
208+
const getSpy = sinon.spy(async () => ({
209+
data: {data: {state: 'started'}},
210+
response: new Response(),
211+
}));
212+
213+
stubOdsClient(command, {
214+
POST: async () => ({
215+
data: {data: {operation: 'start', operationState: 'running'}},
216+
response: new Response(),
217+
}),
218+
GET: getSpy,
219+
});
220+
221+
await command.run();
222+
223+
expect(getSpy.called, 'Expected GET to be called when --wait is true').to.equal(true);
224+
});
181225
});
182226

183227
describe('ods stop', () => {
@@ -195,6 +239,15 @@ describe('ods operations', () => {
195239
expect(OdsStop.enableJsonFlag).to.be.true;
196240
});
197241

242+
it('should expose wait/polling flags', () => {
243+
expect(OdsStop.flags).to.have.property('wait');
244+
expect(OdsStop.flags.wait.char).to.equal('w');
245+
expect(OdsStop.flags).to.have.property('poll-interval');
246+
expect(OdsStop.flags['poll-interval'].dependsOn).to.deep.equal(['wait']);
247+
expect(OdsStop.flags).to.have.property('timeout');
248+
expect(OdsStop.flags.timeout.dependsOn).to.deep.equal(['wait']);
249+
});
250+
198251
it('should return operation data in JSON mode', async () => {
199252
const command = new OdsStop([], {} as any);
200253

@@ -307,6 +360,40 @@ describe('ods operations', () => {
307360
expect(error.message).to.include('Bad request');
308361
}
309362
});
363+
364+
it('should poll sandbox state when --wait is true', async () => {
365+
const command = new OdsStop([], {} as any);
366+
367+
Object.defineProperty(command, 'args', {
368+
value: {sandboxId: 'sandbox-123'},
369+
configurable: true,
370+
});
371+
372+
Object.defineProperty(command, 'flags', {
373+
value: {wait: true, 'poll-interval': 0, timeout: 1},
374+
configurable: true,
375+
});
376+
377+
stubCommandConfigAndLogger(command);
378+
stubJsonEnabled(command, true);
379+
380+
const getSpy = sinon.spy(async () => ({
381+
data: {data: {state: 'stopped'}},
382+
response: new Response(),
383+
}));
384+
385+
stubOdsClient(command, {
386+
POST: async () => ({
387+
data: {data: {operation: 'stop', operationState: 'running'}},
388+
response: new Response(),
389+
}),
390+
GET: getSpy,
391+
});
392+
393+
await command.run();
394+
395+
expect(getSpy.called, 'Expected GET to be called when --wait is true').to.equal(true);
396+
});
310397
});
311398

312399
describe('ods restart', () => {
@@ -324,6 +411,15 @@ describe('ods operations', () => {
324411
expect(OdsRestart.enableJsonFlag).to.be.true;
325412
});
326413

414+
it('should expose wait/polling flags', () => {
415+
expect(OdsRestart.flags).to.have.property('wait');
416+
expect(OdsRestart.flags.wait.char).to.equal('w');
417+
expect(OdsRestart.flags).to.have.property('poll-interval');
418+
expect(OdsRestart.flags['poll-interval'].dependsOn).to.deep.equal(['wait']);
419+
expect(OdsRestart.flags).to.have.property('timeout');
420+
expect(OdsRestart.flags.timeout.dependsOn).to.deep.equal(['wait']);
421+
});
422+
327423
it('should return operation data in JSON mode', async () => {
328424
const command = new OdsRestart([], {} as any);
329425

@@ -436,5 +532,39 @@ describe('ods operations', () => {
436532
expect(error.message).to.include('Bad request');
437533
}
438534
});
535+
536+
it('should poll sandbox state when --wait is true', async () => {
537+
const command = new OdsRestart([], {} as any);
538+
539+
Object.defineProperty(command, 'args', {
540+
value: {sandboxId: 'sandbox-123'},
541+
configurable: true,
542+
});
543+
544+
Object.defineProperty(command, 'flags', {
545+
value: {wait: true, 'poll-interval': 0, timeout: 1},
546+
configurable: true,
547+
});
548+
549+
stubCommandConfigAndLogger(command);
550+
stubJsonEnabled(command, true);
551+
552+
const getSpy = sinon.spy(async () => ({
553+
data: {data: {state: 'started'}},
554+
response: new Response(),
555+
}));
556+
557+
stubOdsClient(command, {
558+
POST: async () => ({
559+
data: {data: {operation: 'restart', operationState: 'running'}},
560+
response: new Response(),
561+
}),
562+
GET: getSpy,
563+
});
564+
565+
await command.run();
566+
567+
expect(getSpy.called, 'Expected GET to be called when --wait is true').to.equal(true);
568+
});
439569
});
440570
});

0 commit comments

Comments
 (0)