test: tighten agent delivery assertions

This commit is contained in:
Peter Steinberger
2026-05-10 01:54:51 +01:00
parent 9b2f947b4c
commit f3f644a6b9

View File

@@ -12,6 +12,41 @@ const mocks = vi.hoisted(() => ({
resolveOutboundTarget: vi.fn(() => ({ ok: true as const, to: "+15551234567" })),
}));
type DeliveryCall = {
accountId?: string;
session?: {
agentId?: string;
key?: string;
};
};
type ResolveTargetCall = {
accountId?: string;
channel?: string;
mode?: string;
to?: string;
};
function readDeliveryCall(): DeliveryCall {
expect(mocks.deliverOutboundPayloads).toHaveBeenCalledOnce();
const calls = mocks.deliverOutboundPayloads.mock.calls as unknown as Array<[unknown]>;
const call = calls[0]?.[0];
if (!call) {
throw new Error("Expected delivery call");
}
return call as DeliveryCall;
}
function readResolveTargetCall(): ResolveTargetCall {
expect(mocks.resolveOutboundTarget).toHaveBeenCalledOnce();
const calls = mocks.resolveOutboundTarget.mock.calls as unknown as Array<[unknown]>;
const call = calls[0]?.[0];
if (!call) {
throw new Error("Expected resolve target call");
}
return call as ResolveTargetCall;
}
vi.mock("../channels/plugins/index.js", () => ({
getChannelPlugin: mocks.getChannelPlugin,
getLoadedChannelPlugin: mocks.getChannelPlugin,
@@ -99,9 +134,7 @@ describe("deliverAgentCommandResult", () => {
} as SessionEntry,
});
expect(mocks.deliverOutboundPayloads).toHaveBeenCalledWith(
expect.objectContaining({ accountId: "kev" }),
);
expect(readDeliveryCall().accountId).toBe("kev");
});
it("falls back to session accountId for implicit delivery", async () => {
@@ -117,9 +150,7 @@ describe("deliverAgentCommandResult", () => {
} as SessionEntry,
});
expect(mocks.deliverOutboundPayloads).toHaveBeenCalledWith(
expect.objectContaining({ accountId: "legacy" }),
);
expect(readDeliveryCall().accountId).toBe("legacy");
});
it("does not infer accountId for explicit delivery targets", async () => {
@@ -136,12 +167,10 @@ describe("deliverAgentCommandResult", () => {
} as SessionEntry,
});
expect(mocks.resolveOutboundTarget).toHaveBeenCalledWith(
expect.objectContaining({ accountId: undefined, mode: "explicit" }),
);
expect(mocks.deliverOutboundPayloads).toHaveBeenCalledWith(
expect.objectContaining({ accountId: undefined }),
);
const targetCall = readResolveTargetCall();
expect(targetCall.accountId).toBeUndefined();
expect(targetCall.mode).toBe("explicit");
expect(readDeliveryCall().accountId).toBeUndefined();
});
it("skips session accountId when channel differs", async () => {
@@ -157,9 +186,9 @@ describe("deliverAgentCommandResult", () => {
} as SessionEntry,
});
expect(mocks.resolveOutboundTarget).toHaveBeenCalledWith(
expect.objectContaining({ accountId: undefined, channel: "whatsapp" }),
);
const targetCall = readResolveTargetCall();
expect(targetCall.accountId).toBeUndefined();
expect(targetCall.channel).toBe("whatsapp");
});
it("uses session last channel when none is provided", async () => {
@@ -174,9 +203,9 @@ describe("deliverAgentCommandResult", () => {
} as SessionEntry,
});
expect(mocks.resolveOutboundTarget).toHaveBeenCalledWith(
expect.objectContaining({ channel: "telegram", to: "123" }),
);
const targetCall = readResolveTargetCall();
expect(targetCall.channel).toBe("telegram");
expect(targetCall.to).toBe("123");
});
it("uses reply overrides for delivery routing", async () => {
@@ -196,9 +225,10 @@ describe("deliverAgentCommandResult", () => {
} as SessionEntry,
});
expect(mocks.resolveOutboundTarget).toHaveBeenCalledWith(
expect.objectContaining({ channel: "slack", to: "#reports", accountId: "ops" }),
);
const targetCall = readResolveTargetCall();
expect(targetCall.channel).toBe("slack");
expect(targetCall.to).toBe("#reports");
expect(targetCall.accountId).toBe("ops");
});
it("stays silent for intentional empty payloads", async () => {
@@ -234,9 +264,10 @@ describe("deliverAgentCommandResult", () => {
} as SessionEntry,
});
expect(mocks.resolveOutboundTarget).toHaveBeenCalledWith(
expect.objectContaining({ channel: "whatsapp", to: "+15559876543", accountId: "work" }),
);
const targetCall = readResolveTargetCall();
expect(targetCall.channel).toBe("whatsapp");
expect(targetCall.to).toBe("+15559876543");
expect(targetCall.accountId).toBe("work");
});
it("does not reuse session lastTo when runContext source omits currentChannelId", async () => {
@@ -254,9 +285,9 @@ describe("deliverAgentCommandResult", () => {
} as SessionEntry,
});
expect(mocks.resolveOutboundTarget).toHaveBeenCalledWith(
expect.objectContaining({ channel: "whatsapp", to: undefined }),
);
const targetCall = readResolveTargetCall();
expect(targetCall.channel).toBe("whatsapp");
expect(targetCall.to).toBeUndefined();
});
it("uses caller-provided outbound session context when opts.sessionKey is absent", async () => {
@@ -273,14 +304,9 @@ describe("deliverAgentCommandResult", () => {
},
});
expect(mocks.deliverOutboundPayloads).toHaveBeenCalledWith(
expect.objectContaining({
session: expect.objectContaining({
key: "agent:exec:hook:gmail:thread-1",
agentId: "exec",
}),
}),
);
const deliveryCall = readDeliveryCall();
expect(deliveryCall.session?.key).toBe("agent:exec:hook:gmail:thread-1");
expect(deliveryCall.session?.agentId).toBe("exec");
});
it("prefixes nested agent outputs with context", async () => {