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