test(cron): dedupe delivery-target whatsapp stubs and cover sessionKey fallback

This commit is contained in:
Peter Steinberger
2026-02-21 19:59:37 +00:00
parent 04a23f45b7
commit adedacbfe1

View File

@@ -47,6 +47,16 @@ function setMainSessionEntry(entry?: SessionStore[string]) {
vi.mocked(loadSessionStore).mockReturnValue(store);
}
function setWhatsAppAllowFrom(allowFrom: string[]) {
vi.mocked(resolveWhatsAppAccount).mockReturnValue({
allowFrom,
} as unknown as ReturnType<typeof resolveWhatsAppAccount>);
}
function setStoredWhatsAppAllowFrom(allowFrom: string[]) {
vi.mocked(readChannelAllowFromStoreSync).mockReturnValue(allowFrom);
}
async function resolveForAgent(params: {
cfg: OpenClawConfig;
target?: { channel?: "last" | "telegram"; to?: string };
@@ -67,10 +77,8 @@ describe("resolveDeliveryTarget", () => {
lastChannel: "whatsapp",
lastTo: "+15550000099",
});
vi.mocked(resolveWhatsAppAccount).mockReturnValue({
allowFrom: [],
} as unknown as ReturnType<typeof resolveWhatsAppAccount>);
vi.mocked(readChannelAllowFromStoreSync).mockReturnValue(["+15550000001"]);
setWhatsAppAllowFrom([]);
setStoredWhatsAppAllowFrom(["+15550000001"]);
const cfg = makeCfg({ bindings: [] });
const result = await resolveDeliveryTarget(cfg, AGENT_ID, { channel: "last", to: undefined });
@@ -86,10 +94,8 @@ describe("resolveDeliveryTarget", () => {
lastChannel: "whatsapp",
lastTo: "+15550000099",
});
vi.mocked(resolveWhatsAppAccount).mockReturnValue({
allowFrom: [],
} as unknown as ReturnType<typeof resolveWhatsAppAccount>);
vi.mocked(readChannelAllowFromStoreSync).mockReturnValue(["+15550000001"]);
setWhatsAppAllowFrom([]);
setStoredWhatsAppAllowFrom(["+15550000001"]);
const cfg = makeCfg({ bindings: [] });
const result = await resolveDeliveryTarget(cfg, AGENT_ID, {
@@ -226,4 +232,44 @@ describe("resolveDeliveryTarget", () => {
expect(result.channel).toBe(DEFAULT_CHAT_CHANNEL);
expect(result.to).toBeUndefined();
});
it("uses sessionKey thread entry before main session entry", async () => {
vi.mocked(loadSessionStore).mockReturnValue({
"agent:test:main": {
sessionId: "main-session",
updatedAt: 1000,
lastChannel: "telegram",
lastTo: "main-chat",
},
"agent:test:thread:42": {
sessionId: "thread-session",
updatedAt: 2000,
lastChannel: "telegram",
lastTo: "thread-chat",
},
} as SessionStore);
const result = await resolveDeliveryTarget(makeCfg({ bindings: [] }), AGENT_ID, {
channel: "last",
sessionKey: "agent:test:thread:42",
to: undefined,
});
expect(result.channel).toBe("telegram");
expect(result.to).toBe("thread-chat");
});
it("uses channel selection result when no previous session target exists", async () => {
setMainSessionEntry(undefined);
vi.mocked(resolveMessageChannelSelection).mockResolvedValueOnce({ channel: "telegram" });
const result = await resolveForAgent({
cfg: makeCfg({ bindings: [] }),
target: { channel: "last", to: undefined },
});
expect(result.channel).toBe("telegram");
expect(result.to).toBeUndefined();
expect(result.mode).toBe("implicit");
});
});