Skip to content

Commit c5e534a

Browse files
committed
Tests, Formatting, Lints, & Edge Cases
Oops, forgot to finish actually implementing the edge case for when a generated name is too long. Also, formatting, tests, and lints.
1 parent d0e3770 commit c5e534a

File tree

4 files changed

+117
-125
lines changed

4 files changed

+117
-125
lines changed

src/client/UsernameInput.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@ import { customElement, property, state } from "lit/decorators.js";
33
import { v4 as uuidv4 } from "uuid";
44
import { translateText } from "../client/Utils";
55
import { UserSettings } from "../core/game/UserSettings";
6+
import { getRandomUsername } from "../core/utilities/UsernameGenerator";
67
import {
78
MAX_USERNAME_LENGTH,
89
validateUsername,
910
} from "../core/validations/username";
10-
import { getRandomUsername } from "../core/utilities/UsernameGenerator";
1111

1212
const usernameKey: string = "username";
1313

@@ -95,7 +95,7 @@ export class UsernameInput extends LitElement {
9595
}
9696

9797
private generateNewUsername(): string {
98-
const newUsername = getRandomUsername(Math.random())
98+
const newUsername = getRandomUsername(Math.random());
9999
this.storeUsername(newUsername);
100100
return newUsername;
101101
}
Lines changed: 85 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -1,104 +1,103 @@
1-
import { simpleHash } from "../Util"
2-
3-
const PLURAL_NOUN = Symbol("plural!")
4-
const NOUN = Symbol("noun!")
1+
const PLURAL_NOUN = Symbol("plural!");
2+
const NOUN = Symbol("noun!");
53

64
const names = [
7-
["World Famous", NOUN],
8-
["Comically Large", NOUN],
9-
["Comically Small", NOUN],
10-
["Clearance Aisle", PLURAL_NOUN],
11-
[NOUN, "For Hire"],
12-
["Suspicious", NOUN],
13-
["Sopping Wet", PLURAL_NOUN],
14-
["Smelly", NOUN],
15-
["Friendly", NOUN],
16-
["Tardy", NOUN],
17-
["Evil", NOUN],
18-
[PLURAL_NOUN, "That Bite"],
19-
["Malicious", NOUN],
20-
["Spiteful", NOUN],
21-
["Mister", NOUN],
22-
["Alternate", NOUN,"Universe"],
23-
[NOUN, "Island"],
24-
[NOUN, "Kingdom"],
25-
[NOUN, "Empire"],
26-
[NOUN, "Dynasty"],
27-
[NOUN, "Cartel"],
28-
[NOUN, "Cabal"],
29-
["Not Too Fond Of", PLURAL_NOUN],
30-
["Honk For", PLURAL_NOUN],
31-
["Canonically Evil", NOUN],
32-
["Limited Edition", NOUN],
33-
[NOUN, "Scientist"],
34-
["Famous", NOUN, "Collection"],
35-
["Supersonic", NOUN, "Spaceship"],
36-
["Patent Pending", NOUN],
37-
["Patented", NOUN],
38-
["Space", NOUN],
39-
["Secret", NOUN, "Agenda"],
40-
[PLURAL_NOUN, "in my walls"],
41-
["The", PLURAL_NOUN, "are SPIES"],
42-
["Traveling", NOUN, "Circus"]
43-
]
5+
["World Famous", NOUN],
6+
["Comically Large", NOUN],
7+
["Comically Small", NOUN],
8+
["Clearance Aisle", PLURAL_NOUN],
9+
[NOUN, "For Hire"],
10+
["Suspicious", NOUN],
11+
["Sopping Wet", PLURAL_NOUN],
12+
["Smelly", NOUN],
13+
["Friendly", NOUN],
14+
["Tardy", NOUN],
15+
["Evil", NOUN],
16+
[PLURAL_NOUN, "That Bite"],
17+
["Malicious", NOUN],
18+
["Spiteful", NOUN],
19+
["Mister", NOUN],
20+
["Alternate", NOUN, "Universe"],
21+
[NOUN, "Island"],
22+
[NOUN, "Kingdom"],
23+
[NOUN, "Empire"],
24+
[NOUN, "Dynasty"],
25+
[NOUN, "Cartel"],
26+
[NOUN, "Cabal"],
27+
["Not Too Fond Of", PLURAL_NOUN],
28+
["Honk For", PLURAL_NOUN],
29+
["Canonically Evil", NOUN],
30+
["Limited Edition", NOUN],
31+
[NOUN, "Scientist"],
32+
["Famous", NOUN, "Collection"],
33+
["Supersonic", NOUN, "Spaceship"],
34+
["Patent Pending", NOUN],
35+
["Patented", NOUN],
36+
["Space", NOUN],
37+
["Secret", NOUN, "Agenda"],
38+
[PLURAL_NOUN, "in my walls"],
39+
["The", PLURAL_NOUN, "are SPIES"],
40+
["Traveling", NOUN, "Circus"],
41+
];
4442

4543
const nouns = [
46-
"Snail",
47-
"Cow",
48-
"Giraffe",
49-
"Donkey",
50-
"Horse",
51-
"Mushroom",
52-
"Salad",
53-
"Kitten",
54-
"Fork",
55-
"Apple",
56-
"Pancake",
57-
"Tree",
58-
"Fern",
59-
"Seashell",
60-
"Turtle",
61-
"Casserole",
62-
"Gnome",
63-
"Frog",
64-
]
44+
"Snail",
45+
"Cow",
46+
"Giraffe",
47+
"Donkey",
48+
"Horse",
49+
"Mushroom",
50+
"Salad",
51+
"Kitten",
52+
"Fork",
53+
"Apple",
54+
"Pancake",
55+
"Tree",
56+
"Fern",
57+
"Seashell",
58+
"Turtle",
59+
"Casserole",
60+
"Gnome",
61+
"Frog",
62+
];
6563

6664
function isSeedAcceptable(sanitizedSeed: number) {
67-
let template = names[sanitizedSeed % names.length];
68-
let noun = nouns[Math.floor(sanitizedSeed / names.length) % nouns.length];
65+
const template = names[sanitizedSeed % names.length];
66+
const noun = nouns[Math.floor(sanitizedSeed / names.length) % nouns.length];
6967

70-
let totalLength =
71-
template.map((v) => ((v as any)?.length ?? 0)).reduce((a,b) => a + b)
72-
+ template.length
73-
+ noun.length;
68+
const totalLength =
69+
template.map((v) => (v as any)?.length ?? 0).reduce((a, b) => a + b) +
70+
template.length +
71+
noun.length;
7472

75-
return totalLength <= 26
73+
return totalLength <= 26;
7674
}
7775
/**
7876
* Generate a random username based on a numeric seed
7977
* @param seed - the seed to use to select a username
8078
* @returns a string suitable for a player username
8179
*/
82-
export function getRandomUsername(seed: number) : string {
83-
let sanitizedSeed = Math.floor((seed * 2999) % (names.length * nouns.length));
84-
let template = names[sanitizedSeed % names.length];
85-
let noun = nouns[Math.floor(sanitizedSeed / names.length) % nouns.length];
86-
let result: [string?] = [];
80+
export function getRandomUsername(seed: number): string {
81+
let sanitizedSeed = Math.floor((seed * 2999) % (names.length * nouns.length));
8782

88-
while (!isSeedAcceptable(sanitizedSeed)) {
89-
sanitizedSeed += 1;
90-
}
83+
while (!isSeedAcceptable(sanitizedSeed)) {
84+
sanitizedSeed += 1;
85+
}
9186

92-
// Convert template to some somewhat-legible word string
93-
for (let step of template) {
94-
if (step == PLURAL_NOUN) {
95-
result.push(`${noun}s`);
96-
} else if (step == NOUN) {
97-
result.push(noun);
98-
} else {
99-
result.push(step.toString());
100-
}
87+
const template = names[sanitizedSeed % names.length];
88+
const noun = nouns[Math.floor(sanitizedSeed / names.length) % nouns.length];
89+
const result: [string?] = [];
90+
91+
// Convert template to some somewhat-legible word string
92+
for (const step of template) {
93+
if (step === PLURAL_NOUN) {
94+
result.push(`${noun}s`);
95+
} else if (step === NOUN) {
96+
result.push(noun);
97+
} else {
98+
result.push(step.toString());
10199
}
100+
}
102101

103-
return result.join(" ")
104-
}
102+
return result.join(" ");
103+
}

src/core/validations/username.ts

Lines changed: 16 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,12 @@ import { translateText } from "../../client/Utils";
1313
import { simpleHash } from "../Util";
1414
import { getRandomUsername } from "../utilities/UsernameGenerator";
1515

16-
const customDataset = new DataSet()
17-
.addAll(englishDataset)
18-
.addPhrase((phrase) =>
19-
phrase.setMetadata({ originalWord: 'nigg' })
16+
const customDataset = new DataSet().addAll(englishDataset).addPhrase((phrase) =>
17+
phrase
18+
.setMetadata({ originalWord: "nigg" })
2019
/* Not used by any english words */
21-
.addPattern(pattern`niqq`))
20+
.addPattern(pattern`niqq`),
21+
);
2222

2323
const matcher = new RegExpMatcher({
2424
...customDataset.build(),
@@ -29,34 +29,24 @@ const matcher = new RegExpMatcher({
2929
skipNonAlphabeticTransformer(),
3030
toAsciiLowerCaseTransformer(),
3131
collapseDuplicatesTransformer({
32-
customThresholds: new Map([
33-
['b', 2],
34-
['e', 2],
35-
['o', 2],
36-
['l', 2],
37-
['s', 2],
38-
['g', 2],
39-
['q', 2]
40-
]),
41-
})
42-
]
32+
customThresholds: new Map([
33+
["b", 2],
34+
["e", 2],
35+
["o", 2],
36+
["l", 2],
37+
["s", 2],
38+
["g", 2],
39+
["q", 2],
40+
]),
41+
}),
42+
],
4343
});
4444

4545
export const MIN_USERNAME_LENGTH = 3;
4646
export const MAX_USERNAME_LENGTH = 27;
4747

4848
const validPattern = /^[a-zA-Z0-9_[\] 🐈🍀üÜ]+$/u;
4949

50-
const shadowNames = [
51-
"NicePeopleOnly",
52-
"BeKindPlz",
53-
"LearningManners",
54-
"StayClassy",
55-
"BeNicer",
56-
"NeedHugs",
57-
"MakeFriends",
58-
];
59-
6050
export function fixProfaneUsername(username: string): string {
6151
if (isProfaneUsername(username)) {
6252
return getRandomUsername(simpleHash(username));

tests/Censor.test.ts

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,18 @@
11
// Mocking the obscenity library to control its behavior in tests.
22
jest.mock("obscenity", () => {
33
return {
4+
DataSet: class {
5+
constructor() {}
6+
addAll() {
7+
return this;
8+
}
9+
addPhrase() {
10+
return this;
11+
}
12+
build() {
13+
return {};
14+
}
15+
},
416
RegExpMatcher: class {
517
private dummy: string[] = ["foo", "bar", "leet", "code"];
618
constructor(_opts: any) {}
@@ -22,6 +34,7 @@ jest.mock("obscenity", () => {
2234
resolveConfusablesTransformer: () => ({}),
2335
resolveLeetSpeakTransformer: () => ({}),
2436
skipNonAlphabeticTransformer: () => ({}),
37+
toAsciiLowerCaseTransformer: () => ({}),
2538
};
2639
});
2740

@@ -41,16 +54,6 @@ import {
4154
} from "../src/core/validations/username";
4255

4356
describe("username.ts functions", () => {
44-
const shadowNames = [
45-
"NicePeopleOnly",
46-
"BeKindPlz",
47-
"LearningManners",
48-
"StayClassy",
49-
"BeNicer",
50-
"NeedHugs",
51-
"MakeFriends",
52-
];
53-
5457
describe("isProfaneUsername & fixProfaneUsername with leet decoding (mocked)", () => {
5558
test.each([
5659
{ username: "l33t", profane: true }, // decodes to "leet"
@@ -76,7 +79,7 @@ describe("username.ts functions", () => {
7679
expect(fixed).toBe(username);
7780
} else {
7881
// When profane: result should be one of shadowNames
79-
expect(shadowNames).toContain(fixed);
82+
expect(fixed).not.toBe(username);
8083
}
8184
});
8285
});

0 commit comments

Comments
 (0)