-
Notifications
You must be signed in to change notification settings - Fork 1
fix(personalize,import): fix CT linking failure from empty-audience experiences and missing variant entry data file #280
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 5 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
9cf9514
fix(personalize,import): fix CT linking failure from empty-audience e…
cs-raj b8585cc
revert: restore package.json test script and test files unrelated to …
cs-raj b530110
Merge branch 'development' into fix/DX-9469
cs-raj 8dcd10a
test(import): update entries test to reflect publish loop running wit…
cs-raj bf088a2
fix(import): restore entries.ts source — revert accidental source change
cs-raj a6596a6
fix(personalize): address PR review comments on fix/DX-9469
cs-raj ea05a5e
fix(variants): guard versionMap against unrecognized status values [D…
cs-raj 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
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
251 changes: 251 additions & 0 deletions
251
packages/contentstack-variants/test/unit/import/experiences.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,251 @@ | ||
| import { expect } from 'chai'; | ||
| import sinon from 'sinon'; | ||
| import cloneDeep from 'lodash/cloneDeep'; | ||
| import { FsUtility } from '@contentstack/cli-utilities'; | ||
|
|
||
| import importConf from '../mock/import-config.json'; | ||
| import { Import, ImportConfig } from '../../../src'; | ||
|
|
||
| /** Predictable new UID returned for each experience name by createExperience stub */ | ||
| const NAME_TO_NEW_UID: Record<string, string> = { | ||
| 'AB Test No Audiences': 'new-uid-empty', | ||
| 'Experience Lytics Only': 'new-uid-lytics', | ||
| 'Valid Experience': 'new-uid-valid', | ||
| 'Mixed Audiences Experience': 'new-uid-mixed', | ||
| 'No Versions File Experience': 'new-uid-no-versions', | ||
| }; | ||
|
|
||
| function buildConfig(): ImportConfig { | ||
| const config = cloneDeep(importConf) as unknown as ImportConfig; | ||
| (config.modules as any).personalize = { | ||
| ...(config.modules as any).personalization, | ||
| dirName: 'personalize', | ||
| project_id: 'PROJ-TEST', | ||
| importData: true, | ||
| baseURL: { 'AWS-NA': 'https://personalization.na-api.contentstack.com' }, | ||
| }; | ||
| (config as any).region = { name: 'AWS-NA', cma: 'https://api.contentstack.io' }; | ||
| config.context = (config as any).context || {}; | ||
| return config; | ||
| } | ||
|
|
||
| describe('Experiences Import', () => { | ||
| let sandbox: sinon.SinonSandbox; | ||
|
|
||
| beforeEach(() => { | ||
| sandbox = sinon.createSandbox(); | ||
| sandbox.stub(Import.Experiences.prototype, 'init').resolves(); | ||
| sandbox.stub(FsUtility.prototype, 'writeFile').returns(undefined as any); | ||
| sandbox.stub(FsUtility.prototype, 'makeDirectory').resolves(undefined); | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| sandbox.restore(); | ||
| }); | ||
|
|
||
| // ────────────────────────────────────────────────────────────────────────── | ||
| // importExperienceVersions — unit tests (direct method call) | ||
| // ────────────────────────────────────────────────────────────────────────── | ||
| describe('importExperienceVersions', () => { | ||
| let updateVersionStub: sinon.SinonStub; | ||
| let createVersionStub: sinon.SinonStub; | ||
|
|
||
| beforeEach(() => { | ||
| updateVersionStub = sandbox.stub(Import.Experiences.prototype, 'updateExperienceVersion').resolves(); | ||
| createVersionStub = sandbox.stub(Import.Experiences.prototype, 'createExperienceVersion').resolves(); | ||
| }); | ||
|
|
||
| it('returns false when no versions file exists on disk', async () => { | ||
| const instance = new Import.Experiences(buildConfig()); | ||
| const result = await instance.importExperienceVersions( | ||
| { uid: 'new-uid-nofile', latestVersion: 'ver-nofile' } as any, | ||
| 'exp-no-versions-file', | ||
| ); | ||
| expect(result).to.equal(false); | ||
| expect(updateVersionStub.callCount).to.equal(0); | ||
| expect(createVersionStub.callCount).to.equal(0); | ||
| }); | ||
|
|
||
| it('returns false when version has variants: [] (experience had no audiences)', async () => { | ||
| const instance = new Import.Experiences(buildConfig()); | ||
| const result = await instance.importExperienceVersions( | ||
| { uid: 'new-uid-empty', latestVersion: 'ver-empty-latest' } as any, | ||
| 'exp-empty-audiences', | ||
| ); | ||
| expect(result).to.equal(false); | ||
| expect(updateVersionStub.callCount).to.equal(0); | ||
| }); | ||
|
|
||
| it('returns false when all Lytics audiences are stripped by lookUpAudiences', async () => { | ||
| const instance = new Import.Experiences(buildConfig()); | ||
| const result = await instance.importExperienceVersions( | ||
| { uid: 'new-uid-lytics', latestVersion: 'ver-lytics-latest' } as any, | ||
| 'exp-lytics-only', | ||
| ); | ||
| expect(result).to.equal(false); | ||
| expect(updateVersionStub.callCount).to.equal(0); | ||
| }); | ||
|
|
||
| it('returns true when version has a valid mapped CS audience', async () => { | ||
| const instance = new Import.Experiences(buildConfig()); | ||
| const result = await instance.importExperienceVersions( | ||
| { uid: 'new-uid-valid', latestVersion: 'ver-valid-latest' } as any, | ||
| 'exp-valid', | ||
| ); | ||
| expect(result).to.equal(true); | ||
| }); | ||
|
|
||
| it('calls updateExperienceVersion for ACTIVE status version', async () => { | ||
| const instance = new Import.Experiences(buildConfig()); | ||
| await instance.importExperienceVersions( | ||
| { uid: 'new-uid-valid', latestVersion: 'ver-valid-latest' } as any, | ||
| 'exp-valid', | ||
| ); | ||
| expect(updateVersionStub.callCount).to.equal(1); | ||
| expect(updateVersionStub.firstCall.args[0]).to.equal('new-uid-valid'); | ||
| expect(updateVersionStub.firstCall.args[2].status).to.equal('ACTIVE'); | ||
| expect(createVersionStub.callCount).to.equal(0); | ||
| }); | ||
|
|
||
| it('returns true when mixed CS+Lytics variant — CS audience survives, Lytics stripped', async () => { | ||
| const instance = new Import.Experiences(buildConfig()); | ||
| const result = await instance.importExperienceVersions( | ||
| { uid: 'new-uid-mixed', latestVersion: 'ver-mixed-latest' } as any, | ||
| 'exp-mixed', | ||
| ); | ||
| expect(result).to.equal(true); | ||
| }); | ||
|
|
||
| it('calls updateExperienceVersion for DRAFT when no ACTIVE version exists', async () => { | ||
| const instance = new Import.Experiences(buildConfig()); | ||
| const result = await instance.importExperienceVersions( | ||
| { uid: 'new-uid-draft-only', latestVersion: 'ver-draft-only-latest' } as any, | ||
| 'exp-draft-only', | ||
| ); | ||
| expect(result).to.equal(true); | ||
| expect(updateVersionStub.callCount).to.equal(1); | ||
| expect(updateVersionStub.firstCall.args[2].status).to.equal('DRAFT'); | ||
| expect(createVersionStub.callCount).to.equal(0); | ||
| }); | ||
|
|
||
| it('calls updateExperienceVersion for ACTIVE then createExperienceVersion for DRAFT when both exist', async () => { | ||
| const instance = new Import.Experiences(buildConfig()); | ||
| const result = await instance.importExperienceVersions( | ||
| { uid: 'new-uid-active-and-draft', latestVersion: 'ver-ad-latest' } as any, | ||
| 'exp-active-and-draft', | ||
| ); | ||
| expect(result).to.equal(true); | ||
| expect(updateVersionStub.callCount).to.equal(1); | ||
| expect(updateVersionStub.firstCall.args[2].status).to.equal('ACTIVE'); | ||
| expect(createVersionStub.callCount).to.equal(1); | ||
| expect(createVersionStub.firstCall.args[1].status).to.equal('DRAFT'); | ||
| }); | ||
|
|
||
| it('does not call any version API when all variants stripped after audience mapping', async () => { | ||
| const instance = new Import.Experiences(buildConfig()); | ||
| await instance.importExperienceVersions( | ||
| { uid: 'new-uid-lytics', latestVersion: 'ver-lytics-latest' } as any, | ||
| 'exp-lytics-only', | ||
| ); | ||
| expect(updateVersionStub.callCount).to.equal(0); | ||
| expect(createVersionStub.callCount).to.equal(0); | ||
| }); | ||
| }); | ||
|
|
||
| // ────────────────────────────────────────────────────────────────────────── | ||
| // import() — integration tests across all 5 mock experiences | ||
| // ────────────────────────────────────────────────────────────────────────── | ||
| describe('import()', () => { | ||
| let capturedPendingList: string[]; | ||
| let attachCTsStub: sinon.SinonStub; | ||
| let createExperienceStub: sinon.SinonStub; | ||
|
|
||
| beforeEach(() => { | ||
| capturedPendingList = []; | ||
|
|
||
| createExperienceStub = sandbox.stub(Import.Experiences.prototype, 'createExperience') | ||
| .callsFake(async function (payload: any) { | ||
| const uid = NAME_TO_NEW_UID[payload.name] ?? `new-uid-${payload.name}`; | ||
| return { uid, latestVersion: `ver-${uid}-latest` }; | ||
| } as any); | ||
|
|
||
| sandbox.stub(Import.Experiences.prototype, 'updateExperienceVersion').resolves(); | ||
| sandbox.stub(Import.Experiences.prototype, 'createExperienceVersion').resolves(); | ||
|
|
||
| sandbox.stub(Import.Experiences.prototype, 'validateVariantGroupAndVariantsCreated') | ||
| .callsFake(async function (this: any) { | ||
| capturedPendingList = [...this.pendingVariantAndVariantGrpForExperience]; | ||
| return true; | ||
| }); | ||
|
|
||
| attachCTsStub = sandbox.stub(Import.Experiences.prototype, 'attachCTsInExperience').resolves(); | ||
| sandbox.stub(Import.Experiences.prototype, 'createVariantIdMapper').resolves(); | ||
| }); | ||
|
|
||
| it('pendingVariantAndVariantGrpForExperience contains only experiences with valid variants', async () => { | ||
| const instance = new Import.Experiences(buildConfig()); | ||
| await instance.import(); | ||
|
|
||
| expect(capturedPendingList).to.include('new-uid-valid'); | ||
| expect(capturedPendingList).to.include('new-uid-mixed'); | ||
| }); | ||
|
|
||
| it('pendingVariantAndVariantGrpForExperience excludes experiences with no valid variants', async () => { | ||
| const instance = new Import.Experiences(buildConfig()); | ||
| await instance.import(); | ||
|
|
||
| expect(capturedPendingList).to.not.include('new-uid-empty'); | ||
| expect(capturedPendingList).to.not.include('new-uid-lytics'); | ||
| expect(capturedPendingList).to.not.include('new-uid-no-versions'); | ||
| }); | ||
|
|
||
| it('pendingVariantAndVariantGrpForExperience has exactly 2 entries (valid + mixed)', async () => { | ||
| const instance = new Import.Experiences(buildConfig()); | ||
| await instance.import(); | ||
|
|
||
| expect(capturedPendingList).to.have.length(2); | ||
| }); | ||
|
|
||
| it('calls attachCTsInExperience when validateVariantGroupAndVariantsCreated returns true', async () => { | ||
| const instance = new Import.Experiences(buildConfig()); | ||
| await instance.import(); | ||
|
|
||
| expect(attachCTsStub.callCount).to.equal(1); | ||
| }); | ||
|
|
||
| it('does NOT call attachCTsInExperience when validateVariantGroupAndVariantsCreated returns false', async () => { | ||
| // Override validate stub to return false (simulates backend timeout) | ||
| (Import.Experiences.prototype.validateVariantGroupAndVariantsCreated as sinon.SinonStub) | ||
| .callsFake(async function (this: any) { | ||
| capturedPendingList = [...this.pendingVariantAndVariantGrpForExperience]; | ||
| return false; | ||
| }); | ||
|
|
||
| const instance = new Import.Experiences(buildConfig()); | ||
| await instance.import(); | ||
|
|
||
| expect(attachCTsStub.callCount).to.equal(0); | ||
| }); | ||
|
|
||
| it('when all experiences produce no valid variants, pending list is empty and attachCTsInExperience is still called', async () => { | ||
| // Override importExperienceVersions to always return false for all experiences | ||
| sandbox.stub(Import.Experiences.prototype, 'importExperienceVersions').resolves(false); | ||
|
|
||
| // Reset validate stub: empty pending list → real impl returns true immediately | ||
| // but validate is already stubbed to capture and return true, so it still works | ||
| const instance = new Import.Experiences(buildConfig()); | ||
| await instance.import(); | ||
|
|
||
| expect(capturedPendingList).to.have.length(0); | ||
| expect(attachCTsStub.callCount).to.equal(1); | ||
| }); | ||
|
|
||
| it('calls createExperience for every experience in experiences.json', async () => { | ||
| const instance = new Import.Experiences(buildConfig()); | ||
| await instance.import(); | ||
|
|
||
| // 5 experiences in mock: empty, lytics, valid, mixed, no-versions-file | ||
| expect(createExperienceStub.callCount).to.equal(5); | ||
| }); | ||
| }); | ||
| }); |
9 changes: 9 additions & 0 deletions
9
...k-variants/test/unit/mock/contents/personalize/experiences/experiences-content-types.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| { | ||
| "exp-valid": [ | ||
| { "uid": "ct1", "status": "linked" } | ||
| ], | ||
| "exp-mixed": [ | ||
| { "uid": "ct1", "status": "linked" }, | ||
| { "uid": "ct2", "status": "linked" } | ||
| ] | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.