"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const nock_1 = __importDefault(require("nock"));
const actions_core_1 = require("@segment/actions-core");
const index_1 = __importDefault(require("../index"));
const audienceName = 'Test Audience';
const testDestination = (0, actions_core_1.createTestIntegration)(index_1.default);
const createAudienceInput = {
    settings: {
        oauth: {
            refresh_token: 'mock-refresh-token'
        }
    },
    audienceName: audienceName,
    audienceSettings: {
        advertiserId: '12345',
        audienceType: 'CUSTOMER_MATCH_CONTACT_INFO',
        membershipDurationDays: '30',
        description: 'Test description'
    }
};
const getAudienceInput = {
    settings: {
        oauth: {
            refresh_token: 'mock-refresh-token'
        }
    },
    audienceSettings: {
        advertiserId: '12345'
    },
    externalId: 'audience-id-123'
};
beforeAll(() => {
    process.env.ACTIONS_FIRST_PARTY_DV360_CLIENT_ID = 'mock-client-id';
    process.env.ACTIONS_FIRST_PARTY_DV360_CLIENT_SECRET = 'mock-client-secret';
});
beforeEach(() => {
    (0, nock_1.default)('https://www.googleapis.com')
        .post('/oauth2/v4/token', {
        refresh_token: 'mock-refresh-token',
        client_id: 'mock-client-id',
        client_secret: 'mock-client-secret',
        grant_type: 'refresh_token'
    })
        .reply(200, { access_token: 'temp-token' });
});
describe('Audience Destination', () => {
    describe('createAudience', () => {
        it('creates an audience successfully with CANARY VERSION', async () => {
            (0, nock_1.default)('https://displayvideo.googleapis.com')
                .post('/v4/firstPartyAndPartnerAudiences?advertiserId=12345', {
                displayName: audienceName,
                audienceType: 'CUSTOMER_MATCH_CONTACT_INFO',
                membershipDurationDays: '30',
                description: 'Test description',
                audienceSource: 'AUDIENCE_SOURCE_UNSPECIFIED',
                firstPartyAndPartnerAudienceType: 'TYPE_FIRST_PARTY'
            })
                .matchHeader('Authorization', 'Bearer temp-token')
                .reply(200, { firstPartyAndPartnerAudienceId: 'audience-id-123' });
            const result = await testDestination.createAudience({
                ...createAudienceInput,
                features: { 'first-party-dv360-canary-version': true }
            });
            expect(result).toEqual({ externalId: 'audience-id-123' });
        });
        it('errors out when no advertiser ID is provided', async () => {
            createAudienceInput.audienceSettings.advertiserId = '';
            await expect(testDestination.createAudience(createAudienceInput)).rejects.toThrowError(actions_core_1.IntegrationError);
        });
    });
    describe('getAudience', () => {
        it('should succeed with CANARY VERSION', async () => {
            (0, nock_1.default)('https://displayvideo.googleapis.com')
                .get(`/v4/firstPartyAndPartnerAudiences/audience-id-123?advertiserId=12345`)
                .matchHeader('Authorization', 'Bearer temp-token')
                .reply(200, { firstPartyAndPartnerAudienceId: 'audience-id-123' });
            const result = await testDestination.getAudience({
                ...getAudienceInput,
                features: { 'first-party-dv360-canary-version': true }
            });
            expect(result).toEqual({ externalId: 'audience-id-123' });
        });
        it('should fail when the audience ID is missing', async () => {
            const missingIdInput = {
                ...getAudienceInput,
                externalId: ''
            };
            await expect(testDestination.getAudience(missingIdInput)).rejects.toThrowError(new actions_core_1.IntegrationError('Failed to retrieve audience ID value', 'MISSING_REQUIRED_FIELD', 400));
        });
    });
    describe('Edit Customer Match Members - Contact Info List', () => {
        const payloadContactInfo = {
            emails: 'test@gmail.com',
            phoneNumbers: '1234567890',
            zipCodes: '12345',
            firstName: 'John',
            lastName: 'Doe',
            countryCode: '+1'
        };
        const event = (0, actions_core_1.createTestEvent)({
            event: 'Audience Entered',
            type: 'track',
            properties: {},
            context: {
                traits: payloadContactInfo,
                personas: {
                    external_audience_id: 'audience-id-123',
                    audience_settings: {
                        advertiserId: '12345',
                        token: 'temp-token'
                    }
                }
            }
        });
        it('should add customer match members successfully with CANARY VERSION', async () => {
            (0, nock_1.default)('https://displayvideo.googleapis.com')
                .post('/v4/firstPartyAndPartnerAudiences/audience-id-123:editCustomerMatchMembers')
                .reply(200, { firstPartyAndPartnerAudienceId: 'audience-id-123' });
            const result = await testDestination.testAction('addToAudContactInfo', {
                event,
                useDefaultMappings: true,
                features: { 'first-party-dv360-canary-version': true }
            });
            expect(result).toContainEqual(expect.objectContaining({
                data: expect.objectContaining({
                    firstPartyAndPartnerAudienceId: 'audience-id-123'
                })
            }));
        });
        it('should remove customer match members successfully with CANARY VERSION', async () => {
            (0, nock_1.default)('https://displayvideo.googleapis.com')
                .post('/v4/firstPartyAndPartnerAudiences/audience-id-123:editCustomerMatchMembers', {
                advertiserId: '12345',
                removedContactInfoList: {
                    contactInfos: [
                        {
                            hashedEmails: '87924606b4131a8aceeeae8868531fbb9712aaa07a5d3a756b26ce0f5d6ca674',
                            hashedPhoneNumbers: 'c775e7b757ede630cd0aa1113bd102661ab38829ca52a6422ab782862f268646',
                            zipCodes: '12345',
                            hashedFirstName: '96d9632f363564cc3032521409cf22a852f2032eec099ed5967c0d000cec607a',
                            hashedLastName: '799ef92a11af918e3fb741df42934f3b568ed2d93ac1df74f1b8d41a27932a6f',
                            countryCode: '+1'
                        }
                    ],
                    consent: {
                        adUserData: 'CONSENT_STATUS_GRANTED',
                        adPersonalization: 'CONSENT_STATUS_GRANTED'
                    }
                }
            })
                .reply(200, { firstPartyAndPartnerAudienceId: 'audience-id-123' });
            const result = await testDestination.testAction('removeFromAudContactInfo', {
                event,
                useDefaultMappings: true,
                features: { 'first-party-dv360-canary-version': true }
            });
            expect(result).toContainEqual(expect.objectContaining({
                data: expect.objectContaining({
                    firstPartyAndPartnerAudienceId: 'audience-id-123'
                })
            }));
        });
    });
    describe('Edit Customer Match Members - Mobile Device ID List', () => {
        const payloadDeviceId = {
            mobileDeviceIds: '123'
        };
        const event = (0, actions_core_1.createTestEvent)({
            event: 'Audience Entered',
            type: 'track',
            properties: {},
            context: {
                traits: payloadDeviceId,
                personas: {
                    external_audience_id: 'audience-id-123',
                    audience_settings: {
                        advertiserId: '12345',
                        token: 'temp-token'
                    }
                }
            }
        });
        it('should add customer match members successfully with CANARY VERSION', async () => {
            (0, nock_1.default)('https://displayvideo.googleapis.com')
                .post('/v4/firstPartyAndPartnerAudiences/audience-id-123:editCustomerMatchMembers', {
                advertiserId: '12345',
                addedMobileDeviceIdList: {
                    mobileDeviceIds: ['123'],
                    consent: {
                        adUserData: 'CONSENT_STATUS_GRANTED',
                        adPersonalization: 'CONSENT_STATUS_GRANTED'
                    }
                }
            })
                .reply(200, { firstPartyAndPartnerAudienceId: 'audience-id-123' });
            const result = await testDestination.testAction('addToAudMobileDeviceId', {
                event,
                useDefaultMappings: true,
                features: { 'first-party-dv360-canary-version': true }
            });
            expect(result).toContainEqual(expect.objectContaining({
                data: expect.objectContaining({
                    firstPartyAndPartnerAudienceId: 'audience-id-123'
                })
            }));
        });
        it('should remove customer match members successfully with CANARY VERSION', async () => {
            (0, nock_1.default)('https://displayvideo.googleapis.com')
                .post('/v4/firstPartyAndPartnerAudiences/audience-id-123:editCustomerMatchMembers', {
                advertiserId: '12345',
                removedMobileDeviceIdList: {
                    mobileDeviceIds: ['123'],
                    consent: {
                        adUserData: 'CONSENT_STATUS_GRANTED',
                        adPersonalization: 'CONSENT_STATUS_GRANTED'
                    }
                }
            })
                .reply(200, { firstPartyAndPartnerAudienceId: 'audience-id-123' });
            const result = await testDestination.testAction('removeFromAudMobileDeviceId', {
                event,
                useDefaultMappings: true,
                features: { 'first-party-dv360-canary-version': true }
            });
            expect(result).toContainEqual(expect.objectContaining({
                data: expect.objectContaining({
                    firstPartyAndPartnerAudienceId: 'audience-id-123'
                })
            }));
        });
    });
});
//# sourceMappingURL=index.test.js.map