refactor: centralize ack reaction removal

This commit is contained in:
Peter Steinberger
2026-01-23 22:29:47 +00:00
parent ed05152cb1
commit cb8c8fee9a
10 changed files with 140 additions and 62 deletions

View File

@@ -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();
});
});

View File

@@ -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));
});
}