openclaw/src/utils/delivery-context.test.ts

76 lines
1.9 KiB
TypeScript

import { describe, expect, it } from "vitest";
import {
deliveryContextKey,
deliveryContextFromSession,
mergeDeliveryContext,
normalizeDeliveryContext,
} from "./delivery-context.js";
describe("delivery context helpers", () => {
it("normalizes channel/to/accountId and drops empty contexts", () => {
expect(
normalizeDeliveryContext({
channel: " whatsapp ",
to: " +1555 ",
accountId: " acct-1 ",
}),
).toEqual({
channel: "whatsapp",
to: "+1555",
accountId: "acct-1",
});
expect(normalizeDeliveryContext({ channel: " " })).toBeUndefined();
});
it("merges primary values over fallback", () => {
const merged = mergeDeliveryContext(
{ channel: "whatsapp", to: "channel:abc" },
{ channel: "slack", to: "channel:def", accountId: "acct" },
);
expect(merged).toEqual({
channel: "whatsapp",
to: "channel:abc",
accountId: "acct",
});
});
it("builds stable keys only when channel and to are present", () => {
expect(deliveryContextKey({ channel: "whatsapp", to: "+1555" })).toBe(
"whatsapp|+1555|",
);
expect(deliveryContextKey({ channel: "whatsapp" })).toBeUndefined();
expect(
deliveryContextKey({ channel: "whatsapp", to: "+1555", accountId: "acct-1" }),
).toBe("whatsapp|+1555|acct-1");
});
it("derives delivery context from a session entry", () => {
expect(
deliveryContextFromSession({
channel: "webchat",
lastChannel: " whatsapp ",
lastTo: " +1777 ",
lastAccountId: " acct-9 ",
}),
).toEqual({
channel: "whatsapp",
to: "+1777",
accountId: "acct-9",
});
expect(
deliveryContextFromSession({
channel: "telegram",
lastTo: " 123 ",
}),
).toEqual({
channel: "telegram",
to: "123",
accountId: undefined,
});
});
});