mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-08 07:01:24 +00:00
refactor: centralize ack reaction removal
This commit is contained in:
@@ -1,6 +1,10 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { describe, expect, it, vi } from "vitest";
|
||||
|
||||
import { shouldAckReaction, shouldAckReactionForWhatsApp } from "./ack-reactions.js";
|
||||
import {
|
||||
removeAckReactionAfterReply,
|
||||
shouldAckReaction,
|
||||
shouldAckReactionForWhatsApp,
|
||||
} from "./ack-reactions.js";
|
||||
|
||||
describe("shouldAckReaction", () => {
|
||||
it("honors direct and group-all scopes", () => {
|
||||
@@ -222,3 +226,44 @@ describe("shouldAckReactionForWhatsApp", () => {
|
||||
).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe("removeAckReactionAfterReply", () => {
|
||||
it("removes only when ack succeeded", async () => {
|
||||
const remove = vi.fn().mockResolvedValue(undefined);
|
||||
const onError = vi.fn();
|
||||
removeAckReactionAfterReply({
|
||||
removeAfterReply: true,
|
||||
ackReactionPromise: Promise.resolve(true),
|
||||
ackReactionValue: "👀",
|
||||
remove,
|
||||
onError,
|
||||
});
|
||||
await new Promise((resolve) => setTimeout(resolve, 0));
|
||||
expect(remove).toHaveBeenCalledTimes(1);
|
||||
expect(onError).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("skips removal when ack did not happen", async () => {
|
||||
const remove = vi.fn().mockResolvedValue(undefined);
|
||||
removeAckReactionAfterReply({
|
||||
removeAfterReply: true,
|
||||
ackReactionPromise: Promise.resolve(false),
|
||||
ackReactionValue: "👀",
|
||||
remove,
|
||||
});
|
||||
await new Promise((resolve) => setTimeout(resolve, 0));
|
||||
expect(remove).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("skips when not configured", async () => {
|
||||
const remove = vi.fn().mockResolvedValue(undefined);
|
||||
removeAckReactionAfterReply({
|
||||
removeAfterReply: false,
|
||||
ackReactionPromise: Promise.resolve(true),
|
||||
ackReactionValue: "👀",
|
||||
remove,
|
||||
});
|
||||
await new Promise((resolve) => setTimeout(resolve, 0));
|
||||
expect(remove).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
|
||||
@@ -53,3 +53,19 @@ export function shouldAckReactionForWhatsApp(params: {
|
||||
shouldBypassMention: params.groupActivated,
|
||||
});
|
||||
}
|
||||
|
||||
export function removeAckReactionAfterReply(params: {
|
||||
removeAfterReply: boolean;
|
||||
ackReactionPromise: Promise<boolean> | null;
|
||||
ackReactionValue: string | null;
|
||||
remove: () => Promise<void>;
|
||||
onError?: (err: unknown) => void;
|
||||
}) {
|
||||
if (!params.removeAfterReply) return;
|
||||
if (!params.ackReactionPromise) return;
|
||||
if (!params.ackReactionValue) return;
|
||||
void params.ackReactionPromise.then((didAck) => {
|
||||
if (!didAck) return;
|
||||
params.remove().catch((err) => params.onError?.(err));
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user