test: clear subagent announce broad matchers

This commit is contained in:
Peter Steinberger
2026-05-10 07:38:11 +01:00
parent 314ab1e936
commit 59fd3e6481

View File

@@ -50,6 +50,35 @@ const longChildCompletionOutput = [
"Verification: pnpm test src/agents/subagent-announce-delivery.test.ts passed with the regression enabled.",
].join("\n");
function expectRecordFields(record: unknown, expected: Record<string, unknown>) {
expect(record).toBeDefined();
const actual = record as Record<string, unknown>;
for (const [key, value] of Object.entries(expected)) {
expect(actual[key]).toEqual(value);
}
return actual;
}
function asMock(fn: unknown) {
return fn as ReturnType<typeof vi.fn>;
}
function mockCallArg(fn: unknown, callIndex = 0, argIndex = 0) {
const call = asMock(fn).mock.calls[callIndex];
if (!call) {
throw new Error(`Expected mock call ${callIndex}`);
}
return call[argIndex];
}
function expectGatewayAgentParams(
callGateway: typeof runtimeCallGateway,
expected: Record<string, unknown>,
) {
const request = expectRecordFields(mockCallArg(callGateway), { method: "agent" });
return expectRecordFields(request.params, expected);
}
async function deliverSlackThreadAnnouncement(params: {
callGateway: typeof runtimeCallGateway;
isActive: boolean;
@@ -394,12 +423,10 @@ describe("deliverSubagentAnnouncement queued delivery", () => {
directIdempotencyKey: "announce-no-external-route",
});
expect(result).toEqual(
expect.objectContaining({
delivered: true,
path: "queued",
}),
);
expectRecordFields(result, {
delivered: true,
path: "queued",
});
await vi.waitFor(() => expect(callGateway).toHaveBeenCalledTimes(1));
return callGateway;
}
@@ -407,19 +434,14 @@ describe("deliverSubagentAnnouncement queued delivery", () => {
it("keeps queued announces with no external route session-only", async () => {
const callGateway = await deliverQueuedAnnouncement({});
expect(callGateway).toHaveBeenCalledWith(
expect.objectContaining({
method: "agent",
params: expect.objectContaining({
sessionKey: "agent:eng:paperclip:issue:123",
deliver: false,
channel: undefined,
accountId: undefined,
to: undefined,
threadId: undefined,
}),
}),
);
expectGatewayAgentParams(callGateway, {
sessionKey: "agent:eng:paperclip:issue:123",
deliver: false,
channel: undefined,
accountId: undefined,
to: undefined,
threadId: undefined,
});
});
it("keeps queued announces with channel-only origins session-only", async () => {
@@ -429,15 +451,11 @@ describe("deliverSubagentAnnouncement queued delivery", () => {
},
});
expect(callGateway).toHaveBeenCalledWith(
expect.objectContaining({
params: expect.objectContaining({
deliver: false,
channel: undefined,
to: undefined,
}),
}),
);
expectGatewayAgentParams(callGateway, {
deliver: false,
channel: undefined,
to: undefined,
});
});
it("keeps queued announces with internal origins session-only", async () => {
@@ -450,17 +468,13 @@ describe("deliverSubagentAnnouncement queued delivery", () => {
},
});
expect(callGateway).toHaveBeenCalledWith(
expect.objectContaining({
params: expect.objectContaining({
deliver: false,
channel: undefined,
accountId: undefined,
to: undefined,
threadId: undefined,
}),
}),
);
expectGatewayAgentParams(callGateway, {
deliver: false,
channel: undefined,
accountId: undefined,
to: undefined,
threadId: undefined,
});
});
it("preserves queued external route fields when channel and target are present", async () => {
@@ -473,17 +487,13 @@ describe("deliverSubagentAnnouncement queued delivery", () => {
},
});
expect(callGateway).toHaveBeenCalledWith(
expect.objectContaining({
params: expect.objectContaining({
deliver: true,
channel: "slack",
accountId: "acct-1",
to: "channel:C123",
threadId: "171.222",
}),
}),
);
expectGatewayAgentParams(callGateway, {
deliver: true,
channel: "slack",
accountId: "acct-1",
to: "channel:C123",
threadId: "171.222",
});
});
});
@@ -500,12 +510,10 @@ describe("deliverSubagentAnnouncement completion delivery", () => {
queueEmbeddedPiMessage,
});
expect(result).toEqual(
expect.objectContaining({
delivered: true,
path: "steered",
}),
);
expectRecordFields(result, {
delivered: true,
path: "steered",
});
expect(queueEmbeddedPiMessage).toHaveBeenCalledWith("requester-session-1", "child done", {
steeringMode: "all",
debounceMs: 500,
@@ -523,19 +531,14 @@ describe("deliverSubagentAnnouncement completion delivery", () => {
directIdempotencyKey: "announce-1b",
});
expect(callGateway).toHaveBeenCalledWith(
expect.objectContaining({
method: "agent",
params: expect.objectContaining({
deliver: true,
channel: "slack",
accountId: "acct-1",
to: "channel:C123",
threadId: "171.222",
bestEffortDeliver: true,
}),
}),
);
expectGatewayAgentParams(callGateway, {
deliver: true,
channel: "slack",
accountId: "acct-1",
to: "channel:C123",
threadId: "171.222",
bestEffortDeliver: true,
});
});
it("keeps announce-agent delivery primary for dormant completion events with child output", async () => {
@@ -568,26 +571,19 @@ describe("deliverSubagentAnnouncement completion delivery", () => {
],
});
expect(result).toEqual(
expect.objectContaining({
delivered: true,
path: "direct",
}),
);
expect(callGateway).toHaveBeenCalledWith(
expect.objectContaining({
method: "agent",
params: expect.objectContaining({
deliver: true,
channel: "slack",
accountId: "acct-1",
to: "channel:C123",
threadId: "171.222",
bestEffortDeliver: true,
internalEvents: expect.any(Array),
}),
}),
);
expectRecordFields(result, {
delivered: true,
path: "direct",
});
const params = expectGatewayAgentParams(callGateway, {
deliver: true,
channel: "slack",
accountId: "acct-1",
to: "channel:C123",
threadId: "171.222",
bestEffortDeliver: true,
});
expect(Array.isArray(params.internalEvents)).toBe(true);
expect(sendMessage).not.toHaveBeenCalled();
});
@@ -621,12 +617,10 @@ describe("deliverSubagentAnnouncement completion delivery", () => {
],
});
expect(result).toEqual(
expect.objectContaining({
delivered: true,
path: "direct",
}),
);
expectRecordFields(result, {
delivered: true,
path: "direct",
});
expect(sendMessage).not.toHaveBeenCalled();
});
@@ -660,12 +654,10 @@ describe("deliverSubagentAnnouncement completion delivery", () => {
],
});
expect(result).toEqual(
expect.objectContaining({
delivered: true,
path: "direct",
}),
);
expectRecordFields(result, {
delivered: true,
path: "direct",
});
expect(sendMessage).not.toHaveBeenCalled();
});
@@ -699,12 +691,10 @@ describe("deliverSubagentAnnouncement completion delivery", () => {
],
});
expect(result).toEqual(
expect.objectContaining({
delivered: true,
path: "direct",
}),
);
expectRecordFields(result, {
delivered: true,
path: "direct",
});
expect(sendMessage).not.toHaveBeenCalled();
});
@@ -750,13 +740,11 @@ describe("deliverSubagentAnnouncement completion delivery", () => {
],
});
expect(result).toEqual(
expect.objectContaining({
delivered: false,
path: "direct",
error: "completion agent did not produce a visible reply",
}),
);
expectRecordFields(result, {
delivered: false,
path: "direct",
error: "completion agent did not produce a visible reply",
});
expect(sendMessage).not.toHaveBeenCalled();
});
@@ -790,12 +778,10 @@ describe("deliverSubagentAnnouncement completion delivery", () => {
],
});
expect(result).toEqual(
expect.objectContaining({
delivered: true,
path: "direct",
}),
);
expectRecordFields(result, {
delivered: true,
path: "direct",
});
expect(sendMessage).not.toHaveBeenCalled();
});
@@ -829,12 +815,10 @@ describe("deliverSubagentAnnouncement completion delivery", () => {
],
});
expect(result).toEqual(
expect.objectContaining({
delivered: true,
path: "direct",
}),
);
expectRecordFields(result, {
delivered: true,
path: "direct",
});
expect(sendMessage).not.toHaveBeenCalled();
});
@@ -866,13 +850,11 @@ describe("deliverSubagentAnnouncement completion delivery", () => {
],
});
expect(result).toEqual(
expect.objectContaining({
delivered: false,
path: "direct",
error: "UNAVAILABLE: gateway lost final output",
}),
);
expectRecordFields(result, {
delivered: false,
path: "direct",
error: "UNAVAILABLE: gateway lost final output",
});
expect(callGateway).toHaveBeenCalled();
expect(sendMessage).not.toHaveBeenCalled();
});
@@ -901,13 +883,11 @@ describe("deliverSubagentAnnouncement completion delivery", () => {
],
});
expect(result).toEqual(
expect.objectContaining({
delivered: false,
path: "direct",
error: "UNAVAILABLE: requester wake failed",
}),
);
expectRecordFields(result, {
delivered: false,
path: "direct",
error: "UNAVAILABLE: requester wake failed",
});
expect(sendMessage).not.toHaveBeenCalled();
});
@@ -936,26 +916,24 @@ describe("deliverSubagentAnnouncement completion delivery", () => {
],
});
expect(result).toEqual(
expect.objectContaining({
delivered: true,
path: "queued",
phases: [
{
phase: "direct-primary",
delivered: false,
path: "direct",
error: "active requester session could not be woken",
},
{
phase: "queue-fallback",
delivered: true,
path: "queued",
error: undefined,
},
],
}),
);
expectRecordFields(result, {
delivered: true,
path: "queued",
phases: [
{
phase: "direct-primary",
delivered: false,
path: "direct",
error: "active requester session could not be woken",
},
{
phase: "queue-fallback",
delivered: true,
path: "queued",
error: undefined,
},
],
});
expect(queueEmbeddedPiMessage).toHaveBeenCalledWith(
"requester-session-telegram",
"child done",
@@ -998,13 +976,11 @@ describe("deliverSubagentAnnouncement completion delivery", () => {
],
});
expect(result).toEqual(
expect.objectContaining({
delivered: false,
path: "direct",
error: "completion agent did not produce a visible reply",
}),
);
expectRecordFields(result, {
delivered: false,
path: "direct",
error: "completion agent did not produce a visible reply",
});
expect(callGateway).toHaveBeenCalled();
expect(sendMessage).not.toHaveBeenCalled();
});
@@ -1036,25 +1012,18 @@ describe("deliverSubagentAnnouncement completion delivery", () => {
],
});
expect(result).toEqual(
expect.objectContaining({
delivered: false,
path: "direct",
error: "completion agent did not produce a visible reply",
}),
);
expect(callGateway).toHaveBeenCalledWith(
expect.objectContaining({
method: "agent",
params: expect.objectContaining({
deliver: true,
channel: "discord",
accountId: "acct-1",
to: "dm:U123",
threadId: undefined,
}),
}),
);
expectRecordFields(result, {
delivered: false,
path: "direct",
error: "completion agent did not produce a visible reply",
});
expectGatewayAgentParams(callGateway, {
deliver: true,
channel: "discord",
accountId: "acct-1",
to: "dm:U123",
threadId: undefined,
});
expect(sendMessage).not.toHaveBeenCalled();
});
@@ -1087,12 +1056,10 @@ describe("deliverSubagentAnnouncement completion delivery", () => {
],
});
expect(result).toEqual(
expect.objectContaining({
delivered: true,
path: "direct",
}),
);
expectRecordFields(result, {
delivered: true,
path: "direct",
});
expect(callGateway).toHaveBeenCalled();
expect(sendMessage).not.toHaveBeenCalled();
});
@@ -1131,24 +1098,17 @@ describe("deliverSubagentAnnouncement completion delivery", () => {
],
});
expect(result).toEqual(
expect.objectContaining({
delivered: true,
path: "direct",
}),
);
expect(callGateway).toHaveBeenCalledWith(
expect.objectContaining({
method: "agent",
params: expect.objectContaining({
deliver: true,
channel: "discord",
accountId: "acct-1",
to: "dm:U123",
threadId: undefined,
}),
}),
);
expectRecordFields(result, {
delivered: true,
path: "direct",
});
expectGatewayAgentParams(callGateway, {
deliver: true,
channel: "discord",
accountId: "acct-1",
to: "dm:U123",
threadId: undefined,
});
expect(sendMessage).not.toHaveBeenCalled();
});
@@ -1189,25 +1149,18 @@ describe("deliverSubagentAnnouncement completion delivery", () => {
],
});
expect(result).toEqual(
expect.objectContaining({
delivered: false,
path: "direct",
error: "completion agent did not deliver through the message tool",
}),
);
expect(callGateway).toHaveBeenCalledWith(
expect.objectContaining({
method: "agent",
params: expect.objectContaining({
deliver: false,
channel: "slack",
accountId: "acct-1",
to: "channel:C123",
threadId: undefined,
}),
}),
);
expectRecordFields(result, {
delivered: false,
path: "direct",
error: "completion agent did not deliver through the message tool",
});
expectGatewayAgentParams(callGateway, {
deliver: false,
channel: "slack",
accountId: "acct-1",
to: "channel:C123",
threadId: undefined,
});
expect(sendMessage).not.toHaveBeenCalled();
});
@@ -1254,12 +1207,10 @@ describe("deliverSubagentAnnouncement completion delivery", () => {
],
});
expect(result).toEqual(
expect.objectContaining({
delivered: true,
path: "direct",
}),
);
expectRecordFields(result, {
delivered: true,
path: "direct",
});
expect(sendMessage).not.toHaveBeenCalled();
});
@@ -1295,12 +1246,10 @@ describe("deliverSubagentAnnouncement completion delivery", () => {
],
});
expect(result).toEqual(
expect.objectContaining({
delivered: true,
path: "direct",
}),
);
expectRecordFields(result, {
delivered: true,
path: "direct",
});
expect(callGateway).toHaveBeenCalled();
expect(sendMessage).not.toHaveBeenCalled();
});
@@ -1335,13 +1284,11 @@ describe("deliverSubagentAnnouncement completion delivery", () => {
],
});
expect(result).toEqual(
expect.objectContaining({
delivered: false,
path: "direct",
error: "completion agent did not produce a visible reply",
}),
);
expectRecordFields(result, {
delivered: false,
path: "direct",
error: "completion agent did not produce a visible reply",
});
expect(callGateway).toHaveBeenCalled();
expect(sendMessage).not.toHaveBeenCalled();
});
@@ -1377,22 +1324,16 @@ describe("deliverSubagentAnnouncement completion delivery", () => {
],
});
expect(result).toEqual(
expect.objectContaining({
delivered: true,
path: "direct",
}),
);
expect(callGateway).toHaveBeenCalledWith(
expect.objectContaining({
params: expect.objectContaining({
deliver: true,
channel: "slack",
accountId: "acct-1",
to: "channel:C123",
}),
}),
);
expectRecordFields(result, {
delivered: true,
path: "direct",
});
expectGatewayAgentParams(callGateway, {
deliver: true,
channel: "slack",
accountId: "acct-1",
to: "channel:C123",
});
});
it("keeps direct external delivery for non-completion announces", async () => {
@@ -1405,18 +1346,13 @@ describe("deliverSubagentAnnouncement completion delivery", () => {
directIdempotencyKey: "announce-2",
});
expect(callGateway).toHaveBeenCalledWith(
expect.objectContaining({
method: "agent",
params: expect.objectContaining({
deliver: true,
channel: "slack",
accountId: "acct-1",
to: "channel:C123",
threadId: "171.222",
bestEffortDeliver: true,
}),
}),
);
expectGatewayAgentParams(callGateway, {
deliver: true,
channel: "slack",
accountId: "acct-1",
to: "channel:C123",
threadId: "171.222",
bestEffortDeliver: true,
});
});
});