From 75f060049f6b155c3991a1aa1e75f9e6eda0e7ae Mon Sep 17 00:00:00 2001 From: Bobban Rydh Date: Sat, 31 Oct 2026 22:29:27 +0100 Subject: [PATCH] Generator: make several server names at once A "How many" field (1-20) next to Generate gives that many different names, none already used by a server and none twice; a list with a Copy button per name and a "Copy all" appears when more than one is made. Co-Authored-By: Claude Sonnet 5.5 --- README.md | 4 +- server/test/generatorNames.test.ts | 62 ++++++++++++++++++++++++++++++ web/src/components/CopyButton.tsx | 4 +- web/src/pages/Generator.tsx | 46 ++++++++++++++++++---- web/src/utils/generators.ts | 38 ++++++++++++++---- 5 files changed, 135 insertions(+), 19 deletions(-) create mode 100644 server/test/generatorNames.test.ts diff --git a/README.md b/README.md index 2e4fbd4..c322567 100644 --- a/README.md +++ b/README.md @@ -229,7 +229,9 @@ that carries the tag. passwords, which are made in your browser and never stored). Server names are picked from name lists: Swedish girl and boy names, Disney and Pixar characters, Norse mythology and Astrid Lindgren to start with, or "Mixed" for all of them together. A name already used -by a server isn't suggested. Admins edit the lists under Settings → Names — add and remove +by a server isn't suggested. "How many" makes up to 20 different names at once (handy when +setting up several machines), each with its own Copy button and a "Copy all" that puts +them one per line on the clipboard. Admins edit the lists under Settings → Names — add and remove names, rename, delete or create lists, put a built-in list back as it was — and can import the most common Swedish names from Skatteverket's open name statistics for girls or boys over the latest one to five years. The import is shown to you first and only diff --git a/server/test/generatorNames.test.ts b/server/test/generatorNames.test.ts new file mode 100644 index 0000000..b2e36f6 --- /dev/null +++ b/server/test/generatorNames.test.ts @@ -0,0 +1,62 @@ +/** + * Server names from the Generator: one at a time, or several different ones in one go. The pure picker lives in the web's own source file and is run here against it. + * + * Run with: npm test --workspace server + */ +import { test, describe } from "node:test"; +import assert from "node:assert/strict"; +import { pathToFileURL } from "node:url"; +import { join } from "node:path"; + +const web = (file: string) => pathToFileURL(join(import.meta.dirname, "../../web/src/utils", file)).href; +const { generateServerName, generateServerNames, MAX_SERVER_NAMES } = (await import(web("generators.ts"))) as typeof import("../../web/src/utils/generators"); + +const pool = ["freja", "elsa", "alice", "maja", "wilma", "alma", "ebba", "lilly", "ella", "saga"]; + +describe("generateServerNames", () => { + test("gives the number asked for, all different, all from the pool", () => { + for (let round = 0; round < 50; round++) { + const names = generateServerNames(pool, [], 6); + assert.equal(names.length, 6); + assert.equal(new Set(names).size, 6); + for (const n of names) assert.ok(pool.includes(n)); + } + }); + + test("leaves out the names a server already has, whatever the case", () => { + for (let round = 0; round < 50; round++) { + const names = generateServerNames(pool, ["Freja", "ELSA", "alice"], 7); + assert.equal(names.length, 7, "exactly the seven that are left"); + assert.deepEqual([...names].sort(), ["alma", "ebba", "ella", "lilly", "maja", "saga", "wilma"]); + } + }); + + test("numbers the rest once the list has run out, still all different and none taken", () => { + const names = generateServerNames(["maja", "saga"], ["maja2"], 6); + assert.equal(names.length, 6); + assert.equal(new Set(names.map((n) => n.toLowerCase())).size, 6); + assert.ok(names.includes("maja") && names.includes("saga"), "the plain ones come first"); + assert.ok(!names.includes("maja2"), "maja2 belongs to a server"); + for (const n of names.slice(2)) assert.match(n, /^(maja|saga)\d+$/); + }); + + test("a name written twice in the pool is still given once", () => { + const names = generateServerNames(["maja", "Maja", "saga"], [], 2); + assert.deepEqual([...names].map((n) => n.toLowerCase()).sort(), ["maja", "saga"]); + }); + + test("asks for too many, too few or nonsense: the count is held to 1–20", () => { + assert.equal(generateServerNames(pool, [], 500).length, MAX_SERVER_NAMES); + assert.equal(generateServerNames(pool, [], 0).length, 1); + assert.equal(generateServerNames(pool, [], -3).length, 1); + assert.equal(generateServerNames(pool, [], Number.NaN).length, 1); + assert.equal(generateServerNames(pool, [], 2.9).length, 2); + }); + + test("an empty pool gives nothing, and the one-name function still works", () => { + assert.deepEqual(generateServerNames([], [], 5), []); + assert.equal(generateServerName([], []), ""); + assert.ok(pool.includes(generateServerName(pool, []))); + assert.equal(generateServerName(["maja"], ["maja"]), "maja2"); + }); +}); diff --git a/web/src/components/CopyButton.tsx b/web/src/components/CopyButton.tsx index 152b71b..a028290 100644 --- a/web/src/components/CopyButton.tsx +++ b/web/src/components/CopyButton.tsx @@ -1,6 +1,6 @@ import { useState } from "react"; -export default function CopyButton({ text }: { text: string }) { +export default function CopyButton({ text, label = "Copy" }: { text: string; label?: string }) { const [copied, setCopied] = useState(false); async function handleCopy() { @@ -15,7 +15,7 @@ export default function CopyButton({ text }: { text: string }) { return ( ); } diff --git a/web/src/pages/Generator.tsx b/web/src/pages/Generator.tsx index aff6b5f..4ab2cc7 100644 --- a/web/src/pages/Generator.tsx +++ b/web/src/pages/Generator.tsx @@ -4,7 +4,8 @@ import { api, type CurrentUser, type NameTheme } from "../api/client"; import CopyButton from "../components/CopyButton"; import { readableError } from "../utils/errors"; import { - generateServerName, + MAX_SERVER_NAMES, + generateServerNames, generateUsername, generatePassword, passwordEntropyBits, @@ -22,7 +23,8 @@ export default function Generator({ user }: { user: CurrentUser }) { const [themes, setThemes] = useState(null); const [themesError, setThemesError] = useState(null); const [theme, setTheme] = useState(MIXED); - const [serverName, setServerName] = useState(""); + const [serverNames, setServerNames] = useState([]); + const [nameCount, setNameCount] = useState(1); useEffect(() => { api.servers @@ -48,7 +50,7 @@ export default function Generator({ user }: { user: CurrentUser }) { }, [themes, theme]); function rollServerName() { - setServerName(generateServerName(pool, existingServerNames)); + setServerNames(generateServerNames(pool, existingServerNames, nameCount)); } // ─── Username ──────────────────────────────────────────────────────── @@ -115,15 +117,43 @@ export default function Generator({ user }: { user: CurrentUser }) { ))}
{themes ? `${pool.length} name${pool.length === 1 ? "" : "s"} to pick from.` : "Loading the lists…"}
-
- - {serverName && } -
+ {serverNames.length > 1 ? ( +
    + {serverNames.map((n) => ( +
  • + {n} + +
  • + ))} +
+ ) : ( +
+ + {serverNames[0] && } +
+ )} + {serverNames.length > 1 && ( +
+ +
+ )} -
+
+
diff --git a/web/src/utils/generators.ts b/web/src/utils/generators.ts index 9bdf8d2..03bb29c 100644 --- a/web/src/utils/generators.ts +++ b/web/src/utils/generators.ts @@ -21,16 +21,38 @@ function pick(list: T[]): T { * fall back to numbering one — maja2, maja3, …. The pools themselves are the name lists under Settings → Names. */ export function generateServerName(pool: string[], existing: string[] = []): string { - if (pool.length === 0) return ""; + return generateServerNames(pool, existing, 1)[0] ?? ""; +} + +/** Most names the Generator makes in one go. */ +export const MAX_SERVER_NAMES = 20; + +/** + * `count` different names, none already in `existing` and none twice (case-insensitively). They're drawn from the pool without + * putting one back; once the pool is used up the rest are numbered ones — maja2, maja3, …. + */ +export function generateServerNames(pool: string[], existing: string[] = [], count = 1): string[] { + if (pool.length === 0) return []; + const wanted = Math.min(MAX_SERVER_NAMES, Math.max(1, Math.floor(count) || 1)); const used = new Set(existing.map((n) => n.toLowerCase())); - const available = pool.filter((n) => !used.has(n.toLowerCase())); - if (available.length > 0) return pick(available); - const base = pick(pool); - for (let suffix = 2; suffix < 1000; suffix++) { - const candidate = `${base}${suffix}`; - if (!used.has(candidate.toLowerCase())) return candidate; + const available = pool.filter((n, i) => !used.has(n.toLowerCase()) && pool.findIndex((m) => m.toLowerCase() === n.toLowerCase()) === i); + const names: string[] = []; + while (names.length < wanted) { + let name: string | undefined; + if (available.length > 0) { + name = available.splice(randomInt(available.length), 1)[0]; + } else { + const base = pick(pool); + for (let suffix = 2; suffix < 1000 && name === undefined; suffix++) { + const candidate = `${base}${suffix}`; + if (!used.has(candidate.toLowerCase())) name = candidate; + } + name ??= `${base}${randomInt(100000)}`; + } + used.add(name.toLowerCase()); + names.push(name); } - return `${base}${randomInt(100000)}`; + return names; } // ─── Usernames ───────────────────────────────────────────────────────────────