fix: normalize subagent announce delivery origin
Co-authored-by: Adam Holt <mail@adamholt.co.nz>
This commit is contained in:
45
src/utils/delivery-context.test.ts
Normal file
45
src/utils/delivery-context.test.ts
Normal file
@@ -0,0 +1,45 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
|
||||
import {
|
||||
deliveryContextKey,
|
||||
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();
|
||||
});
|
||||
});
|
||||
@@ -18,3 +18,23 @@ export function normalizeDeliveryContext(context?: DeliveryContext): DeliveryCon
|
||||
accountId,
|
||||
};
|
||||
}
|
||||
|
||||
export function mergeDeliveryContext(
|
||||
primary?: DeliveryContext,
|
||||
fallback?: DeliveryContext,
|
||||
): DeliveryContext | undefined {
|
||||
const normalizedPrimary = normalizeDeliveryContext(primary);
|
||||
const normalizedFallback = normalizeDeliveryContext(fallback);
|
||||
if (!normalizedPrimary && !normalizedFallback) return undefined;
|
||||
return normalizeDeliveryContext({
|
||||
channel: normalizedPrimary?.channel ?? normalizedFallback?.channel,
|
||||
to: normalizedPrimary?.to ?? normalizedFallback?.to,
|
||||
accountId: normalizedPrimary?.accountId ?? normalizedFallback?.accountId,
|
||||
});
|
||||
}
|
||||
|
||||
export function deliveryContextKey(context?: DeliveryContext): string | undefined {
|
||||
const normalized = normalizeDeliveryContext(context);
|
||||
if (!normalized?.channel || !normalized?.to) return undefined;
|
||||
return `${normalized.channel}|${normalized.to}|${normalized.accountId ?? ""}`;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user