mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-10 04:52:43 +00:00
refactor: unify inbound debounce policy and split gateway/models helpers
This commit is contained in:
61
src/channels/inbound-debounce-policy.test.ts
Normal file
61
src/channels/inbound-debounce-policy.test.ts
Normal file
@@ -0,0 +1,61 @@
|
||||
import { describe, expect, it, vi } from "vitest";
|
||||
import {
|
||||
createChannelInboundDebouncer,
|
||||
shouldDebounceTextInbound,
|
||||
} from "./inbound-debounce-policy.js";
|
||||
|
||||
describe("shouldDebounceTextInbound", () => {
|
||||
it("rejects blank text, media, and control commands", () => {
|
||||
const cfg = {} as Parameters<typeof shouldDebounceTextInbound>[0]["cfg"];
|
||||
|
||||
expect(shouldDebounceTextInbound({ text: " ", cfg })).toBe(false);
|
||||
expect(shouldDebounceTextInbound({ text: "hello", cfg, hasMedia: true })).toBe(false);
|
||||
expect(shouldDebounceTextInbound({ text: "/status", cfg })).toBe(false);
|
||||
});
|
||||
|
||||
it("accepts normal text when debounce is allowed", () => {
|
||||
const cfg = {} as Parameters<typeof shouldDebounceTextInbound>[0]["cfg"];
|
||||
expect(shouldDebounceTextInbound({ text: "hello there", cfg })).toBe(true);
|
||||
expect(shouldDebounceTextInbound({ text: "hello there", cfg, allowDebounce: false })).toBe(
|
||||
false,
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
describe("createChannelInboundDebouncer", () => {
|
||||
it("resolves per-channel debounce and forwards callbacks", async () => {
|
||||
vi.useFakeTimers();
|
||||
try {
|
||||
const flushed: string[][] = [];
|
||||
const cfg = {
|
||||
messages: {
|
||||
inbound: {
|
||||
debounceMs: 10,
|
||||
byChannel: {
|
||||
slack: 25,
|
||||
},
|
||||
},
|
||||
},
|
||||
} as Parameters<typeof createChannelInboundDebouncer<{ id: string }>>[0]["cfg"];
|
||||
|
||||
const { debounceMs, debouncer } = createChannelInboundDebouncer<{ id: string }>({
|
||||
cfg,
|
||||
channel: "slack",
|
||||
buildKey: (item) => item.id,
|
||||
onFlush: async (items) => {
|
||||
flushed.push(items.map((entry) => entry.id));
|
||||
},
|
||||
});
|
||||
|
||||
expect(debounceMs).toBe(25);
|
||||
|
||||
await debouncer.enqueue({ id: "a" });
|
||||
await debouncer.enqueue({ id: "a" });
|
||||
await vi.advanceTimersByTimeAsync(30);
|
||||
|
||||
expect(flushed).toEqual([["a", "a"]]);
|
||||
} finally {
|
||||
vi.useRealTimers();
|
||||
}
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user