]> git.openstreetmap.org Git - nominatim-ui.git/blob - test/_bootstrap.js
correct various typos
[nominatim-ui.git] / test / _bootstrap.js
1 import static_server from 'static-server';
2 import http from 'node:http';
3 import httpProxy from 'http-proxy';
4 import puppeteer from 'puppeteer';
5 import fse from 'fs-extra';
6
7 const testing_port = 9999; // this is the port all tests expect nominatim-ui to listen to
8
9 // The installation on https://nominatim.openstreetmap.org/ui/ is a bit more complex as
10 // for backward compatibility they run the API and the UI on the same port. Nominatim-UI
11 // is installed in the /ui subdirectory plus their webserver has custom redirect rules.
12 //
13 // We can simulate that with a proxy.
14 const use_proxy = !!process.env.API_ON_SAME_PORT;
15 const static_port = use_proxy ? 9998 : 9999;
16 const reverse_only = !!process.env.REVERSE_ONLY;
17
18 // Methods to run at the start and end of the mocha testsuite run
19 // https://mochajs.org/#global-setup-fixtures
20 export async function mochaGlobalSetup() {
21   const workdir = 'dist_for_testing';
22
23   // 1. Prepare build directory
24   fse.mkdirpSync(workdir);
25   fse.copySync('dist', workdir);
26
27   let api_endpoint = use_proxy ? '/' : 'https:/nominatim.openstreetmap.org/';
28
29   fse.outputFile(workdir + '/theme/config.theme.js', `
30 Nominatim_Config.Nominatim_API_Endpoint = '${api_endpoint}';
31 Nominatim_Config.Reverse_Only = ${reverse_only};
32   `);
33
34
35   // 2. Start webserver pointing to build directory
36   // https://github.com/nbluis/static-server#readme
37   this.static_http_server = new static_server({ port: static_port, rootPath: workdir });
38   await this.static_http_server.start();
39   console.log(`static server serving ${workdir} directory running on port ${static_port}`);
40
41   if (use_proxy) {
42     // https://github.com/http-party/node-http-proxy#readme
43     const proxy = await httpProxy.createProxy({ changeOrigin: true, followRedirects: true });
44     this.proxy = proxy;
45     console.log('proxy started');
46
47     this.proxy_server = await http.createServer((req, res) => {
48       // identify if the requests should be served by the (remote) API or static webserver
49       let api_url_match = req.url.match(/\/(\w+\.php)/);
50
51       let target = api_url_match
52         ? 'https://nominatim.openstreetmap.org'
53         : 'http://localhost:' + static_port;
54
55       // console.log(`http proxy ${req.url} => ${target + req.url}`)
56       return proxy.web(req, res, { target: target });
57     }).listen(testing_port);
58     console.log(`proxy server started on port ${testing_port}`);
59   }
60
61
62   // 3. Create browser instance
63   global.browser = await puppeteer.launch({
64     defaultViewport: { width: 1024, height: 768 },
65     timeout: 10000,
66     // latency: 1000,
67     headless: 'new',
68     args: [
69       '--user-agent=Nominatim UI test suite Mozilla/5.0 Gecko/20100101 HeadlessChrome/90.0'
70     ]
71   });
72 }
73
74
75 export async function mochaGlobalTeardown() {
76   global.browser.close();
77
78   await this.static_http_server.stop();
79   console.log('static server stopped');
80
81   if (use_proxy) {
82     await this.proxy.close();
83     console.log('proxy stopped');
84
85     this.proxy_server.close(() => console.log('proxy server stopped'));
86   }
87 }