Setting ENVIRONMENT_NAME (and optionally ENVIRONMENT_COLOR, a name or hex value) gives the sidebar that colour, adds a name badge next to the title and on the sign-in page, and prefixes the browser tab title, so a dev copy can't be taken for production. Unset, nothing changes. The colour is validated to a plain #rrggbb before it reaches the page's styles. A public GET /api/environment lets the sign-in page use it too. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
86 lines
3.3 KiB
TypeScript
86 lines
3.3 KiB
TypeScript
/**
|
|
* Making a dev installation look different from production (ENVIRONMENT_NAME / ENVIRONMENT_COLOR).
|
|
*
|
|
* Run with: npm test --workspace server
|
|
*/
|
|
import { test, describe, before, after } from "node:test";
|
|
import assert from "node:assert/strict";
|
|
import { mkdtempSync, rmSync } from "node:fs";
|
|
import { tmpdir } from "node:os";
|
|
import { join } from "node:path";
|
|
import type { AddressInfo } from "node:net";
|
|
import type { Server } from "node:http";
|
|
|
|
const dir = mkdtempSync(join(tmpdir(), "hm-environment-"));
|
|
process.env.DATABASE_PATH = join(dir, "test.sqlite");
|
|
process.env.SESSION_DIR = join(dir, "sessions");
|
|
process.env.CREDENTIALS_ENCRYPTION_KEY = "ab".repeat(32);
|
|
process.env.ENVIRONMENT_NAME = "Dev";
|
|
process.env.ENVIRONMENT_COLOR = "teal";
|
|
|
|
const { parseEnvironment } = await import("../src/env.js");
|
|
const { createApp } = await import("../src/app.js");
|
|
|
|
describe("parseEnvironment", () => {
|
|
test("nothing set is a normal installation", () => {
|
|
assert.deepEqual(parseEnvironment(undefined, undefined), { look: null, warning: undefined });
|
|
assert.deepEqual(parseEnvironment(" ", ""), { look: null, warning: undefined });
|
|
});
|
|
|
|
test("a name alone gets the default colour", () => {
|
|
const { look } = parseEnvironment("Dev", undefined);
|
|
assert.equal(look?.name, "Dev");
|
|
assert.match(look?.color ?? "", /^#[0-9a-f]{6}$/);
|
|
});
|
|
|
|
test("colours by name or as hex, in any case, short hex expanded", () => {
|
|
assert.equal(parseEnvironment("x", "Blue").look?.color, "#1d4ed8");
|
|
assert.equal(parseEnvironment("x", "#B45309").look?.color, "#b45309");
|
|
assert.equal(parseEnvironment("x", "#0af").look?.color, "#00aaff");
|
|
});
|
|
|
|
test("a colour alone is accepted, with no name", () => {
|
|
assert.deepEqual(parseEnvironment(undefined, "green").look, { name: "", color: "#15803d" });
|
|
});
|
|
|
|
test("anything that is not a plain colour is refused, never passed on into the page's styles", () => {
|
|
for (const bad of ["red; background:url(//evil)", "url(x)", "#12345", "rgb(1,2,3)", "</style><script>", "#zzzzzz"]) {
|
|
const { look, warning } = parseEnvironment("Dev", bad);
|
|
assert.match(look?.color ?? "", /^#[0-9a-f]{6}$/, `${bad} falls back to a safe colour`);
|
|
assert.ok(warning, `${bad} is reported`);
|
|
}
|
|
});
|
|
|
|
test("the name is cleaned of control characters and capped in length", () => {
|
|
assert.equal(parseEnvironment("De\u0000v\n", undefined).look?.name, "Dev");
|
|
assert.equal(parseEnvironment("x".repeat(100), undefined).look?.name.length, 24);
|
|
});
|
|
});
|
|
|
|
describe("GET /api/environment", () => {
|
|
let server: Server;
|
|
let base = "";
|
|
|
|
before(async () => {
|
|
server = await new Promise<Server>((resolve) => {
|
|
const s = createApp().listen(0, "127.0.0.1", () => resolve(s));
|
|
});
|
|
base = `http://127.0.0.1:${(server.address() as AddressInfo).port}`;
|
|
});
|
|
|
|
after(() => {
|
|
server.close();
|
|
try {
|
|
rmSync(dir, { recursive: true, force: true });
|
|
} catch {
|
|
// Windows keeps the database file locked until the process exits.
|
|
}
|
|
});
|
|
|
|
test("is answered without signing in, so the login page can look the part too", async () => {
|
|
const res = await fetch(`${base}/api/environment`);
|
|
assert.equal(res.status, 200);
|
|
assert.deepEqual(await res.json(), { environment: { name: "Dev", color: "#0f766e" } });
|
|
});
|
|
});
|