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