blob: 614461dc1fcf3564c3f53feb0ceee7d2042604f7 [file] [log] [blame]
Mustafa Senol Cosarf86845e2018-12-05 17:50:18 +03001import { ThriftTest } from "./gen-js/ThriftTest_types";
2import "./gen-js/ThriftTest";
3
4var Int64 = require("node-int64");
5var JSONInt64 = require("json-int64");
6var QUnit = require("./qunit");
7
8const transport: Thrift.Transport = new Thrift.Transport("/service");
9const protocol: Thrift.Protocol = new Thrift.Protocol(transport);
Cameron Martincaef0ed2025-01-15 11:58:39 +010010const client: ThriftTest.ThriftTestClient = new ThriftTest.ThriftTestClient(
11 protocol,
12);
Mustafa Senol Cosarf86845e2018-12-05 17:50:18 +030013
Cameron Martincaef0ed2025-01-15 11:58:39 +010014const int64_2_pow_60: typeof Int64 = new Int64("1000000000000000");
15const int64_minus_2_pow_60: typeof Int64 = new Int64("f000000000000000");
Mustafa Senol Cosarf86845e2018-12-05 17:50:18 +030016
17// Work around for old API used by QUnitAdapter of jsTestDriver
Cameron Martincaef0ed2025-01-15 11:58:39 +010018if (typeof QUnit.log == "function") {
19 // When using real QUnit (fron PhantomJS) log failures to console
20 QUnit.log(function (details) {
21 if (!details.result) {
22 console.log("======== FAIL ========");
23 console.log("TestName: " + details.name);
24 if (details.message) console.log(details.message);
25 console.log("Expected: " + JSONInt64.stringify(details.expected));
26 console.log("Actual : " + JSONInt64.stringify(details.actual));
27 console.log("======================");
28 }
29 });
Mustafa Senol Cosarf86845e2018-12-05 17:50:18 +030030}
31
32// all Languages in UTF-8
Cameron Martincaef0ed2025-01-15 11:58:39 +010033const stringTest: string =
34 "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ú, 粵語";
Mustafa Senol Cosarf86845e2018-12-05 17:50:18 +030035
36function checkRecursively(assert, map1: Object, map2: Object): void {
Cameron Martincaef0ed2025-01-15 11:58:39 +010037 if (typeof map1 !== "function" && typeof map2 !== "function") {
38 if (!map1 || typeof map1 !== "object") {
39 assert.equal(map1, map2);
40 } else {
41 for (let key in map1) {
42 checkRecursively(assert, map1[key], map2[key]);
43 }
Mustafa Senol Cosarf86845e2018-12-05 17:50:18 +030044 }
Cameron Martincaef0ed2025-01-15 11:58:39 +010045 }
Mustafa Senol Cosarf86845e2018-12-05 17:50:18 +030046}
47
Cameron Martincaef0ed2025-01-15 11:58:39 +010048QUnit.module("Base Types");
Mustafa Senol Cosarf86845e2018-12-05 17:50:18 +030049
Cameron Martincaef0ed2025-01-15 11:58:39 +010050QUnit.test("Void", function (assert) {
51 assert.equal(client.testVoid(), undefined);
52});
53QUnit.test("Binary (String)", function (assert) {
54 let binary: string = "";
55 for (let v = 255; v >= 0; --v) {
56 binary += String.fromCharCode(v);
57 }
58 assert.equal(client.testBinary(binary), binary);
59});
60QUnit.test("Binary (Uint8Array)", function (assert) {
61 let binary: string = "";
62 for (let v = 255; v >= 0; --v) {
63 binary += String.fromCharCode(v);
64 }
65 const arr: Uint8Array = new Uint8Array(binary.length);
66 for (let i = 0; i < binary.length; ++i) {
67 arr[i] = binary[i].charCodeAt(0);
68 }
69 const hexEncodedString = Array.from(arr, function (byte) {
70 return String.fromCharCode(byte);
71 }).join("");
72 assert.equal(client.testBinary(hexEncodedString), binary);
73});
74QUnit.test("String", function (assert) {
75 assert.equal(client.testString(""), "");
76 assert.equal(client.testString(stringTest), stringTest);
Mustafa Senol Cosarf86845e2018-12-05 17:50:18 +030077
Cameron Martincaef0ed2025-01-15 11:58:39 +010078 const specialCharacters: string =
79 'quote: \" backslash:' +
80 " forwardslash-escaped: \/ " +
81 " backspace: \b formfeed: \f newline: \n return: \r tab: " +
82 ' now-all-of-them-together: "\\\/\b\n\r\t' +
83 " now-a-bunch-of-junk: !@#$%&()(&%$#{}{}<><><";
84 assert.equal(client.testString(specialCharacters), specialCharacters);
85});
86QUnit.test("Double", function (assert) {
87 assert.equal(client.testDouble(0), 0);
88 assert.equal(client.testDouble(-1), -1);
89 assert.equal(client.testDouble(3.14), 3.14);
90 assert.equal(client.testDouble(Math.pow(2, 60)), Math.pow(2, 60));
91});
92QUnit.test("Byte", function (assert) {
93 assert.equal(client.testByte(0), 0);
94 assert.equal(client.testByte(0x01), 0x01);
95});
96QUnit.test("I32", function (assert) {
97 assert.equal(client.testI32(0), 0);
98 assert.equal(client.testI32(Math.pow(2, 30)), Math.pow(2, 30));
99 assert.equal(client.testI32(-Math.pow(2, 30)), -Math.pow(2, 30));
100});
101QUnit.test("I64", function (assert) {
102 assert.equal(client.testI64(new Int64(0)), 0);
Mustafa Senol Cosarf86845e2018-12-05 17:50:18 +0300103
Cameron Martincaef0ed2025-01-15 11:58:39 +0100104 let int64_2_pow_60_result: typeof Int64 = client.testI64(int64_2_pow_60);
105 assert.ok(int64_2_pow_60.equals(int64_2_pow_60_result));
Mustafa Senol Cosarf86845e2018-12-05 17:50:18 +0300106
Cameron Martincaef0ed2025-01-15 11:58:39 +0100107 let int64_minus_2_pow_60_result: typeof Int64 =
108 client.testI64(int64_minus_2_pow_60);
109 assert.ok(int64_minus_2_pow_60.equals(int64_minus_2_pow_60_result));
110});
Mustafa Senol Cosarf86845e2018-12-05 17:50:18 +0300111
Cameron Martincaef0ed2025-01-15 11:58:39 +0100112QUnit.module("Structured Types");
Mustafa Senol Cosarf86845e2018-12-05 17:50:18 +0300113
Cameron Martincaef0ed2025-01-15 11:58:39 +0100114QUnit.test("Struct", function (assert) {
115 const structTestInput: ThriftTest.Xtruct = new ThriftTest.Xtruct();
116 structTestInput.string_thing = "worked";
117 structTestInput.byte_thing = 0x01;
118 structTestInput.i32_thing = Math.pow(2, 30);
119 structTestInput.i64_thing = int64_2_pow_60;
Mustafa Senol Cosarf86845e2018-12-05 17:50:18 +0300120
Cameron Martincaef0ed2025-01-15 11:58:39 +0100121 const structTestOutput: ThriftTest.Xtruct =
122 client.testStruct(structTestInput);
Mustafa Senol Cosarf86845e2018-12-05 17:50:18 +0300123
Cameron Martincaef0ed2025-01-15 11:58:39 +0100124 assert.equal(structTestOutput.string_thing, structTestInput.string_thing);
125 assert.equal(structTestOutput.byte_thing, structTestInput.byte_thing);
126 assert.equal(structTestOutput.i32_thing, structTestInput.i32_thing);
127 assert.ok(structTestOutput.i64_thing.equals(structTestInput.i64_thing));
128 assert.ok(structTestInput.i64_thing.equals(structTestOutput.i64_thing));
Mustafa Senol Cosarf86845e2018-12-05 17:50:18 +0300129
Cameron Martincaef0ed2025-01-15 11:58:39 +0100130 assert.equal(
131 JSONInt64.stringify(structTestOutput),
132 JSONInt64.stringify(structTestInput),
133 );
134});
Mustafa Senol Cosarf86845e2018-12-05 17:50:18 +0300135
Cameron Martincaef0ed2025-01-15 11:58:39 +0100136QUnit.test("Nest", function (assert) {
137 const xtrTestInput: ThriftTest.Xtruct = new ThriftTest.Xtruct();
138 xtrTestInput.string_thing = "worked";
139 xtrTestInput.byte_thing = 0x01;
140 xtrTestInput.i32_thing = Math.pow(2, 30);
141 xtrTestInput.i64_thing = int64_2_pow_60;
Mustafa Senol Cosarf86845e2018-12-05 17:50:18 +0300142
Cameron Martincaef0ed2025-01-15 11:58:39 +0100143 const nestTestInput: ThriftTest.Xtruct2 = new ThriftTest.Xtruct2();
144 nestTestInput.byte_thing = 0x02;
145 nestTestInput.struct_thing = xtrTestInput;
146 nestTestInput.i32_thing = Math.pow(2, 15);
Mustafa Senol Cosarf86845e2018-12-05 17:50:18 +0300147
Cameron Martincaef0ed2025-01-15 11:58:39 +0100148 const nestTestOutput: ThriftTest.Xtruct2 = client.testNest(nestTestInput);
Mustafa Senol Cosarf86845e2018-12-05 17:50:18 +0300149
Cameron Martincaef0ed2025-01-15 11:58:39 +0100150 assert.equal(nestTestOutput.byte_thing, nestTestInput.byte_thing);
151 assert.equal(
152 nestTestOutput.struct_thing.string_thing,
153 nestTestInput.struct_thing.string_thing,
154 );
155 assert.equal(
156 nestTestOutput.struct_thing.byte_thing,
157 nestTestInput.struct_thing.byte_thing,
158 );
159 assert.equal(
160 nestTestOutput.struct_thing.i32_thing,
161 nestTestInput.struct_thing.i32_thing,
162 );
163 assert.ok(
164 nestTestOutput.struct_thing.i64_thing.equals(
165 nestTestInput.struct_thing.i64_thing,
166 ),
167 );
168 assert.equal(nestTestOutput.i32_thing, nestTestInput.i32_thing);
Mustafa Senol Cosarf86845e2018-12-05 17:50:18 +0300169
Cameron Martincaef0ed2025-01-15 11:58:39 +0100170 assert.equal(
171 JSONInt64.stringify(nestTestOutput),
172 JSONInt64.stringify(nestTestInput),
173 );
174});
Mustafa Senol Cosarf86845e2018-12-05 17:50:18 +0300175
Cameron Martincaef0ed2025-01-15 11:58:39 +0100176QUnit.test("Map", function (assert) {
177 const mapTestInput: { [k: number]: number } = { 7: 77, 8: 88, 9: 99 };
Mustafa Senol Cosarf86845e2018-12-05 17:50:18 +0300178
Cameron Martincaef0ed2025-01-15 11:58:39 +0100179 const mapTestOutput: { [k: number]: number } = client.testMap(mapTestInput);
Mustafa Senol Cosarf86845e2018-12-05 17:50:18 +0300180
Cameron Martincaef0ed2025-01-15 11:58:39 +0100181 for (let key in mapTestOutput) {
182 assert.equal(mapTestOutput[key], mapTestInput[key]);
183 }
184});
Mustafa Senol Cosarf86845e2018-12-05 17:50:18 +0300185
Cameron Martincaef0ed2025-01-15 11:58:39 +0100186QUnit.test("StringMap", function (assert) {
187 const mapTestInput: { [k: string]: string } = {
188 a: "123",
189 "a b": "with spaces ",
190 same: "same",
191 "0": "numeric key",
192 longValue: stringTest,
193 stringTest: "long key",
194 };
Mustafa Senol Cosarf86845e2018-12-05 17:50:18 +0300195
Cameron Martincaef0ed2025-01-15 11:58:39 +0100196 const mapTestOutput: { [k: string]: string } =
197 client.testStringMap(mapTestInput);
Mustafa Senol Cosarf86845e2018-12-05 17:50:18 +0300198
Cameron Martincaef0ed2025-01-15 11:58:39 +0100199 for (let key in mapTestOutput) {
200 assert.equal(mapTestOutput[key], mapTestInput[key]);
201 }
202});
Mustafa Senol Cosarf86845e2018-12-05 17:50:18 +0300203
Cameron Martincaef0ed2025-01-15 11:58:39 +0100204QUnit.test("Set", function (assert) {
205 const setTestInput: number[] = [1, 2, 3];
206 assert.ok(client.testSet(setTestInput), setTestInput);
207});
Mustafa Senol Cosarf86845e2018-12-05 17:50:18 +0300208
Cameron Martincaef0ed2025-01-15 11:58:39 +0100209QUnit.test("List", function (assert) {
210 const listTestInput: number[] = [1, 2, 3];
211 assert.ok(client.testList(listTestInput), listTestInput);
212});
Mustafa Senol Cosarf86845e2018-12-05 17:50:18 +0300213
Cameron Martincaef0ed2025-01-15 11:58:39 +0100214QUnit.test("Enum", function (assert) {
215 assert.equal(client.testEnum(ThriftTest.Numberz.ONE), ThriftTest.Numberz.ONE);
216});
Mustafa Senol Cosarf86845e2018-12-05 17:50:18 +0300217
Cameron Martincaef0ed2025-01-15 11:58:39 +0100218QUnit.test("TypeDef", function (assert) {
219 assert.equal(client.testTypedef(new Int64(69)), 69);
220});
Mustafa Senol Cosarf86845e2018-12-05 17:50:18 +0300221
Cameron Martincaef0ed2025-01-15 11:58:39 +0100222QUnit.module("deeper!");
Mustafa Senol Cosarf86845e2018-12-05 17:50:18 +0300223
Cameron Martincaef0ed2025-01-15 11:58:39 +0100224QUnit.test("MapMap", function (assert) {
225 const mapMapTestExpectedResult: { [K: number]: { [k: number]: number } } = {
226 "4": { "1": 1, "2": 2, "3": 3, "4": 4 },
227 "-4": { "-4": -4, "-3": -3, "-2": -2, "-1": -1 },
228 };
229
230 const mapMapTestOutput = client.testMapMap(1);
231
232 for (let key in mapMapTestOutput) {
233 for (let key2 in mapMapTestOutput[key]) {
234 assert.equal(
235 mapMapTestOutput[key][key2],
236 mapMapTestExpectedResult[key][key2],
237 );
238 }
239 }
240
241 checkRecursively(assert, mapMapTestOutput, mapMapTestExpectedResult);
242});
243
244QUnit.module("Exception");
245
246QUnit.test("Xception", function (assert) {
247 assert.expect(2);
248 const done = assert.async();
249 try {
250 client.testException("Xception");
251 assert.ok(false);
252 } catch (e) {
253 assert.equal(e.errorCode, 1001);
254 assert.equal(e.message, "Xception");
255 done();
256 }
257});
258
259QUnit.test("no Exception", function (assert) {
260 assert.expect(1);
261 try {
262 client.testException("no Exception");
263 assert.ok(true);
264 } catch (e) {
265 assert.ok(false);
266 }
267});
268
269QUnit.test("TException", function (assert) {
270 //ThriftTest does not list TException as a legal exception so it will
271 // generate an exception on the server that does not propagate back to
272 // the client. This test has been modified to equate to "no exception"
273 assert.expect(1);
274 try {
275 client.testException("TException");
276 } catch (e) {
277 //assert.ok(false);
278 }
279 assert.ok(true);
280});
281
282QUnit.module("Insanity");
283
284const crazy: ThriftTest.Insanity = {
285 userMap: { "5": new Int64(5), "8": new Int64(8) },
286 xtructs: [
287 {
288 string_thing: "Goodbye4",
289 byte_thing: 4,
290 i32_thing: 4,
291 i64_thing: new Int64(4),
292 },
293 {
294 string_thing: "Hello2",
295 byte_thing: 2,
296 i32_thing: 2,
297 i64_thing: new Int64(2),
298 },
299 ],
300};
301QUnit.test("testInsanity", function (assert) {
302 const insanity: {
303 [k: number]: ThriftTest.Insanity | { [k: number]: ThriftTest.Insanity };
304 } = {
305 "1": {
306 "2": crazy,
307 "3": crazy,
308 },
309 "2": { "6": new ThriftTest.Insanity() },
310 };
311 const res = client.testInsanity(new ThriftTest.Insanity(crazy));
312 assert.ok(res, JSONInt64.stringify(res));
313 assert.ok(insanity, JSONInt64.stringify(insanity));
314
315 checkRecursively(assert, res, insanity);
316});
Mustafa Senol Cosarf86845e2018-12-05 17:50:18 +0300317
318//////////////////////////////////
319//Run same tests asynchronously
320
Cameron Martincaef0ed2025-01-15 11:58:39 +0100321QUnit.module("Async");
Mustafa Senol Cosarf86845e2018-12-05 17:50:18 +0300322
Cameron Martincaef0ed2025-01-15 11:58:39 +0100323QUnit.test("Double", function (assert) {
324 assert.expect(1);
Mustafa Senol Cosarf86845e2018-12-05 17:50:18 +0300325
Cameron Martincaef0ed2025-01-15 11:58:39 +0100326 const done = assert.async();
327 client.testDouble(3.14159265, function (result) {
328 assert.equal(result, 3.14159265);
329 done();
330 });
331});
Mustafa Senol Cosarf86845e2018-12-05 17:50:18 +0300332
Cameron Martincaef0ed2025-01-15 11:58:39 +0100333QUnit.test("Byte", function (assert) {
334 assert.expect(1);
Mustafa Senol Cosarf86845e2018-12-05 17:50:18 +0300335
Cameron Martincaef0ed2025-01-15 11:58:39 +0100336 const done = assert.async();
337 client.testByte(0x01, function (result) {
338 assert.equal(result, 0x01);
339 done();
340 });
341});
Mustafa Senol Cosarf86845e2018-12-05 17:50:18 +0300342
Cameron Martincaef0ed2025-01-15 11:58:39 +0100343QUnit.test("I32", function (assert) {
344 assert.expect(2);
Mustafa Senol Cosarf86845e2018-12-05 17:50:18 +0300345
Cameron Martincaef0ed2025-01-15 11:58:39 +0100346 const done = assert.async(2);
347 client.testI32(Math.pow(2, 30), function (result) {
348 assert.equal(result, Math.pow(2, 30));
349 done();
350 });
Mustafa Senol Cosarf86845e2018-12-05 17:50:18 +0300351
Cameron Martincaef0ed2025-01-15 11:58:39 +0100352 client.testI32(Math.pow(-2, 31), function (result) {
353 assert.equal(result, Math.pow(-2, 31));
354 done();
355 });
356});
Mustafa Senol Cosarf86845e2018-12-05 17:50:18 +0300357
Cameron Martincaef0ed2025-01-15 11:58:39 +0100358QUnit.test("I64", function (assert) {
359 assert.expect(2);
Mustafa Senol Cosarf86845e2018-12-05 17:50:18 +0300360
Cameron Martincaef0ed2025-01-15 11:58:39 +0100361 const done = assert.async(2);
362 client.testI64(int64_2_pow_60, function (result) {
363 assert.ok(int64_2_pow_60.equals(result));
364 done();
365 });
Mustafa Senol Cosarf86845e2018-12-05 17:50:18 +0300366
Cameron Martincaef0ed2025-01-15 11:58:39 +0100367 client.testI64(int64_minus_2_pow_60, function (result) {
368 assert.ok(int64_minus_2_pow_60.equals(result));
369 done();
370 });
371});