|
| 1 | +/* |
| 2 | + * Copyright (c) 2025, Salesforce, Inc. |
| 3 | + * SPDX-License-Identifier: Apache-2 |
| 4 | + * For full license text, see the license.txt file in the repo root or http://www.apache.org/licenses/LICENSE-2.0 |
| 5 | + */ |
| 6 | + |
| 7 | +import {expect} from 'chai'; |
| 8 | +import sinon from 'sinon'; |
| 9 | +import SandboxUpdate from '../../../src/commands/sandbox/update.js'; |
| 10 | +import { |
| 11 | + createIsolatedConfigHooks, |
| 12 | + createTestCommand, |
| 13 | + makeCommandThrowOnError, |
| 14 | + runSilent, |
| 15 | + stubJsonEnabled, |
| 16 | +} from '../../helpers/test-setup.js'; |
| 17 | + |
| 18 | +function stubOdsClient(command: any, client: Partial<{PATCH: any}>): void { |
| 19 | + Object.defineProperty(command, 'odsClient', { |
| 20 | + value: client, |
| 21 | + configurable: true, |
| 22 | + }); |
| 23 | +} |
| 24 | + |
| 25 | +function stubOdsHost(command: any, host = 'admin.dx.test.com'): void { |
| 26 | + Object.defineProperty(command, 'odsHost', { |
| 27 | + value: host, |
| 28 | + configurable: true, |
| 29 | + }); |
| 30 | +} |
| 31 | + |
| 32 | +describe('sandbox update', () => { |
| 33 | + const hooks = createIsolatedConfigHooks(); |
| 34 | + |
| 35 | + beforeEach(async () => { |
| 36 | + await hooks.beforeEach(); |
| 37 | + }); |
| 38 | + |
| 39 | + afterEach(() => { |
| 40 | + sinon.restore(); |
| 41 | + hooks.afterEach(); |
| 42 | + }); |
| 43 | + |
| 44 | + async function setupCommand(flags: Record<string, unknown>, args: Record<string, unknown>): Promise<any> { |
| 45 | + const config = hooks.getConfig(); |
| 46 | + const command = await createTestCommand(SandboxUpdate as any, config, flags, args); |
| 47 | + |
| 48 | + stubOdsHost(command); |
| 49 | + (command as any).log = () => {}; |
| 50 | + makeCommandThrowOnError(command); |
| 51 | + |
| 52 | + return command; |
| 53 | + } |
| 54 | + |
| 55 | + it('sends resourceProfile in PATCH body when --resource-profile is set', async () => { |
| 56 | + const command = await setupCommand({'resource-profile': 'large'}, {sandboxId: 'zzzz-001'}); |
| 57 | + |
| 58 | + sinon.stub(command as any, 'resolveSandboxId').resolves('sb-uuid-123'); |
| 59 | + stubJsonEnabled(command, true); |
| 60 | + |
| 61 | + let requestUrl: string | undefined; |
| 62 | + let requestOptions: any; |
| 63 | + |
| 64 | + stubOdsClient(command, { |
| 65 | + async PATCH(url: string, options: any) { |
| 66 | + requestUrl = url; |
| 67 | + requestOptions = options; |
| 68 | + return { |
| 69 | + data: { |
| 70 | + data: { |
| 71 | + id: 'sb-uuid-123', |
| 72 | + realm: 'zzzz', |
| 73 | + state: 'started', |
| 74 | + resourceProfile: 'large', |
| 75 | + }, |
| 76 | + }, |
| 77 | + }; |
| 78 | + }, |
| 79 | + }); |
| 80 | + |
| 81 | + const result: any = await runSilent(() => command.run()); |
| 82 | + |
| 83 | + expect(requestUrl).to.equal('/sandboxes/{sandboxId}'); |
| 84 | + expect(requestOptions).to.have.nested.property('params.path.sandboxId', 'sb-uuid-123'); |
| 85 | + expect(requestOptions).to.have.nested.property('body.resourceProfile', 'large'); |
| 86 | + expect(result.resourceProfile).to.equal('large'); |
| 87 | + }); |
| 88 | + |
| 89 | + it('allows combining --resource-profile with other update flags', async () => { |
| 90 | + const command = await setupCommand( |
| 91 | + {'resource-profile': 'xlarge', ttl: 48, tags: 'ci,nightly'}, |
| 92 | + {sandboxId: 'zzzz-001'}, |
| 93 | + ); |
| 94 | + |
| 95 | + sinon.stub(command as any, 'resolveSandboxId').resolves('sb-uuid-123'); |
| 96 | + stubJsonEnabled(command, true); |
| 97 | + |
| 98 | + let requestOptions: any; |
| 99 | + stubOdsClient(command, { |
| 100 | + async PATCH(_: string, options: any) { |
| 101 | + requestOptions = options; |
| 102 | + return { |
| 103 | + data: { |
| 104 | + data: { |
| 105 | + id: 'sb-uuid-123', |
| 106 | + realm: 'zzzz', |
| 107 | + state: 'started', |
| 108 | + resourceProfile: 'xlarge', |
| 109 | + tags: ['ci', 'nightly'], |
| 110 | + }, |
| 111 | + }, |
| 112 | + }; |
| 113 | + }, |
| 114 | + }); |
| 115 | + |
| 116 | + await runSilent(() => command.run()); |
| 117 | + |
| 118 | + expect(requestOptions.body).to.include({ |
| 119 | + ttl: 48, |
| 120 | + resourceProfile: 'xlarge', |
| 121 | + }); |
| 122 | + expect(requestOptions.body.tags).to.deep.equal(['ci', 'nightly']); |
| 123 | + }); |
| 124 | + |
| 125 | + it('supports --no-auto-scheduled and sends autoScheduled=false', async () => { |
| 126 | + const command = await setupCommand({'auto-scheduled': false}, {sandboxId: 'zzzz-001'}); |
| 127 | + |
| 128 | + sinon.stub(command as any, 'resolveSandboxId').resolves('sb-uuid-123'); |
| 129 | + stubJsonEnabled(command, true); |
| 130 | + |
| 131 | + let requestOptions: any; |
| 132 | + stubOdsClient(command, { |
| 133 | + async PATCH(_: string, options: any) { |
| 134 | + requestOptions = options; |
| 135 | + return { |
| 136 | + data: { |
| 137 | + data: { |
| 138 | + id: 'sb-uuid-123', |
| 139 | + realm: 'zzzz', |
| 140 | + state: 'started', |
| 141 | + autoScheduled: false, |
| 142 | + }, |
| 143 | + }, |
| 144 | + }; |
| 145 | + }, |
| 146 | + }); |
| 147 | + |
| 148 | + await runSilent(() => command.run()); |
| 149 | + |
| 150 | + expect(requestOptions.body).to.include({ |
| 151 | + autoScheduled: false, |
| 152 | + }); |
| 153 | + }); |
| 154 | + |
| 155 | + it('trims tags and emails when combined with --resource-profile', async () => { |
| 156 | + const command = await setupCommand( |
| 157 | + { |
| 158 | + 'resource-profile': 'xxlarge', |
| 159 | + tags: ' ci , nightly ', |
| 160 | + emails: ' dev@example.com , qa@example.com ', |
| 161 | + }, |
| 162 | + {sandboxId: 'zzzz-001'}, |
| 163 | + ); |
| 164 | + |
| 165 | + sinon.stub(command as any, 'resolveSandboxId').resolves('sb-uuid-123'); |
| 166 | + stubJsonEnabled(command, true); |
| 167 | + |
| 168 | + let requestOptions: any; |
| 169 | + stubOdsClient(command, { |
| 170 | + async PATCH(_: string, options: any) { |
| 171 | + requestOptions = options; |
| 172 | + return { |
| 173 | + data: { |
| 174 | + data: { |
| 175 | + id: 'sb-uuid-123', |
| 176 | + realm: 'zzzz', |
| 177 | + state: 'started', |
| 178 | + resourceProfile: 'xxlarge', |
| 179 | + tags: ['ci', 'nightly'], |
| 180 | + emails: ['dev@example.com', 'qa@example.com'], |
| 181 | + }, |
| 182 | + }, |
| 183 | + }; |
| 184 | + }, |
| 185 | + }); |
| 186 | + |
| 187 | + const result: any = await runSilent(() => command.run()); |
| 188 | + |
| 189 | + expect(requestOptions.body.resourceProfile).to.equal('xxlarge'); |
| 190 | + expect(requestOptions.body.tags).to.deep.equal(['ci', 'nightly']); |
| 191 | + expect(requestOptions.body.emails).to.deep.equal(['dev@example.com', 'qa@example.com']); |
| 192 | + expect(result.tags).to.deep.equal(['ci', 'nightly']); |
| 193 | + expect(result.emails).to.deep.equal(['dev@example.com', 'qa@example.com']); |
| 194 | + }); |
| 195 | + |
| 196 | + it('requires at least one update flag including --resource-profile', async () => { |
| 197 | + const command = await setupCommand({}, {sandboxId: 'zzzz-001'}); |
| 198 | + |
| 199 | + sinon.stub(command as any, 'resolveSandboxId').resolves('sb-uuid-123'); |
| 200 | + stubOdsClient(command, { |
| 201 | + async PATCH() { |
| 202 | + throw new Error('PATCH should not be called when no flags are provided'); |
| 203 | + }, |
| 204 | + }); |
| 205 | + |
| 206 | + try { |
| 207 | + await runSilent(() => command.run()); |
| 208 | + expect.fail('Expected command to error when no update flags are provided'); |
| 209 | + } catch (error: any) { |
| 210 | + expect(error.message).to.include('At least one update flag is required'); |
| 211 | + expect(error.message).to.include('--resource-profile'); |
| 212 | + } |
| 213 | + }); |
| 214 | + |
| 215 | + it('throws a helpful error when API update fails', async () => { |
| 216 | + const command = await setupCommand({'resource-profile': 'large'}, {sandboxId: 'zzzz-001'}); |
| 217 | + |
| 218 | + sinon.stub(command as any, 'resolveSandboxId').resolves('sb-uuid-123'); |
| 219 | + stubOdsClient(command, { |
| 220 | + async PATCH() { |
| 221 | + return { |
| 222 | + data: undefined, |
| 223 | + error: {error: {message: 'Profile update not allowed in current state'}}, |
| 224 | + response: {statusText: 'Bad Request'}, |
| 225 | + }; |
| 226 | + }, |
| 227 | + }); |
| 228 | + |
| 229 | + try { |
| 230 | + await runSilent(() => command.run()); |
| 231 | + expect.fail('Expected command to throw on API error'); |
| 232 | + } catch (error: any) { |
| 233 | + expect(error.message).to.include('Failed to update sandbox'); |
| 234 | + expect(error.message).to.match(/Profile update not allowed|Bad Request/); |
| 235 | + } |
| 236 | + }); |
| 237 | +}); |
0 commit comments