"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.createBatchTestEvents = void 0;
const nock_1 = __importDefault(require("nock"));
const actions_core_1 = require("@segment/actions-core");
const index_1 = __importDefault(require("../../index"));
const testDestination = (0, actions_core_1.createTestIntegration)(index_1.default);
const event = (0, actions_core_1.createTestEvent)({
    event: 'Audience Entered',
    type: 'track',
    properties: {
        audience_key: 'personas_test_audience'
    },
    context: {
        device: {
            advertisingId: '123'
        },
        traits: {
            mobileDeviceIds: 'test-id'
        }
    }
});
describe('First-Party-dv360.addToAudMobileDeviceId', () => {
    it('should use v4 endpoint when CANARY VERSION', async () => {
        (0, nock_1.default)('https://displayvideo.googleapis.com/v4/firstPartyAndPartnerAudiences')
            .post('/1234567890:editCustomerMatchMembers')
            .reply(200, { firstPartyAndPartnerAudienceId: '1234567890' });
        const responses = await testDestination.testAction('addToAudMobileDeviceId', {
            event,
            mapping: {
                mobileDeviceIds: ['test-id'],
                external_id: '1234567890',
                advertiser_id: '1234567890',
                enable_batching: false,
                batch_size: 1
            },
            features: { 'first-party-dv360-canary-version': true }
        });
        expect(JSON.parse(responses[0].options.body)).toMatchInlineSnapshot(`
      Object {
        "addedMobileDeviceIdList": Object {
          "consent": Object {
            "adPersonalization": "CONSENT_STATUS_GRANTED",
            "adUserData": "CONSENT_STATUS_GRANTED",
          },
          "mobileDeviceIds": Array [
            "test-id",
          ],
        },
        "advertiserId": "1234567890",
      }
    `);
    });
    it('should addToAudMobileDeviceId', async () => {
        (0, nock_1.default)('https://displayvideo.googleapis.com/v4/firstPartyAndPartnerAudiences')
            .post('/1234567890:editCustomerMatchMembers')
            .reply(200, { firstPartyAndPartnerAudienceId: '1234567890' });
        const responses = await testDestination.testAction('addToAudMobileDeviceId', {
            event,
            mapping: {
                mobileDeviceIds: ['test-id'],
                external_id: '1234567890',
                advertiser_id: '1234567890',
                enable_batching: false,
                batch_size: 1
            }
        });
        expect(responses[0].options.body).toMatchInlineSnapshot(`"{\\"advertiserId\\":\\"1234567890\\",\\"addedMobileDeviceIdList\\":{\\"mobileDeviceIds\\":[\\"test-id\\"],\\"consent\\":{\\"adUserData\\":\\"CONSENT_STATUS_GRANTED\\",\\"adPersonalization\\":\\"CONSENT_STATUS_GRANTED\\"}}}"`);
    });
    it('should batch multiple payloads into a single request when enable_batching is true', async () => {
        (0, nock_1.default)('https://displayvideo.googleapis.com/v4/firstPartyAndPartnerAudiences')
            .post('/1234567890:editCustomerMatchMembers')
            .reply(200, { firstPartyAndPartnerAudienceId: '1234567890' });
        const events = (0, exports.createBatchTestEvents)(createContactList);
        const responses = await testDestination.testBatchAction('addToAudMobileDeviceId', {
            events: events,
            mapping: {
                mobileDeviceIds: ['test-id'],
                external_id: '1234567890',
                advertiser_id: '1234567890',
                enable_batching: true,
                batch_size: 10
            }
        });
        const requestBody = JSON.parse(String(responses[0].options.body));
        expect(requestBody.addedMobileDeviceIdList.mobileDeviceIds.length).toBe(2);
        expect(requestBody.addedMobileDeviceIdList.mobileDeviceIds[0]).toBeDefined();
        expect(requestBody.addedMobileDeviceIdList.mobileDeviceIds[1]).toBeDefined();
    });
});
const createBatchTestEvents = (batchContactList) => batchContactList.map((contact) => (0, actions_core_1.createTestEvent)({
    type: 'identify',
    traits: {
        mobileDeviceIds: contact.mobileDeviceIds
    }
}));
exports.createBatchTestEvents = createBatchTestEvents;
const createContactList = [
    {
        mobileDeviceIds: 'test-id'
    },
    {
        mobileDeviceIds: 'test-id2'
    }
];
//# sourceMappingURL=index.test.js.map