Validate and deduplicate custom difficulty presets
Some checks failed
Deploy MineGeo to Dokploy / deploy (push) Failing after 6m4s
Some checks failed
Deploy MineGeo to Dokploy / deploy (push) Failing after 6m4s
This commit is contained in:
86
tests/difficulty-presets.test.ts
Normal file
86
tests/difficulty-presets.test.ts
Normal file
@@ -0,0 +1,86 @@
|
||||
import assert from "node:assert/strict";
|
||||
import { test } from "node:test";
|
||||
import { customDifficultySchema } from "../shared/protocol";
|
||||
import { mergeDifficultyPresets } from "../shared/difficulty-presets";
|
||||
|
||||
const rules = { seconds: 90, zoom: true };
|
||||
test("difficulty names accept normal text and count Unicode characters consistently with PostgreSQL", () => {
|
||||
for (const name of [
|
||||
"Normal",
|
||||
"Speed 90",
|
||||
"Тест",
|
||||
"中文",
|
||||
"😎😎",
|
||||
"😎".repeat(40),
|
||||
"a".repeat(40),
|
||||
"a\\b",
|
||||
])
|
||||
assert.equal(
|
||||
customDifficultySchema.safeParse({ name, ...rules }).success,
|
||||
true,
|
||||
name,
|
||||
);
|
||||
assert.equal(
|
||||
customDifficultySchema.parse({ name: " My challenge ", ...rules }).name,
|
||||
"My challenge",
|
||||
);
|
||||
for (const name of [
|
||||
"",
|
||||
"a",
|
||||
"😎",
|
||||
"a".repeat(41),
|
||||
"😎".repeat(41),
|
||||
"x\nname",
|
||||
"x\u0085name",
|
||||
"x\u202ename",
|
||||
"x\u2066name",
|
||||
])
|
||||
assert.equal(
|
||||
customDifficultySchema.safeParse({ name, ...rules }).success,
|
||||
false,
|
||||
);
|
||||
});
|
||||
|
||||
test("difficulty rules enforce their supported range and strict request shape", () => {
|
||||
for (const seconds of [15, 90, 600])
|
||||
assert.equal(
|
||||
customDifficultySchema.safeParse({ name: "Fixture", ...rules, seconds })
|
||||
.success,
|
||||
true,
|
||||
);
|
||||
for (const seconds of [0, 14, 16, 601, 15.5, NaN, Infinity, "90"])
|
||||
assert.equal(
|
||||
customDifficultySchema.safeParse({ name: "Fixture", ...rules, seconds })
|
||||
.success,
|
||||
false,
|
||||
);
|
||||
assert.equal(
|
||||
customDifficultySchema.safeParse({
|
||||
name: "Fixture",
|
||||
...rules,
|
||||
owner: "forged",
|
||||
}).success,
|
||||
false,
|
||||
);
|
||||
});
|
||||
|
||||
test("a delayed preset listing cannot erase a newly saved preset or duplicate it", async () => {
|
||||
const old = { id: "old", name: "Old", ...rules };
|
||||
const saved = { id: "saved", name: "New", ...rules };
|
||||
let release!: (presets: (typeof old)[]) => void;
|
||||
const response = new Promise<(typeof old)[]>((resolve) => {
|
||||
release = resolve;
|
||||
});
|
||||
let presets = [old];
|
||||
const loading = response.then((snapshot) => {
|
||||
presets = mergeDifficultyPresets(presets, snapshot);
|
||||
});
|
||||
presets = mergeDifficultyPresets(presets, [saved]);
|
||||
release([old]);
|
||||
await loading;
|
||||
assert.deepEqual(
|
||||
presets.map((p) => p.id),
|
||||
["saved", "old"],
|
||||
);
|
||||
assert.equal(mergeDifficultyPresets(presets, [saved]).length, 2);
|
||||
});
|
||||
Reference in New Issue
Block a user