refactor
[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             expect(hits_all_targets).toBeGreaterThan(hits_single_target);
281         });
282     });
283 });
284
285 describe("Check active clients author", function () {
286     it("check for active clients after limited author search", function () {
287         waitsFor(function () {
288             var clients = $("div#mkwsStat span.clients");
289             //debug("clients: " + clients.text());
290             return clients.length == 1 && clients.text().match("/[1-9]+[0-9]+$");
291         }, "wait for Active clients: x/y", 5 * jasmine_config.second);
292
293         runs(function () {
294             var clients = $("div#mkwsStat span.clients");
295             debug("span.clients: " + clients.text());
296             expect(clients.text()).toMatch("/[1-9]+[0-9]+$");
297
298             // exact match of active clients (e.g. a SP misconfiguration)
299             if (jasmine_config.active_clients) {
300                 debug("check for " + jasmine_config.active_clients + " active connections");
301                 expect(clients.text()).toMatch(" [0-9]+/" + jasmine_config.active_clients + "$");
302             }
303         });
304     });
305 });
306
307 describe("Check Source Facets", function () {
308     it("limit search to first source", function () {
309         var hits_all_targets = get_hit_counter();
310         var source_number = 2; // 2=first source
311         // wait for a stat response
312         var waitcount = 0;
313         // do not click on wikipedia link - no author or subject facets possible
314         var link = "div.mkwsFacet[data-mkws-facet='xtargets'] div.term a";
315
316         // wait for a visible source link in facets
317         waitsFor(function () {
318             var terms = $(link);
319             return terms && terms.length > 0;
320         }, "wait for source facets after author search", 5 * jasmine_config.second);
321
322
323         runs(function () {
324             var terms = $(link);
325             for (var i = 0; i < terms.length; i++) {
326                 var term = $(terms[i]).text();
327                 debug("check for good source: " + term);
328
329                 if (term.match(/wikipedia/i)) {
330                     debug("ignore source facet: " + term);
331                     source_number++;
332                 } else {
333                     break;
334                 }
335             }
336             debug("Source counter: " + terms.length + ", select: " + (source_number - 1));
337
338             if ($("div.mkwsFacet[data-mkws-facet='xtargets'] div.term:nth-child(" + source_number + ") a").text().length == 0) {
339                 debug("No good source found. Not clicking on the bad ones");
340                 return;
341             }
342
343             debug("click on source link nth-child(): " + source_number);
344             $("div.mkwsFacet[data-mkws-facet='xtargets'] div.term:nth-child(" + source_number + ") a").trigger("click");
345
346             $(".mkwsPager").bind("DOMNodeInserted DOMNodeRemoved propertychange", function () {
347                 waitcount++;
348                 debug("DOM wait for stat: " + waitcount);
349             });
350         });
351
352         waitsFor(function () {
353             if ($("div.mkwsNavi").length && $("div.mkwsNavi").text().match(/(Source|datenquelle|kilder): /i)) {
354                 return true;
355             } else {
356                 return false;
357             }
358         }, "Search for source in navi bar", 4 * jasmine_config.second);
359
360         // Note: it may happens that limited source search returns the same number of hits
361         // as before. Thats not really an error, but unfortunate
362         waitsFor(function () {
363             var hits_single_target = get_hit_counter();
364
365             return waitcount >= 2 && hits_single_target > 0 && hits_single_target <= hits_all_targets ? true : false;
366         }, "Limited source search for less than " + hits_all_targets + " hits", 5 * jasmine_config.second);
367
368         runs(function () {
369             var hits_single_target = get_hit_counter();
370             debug("get less hits for sources: " + hits_all_targets + " > " + hits_single_target);
371             expect(hits_all_targets).not.toBeLessThan(hits_single_target);
372             jasmine_status.source_click = 1;
373
374             $(".mkwsPager").unbind("DOMNodeInserted DOMNodeRemoved propertychange");
375         });
376     });
377 });
378
379
380 describe("Check record list", function () {
381     it("check for single active client", function () {
382         if (!jasmine_status.source_click) {
383             debug("skip clients check due missing source click");
384             return;
385         }
386
387         waitsFor(function () {
388             var clients = $("div#mkwsStat span.clients");
389             //debug("clients: " + clients.text());
390             return clients.length == 1 && clients.text().match("/1$");
391         }, "wait for Active clients: x/1", 5 * jasmine_config.second);
392
393         runs(function () {
394             var clients = $("div#mkwsStat span.clients");
395             debug("span.clients: " + clients.text());
396             expect(clients.text()).toMatch("/1$");
397         });
398     });
399
400     it("got a record", function () {
401         var linkaddr = "div.mkwsRecords div.record:nth-child(1) a";
402
403         waitsFor(function () {
404             // remove + insert node: must be at least 2
405             return $(linkaddr).length > 0;
406         }, "wait until we see a new record", 2.5 * jasmine_config.second);
407
408         runs(function () {
409             expect($(linkaddr).length).toBeGreaterThan(0);
410         });
411     });
412 });
413
414 describe("Show record", function () {
415     var record_number = 1; // the Nth record in hit list
416     it("show record author", function () {
417         var click = $("div.mkwsRecords div.record:nth-child(" + record_number + ") a").trigger("click");
418         debug("show record click is success: " + click.length);
419         expect(click.length).toBe(1);
420
421         // wait until the record pops up
422         waitsFor(function () {
423             var show = $("div.mkwsRecords div.record:nth-child(" + record_number + ") > div.details");
424             //debug("poprecord: " + (show ? show.length : -1) + " " + $("div.mkwsRecords div.record").text());
425             return show != null && show.length ? true : false;
426         }, "wait some miliseconds to show up a record", 2 * jasmine_config.second);
427
428         runs(function () {
429             debug("show record pop up");
430             expect($("div.mkwsRecords div.record:nth-child(" + record_number + ") div")).not.toBe(null);
431         });
432     });
433
434     it("extract URL", function () {
435         if (jasmine_config.show_record_url == false) {
436             debug("ignore test for URL in record")
437             return;
438         }
439
440         var urls = $("div.mkwsRecords div.record:nth-child(" + record_number + ") div table tbody tr td a");
441         debug("number of extracted URL from record: " + urls.length);
442         // expect(urls.length).toBeGreaterThan(0); // LoC has records without links
443         for (var i = 0; i < urls.length; i++) {
444             var url = $(urls[i]);
445             debug("URL: " + url.attr('href') + " text: " + url.text());
446
447             expect(url.attr('href')).not.toBe(null);
448             expect(url.attr('href')).toMatch(/^https?:\/\/[a-z0-9\-]+\.[0-9a-z].*\//i);
449             expect(url.text()).not.toBe("");
450         }
451     });
452 });
453
454 describe("Check switch menu Records/Targets", function () {
455     it("check mkwsSwitch", function () {
456         expect($("div.mkwsSwitch").length).toBe(1);
457
458         // expect 2 clickable links
459         expect($("div.mkwsSwitch a").length).toBe(2);
460     });
461
462     it("switch to target view", function () {
463         $("div.mkwsSwitch").children('a').eq(1).trigger("click");
464
465         // now the target table must be visible
466         expect($("div.mkwsBytarget").is(":visible")).toBe(true);
467         expect($("div.mkwsRecords").is(":visible")).toBe(false);
468
469         // wait a half second, to show the target view
470         var time = (new Date).getTime();
471         waitsFor(function () {
472             return (new Date).getTime() - time > 700 ? true : false;
473         }, "wait some miliseconds", 1 * jasmine_config.second);
474
475         // look for table header
476         runs(function () {
477             expect($("div.mkwsBytarget").html()).toMatch(/Target ID/);
478         });
479     });
480
481     it("switch back to record view", function () {
482         $("div.mkwsSwitch").children('a').eq(0).trigger("click");
483
484         // now the target table must be visible
485         expect($("div.mkwsBytarget").is(":visible")).toBe(false);
486         expect($("div.mkwsRecords").is(":visible")).toBe(true);
487     });
488 });
489
490 describe("Check status client counter", function () {
491     function get_time() {
492         var date = new Date();
493         return date.getTime();
494     }
495     var time = get_time();
496
497     it("check status clients", function () {
498         if (!jasmine_status.source_click) {
499             debug("skip clients check due missing source click");
500             return;
501         }
502
503         waitsFor(function () {
504             var clients = $("div#mkwsStat span.clients");
505             debug("clients: " + clients.text());
506             if (clients.length == 1 && clients.text().match("0/1$")) {
507                 return true;
508             } else {
509                 return false;
510             }
511         }, "wait for Active clients: 0/1", 4 * jasmine_config.second);
512
513         runs(function () {
514             var clients = $("div#mkwsStat span.clients");
515             debug("span.clients: " + clients.text());
516             expect(clients.text()).toMatch("0/1$");
517         });
518     });
519 });
520
521 /* done */
522 describe("All tests are done", function () {
523     it(">>> hooray <<<", function () {
524         mkws.jasmine_done = true;
525     });
526 });