|
| 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 | +import {expect} from 'chai'; |
| 7 | +import sinon from 'sinon'; |
| 8 | +import {Config} from '@oclif/core'; |
| 9 | +import {CartridgeCommand} from '@salesforce/b2c-tooling-sdk/cli'; |
| 10 | +import {stubParse} from '../helpers/stub-parse.js'; |
| 11 | +import {isolateConfig, restoreConfig} from '../helpers/config-isolation.js'; |
| 12 | + |
| 13 | +class TestCartridgeCommand extends CartridgeCommand<typeof TestCartridgeCommand> { |
| 14 | + static id = 'test:cartridge'; |
| 15 | + async run(): Promise<void> {} |
| 16 | + |
| 17 | + // Expose protected for testing |
| 18 | + public get testCartridgePath() { |
| 19 | + return this.cartridgePath; |
| 20 | + } |
| 21 | + public get testCartridgeOptions() { |
| 22 | + return this.cartridgeOptions; |
| 23 | + } |
| 24 | + public get testCartridgeProviderRunner() { |
| 25 | + return this.cartridgeProviderRunner; |
| 26 | + } |
| 27 | + public testFindCartridgesWithProviders(dir?: string) { |
| 28 | + return this.findCartridgesWithProviders(dir); |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | +describe('cli/cartridge-command', () => { |
| 33 | + let config: Config; |
| 34 | + let command: TestCartridgeCommand; |
| 35 | + |
| 36 | + beforeEach(async () => { |
| 37 | + isolateConfig(); |
| 38 | + config = await Config.load(); |
| 39 | + command = new TestCartridgeCommand([], config); |
| 40 | + }); |
| 41 | + |
| 42 | + afterEach(() => { |
| 43 | + sinon.restore(); |
| 44 | + restoreConfig(); |
| 45 | + }); |
| 46 | + |
| 47 | + describe('cartridgePath', () => { |
| 48 | + it('returns cartridgePath from args', async () => { |
| 49 | + stubParse(command, {}, {cartridgePath: '/path/to/cartridges'}); |
| 50 | + await command.init(); |
| 51 | + expect(command.testCartridgePath).to.equal('/path/to/cartridges'); |
| 52 | + }); |
| 53 | + |
| 54 | + it('defaults to current directory', async () => { |
| 55 | + stubParse(command, {}, {cartridgePath: '.'}); |
| 56 | + await command.init(); |
| 57 | + expect(command.testCartridgePath).to.equal('.'); |
| 58 | + }); |
| 59 | + }); |
| 60 | + |
| 61 | + describe('cartridgeOptions', () => { |
| 62 | + it('returns include/exclude from flags', async () => { |
| 63 | + stubParse( |
| 64 | + command, |
| 65 | + { |
| 66 | + cartridge: ['app_storefront', 'app_custom'], |
| 67 | + 'exclude-cartridge': ['bm_extensions'], |
| 68 | + }, |
| 69 | + {cartridgePath: '.'}, |
| 70 | + ); |
| 71 | + await command.init(); |
| 72 | + |
| 73 | + const options = command.testCartridgeOptions; |
| 74 | + expect(options.include).to.deep.equal(['app_storefront', 'app_custom']); |
| 75 | + expect(options.exclude).to.deep.equal(['bm_extensions']); |
| 76 | + }); |
| 77 | + |
| 78 | + it('returns undefined for unset include/exclude', async () => { |
| 79 | + stubParse(command, {}, {cartridgePath: '.'}); |
| 80 | + await command.init(); |
| 81 | + |
| 82 | + const options = command.testCartridgeOptions; |
| 83 | + expect(options.include).to.be.undefined; |
| 84 | + expect(options.exclude).to.be.undefined; |
| 85 | + }); |
| 86 | + }); |
| 87 | + |
| 88 | + describe('collectCartridgeProviders', () => { |
| 89 | + it('creates CartridgeProviderRunner during init', async () => { |
| 90 | + stubParse(command, {}, {cartridgePath: '.'}); |
| 91 | + await command.init(); |
| 92 | + // Runner is created even with no plugins |
| 93 | + expect(command.testCartridgeProviderRunner).to.exist; |
| 94 | + }); |
| 95 | + }); |
| 96 | + |
| 97 | + describe('findCartridgesWithProviders', () => { |
| 98 | + it('returns empty array when no cartridges found', async () => { |
| 99 | + stubParse(command, {server: 'test.demandware.net'}, {cartridgePath: '.'}); |
| 100 | + await command.init(); |
| 101 | + |
| 102 | + // With no .project files in cwd, returns empty array |
| 103 | + const cartridges = await command.testFindCartridgesWithProviders(); |
| 104 | + expect(cartridges).to.be.an('array'); |
| 105 | + }); |
| 106 | + |
| 107 | + it('uses custom directory when provided', async () => { |
| 108 | + stubParse(command, {server: 'test.demandware.net'}, {cartridgePath: '/default/path'}); |
| 109 | + await command.init(); |
| 110 | + |
| 111 | + // Should not throw when using a custom directory |
| 112 | + const cartridges = await command.testFindCartridgesWithProviders('/tmp'); |
| 113 | + expect(cartridges).to.be.an('array'); |
| 114 | + }); |
| 115 | + }); |
| 116 | +}); |
0 commit comments