|
| 1 | +"use strict"; |
| 2 | + |
| 3 | +const assert = require("node:assert/strict"); |
| 4 | +const fs = require("node:fs"); |
| 5 | +const path = require("node:path"); |
| 6 | +const vm = require("node:vm"); |
| 7 | + |
| 8 | +const html = fs.readFileSync(path.join(__dirname, "../search.html"), "utf8"); |
| 9 | +const script = html.match(/<script>([\s\S]*?)<\/script>/)[1]; |
| 10 | +const pages = [ |
| 11 | + { title: "Wall treatment", text: "wall function transition", url: "/SU2/docs_v7/Wall-Treatment/", section: "docs_v7" }, |
| 12 | + { title: "Omega", text: "omega equation", url: "/SU2/docs_v7/Turbulence-Models/", section: "docs_v7" } |
| 13 | +]; |
| 14 | + |
| 15 | +function openSearch(fetch, query = "") { |
| 16 | + const box = { value: "", addEventListener: (_, callback) => { box.run = callback; } }; |
| 17 | + const count = { textContent: "" }; |
| 18 | + const list = { rows: [], appendChild: row => list.rows.push(row.innerHTML) }; |
| 19 | + Object.defineProperty(list, "innerHTML", { set: () => { list.rows = []; } }); |
| 20 | + const document = { |
| 21 | + getElementById: id => ({ "search-box": box, "search-count": count, "search-results": list })[id], |
| 22 | + createElement: () => ({ innerHTML: "" }) |
| 23 | + }; |
| 24 | + vm.runInNewContext(script, { document, fetch, URLSearchParams, location: { search: query } }); |
| 25 | + return { box, count, list, search: query => { box.value = query; box.run(); } }; |
| 26 | +} |
| 27 | + |
| 28 | +const settled = () => new Promise(resolve => setImmediate(resolve)); |
| 29 | + |
| 30 | +(async () => { |
| 31 | + let resolve; |
| 32 | + const page = openSearch(() => new Promise(done => { resolve = done; }), "?q=wall+function"); |
| 33 | + assert.match(page.count.textContent, /loading/i); |
| 34 | + assert.equal(page.box.value, "wall function"); |
| 35 | + page.search("omega"); |
| 36 | + assert.match(page.count.textContent, /loading/i); |
| 37 | + resolve({ ok: true, json: () => Promise.resolve(pages) }); |
| 38 | + await settled(); |
| 39 | + assert.equal(page.box.value, "omega"); |
| 40 | + assert.equal(page.count.textContent, "1 page found."); |
| 41 | + assert.match(page.list.rows[0], /Turbulence-Models/); |
| 42 | + page.search("wall function"); |
| 43 | + assert.equal(page.count.textContent, "1 page found."); |
| 44 | + assert.match(page.list.rows[0], /Wall-Treatment/); |
| 45 | + page.search("wall omega"); |
| 46 | + assert.equal(page.count.textContent, "0 pages found."); |
| 47 | + page.search("k omega"); |
| 48 | + assert.equal(page.count.textContent, "1 page found."); |
| 49 | + page.search("k"); |
| 50 | + assert.match(page.count.textContent, /at least two characters/i); |
| 51 | + assert.equal(page.list.rows.length, 0); |
| 52 | + page.search("ω"); |
| 53 | + assert.match(page.count.textContent, /at least two characters/i); |
| 54 | + page.search(" "); |
| 55 | + assert.equal(page.count.textContent, ""); |
| 56 | + |
| 57 | + const failures = [ |
| 58 | + () => Promise.reject(new Error("Network unavailable")), |
| 59 | + () => Promise.resolve({ ok: false, json: () => Promise.resolve([]) }), |
| 60 | + () => Promise.resolve({ ok: true, json: () => Promise.reject(new SyntaxError("Invalid JSON")) }), |
| 61 | + () => Promise.resolve({ ok: true, json: () => Promise.resolve({ error: "No index" }) }) |
| 62 | + ]; |
| 63 | + for (const fetch of failures) { |
| 64 | + const failed = openSearch(fetch); |
| 65 | + await settled(); |
| 66 | + assert.match(failed.count.textContent, /could not load/i); |
| 67 | + failed.search("wall"); |
| 68 | + assert.match(failed.count.textContent, /could not load/i); |
| 69 | + assert.equal(failed.list.rows.length, 0); |
| 70 | + } |
| 71 | + console.log("Search checks passed: loading, matching, short queries and four index failures."); |
| 72 | +})().catch(error => { |
| 73 | + console.error(error); |
| 74 | + process.exitCode = 1; |
| 75 | +}); |
0 commit comments