refactor(web): reuse send api + access-control test helpers

This commit is contained in:
Peter Steinberger
2026-02-16 17:22:03 +00:00
parent 94a4dd0189
commit 291275982c
2 changed files with 26 additions and 26 deletions

View File

@@ -24,6 +24,12 @@ async function checkUnauthorizedWorkDmSender() {
});
}
function expectSilentlyBlocked(result: { allowed: boolean }) {
expect(result.allowed).toBe(false);
expect(upsertPairingRequestMock).not.toHaveBeenCalled();
expect(sendMessageMock).not.toHaveBeenCalled();
}
describe("checkInboundAccessControl pairing grace", () => {
async function runPairingGraceCase(messageTimestampMs: number) {
const connectedAtMs = 1_000_000;
@@ -80,10 +86,7 @@ describe("WhatsApp dmPolicy precedence", () => {
});
const result = await checkUnauthorizedWorkDmSender();
expect(result.allowed).toBe(false);
expect(upsertPairingRequestMock).not.toHaveBeenCalled();
expect(sendMessageMock).not.toHaveBeenCalled();
expectSilentlyBlocked(result);
});
it("inherits channel-level dmPolicy when account-level dmPolicy is unset", async () => {
@@ -103,9 +106,6 @@ describe("WhatsApp dmPolicy precedence", () => {
});
const result = await checkUnauthorizedWorkDmSender();
expect(result.allowed).toBe(false);
expect(upsertPairingRequestMock).not.toHaveBeenCalled();
expect(sendMessageMock).not.toHaveBeenCalled();
expectSilentlyBlocked(result);
});
});

View File

@@ -3,6 +3,20 @@ import type { ActiveWebSendOptions } from "../active-listener.js";
import { recordChannelActivity } from "../../infra/channel-activity.js";
import { toWhatsappJid } from "../../utils.js";
function recordWhatsAppOutbound(accountId: string) {
recordChannelActivity({
channel: "whatsapp",
accountId,
direction: "outbound",
});
}
function resolveOutboundMessageId(result: unknown): string {
return typeof result === "object" && result && "key" in result
? String((result as { key?: { id?: string } }).key?.id ?? "unknown")
: "unknown";
}
export function createWebSendApi(params: {
sock: {
sendMessage: (jid: string, content: AnyMessageContent) => Promise<unknown>;
@@ -51,15 +65,8 @@ export function createWebSendApi(params: {
}
const result = await params.sock.sendMessage(jid, payload);
const accountId = sendOptions?.accountId ?? params.defaultAccountId;
recordChannelActivity({
channel: "whatsapp",
accountId,
direction: "outbound",
});
const messageId =
typeof result === "object" && result && "key" in result
? String((result as { key?: { id?: string } }).key?.id ?? "unknown")
: "unknown";
recordWhatsAppOutbound(accountId);
const messageId = resolveOutboundMessageId(result);
return { messageId };
},
sendPoll: async (
@@ -74,15 +81,8 @@ export function createWebSendApi(params: {
selectableCount: poll.maxSelections ?? 1,
},
} as AnyMessageContent);
recordChannelActivity({
channel: "whatsapp",
accountId: params.defaultAccountId,
direction: "outbound",
});
const messageId =
typeof result === "object" && result && "key" in result
? String((result as { key?: { id?: string } }).key?.id ?? "unknown")
: "unknown";
recordWhatsAppOutbound(params.defaultAccountId);
const messageId = resolveOutboundMessageId(result);
return { messageId };
},
sendReaction: async (