documentation: hits count in source may be less or equal
[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);
66             }
67             expect(hits).toBeGreaterThan(0);
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         expect($(".mkwsMOTD").length).toBe(1);
143         expect($(".mkwsMOTD").is(":hidden")).toBe(true);
144         debug("motd t=" + $(".mkwsMOTD").text());
145         debug("motd v=" + $(".mkwsMOTD").is(":visible"));
146     });
147 });
148
149
150 /*
151  * This part runs in background. It should be rewritten with
152  * async jasmine functions
153  *
154  */
155 describe("Check pazpar2 navigation", function () {
156     // Asynchronous part
157     it("check running search next/prev", function () {
158         expect($(".mkwsPager").length).toBe(1);
159
160         function my_click(id, time) {
161             setTimeout(function () {
162                 debug("trigger click on id: " + id);
163                 $(id).trigger("click");
164             }, time * jasmine_config.second);
165         }
166
167         waitsFor(function () {
168             return $("div.mkwsPager div:nth-child(2) a").length >= 2 ? true : false;
169         }, "Expect next link 2", 10 * jasmine_config.second);
170
171         runs(function () {
172             // click next/prev after N seconds
173             my_click(".mkwsNext", 0);
174         });
175
176         waitsFor(function () {
177             return $("div.mkwsPager div:nth-child(2) a").length >= 3 ? true : false;
178         }, "Expect next link 3", 5 * jasmine_config.second);
179
180         runs(function () {
181             // click next/prev after N seconds
182             my_click(".mkwsNext", 0);
183             my_click(".mkwsPrev", 0.2);
184         });
185     });
186 });
187
188 describe("Check pazpar2 hit counter", function () {
189     it("check running search hit counter", function () {
190         var max_time = jasmine_config.max_time; // in seconds
191         var expected_hits = jasmine_config.expected_hits; // at least expected hit counter
192         var hits = 0;
193
194         waitsFor(function () {
195             hits = get_hit_counter();
196             return hits > expected_hits;
197         }, "Expect " + expected_hits + " hits", max_time * jasmine_config.second);
198
199         runs(function () {
200             debug("mkws pager found records: '" + hits + "'");
201             expect($(".mkwsPager").length).toBe(1);
202             expect(hits).toBeGreaterThan(expected_hits);
203         });
204     });
205 });
206
207 describe("Check Termlist", function () {
208     it("found Termlist", function () {
209         var termlist = $("div.mkwsTermlists");
210         debug("Termlist success: " + termlist.length);
211         expect(termlist.length).toBe(1);
212
213         waitsFor(function () {
214             return $("div.mkwsFacet[data-mkws-facet='xtargets']").length == 1 ? true : false;
215         }, "check for facet sources", 4 * jasmine_config.second);
216
217         // everything displayed?
218         runs(function () {
219             var sources = $("div.mkwsFacet[data-mkws-facet='xtargets']");
220             debug("Termlist sources success: " + sources.length);
221             expect(sources.length).toBe(1);
222
223             var subjects = $("div.mkwsFacet[data-mkws-facet='subject']");
224             expect(subjects.length).toBe(1);
225
226             var authors = $("div.mkwsFacet[data-mkws-facet='author']");
227             expect(authors.length).toBe(1);
228         });
229
230         waitsFor(function () {
231             return $("div.mkwsFacet[data-mkws-facet='author'] div.term").length >= 2 ? true : false;
232         }, "At least one author link displayed", 4 * jasmine_config.second);
233
234         runs(function () {
235             expect($("div.mkwsFacet[data-mkws-facet='author'] div.term").length).toBeGreaterThan(1);
236         });
237     });
238 });
239
240 describe("Check Author Facets", function () {
241     it("limit search to first author", function () {
242         if (mkws.config.disable_facet_authors_search) {
243             debug("Facets: ignore limit search for authors");
244             return;
245         }
246
247         var hits_all_targets = get_hit_counter();
248         var author_number = 2; // 2=first author
249         // do not click on author with numbers, e.g.: "Bower, James M. Beeman, David, 1938-"
250         // do not click on author names without a comma, e.g.: "Joe Barbara"
251         // because searching on such authors won't find anything.
252         runs(function () {
253             var terms = $("div.mkwsFacet[data-mkws-facet='author'] div.term a");
254             for (var i = 0; i < terms.length; i++) {
255                 var term = $(terms[i]).text();
256                 if (term.match(/[0-9].+[0-9]/i) || !term.match(/,/)) {
257                     debug("ignore author facet: " + term);
258                     author_number++;
259                 } else {
260                     break;
261                 }
262             }
263             if ($("div.mkwsFacet[data-mkws-facet='author'] div.term:nth-child(" + author_number + ") a").text().length == 0) {
264                 debug("No good authors found. Not clicking on the bad ones");
265                 return;
266             }
267
268             debug("Clicking on author (" + author_number + ") " + $("div.mkwsFacet[data-mkws-facet='author'] div.term:nth-child(" + author_number + ") a").text());
269             $("div.mkwsFacet[data-mkws-facet='author'] div.term:nth-child(" + author_number + ") a").trigger("click");
270         });
271
272         waitsFor(function () {
273             var hits_single_target = get_hit_counter();
274             return hits_single_target > 0 && hits_single_target < hits_all_targets ? true : false;
275         }, "Limited author search for less than " + hits_all_targets + " hits", 4.5 * jasmine_config.second);
276
277         runs(function () {
278             var hits_single_target = get_hit_counter();
279             debug("get less hits for authors: " + hits_all_targets + " > " + hits_single_target);
280         });
281     });
282 });
283
284 describe("Check active clients author", function () {
285     it("check for active clients after limited author search", function () {
286         waitsFor(function () {
287             var clients = $("div.mkwsStat span.clients");
288             //debug("clients: " + clients.text());
289             return clients.length == 1 && clients.text().match("/[1-9]+[0-9]+$");
290         }, "wait for Active clients: x/y", 5 * jasmine_config.second);
291
292         runs(function () {
293             var clients = $("div.mkwsStat span.clients");
294             debug("span.clients: " + clients.text());
295             expect(clients.text()).toMatch("/[1-9]+[0-9]+$");
296
297             // exact match of active clients (e.g. a SP misconfiguration)
298             if (jasmine_config.active_clients) {
299                 debug("check for " + jasmine_config.active_clients + " active connections");
300                 expect(clients.text()).toMatch(" [0-9]+/" + jasmine_config.active_clients + "$");
301             }
302         });
303     });
304 });
305
306 describe("Check Source Facets", function () {
307     it("limit search to first source", function () {
308         var hits_all_targets = get_hit_counter();
309         var source_number = 2; // 2=first source
310         // wait for a stat response
311         var waitcount = 0;
312         // do not click on wikipedia link - no author or subject facets possible
313         var link = "div.mkwsFacet[data-mkws-facet='xtargets'] div.term a";
314
315         // wait for a visible source link in facets
316         waitsFor(function () {
317             var terms = $(link);
318             return terms && terms.length > 0;
319         }, "wait for source facets after author search", 5 * jasmine_config.second);
320
321
322         runs(function () {
323             var terms = $(link);
324             for (var i = 0; i < terms.length; i++) {
325                 var term = $(terms[i]).text();
326                 debug("check for good source: " + term);
327
328                 if (term.match(/wikipedia/i)) {
329                     debug("ignore source facet: " + term);
330                     source_number++;
331                 } else {
332                     break;
333                 }
334             }
335             debug("Source counter: " + terms.length + ", select: " + (source_number - 1));
336
337             if ($("div.mkwsFacet[data-mkws-facet='xtargets'] div.term:nth-child(" + source_number + ") a").text().length == 0) {
338                 debug("No good source found. Not clicking on the bad ones");
339                 return;
340             }
341
342             debug("click on source link nth-child(): " + source_number);
343             $("div.mkwsFacet[data-mkws-facet='xtargets'] div.term:nth-child(" + source_number + ") a").trigger("click");
344
345             $(".mkwsPager").bind("DOMNodeInserted DOMNodeRemoved propertychange", function () {
346                 waitcount++;
347                 debug("DOM wait for stat: " + waitcount);
348             });
349         });
350
351         waitsFor(function () {
352             if ($("div.mkwsNavi").length && $("div.mkwsNavi").text().match(/(Source|datenquelle|kilder): /i)) {
353                 return true;
354             } else {
355                 return false;
356             }
357         }, "Search for source in navi bar", 4 * jasmine_config.second);
358
359         // Note: it may happens that limited source search returns the same number of hits
360         // as before. Thats not really an error, but unfortunate
361         waitsFor(function () {
362             var hits_single_target = get_hit_counter();
363
364             return waitcount >= 2 && hits_single_target > 0 && hits_single_target <= hits_all_targets ? true : false;
365         }, "Limited source search for less than " + hits_all_targets + " hits", 5 * jasmine_config.second);
366
367         runs(function () {
368             var hits_single_target = get_hit_counter();
369             debug("get less hits for sources: " + hits_all_targets + " >= " + hits_single_target);
370             expect(hits_all_targets).not.toBeLessThan(hits_single_target);
371             jasmine_status.source_click = 1;
372
373             $(".mkwsPager").unbind("DOMNodeInserted DOMNodeRemoved propertychange");
374         });
375     });
376 });
377
378
379 describe("Check record list", function () {
380     it("check for single active client", function () {
381         if (!jasmine_status.source_click) {
382             debug("skip clients check due missing source click");
383             return;
384         }
385
386         waitsFor(function () {
387             var clients = $("div.mkwsStat span.clients");
388             //debug("clients: " + clients.text());
389             return clients.length == 1 && clients.text().match("/1$");
390         }, "wait for Active clients: x/1", 5 * jasmine_config.second);
391
392         runs(function () {
393             var clients = $("div.mkwsStat span.clients");
394             debug("span.clients: " + clients.text());
395             expect(clients.text()).toMatch("/1$");
396         });
397     });
398
399     it("got a record", function () {
400         var linkaddr = "div.mkwsRecords div.record:nth-child(1) a";
401
402         waitsFor(function () {
403             // remove + insert node: must be at least 2
404             return $(linkaddr).length > 0;
405         }, "wait until we see a new record", 2.5 * jasmine_config.second);
406
407         runs(function () {
408             expect($(linkaddr).length).toBeGreaterThan(0);
409         });
410     });
411 });
412
413 describe("Show record", function () {
414     var record_number = 1; // the Nth record in hit list
415     it("show record author", function () {
416         var click = $("div.mkwsRecords div.record:nth-child(" + record_number + ") a").trigger("click");
417         debug("show record click is success: " + click.length);
418         expect(click.length).toBe(1);
419
420         // wait until the record pops up
421         waitsFor(function () {
422             var show = $("div.mkwsRecords div.record:nth-child(" + record_number + ") > div.details");
423             //debug("poprecord: " + (show ? show.length : -1) + " " + $("div.mkwsRecords div.record").text());
424             return show != null && show.length ? true : false;
425         }, "wait some miliseconds to show up a record", 2 * jasmine_config.second);
426
427         runs(function () {
428             debug("show record pop up");
429             expect($("div.mkwsRecords div.record:nth-child(" + record_number + ") div")).not.toBe(null);
430         });
431     });
432
433     it("extract URL", function () {
434         if (jasmine_config.show_record_url == false) {
435             debug("ignore test for URL in record")
436             return;
437         }
438
439         var urls = $("div.mkwsRecords div.record:nth-child(" + record_number + ") div table tbody tr td a");
440         debug("number of extracted URL from record: " + urls.length);
441         // expect(urls.length).toBeGreaterThan(0); // LoC has records without links
442         for (var i = 0; i < urls.length; i++) {
443             var url = $(urls[i]);
444             debug("URL: " + url.attr('href') + " text: " + url.text());
445
446             expect(url.attr('href')).not.toBe(null);
447             expect(url.attr('href')).toMatch(/^https?:\/\/[a-z0-9\-]+\.[0-9a-z].*\//i);
448             expect(url.text()).not.toBe("");
449         }
450     });
451 });
452
453 describe("Check switch menu Records/Targets", function () {
454     it("check mkwsSwitch", function () {
455         expect($("div.mkwsSwitch").length).toBe(1);
456
457         // expect 2 clickable links
458         expect($("div.mkwsSwitch a").length).toBe(2);
459     });
460
461     it("switch to target view", function () {
462         $("div.mkwsSwitch").children('a').eq(1).trigger("click");
463
464         // now the target table must be visible
465         expect($("div.mkwsBytarget").is(":visible")).toBe(true);
466         expect($("div.mkwsRecords").is(":visible")).toBe(false);
467
468         // wait a half second, to show the target view
469         var time = (new Date).getTime();
470         waitsFor(function () {
471             return (new Date).getTime() - time > 700 ? true : false;
472         }, "wait some miliseconds", 1 * jasmine_config.second);
473
474         // look for table header
475         runs(function () {
476             expect($("div.mkwsBytarget").html()).toMatch(/Target ID/);
477         });
478     });
479
480     it("switch back to record view", function () {
481         $("div.mkwsSwitch").children('a').eq(0).trigger("click");
482
483         // now the target table must be visible
484         expect($("div.mkwsBytarget").is(":visible")).toBe(false);
485         expect($("div.mkwsRecords").is(":visible")).toBe(true);
486     });
487 });
488
489 describe("Check status client counter", function () {
490     function get_time() {
491         var date = new Date();
492         return date.getTime();
493     }
494     var time = get_time();
495
496     it("check status clients", function () {
497         if (!jasmine_status.source_click) {
498             debug("skip clients check due missing source click");
499             return;
500         }
501
502         waitsFor(function () {
503             var clients = $("div.mkwsStat span.clients");
504             debug("clients: " + clients.text());
505             if (clients.length == 1 && clients.text().match("0/1$")) {
506                 return true;
507             } else {
508                 return false;
509             }
510         }, "wait for Active clients: 0/1", 4 * jasmine_config.second);
511
512         runs(function () {
513             var clients = $("div.mkwsStat span.clients");
514             debug("span.clients: " + clients.text());
515             expect(clients.text()).toMatch("0/1$");
516         });
517     });
518 });
519
520 /* done */
521 describe("All tests are done", function () {
522     it(">>> hooray <<<", function () {
523         mkws.jasmine_done = true;
524     });
525 });