|
| 1 | +/* |
| 2 | + * Copyright (c) 2025, salesforce.com, inc. |
| 3 | + * All rights reserved. |
| 4 | + * Licensed under the BSD 3-Clause license. |
| 5 | + * For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause |
| 6 | + */ |
| 7 | + |
| 8 | +import { expect, test } from '@oclif/test'; |
| 9 | +import * as sinon from 'sinon'; |
| 10 | +import { Org } from '@salesforce/core'; |
| 11 | +import * as createWorkItemModule from '../../../../src/utils/createWorkItem'; |
| 12 | + |
| 13 | +describe('devops work-item create', () => { |
| 14 | + let sandbox: sinon.SinonSandbox; |
| 15 | + const mockConnection = { getApiVersion: () => '65.0' }; |
| 16 | + const mockOrg = { id: '1', getOrgId: () => '1', getConnection: () => mockConnection }; |
| 17 | + |
| 18 | + beforeEach(() => { |
| 19 | + sandbox = sinon.createSandbox(); |
| 20 | + }); |
| 21 | + |
| 22 | + afterEach(() => { |
| 23 | + sandbox.restore(); |
| 24 | + }); |
| 25 | + |
| 26 | + describe('successful creation', () => { |
| 27 | + test |
| 28 | + .stdout() |
| 29 | + .do(() => { |
| 30 | + // eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 31 | + sandbox.stub(Org, 'create' as any).returns(mockOrg); |
| 32 | + sandbox.stub(createWorkItemModule, 'createWorkItem').resolves({ |
| 33 | + success: true, |
| 34 | + workItemId: 'WI001', |
| 35 | + workItemName: 'WI-001', |
| 36 | + subject: 'Fix bug', |
| 37 | + }); |
| 38 | + }) |
| 39 | + .command([ |
| 40 | + 'devops:work-item:create', |
| 41 | + '--target-org', |
| 42 | + 'testOrg', |
| 43 | + '--project-id', |
| 44 | + 'PROJ001', |
| 45 | + '--subject', |
| 46 | + 'Fix bug', |
| 47 | + ]) |
| 48 | + .it('logs success message', (ctx) => { |
| 49 | + expect(ctx.stdout).to.contain('Successfully created work item'); |
| 50 | + }); |
| 51 | + }); |
| 52 | + |
| 53 | + describe('successful creation with no workItemName', () => { |
| 54 | + test |
| 55 | + .stdout() |
| 56 | + .do(() => { |
| 57 | + // eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 58 | + sandbox.stub(Org, 'create' as any).returns(mockOrg); |
| 59 | + sandbox.stub(createWorkItemModule, 'createWorkItem').resolves({ |
| 60 | + success: true, |
| 61 | + workItemId: 'WI001', |
| 62 | + subject: 'Fix bug', |
| 63 | + }); |
| 64 | + }) |
| 65 | + .command([ |
| 66 | + 'devops:work-item:create', |
| 67 | + '--target-org', |
| 68 | + 'testOrg', |
| 69 | + '--project-id', |
| 70 | + 'PROJ001', |
| 71 | + '--subject', |
| 72 | + 'Fix bug', |
| 73 | + ]) |
| 74 | + .it('falls back to workItemId in log', (ctx) => { |
| 75 | + expect(ctx.stdout).to.contain('WI001'); |
| 76 | + }); |
| 77 | + }); |
| 78 | + |
| 79 | + describe('creation failure', () => { |
| 80 | + test |
| 81 | + .stdout() |
| 82 | + .stderr() |
| 83 | + .do(() => { |
| 84 | + // eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 85 | + sandbox.stub(Org, 'create' as any).returns(mockOrg); |
| 86 | + sandbox.stub(createWorkItemModule, 'createWorkItem').resolves({ |
| 87 | + success: false, |
| 88 | + error: 'Not found', |
| 89 | + }); |
| 90 | + }) |
| 91 | + .command([ |
| 92 | + 'devops:work-item:create', |
| 93 | + '--target-org', |
| 94 | + 'testOrg', |
| 95 | + '--project-id', |
| 96 | + 'PROJ001', |
| 97 | + '--subject', |
| 98 | + 'Fix bug', |
| 99 | + ]) |
| 100 | + .catch(() => {}) |
| 101 | + .it('shows failure error', (ctx) => { |
| 102 | + expect(ctx.stderr).to.contain('Failed to create work item'); |
| 103 | + }); |
| 104 | + }); |
| 105 | + |
| 106 | + describe('DevOps Center not enabled', () => { |
| 107 | + test |
| 108 | + .stdout() |
| 109 | + .stderr() |
| 110 | + .do(() => { |
| 111 | + // eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 112 | + sandbox.stub(Org, 'create' as any).returns(mockOrg); |
| 113 | + sandbox |
| 114 | + .stub(createWorkItemModule, 'createWorkItem') |
| 115 | + .rejects(new Error("sObject type 'WorkItem' is not supported")); |
| 116 | + }) |
| 117 | + .command([ |
| 118 | + 'devops:work-item:create', |
| 119 | + '--target-org', |
| 120 | + 'testOrg', |
| 121 | + '--project-id', |
| 122 | + 'PROJ001', |
| 123 | + '--subject', |
| 124 | + 'Fix bug', |
| 125 | + ]) |
| 126 | + .catch(() => {}) |
| 127 | + .it('shows DevOps Center not enabled error', (ctx) => { |
| 128 | + expect(ctx.stderr).to.contain('DevOps Center is not enabled'); |
| 129 | + }); |
| 130 | + }); |
| 131 | + |
| 132 | + describe('rethrows other errors', () => { |
| 133 | + test |
| 134 | + .stdout() |
| 135 | + .stderr() |
| 136 | + .do(() => { |
| 137 | + // eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 138 | + sandbox.stub(Org, 'create' as any).returns(mockOrg); |
| 139 | + sandbox.stub(createWorkItemModule, 'createWorkItem').rejects(new Error('Network error')); |
| 140 | + }) |
| 141 | + .command([ |
| 142 | + 'devops:work-item:create', |
| 143 | + '--target-org', |
| 144 | + 'testOrg', |
| 145 | + '--project-id', |
| 146 | + 'PROJ001', |
| 147 | + '--subject', |
| 148 | + 'Fix bug', |
| 149 | + ]) |
| 150 | + .catch((err) => { |
| 151 | + expect(err.message).to.contain('Network error'); |
| 152 | + }) |
| 153 | + .it('rethrows non-DevOps errors', () => {}); |
| 154 | + }); |
| 155 | +}); |
0 commit comments