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