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 <noreply@anthropic.com>
63 lines
3.0 KiB
TypeScript
63 lines
3.0 KiB
TypeScript
/**
|
||
* 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");
|
||
});
|
||
});
|