<commit_before>//
//  Copyright RevenueCat Inc. All Rights Reserved.
//
//  Licensed under the MIT License (the "License");
//  you may not use this file except in compliance with the License.
//  You may obtain a copy of the License at
//
//      https://opensource.org/licenses/MIT
//
//  PurchasesAttributionDataTests.swift
//
//  Created by <NAME> on 5/31/22.

import Nimble
import StoreKit
import XCTest

@testable import RevenueCat

class PurchasesAttributionDataTests: BasePurchasesTests {

    func testAddAttributionAlwaysAddsAdIdsEmptyDict() throws {
        self.setupPurchases()

        Purchases.deprecated.addAttributionData([:], fromNetwork: .adjust)

        let attributionData = try XCTUnwrap(
            self.subscriberAttributesManager
                .invokedConvertAttributionDataAndSetParameters?
                .attributionData
        )

        expect(attributionData.count) == 2
        expect(attributionData["rc_idfa"] as? String) == "rc_idfa"
        expect(attributionData["rc_idfv"] as? String) == "rc_idfv"
    }

    @available(*, deprecated)
    func testPassesTheArrayForAllNetworks() {
        self.setupPurchases()

        let data = ["yo": "dog", "what": 45, "is": ["up"]] as [String: Any]

        Purchases.deprecated.addAttributionData(data, fromNetwork: .appleSearchAds)

        for key in data.keys {
            expect(self.backend.invokedPostAttributionDataParametersList[0].data?.keys.contains(key))
                .toEventually(beTrue())
        }
        expect(self.backend.invokedPostAttributionDataParametersList[0].data?.keys.contains("rc_idfa")) == true
        expect(self.backend.invokedPostAttributionDataParametersList[0].data?.keys.contains("rc_idfv")) == true
        expect(self.backend.invokedPostAttributionDataParametersList[0].network) == .appleSearchAds
        expect(self.backend.invokedPostAttributionDataParametersList[0].appUserID) == self.purchases?.appUserID
    }

    func testAttributionDataIsPostponedIfThereIsNoInstance() throws {
        let data = ["yo": "dog", "what": 45, "is": ["up"]] as [String: Any]

        Purchases.deprecated.addAttributionData(data, fromNetwork: .appsFlyer)

        self.setupPurchases()

        let invokedParameters = try XCTUnwrap(
            self.subscriberAttributesManager.invokedConvertAttributionDataAndSetParameters
        )

        for key in data.keys {
            expect(invokedParameters.attributionData.keys.contains(key)).toEventually(beTrue())
        }

        expect(invokedParameters.attributionData.keys.contains("rc_idfa")) == true
        expect(invokedParameters.attributionData.keys.contains("rc_idfv")) == true
        expect(invokedParameters.network) == .appsFlyer
        expect(invokedParameters.appUserID) == self.purchases?.appUserID
    }

    @available(*, deprecated)
    func testAttributionDataSendsNetworkAppUserId() throws {
        let data = ["yo": "dog", "what": 45, "is": ["up"]] as [String: Any]

        Purchases.deprecated.addAttributionData(data,
                                                from: .appleSearchAds,
                                                forNetworkUserId: "newuser")

        self.setupPurchases()

        expect(self.backend.invokedPostAttributionData).toEventually(beTrue())

        let invokedMethodParams = try XCTUnwrap(self.backend.invokedPostAttributionDataParameters)
        for key in data.keys {
            expect(invokedMethodParams.data?.keys.contains(key)).to(beTrue())
        }

        expect(invokedMethodParams.data?.keys.contains("rc_idfa")) == true
        expect(invokedMethodParams.data?.keys.contains("rc_idfv")) == true
        expect(invokedMethodParams.data?.keys.contains("rc_attribution_network_id")) == true
        expect(invokedMethodParams.data?["rc_attribution_network_id"] as? String) == "newuser"
        expect(invokedMethodParams.network) == .appleSearchAds
        expect(invokedMethodParams.appUserID) == identityManager.currentAppUserID
    }

    @available(*, deprecated)
    func testAttributionDataDontSendNetworkAppUserIdIfNotProvided() throws {
        let data = ["yo": "dog", "what": 45, "is": ["up"]] as [String: Any]

        Purchases.deprecated.addAttributionData(data, fromNetwork: .appleSearchAds)

        self.setupPurchases()

        let invokedMethodParams = try XCTUnwrap(self.backend.invokedPostAttributionDataParameters)
        for key in data.keys {
            expect(invokedMethodParams.data?.keys.contains(key)) == true
        }

        expect(invokedMethodParams.data?.keys.contains("rc_idfa")) == true
        expect(invokedMethodParams.data?.keys.contains("rc_idfv")) == true
        expect(invokedMethodParams.data?.keys.contains("rc_attribution_network_id")) == false
        expect(invokedMethodParams.network) == .appleSearchAds
        expect(invokedMethodParams.appUserID) == identityManager.currentAppUserID
    }

    @available(*, deprecated)
    func testAdClientAttributionDataIsAutomaticallyCollected() throws {
        self.setupPurchases(automaticCollection: true)

        let invokedMethodParams = try XCTUnwrap(self.backend.invokedPostAttributionDataParameters)

        expect(invokedMethodParams).toNot(beNil())
        expect(invokedMethodParams.network) == .appleSearchAds

        let obtainedVersionData = try XCTUnwrap(invokedMethodParams.data?["Version3.1"] as? NSDictionary)
        expect(obtainedVersionData["iad-campaign-id"]).toNot(beNil())
    }

    func testAdClientAttributionDataIsNotAutomaticallyCollectedIfDisabled() {
        self.setupPurchases(automaticCollection: false)
        expect(self.backend.invokedPostAttributionDataParameters).to(beNil())
    }

    func testAttributionDataPostponesMultiple() {
        let data = ["yo": "dog", "what": 45, "is": ["up"]] as [String: Any]

        Purchases.deprecated.addAttributionData(data, from: .adjust, forNetworkUserId: "newuser")

        self.setupPurchases(automaticCollection: true)
        expect(self.backend.invokedPostAttributionDataParametersList.count) == 1
        expect(self.subscriberAttributesManager.invokedConvertAttributionDataAndSetParametersList.count) == 1
    }

}<commit_msg>`PurchasesAttributionDataTests`: fixed potential race condition in flaky test (#1805)<commit_after>//
//  Copyright RevenueCat Inc. All Rights Reserved.
//
//  Licensed under the MIT License (the "License");
//  you may not use this file except in compliance with the License.
//  You may obtain a copy of the License at
//
//      https://opensource.org/licenses/MIT
//
//  PurchasesAttributionDataTests.swift
//
//  Created by <NAME> on 5/31/22.

import Nimble
import StoreKit
import XCTest

@testable import RevenueCat

class PurchasesAttributionDataTests: BasePurchasesTests {

    func testAddAttributionAlwaysAddsAdIdsEmptyDict() throws {
        self.setupPurchases()

        Purchases.deprecated.addAttributionData([:], fromNetwork: .adjust)

        let attributionData = try XCTUnwrap(
            self.subscriberAttributesManager
                .invokedConvertAttributionDataAndSetParameters?
                .attributionData
        )

        expect(attributionData.count) == 2
        expect(attributionData["rc_idfa"] as? String) == "rc_idfa"
        expect(attributionData["rc_idfv"] as? String) == "rc_idfv"
    }

    @available(*, deprecated)
    func testPassesTheArrayForAllNetworks() {
        self.setupPurchases()

        let data = ["yo": "dog", "what": 45, "is": ["up"]] as [String: Any]

        Purchases.deprecated.addAttributionData(data, fromNetwork: .appleSearchAds)

        for key in data.keys {
            expect(self.backend.invokedPostAttributionDataParametersList[0].data?.keys.contains(key))
                .toEventually(beTrue())
        }
        expect(self.backend.invokedPostAttributionDataParametersList[0].data?.keys.contains("rc_idfa")) == true
        expect(self.backend.invokedPostAttributionDataParametersList[0].data?.keys.contains("rc_idfv")) == true
        expect(self.backend.invokedPostAttributionDataParametersList[0].network) == .appleSearchAds
        expect(self.backend.invokedPostAttributionDataParametersList[0].appUserID) == self.purchases?.appUserID
    }

    func testAttributionDataIsPostponedIfThereIsNoInstance() throws {
        let data = ["yo": "dog", "what": 45, "is": ["up"]] as [String: Any]

        Purchases.deprecated.addAttributionData(data, fromNetwork: .appsFlyer)

        self.setupPurchases()

        let invokedParameters = try XCTUnwrap(
            self.subscriberAttributesManager.invokedConvertAttributionDataAndSetParameters
        )

        for key in data.keys {
            expect(invokedParameters.attributionData.keys.contains(key)).toEventually(beTrue())
        }

        expect(invokedParameters.attributionData.keys.contains("rc_idfa")) == true
        expect(invokedParameters.attributionData.keys.contains("rc_idfv")) == true
        expect(invokedParameters.network) == .appsFlyer
        expect(invokedParameters.appUserID) == self.purchases?.appUserID
    }

    @available(*, deprecated)
    func testAttributionDataSendsNetworkAppUserId() throws {
        let data = ["yo": "dog", "what": 45, "is": ["up"]] as [String: Any]

        Purchases.deprecated.addAttributionData(data,
                                                from: .appleSearchAds,
                                                forNetworkUserId: "newuser")

        self.setupPurchases()

        expect(self.backend.invokedPostAttributionData).toEventually(beTrue())

        let invokedMethodParams = try XCTUnwrap(self.backend.invokedPostAttributionDataParameters)
        for key in data.keys {
            expect(invokedMethodParams.data?.keys.contains(key)).to(beTrue())
        }

        expect(invokedMethodParams.data?.keys.contains("rc_idfa")) == true
        expect(invokedMethodParams.data?.keys.contains("rc_idfv")) == true
        expect(invokedMethodParams.data?.keys.contains("rc_attribution_network_id")) == true
        expect(invokedMethodParams.data?["rc_attribution_network_id"] as? String) == "newuser"
        expect(invokedMethodParams.network) == .appleSearchAds
        expect(invokedMethodParams.appUserID) == identityManager.currentAppUserID
    }

    @available(*, deprecated)
    func testAttributionDataDontSendNetworkAppUserIdIfNotProvided() throws {
        let data = ["yo": "dog", "what": 45, "is": ["up"]] as [String: Any]

        Purchases.deprecated.addAttributionData(data, fromNetwork: .appleSearchAds)

        self.setupPurchases()

        let invokedMethodParams = try XCTUnwrap(self.backend.invokedPostAttributionDataParameters)
        for key in data.keys {
            expect(invokedMethodParams.data?.keys.contains(key)) == true
        }

        expect(invokedMethodParams.data?.keys.contains("rc_idfa")) == true
        expect(invokedMethodParams.data?.keys.contains("rc_idfv")) == true
        expect(invokedMethodParams.data?.keys.contains("rc_attribution_network_id")) == false
        expect(invokedMethodParams.network) == .appleSearchAds
        expect(invokedMethodParams.appUserID) == identityManager.currentAppUserID
    }

    @available(*, deprecated)
    func testAdClientAttributionDataIsAutomaticallyCollected() throws {
        self.setupPurchases(automaticCollection: true)

        let invokedMethodParams = try XCTUnwrap(self.backend.invokedPostAttributionDataParameters)

        expect(invokedMethodParams).toNot(beNil())
        expect(invokedMethodParams.network) == .appleSearchAds

        let obtainedVersionData = try XCTUnwrap(invokedMethodParams.data?["Version3.1"] as? NSDictionary)
        expect(obtainedVersionData["iad-campaign-id"]).toNot(beNil())
    }

    func testAdClientAttributionDataIsNotAutomaticallyCollectedIfDisabled() {
        self.setupPurchases(automaticCollection: false)
        expect(self.backend.invokedPostAttributionDataParameters).to(beNil())
    }

    func testAttributionDataPostponesMultiple() {
        let data = ["yo": "dog", "what": 45, "is": ["up"]] as [String: Any]

        Purchases.deprecated.addAttributionData(data, from: .adjust, forNetworkUserId: "newuser")

        self.setupPurchases(automaticCollection: true)

        expect(self.backend.invokedPostAttributionDataParametersList).toEventually(haveCount(1))
        expect(self.subscriberAttributesManager.invokedConvertAttributionDataAndSetParametersList).to(haveCount(1))
    }

}
{'fraction_non_alphanumeric': 0.11099476439790576, 'fraction_numerical': 0.004349577124446234, 'mean_word_length': 3.4694024478041756, 'pattern_counts': {'":': 30, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '680560', 'n_tokens_mistral': 3697, 'n_tokens_neox': 3478, 'n_words': 572}