/);
});
test("another method on an unknown address is a 404 too", async () => {
const res = await fetch(`${base}/api/no-such-thing`, { method: "POST", headers: { "content-type": "application/json", origin: base }, body: "{}" });
assert.ok([403, 404].includes(res.status), String(res.status));
});
test("the app's own pages still get the app (it draws their 404 itself)", { skip: !existsSync(new URL("../../web/dist/index.html", import.meta.url)) }, async () => {
for (const path of ["/", "/servers", "/servers/3", "/alerts/incidents/9", "/share/status/abc-DEF_123", "/no-such-page"]) {
const res = await get(path, "text/html");
assert.equal(res.status, 200, path);
assert.match(await res.text(), /
/, path);
}
});
});
describe("what the app draws for itself", () => {
test("an unknown address is a 404 page inside the menu, at the top level and under Settings", () => {
const app = read("App.tsx");
assert.match(app, /\} \/>\s*<\/Routes>/, "the end of the list of pages");
assert.match(app, /\} \/>\s*<\/Route>/, "under /settings");
});
test("a page the role doesn't open is a 403 page, and a page that fails is caught so the menu stays", () => {
const app = read("App.tsx");
assert.match(app, /\{t\("You don't have permission/);
assert.match(app, //);
assert.match(read("main.tsx"), //, "and a crash outside the frame too");
assert.match(read("components/ErrorPage.tsx"), /componentDidUpdate[\s\S]*prev\.resetKey !== this\.props\.resetKey/, "going to another page clears the failure");
});
test("a server, an incident and a status link that are gone say so with the same page", () => {
assert.match(read("pages/ServerDetail.tsx"), /404[\s\S]* {
const css = read("styles.css");
for (const rule of [".error-page", ".error-code", ".error-title", ".error-actions", ".error-card"]) assert.ok(css.includes(rule), rule);
});
});
describe("maybe you were looking for", () => {
const pages = [
{ to: "/", label: "Dashboard" },
{ to: "/servers", label: "Servers" },
{ to: "/alerts", label: "Alerts" },
{ to: "/network/devices", label: "Network devices" },
{ to: "/settings", label: "Settings" },
{ to: "/proxmox", label: "Proxmox" },
{ to: "/uptime-kuma", label: "Uptime Kuma" },
];
const first = (path: string) => suggestPages(path, pages).map((p) => p.to);
test("a part of a name, a misspelling and a page that has moved are all recognised", () => {
assert.deepEqual(first("/server"), ["/servers"]);
assert.equal(first("/sevrers")[0], "/servers");
assert.ok(!suggestPages("/sevrers", [...pages, { to: "/secrets", label: "Secrets" }]).some((p) => p.to === "/secrets"), "a different word two slips away is not a suggestion");
assert.equal(first("/alert")[0], "/alerts");
assert.equal(first("/settings/no-such-tab")[0], "/settings");
assert.equal(first("/network/devicess")[0], "/network/devices");
assert.equal(first("/uptimekuma")[0], "/uptime-kuma");
assert.equal(first("/Proxmox/")[0], "/proxmox");
});
test("a query or a fragment is not part of the address, and an address with nothing like it suggests nothing", () => {
assert.equal(first("/servers?status=offline")[0], "/servers");
assert.deepEqual(first("/zzzz"), []);
assert.deepEqual(first("/"), []);
assert.deepEqual(first("/qx/yw"), []);
});
test("at most three, none twice, best first", () => {
const many = [...pages, { to: "/servers", label: "Servers again" }, { to: "/servers/compare", label: "Compare servers" }, { to: "/servers/x", label: "Server x" }, { to: "/servers/y", label: "Server y" }];
const found = suggestPages("/servers", many).map((p) => p.to);
assert.equal(found.length, 3);
assert.equal(new Set(found).size, 3);
assert.equal(found[0], "/servers");
});
test("the distance between two words counts the single-letter changes", () => {
assert.equal(distance("servers", "servers"), 0);
assert.equal(distance("sevrers", "servers"), 1, "two letters swapped is one slip");
assert.equal(distance("sevrers", "secrets"), 2);
assert.equal(distance("alert", "alerts"), 1);
assert.equal(distance("", "abc"), 3);
});
});