|
| 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 project create', () => { |
| 23 | + let sandbox: sinon.SinonSandbox; |
| 24 | + // eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 25 | + let CreateCommand: any; |
| 26 | + const mockConnection = { getApiVersion: () => '65.0' }; |
| 27 | + const mockOrg = { id: '1', getOrgId: () => '1', getConnection: () => mockConnection }; |
| 28 | + const createProjectStub = sinon.stub(); |
| 29 | + |
| 30 | + before(async () => { |
| 31 | + const mod = await esmock('../../../../src/commands/devops/project/create.js', { |
| 32 | + '../../../../src/utils/createProject.js': { |
| 33 | + createProject: createProjectStub, |
| 34 | + }, |
| 35 | + }); |
| 36 | + CreateCommand = mod.default; |
| 37 | + }); |
| 38 | + |
| 39 | + beforeEach(() => { |
| 40 | + sandbox = sinon.createSandbox(); |
| 41 | + createProjectStub.reset(); |
| 42 | + }); |
| 43 | + |
| 44 | + afterEach(() => { |
| 45 | + sandbox.restore(); |
| 46 | + }); |
| 47 | + |
| 48 | + describe('successful creation', () => { |
| 49 | + test |
| 50 | + .stdout() |
| 51 | + .stderr() |
| 52 | + .it('logs success with name and id', async (ctx) => { |
| 53 | + // eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 54 | + sandbox.stub(Org, 'create' as any).returns(mockOrg); |
| 55 | + createProjectStub.resolves({ |
| 56 | + success: true, |
| 57 | + projectId: '1Qg000000000001', |
| 58 | + name: 'MyApp Release', |
| 59 | + }); |
| 60 | + |
| 61 | + await CreateCommand.run(['--target-org', 'testOrg', '--name', 'MyApp Release']); |
| 62 | + |
| 63 | + expect(ctx.stdout).to.contain('Successfully created project: MyApp Release'); |
| 64 | + expect(ctx.stdout).to.contain('1Qg000000000001'); |
| 65 | + }); |
| 66 | + }); |
| 67 | + |
| 68 | + describe('successful creation with description', () => { |
| 69 | + test |
| 70 | + .stdout() |
| 71 | + .stderr() |
| 72 | + .it('logs description when provided', async (ctx) => { |
| 73 | + // eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 74 | + sandbox.stub(Org, 'create' as any).returns(mockOrg); |
| 75 | + createProjectStub.resolves({ |
| 76 | + success: true, |
| 77 | + projectId: '1Qg000000000002', |
| 78 | + name: 'Platform Update', |
| 79 | + description: 'Platform services update', |
| 80 | + }); |
| 81 | + |
| 82 | + await CreateCommand.run([ |
| 83 | + '--target-org', |
| 84 | + 'testOrg', |
| 85 | + '--name', |
| 86 | + 'Platform Update', |
| 87 | + '--description', |
| 88 | + 'Platform services update', |
| 89 | + ]); |
| 90 | + |
| 91 | + expect(ctx.stdout).to.contain('Platform Update'); |
| 92 | + expect(ctx.stdout).to.contain('Platform services update'); |
| 93 | + }); |
| 94 | + }); |
| 95 | + |
| 96 | + describe('creation failure', () => { |
| 97 | + test |
| 98 | + .stdout() |
| 99 | + .stderr() |
| 100 | + .it('shows failure error', async (ctx) => { |
| 101 | + // eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 102 | + sandbox.stub(Org, 'create' as any).returns(mockOrg); |
| 103 | + createProjectStub.resolves({ |
| 104 | + success: false, |
| 105 | + error: 'DUPLICATE_VALUE: Name already exists', |
| 106 | + }); |
| 107 | + |
| 108 | + try { |
| 109 | + await CreateCommand.run(['--target-org', 'testOrg', '--name', 'Duplicate']); |
| 110 | + } catch (e) { |
| 111 | + // expected |
| 112 | + } |
| 113 | + |
| 114 | + expect(ctx.stderr).to.contain('Failed to create project'); |
| 115 | + }); |
| 116 | + }); |
| 117 | + |
| 118 | + describe('DevOps Center not enabled', () => { |
| 119 | + test |
| 120 | + .stdout() |
| 121 | + .stderr() |
| 122 | + .it('shows DevOps Center not enabled error', async (ctx) => { |
| 123 | + // eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 124 | + sandbox.stub(Org, 'create' as any).returns(mockOrg); |
| 125 | + createProjectStub.rejects(new Error("sObject type 'DevopsProject' is not supported")); |
| 126 | + |
| 127 | + try { |
| 128 | + await CreateCommand.run(['--target-org', 'testOrg', '--name', 'Test']); |
| 129 | + } catch (e) { |
| 130 | + // expected |
| 131 | + } |
| 132 | + |
| 133 | + expect(ctx.stderr).to.contain("DevOps Center isn't enabled"); |
| 134 | + }); |
| 135 | + }); |
| 136 | + |
| 137 | + describe('rethrows other errors', () => { |
| 138 | + test |
| 139 | + .stdout() |
| 140 | + .stderr() |
| 141 | + .it('rethrows non-DevOps errors', async () => { |
| 142 | + // eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 143 | + sandbox.stub(Org, 'create' as any).returns(mockOrg); |
| 144 | + createProjectStub.rejects(new Error('Network error')); |
| 145 | + |
| 146 | + try { |
| 147 | + await CreateCommand.run(['--target-org', 'testOrg', '--name', 'Test']); |
| 148 | + expect.fail('should have thrown'); |
| 149 | + } catch (e: unknown) { |
| 150 | + expect((e as Error).message).to.contain('Network error'); |
| 151 | + } |
| 152 | + }); |
| 153 | + }); |
| 154 | +}); |
0 commit comments