bring debug() function back
[mkws-moved-to-github.git] / test / spec / mkws-pazpar2.js
1 /* Copyright (c) 2013 IndexData ApS. http://indexdata.com
2  *
3  * perform papzpar2 / pz2.js search & retrieve request in the browser
4  *
5  */
6
7 var debug_level = mkws.debug_level;
8 var debug = mkws.debug_function;
9
10 var get_hit_counter = function () {
11         // not yet here
12         if ($("#mkwsPager").length == 0) return -1;
13
14         var found = $("#mkwsPager").text();
15         var re = /\([A-Za-z]+:\s+([0-9]+)\)/;
16         re.exec(found);
17         var hits = -1;
18
19         if (RegExp.$1) {
20             hits = parseInt(RegExp.$1);
21             expect(hits).toBeGreaterThan(0);
22         }
23
24         //debug("Hits: " + hits);
25         return hits;
26     }
27
28 describe("Check pazpar2 search", function () {
29     it("pazpar2 was successfully initialize", function () {
30         expect(mkws_config.error).toBe(undefined);
31     });
32
33     it("validate HTML id's", function () {
34         expect($("input#mkwsQuery").length).toBe(1);
35         expect($("input#mkwsButton").length).toBe(1);
36
37         expect($("#mkwsNext").length).not.toBe(1);
38         expect($("#mkwsPrev").length).not.toBe(1);
39     });
40
41     it("run search query", function () {
42         var search_query = "freebsd"; // short hit counter with some paging
43         $("input#mkwsQuery").val(search_query);
44         debug("set search query: " + search_query)
45         expect($("input#mkwsQuery").val()).toMatch("^" + search_query + "$");
46
47         // wait for service proxy auth
48         waitsFor(function () {
49             return mkws.service_proxy_auth;
50         }, "SP auth done", 10 * 1000);
51
52         runs(function () {
53             debug("Click on submit button");
54             var click = $("input#mkwsButton").trigger("click");
55             expect(click.length).toBe(1);
56         })
57     });
58 });
59
60
61 /*
62  * This part runs in background. It should be rewritten with
63  * async jasmine functions
64  *
65  */
66 describe("Check pazpar2 navigation", function () {
67     // Asynchronous part
68     it("check running search next/prev", function () {
69         expect($("#mkwsPager").length).toBe(1);
70
71         function my_click(id, time) {
72             setTimeout(function () {
73                 debug("trigger click on id: " + id);
74                 var click = $(id).trigger("click");
75
76                 debug("next/prev: " + id + " click is success: " + click.length);
77                 expect(click.length).toBe(1);
78             }, time * 1000);
79         }
80
81         waitsFor(function () {
82             return $("div#mkwsPager div:nth-child(2) a").length >= 2 ? true : false;
83         }, "Expect next link 2", 10 * 1000);
84
85         runs(function () {
86             // click next/prev after N seconds
87             my_click("#mkwsNext", 0);
88         });
89
90         waitsFor(function () {
91             return $("div#mkwsPager div:nth-child(2) a").length >= 3 ? true : false;
92         }, "Expect next link 3", 5 * 1000);
93
94         runs(function () {
95             // click next/prev after N seconds
96             my_click("#mkwsNext", 0);
97             my_click("#mkwsPrev", 0.2);
98         });
99     });
100 });
101
102 describe("Check pazpar2 hit counter", function () {
103     it("check running search hit counter", function () {
104         var max_time = 16; // in seconds
105         var expected_hits = 80; // at least expected hit counter
106         var hits = 0;
107
108         waitsFor(function () {
109             hits = get_hit_counter();
110
111             return hits > expected_hits;
112         }, "Expect " + expected_hits + " hits", max_time * 1000);
113
114
115         runs(function () {
116             debug("mkws pager found records: '" + hits + "'");
117             expect($("#mkwsPager").length).toBe(1);
118             expect(hits).toBeGreaterThan(expected_hits);
119         });
120     });
121 });
122
123 describe("Check Termlist", function () {
124     it("found Termlist", function () {
125         var termlist = $("div#mkwsTermlists");
126         debug("Termlist success: " + termlist.length);
127         expect(termlist.length).toBe(1);
128
129         waitsFor(function () {
130             return $("div#mkwsFacetSources").length == 1 ? true : false;
131         }, "check for facet sources", 2 * 1000);
132
133
134         // everything displayed?
135         runs(function () {
136             var sources = $("div#mkwsFacetSources");
137             debug("Termlist sources success: " + sources.length);
138             expect(sources.length).toBe(1);
139
140             var subjects = $("div#mkwsFacetSubjects");
141             expect(subjects.length).toBe(1);
142
143             var authors = $("div#mkwsFacetAuthors");
144             expect(authors.length).toBe(1);
145         });
146
147         waitsFor(function () {
148             return $("div#mkwsFacetAuthors div.term").length >= 2 ? true : false;
149         }, "At least one author link displayed", 2 * 1000);
150
151         runs(function () {
152             expect($("div#mkwsFacetAuthors div.term").length).toBeGreaterThan(1);
153         });
154     });
155
156     it("limit search to first author", function () {
157         var hits_all_targets = get_hit_counter();
158
159         var click = $("div#mkwsFacetAuthors div.term:nth-child(2) a").trigger("click");
160         debug("limit author click is success: " + click.length);
161         expect(click.length).toBe(1);
162
163         waitsFor(function () {
164             return get_hit_counter() < hits_all_targets ? true : false;
165         }, "Limited author search for less than " + hits_all_targets + " hits", 8 * 1000);
166
167         runs(function () {
168             var hits_single_target = get_hit_counter();
169             debug("get less hits for authors: " + hits_all_targets + " > " + hits_single_target);
170             expect(hits_all_targets).toBeGreaterThan(hits_single_target);
171         });
172     });
173
174     it("limit search to first source", function () {
175         var hits_all_targets = get_hit_counter();
176         var source_number = 2; // 2=first source
177         var source_name = $("div#mkwsFacetSources div.term:nth-child(" + source_number + ") a").text();
178         // do not click on wikipedia link - no author or subject facets possible
179         if (source_name.match(/wikipedia/i)) {
180             source_number++;
181         }
182
183         var click = $("div#mkwsFacetSources div.term:nth-child(" + source_number + ") a").trigger("click");
184         debug("limit source click " + (source_number - 1) + " is success: " + click.length);
185         expect(click.length).toBe(1);
186
187         waitsFor(function () {
188             if ($("div#mkwsNavi").length && $("div#mkwsNavi").text().match(/^Source/)) {
189                 return true;
190             } else {
191                 return false;
192             }
193         }, "Search for source in navi bar", 1000);
194
195         waitsFor(function () {
196             return get_hit_counter() < hits_all_targets ? true : false;
197         }, "Limited source earch for less than " + hits_all_targets + " hits", 9 * 1000);
198
199         runs(function () {
200             var hits_single_target = get_hit_counter();
201             debug("get less hits for sources: " + hits_all_targets + " > " + hits_single_target);
202             expect(hits_all_targets).toBeGreaterThan(hits_single_target);
203         });
204     });
205 });
206
207 describe("Show record", function () {
208     var record_number = 1; // the Nth record in hit list
209     it("show record author", function () {
210         var click = $("div#mkwsRecords div.record:nth-child(" + record_number + ") :nth-child(2)").trigger("click");
211         debug("show click is success: " + click.length);
212         expect(click.length).toBe(1);
213
214         // wait until the record pops up
215         waitsFor(function () {
216             var show = $("div#mkwsRecords div.record:nth-child(" + record_number + ") div");
217             return show != null && show.length ? true : false;
218         }, "wait some miliseconds to show up a record", 2 * 1000);
219
220         runs(function () {
221             debug("show record pop up");
222             expect($("div#mkwsRecords div.record:nth-child(" + record_number + ") div")).not.toBe(null);
223         });
224     });
225
226     it("extract URL", function () {
227         var url = $("div#mkwsRecords div.record:nth-child(" + record_number + ") div table tbody tr td a").text();
228         debug("extracted URL from record: " + url);
229
230         expect(url).not.toBe(null);
231         expect(url).toMatch(/^http:\/\/[a-z0-9]+\.[0-9a-z].*\//i);
232     });
233 });
234
235 describe("Check switch menu Records/Targets", function () {
236     it("check mkwsSwitch", function () {
237         expect($("div#mkwsSwitch").length).toBe(1);
238
239         // expect 2 clickable links
240         expect($("div#mkwsSwitch a").length).toBe(2);
241     });
242
243     it("switch to target view", function () {
244         var click = $("a#mkwsSwitch_targets").trigger("click");
245         debug("target click is success: " + click.length);
246         expect(click.length).toBe(1);
247
248         // now the target table must be visible
249         expect($("div#mkwsBytarget").is(":visible")).toBe(true);
250         expect($("div#mkwsRecords").is(":visible")).toBe(false);
251
252         // wait a half second, to show the target view
253         var time = (new Date).getTime();
254         waitsFor(function () {
255             return (new Date).getTime() - time > 700 ? true : false;
256         }, "wait some miliseconds", 1 * 1000);
257
258         // look for table header
259         runs(function () {
260             expect($("div#mkwsBytarget").html()).toMatch(/Target ID/);
261         });
262     });
263
264     it("switch back to record view", function () {
265         var click = $("a#mkwsSwitch_records").trigger("click");
266         debug("record click is success: " + click.length);
267         expect(click.length).toBe(1);
268
269         // now the target table must be visible
270         expect($("div#mkwsBytarget").is(":visible")).toBe(false);
271         expect($("div#mkwsRecords").is(":visible")).toBe(true);
272     });
273 });
274
275 describe("Check status client counter", function () {
276     function get_time() {
277         var date = new Date();
278         return date.getTime();
279     }
280     var time = get_time();
281
282     it("check status clients", function () {
283         waitsFor(function () {
284             var clients = $("div#mkwsStat span.clients");
285             if (clients.length == 1 && clients.text() == "0/1") {
286                 return true;
287             } else {
288                 return false;
289             }
290
291         }, "wait for status", 4 * 1000);
292
293         runs(function () {
294             var clients = $("div#mkwsStat span.clients");
295             debug("span.clients: " + clients.text());
296             expect(clients.text()).toEqual("0/1");
297         });
298
299     });
300
301 });
302
303 /* dummy EOF */
304 describe("All tests are done", function () {
305     it(">>> hooray <<<", function () {});
306 });