refactor(channels): dedupe transport and gateway test scaffolds

This commit is contained in:
Peter Steinberger
2026-02-16 14:52:15 +00:00
parent f717a13039
commit 93ca0ed54f
95 changed files with 4068 additions and 5221 deletions

View File

@@ -12,18 +12,54 @@ import {
describe("web monitor inbox", () => {
installWebMonitorInboxUnitTestHooks();
type InboxOnMessage = NonNullable<Parameters<typeof monitorWebInbox>[0]["onMessage"]>;
async function tick() {
await new Promise((resolve) => setImmediate(resolve));
}
async function startInboxMonitor(onMessage: InboxOnMessage) {
const listener = await monitorWebInbox({
verbose: false,
onMessage,
accountId: DEFAULT_ACCOUNT_ID,
authDir: getAuthDir(),
});
return { listener, sock: getSock() };
}
function buildMessageUpsert(params: {
id: string;
remoteJid: string;
text: string;
timestamp: number;
pushName?: string;
participant?: string;
}) {
return {
type: "notify",
messages: [
{
key: {
id: params.id,
fromMe: false,
remoteJid: params.remoteJid,
participant: params.participant,
},
message: { conversation: params.text },
messageTimestamp: params.timestamp,
pushName: params.pushName,
},
],
};
}
async function expectQuotedReplyContext(quotedMessage: unknown) {
const onMessage = vi.fn(async (msg) => {
await msg.reply("pong");
});
const listener = await monitorWebInbox({ verbose: false, onMessage });
const sock = getSock();
const { listener, sock } = await startInboxMonitor(onMessage);
const upsert = {
type: "notify",
messages: [
@@ -68,25 +104,15 @@ describe("web monitor inbox", () => {
await msg.reply("pong");
});
const listener = await monitorWebInbox({
verbose: false,
onMessage,
accountId: DEFAULT_ACCOUNT_ID,
authDir: getAuthDir(),
});
const sock = getSock();
const { listener, sock } = await startInboxMonitor(onMessage);
expect(sock.sendPresenceUpdate).toHaveBeenCalledWith("available");
const upsert = {
type: "notify",
messages: [
{
key: { id: "abc", fromMe: false, remoteJid: "999@s.whatsapp.net" },
message: { conversation: "ping" },
messageTimestamp: 1_700_000_000,
pushName: "Tester",
},
],
};
const upsert = buildMessageUpsert({
id: "abc",
remoteJid: "999@s.whatsapp.net",
text: "ping",
timestamp: 1_700_000_000,
pushName: "Tester",
});
sock.ev.emit("messages.upsert", upsert);
await tick();
@@ -116,24 +142,14 @@ describe("web monitor inbox", () => {
return;
});
const listener = await monitorWebInbox({
verbose: false,
onMessage,
accountId: DEFAULT_ACCOUNT_ID,
authDir: getAuthDir(),
const { listener, sock } = await startInboxMonitor(onMessage);
const upsert = buildMessageUpsert({
id: "abc",
remoteJid: "999@s.whatsapp.net",
text: "ping",
timestamp: 1_700_000_000,
pushName: "Tester",
});
const sock = getSock();
const upsert = {
type: "notify",
messages: [
{
key: { id: "abc", fromMe: false, remoteJid: "999@s.whatsapp.net" },
message: { conversation: "ping" },
messageTimestamp: 1_700_000_000,
pushName: "Tester",
},
],
};
sock.ev.emit("messages.upsert", upsert);
sock.ev.emit("messages.upsert", upsert);
@@ -149,26 +165,16 @@ describe("web monitor inbox", () => {
return;
});
const listener = await monitorWebInbox({
verbose: false,
onMessage,
accountId: DEFAULT_ACCOUNT_ID,
authDir: getAuthDir(),
});
const sock = getSock();
const { listener, sock } = await startInboxMonitor(onMessage);
const getPNForLID = vi.spyOn(sock.signalRepository.lidMapping, "getPNForLID");
sock.signalRepository.lidMapping.getPNForLID.mockResolvedValueOnce("999:0@s.whatsapp.net");
const upsert = {
type: "notify",
messages: [
{
key: { id: "abc", fromMe: false, remoteJid: "999@lid" },
message: { conversation: "ping" },
messageTimestamp: 1_700_000_000,
pushName: "Tester",
},
],
};
const upsert = buildMessageUpsert({
id: "abc",
remoteJid: "999@lid",
text: "ping",
timestamp: 1_700_000_000,
pushName: "Tester",
});
sock.ev.emit("messages.upsert", upsert);
await tick();
@@ -190,25 +196,15 @@ describe("web monitor inbox", () => {
JSON.stringify("1555"),
);
const listener = await monitorWebInbox({
verbose: false,
onMessage,
accountId: DEFAULT_ACCOUNT_ID,
authDir: getAuthDir(),
});
const sock = getSock();
const { listener, sock } = await startInboxMonitor(onMessage);
const getPNForLID = vi.spyOn(sock.signalRepository.lidMapping, "getPNForLID");
const upsert = {
type: "notify",
messages: [
{
key: { id: "abc", fromMe: false, remoteJid: "555@lid" },
message: { conversation: "ping" },
messageTimestamp: 1_700_000_000,
pushName: "Tester",
},
],
};
const upsert = buildMessageUpsert({
id: "abc",
remoteJid: "555@lid",
text: "ping",
timestamp: 1_700_000_000,
pushName: "Tester",
});
sock.ev.emit("messages.upsert", upsert);
await tick();
@@ -226,30 +222,16 @@ describe("web monitor inbox", () => {
return;
});
const listener = await monitorWebInbox({
verbose: false,
onMessage,
accountId: DEFAULT_ACCOUNT_ID,
authDir: getAuthDir(),
});
const sock = getSock();
const { listener, sock } = await startInboxMonitor(onMessage);
const getPNForLID = vi.spyOn(sock.signalRepository.lidMapping, "getPNForLID");
sock.signalRepository.lidMapping.getPNForLID.mockResolvedValueOnce("444:0@s.whatsapp.net");
const upsert = {
type: "notify",
messages: [
{
key: {
id: "abc",
fromMe: false,
remoteJid: "123@g.us",
participant: "444@lid",
},
message: { conversation: "ping" },
messageTimestamp: 1_700_000_000,
},
],
};
const upsert = buildMessageUpsert({
id: "abc",
remoteJid: "123@g.us",
participant: "444@lid",
text: "ping",
timestamp: 1_700_000_000,
});
sock.ev.emit("messages.upsert", upsert);
await tick();
@@ -277,13 +259,7 @@ describe("web monitor inbox", () => {
}
});
const listener = await monitorWebInbox({
verbose: false,
onMessage,
accountId: DEFAULT_ACCOUNT_ID,
authDir: getAuthDir(),
});
const sock = getSock();
const { listener, sock } = await startInboxMonitor(onMessage);
const upsert = {
type: "notify",
messages: [