<commit_before>//
//  ChatManagerTests.swift
//  CriticalMapsTests
//
//  Created by <NAME> on 1/26/19.
//

@testable import CriticalMaps
import XCTest

class ChatManagerTests: XCTestCase {
    func getSetup() -> (chatManager: ChatManager, networkLayer: MockNetworkLayer, dataStore: DataStore) {
        let networkLayer = MockNetworkLayer()
        let dataStore = MemoryDataStore()
        let requestManager = RequestManager(dataStore: dataStore, locationProvider: MockLocationProvider(), networkLayer: networkLayer, deviceId: UUID().uuidString, url: Constants.apiEndpoint)
        let chatManager = ChatManager(requestManager: requestManager)
        return (chatManager, networkLayer, dataStore)
    }

    func testSendMessage() {
        let setup = getSetup()
        let timeInterval = Date().timeIntervalSince1970
        setup.networkLayer.mockResponse = ApiResponse(locations: [:], chatMessages: ["1233": ChatMessage(message: "Hello World", timestamp: timeInterval)])

        let exp = expectation(description: "Wait for response")

        setup.chatManager.send(message: "Hello World") { success in

            XCTAssertTrue(success)
            exp.fulfill()
        }
        wait(for: [exp], timeout: 1)
        guard let lastUsedBody = setup.networkLayer.lastUsedPostBody else {
            XCTFail()
            return
        }
        let messageJSONs = Array(lastUsedBody["messages"] as! [[String: Any]])
        XCTAssertEqual(messageJSONs.count, 1)

        guard let messageJSON = messageJSONs.first else {
            XCTFail()
            return
        }

        XCTAssertEqual(messageJSON["text"] as! String, "Hello World")
        XCTAssertEqual(messageJSON["timestamp"] as! TimeInterval, timeInterval, accuracy: 0.01)
        XCTAssertNotNil(messageJSON["identifier"])
    }

    func testSendMessageFails() {
        let setup = getSetup()
        setup.networkLayer.mockResponse = nil

        let exp = expectation(description: "Wait for response")

        setup.chatManager.send(message: "Hello World") { success in

            XCTAssertFalse(success)
            exp.fulfill()
        }
        wait(for: [exp], timeout: 1)
    }

    func testSendMessageIdentifierChanges() {
        let setup = getSetup()
        let timeInterval = Date().timeIntervalSince1970
        setup.networkLayer.mockResponse = ApiResponse(locations: [:], chatMessages: ["1233": ChatMessage(message: "Hello World", timestamp: timeInterval)])

        let firstExp = expectation(description: "Wait for response")

        setup.chatManager.send(message: "Hello World") { success in<commit_msg>increasing accuracy in timestamp check<commit_after>//
//  ChatManagerTests.swift
//  CriticalMapsTests
//
//  Created by <NAME> on 1/26/19.
//

@testable import CriticalMaps
import XCTest

class ChatManagerTests: XCTestCase {
    func getSetup() -> (chatManager: ChatManager, networkLayer: MockNetworkLayer, dataStore: DataStore) {
        let networkLayer = MockNetworkLayer()
        let dataStore = MemoryDataStore()
        let requestManager = RequestManager(dataStore: dataStore, locationProvider: MockLocationProvider(), networkLayer: networkLayer, deviceId: UUID().uuidString, url: Constants.apiEndpoint)
        let chatManager = ChatManager(requestManager: requestManager)
        return (chatManager, networkLayer, dataStore)
    }

    func testSendMessage() {
        let setup = getSetup()
        let timeInterval = Date().timeIntervalSince1970
        setup.networkLayer.mockResponse = ApiResponse(locations: [:], chatMessages: ["1233": ChatMessage(message: "Hello World", timestamp: timeInterval)])

        let exp = expectation(description: "Wait for response")

        setup.chatManager.send(message: "Hello World") { success in

            XCTAssertTrue(success)
            exp.fulfill()
        }
        wait(for: [exp], timeout: 1)
        guard let lastUsedBody = setup.networkLayer.lastUsedPostBody else {
            XCTFail()
            return
        }
        let messageJSONs = Array(lastUsedBody["messages"] as! [[String: Any]])
        XCTAssertEqual(messageJSONs.count, 1)

        guard let messageJSON = messageJSONs.first else {
            XCTFail()
            return
        }

        XCTAssertEqual(messageJSON["text"] as! String, "Hello World")
        XCTAssertEqual(messageJSON["timestamp"] as! TimeInterval, timeInterval, accuracy: 0.1)
        XCTAssertNotNil(messageJSON["identifier"])
    }

    func testSendMessageFails() {
        let setup = getSetup()
        setup.networkLayer.mockResponse = nil

        let exp = expectation(description: "Wait for response")

        setup.chatManager.send(message: "Hello World") { success in

            XCTAssertFalse(success)
            exp.fulfill()
        }
        wait(for: [exp], timeout: 1)
    }

    func testSendMessageIdentifierChanges() {
        let setup = getSetup()
        let timeInterval = Date().timeIntervalSince1970
        setup.networkLayer.mockResponse = ApiResponse(locations: [:], chatMessages: ["1233": ChatMessage(message: "Hello World", timestamp: timeInterval)])

        let firstExp = expectation(description: "Wait for response")

        setup.chatManager.send(message: "Hello World") { success in
{'fraction_non_alphanumeric': 0.10331681280975982, 'fraction_numerical': 0.010102935569958064, 'mean_word_length': 3.0206896551724136, 'pattern_counts': {'":': 4, '<': 5, '<?xml version=': 0, '>': 7, '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': '3506155', 'n_tokens_mistral': 1438, 'n_tokens_neox': 1370, 'n_words': 343}