test: cover MEDIA backticks and web media fallback logging
parent
8ea7f9b439
commit
9fbeb2ccd3
|
|
@ -198,6 +198,31 @@ describe("config and templating", () => {
|
||||||
expect(result?.text).toBe("caption before caption after");
|
expect(result?.text).toBe("caption before caption after");
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it("captures MEDIA wrapped in backticks", async () => {
|
||||||
|
const runSpy = vi.spyOn(index, "runCommandWithTimeout").mockResolvedValue({
|
||||||
|
stdout: "MEDIA:`/tmp/pic.png` cool",
|
||||||
|
stderr: "",
|
||||||
|
code: 0,
|
||||||
|
signal: null,
|
||||||
|
killed: false,
|
||||||
|
});
|
||||||
|
const cfg = {
|
||||||
|
inbound: {
|
||||||
|
reply: {
|
||||||
|
mode: "command" as const,
|
||||||
|
command: ["echo", "{{Body}}"],
|
||||||
|
},
|
||||||
|
},
|
||||||
|
};
|
||||||
|
const result = await index.getReplyFromConfig(
|
||||||
|
{ Body: "hi", From: "+1", To: "+2" },
|
||||||
|
undefined,
|
||||||
|
cfg,
|
||||||
|
runSpy,
|
||||||
|
);
|
||||||
|
expect(result?.mediaUrl).toBe("/tmp/pic.png");
|
||||||
|
});
|
||||||
|
|
||||||
it("ignores invalid MEDIA lines with whitespace", async () => {
|
it("ignores invalid MEDIA lines with whitespace", async () => {
|
||||||
const runSpy = vi.spyOn(index, "runCommandWithTimeout").mockResolvedValue({
|
const runSpy = vi.spyOn(index, "runCommandWithTimeout").mockResolvedValue({
|
||||||
stdout: "hello\nMEDIA: not a url with spaces\nrest\n",
|
stdout: "hello\nMEDIA: not a url with spaces\nrest\n",
|
||||||
|
|
|
||||||
|
|
@ -46,6 +46,7 @@ import {
|
||||||
sendMessageWeb,
|
sendMessageWeb,
|
||||||
waitForWaConnection,
|
waitForWaConnection,
|
||||||
} from "./provider-web.js";
|
} from "./provider-web.js";
|
||||||
|
import { monitorWebProvider } from "./index.js";
|
||||||
|
|
||||||
const baileys = (await import(
|
const baileys = (await import(
|
||||||
"@whiskeysockets/baileys"
|
"@whiskeysockets/baileys"
|
||||||
|
|
@ -209,6 +210,49 @@ describe("provider-web", () => {
|
||||||
await listener.close();
|
await listener.close();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it("monitorWebProvider falls back to text when media send fails", async () => {
|
||||||
|
const sendMedia = vi.fn().mockRejectedValue(new Error("boom"));
|
||||||
|
const reply = vi.fn().mockResolvedValue(undefined);
|
||||||
|
const sendComposing = vi.fn();
|
||||||
|
const resolver = vi.fn().mockResolvedValue({
|
||||||
|
text: "hi",
|
||||||
|
mediaUrl: "https://example.com/img.png",
|
||||||
|
});
|
||||||
|
|
||||||
|
let capturedOnMessage: ((msg: any) => Promise<void>) | undefined;
|
||||||
|
const listenerFactory = async (opts: { onMessage: (msg: any) => Promise<void> }) => {
|
||||||
|
capturedOnMessage = opts.onMessage;
|
||||||
|
return { close: vi.fn() };
|
||||||
|
};
|
||||||
|
|
||||||
|
const fetchMock = vi
|
||||||
|
.spyOn(global as any, "fetch")
|
||||||
|
.mockResolvedValue({
|
||||||
|
ok: true,
|
||||||
|
body: true,
|
||||||
|
arrayBuffer: async () => new ArrayBuffer(1024),
|
||||||
|
headers: { get: () => "image/png" },
|
||||||
|
status: 200,
|
||||||
|
} as any);
|
||||||
|
|
||||||
|
await monitorWebProvider(false, listenerFactory as any, false, resolver);
|
||||||
|
|
||||||
|
expect(capturedOnMessage).toBeDefined();
|
||||||
|
await capturedOnMessage?.({
|
||||||
|
body: "hello",
|
||||||
|
from: "+1",
|
||||||
|
to: "+2",
|
||||||
|
id: "msg1",
|
||||||
|
sendComposing,
|
||||||
|
reply,
|
||||||
|
sendMedia,
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(sendMedia).toHaveBeenCalled();
|
||||||
|
expect(reply).toHaveBeenCalledWith("hi");
|
||||||
|
fetchMock.mockRestore();
|
||||||
|
});
|
||||||
|
|
||||||
it("logWebSelfId prints cached E.164 when creds exist", () => {
|
it("logWebSelfId prints cached E.164 when creds exist", () => {
|
||||||
const existsSpy = vi
|
const existsSpy = vi
|
||||||
.spyOn(fsSync, "existsSync")
|
.spyOn(fsSync, "existsSync")
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue