/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements. See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership. The ASF licenses this file
 * to you 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.
 */
 /* jshint -W100 */
 
/*
 * Fully Async JavaScript test suite for ThriftTest.thrift. 
 * These tests are designed to exercise the WebSocket transport
 * (which is exclusively async).
 *
 * To compile client code for this test use:
 *      $ thrift -gen js ThriftTest.thrift
 */



// all Languages in UTF-8
var stringTest = "Afrikaans, Alemannisch, Aragonés, العربية, مصرى, Asturianu, Aymar aru, Azərbaycan, Башҡорт, Boarisch, Žemaitėška, Беларуская, Беларуская (тарашкевіца), Български, Bamanankan, বাংলা, Brezhoneg, Bosanski, Català, Mìng-dĕ̤ng-ngṳ̄, Нохчийн, Cebuano, ᏣᎳᎩ, Česky, Словѣ́ньскъ / ⰔⰎⰑⰂⰡⰐⰠⰔⰍⰟ, Чӑвашла, Cymraeg, Dansk, Zazaki, ދިވެހިބަސް, Ελληνικά, Emiliàn e rumagnòl, English, Esperanto, Español, Eesti, Euskara, فارسی, Suomi, Võro, Føroyskt, Français, Arpetan, Furlan, Frysk, Gaeilge, 贛語, Gàidhlig, Galego, Avañe'ẽ, ગુજરાતી, Gaelg, עברית, हिन्दी, Fiji Hindi, Hrvatski, Kreyòl ayisyen, Magyar, Հայերեն, Interlingua, Bahasa Indonesia, Ilokano, Ido, Íslenska, Italiano, 日本語, Lojban, Basa Jawa, ქართული, Kongo, Kalaallisut, ಕನ್ನಡ, 한국어, Къарачай-Малкъар, Ripoarisch, Kurdî, Коми, Kernewek, Кыргызча, Latina, Ladino, Lëtzebuergesch, Limburgs, Lingála, ລາວ, Lietuvių, Latviešu, Basa Banyumasan, Malagasy, Македонски, മലയാളം, मराठी, Bahasa Melayu, مازِرونی, Nnapulitano, Nedersaksisch, नेपाल भाषा, Nederlands, ‪Norsk (nynorsk)‬, ‪Norsk (bokmål)‬, Nouormand, Diné bizaad, Occitan, Иронау, Papiamentu, Deitsch, Norfuk / Pitkern, Polski, پنجابی, پښتو, Português, Runa Simi, Rumantsch, Romani, Română, Русский, Саха тыла, Sardu, Sicilianu, Scots, Sámegiella, Simple English, Slovenčina, Slovenščina, Српски / Srpski, Seeltersk, Svenska, Kiswahili, தமிழ், తెలుగు, Тоҷикӣ, ไทย, Türkmençe, Tagalog, Türkçe, Татарча/Tatarça, Українська, اردو, Tiếng Việt, Volapük, Walon, Winaray, 吴语, isiXhosa, ייִדיש, Yorùbá, Zeêuws, 中文, Bân-lâm-gú, 粵語";
  
function checkRecursively(map1, map2) {
  if (typeof map1 !== 'function' && typeof map2 !== 'function') {
    if (!map1 || typeof map1 !== 'object') {
        equal(map1, map2);
    } else {
      for (var key in map1) {
        checkRecursively(map1[key], map2[key]);
      }
    }
  }
}

module("Base Types");

  asyncTest("Void", function() {
    expect( 1 );
    client.testVoid(function(result) {
      equal(result, undefined);
      QUnit.start();
    });
  });
  
  
  asyncTest("String", function() {
    expect( 3 );
    QUnit.stop(2);
    client.testString('', function(result){
       equal(result, '');
       QUnit.start();
    });
    client.testString(stringTest, function(result){
       equal(result, stringTest);
       QUnit.start();
    });

    var specialCharacters = 'quote: \" backslash:' +
          ' forwardslash-escaped: \/ ' +
          ' backspace: \b formfeed: \f newline: \n return: \r tab: ' +
          ' now-all-of-them-together: "\\\/\b\n\r\t' +
          ' now-a-bunch-of-junk: !@#$%&()(&%$#{}{}<><><';
    client.testString(specialCharacters, function(result){
       equal(result, specialCharacters);
       QUnit.start();
    });
  });
  asyncTest("Double", function() {
    expect( 4 );
    QUnit.stop(3);
    client.testDouble(0, function(result){
       equal(result, 0);
       QUnit.start();
    });
    client.testDouble(-1, function(result){
       equal(result, -1);
       QUnit.start();
    });
    client.testDouble(3.14, function(result){
       equal(result, 3.14);
       QUnit.start();
    });
    client.testDouble(Math.pow(2,60), function(result){
       equal(result, Math.pow(2,60));
       QUnit.start();
    });
  });
  asyncTest("Byte", function() {
    expect( 2 );
    QUnit.stop();
    client.testByte(0, function(result) {
       equal(result, 0);
       QUnit.start();
    });
    client.testByte(0x01, function(result) {
       equal(result, 0x01);
       QUnit.start();
    });
  });
  asyncTest("I32", function() {
    expect( 3 );
    QUnit.stop(2);
    client.testI32(0, function(result){
       equal(result, 0);
       QUnit.start();
    });
    client.testI32(Math.pow(2,30), function(result){
       equal(result, Math.pow(2,30));
       QUnit.start();
    });
    client.testI32(-Math.pow(2,30), function(result){
       equal(result, -Math.pow(2,30));
       QUnit.start();
    });
  });
  asyncTest("I64", function() {
    expect( 3 );
    QUnit.stop(2);
    client.testI64(0, function(result){
       equal(result, 0);
       QUnit.start();
    });
    //This is usually 2^60 but JS cannot represent anything over 2^52 accurately
    client.testI64(Math.pow(2,52), function(result){
       equal(result, Math.pow(2,52));
       QUnit.start();
    });
    client.testI64(-Math.pow(2,52), function(result){
       equal(result, -Math.pow(2,52));
       QUnit.start();
    });
  });
  

  

module("Structured Types");

  asyncTest("Struct", function() {
    expect( 5 );
    var structTestInput = new ThriftTest.Xtruct();
    structTestInput.string_thing = 'worked';
    structTestInput.byte_thing = 0x01;
    structTestInput.i32_thing = Math.pow(2,30);
    //This is usually 2^60 but JS cannot represent anything over 2^52 accurately
    structTestInput.i64_thing = Math.pow(2,52);

    client.testStruct(structTestInput, function(result){
      equal(result.string_thing, structTestInput.string_thing);
      equal(result.byte_thing, structTestInput.byte_thing);
      equal(result.i32_thing, structTestInput.i32_thing);
      equal(result.i64_thing, structTestInput.i64_thing);
      equal(JSON.stringify(result), JSON.stringify(structTestInput));
      QUnit.start();      
    });
  });

  asyncTest("Nest", function() {
    expect( 7 );
    var xtrTestInput = new ThriftTest.Xtruct();
    xtrTestInput.string_thing = 'worked';
    xtrTestInput.byte_thing = 0x01;
    xtrTestInput.i32_thing = Math.pow(2,30);
    //This is usually 2^60 but JS cannot represent anything over 2^52 accurately
    xtrTestInput.i64_thing = Math.pow(2,52);
    
    var nestTestInput = new ThriftTest.Xtruct2();
    nestTestInput.byte_thing = 0x02;
    nestTestInput.struct_thing = xtrTestInput;
    nestTestInput.i32_thing = Math.pow(2,15);
    
    client.testNest(nestTestInput, function(result){
      equal(result.byte_thing, nestTestInput.byte_thing);
      equal(result.struct_thing.string_thing, nestTestInput.struct_thing.string_thing);
      equal(result.struct_thing.byte_thing, nestTestInput.struct_thing.byte_thing);
      equal(result.struct_thing.i32_thing, nestTestInput.struct_thing.i32_thing);
      equal(result.struct_thing.i64_thing, nestTestInput.struct_thing.i64_thing);
      equal(result.i32_thing, nestTestInput.i32_thing);  
      equal(JSON.stringify(result), JSON.stringify(nestTestInput));
      QUnit.start();      
    });
  });

  asyncTest("Map", function() {
    expect( 3 );
    var mapTestInput = {7:77, 8:88, 9:99};

    client.testMap(mapTestInput, function(result){
      for (var key in result) {
        equal(result[key], mapTestInput[key]);
      }
      QUnit.start();  
    });
  });

  asyncTest("StringMap", function() {
    expect( 6 );
    var mapTestInput = {
      "a":"123", "a b":"with spaces ", "same":"same", "0":"numeric key",
      "longValue":stringTest, stringTest:"long key"
    };

    client.testStringMap(mapTestInput, function(result){
      for (var key in result) {
        equal(result[key], mapTestInput[key]);
      }
      QUnit.start();
    });
  });

  asyncTest("Set", function() {
    expect( 1 );
    var setTestInput = [1,2,3];
    client.testSet(setTestInput, function(result){
      ok(result, setTestInput);
      QUnit.start();
    });
  });

  asyncTest("List", function() {
    expect( 1 );
    var listTestInput = [1,2,3];
    client.testList(listTestInput, function(result){
      ok(result, listTestInput);
      QUnit.start();
    });
  });

  asyncTest("Enum", function() {
    expect( 1 );
    client.testEnum(ThriftTest.Numberz.ONE, function(result){
      equal(result, ThriftTest.Numberz.ONE);
      QUnit.start();
    });
  });

  asyncTest("TypeDef", function() {
    expect( 1 );
    client.testTypedef(69, function(result){
      equal(result, 69);
      QUnit.start();
    });
  });


module("deeper!");

  asyncTest("MapMap", function() {
    expect( 16 );
    var mapMapTestExpectedResult = {
      "4":{"1":1,"2":2,"3":3,"4":4},
      "-4":{"-4":-4, "-3":-3, "-2":-2, "-1":-1}
    };

    client.testMapMap(1, function(result){
      for (var key in result) {
        for (var key2 in result[key]) {
          equal(result[key][key2], mapMapTestExpectedResult[key][key2]);
        }
      }
      checkRecursively(result, mapMapTestExpectedResult);
      QUnit.start();
    });
  });


module("Exception");

  asyncTest("Xception", function() {
    expect(2);
    client.testException("Xception", function(e){
      equal(e.errorCode, 1001);
      equal(e.message, "Xception");
      QUnit.start();
    });
  });

  asyncTest("no Exception", 0, function() {
    expect( 1 );
    client.testException("no Exception", function(e){
      ok(!e);
      QUnit.start();
    });
  });

module("Insanity");

  asyncTest("testInsanity", function() {
    expect( 24 );
    var insanity = {
      "1":{
        "2":{
          "userMap":{ "5":5, "8":8 },
          "xtructs":[{
              "string_thing":"Goodbye4",
              "byte_thing":4,
              "i32_thing":4,
              "i64_thing":4
            },
            {
              "string_thing":"Hello2",
              "byte_thing":2,
              "i32_thing":2,
              "i64_thing":2
            }
          ]
        },
        "3":{
          "userMap":{ "5":5, "8":8 },
          "xtructs":[{
              "string_thing":"Goodbye4",
              "byte_thing":4,
              "i32_thing":4,
              "i64_thing":4
            },
            {
              "string_thing":"Hello2",
              "byte_thing":2,
              "i32_thing":2,
              "i64_thing":2
            }
          ]
        }
      },
      "2":{ "6":{ "userMap":null, "xtructs":null } }
    };
    client.testInsanity(new ThriftTest.Insanity(), function(res){
      ok(res, JSON.stringify(res));
      ok(insanity, JSON.stringify(insanity));
      checkRecursively(res, insanity);
      QUnit.start();
    });
  });


