// Unit tests for src/main.js, run by script/frontend-test with Node's // built-in test runner. Importing the module does not start the page. import { after, before, test } from "node:test"; import assert from "node:assert/strict"; import { createServer } from "node:http"; import { CONFIG, measureLatency } from "../../src/main.js"; // measureLatency writes timeouts to the debug log, which looks for its // panel in the page. There is no page here. globalThis.document = { getElementById: () => null }; // A target that answers after the number of milliseconds in the path, // e.g. /600. let server; let target; before(async () => { server = createServer((req, res) => { const delay = Number(new URL(req.url, "http://x").pathname.slice(1)); setTimeout(() => res.end(), delay); }); await new Promise((resolve) => server.listen(0, "127.0.0.1", resolve)); target = `http://127.0.0.1:${server.address().port}`; }); after(() => { server.closeAllConnections(); server.close(); }); test("the timeout is 80% of the refresh interval", () => { CONFIG.updateInterval = 30000; assert.equal(CONFIG.requestTimeout, 24000); CONFIG.updateInterval = 3000; assert.equal(CONFIG.requestTimeout, 2400); }); test("an answer within the timeout is recorded with its real time, a later one as a timeout", async () => { // 600ms is past the 400ms timeout of a 500ms interval... CONFIG.updateInterval = 500; assert.deepEqual(await measureLatency(`${target}/600`), { latency: null, error: "timeout", }); // ...and within the 1200ms timeout of a 1500ms interval. CONFIG.updateInterval = 1500; const { latency, error } = await measureLatency(`${target}/600`); assert.equal(error, null); assert.ok(latency >= 600 && latency < 1200, `latency ${latency}ms`); });