mirror of
https://github.com/openclaw/openclaw.git
synced 2026-02-19 18:39:20 -05:00
perf(test): dedupe telegram allowlist and speed twitch probe
This commit is contained in:
@@ -26,9 +26,8 @@ const mockOnAuthenticationFailure = vi.fn((_handler: () => void) => {
|
||||
|
||||
// Connect mock that triggers the registered handler
|
||||
const defaultConnectImpl = async () => {
|
||||
// Simulate successful connection by calling the handler after a delay
|
||||
// Simulate successful connection by calling the handler immediately.
|
||||
if (connectHandler) {
|
||||
await new Promise((resolve) => setTimeout(resolve, 1));
|
||||
connectHandler();
|
||||
}
|
||||
};
|
||||
@@ -114,15 +113,19 @@ describe("probeTwitch", () => {
|
||||
});
|
||||
|
||||
it("times out when connection takes too long", async () => {
|
||||
mockConnect.mockImplementationOnce(() => new Promise(() => {})); // Never resolves
|
||||
vi.useFakeTimers();
|
||||
try {
|
||||
mockConnect.mockImplementationOnce(() => new Promise(() => {})); // Never resolves
|
||||
const resultPromise = probeTwitch(mockAccount, 100);
|
||||
await vi.advanceTimersByTimeAsync(100);
|
||||
const result = await resultPromise;
|
||||
|
||||
const result = await probeTwitch(mockAccount, 100);
|
||||
|
||||
expect(result.ok).toBe(false);
|
||||
expect(result.error).toContain("timeout");
|
||||
|
||||
// Reset mock
|
||||
mockConnect.mockImplementation(defaultConnectImpl);
|
||||
expect(result.ok).toBe(false);
|
||||
expect(result.error).toContain("timeout");
|
||||
} finally {
|
||||
vi.useRealTimers();
|
||||
mockConnect.mockImplementation(defaultConnectImpl);
|
||||
}
|
||||
});
|
||||
|
||||
it("cleans up client even on failure", async () => {
|
||||
@@ -130,7 +133,6 @@ describe("probeTwitch", () => {
|
||||
// Simulate connection failure by calling disconnect handler
|
||||
// onDisconnect signature: (manually: boolean, reason?: Error) => void
|
||||
if (disconnectHandler) {
|
||||
await new Promise((resolve) => setTimeout(resolve, 1));
|
||||
disconnectHandler(false, new Error("Connection failed"));
|
||||
}
|
||||
});
|
||||
@@ -150,7 +152,6 @@ describe("probeTwitch", () => {
|
||||
// Simulate connection failure by calling disconnect handler
|
||||
// onDisconnect signature: (manually: boolean, reason?: Error) => void
|
||||
if (disconnectHandler) {
|
||||
await new Promise((resolve) => setTimeout(resolve, 1));
|
||||
disconnectHandler(false, new Error("Network error"));
|
||||
}
|
||||
});
|
||||
@@ -180,7 +181,6 @@ describe("probeTwitch", () => {
|
||||
// Simulate connection failure by calling disconnect handler
|
||||
// onDisconnect signature: (manually: boolean, reason?: Error) => void
|
||||
if (disconnectHandler) {
|
||||
await new Promise((resolve) => setTimeout(resolve, 1));
|
||||
disconnectHandler(false, "String error" as unknown as Error);
|
||||
}
|
||||
});
|
||||
|
||||
@@ -1197,35 +1197,6 @@ describe("createTelegramBot", () => {
|
||||
expect(payload.ReplyToSender).toBe("Ada");
|
||||
});
|
||||
|
||||
it("matches tg:-prefixed allowFrom entries case-insensitively in group allowlist", async () => {
|
||||
onSpy.mockReset();
|
||||
replySpy.mockReset();
|
||||
loadConfig.mockReturnValue({
|
||||
channels: {
|
||||
telegram: {
|
||||
groupPolicy: "allowlist",
|
||||
allowFrom: ["TG:123456789"],
|
||||
groups: { "*": { requireMention: false } },
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
createTelegramBot({ token: "tok" });
|
||||
const handler = getOnHandler("message") as (ctx: Record<string, unknown>) => Promise<void>;
|
||||
|
||||
await handler({
|
||||
message: {
|
||||
chat: { id: -100123456789, type: "group", title: "Test Group" },
|
||||
from: { id: 123456789, username: "testuser" },
|
||||
text: "hello from prefixed user",
|
||||
date: 1736380800,
|
||||
},
|
||||
me: { username: "openclaw_bot" },
|
||||
getFile: async () => ({ download: async () => new Uint8Array() }),
|
||||
});
|
||||
|
||||
expect(replySpy).toHaveBeenCalled();
|
||||
});
|
||||
it("blocks group messages when groupPolicy allowlist has no groupAllowFrom", async () => {
|
||||
onSpy.mockReset();
|
||||
replySpy.mockReset();
|
||||
|
||||
Reference in New Issue
Block a user