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