| 1 | import assert from "node:assert/strict"; |
| 2 | import { test } from "node:test"; |
| 3 | |
| 4 | import { |
| 5 | USERNAME_PATTERN, |
| 6 | canonicalUsername, |
| 7 | isActionOwnerName, |
| 8 | isClaimableName, |
| 9 | isReservedName, |
| 10 | isValidNamespace, |
| 11 | isValidUsername, |
| 12 | shownUsername, |
| 13 | } from "./names.ts"; |
| 14 | |
| 15 | test("usernames take letters of either case, digits and single hyphens, up to 39", () => { |
| 16 | for (const good of ["ana", "Ana", "ANA-Lopez", "a", "A1", "x".repeat(39)]) assert.ok(isValidUsername(good), good); |
| 17 | for (const bad of ["", "-Ana", "Ana-", "An--a", "An_a", "Ana.B", "Ana B", "x".repeat(40), "Ána"]) assert.ok(!isValidUsername(bad), bad); |
| 18 | }); |
| 19 | |
| 20 | test("reserved names are reserved in any case", () => { |
| 21 | for (const name of ["G1T", "G1t-Agent", "Ghost", "Settings", "API", "Features"]) { |
| 22 | assert.ok(isReservedName(name), name); |
| 23 | assert.ok(!isValidUsername(name), name); |
| 24 | } |
| 25 | }); |
| 26 | |
| 27 | test("a username is found lowercased and shown as written", () => { |
| 28 | assert.equal(canonicalUsername(" Ana-Lopez "), "ana-lopez"); |
| 29 | assert.equal(shownUsername({ username: "ana", display_username: "Ana" }), "Ana"); |
| 30 | assert.equal(shownUsername({ username: "ana", displayUsername: "ANA" }), "ANA"); |
| 31 | assert.equal(shownUsername({ username: "ana" }), "ana"); |
| 32 | // A display form that is some other name is never shown. |
| 33 | assert.equal(shownUsername({ username: "ana", display_username: "Bob" }), "ana"); |
| 34 | // Workspace slugs stay lowercase. |
| 35 | assert.ok(!isValidNamespace("Acme")); |
| 36 | }); |
| 37 | |
| 38 | test("the form's pattern says what the rule says", () => { |
| 39 | const pattern = new RegExp(`^(?:${USERNAME_PATTERN})$`); |
| 40 | for (const name of ["Ana", "ana-lopez", "A1"]) assert.ok(pattern.test(name), name); |
| 41 | for (const name of ["-ana", "ana-", "an--a", "an_a"]) assert.ok(!pattern.test(name), name); |
| 42 | }); |
| 43 | |
| 44 | test("well-known GitHub action owners cannot be claimed, but stay valid names", () => { |
| 45 | for (const name of ["actions", "Docker", " aws-actions "]) { |
| 46 | assert.ok(isActionOwnerName(name), name); |
| 47 | assert.ok(!isValidUsername(name), name); |
| 48 | } |
| 49 | assert.ok(!isClaimableName("actions")); |
| 50 | assert.ok(isValidNamespace("actions")); |
| 51 | assert.ok(isClaimableName("actions-team")); |
| 52 | }); |