-
Notifications
You must be signed in to change notification settings - Fork 311
STRATCONN-6832 - [MNTN] - New audience destination #3809
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 1 commit
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
46783e3
MNTN new audience destinations
joe-ayoub-segment 0a435fc
versioning
joe-ayoub-segment 4beca4c
trimming before adding
joe-ayoub-segment b545cc1
more edits from Copilot feedback
joe-ayoub-segment f44138c
Merge branch 'main' into mntn-audiences
joe-ayoub-segment fe561a4
[MNTN] Fail fast on missing audience membership in syncAudience
joe-ayoub-segment 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
There are no files selected for viewing
158 changes: 158 additions & 0 deletions
158
packages/destination-actions/src/destinations/mntn-audiences/__tests__/destination.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,158 @@ | ||
| import nock from 'nock' | ||
| import { createTestIntegration, IntegrationError, PayloadValidationError } from '@segment/actions-core' | ||
| import Destination from '../index' | ||
|
|
||
| const testDestination = createTestIntegration(Destination as any) | ||
|
|
||
| const MNTN_BASE = 'https://integrations.ex.mountain.com' | ||
| const TEST_SETTINGS = { | ||
| advertiser_id: 'adv-001', | ||
| api_key: 'test-api-key-secret' | ||
| } | ||
|
|
||
| beforeEach(() => { | ||
| nock.cleanAll() | ||
| }) | ||
|
|
||
| afterAll(() => { | ||
| nock.cleanAll() | ||
| nock.restore() | ||
| }) | ||
|
|
||
| // ─── testAuthentication ─────────────────────────────────────────────────────── | ||
|
|
||
| describe('testAuthentication', () => { | ||
| it('succeeds when the API responds 200', async () => { | ||
| nock(MNTN_BASE, { reqheaders: { authorization: `Bearer ${TEST_SETTINGS.api_key}` } }) | ||
| .get('/v2026/audience/segments') | ||
| .query({ limit: '1' }) | ||
| .reply(200, { segments: [] }) | ||
|
|
||
| await expect(testDestination.testAuthentication(TEST_SETTINGS)).resolves.not.toThrow() | ||
| }) | ||
|
|
||
| it('throws when the API responds 401', async () => { | ||
| nock(MNTN_BASE) | ||
| .get('/v2026/audience/segments') | ||
| .query({ limit: '1' }) | ||
| .reply(401, { error: { code: 'Unauthenticated' } }) | ||
|
|
||
| await expect(testDestination.testAuthentication(TEST_SETTINGS)).rejects.toThrow(/Credentials are invalid: 401/) | ||
| }) | ||
| }) | ||
|
|
||
| // ─── createAudience ─────────────────────────────────────────────────────────── | ||
|
|
||
| describe('createAudience', () => { | ||
| it('POSTs to create a new segment and returns its ID as externalId', async () => { | ||
| nock(MNTN_BASE) | ||
| .post('/v2026/audience/segments', { segment: { name: 'High Value Users' } }) | ||
| .reply(200, { segment: { id: 'mntn-new-id', name: 'High Value Users' } }) | ||
|
|
||
| const result = await testDestination.createAudience({ | ||
| audienceName: 'High Value Users', | ||
| settings: TEST_SETTINGS, | ||
| audienceSettings: {} | ||
| }) | ||
|
|
||
| expect(result).toEqual({ externalId: 'mntn-new-id' }) | ||
| }) | ||
|
|
||
| it('uses pre-configured segment_id without calling the API', async () => { | ||
| const result = await testDestination.createAudience({ | ||
| audienceName: 'My Audience', | ||
| settings: TEST_SETTINGS, | ||
| audienceSettings: { segment_id: 'pre-existing-001' } | ||
| }) | ||
|
|
||
| expect(result).toEqual({ externalId: 'pre-existing-001' }) | ||
| }) | ||
|
|
||
| it('throws PayloadValidationError when audienceName is missing and no segment_id configured', async () => { | ||
| await expect( | ||
| testDestination.createAudience({ | ||
| audienceName: '', | ||
| settings: TEST_SETTINGS, | ||
| audienceSettings: {} | ||
| }) | ||
| ).rejects.toThrow(/missing audience name/i) | ||
| }) | ||
|
|
||
| it('throws IntegrationError if the API response has no segment.id', async () => { | ||
| nock(MNTN_BASE).post('/v2026/audience/segments').reply(200, { segment: {} }) | ||
|
|
||
| await expect( | ||
| testDestination.createAudience({ | ||
| audienceName: 'Broken Audience', | ||
| settings: TEST_SETTINGS, | ||
| audienceSettings: {} | ||
| }) | ||
| ).rejects.toThrow(/unexpected response/i) | ||
| }) | ||
| }) | ||
|
|
||
| // ─── getAudience ────────────────────────────────────────────────────────────── | ||
|
|
||
| describe('getAudience', () => { | ||
| it('GETs the segment by externalId and returns it', async () => { | ||
| const segmentId = 'existing-seg-abc' | ||
|
|
||
| nock(MNTN_BASE).get(`/v2026/audience/segments/${segmentId}`).reply(200, { segment: { id: segmentId } }) | ||
|
|
||
| const result = await testDestination.getAudience({ | ||
| externalId: segmentId, | ||
| settings: TEST_SETTINGS, | ||
| audienceSettings: {} | ||
| }) | ||
|
|
||
| expect(result).toEqual({ externalId: segmentId }) | ||
| }) | ||
|
|
||
| it('prefers audienceSettings.segment_id over externalId', async () => { | ||
| const overrideId = 'override-seg-999' | ||
|
|
||
| nock(MNTN_BASE).get(`/v2026/audience/segments/${overrideId}`).reply(200, { segment: { id: overrideId } }) | ||
|
|
||
| const result = await testDestination.getAudience({ | ||
| externalId: 'stale-id', | ||
| settings: TEST_SETTINGS, | ||
| audienceSettings: { segment_id: overrideId } | ||
| }) | ||
|
|
||
| expect(result).toEqual({ externalId: overrideId }) | ||
| }) | ||
|
|
||
| it('throws when neither externalId nor segment_id is provided', async () => { | ||
| await expect( | ||
| testDestination.getAudience({ | ||
| externalId: '', | ||
| settings: TEST_SETTINGS, | ||
| audienceSettings: {} | ||
| }) | ||
| ).rejects.toThrow(/no mntn segment id found/i) | ||
| }) | ||
|
|
||
| it('throws when the API responds 404', async () => { | ||
| nock(MNTN_BASE).get('/v2026/audience/segments/does-not-exist').reply(404, { error: { code: 'NotFound' } }) | ||
|
|
||
| await expect( | ||
| testDestination.getAudience({ | ||
| externalId: 'does-not-exist', | ||
| settings: TEST_SETTINGS, | ||
| audienceSettings: {} | ||
| }) | ||
| ).rejects.toThrow() | ||
| }) | ||
|
|
||
| it('throws IntegrationError if the API response has no segment.id', async () => { | ||
| nock(MNTN_BASE).get('/v2026/audience/segments/seg-abc').reply(200, { segment: {} }) | ||
|
|
||
| await expect( | ||
| testDestination.getAudience({ | ||
| externalId: 'seg-abc', | ||
| settings: TEST_SETTINGS, | ||
| audienceSettings: {} | ||
| }) | ||
| ).rejects.toThrow(/unexpected response/i) | ||
| }) | ||
| }) | ||
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.