| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293 |
- import { describe, expect, it } from "vitest";
- import {
- normalizeConcurrentSessionLimit,
- resolveKeyConcurrentSessionLimit,
- resolveKeyUserConcurrentSessionLimits,
- } from "@/lib/rate-limit/concurrent-session-limit";
- describe("resolveKeyConcurrentSessionLimit", () => {
- const cases: Array<{
- title: string;
- keyLimit: number | null | undefined;
- userLimit: number | null | undefined;
- expected: number;
- }> = [
- { title: "Key > 0 时应优先使用 Key", keyLimit: 10, userLimit: 15, expected: 10 },
- { title: "Key 为 0 时应回退到 User", keyLimit: 0, userLimit: 15, expected: 15 },
- { title: "Key 为 null 时应回退到 User", keyLimit: null, userLimit: 15, expected: 15 },
- { title: "Key 为 undefined 时应回退到 User", keyLimit: undefined, userLimit: 15, expected: 15 },
- {
- title: "Key 为 NaN 时应回退到 User",
- keyLimit: Number.NaN,
- userLimit: 15,
- expected: 15,
- },
- {
- title: "Key 为 Infinity 时应回退到 User",
- keyLimit: Number.POSITIVE_INFINITY,
- userLimit: 15,
- expected: 15,
- },
- { title: "Key < 0 时应回退到 User", keyLimit: -1, userLimit: 15, expected: 15 },
- { title: "Key 为小数时应向下取整", keyLimit: 5.9, userLimit: 15, expected: 5 },
- { title: "Key 小数 < 1 时应回退到 User", keyLimit: 0.9, userLimit: 15, expected: 15 },
- { title: "User 为小数时应向下取整", keyLimit: 0, userLimit: 7.8, expected: 7 },
- {
- title: "Key 与 User 均未设置/无效时应返回 0(无限制)",
- keyLimit: undefined,
- userLimit: null,
- expected: 0,
- },
- {
- title: "Key 为 0 且 User 为 Infinity 时应返回 0(无限制)",
- keyLimit: 0,
- userLimit: Number.POSITIVE_INFINITY,
- expected: 0,
- },
- ];
- for (const testCase of cases) {
- it(testCase.title, () => {
- expect(resolveKeyConcurrentSessionLimit(testCase.keyLimit, testCase.userLimit)).toBe(
- testCase.expected
- );
- });
- }
- });
- describe("normalizeConcurrentSessionLimit", () => {
- const cases: Array<{ title: string; input: number | null | undefined; expected: number }> = [
- { title: "null 应归一化为 0", input: null, expected: 0 },
- { title: "undefined 应归一化为 0", input: undefined, expected: 0 },
- { title: "0 应归一化为 0", input: 0, expected: 0 },
- { title: "负数应归一化为 0", input: -1, expected: 0 },
- { title: "NaN 应归一化为 0", input: Number.NaN, expected: 0 },
- { title: "Infinity 应归一化为 0", input: Number.POSITIVE_INFINITY, expected: 0 },
- { title: "正整数应保持不变", input: 15, expected: 15 },
- { title: "小数应向下取整", input: 7.9, expected: 7 },
- { title: "小数 < 1 应向下取整为 0", input: 0.9, expected: 0 },
- ];
- for (const testCase of cases) {
- it(testCase.title, () => {
- expect(normalizeConcurrentSessionLimit(testCase.input)).toBe(testCase.expected);
- });
- }
- });
- describe("resolveKeyUserConcurrentSessionLimits", () => {
- it("Key 未设置且 User 已设置时:effectiveKeyLimit 应继承 User,且 enabled=true", () => {
- const result = resolveKeyUserConcurrentSessionLimits(0, 15);
- expect(result).toEqual({ effectiveKeyLimit: 15, normalizedUserLimit: 15, enabled: true });
- });
- it("Key 已设置且 User 已设置时:Key 优先,User 保留为 normalizedUserLimit", () => {
- const result = resolveKeyUserConcurrentSessionLimits(10, 15);
- expect(result).toEqual({ effectiveKeyLimit: 10, normalizedUserLimit: 15, enabled: true });
- });
- it("Key/User 均未设置时:enabled=false", () => {
- const result = resolveKeyUserConcurrentSessionLimits(0, null);
- expect(result).toEqual({ effectiveKeyLimit: 0, normalizedUserLimit: 0, enabled: false });
- });
- });
|