

























@@ -73,6 +73,35 @@ function createImageEvent(content: Record<string, unknown>) {
7373});
7474}
757576+type MockWithCalls = {
77+mock: { calls: unknown[][] };
78+};
79+80+function firstObjectArg(mock: MockWithCalls): Record<string, unknown> {
81+const value = mock.mock.calls[0]?.[0];
82+if (value === undefined || value === null || typeof value !== "object" || Array.isArray(value)) {
83+throw new Error("expected first mock call object argument");
84+}
85+return value as Record<string, unknown>;
86+}
87+88+function objectArgAt(mock: MockWithCalls, index: number): Record<string, unknown> {
89+const value = mock.mock.calls[0]?.[index];
90+if (value === undefined || value === null || typeof value !== "object" || Array.isArray(value)) {
91+throw new Error(`expected first mock call argument ${index} to be an object`);
92+}
93+return value as Record<string, unknown>;
94+}
95+96+function firstInboundContext(recordInboundSession: unknown): Record<string, unknown> {
97+const payload = firstObjectArg(recordInboundSession as MockWithCalls);
98+const ctx = payload.ctx;
99+if (ctx === undefined || ctx === null || typeof ctx !== "object" || Array.isArray(ctx)) {
100+throw new Error("expected inbound session ctx");
101+}
102+return ctx as Record<string, unknown>;
103+}
104+76105describe("createMatrixRoomMessageHandler media failures", () => {
77106beforeEach(() => {
78107downloadMatrixMediaMock.mockReset();
@@ -96,13 +125,10 @@ describe("createMatrixRoomMessageHandler media failures", () => {
96125}),
97126);
9812799-expect(downloadMatrixMediaMock).toHaveBeenCalledWith(
100-expect.objectContaining({
101-mxcUrl: "mxc://example/image",
102-maxBytes: 5 * 1024 * 1024,
103-originalFilename: "Screenshot 2026-03-27.png",
104-}),
105-);
128+const downloadOptions = firstObjectArg(downloadMatrixMediaMock);
129+expect(downloadOptions.mxcUrl).toBe("mxc://example/image");
130+expect(downloadOptions.maxBytes).toBe(5 * 1024 * 1024);
131+expect(downloadOptions.originalFilename).toBe("Screenshot 2026-03-27.png");
106132});
107133108134it("prefers content.filename over body text when deriving originalFilename", async () => {
@@ -123,10 +149,8 @@ describe("createMatrixRoomMessageHandler media failures", () => {
123149}),
124150);
125151126-expect(downloadMatrixMediaMock).toHaveBeenCalledWith(
127-expect.objectContaining({
128-originalFilename: "Screenshot 2026-03-27.png",
129-}),
152+expect(firstObjectArg(downloadMatrixMediaMock).originalFilename).toBe(
153+"Screenshot 2026-03-27.png",
130154);
131155});
132156@@ -143,23 +167,15 @@ describe("createMatrixRoomMessageHandler media failures", () => {
143167}),
144168);
145169146-expect(recordInboundSession).toHaveBeenCalledWith(
147-expect.objectContaining({
148-ctx: expect.objectContaining({
149-RawBody: "[matrix image attachment unavailable]",
150-CommandBody: "[matrix image attachment unavailable]",
151-MediaPath: undefined,
152-}),
153-}),
154-);
155-expect(logger.warn).toHaveBeenCalledWith(
156-"matrix media download failed",
157-expect.objectContaining({
158-eventId: "$event1",
159-msgtype: "m.image",
160-encrypted: false,
161-}),
162-);
170+const ctx = firstInboundContext(recordInboundSession);
171+expect(ctx.RawBody).toBe("[matrix image attachment unavailable]");
172+expect(ctx.CommandBody).toBe("[matrix image attachment unavailable]");
173+expect(ctx.MediaPath).toBeUndefined();
174+expect(logger.warn.mock.calls[0]?.[0]).toBe("matrix media download failed");
175+const warningMetadata = objectArgAt(logger.warn, 1);
176+expect(warningMetadata.eventId).toBe("$event1");
177+expect(warningMetadata.msgtype).toBe("m.image");
178+expect(warningMetadata.encrypted).toBe(false);
163179expect(runtime.error).not.toHaveBeenCalled();
164180});
165181@@ -182,15 +198,10 @@ describe("createMatrixRoomMessageHandler media failures", () => {
182198}),
183199);
184200185-expect(recordInboundSession).toHaveBeenCalledWith(
186-expect.objectContaining({
187-ctx: expect.objectContaining({
188-RawBody: "[matrix image attachment unavailable]",
189-CommandBody: "[matrix image attachment unavailable]",
190-MediaPath: undefined,
191-}),
192-}),
193-);
201+const ctx = firstInboundContext(recordInboundSession);
202+expect(ctx.RawBody).toBe("[matrix image attachment unavailable]");
203+expect(ctx.CommandBody).toBe("[matrix image attachment unavailable]");
204+expect(ctx.MediaPath).toBeUndefined();
194205});
195206196207it("preserves a real caption while marking the attachment unavailable", async () => {
@@ -207,13 +218,10 @@ describe("createMatrixRoomMessageHandler media failures", () => {
207218}),
208219);
209220210-expect(recordInboundSession).toHaveBeenCalledWith(
211-expect.objectContaining({
212-ctx: expect.objectContaining({
213-RawBody: "can you see this image?\n\n[matrix image attachment unavailable]",
214-CommandBody: "can you see this image?\n\n[matrix image attachment unavailable]",
215-}),
216-}),
221+const ctx = firstInboundContext(recordInboundSession);
222+expect(ctx.RawBody).toBe("can you see this image?\n\n[matrix image attachment unavailable]");
223+expect(ctx.CommandBody).toBe(
224+"can you see this image?\n\n[matrix image attachment unavailable]",
217225);
218226});
219227@@ -230,15 +238,10 @@ describe("createMatrixRoomMessageHandler media failures", () => {
230238}),
231239);
232240233-expect(recordInboundSession).toHaveBeenCalledWith(
234-expect.objectContaining({
235-ctx: expect.objectContaining({
236-RawBody: "[matrix image attachment too large]",
237-CommandBody: "[matrix image attachment too large]",
238-MediaPath: undefined,
239-}),
240-}),
241-);
241+const ctx = firstInboundContext(recordInboundSession);
242+expect(ctx.RawBody).toBe("[matrix image attachment too large]");
243+expect(ctx.CommandBody).toBe("[matrix image attachment too large]");
244+expect(ctx.MediaPath).toBeUndefined();
242245});
243246244247it("preserves a real caption while marking the attachment too large on size limit error", async () => {
@@ -255,13 +258,8 @@ describe("createMatrixRoomMessageHandler media failures", () => {
255258}),
256259);
257260258-expect(recordInboundSession).toHaveBeenCalledWith(
259-expect.objectContaining({
260-ctx: expect.objectContaining({
261-RawBody: "check this out\n\n[matrix image attachment too large]",
262-CommandBody: "check this out\n\n[matrix image attachment too large]",
263-}),
264-}),
265-);
261+const ctx = firstInboundContext(recordInboundSession);
262+expect(ctx.RawBody).toBe("check this out\n\n[matrix image attachment too large]");
263+expect(ctx.CommandBody).toBe("check this out\n\n[matrix image attachment too large]");
266264});
267265});
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。