




















@@ -105,6 +105,49 @@ function latestOutboundDeliveryArgs(): {
105105return args as { payloads: ReplyPayload[]; bestEffort?: boolean; queuePolicy?: string };
106106}
107107108+type DeliveryStatusLike = {
109+requested?: unknown;
110+attempted?: unknown;
111+status?: unknown;
112+succeeded?: unknown;
113+reason?: unknown;
114+error?: unknown;
115+errorMessage?: unknown;
116+resultCount?: unknown;
117+sentBeforeError?: unknown;
118+payloadOutcomes?: Array<Record<string, unknown>>;
119+};
120+121+function deliveryStatus(delivered: { deliveryStatus?: unknown }): DeliveryStatusLike {
122+return (delivered.deliveryStatus ?? {}) as DeliveryStatusLike;
123+}
124+125+function expectDeliveryStatusFields(
126+delivered: { deliveryStatus?: unknown },
127+expected: Record<string, unknown>,
128+) {
129+const status = deliveryStatus(delivered);
130+for (const [key, value] of Object.entries(expected)) {
131+expect(status[key as keyof DeliveryStatusLike], key).toEqual(value);
132+}
133+return status;
134+}
135+136+function expectRuntimeErrorIncludes(
137+runtime: { error: { mock: { calls: Array<Array<unknown>> } } },
138+text: string,
139+) {
140+expect(runtime.error.mock.calls.some(([message]) => String(message).includes(text))).toBe(true);
141+}
142+143+function latestJsonOutput(runtime: { writeJson: { mock: { calls: Array<Array<unknown>> } } }) {
144+const output = runtime.writeJson.mock.calls.at(-1)?.[0];
145+if (!output || typeof output !== "object") {
146+throw new Error("expected JSON output");
147+}
148+return output as { deliveryStatus?: DeliveryStatusLike };
149+}
150+108151async function deliverMediaReplyForTest(
109152outboundSession: DeliverParams["outboundSession"],
110153optsOverrides: Partial<AgentCommandOpts> = {},
@@ -267,7 +310,7 @@ describe("normalizeAgentCommandReplyPayloads", () => {
267310} as never);
268311269312expect(delivered.deliverySucceeded).toBe(true);
270-expect(delivered.deliveryStatus).toMatchObject({
313+expectDeliveryStatusFields(delivered, {
271314requested: true,
272315attempted: true,
273316status: "suppressed",
@@ -336,14 +379,14 @@ describe("normalizeAgentCommandReplyPayloads", () => {
336379});
337380338381expect(delivered.deliverySucceeded).toBe(false);
339-expect(delivered.deliveryStatus).toMatchObject({
382+expectDeliveryStatusFields(delivered, {
340383requested: true,
341384attempted: true,
342385status: "failed",
343386succeeded: false,
344387error: true,
345388});
346-expect(runtime.error).toHaveBeenCalledWith(expect.stringContaining("send failed"));
389+expectRuntimeErrorIncludes(runtime, "send failed");
347390const deliverArgs = latestOutboundDeliveryArgs();
348391expect(deliverArgs.bestEffort).toBe(true);
349392expect(deliverArgs.queuePolicy).toBe("best_effort");
@@ -471,18 +514,14 @@ describe("normalizeAgentCommandReplyPayloads", () => {
471514});
472515473516expect(runtime.writeJson).toHaveBeenCalledTimes(1);
474-expect(runtime.writeJson).toHaveBeenCalledWith(
475-expect.objectContaining({
476-deliveryStatus: {
477-requested: true,
478-attempted: true,
479-status: "sent",
480-succeeded: true,
481-resultCount: 1,
482-},
483-}),
484-2,
485-);
517+const json = latestJsonOutput(runtime);
518+expect(json.deliveryStatus).toEqual({
519+requested: true,
520+attempted: true,
521+status: "sent",
522+succeeded: true,
523+resultCount: 1,
524+});
486525expect(delivered.deliverySucceeded).toBe(true);
487526expect(delivered.deliveryStatus?.status).toBe("sent");
488527});
@@ -513,21 +552,21 @@ describe("normalizeAgentCommandReplyPayloads", () => {
513552} as never);
514553515554expect(delivered.deliverySucceeded).toBe(true);
516-expect(delivered.deliveryStatus).toMatchObject({
555+const status = expectDeliveryStatusFields(delivered, {
517556requested: true,
518557attempted: true,
519558status: "suppressed",
520559succeeded: true,
521560reason: "cancelled_by_message_sending_hook",
522-payloadOutcomes: [
523-{
524-index: 0,
525-status: "suppressed",
526-reason: "cancelled_by_message_sending_hook",
527-hookEffect: { cancelReason: "owned-by-other-agent" },
528-},
529-],
530561});
562+expect(status.payloadOutcomes).toEqual([
563+{
564+index: 0,
565+status: "suppressed",
566+reason: "cancelled_by_message_sending_hook",
567+hookEffect: { cancelReason: "owned-by-other-agent" },
568+},
569+]);
531570});
532571533572it("surfaces durable partial failures without clearing delivery retry state", async () => {
@@ -561,25 +600,23 @@ describe("normalizeAgentCommandReplyPayloads", () => {
561600);
562601563602expect(delivered.deliverySucceeded).toBe(false);
564-expect(delivered.deliveryStatus).toMatchObject({
603+const status = expectDeliveryStatusFields(delivered, {
565604requested: true,
566605attempted: true,
567606status: "partial_failed",
568607succeeded: "partial",
569608error: true,
570-errorMessage: expect.stringContaining("second chunk failed"),
571609resultCount: 1,
572610sentBeforeError: true,
573-payloadOutcomes: [
574-{
575-index: 1,
576-status: "failed",
577-error: expect.stringContaining("second chunk failed"),
578-sentBeforeError: true,
579-stage: "platform_send",
580-},
581-],
582611});
612+expect(String(status.errorMessage)).toContain("second chunk failed");
613+expect(status.payloadOutcomes).toHaveLength(1);
614+const outcome = status.payloadOutcomes?.[0];
615+expect(outcome?.index).toBe(1);
616+expect(outcome?.status).toBe("failed");
617+expect(String(outcome?.error)).toContain("second chunk failed");
618+expect(outcome?.sentBeforeError).toBe(true);
619+expect(outcome?.stage).toBe("platform_send");
583620});
584621585622it("marks no-payload deliveryStatus as terminal delivery success", async () => {
@@ -600,7 +637,7 @@ describe("normalizeAgentCommandReplyPayloads", () => {
600637});
601638602639expect(delivered.deliverySucceeded).toBe(true);
603-expect(delivered.deliveryStatus).toMatchObject({
640+expectDeliveryStatusFields(delivered, {
604641requested: true,
605642attempted: false,
606643status: "suppressed",
@@ -629,7 +666,7 @@ describe("normalizeAgentCommandReplyPayloads", () => {
629666630667expect(delivered.payloads).toEqual([]);
631668expect(delivered.deliverySucceeded).toBe(true);
632-expect(delivered.deliveryStatus).toMatchObject({
669+expectDeliveryStatusFields(delivered, {
633670requested: true,
634671attempted: false,
635672status: "suppressed",
@@ -660,15 +697,15 @@ describe("normalizeAgentCommandReplyPayloads", () => {
660697});
661698662699expect(delivered.deliverySucceeded).toBeUndefined();
663-expect(delivered.deliveryStatus).toMatchObject({
700+expectDeliveryStatusFields(delivered, {
664701requested: true,
665702attempted: false,
666703status: "failed",
667704succeeded: false,
668705error: true,
669706reason: "unknown_channel",
670707});
671-expect(runtime.error).toHaveBeenCalledWith(expect.stringContaining("Unknown channel"));
708+expectRuntimeErrorIncludes(runtime, "Unknown channel");
672709expect(deliverOutboundPayloadsMock).not.toHaveBeenCalled();
673710});
674711@@ -709,19 +746,13 @@ describe("normalizeAgentCommandReplyPayloads", () => {
709746).rejects.toThrow("Slack API timeout");
710747711748expect(runtime.writeJson).toHaveBeenCalledTimes(1);
712-expect(runtime.writeJson).toHaveBeenCalledWith(
713-expect.objectContaining({
714-deliveryStatus: {
715-requested: true,
716-attempted: true,
717-status: "failed",
718-succeeded: false,
719-error: true,
720-errorMessage: expect.stringContaining("Slack API timeout"),
721-},
722-}),
723-2,
724-);
749+const json = latestJsonOutput(runtime);
750+expect(json.deliveryStatus?.requested).toBe(true);
751+expect(json.deliveryStatus?.attempted).toBe(true);
752+expect(json.deliveryStatus?.status).toBe("failed");
753+expect(json.deliveryStatus?.succeeded).toBe(false);
754+expect(json.deliveryStatus?.error).toBe(true);
755+expect(String(json.deliveryStatus?.errorMessage)).toContain("Slack API timeout");
725756});
726757727758it("emits JSON deliveryStatus before strict preflight failures rethrow", async () => {
@@ -763,18 +794,14 @@ describe("normalizeAgentCommandReplyPayloads", () => {
763794expect(deliverOutboundPayloadsMock).not.toHaveBeenCalled();
764795expect(createReplyMediaPathNormalizerMock).not.toHaveBeenCalled();
765796expect(runtime.writeJson).toHaveBeenCalledTimes(1);
766-expect(runtime.writeJson).toHaveBeenCalledWith(
767-expect.objectContaining({
768-deliveryStatus: {
769-requested: true,
770-attempted: false,
771-status: "failed",
772-succeeded: false,
773-error: true,
774-reason: "unknown_channel",
775-},
776-}),
777-2,
778-);
797+const json = latestJsonOutput(runtime);
798+expect(json.deliveryStatus).toEqual({
799+requested: true,
800+attempted: false,
801+status: "failed",
802+succeeded: false,
803+error: true,
804+reason: "unknown_channel",
805+});
779806});
780807});
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。