-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathwait.test.ts
More file actions
212 lines (185 loc) · 8.07 KB
/
wait.test.ts
File metadata and controls
212 lines (185 loc) · 8.07 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
/*
* 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
*/
import {expect} from 'chai';
import sinon from 'sinon';
import {Config} from '@oclif/core';
import ReplicationsWait from '../../../../src/commands/scapi/replications/wait.js';
import {stubParse} from '../../../helpers/stub-parse.js';
import {createIsolatedEnvHooks} from '../../../helpers/test-setup.js';
describe('scapi replications wait', () => {
const hooks = createIsolatedEnvHooks();
beforeEach(hooks.beforeEach);
afterEach(hooks.afterEach);
describe('run', () => {
let config: Config;
beforeEach(async () => {
config = await Config.load();
});
afterEach(() => {
sinon.restore();
});
it('waits for process to complete', async () => {
const command: any = new ReplicationsWait([], config);
stubParse(command, {'tenant-id': 'zzxy_prd', timeout: 10, interval: 0}, {'process-id': 'proc-123'});
await command.init();
sinon.stub(command, 'requireOAuthCredentials').returns(void 0);
sinon.stub(command, 'jsonEnabled').returns(true);
sinon.stub(command, 'resolvedConfig').get(() => ({values: {shortCode: 'kv7kzm78', tenantId: 'zzxy_prd'}}));
sinon.stub(command, 'getOAuthStrategy').returns({getAuthorizationHeader: async () => 'Bearer test'});
let callCount = 0;
sinon.stub(globalThis, 'fetch').callsFake(async () => {
callCount++;
if (callCount === 1) {
return new Response(
JSON.stringify({
id: 'proc-123',
status: 'in_progress',
startTime: '2025-01-01T00:00:00Z',
initiatedBy: 'user@example.com',
productItem: {productId: 'PROD-1'},
}),
{status: 200, headers: {'content-type': 'application/json'}},
);
}
return new Response(
JSON.stringify({
id: 'proc-123',
status: 'completed',
startTime: '2025-01-01T00:00:00Z',
endTime: '2025-01-01T00:05:00Z',
initiatedBy: 'user@example.com',
productItem: {productId: 'PROD-1'},
}),
{status: 200, headers: {'content-type': 'application/json'}},
);
});
const result = await command.run();
expect(result.id).to.equal('proc-123');
expect(result.status).to.equal('completed');
expect(callCount).to.be.greaterThan(1);
});
it('returns failed status', async () => {
const command: any = new ReplicationsWait([], config);
stubParse(command, {'tenant-id': 'zzxy_prd', timeout: 10, interval: 0}, {'process-id': 'proc-456'});
await command.init();
sinon.stub(command, 'requireOAuthCredentials').returns(void 0);
sinon.stub(command, 'jsonEnabled').returns(true);
sinon.stub(command, 'resolvedConfig').get(() => ({values: {shortCode: 'kv7kzm78', tenantId: 'zzxy_prd'}}));
sinon.stub(command, 'getOAuthStrategy').returns({getAuthorizationHeader: async () => 'Bearer test'});
sinon.stub(globalThis, 'fetch').resolves(
new Response(
JSON.stringify({
id: 'proc-456',
status: 'failed',
startTime: '2025-01-01T01:00:00Z',
endTime: '2025-01-01T01:02:00Z',
initiatedBy: 'user@example.com',
productItem: {productId: 'PROD-2'},
}),
{status: 200, headers: {'content-type': 'application/json'}},
),
);
const result = await command.run();
expect(result.status).to.equal('failed');
});
it('logs status transitions in non-JSON mode', async () => {
const command: any = new ReplicationsWait([], config);
stubParse(command, {'tenant-id': 'zzxy_prd', timeout: 10, interval: 0}, {'process-id': 'proc-789'});
await command.init();
sinon.stub(command, 'requireOAuthCredentials').returns(void 0);
sinon.stub(command, 'jsonEnabled').returns(false);
const logStub = sinon.stub(command, 'log');
sinon.stub(command, 'resolvedConfig').get(() => ({values: {shortCode: 'kv7kzm78', tenantId: 'zzxy_prd'}}));
sinon.stub(command, 'getOAuthStrategy').returns({getAuthorizationHeader: async () => 'Bearer test'});
let callCount = 0;
sinon.stub(globalThis, 'fetch').callsFake(async () => {
callCount++;
if (callCount === 1) {
return new Response(
JSON.stringify({
id: 'proc-789',
status: 'in_progress',
startTime: '2025-01-01T02:00:00Z',
initiatedBy: 'user@example.com',
productItem: {productId: 'PROD-3'},
}),
{status: 200, headers: {'content-type': 'application/json'}},
);
}
return new Response(
JSON.stringify({
id: 'proc-789',
status: 'completed',
startTime: '2025-01-01T02:00:00Z',
endTime: '2025-01-01T02:05:00Z',
initiatedBy: 'user@example.com',
productItem: {productId: 'PROD-3'},
}),
{status: 200, headers: {'content-type': 'application/json'}},
);
});
await command.run();
const logMessages = logStub.getCalls().map((c) => String(c.args[0] ?? ''));
const allLogs = logMessages.join('\n');
// Should have logged the transition through both statuses
expect(allLogs).to.include('in_progress');
expect(allLogs).to.include('completed');
// And the explicit "Process completed successfully" final message
expect(logMessages.some((m) => /completed successfully/i.test(m))).to.equal(true);
});
it('times out if process does not complete', async () => {
const command: any = new ReplicationsWait([], config);
stubParse(command, {'tenant-id': 'zzxy_prd', timeout: 0.1, interval: 0.05}, {'process-id': 'proc-timeout'});
await command.init();
sinon.stub(command, 'requireOAuthCredentials').returns(void 0);
sinon.stub(command, 'jsonEnabled').returns(false);
sinon.stub(command, 'log').returns(void 0);
sinon.stub(command, 'resolvedConfig').get(() => ({values: {shortCode: 'kv7kzm78', tenantId: 'zzxy_prd'}}));
sinon.stub(command, 'getOAuthStrategy').returns({getAuthorizationHeader: async () => 'Bearer test'});
const errorStub = sinon.stub(command, 'error').throws(new Error('Timeout error'));
sinon.stub(globalThis, 'fetch').callsFake(async () => {
return new Response(
JSON.stringify({
id: 'proc-timeout',
status: 'in_progress',
startTime: '2025-01-01T03:00:00Z',
initiatedBy: 'user@example.com',
productItem: {productId: 'PROD-4'},
}),
{status: 200, headers: {'content-type': 'application/json'}},
);
});
try {
await command.run();
expect.fail('Should have thrown');
} catch {
expect(errorStub.calledOnce).to.equal(true);
}
});
it('handles API errors during polling', async () => {
const command: any = new ReplicationsWait([], config);
stubParse(command, {'tenant-id': 'zzxy_prd', timeout: 10, interval: 0}, {'process-id': 'proc-error'});
await command.init();
sinon.stub(command, 'requireOAuthCredentials').returns(void 0);
sinon.stub(command, 'jsonEnabled').returns(true);
sinon.stub(command, 'resolvedConfig').get(() => ({values: {shortCode: 'kv7kzm78', tenantId: 'zzxy_prd'}}));
sinon.stub(command, 'getOAuthStrategy').returns({getAuthorizationHeader: async () => 'Bearer test'});
const errorStub = sinon.stub(command, 'error').throws(new Error('Expected error'));
sinon.stub(globalThis, 'fetch').resolves(
new Response(JSON.stringify({title: 'Not Found', detail: 'Process not found'}), {
status: 404,
headers: {'content-type': 'application/json'},
}),
);
try {
await command.run();
expect.fail('Should have thrown');
} catch {
expect(errorStub.calledOnce).to.equal(true);
}
});
});
});