Look for mkwsClientCount class instead of "clients".
[mkws-moved-to-github.git] / test / spec / mkws-pazpar2.js
1 /* Copyright (c) 2013-2014 IndexData ApS. http://indexdata.com
2  *
3  * perform papzpar2 / pz2.js search & retrieve request in the browser
4  *
5  */
6
7 // get references from mkws.js, lazy evaluation
8 var debug = function (text) {
9         mkws.log("Jasmine: " + text)
10     }
11
12     // Define empty jasmine_config for simple applications that don't define it.
13 if (jasmine_config == null || typeof jasmine_config != 'object') {
14     var jasmine_config = {};
15 }
16
17 var jasmine_status = {
18     source_click: 0
19 };
20
21 /* check config for jasmine test
22  *
23  * you can override the default values in the config
24  * object: jasmine_config = {};
25  *
26  */
27 function init_jasmine_config() {
28
29     var jasmine_config_default = {
30         search_query: "freebsd",
31         max_time: 17,
32         // in seconds
33         expected_hits: 80,
34         // at least expected hit counter
35         second: 1000,
36         // miliseconds to seconds
37         show_record_url: true,
38         // check for valid URL in records
39         check_motd: true,
40         dummy: false
41     };
42
43     // use default values for undefined values
44     for (var key in jasmine_config_default) {
45         if (!jasmine_config.hasOwnProperty(key)) {
46             jasmine_config[key] = jasmine_config_default[key];
47         }
48         debug("jasmine config: " + key + " => " + jasmine_config[key]);
49     }
50
51     mkws.jasmine_done = false;
52 }
53
54 var get_hit_counter = function () {
55         // not yet here
56         if ($(".mkwsPager").length == 0) return -1;
57
58         var found = $(".mkwsPager").text();
59         var re = /\([A-Za-z]+:\s+([0-9]+)\)/;
60         re.exec(found);
61         var hits = -1;
62
63         if (RegExp.$1) {
64             hits = parseInt(RegExp.$1);
65             if (hits <= 0) {
66                 debug("Oooops in get_hit_counter: " + RegExp.$1 + " '" + found + "'");
67             }
68         }
69
70         //debug("Hits: " + hits);
71         return hits;
72     }
73
74 describe("Init jasmine config", function () {
75     it("jasmine was successfully initialized", function () {
76         init_jasmine_config();
77
78         expect(jasmine_config.search_query).toMatch(/\w/);
79         expect(jasmine_config.second).toBeGreaterThan(100);
80         expect(jasmine_config.max_time).toBeGreaterThan(1);
81         expect(jasmine_config.expected_hits).toBeGreaterThan(1);
82     });
83 });
84
85 //disabled
86 xdescribe("Check MOTD before search", function () {
87     // Check that the MOTD has been moved into its container, and
88     // is visible before the search.
89     // the mkwsMOTD div was originally inside a testMOTD div, which should
90     // now be empty
91     // Note that the testMOTD is a regular div, and uses #testMOTD,
92     // since the automagic class-making does not apply to it.
93     it("MOTD is hidden", function () {
94         expect($(".mkwsMOTD").length).toBe(1);
95         expect($("#testMOTD").length).toBe(1);
96         expect($("#testMOTD").text()).toMatch("^ *$");
97     });
98
99     it("mkwsMOTDContainer has received the text", function () {
100         expect($(".mkwsMOTDContainer").length).toBe(1);
101         expect($(".mkwsMOTDContainer").text()).toMatch(/MOTD/);
102     });
103 });
104
105 describe("Check pazpar2 search", function () {
106     it("pazpar2 was successfully initialized", function () {
107         expect(mkws.config.error).toBe(undefined);
108     });
109
110     it("validate HTML id's", function () {
111         expect($("input.mkwsQuery").length).toBe(1);
112         expect($("input.mkwsButton").length).toBe(1);
113
114         expect($(".mkwsNext").length).not.toBe(1);
115         expect($(".mkwsPrev").length).not.toBe(1);
116     });
117
118     it("run search query", function () {
119         var search_query = jasmine_config.search_query; // short hit counter with some paging
120         $("input.mkwsQuery").val(search_query);
121         debug("set search query: " + search_query)
122         expect($("input.mkwsQuery").val()).toMatch("^" + search_query + "$");
123
124         if (mkws.config.use_service_proxy) {
125             // wait for service proxy auth
126             waitsFor(function () {
127                 return mkws.authenticated;
128             }, "SP auth done", 10 * jasmine_config.second);
129         } else {
130             debug("running raw pp2, don't wait for mkws auth");
131         }
132
133         runs(function () {
134             debug("Click on submit button");
135             $("input.mkwsButton").trigger("click");
136         })
137     });
138 });
139
140 describe("Check MOTD after search", function () {
141     it("MOTD is hidden", function () {
142         if (!jasmine_config.check_motd) {
143             return;
144         }
145
146         expect($(".mkwsMOTD").length).toBe(1);
147         expect($(".mkwsMOTD").is(":hidden")).toBe(true);
148         debug("motd t=" + $(".mkwsMOTD").text());
149         debug("motd v=" + $(".mkwsMOTD").is(":visible"));
150     });
151 });
152
153
154 /*
155  * This part runs in background. It should be rewritten with
156  * async jasmine functions
157  *
158  */
159 describe("Check pazpar2 navigation", function () {
160     // Asynchronous part
161     it("check running search next/prev", function () {
162         expect($(".mkwsPager").length).toBe(1);
163
164         function my_click(id, time) {
165             setTimeout(function () {
166                 debug("trigger click on id: " + id);
167                 $(id).trigger("click");
168             }, time * jasmine_config.second);
169         }
170
171         waitsFor(function () {
172             return $("div.mkwsPager div:nth-child(2) a").length >= 2 ? true : false;
173         }, "Expect next link 2", 10 * jasmine_config.second);
174
175         runs(function () {
176             // click next/prev after N seconds
177             my_click(".mkwsNext", 0);
178         });
179
180         waitsFor(function () {
181             return $("div.mkwsPager div:nth-child(2) a").length >= 3 ? true : false;
182         }, "Expect next link 3", 5 * jasmine_config.second);
183
184         runs(function () {
185             // click next/prev after N seconds
186             my_click(".mkwsNext", 0);
187             my_click(".mkwsPrev", 0.2);
188         });
189     });
190 });
191
192 describe("Check pazpar2 hit counter", function () {
193     it("check running search hit counter", function () {
194         var max_time = jasmine_config.max_time; // in seconds
195         var expected_hits = jasmine_config.expected_hits; // at least expected hit counter
196         var hits = 0;
197
198         waitsFor(function () {
199             hits = get_hit_counter();
200             return hits > expected_hits;
201         }, "Expect " + expected_hits + " hits", max_time * jasmine_config.second);
202
203         runs(function () {
204             debug("mkws pager found records: '" + hits + "'");
205             expect($(".mkwsPager").length).toBe(1);
206             expect(hits).toBeGreaterThan(expected_hits);
207         });
208     });
209 });
210
211 describe("Check Termlist", function () {
212     it("found Termlist", function () {
213         var termlist = $("div.mkwsTermlists");
214         debug("Termlist success: " + termlist.length);
215         expect(termlist.length).toBe(1);
216
217         waitsFor(function () {
218             return $("div.mkwsFacet[data-mkws-facet='xtargets']").length == 1 ? true : false;
219         }, "check for facet sources", 4 * jasmine_config.second);
220
221         // everything displayed?
222         runs(function () {
223             var sources = $("div.mkwsFacet[data-mkws-facet='xtargets']");
224             debug("Termlist sources success: " + sources.length);
225             expect(sources.length).toBe(1);
226
227             var subjects = $("div.mkwsFacet[data-mkws-facet='subject']");
228             expect(subjects.length).toBe(1);
229
230             var authors = $("div.mkwsFacet[data-mkws-facet='author']");
231             expect(authors.length).toBe(1);
232         });
233
234         waitsFor(function () {
235             return $("div.mkwsFacet[data-mkws-facet='author'] div.term").length >= 2 ? true : false;
236         }, "At least one author link displayed", 4 * jasmine_config.second);
237
238         runs(function () {
239             expect($("div.mkwsFacet[data-mkws-facet='author'] div.term").length).toBeGreaterThan(1);
240         });
241     });
242 });
243
244 describe("Check Author Facets", function () {
245     it("limit search to first author", function () {
246         if (mkws.config.disable_facet_authors_search) {
247             debug("Facets: ignore limit search for authors");
248             return;
249         }
250
251         var hits_all_targets = get_hit_counter();
252         var author_number = 2; // 2=first author
253         // do not click on author with numbers, e.g.: "Bower, James M. Beeman, David, 1938-"
254         // do not click on author names without a comma, e.g.: "Joe Barbara"
255         // because searching on such authors won't find anything.
256         runs(function () {
257             var terms = $("div.mkwsFacet[data-mkws-facet='author'] div.term a");
258             for (var i = 0; i < terms.length; i++) {
259                 var term = $(terms[i]).text();
260                 if (term.match(/[0-9].+[0-9]/i) || !term.match(/,/)) {
261                     debug("ignore author facet: " + term);
262                     author_number++;
263                 } else {
264                     break;
265                 }
266             }
267             if ($("div.mkwsFacet[data-mkws-facet='author'] div.term:nth-child(" + author_number + ") a").text().length == 0) {
268                 debug("No good authors found. Not clicking on the bad ones");
269                 return;
270             }
271
272             debug("Clicking on author (" + author_number + ") " + $("div.mkwsFacet[data-mkws-facet='author'] div.term:nth-child(" + author_number + ") a").text());
273             $("div.mkwsFacet[data-mkws-facet='author'] div.term:nth-child(" + author_number + ") a").trigger("click");
274         });
275
276         waitsFor(function () {
277             var hits_single_target = get_hit_counter();
278             return hits_single_target > 0 && hits_single_target < hits_all_targets ? true : false;
279         }, "Limited author search for less than " + hits_all_targets + " hits", 4.5 * jasmine_config.second);
280
281         runs(function () {
282             var hits_single_target = get_hit_counter();
283             debug("get less hits for authors: " + hits_all_targets + " > " + hits_single_target);
284         });
285     });
286 });
287
288 describe("Check active clients author", function () {
289     it("check for active clients after limited author search", function () {
290         waitsFor(function () {
291             var clients = $("div.mkwsStat span.mkwsClientCount");
292             // debug("clients: " + clients.text());
293             return clients.length == 1 && clients.text().match("/[1-9]+[0-9]*$");
294         }, "wait for Active clients: x/y", 5.5 * jasmine_config.second);
295
296         runs(function () {
297             var clients = $("div.mkwsStat span.mkwsClientCount");
298             debug("span.mkwsClientCount: " + clients.text());
299             expect(clients.text()).toMatch("/[1-9]+[0-9]*$");
300
301             // exact match of active clients (e.g. a SP misconfiguration)
302             if (jasmine_config.active_clients) {
303                 debug("check for " + jasmine_config.active_clients + " active connections");
304                 expect(clients.text()).toMatch(" [0-9]+/" + jasmine_config.active_clients + "$");
305             }
306         });
307     });
308 });
309
310 describe("Check Source Facets", function () {
311     it("limit search to first source", function () {
312         var hits_all_targets = get_hit_counter();
313         var source_number = 2; // 2=first source
314         // wait for a stat response
315         var waitcount = 0;
316         // do not click on wikipedia link - no author or subject facets possible
317         var link = "div.mkwsFacet[data-mkws-facet='xtargets'] div.term a";
318
319         // wait for a visible source link in facets
320         waitsFor(function () {
321             var terms = $(link);
322             return terms && terms.length > 0;
323         }, "wait for source facets after author search", 5 * jasmine_config.second);
324
325
326         runs(function () {
327             var terms = $(link);
328             for (var i = 0; i < terms.length; i++) {
329                 var term = $(terms[i]).text();
330                 debug("check for good source: " + term);
331
332                 if (term.match(/wikipedia/i)) {
333                     debug("ignore source facet: " + term);
334                     source_number++;
335                 } else {
336                     break;
337                 }
338             }
339             debug("Source counter: " + terms.length + ", select: " + (source_number - 1));
340
341             if ($("div.mkwsFacet[data-mkws-facet='xtargets'] div.term:nth-child(" + source_number + ") a").text().length == 0) {
342                 debug("No good source found. Not clicking on the bad ones");
343                 return;
344             }
345
346             debug("click on source link nth-child(): " + source_number);
347             $("div.mkwsFacet[data-mkws-facet='xtargets'] div.term:nth-child(" + source_number + ") a").trigger("click");
348
349             $(".mkwsPager").bind("DOMNodeInserted DOMNodeRemoved propertychange", function () {
350                 waitcount++;
351                 debug("DOM wait for stat: " + waitcount);
352             });
353         });
354
355         waitsFor(function () {
356             if ($("div.mkwsNavi").length && $("div.mkwsNavi").text().match(/(Source|datenquelle|kilder): /i)) {
357                 return true;
358             } else {
359                 return false;
360             }
361         }, "Search for source in navi bar", 4 * jasmine_config.second);
362
363         // Note: it may happens that limited source search returns the same number of hits
364         // as before. Thats not really an error, but unfortunate
365         waitsFor(function () {
366             var hits_single_target = get_hit_counter();
367
368             return waitcount >= 2 && hits_single_target > 0 && hits_single_target <= hits_all_targets ? true : false;
369         }, "Limited source search for less than " + hits_all_targets + " hits", 5 * jasmine_config.second);
370
371         runs(function () {
372             var hits_single_target = get_hit_counter();
373             debug("get less hits for sources: " + hits_all_targets + " >= " + hits_single_target);
374             expect(hits_all_targets).not.toBeLessThan(hits_single_target);
375             jasmine_status.source_click = 1;
376
377             $(".mkwsPager").unbind("DOMNodeInserted DOMNodeRemoved propertychange");
378         });
379     });
380 });
381
382
383 describe("Check record list", function () {
384     it("check for single active client", function () {
385         if (!jasmine_status.source_click) {
386             debug("skip clients check due missing source click");
387             return;
388         }
389
390         waitsFor(function () {
391             var clients = $("div.mkwsStat span.mkwsClientCount");
392             //debug("clients: " + clients.text());
393             return clients.length == 1 && clients.text().match("/1$");
394         }, "wait for Active clients: x/1", 5 * jasmine_config.second);
395
396         runs(function () {
397             var clients = $("div.mkwsStat span.mkwsClientCount");
398             debug("span.mkwsClientCount: " + clients.text());
399             expect(clients.text()).toMatch("/1$");
400         });
401     });
402
403     it("got a record", function () {
404         var linkaddr = "div.mkwsRecords div.record:nth-child(1) a";
405
406         waitsFor(function () {
407             // remove + insert node: must be at least 2
408             return $(linkaddr).length > 0;
409         }, "wait until we see a new record", 2.5 * jasmine_config.second);
410
411         runs(function () {
412             expect($(linkaddr).length).toBeGreaterThan(0);
413         });
414     });
415 });
416
417 describe("Show record", function () {
418     var record_number = 1; // the Nth record in hit list
419     it("show record author", function () {
420         var click = $("div.mkwsRecords div.record:nth-child(" + record_number + ") a").trigger("click");
421         debug("show record click is success: " + click.length);
422         expect(click.length).toBe(1);
423
424         // wait until the record pops up
425         waitsFor(function () {
426             var show = $("div.mkwsRecords div.record:nth-child(" + record_number + ") > div.mkwsDetails");
427             //debug("poprecord: " + (show ? show.length : -1) + " " + $("div.mkwsRecords div.record").text());
428             return show != null && show.length ? true : false;
429         }, "wait some miliseconds to show up a record", 2 * jasmine_config.second);
430
431         runs(function () {
432             debug("show record pop up");
433             expect($("div.mkwsRecords div.record:nth-child(" + record_number + ") div")).not.toBe(null);
434         });
435     });
436
437     it("extract URL", function () {
438         if (jasmine_config.show_record_url == false) {
439             debug("ignore test for URL in record")
440             return;
441         }
442
443         var urls = $("div.mkwsRecords div.record:nth-child(" + record_number + ") div table tbody tr td a");
444         debug("number of extracted URL from record: " + urls.length);
445         // expect(urls.length).toBeGreaterThan(0); // LoC has records without links
446         for (var i = 0; i < urls.length; i++) {
447             var url = $(urls[i]);
448             debug("URL: " + url.attr('href') + " text: " + url.text());
449
450             expect(url.attr('href')).not.toBe(null);
451             expect(url.attr('href')).toMatch(/^https?:\/\/[a-z0-9\-]+\.[0-9a-z].*\//i);
452             expect(url.text()).not.toBe("");
453         }
454     });
455 });
456
457 describe("Check switch menu Records/Targets", function () {
458     it("check mkwsSwitch", function () {
459         expect($("div.mkwsSwitch").length).toBe(1);
460
461         // expect 2 clickable links
462         expect($("div.mkwsSwitch a").length).toBe(2);
463     });
464
465     it("switch to target view", function () {
466         $("div.mkwsSwitch").children('a').eq(1).trigger("click");
467
468         // now the target table must be visible
469         expect($("div.mkwsTargets").is(":visible")).toBe(true);
470         expect($("div.mkwsRecords").is(":visible")).toBe(false);
471
472         // wait a half second, to show the target view
473         var time = (new Date).getTime();
474         waitsFor(function () {
475             return (new Date).getTime() - time > 700 ? true : false;
476         }, "wait some miliseconds", 1 * jasmine_config.second);
477
478         // look for table header
479         runs(function () {
480             expect($("div.mkwsTargets").html()).toMatch(/Target ID/);
481         });
482     });
483
484     it("switch back to record view", function () {
485         $("div.mkwsSwitch").children('a').eq(0).trigger("click");
486
487         // now the target table must be visible
488         expect($("div.mkwsTargets").is(":visible")).toBe(false);
489         expect($("div.mkwsRecords").is(":visible")).toBe(true);
490     });
491 });
492
493 describe("Check status client counter", function () {
494     function get_time() {
495         var date = new Date();
496         return date.getTime();
497     }
498     var time = get_time();
499
500     it("check status clients", function () {
501         if (!jasmine_status.source_click) {
502             debug("skip clients check due missing source click");
503             return;
504         }
505
506         waitsFor(function () {
507             var clients = $("div.mkwsStat span.mkwsClientCount");
508             debug("clients: " + clients.text());
509             if (clients.length == 1 && clients.text().match("0/1$")) {
510                 return true;
511             } else {
512                 return false;
513             }
514         }, "wait for Active clients: 0/1", 4 * jasmine_config.second);
515
516         runs(function () {
517             var clients = $("div.mkwsStat span.mkwsClientCount");
518             debug("span.mkwsClientCount: " + clients.text());
519             expect(clients.text()).toMatch("0/1$");
520         });
521     });
522 });
523
524 /* done */
525 describe("All tests are done", function () {
526     it(">>> hooray <<<", function () {
527         mkws.jasmine_done = true;
528     });
529 });