From 930223019e256a6bd0a72068952975ac405743c8 Mon Sep 17 00:00:00 2001 From: bbassie <17990055+bbassie@users.noreply.github.com> Date: Fri, 28 Aug 2026 10:54:25 +0000 Subject: [PATCH] Add serveWeb test for webRoot and staticRoot substitution Co-Authored-By: Claude Fable 5 --- test/controller/Controller.js | 37 +++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/test/controller/Controller.js b/test/controller/Controller.js index 62c1c264..35e6db9c 100644 --- a/test/controller/Controller.js +++ b/test/controller/Controller.js @@ -3,6 +3,9 @@ const path = require("node:path"); const assert = require("assert").strict; const { Controller, HostRecord, InstanceRecord, UserRecord } = require("@clusterio/controller"); const { EventEmitter } = require("stream"); +const events = require("node:events"); +const http = require("node:http"); +const express = require("express"); const { ControllerConfig, Address, RequestError, @@ -48,6 +51,40 @@ describe("controller/src/Controller", function() { assert(list.check(ip), `${ip} missing from list`); } } + describe(".serveWeb()", function() { + let server, port; + before(async function() { + const app = express(); + app.locals.controller = { config: { get: () => "Test Cluster" } }; + app.locals.mainBundle = "static/main.abc.js"; + for (let route of ["/", "/instances/:id/view"]) { + app.get(route, Controller.serveWeb(route)); + } + server = http.createServer(app); + server.listen(0, "localhost"); + await events.once(server, "listening"); + port = server.address().port; + }); + after(function() { + server.close(); + }); + it("should serve the root with static/ in staticRoot", async function() { + const html = await (await fetch(`http://localhost:${port}/`)).text(); + assert(html.includes("Test Cluster")); + assert(html.includes("src=\"./static/main.abc.js\"")); + assert(html.includes("new URL(\"./\", document.location)")); + assert(html.includes("new URL(\"./static/\", document.location)")); + }); + it("should use relative roots for nested routes", async function() { + let html = await (await fetch(`http://localhost:${port}/instances/1/view`)).text(); + assert(html.includes("src=\"../../static/main.abc.js\"")); + assert(html.includes("new URL(\"../../\", document.location)")); + assert(html.includes("new URL(\"../../static/\", document.location)")); + html = await (await fetch(`http://localhost:${port}/instances/1/view/`)).text(); + assert(html.includes("src=\"../../../static/main.abc.js\"")); + assert(html.includes("new URL(\"../../../static/\", document.location)")); + }); + }); describe(".parseTrustedProxies()", function() { it("should parse addresses", function() { controller.config.set("controller.trusted_proxies", "10.0.0.1");