<commit_before>//
//  StoreKitIntegrationTests.swift
//  StoreKitIntegrationTests
//
//  Created by <NAME> on 5/3/21.
//  Copyright © 2021 Purchases. All rights reserved.
//

import Nimble
import RevenueCat
import StoreKitTest
import XCTest

class StoreKit2IntegrationTests: StoreKit1IntegrationTests {

    override class var sk2Enabled: Bool { return true }

}

class StoreKit1IntegrationTests: BaseBackendIntegrationTests {

    private var testSession: SKTestSession!

    private static let timeout: DispatchTimeInterval = .seconds(10)

    override func setUpWithError() throws {
        try super.setUpWithError()

        testSession = try SKTestSession(configurationFileNamed: Constants.storeKitConfigFileName)
        testSession.resetToDefaultState()
        testSession.disableDialogs = true
        testSession.clearTransactions()
    }

    func testCanGetOfferings() async throws {
        let receivedOfferings = try await Purchases.shared.offerings()
        expect(receivedOfferings.all).toNot(beEmpty())
    }

    func testCanMakePurchase() async throws {
        try await self.purchaseMonthlyOffering()

        try self.verifyEntitlementWentThrough()
        let entitlements = self.purchasesDelegate.customerInfo?.entitlements
        expect(entitlements?[Self.entitlementIdentifier]?.isActive) == true
    }

    func testPurchaseMadeBeforeLogInIsRetainedAfter() async throws {
        let customerInfo = try await self.purchaseMonthlyOffering().customerInfo<commit_msg>`BackendIntegrationTests`: added test for failed purchase (#1449)<commit_after>//
//  StoreKitIntegrationTests.swift
//  StoreKitIntegrationTests
//
//  Created by <NAME> on 5/3/21.
//  Copyright © 2021 Purchases. All rights reserved.
//

import Nimble
import RevenueCat
import StoreKitTest
import XCTest

class StoreKit2IntegrationTests: StoreKit1IntegrationTests {

    override class var sk2Enabled: Bool { return true }

}

class StoreKit1IntegrationTests: BaseBackendIntegrationTests {

    private var testSession: SKTestSession!

    private static let timeout: DispatchTimeInterval = .seconds(10)

    override func setUpWithError() throws {
        try super.setUpWithError()

        testSession = try SKTestSession(configurationFileNamed: Constants.storeKitConfigFileName)
        testSession.resetToDefaultState()
        testSession.disableDialogs = true
        testSession.clearTransactions()
    }

    func testCanGetOfferings() async throws {
        let receivedOfferings = try await Purchases.shared.offerings()
        expect(receivedOfferings.all).toNot(beEmpty())
    }

    func testCanMakePurchase() async throws {
        try await self.purchaseMonthlyOffering()

        try self.verifyEntitlementWentThrough()
        let entitlements = self.purchasesDelegate.customerInfo?.entitlements
        expect(entitlements?[Self.entitlementIdentifier]?.isActive) == true
    }

    func testPurchaseFailuresAreReportedCorrectly() async throws {
        self.testSession.failTransactionsEnabled = true
        self.testSession.failureError = .invalidSignature

        do {
            try await self.purchaseMonthlyOffering()
            fail("Expected error")
        } catch {
            expect(error).to(matchError(ErrorCode.invalidPromotionalOfferError))
        }
    }

    func testPurchaseMadeBeforeLogInIsRetainedAfter() async throws {
        let customerInfo = try await self.purchaseMonthlyOffering().customerInfo
{'fraction_non_alphanumeric': 0.07497820401046207, 'fraction_numerical': 0.00929962220284801, 'mean_word_length': 3.63257065948856, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6887207', 'n_tokens_mistral': 983, 'n_tokens_neox': 922, 'n_words': 207}