|
| 1 | +import { expect } from 'chai'; |
| 2 | +import sinon from 'sinon'; |
| 3 | + |
| 4 | +import ImportWorkspace from '../../../src/import/workspaces'; |
| 5 | +import ImportAssets from '../../../src/import/assets'; |
| 6 | +import { AssetManagementImportAdapter } from '../../../src/import/base'; |
| 7 | + |
| 8 | +import type { AssetManagementAPIConfig, ImportContext } from '../../../src/types/asset-management-api'; |
| 9 | + |
| 10 | +describe('ImportWorkspace', () => { |
| 11 | + const apiConfig: AssetManagementAPIConfig = { |
| 12 | + baseURL: 'https://am.example.com', |
| 13 | + headers: { organization_uid: 'org-1' }, |
| 14 | + }; |
| 15 | + |
| 16 | + const importContext: ImportContext = { |
| 17 | + spacesRootPath: '/tmp/import/spaces', |
| 18 | + apiKey: 'api-key-1', |
| 19 | + host: 'https://api.contentstack.io/v3', |
| 20 | + org_uid: 'org-1', |
| 21 | + }; |
| 22 | + |
| 23 | + const spaceDir = '/tmp/import/spaces/am-space-1'; |
| 24 | + |
| 25 | + const stubMetadata = (metadata: Record<string, unknown>) => { |
| 26 | + const fs = require('node:fs'); |
| 27 | + sinon.stub(fs, 'readFileSync').returns(JSON.stringify(metadata)); |
| 28 | + }; |
| 29 | + |
| 30 | + beforeEach(() => { |
| 31 | + sinon.stub(AssetManagementImportAdapter.prototype, 'init' as any).resolves(); |
| 32 | + sinon.stub(AssetManagementImportAdapter.prototype, 'tick' as any); |
| 33 | + sinon.stub(ImportAssets.prototype, 'setParentProgressManager'); |
| 34 | + sinon.stub(ImportAssets.prototype, 'setProcessName' as any); |
| 35 | + }); |
| 36 | + |
| 37 | + afterEach(() => { |
| 38 | + sinon.restore(); |
| 39 | + }); |
| 40 | + |
| 41 | + describe('default-space mapping path', () => { |
| 42 | + it('should use targetDefaultSpaceUid and skip createSpace when isDefault=true', async () => { |
| 43 | + stubMetadata({ title: 'Source Default Space', is_default: true }); |
| 44 | + const createSpaceStub = sinon.stub(AssetManagementImportAdapter.prototype, 'createSpace' as any); |
| 45 | + const assetsStartStub = sinon.stub(ImportAssets.prototype, 'start').resolves({ uidMap: {}, urlMap: {} }); |
| 46 | + |
| 47 | + const importer = new ImportWorkspace(apiConfig, importContext); |
| 48 | + const result = await importer.start( |
| 49 | + 'am-space-1', |
| 50 | + spaceDir, |
| 51 | + new Set(), |
| 52 | + undefined, |
| 53 | + 'target-default-space-uid', |
| 54 | + 'target-ws-uid', |
| 55 | + ); |
| 56 | + |
| 57 | + expect(createSpaceStub.callCount).to.equal(0); |
| 58 | + expect(assetsStartStub.callCount).to.equal(1); |
| 59 | + expect(assetsStartStub.firstCall.args[0]).to.equal('target-default-space-uid'); |
| 60 | + expect(result.newSpaceUid).to.equal('target-default-space-uid'); |
| 61 | + expect(result.workspaceUid).to.equal('target-ws-uid'); |
| 62 | + expect(result.isDefault).to.equal(true); |
| 63 | + expect(result.oldSpaceUid).to.equal('am-space-1'); |
| 64 | + }); |
| 65 | + |
| 66 | + it('should upload assets into the existing target default space (not identity-map)', async () => { |
| 67 | + stubMetadata({ title: 'Source Default Space', is_default: true }); |
| 68 | + sinon.stub(AssetManagementImportAdapter.prototype, 'createSpace' as any); |
| 69 | + const assetsStartStub = sinon.stub(ImportAssets.prototype, 'start').resolves({ |
| 70 | + uidMap: { 'old-asset-1': 'new-asset-1' }, |
| 71 | + urlMap: { 'old-url-1': 'new-url-1' }, |
| 72 | + }); |
| 73 | + |
| 74 | + const importer = new ImportWorkspace(apiConfig, importContext); |
| 75 | + const result = await importer.start('am-space-1', spaceDir, new Set(), undefined, 'target-space-3'); |
| 76 | + |
| 77 | + expect(assetsStartStub.firstCall.args[0]).to.equal('target-space-3'); |
| 78 | + expect(result.uidMap).to.deep.equal({ 'old-asset-1': 'new-asset-1' }); |
| 79 | + expect(result.urlMap).to.deep.equal({ 'old-url-1': 'new-url-1' }); |
| 80 | + }); |
| 81 | + |
| 82 | + it('should fall back to "main" as workspaceUid when targetDefaultWorkspaceUid is not provided', async () => { |
| 83 | + stubMetadata({ is_default: true }); |
| 84 | + sinon.stub(AssetManagementImportAdapter.prototype, 'createSpace' as any); |
| 85 | + sinon.stub(ImportAssets.prototype, 'start').resolves({ uidMap: {}, urlMap: {} }); |
| 86 | + |
| 87 | + const importer = new ImportWorkspace(apiConfig, importContext); |
| 88 | + const result = await importer.start('am-space-1', spaceDir, new Set(), undefined, 'target-space-3'); |
| 89 | + |
| 90 | + expect(result.workspaceUid).to.equal('main'); |
| 91 | + }); |
| 92 | + |
| 93 | + it('should NOT use the default-space path when isDefault=false even if targetDefaultSpaceUid is set', async () => { |
| 94 | + stubMetadata({ title: 'Non-default Space', is_default: false }); |
| 95 | + const createSpaceStub = sinon |
| 96 | + .stub(AssetManagementImportAdapter.prototype, 'createSpace' as any) |
| 97 | + .resolves({ space: { uid: 'new-space-uid' } }); |
| 98 | + sinon.stub(ImportAssets.prototype, 'start').resolves({ uidMap: {}, urlMap: {} }); |
| 99 | + |
| 100 | + const importer = new ImportWorkspace(apiConfig, importContext); |
| 101 | + const result = await importer.start('am-space-2', spaceDir, new Set(), undefined, 'target-space-3'); |
| 102 | + |
| 103 | + expect(createSpaceStub.callCount).to.equal(1); |
| 104 | + expect(result.newSpaceUid).to.equal('new-space-uid'); |
| 105 | + }); |
| 106 | + |
| 107 | + it('should NOT use the default-space path when targetDefaultSpaceUid is undefined', async () => { |
| 108 | + stubMetadata({ title: 'Source Default Space', is_default: true }); |
| 109 | + const createSpaceStub = sinon |
| 110 | + .stub(AssetManagementImportAdapter.prototype, 'createSpace' as any) |
| 111 | + .resolves({ space: { uid: 'brand-new-uid' } }); |
| 112 | + sinon.stub(ImportAssets.prototype, 'start').resolves({ uidMap: {}, urlMap: {} }); |
| 113 | + |
| 114 | + const importer = new ImportWorkspace(apiConfig, importContext); |
| 115 | + const result = await importer.start('am-space-1', spaceDir, new Set()); |
| 116 | + |
| 117 | + expect(createSpaceStub.callCount).to.equal(1); |
| 118 | + expect(result.newSpaceUid).to.equal('brand-new-uid'); |
| 119 | + }); |
| 120 | + }); |
| 121 | + |
| 122 | + describe('identity-reuse path (existing uid match)', () => { |
| 123 | + it('should reuse existing space uid and call buildIdentityMappersFromExport', async () => { |
| 124 | + stubMetadata({ title: 'Space', is_default: false }); |
| 125 | + const identityStub = sinon |
| 126 | + .stub(ImportAssets.prototype, 'buildIdentityMappersFromExport') |
| 127 | + .resolves({ uidMap: { a: 'a' }, urlMap: {} }); |
| 128 | + sinon.stub(AssetManagementImportAdapter.prototype, 'createSpace' as any); |
| 129 | + |
| 130 | + const importer = new ImportWorkspace(apiConfig, importContext); |
| 131 | + const result = await importer.start('am-space-existing', spaceDir, new Set(['am-space-existing'])); |
| 132 | + |
| 133 | + expect(identityStub.callCount).to.equal(1); |
| 134 | + expect(result.newSpaceUid).to.equal('am-space-existing'); |
| 135 | + }); |
| 136 | + }); |
| 137 | + |
| 138 | + describe('create new space path', () => { |
| 139 | + it('should create a new space and upload assets for non-default non-existing space', async () => { |
| 140 | + stubMetadata({ title: 'Source Space 2', is_default: false }); |
| 141 | + const createStub = sinon |
| 142 | + .stub(AssetManagementImportAdapter.prototype, 'createSpace' as any) |
| 143 | + .resolves({ space: { uid: 'new-space-2-uid' } }); |
| 144 | + sinon.stub(ImportAssets.prototype, 'start').resolves({ uidMap: {}, urlMap: {} }); |
| 145 | + |
| 146 | + const importer = new ImportWorkspace(apiConfig, importContext); |
| 147 | + const result = await importer.start('am-space-2', spaceDir, new Set()); |
| 148 | + |
| 149 | + expect(createStub.callCount).to.equal(1); |
| 150 | + expect(result.newSpaceUid).to.equal('new-space-2-uid'); |
| 151 | + expect(result.isDefault).to.equal(false); |
| 152 | + }); |
| 153 | + }); |
| 154 | +}); |
0 commit comments