<commit_before>/**
 * Copyright IBM Corporation 2015
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 **/

import XCTest
@testable import WatsonSDK

class SpeechToTextTests: XCTestCase {
    
    private let service = SpeechToText()
    
    private var inputText: String?
    private let timeout: NSTimeInterval = 30.0
    
    var recorder: AVAudioRecorder!
    var recordingSession: AVAudioSession!
    
    private lazy var username: String = ""
    private lazy var password: String = ""
    
    override func setUp() {
        super.setUp()
        if let url = NSBundle(forClass: self.dynamicType).pathForResource("Credentials", ofType: "plist") {
            if let dict = NSDictionary(contentsOfFile: url) as? Dictionary<String, String> {
               
                username = dict["SpeechToTextUsername"]!
                password = dict["<PASSWORD>"]!
                
                service.setUsernameAndPassword(
                    dict["SpeechToTextUsername"]!,
                    password: dict["<PASSWORD>"]!)
                
            } else {
                XCTFail("Unable to extract dictionary from plist")
            }
        } else {
            XCTFail("Plist file not found")
        }
        
        recordingSession = AVAudioSession.sharedInstance()
        
        do {
            try recordingSession.setCategory(AVAudioSessionCategoryPlayAndRecord)
            try recordingSession.setActive(true)
            
        } catch {
            
        }
    }
    
    override func tearDown() {
        // Put teardown code here. This method is called after the invocation of each test method in the class.
        super.tearDown()
    }
    
    func testEncoding() {
        
        let url = NSBundle(forClass: self.dynamicType).URLForResource("test", withExtension: "raw")
        if let url = url
        {
            if let data = NSData(contentsOfURL: url)
            {
                let encodedAudio = service.encodeOpus( data)
                
                XCTAssertLessThan(encodedAudio.length, data.length,
                    "Encoded audio must be smaller than the original.")
                
            } else {
                XCTAssert(true, "Could not load test PCM file")
            }
            
        }
    
    }
    
    
    func testGetToken() {
        
        let expectation = expectationWithDescription("TokenExpectation")
        
        let socket = WatsonSocket()
        socket.username = self.username
        socket.password = <PASSWORD>
    
        socket.getToken(username, password: password, oncompletion: {
            token, error in
            
            XCTAssertNotNil(token, "Token must be returned")
            
            if let token = token {
                XCTAssertGreaterThan(token.characters.count, 10,
                    "Token does not appear to be long enough")
            }
            
            Log.sharedLogger.info(token)
            expectation.fulfill()
            
        })
        
        waitForExpectationsWithTimeout(timeout) {
            error in XCTAssertNil(error, "Timeout")
        }
        
    }
    
    func testWatsonSockets() {
    
        let expectation = expectationWithDescription("WatsonSocket Expectation")
        
        let data = NSData()
        
        let socket = WatsonSocket()
        socket.username = self.username
        socket.password = <PASSWORD>

        socket.send(data)
            
        sleep(5)
        
        if let ws = socket.socket {
            XCTAssertTrue(ws.isConnected, "Web socket is not connected")
        }
            
        expectation.fulfill()
        
        
        waitForExpectationsWithTimeout(timeout) {
            error in XCTAssertNil(error, "Timeout")
        }

        
    }
    
    func testContinuousRecording() {
        
        service.startListening()
                
    }
    
//    func testRecording() {
//    
//        let recordExpectation = expectationWithDescription("Record")
//        
//        let recordSettings = [
//            // AVFormatIDKey: NSNumber(unsignedInt:kAudioFormatLinearPCM),
//            AVNumberOfChannelsKey: 1,
//            AVSampleRateKey : 16000.0
//        ]
//        
//        let dirsPaths = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)
//        let docsDir = dirsPaths[0] as String
//        let soundFilePath = docsDir + "/test.wav"
//        
//        print("Saving recorded audio file in \(soundFilePath)")
//        AVAudioSession.sharedInstance().requestRecordPermission({(granted: Bool) -> Void
//            in
//            if granted {
//                let soundFileURL = NSURL(string: soundFilePath)
//                
//                if let soundFileURL = soundFileURL {
//                    <commit_msg>Moved token fetching to the Websocket<commit_after>/**
 * Copyright IBM Corporation 2015
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 **/

import XCTest
@testable import WatsonSDK

class SpeechToTextTests: XCTestCase {
    
    private let service = SpeechToText()
    
    private var inputText: String?
    private let timeout: NSTimeInterval = 30.0
    
    var recorder: AVAudioRecorder!
    var recordingSession: AVAudioSession!
    
    private lazy var username: String = ""
    private lazy var password: String = ""
    
    override func setUp() {
        super.setUp()
        if let url = NSBundle(forClass: self.dynamicType).pathForResource("Credentials", ofType: "plist") {
            if let dict = NSDictionary(contentsOfFile: url) as? Dictionary<String, String> {
               
                username = dict["SpeechToTextUsername"]!
                password = dict["SpeechToTextPassword"]!
                
                service.setUsernameAndPassword(
                    dict["SpeechToTextUsername"]!,
                    password: dict["SpeechToTextPassword"]!)
                
            } else {
                XCTFail("Unable to extract dictionary from plist")
            }
        } else {
            XCTFail("Plist file not found")
        }
        
        recordingSession = AVAudioSession.sharedInstance()
        
        do {
            try recordingSession.setCategory(AVAudioSessionCategoryPlayAndRecord)
            try recordingSession.setActive(true)
            
        } catch {
            
        }
    }
    
    override func tearDown() {
        // Put teardown code here. This method is called after the invocation of each test method in the class.
        super.tearDown()
    }
    
    func testEncoding() {
        
        let url = NSBundle(forClass: self.dynamicType).URLForResource("test", withExtension: "raw")
        if let url = url
        {
            if let data = NSData(contentsOfURL: url)
            {
                let encodedAudio = service.encodeOpus( data)
                
                XCTAssertLessThan(encodedAudio.length, data.length,
                    "Encoded audio must be smaller than the original.")
                
            } else {
                XCTAssert(true, "Could not load test PCM file")
            }
            
        }
    
    }
    
    
       
    func testContinuousRecording() {
        
        service.startListening()
                
    }
    
//    func testRecording() {
//    
//        let recordExpectation = expectationWithDescription("Record")
//        
//        let recordSettings = [
//            // AVFormatIDKey: NSNumber(unsignedInt:kAudioFormatLinearPCM),
//            AVNumberOfChannelsKey: 1,
//            AVSampleRateKey : 16000.0
//        ]
//        
//        let dirsPaths = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)
//        let docsDir = dirsPaths[0] as String
//        let soundFilePath = docsDir + "/test.wav"
//        
//        print("Saving recorded audio file in \(soundFilePath)")
//        AVAudioSession.sharedInstance().requestRecordPermission({(granted: Bool) -> Void
//            in
//            if granted {
//                let soundFileURL = NSURL(string: soundFilePath)
//                
//                if let soundFileURL = soundFileURL {
//                    
{'fraction_non_alphanumeric': 0.08314387788242936, 'fraction_numerical': 0.0044386705640359425, 'mean_word_length': 1.7011695906432749, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '821485', 'n_tokens_mistral': 2381, 'n_tokens_neox': 2300, 'n_words': 640}