Marc Kupietz | 93d7f70 | 2025-06-27 15:41:48 +0200 | [diff] [blame^] | 1 | const https = require('https'); |
Marc Kupietz | 490b053 | 2024-09-05 09:36:21 +0200 | [diff] [blame] | 2 | const puppeteer = require('puppeteer-extra'); |
| 3 | puppeteer.use(require('puppeteer-extra-plugin-user-preferences')({ |
| 4 | userPrefs: { |
| 5 | safebrowsing: { |
| 6 | enabled: false, |
| 7 | enhanced: false |
| 8 | } |
| 9 | } |
| 10 | })); |
Marc Kupietz | 55fc316 | 2022-12-04 16:25:49 +0100 | [diff] [blame] | 11 | const chai = require('chai'); |
Marc Kupietz | 4c5a7a5 | 2022-12-04 16:56:30 +0100 | [diff] [blame] | 12 | const { afterEach } = require('mocha'); |
Marc Kupietz | 55fc316 | 2022-12-04 16:25:49 +0100 | [diff] [blame] | 13 | const assert = chai.assert; |
| 14 | const should = chai.should(); |
Marc Kupietz | 7f1666a | 2024-07-12 18:35:31 +0200 | [diff] [blame] | 15 | var slack = null; |
Marc Kupietz | 55fc316 | 2022-12-04 16:25:49 +0100 | [diff] [blame] | 16 | |
Marc Kupietz | 0f6c54d | 2022-12-03 15:32:40 +0100 | [diff] [blame] | 17 | const KORAP_URL = process.env.KORAP_URL || "http://localhost:64543"; |
Marc Kupietz | bfb2301 | 2025-06-03 15:47:10 +0200 | [diff] [blame] | 18 | const KORAP_LOGIN = 'KORAP_USERNAME' in process.env ? process.env.KORAP_USERNAME : 'KORAP_LOGIN' in process.env ? process.env.KORAP_LOGIN : "user2" |
| 19 | const KORAP_PWD = process.env.KORAP_PWD || process.env.KORAP_PASSWORD || "password2"; |
Marc Kupietz | 2698238 | 2022-12-04 19:02:57 +0100 | [diff] [blame] | 20 | const KORAP_QUERIES = process.env.KORAP_QUERIES || 'geht, [orth=geht & cmc/pos=VVFIN]' |
Marc Kupietz | c8ffb2b | 2025-06-12 16:44:23 +0200 | [diff] [blame] | 21 | const KORAP_MIN_TOKENS_IN_CORPUS = parseInt(process.env.KORAP_MIN_TOKENS_IN_CORPUS || "100000", 10); |
Marc Kupietz | 5a73a4d | 2022-12-04 14:09:58 +0100 | [diff] [blame] | 22 | const korap_rc = require('../lib/korap_rc.js').new(KORAP_URL) |
| 23 | |
Marc Kupietz | 7f1666a | 2024-07-12 18:35:31 +0200 | [diff] [blame] | 24 | const slack_webhook = process.env.SLACK_WEBHOOK_URL; |
Marc Kupietz | 4d335a3 | 2024-09-04 16:13:48 +0200 | [diff] [blame] | 25 | |
Marc Kupietz | 7f1666a | 2024-07-12 18:35:31 +0200 | [diff] [blame] | 26 | if (slack_webhook) { |
| 27 | slack = require('slack-notify')(slack_webhook); |
| 28 | } |
| 29 | |
Marc Kupietz | 5a73a4d | 2022-12-04 14:09:58 +0100 | [diff] [blame] | 30 | function ifConditionIt(title, condition, test) { |
Marc Kupietz | 55fc316 | 2022-12-04 16:25:49 +0100 | [diff] [blame] | 31 | return condition ? it(title, test) : it.skip(title + " (skipped)", test) |
Marc Kupietz | 5a73a4d | 2022-12-04 14:09:58 +0100 | [diff] [blame] | 32 | } |
Marc Kupietz | c407782 | 2022-12-03 15:32:40 +0100 | [diff] [blame] | 33 | |
Marc Kupietz | 0f6c54d | 2022-12-03 15:32:40 +0100 | [diff] [blame] | 34 | describe('Running KorAP UI end-to-end tests on ' + KORAP_URL, () => { |
Marc Kupietz | c407782 | 2022-12-03 15:32:40 +0100 | [diff] [blame] | 35 | |
Marc Kupietz | 93d7f70 | 2025-06-27 15:41:48 +0200 | [diff] [blame^] | 36 | let browser; |
| 37 | let page; |
| 38 | |
| 39 | |
Marc Kupietz | c407782 | 2022-12-03 15:32:40 +0100 | [diff] [blame] | 40 | before(async () => { |
Marc Kupietz | 69e0280 | 2023-11-08 14:37:22 +0100 | [diff] [blame] | 41 | browser = await puppeteer.launch({ |
Marc Kupietz | bfb2301 | 2025-06-03 15:47:10 +0200 | [diff] [blame] | 42 | 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 Kupietz | 69e0280 | 2023-11-08 14:37:22 +0100 | [diff] [blame] | 52 | }) |
Marc Kupietz | c407782 | 2022-12-03 15:32:40 +0100 | [diff] [blame] | 53 | page = await browser.newPage() |
Marc Kupietz | 4c5a7a5 | 2022-12-04 16:56:30 +0100 | [diff] [blame] | 54 | await page.setViewport({ |
Marc Kupietz | 9e0f519 | 2025-03-09 12:12:16 +0100 | [diff] [blame] | 55 | width: 1980, |
Marc Kupietz | 4c5a7a5 | 2022-12-04 16:56:30 +0100 | [diff] [blame] | 56 | height: 768, |
| 57 | deviceScaleFactor: 1, |
Marc Kupietz | 964e777 | 2025-06-03 15:02:30 +0200 | [diff] [blame] | 58 | }); |
Marc Kupietz | 93d7f70 | 2025-06-27 15:41:48 +0200 | [diff] [blame^] | 59 | |
Marc Kupietz | c407782 | 2022-12-03 15:32:40 +0100 | [diff] [blame] | 60 | }) |
| 61 | |
Marc Kupietz | 93d7f70 | 2025-06-27 15:41:48 +0200 | [diff] [blame^] | 62 | after(async function() { |
| 63 | if (browser && typeof browser.close === 'function') { |
| 64 | await browser.close(); |
| 65 | } |
Marc Kupietz | c407782 | 2022-12-03 15:32:40 +0100 | [diff] [blame] | 66 | }) |
| 67 | |
Marc Kupietz | 4c5a7a5 | 2022-12-04 16:56:30 +0100 | [diff] [blame] | 68 | afterEach(async function () { |
| 69 | if (this.currentTest.state == "failed") { |
Marc Kupietz | 7f1666a | 2024-07-12 18:35:31 +0200 | [diff] [blame] | 70 | if (slack) { |
Marc Kupietz | 8f7c204 | 2025-06-24 09:55:03 +0200 | [diff] [blame] | 71 | try { |
Marc Kupietz | 8f7c204 | 2025-06-24 09:55:03 +0200 | [diff] [blame] | 72 | 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 Kupietz | 93d7f70 | 2025-06-27 15:41:48 +0200 | [diff] [blame^] | 92 | // 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 Kupietz | 8f7c204 | 2025-06-24 09:55:03 +0200 | [diff] [blame] | 100 | |
Marc Kupietz | 93d7f70 | 2025-06-27 15:41:48 +0200 | [diff] [blame^] | 101 | 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 Kupietz | 8f7c204 | 2025-06-24 09:55:03 +0200 | [diff] [blame] | 107 | |
Marc Kupietz | 93d7f70 | 2025-06-27 15:41:48 +0200 | [diff] [blame^] | 108 | 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 Kupietz | 8f7c204 | 2025-06-24 09:55:03 +0200 | [diff] [blame] | 115 | |
Marc Kupietz | 93d7f70 | 2025-06-27 15:41:48 +0200 | [diff] [blame^] | 116 | } catch (uploadError) { |
| 117 | console.error('Failed to upload screenshot to Slack:', uploadError.message); |
| 118 | } |
Marc Kupietz | 8f7c204 | 2025-06-24 09:55:03 +0200 | [diff] [blame] | 119 | } |
Marc Kupietz | 7f1666a | 2024-07-12 18:35:31 +0200 | [diff] [blame] | 120 | } |
Marc Kupietz | 4c5a7a5 | 2022-12-04 16:56:30 +0100 | [diff] [blame] | 121 | } |
Marc Kupietz | 964e777 | 2025-06-03 15:02:30 +0200 | [diff] [blame] | 122 | }) |
Marc Kupietz | 4c5a7a5 | 2022-12-04 16:56:30 +0100 | [diff] [blame] | 123 | |
Marc Kupietz | 93d7f70 | 2025-06-27 15:41:48 +0200 | [diff] [blame^] | 124 | 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 Kupietz | 5a73a4d | 2022-12-04 14:09:58 +0100 | [diff] [blame] | 128 | |
Marc Kupietz | 93d7f70 | 2025-06-27 15:41:48 +0200 | [diff] [blame^] | 129 | 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 Kupietz | 5a73a4d | 2022-12-04 14:09:58 +0100 | [diff] [blame] | 160 | |
Marc Kupietz | 93d7f70 | 2025-06-27 15:41:48 +0200 | [diff] [blame^] | 161 | 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 Kupietz | c407782 | 2022-12-03 15:32:40 +0100 | [diff] [blame] | 211 | |
Marc Kupietz | 93d7f70 | 2025-06-27 15:41:48 +0200 | [diff] [blame^] | 212 | describe('UI Tests', function() { |
Marc Kupietz | c407782 | 2022-12-03 15:32:40 +0100 | [diff] [blame] | 213 | |
Marc Kupietz | 93d7f70 | 2025-06-27 15:41:48 +0200 | [diff] [blame^] | 214 | 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 Kupietz | c407782 | 2022-12-03 15:32:40 +0100 | [diff] [blame] | 221 | |
Marc Kupietz | 93d7f70 | 2025-06-27 15:41:48 +0200 | [diff] [blame^] | 222 | |
Marc Kupietz | c8ffb2b | 2025-06-12 16:44:23 +0200 | [diff] [blame] | 223 | |
Marc Kupietz | 93d7f70 | 2025-06-27 15:41:48 +0200 | [diff] [blame^] | 224 | 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 Kupietz | 0f6c54d | 2022-12-03 15:32:40 +0100 | [diff] [blame] | 233 | }) |
Marc Kupietz | 5a73a4d | 2022-12-04 14:09:58 +0100 | [diff] [blame] | 234 | |
Marc Kupietz | c407782 | 2022-12-03 15:32:40 +0100 | [diff] [blame] | 235 | |
Marc Kupietz | 93d7f70 | 2025-06-27 15:41:48 +0200 | [diff] [blame^] | 236 | 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 | }); |