blob: fcf1fcd3929c91f14940f7a7db06660ab773bed1 [file] [log] [blame]
Marc Kupietz93d7f702025-06-27 15:41:48 +02001const https = require('https');
Marc Kupietz490b0532024-09-05 09:36:21 +02002const puppeteer = require('puppeteer-extra');
3puppeteer.use(require('puppeteer-extra-plugin-user-preferences')({
4 userPrefs: {
5 safebrowsing: {
6 enabled: false,
7 enhanced: false
8 }
9 }
10}));
Marc Kupietz55fc3162022-12-04 16:25:49 +010011const chai = require('chai');
Marc Kupietz4c5a7a52022-12-04 16:56:30 +010012const { afterEach } = require('mocha');
Marc Kupietz55fc3162022-12-04 16:25:49 +010013const assert = chai.assert;
14const should = chai.should();
Marc Kupietz7f1666a2024-07-12 18:35:31 +020015var slack = null;
Marc Kupietz55fc3162022-12-04 16:25:49 +010016
Marc Kupietz0f6c54d2022-12-03 15:32:40 +010017const KORAP_URL = process.env.KORAP_URL || "http://localhost:64543";
Marc Kupietzbfb23012025-06-03 15:47:10 +020018const KORAP_LOGIN = 'KORAP_USERNAME' in process.env ? process.env.KORAP_USERNAME : 'KORAP_LOGIN' in process.env ? process.env.KORAP_LOGIN : "user2"
19const KORAP_PWD = process.env.KORAP_PWD || process.env.KORAP_PASSWORD || "password2";
Marc Kupietz26982382022-12-04 19:02:57 +010020const KORAP_QUERIES = process.env.KORAP_QUERIES || 'geht, [orth=geht & cmc/pos=VVFIN]'
Marc Kupietzc8ffb2b2025-06-12 16:44:23 +020021const KORAP_MIN_TOKENS_IN_CORPUS = parseInt(process.env.KORAP_MIN_TOKENS_IN_CORPUS || "100000", 10);
Marc Kupietz5a73a4d2022-12-04 14:09:58 +010022const korap_rc = require('../lib/korap_rc.js').new(KORAP_URL)
23
Marc Kupietz7f1666a2024-07-12 18:35:31 +020024const slack_webhook = process.env.SLACK_WEBHOOK_URL;
Marc Kupietz4d335a32024-09-04 16:13:48 +020025
Marc Kupietz7f1666a2024-07-12 18:35:31 +020026if (slack_webhook) {
27 slack = require('slack-notify')(slack_webhook);
28}
29
Marc Kupietz5a73a4d2022-12-04 14:09:58 +010030function ifConditionIt(title, condition, test) {
Marc Kupietz55fc3162022-12-04 16:25:49 +010031 return condition ? it(title, test) : it.skip(title + " (skipped)", test)
Marc Kupietz5a73a4d2022-12-04 14:09:58 +010032}
Marc Kupietzc4077822022-12-03 15:32:40 +010033
Marc Kupietz0f6c54d2022-12-03 15:32:40 +010034describe('Running KorAP UI end-to-end tests on ' + KORAP_URL, () => {
Marc Kupietzc4077822022-12-03 15:32:40 +010035
Marc Kupietz93d7f702025-06-27 15:41:48 +020036 let browser;
37 let page;
38
39
Marc Kupietzc4077822022-12-03 15:32:40 +010040 before(async () => {
Marc Kupietz69e02802023-11-08 14:37:22 +010041 browser = await puppeteer.launch({
Marc Kupietzbfb23012025-06-03 15:47:10 +020042 headless: "shell",
43 args: [
44 '--no-sandbox',
45 '--disable-setuid-sandbox',
46 '--disable-dev-shm-usage',
47 '--disable-accelerated-2d-canvas',
48 '--no-first-run',
49 '--no-zygote',
50 '--disable-gpu'
51 ]
Marc Kupietz69e02802023-11-08 14:37:22 +010052 })
Marc Kupietzc4077822022-12-03 15:32:40 +010053 page = await browser.newPage()
Marc Kupietz4c5a7a52022-12-04 16:56:30 +010054 await page.setViewport({
Marc Kupietz9e0f5192025-03-09 12:12:16 +010055 width: 1980,
Marc Kupietz4c5a7a52022-12-04 16:56:30 +010056 height: 768,
57 deviceScaleFactor: 1,
Marc Kupietz964e7772025-06-03 15:02:30 +020058 });
Marc Kupietz93d7f702025-06-27 15:41:48 +020059
Marc Kupietzc4077822022-12-03 15:32:40 +010060 })
61
Marc Kupietz93d7f702025-06-27 15:41:48 +020062 after(async function() {
63 if (browser && typeof browser.close === 'function') {
64 await browser.close();
65 }
Marc Kupietzc4077822022-12-03 15:32:40 +010066 })
67
Marc Kupietz4c5a7a52022-12-04 16:56:30 +010068 afterEach(async function () {
69 if (this.currentTest.state == "failed") {
Marc Kupietz7f1666a2024-07-12 18:35:31 +020070 if (slack) {
Marc Kupietz8f7c2042025-06-24 09:55:03 +020071 try {
Marc Kupietz8f7c2042025-06-24 09:55:03 +020072 slack.alert({
73 text: `🚨 Test on ${KORAP_URL} failed: *${this.currentTest.title}*`,
74 attachments: [{
75 color: 'danger',
76 fields: [{
77 title: 'Failed Test',
78 value: this.currentTest.title,
79 short: false
80 }, {
81 title: 'URL',
82 value: KORAP_URL,
83 short: true
84 }]
85 }]
86 });
87 } catch (slackError) {
88 console.error('Failed to send notification to Slack:', slackError.message);
89 }
90 }
91
Marc Kupietz93d7f702025-06-27 15:41:48 +020092 // Only take screenshot if it's not one of the initial connectivity/SSL tests
93 const initialTestTitles = [
94 'should be reachable',
95 'should have a valid SSL certificate'
96 ];
97 if (!initialTestTitles.includes(this.currentTest.title) && page) {
98 const screenshotPath = "failed_" + this.currentTest.title.replaceAll(/[ &\/]/g, "_") + '.png';
99 await page.screenshot({ path: screenshotPath });
Marc Kupietz8f7c2042025-06-24 09:55:03 +0200100
Marc Kupietz93d7f702025-06-27 15:41:48 +0200101 const slackToken = process.env.SLACK_TOKEN;
102 if (slackToken) {
103 try {
104 const { WebClient } = require('@slack/web-api');
105 const fs = require('fs'); const web = new WebClient(slackToken);
106 const channelId = process.env.SLACK_CHANNEL_ID || 'C07CM4JS48H';
Marc Kupietz8f7c2042025-06-24 09:55:03 +0200107
Marc Kupietz93d7f702025-06-27 15:41:48 +0200108 const result = await web.files.uploadV2({
109 channel_id: channelId,
110 file: fs.createReadStream(screenshotPath),
111 filename: screenshotPath,
112 title: `Screenshot: ${this.currentTest.title}`,
113 initial_comment: `📸 Screenshot of failed test: ${this.currentTest.title} on ${KORAP_URL}`
114 });
Marc Kupietz8f7c2042025-06-24 09:55:03 +0200115
Marc Kupietz93d7f702025-06-27 15:41:48 +0200116 } catch (uploadError) {
117 console.error('Failed to upload screenshot to Slack:', uploadError.message);
118 }
Marc Kupietz8f7c2042025-06-24 09:55:03 +0200119 }
Marc Kupietz7f1666a2024-07-12 18:35:31 +0200120 }
Marc Kupietz4c5a7a52022-12-04 16:56:30 +0100121 }
Marc Kupietz964e7772025-06-03 15:02:30 +0200122 })
Marc Kupietz4c5a7a52022-12-04 16:56:30 +0100123
Marc Kupietz93d7f702025-06-27 15:41:48 +0200124 it('should be reachable', function (done) {
125 let doneCalled = false;
126 const url = new URL(KORAP_URL);
127 const httpModule = url.protocol === 'https:' ? https : require('http');
Marc Kupietz5a73a4d2022-12-04 14:09:58 +0100128
Marc Kupietz93d7f702025-06-27 15:41:48 +0200129 const req = httpModule.request({
130 method: 'HEAD',
131 hostname: url.hostname,
132 port: url.port || (url.protocol === 'https:' ? 443 : 80),
133 path: url.pathname,
134 timeout: 5000
135 }, res => {
136 if (!doneCalled) {
137 doneCalled = true;
138 if (res.statusCode >= 200 && res.statusCode < 400) {
139 done();
140 } else {
141 done(new Error(`Server is not reachable. Status code: ${res.statusCode}`));
142 }
143 }
144 });
145 req.on('timeout', () => {
146 if (!doneCalled) {
147 doneCalled = true;
148 req.destroy();
149 done(new Error('Request to server timed out.'));
150 }
151 });
152 req.on('error', err => {
153 if (!doneCalled) {
154 doneCalled = true;
155 done(err);
156 }
157 });
158 req.end();
159 });
Marc Kupietz5a73a4d2022-12-04 14:09:58 +0100160
Marc Kupietz93d7f702025-06-27 15:41:48 +0200161 it('should have a valid SSL certificate', function (done) {
162 let doneCalled = false;
163 const url = new URL(KORAP_URL);
164 if (url.protocol !== 'https:') {
165 return this.skip();
166 }
167 const req = https.request({
168 method: 'HEAD',
169 hostname: url.hostname,
170 port: url.port || 443,
171 path: url.pathname,
172 timeout: 5000
173 }, res => {
174 if (!doneCalled) {
175 doneCalled = true;
176 const cert = res.socket.getPeerCertificate();
177 if (cert && cert.valid_to) {
178 const validTo = new Date(cert.valid_to);
179 if (validTo > new Date()) {
180 done();
181 } else {
182 done(new Error(`SSL certificate expired on ${validTo.toDateString()}`));
183 }
184 } else if (res.socket.isSessionReused()){
185 done();
186 }
187 else {
188 done(new Error('Could not retrieve SSL certificate information.'));
189 }
190 }
191 });
192 req.on('timeout', () => {
193 if (!doneCalled) {
194 doneCalled = true;
195 req.destroy();
196 done(new Error('Request to server timed out.'));
197 }
198 });
199 req.on('error', err => {
200 if (!doneCalled) {
201 doneCalled = true;
202 if (err.code === 'CERT_HAS_EXPIRED') {
203 done(new Error('SSL certificate has expired.'));
204 } else {
205 done(err);
206 }
207 }
208 });
209 req.end();
210 });
Marc Kupietzc4077822022-12-03 15:32:40 +0100211
Marc Kupietz93d7f702025-06-27 15:41:48 +0200212 describe('UI Tests', function() {
Marc Kupietzc4077822022-12-03 15:32:40 +0100213
Marc Kupietz93d7f702025-06-27 15:41:48 +0200214 before(function() {
215 // Check the state of the parent suite's tests
216 const initialTests = this.test.parent.parent.tests;
217 if (initialTests[0].state === 'failed' || initialTests[1].state === 'failed') {
218 this.skip();
219 }
220 });
Marc Kupietzc4077822022-12-03 15:32:40 +0100221
Marc Kupietz93d7f702025-06-27 15:41:48 +0200222
Marc Kupietzc8ffb2b2025-06-12 16:44:23 +0200223
Marc Kupietz93d7f702025-06-27 15:41:48 +0200224 it('KorAP UI is up and running', async function () {
225 try {
226 await page.goto(KORAP_URL, { waitUntil: 'domcontentloaded' });
227 await page.waitForSelector("#q-field", { visible: true });
228 const query_field = await page.$("#q-field")
229 assert.isNotNull(query_field, "#q-field not found. Kalamar not running?");
230 } catch (error) {
231 throw new Error(`Failed to load KorAP UI or find query field: ${error.message}`);
232 }
Marc Kupietz0f6c54d2022-12-03 15:32:40 +0100233 })
Marc Kupietz5a73a4d2022-12-04 14:09:58 +0100234
Marc Kupietzc4077822022-12-03 15:32:40 +0100235
Marc Kupietz93d7f702025-06-27 15:41:48 +0200236 ifConditionIt('Login into KorAP with incorrect credentials fails',
237 KORAP_LOGIN != "",
238 (async () => {
239 const login_result = await korap_rc.login(page, KORAP_LOGIN, KORAP_PWD + "*")
240 login_result.should.be.false
241 }))
242
243 ifConditionIt('Login into KorAP with correct credentials succeeds',
244 KORAP_LOGIN != "",
245 (async () => {
246 const login_result = await korap_rc.login(page, KORAP_LOGIN, KORAP_PWD)
247 login_result.should.be.true
248 }))
249
250 it('Can turn glimpse off',
251 (async () => {
252 await korap_rc.assure_glimpse_off(page)
253 }))
254
255 it('Corpus statistics show sufficient tokens',
256 (async () => {
257 const tokenCount = await korap_rc.check_corpus_statistics(page, KORAP_MIN_TOKENS_IN_CORPUS);
258 console.log(`Found ${tokenCount} tokens in corpus, minimum required: ${KORAP_MIN_TOKENS_IN_CORPUS}`);
259 tokenCount.should.be.above(KORAP_MIN_TOKENS_IN_CORPUS - 1,
260 `Corpus should have at least ${KORAP_MIN_TOKENS_IN_CORPUS} tokens, but found ${tokenCount}`);
261 })).timeout(90000)
262
263 describe('Running searches that should have hits', () => {
264
265 before(async () => { await korap_rc.login(page, KORAP_LOGIN, KORAP_PWD) })
266
267 KORAP_QUERIES.split(/[;,] */).forEach((query, i) => {
268 it('Search for "' + query + '" has hits',
269 (async () => {
270 await korap_rc.assure_glimpse_off(page)
271 const hits = await korap_rc.search(page, query)
272 hits.should.be.above(0)
273 })).timeout(20000)
274 })
275 })
276
277 ifConditionIt('Logout works',
278 KORAP_LOGIN != "",
279 (async () => {
280 const logout_result = await korap_rc.logout(page)
281 logout_result.should.be.true
282 })).timeout(15000)
283 });
284});