|
| 1 | +import { expect } from 'chai'; |
| 2 | +import sinon from 'sinon'; |
| 3 | +import { CLIProgressManager, configHandler } from '@contentstack/cli-utilities'; |
| 4 | + |
| 5 | +import { ImportSpaces } from '../../../src/import/spaces'; |
| 6 | +import ImportWorkspace from '../../../src/import/workspaces'; |
| 7 | +import ImportFields from '../../../src/import/fields'; |
| 8 | +import ImportAssetTypes from '../../../src/import/asset-types'; |
| 9 | +import { CSAssetsAdapter } from '../../../src/utils/cs-assets-api-adapter'; |
| 10 | +import { CSAssetsImportAdapter } from '../../../src/import/base'; |
| 11 | +import { PROCESS_NAMES } from '../../../src/constants/index'; |
| 12 | + |
| 13 | +import type { ImportSpacesOptions } from '../../../src/types/cs-assets-api'; |
| 14 | + |
| 15 | +describe('ImportSpaces', () => { |
| 16 | + const baseOptions: ImportSpacesOptions = { |
| 17 | + contentDir: '/tmp/import', |
| 18 | + csAssetsUrl: 'https://am.example.com', |
| 19 | + org_uid: 'org-1', |
| 20 | + apiKey: 'api-key-1', |
| 21 | + host: 'https://api.contentstack.io/v3', |
| 22 | + }; |
| 23 | + |
| 24 | + const fakeProgress = { |
| 25 | + addProcess: sinon.stub().returnsThis(), |
| 26 | + startProcess: sinon.stub().returnsThis(), |
| 27 | + updateStatus: sinon.stub().returnsThis(), |
| 28 | + tick: sinon.stub(), |
| 29 | + completeProcess: sinon.stub(), |
| 30 | + }; |
| 31 | + |
| 32 | + beforeEach(() => { |
| 33 | + sinon.stub(configHandler, 'get').returns({ showConsoleLogs: false }); |
| 34 | + sinon.stub(CLIProgressManager, 'createNested').returns(fakeProgress as any); |
| 35 | + // init and listSpaces live on AssetManagementAdapter (the common base). |
| 36 | + // Stubbing the base once covers both the adapter used for listSpaces and ImportWorkspace. |
| 37 | + sinon.stub(CSAssetsAdapter.prototype, 'init' as any).resolves(); |
| 38 | + sinon.stub(CSAssetsAdapter.prototype, 'listSpaces' as any).resolves({ spaces: [] }); |
| 39 | + sinon.stub(ImportFields.prototype, 'start').resolves(); |
| 40 | + sinon.stub(ImportFields.prototype, 'setParentProgressManager'); |
| 41 | + sinon.stub(ImportAssetTypes.prototype, 'start').resolves(); |
| 42 | + sinon.stub(ImportAssetTypes.prototype, 'setParentProgressManager'); |
| 43 | + sinon.stub(ImportWorkspace.prototype, 'setParentProgressManager'); |
| 44 | + |
| 45 | + fakeProgress.addProcess.resetHistory(); |
| 46 | + fakeProgress.addProcess.returnsThis(); |
| 47 | + fakeProgress.startProcess.resetHistory(); |
| 48 | + fakeProgress.startProcess.returnsThis(); |
| 49 | + fakeProgress.completeProcess.reset(); |
| 50 | + fakeProgress.tick.reset(); |
| 51 | + }); |
| 52 | + |
| 53 | + afterEach(() => { |
| 54 | + sinon.restore(); |
| 55 | + }); |
| 56 | + |
| 57 | + const stubSpaceDirs = (dirs: string[]) => { |
| 58 | + const fsMock = require('node:fs'); |
| 59 | + sinon.stub(fsMock, 'readdirSync').returns(dirs as any); |
| 60 | + sinon.stub(fsMock, 'statSync').returns({ isDirectory: () => true } as any); |
| 61 | + }; |
| 62 | + |
| 63 | + describe('targetDefaultSpaceUid threading', () => { |
| 64 | + it('should pass targetDefaultSpaceUid and targetDefaultWorkspaceUid to ImportWorkspace.start()', async () => { |
| 65 | + stubSpaceDirs(['am-space-1']); |
| 66 | + const startStub = sinon.stub(ImportWorkspace.prototype, 'start').resolves({ |
| 67 | + oldSpaceUid: 'am-space-1', |
| 68 | + newSpaceUid: 'target-space-3', |
| 69 | + workspaceUid: 'ws-3', |
| 70 | + isDefault: true, |
| 71 | + uidMap: {}, |
| 72 | + urlMap: {}, |
| 73 | + }); |
| 74 | + |
| 75 | + const options: ImportSpacesOptions = { |
| 76 | + ...baseOptions, |
| 77 | + targetDefaultSpaceUid: 'target-space-3', |
| 78 | + targetDefaultWorkspaceUid: 'ws-3', |
| 79 | + }; |
| 80 | + const importer = new ImportSpaces(options); |
| 81 | + await importer.start(); |
| 82 | + |
| 83 | + expect(startStub.callCount).to.equal(1); |
| 84 | + const args = startStub.firstCall.args; |
| 85 | + expect(args[4]).to.equal('target-space-3'); |
| 86 | + expect(args[5]).to.equal('ws-3'); |
| 87 | + }); |
| 88 | + |
| 89 | + it('should pass undefined to ImportWorkspace when targetDefaultSpaceUid is not set', async () => { |
| 90 | + stubSpaceDirs(['am-space-1']); |
| 91 | + const startStub = sinon.stub(ImportWorkspace.prototype, 'start').resolves({ |
| 92 | + oldSpaceUid: 'am-space-1', |
| 93 | + newSpaceUid: 'new-space', |
| 94 | + workspaceUid: 'main', |
| 95 | + isDefault: false, |
| 96 | + uidMap: {}, |
| 97 | + urlMap: {}, |
| 98 | + }); |
| 99 | + |
| 100 | + const importer = new ImportSpaces(baseOptions); |
| 101 | + await importer.start(); |
| 102 | + |
| 103 | + expect(startStub.callCount).to.equal(1); |
| 104 | + expect(startStub.firstCall.args[4]).to.be.undefined; |
| 105 | + expect(startStub.firstCall.args[5]).to.be.undefined; |
| 106 | + }); |
| 107 | + |
| 108 | + it('should record the correct spaceUidMap entry when default space is remapped', async () => { |
| 109 | + stubSpaceDirs(['am-space-1']); |
| 110 | + sinon.stub(ImportWorkspace.prototype, 'start').resolves({ |
| 111 | + oldSpaceUid: 'am-space-1', |
| 112 | + newSpaceUid: 'target-space-3', |
| 113 | + workspaceUid: 'ws-3', |
| 114 | + isDefault: true, |
| 115 | + uidMap: {}, |
| 116 | + urlMap: {}, |
| 117 | + }); |
| 118 | + |
| 119 | + const options: ImportSpacesOptions = { |
| 120 | + ...baseOptions, |
| 121 | + targetDefaultSpaceUid: 'target-space-3', |
| 122 | + }; |
| 123 | + const importer = new ImportSpaces(options); |
| 124 | + const result = await importer.start(); |
| 125 | + |
| 126 | + expect(result.spaceUidMap['am-space-1']).to.equal('target-space-3'); |
| 127 | + expect(result.spaceMappings[0].newSpaceUid).to.equal('target-space-3'); |
| 128 | + expect(result.spaceMappings[0].isDefault).to.equal(true); |
| 129 | + }); |
| 130 | + |
| 131 | + it('should process non-default spaces normally alongside the remapped default space', async () => { |
| 132 | + stubSpaceDirs(['am-space-1', 'am-space-2']); |
| 133 | + const startStub = sinon.stub(ImportWorkspace.prototype, 'start'); |
| 134 | + startStub.onFirstCall().resolves({ |
| 135 | + oldSpaceUid: 'am-space-1', |
| 136 | + newSpaceUid: 'target-space-3', |
| 137 | + workspaceUid: 'ws-3', |
| 138 | + isDefault: true, |
| 139 | + uidMap: {}, |
| 140 | + urlMap: {}, |
| 141 | + }); |
| 142 | + startStub.onSecondCall().resolves({ |
| 143 | + oldSpaceUid: 'am-space-2', |
| 144 | + newSpaceUid: 'brand-new-space', |
| 145 | + workspaceUid: 'main', |
| 146 | + isDefault: false, |
| 147 | + uidMap: {}, |
| 148 | + urlMap: {}, |
| 149 | + }); |
| 150 | + |
| 151 | + const options: ImportSpacesOptions = { |
| 152 | + ...baseOptions, |
| 153 | + targetDefaultSpaceUid: 'target-space-3', |
| 154 | + targetDefaultWorkspaceUid: 'ws-3', |
| 155 | + }; |
| 156 | + const importer = new ImportSpaces(options); |
| 157 | + const result = await importer.start(); |
| 158 | + |
| 159 | + expect(result.spaceMappings).to.have.lengthOf(2); |
| 160 | + expect(result.spaceUidMap['am-space-1']).to.equal('target-space-3'); |
| 161 | + expect(result.spaceUidMap['am-space-2']).to.equal('brand-new-space'); |
| 162 | + }); |
| 163 | + }); |
| 164 | + |
| 165 | + describe('no spaces scenario', () => { |
| 166 | + it('should return empty maps when spaces directory has no am* dirs', async () => { |
| 167 | + stubSpaceDirs([]); |
| 168 | + |
| 169 | + const importer = new ImportSpaces(baseOptions); |
| 170 | + const result = await importer.start(); |
| 171 | + |
| 172 | + expect(result.spaceMappings).to.deep.equal([]); |
| 173 | + expect(result.spaceUidMap).to.deep.equal({}); |
| 174 | + }); |
| 175 | + }); |
| 176 | +}); |
0 commit comments