mirror of https://github.com/openclaw/openclaw.git
test: share delivery target session helpers
This commit is contained in:
parent
d7f9035e80
commit
0574ac23d0
|
|
@ -64,6 +64,23 @@ function setMainSessionEntry(entry?: SessionStore[string]) {
|
||||||
vi.mocked(loadSessionStore).mockReturnValue(store);
|
vi.mocked(loadSessionStore).mockReturnValue(store);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function setLastSessionEntry(params: {
|
||||||
|
sessionId: string;
|
||||||
|
lastChannel: string;
|
||||||
|
lastTo: string;
|
||||||
|
lastThreadId?: string;
|
||||||
|
lastAccountId?: string;
|
||||||
|
}) {
|
||||||
|
setMainSessionEntry({
|
||||||
|
sessionId: params.sessionId,
|
||||||
|
updatedAt: 1000,
|
||||||
|
lastChannel: params.lastChannel,
|
||||||
|
lastTo: params.lastTo,
|
||||||
|
...(params.lastThreadId ? { lastThreadId: params.lastThreadId } : {}),
|
||||||
|
...(params.lastAccountId ? { lastAccountId: params.lastAccountId } : {}),
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
function setWhatsAppAllowFrom(allowFrom: string[]) {
|
function setWhatsAppAllowFrom(allowFrom: string[]) {
|
||||||
vi.mocked(resolveWhatsAppAccount).mockReturnValue({
|
vi.mocked(resolveWhatsAppAccount).mockReturnValue({
|
||||||
allowFrom,
|
allowFrom,
|
||||||
|
|
@ -86,11 +103,17 @@ async function resolveForAgent(params: {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async function resolveLastTarget(cfg: OpenClawConfig) {
|
||||||
|
return resolveForAgent({
|
||||||
|
cfg,
|
||||||
|
target: { channel: "last", to: undefined },
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
describe("resolveDeliveryTarget", () => {
|
describe("resolveDeliveryTarget", () => {
|
||||||
it("reroutes implicit whatsapp delivery to authorized allowFrom recipient", async () => {
|
it("reroutes implicit whatsapp delivery to authorized allowFrom recipient", async () => {
|
||||||
setMainSessionEntry({
|
setLastSessionEntry({
|
||||||
sessionId: "sess-w1",
|
sessionId: "sess-w1",
|
||||||
updatedAt: 1000,
|
|
||||||
lastChannel: "whatsapp",
|
lastChannel: "whatsapp",
|
||||||
lastTo: "+15550000099",
|
lastTo: "+15550000099",
|
||||||
});
|
});
|
||||||
|
|
@ -98,16 +121,15 @@ describe("resolveDeliveryTarget", () => {
|
||||||
setStoredWhatsAppAllowFrom(["+15550000001"]);
|
setStoredWhatsAppAllowFrom(["+15550000001"]);
|
||||||
|
|
||||||
const cfg = makeCfg({ bindings: [] });
|
const cfg = makeCfg({ bindings: [] });
|
||||||
const result = await resolveDeliveryTarget(cfg, AGENT_ID, { channel: "last", to: undefined });
|
const result = await resolveLastTarget(cfg);
|
||||||
|
|
||||||
expect(result.channel).toBe("whatsapp");
|
expect(result.channel).toBe("whatsapp");
|
||||||
expect(result.to).toBe("+15550000001");
|
expect(result.to).toBe("+15550000001");
|
||||||
});
|
});
|
||||||
|
|
||||||
it("keeps explicit whatsapp target unchanged", async () => {
|
it("keeps explicit whatsapp target unchanged", async () => {
|
||||||
setMainSessionEntry({
|
setLastSessionEntry({
|
||||||
sessionId: "sess-w2",
|
sessionId: "sess-w2",
|
||||||
updatedAt: 1000,
|
|
||||||
lastChannel: "whatsapp",
|
lastChannel: "whatsapp",
|
||||||
lastTo: "+15550000099",
|
lastTo: "+15550000099",
|
||||||
});
|
});
|
||||||
|
|
@ -220,9 +242,8 @@ describe("resolveDeliveryTarget", () => {
|
||||||
});
|
});
|
||||||
|
|
||||||
it("drops session threadId when destination does not match the previous recipient", async () => {
|
it("drops session threadId when destination does not match the previous recipient", async () => {
|
||||||
setMainSessionEntry({
|
setLastSessionEntry({
|
||||||
sessionId: "sess-2",
|
sessionId: "sess-2",
|
||||||
updatedAt: 1000,
|
|
||||||
lastChannel: "telegram",
|
lastChannel: "telegram",
|
||||||
lastTo: "999999",
|
lastTo: "999999",
|
||||||
lastThreadId: "thread-1",
|
lastThreadId: "thread-1",
|
||||||
|
|
@ -233,9 +254,8 @@ describe("resolveDeliveryTarget", () => {
|
||||||
});
|
});
|
||||||
|
|
||||||
it("keeps session threadId when destination matches the previous recipient", async () => {
|
it("keeps session threadId when destination matches the previous recipient", async () => {
|
||||||
setMainSessionEntry({
|
setLastSessionEntry({
|
||||||
sessionId: "sess-3",
|
sessionId: "sess-3",
|
||||||
updatedAt: 1000,
|
|
||||||
lastChannel: "telegram",
|
lastChannel: "telegram",
|
||||||
lastTo: "123456",
|
lastTo: "123456",
|
||||||
lastThreadId: "thread-2",
|
lastThreadId: "thread-2",
|
||||||
|
|
@ -248,10 +268,7 @@ describe("resolveDeliveryTarget", () => {
|
||||||
it("uses single configured channel when neither explicit nor session channel exists", async () => {
|
it("uses single configured channel when neither explicit nor session channel exists", async () => {
|
||||||
setMainSessionEntry(undefined);
|
setMainSessionEntry(undefined);
|
||||||
|
|
||||||
const result = await resolveForAgent({
|
const result = await resolveLastTarget(makeCfg({ bindings: [] }));
|
||||||
cfg: makeCfg({ bindings: [] }),
|
|
||||||
target: { channel: "last", to: undefined },
|
|
||||||
});
|
|
||||||
expect(result.channel).toBe("telegram");
|
expect(result.channel).toBe("telegram");
|
||||||
expect(result.ok).toBe(false);
|
expect(result.ok).toBe(false);
|
||||||
if (result.ok) {
|
if (result.ok) {
|
||||||
|
|
@ -268,10 +285,7 @@ describe("resolveDeliveryTarget", () => {
|
||||||
new Error("Channel is required when multiple channels are configured: telegram, slack"),
|
new Error("Channel is required when multiple channels are configured: telegram, slack"),
|
||||||
);
|
);
|
||||||
|
|
||||||
const result = await resolveForAgent({
|
const result = await resolveLastTarget(makeCfg({ bindings: [] }));
|
||||||
cfg: makeCfg({ bindings: [] }),
|
|
||||||
target: { channel: "last", to: undefined },
|
|
||||||
});
|
|
||||||
expect(result.channel).toBeUndefined();
|
expect(result.channel).toBeUndefined();
|
||||||
expect(result.to).toBeUndefined();
|
expect(result.to).toBeUndefined();
|
||||||
expect(result.ok).toBe(false);
|
expect(result.ok).toBe(false);
|
||||||
|
|
@ -308,17 +322,13 @@ describe("resolveDeliveryTarget", () => {
|
||||||
});
|
});
|
||||||
|
|
||||||
it("uses main session channel when channel=last and session route exists", async () => {
|
it("uses main session channel when channel=last and session route exists", async () => {
|
||||||
setMainSessionEntry({
|
setLastSessionEntry({
|
||||||
sessionId: "sess-4",
|
sessionId: "sess-4",
|
||||||
updatedAt: 1000,
|
|
||||||
lastChannel: "telegram",
|
lastChannel: "telegram",
|
||||||
lastTo: "987654",
|
lastTo: "987654",
|
||||||
});
|
});
|
||||||
|
|
||||||
const result = await resolveForAgent({
|
const result = await resolveLastTarget(makeCfg({ bindings: [] }));
|
||||||
cfg: makeCfg({ bindings: [] }),
|
|
||||||
target: { channel: "last", to: undefined },
|
|
||||||
});
|
|
||||||
|
|
||||||
expect(result.channel).toBe("telegram");
|
expect(result.channel).toBe("telegram");
|
||||||
expect(result.to).toBe("987654");
|
expect(result.to).toBe("987654");
|
||||||
|
|
@ -326,9 +336,8 @@ describe("resolveDeliveryTarget", () => {
|
||||||
});
|
});
|
||||||
|
|
||||||
it("explicit delivery.accountId overrides session-derived accountId", async () => {
|
it("explicit delivery.accountId overrides session-derived accountId", async () => {
|
||||||
setMainSessionEntry({
|
setLastSessionEntry({
|
||||||
sessionId: "sess-5",
|
sessionId: "sess-5",
|
||||||
updatedAt: 1000,
|
|
||||||
lastChannel: "telegram",
|
lastChannel: "telegram",
|
||||||
lastTo: "chat-999",
|
lastTo: "chat-999",
|
||||||
lastAccountId: "default",
|
lastAccountId: "default",
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue