From d3f72cdb788581f522de03e3408b4d754e2d0164 Mon Sep 17 00:00:00 2001 From: Netraj Patel Date: Fri, 21 Nov 2025 19:56:13 +0530 Subject: [PATCH 1/2] Removed deprecated code from config plugin --- packages/contentstack-config/README.md | 10 +++++----- .../commands/config/get/early-access-header.ts | 4 ++-- .../src/commands/config/set/region.ts | 17 ++--------------- packages/contentstack/README.md | 10 +++++----- 4 files changed, 14 insertions(+), 27 deletions(-) diff --git a/packages/contentstack-config/README.md b/packages/contentstack-config/README.md index d659d0e65e..019946c990 100644 --- a/packages/contentstack-config/README.md +++ b/packages/contentstack-config/README.md @@ -391,18 +391,18 @@ Set region for CLI ``` USAGE - $ csdx config:set:region [REGION] [-d -m --ui-host -n ] [--developer-hub ] - [--personalize ] [--launch ] + $ csdx config:set:region [REGION] [--cda --cma --ui-host -n ] [--developer-hub + ] [--personalize ] [--launch ] ARGUMENTS [REGION] Name for the region FLAGS - -d, --cda= Custom host to set for content delivery API, if this flag is added then cma, ui-host and + -n, --name= Name for the region, if this flag is added then cda, cma and ui-host flags are required + --cda= Custom host to set for content delivery API, if this flag is added then cma, ui-host and name flags are required - -m, --cma= Custom host to set for content management API, , if this flag is added then cda, ui-host + --cma= Custom host to set for content management API, , if this flag is added then cda, ui-host and name flags are required - -n, --name= Name for the region, if this flag is added then cda, cma and ui-host flags are required --developer-hub= Custom host to set for Developer hub API --launch= Custom host to set for Launch API --personalize= Custom host to set for Personalize API diff --git a/packages/contentstack-config/src/commands/config/get/early-access-header.ts b/packages/contentstack-config/src/commands/config/get/early-access-header.ts index 883cae1220..9d6dd2c897 100644 --- a/packages/contentstack-config/src/commands/config/get/early-access-header.ts +++ b/packages/contentstack-config/src/commands/config/get/early-access-header.ts @@ -8,9 +8,9 @@ export default class GetEarlyAccessHeaderCommand extends Command { async run() { try { - let config = configHandler.get(`earlyAccessHeaders`); + const config = configHandler.get(`earlyAccessHeaders`); if (config && Object.keys(config).length > 0) { - let tableData = Object.keys(config).map((key) => ({ + const tableData = Object.keys(config).map((key) => ({ ['Alias']: key, ['Early access header']: config[key], })); diff --git a/packages/contentstack-config/src/commands/config/set/region.ts b/packages/contentstack-config/src/commands/config/set/region.ts index fed827788d..8391580cab 100644 --- a/packages/contentstack-config/src/commands/config/set/region.ts +++ b/packages/contentstack-config/src/commands/config/set/region.ts @@ -1,34 +1,21 @@ -import { Command } from '@contentstack/cli-command'; -import { - cliux, - printFlagDeprecation, - flags as _flags, - authHandler, - FlagInput, - ArgInput, - args, -} from '@contentstack/cli-utilities'; +import { cliux, flags as _flags, authHandler, FlagInput, ArgInput, args } from '@contentstack/cli-utilities'; import { Region } from '../../../interfaces'; import { regionHandler, interactive } from '../../../utils'; -import { Args, BaseCommand } from '../../../base-command'; +import { BaseCommand } from '../../../base-command'; export default class RegionSetCommand extends BaseCommand { config: any; static description = 'Set region for CLI'; static flags: FlagInput = { cda: _flags.string({ - char: 'd', description: 'Custom host to set for content delivery API, if this flag is added then cma, ui-host and name flags are required', dependsOn: ['cma', 'ui-host', 'name'], - parse: printFlagDeprecation(['-d'], ['--cda']), }), cma: _flags.string({ - char: 'm', description: 'Custom host to set for content management API, , if this flag is added then cda, ui-host and name flags are required', dependsOn: ['cda', 'ui-host', 'name'], - parse: printFlagDeprecation(['-m'], ['--cma']), }), 'ui-host': _flags.string({ description: 'Custom UI host to set for CLI, if this flag is added then cda, cma and name flags are required', diff --git a/packages/contentstack/README.md b/packages/contentstack/README.md index 3e4d93611d..e19ed7b138 100644 --- a/packages/contentstack/README.md +++ b/packages/contentstack/README.md @@ -3416,18 +3416,18 @@ Set region for CLI ``` USAGE - $ csdx config:set:region [REGION] [-d -m --ui-host -n ] [--developer-hub ] - [--personalize ] [--launch ] + $ csdx config:set:region [REGION] [--cda --cma --ui-host -n ] [--developer-hub + ] [--personalize ] [--launch ] ARGUMENTS [REGION] Name for the region FLAGS - -d, --cda= Custom host to set for content delivery API, if this flag is added then cma, ui-host and + -n, --name= Name for the region, if this flag is added then cda, cma and ui-host flags are required + --cda= Custom host to set for content delivery API, if this flag is added then cma, ui-host and name flags are required - -m, --cma= Custom host to set for content management API, , if this flag is added then cda, ui-host + --cma= Custom host to set for content management API, , if this flag is added then cda, ui-host and name flags are required - -n, --name= Name for the region, if this flag is added then cda, cma and ui-host flags are required --developer-hub= Custom host to set for Developer hub API --launch= Custom host to set for Launch API --personalize= Custom host to set for Personalize API From 67ee24bcf157a56507f24953e6015ede32b9458d Mon Sep 17 00:00:00 2001 From: Netraj Patel Date: Fri, 21 Nov 2025 20:02:00 +0530 Subject: [PATCH 2/2] Updated unit tests for config plugin --- .talismanrc | 2 + .../unit/commands/remove-base-branch.test.ts | 115 +++++++++++++----- 2 files changed, 89 insertions(+), 28 deletions(-) diff --git a/.talismanrc b/.talismanrc index 27bd6cbc7c..61c8d12165 100644 --- a/.talismanrc +++ b/.talismanrc @@ -5,4 +5,6 @@ fileignoreconfig: checksum: fb0c32cd846cce3a53927316699a1c5aaa814939fe9b33bcd9141addbbe447d0 - filename: packages/contentstack-bootstrap/test/bootstrap.test.js checksum: b1f46b3447b1b358f80d6404d9d5b385fb385714e5c1f865ca97d64d6edaefc2 + - filename: packages/contentstack-config/test/unit/commands/remove-base-branch.test.ts + checksum: 7e98fa2a3bc763ea8a35a229533fb815729c39e08531480c46326737fbe07810 version: '1.0' diff --git a/packages/contentstack-config/test/unit/commands/remove-base-branch.test.ts b/packages/contentstack-config/test/unit/commands/remove-base-branch.test.ts index 13653d3dd9..7629fdcef9 100644 --- a/packages/contentstack-config/test/unit/commands/remove-base-branch.test.ts +++ b/packages/contentstack-config/test/unit/commands/remove-base-branch.test.ts @@ -1,50 +1,109 @@ -import { describe, it } from 'mocha'; +import { describe, it, beforeEach, afterEach } from 'mocha'; import { expect } from 'chai'; -import { stub } from 'sinon'; +import { stub, restore } from 'sinon'; import RemoveBranchConfigCommand from '../../../src/commands/config/remove/base-branch'; import { interactive } from '../../../src/utils'; import { removeConfigMockData } from '../mock'; import { cliux, configHandler } from '@contentstack/cli-utilities'; describe('Delete config', () => { + const testApiKey = removeConfigMockData.flags.apiKey; + const testBaseBranch = 'test-branch'; + + beforeEach(() => { + // Set up test config before each test + configHandler.set(`baseBranch.${testApiKey}`, testBaseBranch); + }); + + afterEach(() => { + // Clean up test config after each test + try { + configHandler.delete(`baseBranch.${testApiKey}`); + } catch (error) { + // Ignore if config doesn't exist + } + restore(); + }); + it('Delete config with all flags, should be successful', async function () { - const stub1 = stub(RemoveBranchConfigCommand.prototype, 'run'); - await RemoveBranchConfigCommand.run(['--stack-api-key', removeConfigMockData.flags.apiKey, '-y']); - expect(stub1.calledOnce).to.be.true; - stub1.restore(); + const successStub = stub(cliux, 'success'); + await RemoveBranchConfigCommand.run(['--stack-api-key', testApiKey, '-y']); + + // Verify that base branch and stack-api-key are displayed before deletion + expect(successStub.called).to.be.true; + const successCalls = successStub.getCalls(); + const messages = successCalls.map(call => call.args[0]); + + // Should show base branch and stack-api-key before deletion + expect(messages.some(msg => msg.includes(`base branch : ${testBaseBranch}`))).to.be.true; + expect(messages.some(msg => msg.includes(`stack-api-key: ${testApiKey}`))).to.be.true; + // Should show success message after deletion + expect(messages.some(msg => msg.includes('removed successfully'))).to.be.true; }); + it('Should prompt when api key is not passed', async () => { - const askStackAPIKey = stub(interactive, 'askStackAPIKey').resolves(removeConfigMockData.flags.apiKey); + const askStackAPIKey = stub(interactive, 'askStackAPIKey').resolves(testApiKey); await RemoveBranchConfigCommand.run(['-y']); expect(askStackAPIKey.calledOnce).to.be.true; - askStackAPIKey.restore(); }); + it('Should throw an error if config doesnt exist', async () => { - const config = configHandler; - const getConfig = config.get(`baseBranch.${removeConfigMockData.flags.apiKey}`); + // Remove the config first + configHandler.delete(`baseBranch.${testApiKey}`); + + const errorStub = stub(cliux, 'error'); + await RemoveBranchConfigCommand.run(['--stack-api-key', testApiKey]); - const throwError = stub(cliux, 'error'); - await RemoveBranchConfigCommand.run(['--stack-api-key', removeConfigMockData.flags.apiKey]); - if (getConfig === undefined) expect(throwError.calledOnce).to.be.true; - throwError.restore(); + expect(errorStub.calledOnce).to.be.true; + expect(errorStub.getCalls()[0].args[0]).to.include(`No config set for stack-api-key : ${testApiKey}`); }); + it('Should ask for confirmation to remove config if the config exists', async () => { - const config = configHandler; - const getConfig = config.get(`baseBranch.${removeConfigMockData.flags.apiKey}`); + const askConfirmation = stub(interactive, 'askConfirmation').resolves(true); + await RemoveBranchConfigCommand.run(['--stack-api-key', testApiKey]); + + expect(askConfirmation.calledOnce).to.be.true; + }); - const askConfirmation = stub(interactive, 'askConfirmation'); - await RemoveBranchConfigCommand.run(['--stack-api-key', removeConfigMockData.flags.apiKey]); - if (getConfig) expect(askConfirmation.calledOnce).to.be.true; - askConfirmation.restore(); + it('Should show base branch and stack-api-key before deletion', async () => { + const successStub = stub(cliux, 'success'); + const askConfirmation = stub(interactive, 'askConfirmation').resolves(true); + + await RemoveBranchConfigCommand.run(['--stack-api-key', testApiKey]); + + const successCalls = successStub.getCalls(); + const messages = successCalls.map(call => call.args[0]); + + // Verify that base branch and stack-api-key are displayed + expect(messages.some(msg => msg.includes(`base branch : ${testBaseBranch}`))).to.be.true; + expect(messages.some(msg => msg.includes(`stack-api-key: ${testApiKey}`))).to.be.true; }); + it('Should show success message on deletion of config', async () => { - const config = configHandler; - const getConfig = config.get(`baseBranch.${removeConfigMockData.flags.apiKey}`); - - const askConfirmation = stub(interactive, 'askConfirmation'); - const showSuccess = stub(cliux, 'success'); - await RemoveBranchConfigCommand.run(['--stack-api-key', removeConfigMockData.flags.apiKey]); - if (getConfig && askConfirmation.calledOnce) expect(showSuccess.calledOnce).to.be.true; - askConfirmation.restore(); + const successStub = stub(cliux, 'success'); + const askConfirmation = stub(interactive, 'askConfirmation').resolves(true); + + await RemoveBranchConfigCommand.run(['--stack-api-key', testApiKey]); + + expect(askConfirmation.calledOnce).to.be.true; + + const successCalls = successStub.getCalls(); + const messages = successCalls.map(call => call.args[0]); + + // Should show success message after deletion + expect(messages.some(msg => msg.includes('removed successfully'))).to.be.true; + }); + + it('Should not delete config if confirmation is rejected', async () => { + const askConfirmation = stub(interactive, 'askConfirmation').resolves(false); + const deleteStub = stub(configHandler, 'delete'); + + await RemoveBranchConfigCommand.run(['--stack-api-key', testApiKey]); + + expect(askConfirmation.calledOnce).to.be.true; + // delete should not be called if confirmation is rejected + expect(deleteStub.called).to.be.false; + + deleteStub.restore(); }); });