-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtransfer.test.ts
More file actions
249 lines (225 loc) · 7.28 KB
/
Copy pathtransfer.test.ts
File metadata and controls
249 lines (225 loc) · 7.28 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
import * as fs from 'node:fs';
import * as os from 'node:os';
import * as path from 'node:path';
import { TestContext, MockTestOrgData } from '@salesforce/core/testSetup';
import { expect } from 'chai';
import { stubSfCommandUx } from '@salesforce/sf-plugins-core';
import SetupTransfer from '../../../../src/commands/data/setup/transfer.js';
describe('data setup transfer', () => {
const $$ = new TestContext();
let sfCommandStubs: ReturnType<typeof stubSfCommandUx>;
const mockSourceOrg = new MockTestOrgData();
const mockTargetOrg = new MockTestOrgData();
const mockExportResponse = {
isSuccess: true,
metadata: {
dataSetName: 'testDefinition',
version: '1.0.0',
},
objects: [
{
objectName: 'Account',
recordCount: 2,
records: [
{ Id: '001xx000000001', Name: 'Test Account 1' },
{ Id: '001xx000000002', Name: 'Test Account 2' },
],
},
],
};
const mockImportResponse = {
isSuccess: true,
message: 'Import completed successfully',
};
beforeEach(async () => {
sfCommandStubs = stubSfCommandUx($$.SANDBOX);
await $$.stubAuths(mockSourceOrg, mockTargetOrg);
$$.fakeConnectionRequest = (request) => {
if (typeof request === 'string' || (request as { url: string }).url.includes('/export')) {
return Promise.resolve(mockExportResponse);
}
return Promise.resolve(mockImportResponse);
};
});
afterEach(() => {
$$.restore();
});
describe('flag validation', () => {
it('throws error when no flags are provided', async () => {
try {
await SetupTransfer.run(['--source-org', 'source@test.org', '--target-org', 'target@test.org']);
expect.fail('Should have thrown an error');
} catch (error) {
expect((error as Error).message).to.include('must provide either');
}
});
it('throws error when definition-identifier provided without version', async () => {
try {
await SetupTransfer.run([
'--definition-identifier',
'testDefinition',
'--source-org',
'source@test.org',
'--target-org',
'target@test.org',
]);
expect.fail('Should have thrown an error');
} catch (error) {
expect((error as Error).message).to.include('version is also required');
}
});
it('throws error when version provided without definition-identifier', async () => {
try {
await SetupTransfer.run([
'--version',
'1.0.0',
'--source-org',
'source@test.org',
'--target-org',
'target@test.org',
]);
expect.fail('Should have thrown an error');
} catch (error) {
expect((error as Error).message).to.include('definition-identifier is also required');
}
});
it('throws error when both standard and custom definition flags are provided', async () => {
const tmpDefFile = path.join(os.tmpdir(), `test-definition-${Date.now()}.json`);
fs.writeFileSync(tmpDefFile, '{}', 'utf8');
try {
await SetupTransfer.run([
'--definition-identifier',
'testDefinition',
'--version',
'1.0.0',
'--extended-definition-file',
tmpDefFile,
'--source-org',
'source@test.org',
'--target-org',
'target@test.org',
]);
expect.fail('Should have thrown an error');
} catch (error) {
expect((error as Error).message).to.include('Cannot use');
} finally {
fs.rmSync(tmpDefFile, { force: true });
}
});
});
describe('standard definition mode', () => {
it('successfully transfers data using standard definition', async () => {
const result = await SetupTransfer.run([
'--definition-identifier',
'testDefinition',
'--version',
'1.0.0',
'--source-org',
'source@test.org',
'--target-org',
'target@test.org',
'--json',
]);
expect(result.success).to.be.true;
expect(result.exportResponse).to.deep.include({
metadata: mockExportResponse.metadata,
objects: mockExportResponse.objects,
});
expect(result.importResponse).to.exist;
});
it('logs success messages during transfer', async () => {
await SetupTransfer.run([
'--definition-identifier',
'testDefinition',
'--version',
'1.0.0',
'--source-org',
'source@test.org',
'--target-org',
'target@test.org',
]);
const output = sfCommandStubs.log
.getCalls()
.flatMap((c) => c.args)
.join('\n');
expect(output).to.include('Export from source org completed successfully');
expect(output).to.include('Setup transfer completed successfully');
expect(output).to.include('Payload file saved to');
});
});
describe('error handling', () => {
it('throws error when export API returns error', async () => {
$$.fakeConnectionRequest = (request) => {
if (typeof request === 'string' || (request as { url: string }).url.includes('/export')) {
return Promise.resolve({
isSuccess: false,
errors: [{ message: 'Invalid definition identifier' }],
});
}
return Promise.resolve(mockImportResponse);
};
try {
await SetupTransfer.run([
'--definition-identifier',
'invalidDefinition',
'--version',
'1.0.0',
'--source-org',
'source@test.org',
'--target-org',
'target@test.org',
]);
expect.fail('Should have thrown an error');
} catch (error) {
expect((error as Error).message).to.include('Export failed');
expect((error as Error).message).to.include('Invalid definition identifier');
}
});
it('throws error when export API request fails', async () => {
$$.fakeConnectionRequest = (request) => {
if (typeof request === 'string' || (request as { url: string }).url.includes('/export')) {
return Promise.reject(new Error('REQUEST_FAILED: Bad Request'));
}
return Promise.resolve(mockImportResponse);
};
try {
await SetupTransfer.run([
'--definition-identifier',
'testDefinition',
'--version',
'1.0.0',
'--source-org',
'source@test.org',
'--target-org',
'target@test.org',
]);
expect.fail('Should have thrown an error');
} catch (error) {
expect((error as Error).message).to.include('REQUEST_FAILED');
}
});
});
describe('API version handling', () => {
it('uses specified api-version flag', async () => {
await SetupTransfer.run([
'--definition-identifier',
'testDefinition',
'--version',
'1.0.0',
'--source-org',
'source@test.org',
'--target-org',
'target@test.org',
'--api-version',
'60.0',
'--json',
]);
// Test passes if no error is thrown with api-version flag
const output = sfCommandStubs.log
.getCalls()
.flatMap((c) => c.args)
.join('\n');
expect(output).to.include('Setup transfer completed successfully');
});
});
});