Skip to content
33 linesCodeBlameRaw
1import assert from "node:assert/strict";
2import { test } from "node:test";
3
4import { USERNAME_PATTERN, canonicalUsername, isReservedName, isValidNamespace, isValidUsername, shownUsername } from "./names.ts";
5
6test("usernames take letters of either case, digits and single hyphens, up to 39", () => {
7 for (const good of ["ana", "Ana", "ANA-Lopez", "a", "A1", "x".repeat(39)]) assert.ok(isValidUsername(good), good);
8 for (const bad of ["", "-Ana", "Ana-", "An--a", "An_a", "Ana.B", "Ana B", "x".repeat(40), "Ána"]) assert.ok(!isValidUsername(bad), bad);
9});
10
11test("reserved names are reserved in any case", () => {
12 for (const name of ["G1T", "G1t-Agent", "Ghost", "Settings", "API"]) {
13 assert.ok(isReservedName(name), name);
14 assert.ok(!isValidUsername(name), name);
15 }
16});
17
18test("a username is found lowercased and shown as written", () => {
19 assert.equal(canonicalUsername(" Ana-Lopez "), "ana-lopez");
20 assert.equal(shownUsername({ username: "ana", display_username: "Ana" }), "Ana");
21 assert.equal(shownUsername({ username: "ana", displayUsername: "ANA" }), "ANA");
22 assert.equal(shownUsername({ username: "ana" }), "ana");
23 // A display form that is some other name is never shown.
24 assert.equal(shownUsername({ username: "ana", display_username: "Bob" }), "ana");
25 // Workspace slugs stay lowercase.
26 assert.ok(!isValidNamespace("Acme"));
27});
28
29test("the form's pattern says what the rule says", () => {
30 const pattern = new RegExp(`^(?:${USERNAME_PATTERN})$`);
31 for (const name of ["Ana", "ana-lopez", "A1"]) assert.ok(pattern.test(name), name);
32 for (const name of ["-ana", "ana-", "an--a", "an_a"]) assert.ok(!pattern.test(name), name);
33});