openclaw/src/infra/outbound/message-action-normalization.test.ts
Peter Steinberger 8bd0eb5424 fix(outbound): land #38944 from @Narcooo
Co-authored-by: Narcooo <Narcooo@users.noreply.github.com>
2026-03-07 18:46:48 +00:00

84 lines
2.2 KiB
TypeScript

import { describe, expect, it } from "vitest";
import { normalizeMessageActionInput } from "./message-action-normalization.js";
describe("normalizeMessageActionInput", () => {
it("prefers explicit target and clears legacy target fields", () => {
const normalized = normalizeMessageActionInput({
action: "send",
args: {
target: "channel:C1",
to: "legacy",
channelId: "legacy-channel",
},
});
expect(normalized.target).toBe("channel:C1");
expect(normalized.to).toBe("channel:C1");
expect("channelId" in normalized).toBe(false);
});
it("ignores empty-string legacy target fields when explicit target is present", () => {
const normalized = normalizeMessageActionInput({
action: "send",
args: {
target: "1214056829",
channelId: "",
to: " ",
},
});
expect(normalized.target).toBe("1214056829");
expect(normalized.to).toBe("1214056829");
expect("channelId" in normalized).toBe(false);
});
it("maps legacy target fields into canonical target", () => {
const normalized = normalizeMessageActionInput({
action: "send",
args: {
to: "channel:C1",
},
});
expect(normalized.target).toBe("channel:C1");
expect(normalized.to).toBe("channel:C1");
});
it("infers target from tool context when required", () => {
const normalized = normalizeMessageActionInput({
action: "send",
args: {},
toolContext: {
currentChannelId: "channel:C1",
},
});
expect(normalized.target).toBe("channel:C1");
expect(normalized.to).toBe("channel:C1");
});
it("infers channel from tool context provider", () => {
const normalized = normalizeMessageActionInput({
action: "send",
args: {
target: "channel:C1",
},
toolContext: {
currentChannelId: "C1",
currentChannelProvider: "slack",
},
});
expect(normalized.channel).toBe("slack");
});
it("throws when required target remains unresolved", () => {
expect(() =>
normalizeMessageActionInput({
action: "send",
args: {},
}),
).toThrow(/requires a target/);
});
});