mirror of
https://github.com/hansjone/oclaw.git
synced 2026-10-11 04:10:44 +08:00
同时收敛启动与运维脚本默认行为(含 wiki worker)、更新 Admin 可观测性与相关测试,降低首轮时延并提高运行稳定性。 Made-with: Cursor
50 lines
1.6 KiB
TypeScript
50 lines
1.6 KiB
TypeScript
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
|
import { createLoopRateLimiter } from "./loop-rate-limiter.js";
|
|
|
|
describe("createLoopRateLimiter", () => {
|
|
beforeEach(() => {
|
|
vi.useFakeTimers();
|
|
});
|
|
|
|
afterEach(() => {
|
|
vi.useRealTimers();
|
|
});
|
|
|
|
it("allows messages below the threshold", () => {
|
|
const limiter = createLoopRateLimiter({ windowMs: 10_000, maxHits: 3 });
|
|
limiter.record("conv:1");
|
|
limiter.record("conv:1");
|
|
expect(limiter.isRateLimited("conv:1")).toBe(false);
|
|
});
|
|
|
|
it("rate limits at the threshold", () => {
|
|
const limiter = createLoopRateLimiter({ windowMs: 10_000, maxHits: 3 });
|
|
limiter.record("conv:1");
|
|
limiter.record("conv:1");
|
|
limiter.record("conv:1");
|
|
expect(limiter.isRateLimited("conv:1")).toBe(true);
|
|
});
|
|
|
|
it("does not cross-contaminate conversations", () => {
|
|
const limiter = createLoopRateLimiter({ windowMs: 10_000, maxHits: 2 });
|
|
limiter.record("conv:1");
|
|
limiter.record("conv:1");
|
|
expect(limiter.isRateLimited("conv:1")).toBe(true);
|
|
expect(limiter.isRateLimited("conv:2")).toBe(false);
|
|
});
|
|
|
|
it("resets after the time window expires", () => {
|
|
const limiter = createLoopRateLimiter({ windowMs: 5_000, maxHits: 2 });
|
|
limiter.record("conv:1");
|
|
limiter.record("conv:1");
|
|
expect(limiter.isRateLimited("conv:1")).toBe(true);
|
|
|
|
vi.advanceTimersByTime(6_000);
|
|
expect(limiter.isRateLimited("conv:1")).toBe(false);
|
|
});
|
|
|
|
it("returns false for unknown conversations", () => {
|
|
const limiter = createLoopRateLimiter();
|
|
expect(limiter.isRateLimited("unknown")).toBe(false);
|
|
});
|
|
});
|