Skip to content

Commit 0e3b8ca

Browse files
add test cases for progress manager
1 parent 1983f23 commit 0e3b8ca

1 file changed

Lines changed: 130 additions & 1 deletion

File tree

packages/contentstack-bulk-operations/test/unit/base-bulk-command.test.ts

Lines changed: 130 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import sinon from 'sinon';
44
import { Command } from '@contentstack/cli-command';
55
import messages, { $t } from '../../src/messages';
66
import { BaseBulkCommand } from '../../src/base-bulk-command';
7-
import { ResourceType, BulkOperationResult } from '../../src/interfaces';
7+
import { ResourceType, BulkOperationResult, PublishMode } from '../../src/interfaces';
88
import * as utils from '../../src/utils';
99

1010
class TestBulkCommand extends BaseBulkCommand {
@@ -521,6 +521,135 @@ describe('BaseBulkCommand', () => {
521521
});
522522
});
523523

524+
describe('progress manager (summary + cleanup)', () => {
525+
let cliUtils: any;
526+
let progressStub: { tick: sinon.SinonStub; complete: sinon.SinonStub };
527+
528+
beforeEach(() => {
529+
cliUtils = require('@contentstack/cli-utilities');
530+
progressStub = { tick: sandbox.stub(), complete: sandbox.stub() };
531+
sandbox.stub(cliUtils.CLIProgressManager, 'createSimple').returns(progressStub as any);
532+
sandbox.stub(cliUtils.CLIProgressManager, 'initializeGlobalSummary').returns({} as any);
533+
sandbox.stub(cliUtils.CLIProgressManager, 'printGlobalSummary').callsFake(() => {});
534+
sandbox.stub(cliUtils.CLIProgressManager, 'clearGlobalSummary').callsFake(() => {});
535+
// clearProgressModuleSetting is a frozen re-export (not stubbable); let the real one run
536+
// and drive/assert it through configHandler instead.
537+
sandbox.stub(cliUtils.configHandler, 'get').returns({ showConsoleLogs: false });
538+
sandbox.stub(cliUtils.configHandler, 'set').callsFake(() => {});
539+
});
540+
541+
describe('beginOperationSummary', () => {
542+
it('builds a "BULK <OP>-<branch>" label and passes the branch', () => {
543+
(command as any).bulkOperationConfig = { operation: 'publish', branch: 'main' };
544+
545+
(command as any).beginOperationSummary(5);
546+
547+
expect(cliUtils.CLIProgressManager.initializeGlobalSummary.calledOnce).to.be.true;
548+
const args = cliUtils.CLIProgressManager.initializeGlobalSummary.firstCall.args;
549+
expect(args[0]).to.equal('BULK PUBLISH-main');
550+
expect(args[1]).to.equal('main');
551+
});
552+
553+
it('omits the branch suffix when the branch is unset', () => {
554+
(command as any).bulkOperationConfig = { operation: 'unpublish', branch: '' };
555+
556+
(command as any).beginOperationSummary(0);
557+
558+
expect(cliUtils.CLIProgressManager.initializeGlobalSummary.firstCall.args[0]).to.equal('BULK UNPUBLISH');
559+
});
560+
561+
it('does not throw when bulkOperationConfig is undefined', () => {
562+
(command as any).bulkOperationConfig = undefined;
563+
564+
expect(() => (command as any).beginOperationSummary(3)).to.not.throw();
565+
expect(cliUtils.CLIProgressManager.initializeGlobalSummary.firstCall.args[0]).to.equal('BULK OPERATION');
566+
});
567+
});
568+
569+
describe('recordModuleSummary', () => {
570+
it('SINGLE mode: uses the real success/failed counts from the result', () => {
571+
(command as any).bulkOperationConfig = { publishMode: PublishMode.SINGLE };
572+
573+
(command as any).recordModuleSummary({ success: 8, failed: 2, total: 10 } as BulkOperationResult, 10);
574+
575+
expect(cliUtils.CLIProgressManager.createSimple.calledWith(ResourceType.ENTRY, 10)).to.be.true;
576+
expect(progressStub.tick.withArgs(true).callCount).to.equal(8);
577+
expect(progressStub.tick.withArgs(false).callCount).to.equal(2);
578+
expect(progressStub.complete.calledWith(false)).to.be.true;
579+
});
580+
581+
it('BULK mode: derives failures from batchResults and counts the rest as submitted', () => {
582+
(command as any).bulkOperationConfig = { publishMode: PublishMode.BULK };
583+
// buildBulkModeResult reports success:0/failed:0; failures live in batchResults.
584+
(command as any).batchResults = new Map<string, any>([
585+
['b1', { status: 'failed', success: 0, failed: 50, jobId: '' }],
586+
['b2', { status: 'success', success: 0, failed: 0, jobId: 'job-2' }],
587+
]);
588+
589+
(command as any).recordModuleSummary({ success: 0, failed: 0, total: 126 } as BulkOperationResult, 126);
590+
591+
// failed = 50 (from batchResults); success = 126 - 50 = 76
592+
expect(progressStub.tick.withArgs(true).callCount).to.equal(76);
593+
expect(progressStub.tick.withArgs(false).callCount).to.equal(50);
594+
expect(progressStub.complete.calledWith(false)).to.be.true;
595+
});
596+
597+
it('BULK mode: with no submission failures, all submitted items count as success', () => {
598+
(command as any).bulkOperationConfig = { publishMode: PublishMode.BULK };
599+
(command as any).batchResults = new Map<string, any>([
600+
['b1', { status: 'success', success: 0, failed: 0, jobId: 'job-1' }],
601+
]);
602+
603+
(command as any).recordModuleSummary({ success: 0, failed: 0, total: 50 } as BulkOperationResult, 50);
604+
605+
expect(progressStub.tick.withArgs(true).callCount).to.equal(50);
606+
expect(progressStub.tick.withArgs(false).callCount).to.equal(0);
607+
expect(progressStub.complete.calledWith(true)).to.be.true;
608+
});
609+
610+
it('clamps counts so failures never exceed the total', () => {
611+
(command as any).bulkOperationConfig = { publishMode: PublishMode.BULK };
612+
(command as any).batchResults = new Map<string, any>([
613+
['b1', { status: 'failed', success: 0, failed: 999, jobId: '' }],
614+
]);
615+
616+
(command as any).recordModuleSummary({ success: 0, failed: 0, total: 10 } as BulkOperationResult, 10);
617+
618+
expect(progressStub.tick.withArgs(false).callCount).to.equal(10); // clamped to total
619+
expect(progressStub.tick.withArgs(true).callCount).to.equal(0);
620+
});
621+
622+
it('falls back to submittedCount when the result has no total', () => {
623+
(command as any).bulkOperationConfig = { publishMode: PublishMode.BULK };
624+
(command as any).batchResults = new Map<string, any>();
625+
626+
(command as any).recordModuleSummary({ success: 0, failed: 0 } as BulkOperationResult, 7);
627+
628+
expect(cliUtils.CLIProgressManager.createSimple.calledWith(ResourceType.ENTRY, 7)).to.be.true;
629+
expect(progressStub.tick.withArgs(true).callCount).to.equal(7);
630+
});
631+
});
632+
633+
describe('finalizeProgressSummary', () => {
634+
it('prints the summary and clears it', () => {
635+
(command as any).finalizeProgressSummary();
636+
637+
expect(cliUtils.CLIProgressManager.printGlobalSummary.calledOnce).to.be.true;
638+
expect(cliUtils.CLIProgressManager.clearGlobalSummary.calledOnce).to.be.true;
639+
});
640+
641+
it('clears the persisted progress-module flag', () => {
642+
// Simulate the flag being set, then verify clearProgressModuleSetting removes it.
643+
cliUtils.configHandler.get.returns({ progressSupportedModule: 'bulk-operations', showConsoleLogs: false });
644+
645+
(command as any).finalizeProgressSummary();
646+
647+
expect(cliUtils.configHandler.set.calledWith('log', sinon.match((v: any) => !('progressSupportedModule' in v)))).to
648+
.be.true;
649+
});
650+
});
651+
});
652+
524653
describe('printOperationSummary', () => {
525654
beforeEach(() => {
526655
// Initialize bulkOperationConfig required for printOperationSummary

0 commit comments

Comments
 (0)