import { expect } from "chai"; import mockDate from "mockdate"; import { toChoiceRequest, toOpenRtbResponse } from "../../../src/mappers/openRtb.mapper"; import { ChoiceRequest, RequestInventorySource } from "../../../src/models"; import constants from "../../../src/utils/constants"; describe("OpenRtbMapper", function () { let openRtbRequest: any; beforeEach(() => { openRtbRequest = { bid_request: { id: "1", imp: [ { id: "12", bidfloor: 8, ext: { environment_type: 1, }, }, ], device: { ifa: "a19d8bfc-01ec-11e9-8eb2-f2801f1b9fd1", model: "Shiftphone S3", make: "SFFC", osv: "1.2.3", os: "Android", connectiontype: 2, // wifi carrier: "Mexicell", geo: { city: "Guadalajara", country: "MEX", }, language: "mx", ip: "1.1.1.1", ua: "ua", }, app: { bundle: "appBundle", name: "application name", }, user: {}, ext: { auctionid_str: "auctionidentity", inventory_source: 36, }, at: 1, }, adcandidates: [ { line_item_id: 1, line_item_alternative_id: "11", creative_ids: [111], }, { line_item_id: 2, line_item_alternative_id: "22", creative_ids: [222], }, { line_item_id: 3, line_item_alternative_id: "33", creative_ids: [333], }, ], }; }); afterEach(() => { mockDate.reset(); }); it("should map correctly openRTB request into choices request (first price)", async () => { mockDate.set("2021-08-01T12:00:00.000Z"); const request: ChoiceRequest = toChoiceRequest(openRtbRequest); expect(request.id).to.equal("62c64e52-c3ed-9e8e-5ea3-faa8cd0582a9"); expect(request.inventory_source).to.equal(RequestInventorySource.EXTERNAL); expect(request.context).to.deep.equal({ day_of_week: 0, hour: 12, city: "Guadalajara", ssp: "adru", country: "MX", }); expect(request.device).to.deep.equal({ name: "Shiftphone S3", language: "mx", os_version: "1.2.3", os: "Android", connectivity: "wifi", isp: "Mexicell", ip: "1.1.1.1", ua: "ua", ipv6: "", }); expect(request.user).to.deep.equal({ odid: constants.DEFAULT_ODID, data_optin: true, }); expect(request.app).to.deep.equal({ bundle_id: "appBundle", app_id: "appBundle", name: "application name", }); expect(request.campaigns).to.deep.equal([ { id: "11", category_iab: [], metadata: {}, metadataTmp: {}, stepsPassedStatus: {}, }, { id: "22", category_iab: [], metadata: {}, metadataTmp: {}, stepsPassedStatus: {}, }, { id: "33", category_iab: [], metadata: {}, metadataTmp: {}, stepsPassedStatus: {}, }, ]); expect(request.bidfloor).to.equal(8); expect(request.decodedRequest).to.deep.equal(openRtbRequest); }); it("should map correctly openRTB request into choices request (second price)", async () => { mockDate.set("2021-08-01T12:00:00.000Z"); openRtbRequest.bid_request.at = 0; const request: ChoiceRequest = toChoiceRequest(openRtbRequest); expect(request.id).to.equal("62c64e52-c3ed-9e8e-5ea3-faa8cd0582a9"); expect(request.inventory_source).to.equal(RequestInventorySource.EXTERNAL); expect(request.context).to.deep.equal({ day_of_week: 0, hour: 12, city: "Guadalajara", ssp: "adru", country: "MX", }); expect(request.device).to.deep.equal({ name: "Shiftphone S3", language: "mx", os_version: "1.2.3", os: "Android", connectivity: "wifi", isp: "Mexicell", ip: "1.1.1.1", ua: "ua", ipv6: "", }); expect(request.user).to.deep.equal({ odid: constants.DEFAULT_ODID, data_optin: true, }); expect(request.app).to.deep.equal({ bundle_id: "appBundle", app_id: "appBundle", name: "application name", }); expect(request.campaigns).to.deep.equal([ { id: "11", category_iab: [], metadata: {}, metadataTmp: {}, stepsPassedStatus: {}, }, { id: "22", category_iab: [], metadata: {}, metadataTmp: {}, stepsPassedStatus: {}, }, { id: "33", category_iab: [], metadata: {}, metadataTmp: {}, stepsPassedStatus: {}, }, ]); expect(request.bidfloor).to.equal(8); expect(request.decodedRequest).to.deep.equal(openRtbRequest); }); it("should map correctly into openRTB response (first price auction)", async () => { const choiceRequest: ChoiceRequest = toChoiceRequest(openRtbRequest); const winningCampaign = choiceRequest.campaigns.find((c) => c.id === "22"); winningCampaign!.metadata.bid_price = 8; choiceRequest.campaigns = [winningCampaign!]; const response = toOpenRtbResponse(openRtbRequest, choiceRequest); expect(response).to.deep.equal({ bids: [ { line_item_id: 1, creative: { id: 111, }, bid_price_micros: 0, }, { line_item_id: 2, creative: { id: 222, }, bid_price_micros: 8000, }, { line_item_id: 3, creative: { id: 333, }, bid_price_micros: 0, }, ], }); }); it("should map correctly into openRTB response (no winner)", async () => { const choiceRequest: ChoiceRequest = toChoiceRequest(openRtbRequest); choiceRequest.campaigns = []; const response = toOpenRtbResponse(openRtbRequest, choiceRequest); expect(response).to.deep.equal({ bids: [ { line_item_id: 1, creative: { id: 111, }, bid_price_micros: 0, }, { line_item_id: 2, creative: { id: 222, }, bid_price_micros: 0, }, { line_item_id: 3, creative: { id: 333, }, bid_price_micros: 0, }, ], }); }); });