79 lines
2.4 KiB
TypeScript
79 lines
2.4 KiB
TypeScript
import { describe, expect, it } from "vitest";
|
|
|
|
import {
|
|
getRankPermissionColumn,
|
|
toPermissionValue,
|
|
splitRankUpdateFields,
|
|
RANK_GENERAL_FIELDS,
|
|
} from "./permission-ranks";
|
|
|
|
describe("getRankPermissionColumn", () => {
|
|
it("returns rank_N for valid rank", () => {
|
|
expect(getRankPermissionColumn(1)).toBe("rank_1");
|
|
expect(getRankPermissionColumn(255)).toBe("rank_255");
|
|
});
|
|
|
|
it("throws for invalid rank", () => {
|
|
expect(() => getRankPermissionColumn(0)).toThrow("Invalid rank id");
|
|
expect(() => getRankPermissionColumn(256)).toThrow("Invalid rank id");
|
|
expect(() => getRankPermissionColumn(1.5)).toThrow("Invalid rank id");
|
|
expect(() => getRankPermissionColumn(-1)).toThrow("Invalid rank id");
|
|
});
|
|
});
|
|
|
|
describe("toPermissionValue", () => {
|
|
it("returns numeric value for valid input", () => {
|
|
expect(toPermissionValue(5)).toBe(5);
|
|
expect(toPermissionValue("10")).toBe(10);
|
|
expect(toPermissionValue("255")).toBe(255);
|
|
});
|
|
|
|
it("clamps to 0-255 range", () => {
|
|
expect(toPermissionValue(-1)).toBe(0);
|
|
expect(toPermissionValue(256)).toBe(255);
|
|
expect(toPermissionValue(999)).toBe(255);
|
|
});
|
|
|
|
it("returns 0 for non-numeric strings", () => {
|
|
expect(toPermissionValue("abc")).toBe(0);
|
|
expect(toPermissionValue(NaN)).toBe(0);
|
|
});
|
|
});
|
|
|
|
describe("splitRankUpdateFields", () => {
|
|
it("splits rank general fields from permission fields", () => {
|
|
const { rankFields, permissionFields } = splitRankUpdateFields({
|
|
rank_name: "Moderator",
|
|
level: 5,
|
|
cmd_ban: "1",
|
|
cmd_kick: "1",
|
|
});
|
|
expect(rankFields).toEqual({ rank_name: "Moderator", level: 5 });
|
|
expect(permissionFields).toEqual({ cmd_ban: 1, cmd_kick: 1 });
|
|
});
|
|
|
|
it("skips id field and invalid keys", () => {
|
|
const { rankFields, permissionFields } = splitRankUpdateFields({
|
|
id: 5,
|
|
"": "test",
|
|
});
|
|
expect(rankFields).toEqual({});
|
|
expect(permissionFields).toEqual({});
|
|
});
|
|
|
|
it("returns empty objects for empty input", () => {
|
|
const { rankFields, permissionFields } = splitRankUpdateFields({});
|
|
expect(rankFields).toEqual({});
|
|
expect(permissionFields).toEqual({});
|
|
});
|
|
});
|
|
|
|
describe("RANK_GENERAL_FIELDS", () => {
|
|
it("contains expected fields", () => {
|
|
expect(RANK_GENERAL_FIELDS.has("rank_name")).toBe(true);
|
|
expect(RANK_GENERAL_FIELDS.has("badge")).toBe(true);
|
|
expect(RANK_GENERAL_FIELDS.has("level")).toBe(true);
|
|
expect(RANK_GENERAL_FIELDS.has("nonexistent")).toBe(false);
|
|
});
|
|
});
|