Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 3 additions & 5 deletions packages/contentstack-branches/src/branch/merge-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -400,13 +400,12 @@ export default class MergeHandler {
if (!this.userInputs.executeOption) {
this.executeOption = null;
}
if (!this.userInputs.executeOption) {
this.executeOption = null;
}
this.mergeSettings.strategy = null;
this.mergeSettings.itemMergeStrategies = [];

await this.start();
// Only ask the questions again. Calling start() here would leave the first start() waiting on
// the stack, and it would run the selected action (and the merge) a second time (DX-33747).
return await this.collectMergeSettings();
}

async start() {
Expand All @@ -431,7 +430,6 @@ export default class MergeHandler {
} else {
await this.exportSummary(mergePayload);
await this.executeMerge(mergePayload);
this.enableEntryExp = true;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,274 @@
import { describe, it, beforeEach, afterEach } from 'mocha';
import { expect } from 'chai';
import { createSandbox, SinonSandbox, SinonStub } from 'sinon';
import { cliux } from '@contentstack/cli-utilities';
import { interactive } from '../../../src/utils';
import MergeHandler from '../../../src/branch/merge-handler';

/**
* DX-33747: "Start Over" (and "Go Back" for strategies that route to a restart) must only re-ask
* the merge questions. The selected action has to run once, with the answers given last.
*
* These tests run the real collectMergeSettings() and stub only the prompts and the side effects
* (summary export, merge request, summary display), then count what actually ran.
*/
describe('MergeHandler menu navigation (DX-33747)', () => {
let sandbox: SinonSandbox;
let exportSummary: SinonStub;
let executeMerge: SinonStub;
let selectStrategy: SinonStub;
let selectSubOption: SinonStub;
let selectAction: SinonStub;
let selectCustomPreferences: SinonStub;

const branchCompareData = () => ({
content_types: {
added: [{ status: 'added', title: 'Added CT', type: 'content_type', uid: 'added_ct' }],
deleted: [],
modified: [{ status: 'modified', title: 'Modified CT', type: 'content_type', uid: 'modified_ct' }],
},
});

const handlerOptions = (overrides = {}): any => ({
baseBranch: 'main',
branchCompareData: branchCompareData(),
compareBranch: 'dev',
enableEntryExp: false,
exportSummaryPath: '/tmp',
host: 'api.example.com',
mergeComment: 'merge comment',
...overrides,
});

const customSelection = (uid: string, status: 'added' | 'modified') => [
{ status, value: { merge_strategy: 'merge_prefer_compare', type: 'content_type', uid } },
];

const run = async (overrides = {}) => {
const handler: any = new MergeHandler(handlerOptions(overrides));
await handler.start();
return handler;
};

beforeEach(() => {
sandbox = createSandbox();
sandbox.stub(cliux, 'print');
sandbox.stub(MergeHandler.prototype, 'displayMergeSummary');
exportSummary = sandbox.stub(MergeHandler.prototype, 'exportSummary').resolves();
executeMerge = sandbox.stub(MergeHandler.prototype, 'executeMerge').resolves();
selectStrategy = sandbox.stub(interactive, 'selectMergeStrategy');
selectSubOption = sandbox.stub(interactive, 'selectMergeStrategySubOptions');
selectAction = sandbox.stub(interactive, 'selectMergeExecution');
selectCustomPreferences = sandbox.stub(interactive, 'selectCustomPreferences');
});

afterEach(() => {
sandbox.restore();
});

describe('runs the selected action once', () => {
it('Start Over in the action menu, then Execute Merge', async () => {
selectStrategy.resolves('merge_prefer_base');
selectSubOption.resolves('both');
selectAction.onFirstCall().resolves('restart').onSecondCall().resolves('both');

await run();

expect(selectAction.callCount).to.equal(2);
expect(exportSummary.callCount).to.equal(1);
expect(executeMerge.callCount).to.equal(1);
});

it('Start Over three times, then Execute Merge', async () => {
selectStrategy.resolves('merge_prefer_base');
selectSubOption.resolves('both');
selectAction
.onCall(0)
.resolves('restart')
.onCall(1)
.resolves('restart')
.onCall(2)
.resolves('restart')
.onCall(3)
.resolves('both');

await run();

expect(selectAction.callCount).to.equal(4);
expect(exportSummary.callCount).to.equal(1);
expect(executeMerge.callCount).to.equal(1);
});

it('Start Over in the sub-option menu, then Execute Merge', async () => {
selectStrategy.resolves('merge_prefer_compare');
selectSubOption.onFirstCall().resolves('restart').onSecondCall().resolves('both');
selectAction.resolves('both');

await run();

expect(selectStrategy.callCount).to.equal(2);
expect(exportSummary.callCount).to.equal(1);
expect(executeMerge.callCount).to.equal(1);
});

it('Go Back in the action menu with Overwrite with Compare', async () => {
selectStrategy.resolves('overwrite_with_compare');
selectAction.onFirstCall().resolves('previous').onSecondCall().resolves('both');

await run();

expect(selectStrategy.callCount).to.equal(2);
expect(exportSummary.callCount).to.equal(1);
expect(executeMerge.callCount).to.equal(1);
});

it('Go Back in the action menu with Ask for Preference', async () => {
selectStrategy.resolves('custom_preferences');
selectCustomPreferences.resolves(customSelection('added_ct', 'added'));
selectAction.onFirstCall().resolves('previous').onSecondCall().resolves('both');

await run();

expect(selectStrategy.callCount).to.equal(2);
expect(exportSummary.callCount).to.equal(1);
expect(executeMerge.callCount).to.equal(1);
});

it('Go Back in the action menu with Prefer Base', async () => {
selectStrategy.resolves('merge_prefer_base');
selectSubOption.resolves('both');
selectAction.onFirstCall().resolves('previous').onSecondCall().resolves('both');

await run();

expect(selectSubOption.callCount).to.equal(2);
expect(exportSummary.callCount).to.equal(1);
expect(executeMerge.callCount).to.equal(1);
});

it('Start Over, then Export Merge Summary', async () => {
selectStrategy.resolves('merge_prefer_base');
selectSubOption.resolves('both');
selectAction.onFirstCall().resolves('restart').onSecondCall().resolves('export');

await run();

expect(exportSummary.callCount).to.equal(1);
expect(executeMerge.callCount).to.equal(0);
});

it('Start Over, then Execute Merge and Generate Content Migration Scripts', async () => {
selectStrategy.resolves('merge_prefer_base');
selectSubOption.resolves('both');
selectAction.onFirstCall().resolves('restart').onSecondCall().resolves('merge_n_scripts');

const handler = await run();

expect(exportSummary.callCount).to.equal(0);
expect(executeMerge.callCount).to.equal(1);
expect(handler.enableEntryExp).to.equal(true);
});

it('Start Over, then Export Summary and Generate Content Migration Scripts', async () => {
selectStrategy.resolves('merge_prefer_base');
selectSubOption.resolves('both');
selectAction.onFirstCall().resolves('restart').onSecondCall().resolves('summary_n_scripts');

const handler = await run();

expect(exportSummary.callCount).to.equal(1);
expect(executeMerge.callCount).to.equal(0);
expect(handler.enableEntryExp).to.equal(true);
});

it('--strategy and --merge-action flags, Start Over in the sub-option menu', async () => {
selectSubOption.onFirstCall().resolves('restart').onSecondCall().resolves('both');

await run({ executeOption: 'both', strategy: 'merge_prefer_base' });

expect(selectStrategy.callCount).to.equal(0);
expect(selectAction.callCount).to.equal(0);
expect(exportSummary.callCount).to.equal(1);
expect(executeMerge.callCount).to.equal(1);
});

it('no navigation, Execute Merge', async () => {
selectStrategy.resolves('merge_prefer_base');
selectSubOption.resolves('both');
selectAction.resolves('both');

await run();

expect(exportSummary.callCount).to.equal(1);
expect(executeMerge.callCount).to.equal(1);
});
});

describe('sends the answers given after Start Over', () => {
it('Prefer Base (new only), then Overwrite with Compare', async () => {
selectStrategy.onFirstCall().resolves('merge_prefer_base').onSecondCall().resolves('overwrite_with_compare');
selectSubOption.resolves('new');
selectAction.onFirstCall().resolves('restart').onSecondCall().resolves('both');

await run();

expect(executeMerge.callCount).to.equal(1);
const payload = executeMerge.firstCall.args[0];
expect(payload.default_merge_strategy).to.equal('overwrite_with_compare');
expect(payload.item_merge_strategies).to.deep.equal([]);
});

it('Ask for Preference, then Prefer Compare (both)', async () => {
selectStrategy.onFirstCall().resolves('custom_preferences').onSecondCall().resolves('merge_prefer_compare');
selectCustomPreferences.resolves(customSelection('added_ct', 'added'));
selectSubOption.resolves('both');
selectAction.onFirstCall().resolves('restart').onSecondCall().resolves('both');

await run();

expect(executeMerge.callCount).to.equal(1);
const payload = executeMerge.firstCall.args[0];
expect(payload.default_merge_strategy).to.equal('merge_prefer_compare');
expect(payload.item_merge_strategies).to.deep.equal([]);
});

it('Ask for Preference twice, keeps only the second selection', async () => {
selectStrategy.resolves('custom_preferences');
selectCustomPreferences
.onFirstCall()
.resolves(customSelection('added_ct', 'added'))
.onSecondCall()
.resolves(customSelection('modified_ct', 'modified'));
selectAction.onFirstCall().resolves('restart').onSecondCall().resolves('both');

await run();

expect(executeMerge.callCount).to.equal(1);
const payload = executeMerge.firstCall.args[0];
expect(payload.default_merge_strategy).to.equal('ignore');
expect(payload.item_merge_strategies.map((item) => item.uid)).to.deep.equal(['modified_ct']);
});
});

describe('entry migration', () => {
it('is not turned on by Execute Merge', async () => {
selectStrategy.resolves('merge_prefer_base');
selectSubOption.resolves('both');
selectAction.resolves('both');

const handler = await run();

expect(handler.enableEntryExp).to.equal(false);
});

it('is not turned on by the --merge-action execute flag', async () => {
selectStrategy.resolves('merge_prefer_base');
selectSubOption.resolves('both');

const handler = await run({ executeOption: 'execute' });

expect(handler.enableEntryExp).to.equal(false);
expect(executeMerge.callCount).to.equal(1);
});
});
});
Loading