|
| 1 | +/* |
| 2 | + * Copyright 2026, Salesforce, Inc. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +import esmock from 'esmock'; |
| 18 | +import { expect, test } from '@oclif/test'; |
| 19 | +import sinon from 'sinon'; |
| 20 | +import { Org } from '@salesforce/core'; |
| 21 | + |
| 22 | +describe('devops pipeline activate', () => { |
| 23 | + let sandbox: sinon.SinonSandbox; |
| 24 | + // eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 25 | + let ActivateCommand: any; |
| 26 | + const mockConnection = { getApiVersion: () => '65.0' }; |
| 27 | + const mockOrg = { id: '1', getOrgId: () => '1', getConnection: () => mockConnection, getUsername: () => 'testOrg' }; |
| 28 | + const activatePipelineStub = sinon.stub(); |
| 29 | + const fetchPipelineStagesStub = sinon.stub(); |
| 30 | + |
| 31 | + before(async () => { |
| 32 | + const mod = await esmock('../../../../src/commands/devops/pipeline/activate.js', { |
| 33 | + '../../../../src/utils/activatePipeline.js': { |
| 34 | + activatePipeline: activatePipelineStub, |
| 35 | + }, |
| 36 | + '../../../../src/utils/pipelineUtils.js': { |
| 37 | + fetchPipelineStages: fetchPipelineStagesStub, |
| 38 | + }, |
| 39 | + }); |
| 40 | + ActivateCommand = mod.default; |
| 41 | + }); |
| 42 | + |
| 43 | + beforeEach(() => { |
| 44 | + sandbox = sinon.createSandbox(); |
| 45 | + activatePipelineStub.reset(); |
| 46 | + fetchPipelineStagesStub.reset(); |
| 47 | + }); |
| 48 | + |
| 49 | + afterEach(() => { |
| 50 | + sandbox.restore(); |
| 51 | + }); |
| 52 | + |
| 53 | + describe('successful activation', () => { |
| 54 | + test |
| 55 | + .stdout() |
| 56 | + .stderr() |
| 57 | + .it('activates pipeline and logs success', async (ctx) => { |
| 58 | + // eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 59 | + sandbox.stub(Org, 'create' as any).returns(mockOrg); |
| 60 | + fetchPipelineStagesStub.resolves([ |
| 61 | + { Id: '1', Name: 'Integration' }, |
| 62 | + { Id: '2', Name: 'UAT' }, |
| 63 | + { Id: '3', Name: 'Production' }, |
| 64 | + ]); |
| 65 | + activatePipelineStub.resolves({ |
| 66 | + success: true, |
| 67 | + pipelineId: '0XB000000000001', |
| 68 | + status: 'Active', |
| 69 | + }); |
| 70 | + |
| 71 | + await ActivateCommand.run(['--target-org', 'testOrg', '--pipeline-id', '0XB000000000001']); |
| 72 | + |
| 73 | + expect(ctx.stdout).to.contain('Successfully activated the pipeline.'); |
| 74 | + expect(ctx.stdout).to.contain('0XB000000000001'); |
| 75 | + expect(ctx.stdout).to.contain('Active'); |
| 76 | + expect(ctx.stdout).to.contain('3'); |
| 77 | + }); |
| 78 | + }); |
| 79 | + |
| 80 | + describe('no stages error', () => { |
| 81 | + test |
| 82 | + .stdout() |
| 83 | + .stderr() |
| 84 | + .it('errors when pipeline has no stages', async (ctx) => { |
| 85 | + // eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 86 | + sandbox.stub(Org, 'create' as any).returns(mockOrg); |
| 87 | + fetchPipelineStagesStub.resolves([]); |
| 88 | + |
| 89 | + try { |
| 90 | + await ActivateCommand.run(['--target-org', 'testOrg', '--pipeline-id', '0XB000000000001']); |
| 91 | + expect.fail('should have thrown'); |
| 92 | + } catch (e) { |
| 93 | + // expected |
| 94 | + } |
| 95 | + |
| 96 | + expect(ctx.stderr).to.contain('Add at least one stage'); |
| 97 | + }); |
| 98 | + }); |
| 99 | + |
| 100 | + describe('already active error', () => { |
| 101 | + test |
| 102 | + .stdout() |
| 103 | + .stderr() |
| 104 | + .it('errors when pipeline is already active', async (ctx) => { |
| 105 | + // eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 106 | + sandbox.stub(Org, 'create' as any).returns(mockOrg); |
| 107 | + fetchPipelineStagesStub.resolves([{ Id: '1', Name: 'Integration' }]); |
| 108 | + activatePipelineStub.rejects(new Error('Pipeline is already active')); |
| 109 | + |
| 110 | + try { |
| 111 | + await ActivateCommand.run(['--target-org', 'testOrg', '--pipeline-id', '0XB000000000001']); |
| 112 | + expect.fail('should have thrown'); |
| 113 | + } catch (e) { |
| 114 | + // expected |
| 115 | + } |
| 116 | + |
| 117 | + expect(ctx.stderr).to.contain('already active'); |
| 118 | + }); |
| 119 | + }); |
| 120 | + |
| 121 | + describe('DevOps Center not enabled', () => { |
| 122 | + test |
| 123 | + .stdout() |
| 124 | + .stderr() |
| 125 | + .it('shows DevOps Center not enabled error', async (ctx) => { |
| 126 | + // eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 127 | + sandbox.stub(Org, 'create' as any).returns(mockOrg); |
| 128 | + fetchPipelineStagesStub.rejects(new Error("sObject type 'DevopsPipelineStage' is not supported")); |
| 129 | + |
| 130 | + try { |
| 131 | + await ActivateCommand.run(['--target-org', 'testOrg', '--pipeline-id', '0XB000000000001']); |
| 132 | + } catch (e) { |
| 133 | + // expected |
| 134 | + } |
| 135 | + |
| 136 | + expect(ctx.stderr).to.contain("DevOps Center isn't enabled"); |
| 137 | + }); |
| 138 | + }); |
| 139 | + |
| 140 | + describe('rethrows other errors', () => { |
| 141 | + test |
| 142 | + .stdout() |
| 143 | + .stderr() |
| 144 | + .it('rethrows non-DevOps errors', async () => { |
| 145 | + // eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 146 | + sandbox.stub(Org, 'create' as any).returns(mockOrg); |
| 147 | + fetchPipelineStagesStub.resolves([{ Id: '1', Name: 'Integration' }]); |
| 148 | + activatePipelineStub.rejects(new Error('Network error')); |
| 149 | + |
| 150 | + try { |
| 151 | + await ActivateCommand.run(['--target-org', 'testOrg', '--pipeline-id', '0XB000000000001']); |
| 152 | + expect.fail('should have thrown'); |
| 153 | + } catch (e: unknown) { |
| 154 | + expect((e as Error).message).to.contain('Network error'); |
| 155 | + } |
| 156 | + }); |
| 157 | + }); |
| 158 | +}); |
0 commit comments