
























@@ -34,6 +34,70 @@ const mockState = vi.hoisted(() => ({
3434uploadMattermostFile: vi.fn(),
3535}));
363637+type MattermostPostParams = {
38+channelId?: string;
39+message?: string;
40+props?: {
41+attachments?: Array<{
42+actions?: Array<{ id?: string; name?: string }>;
43+}>;
44+};
45+};
46+47+type MattermostUploadParams = {
48+channelId?: string;
49+fileName?: string;
50+contentType?: string;
51+};
52+53+type MattermostDirectRetryOptions = {
54+maxRetries?: number;
55+initialDelayMs?: number;
56+maxDelayMs?: number;
57+timeoutMs?: number;
58+onRetry?: () => void;
59+};
60+61+function mockCall(mock: unknown, label: string, index = 0): unknown[] {
62+const calls = (mock as { mock?: { calls?: unknown[][] } }).mock?.calls;
63+const call = calls?.at(index);
64+if (!call) {
65+throw new Error(`Expected ${label} call ${index + 1}`);
66+}
67+return call;
68+}
69+70+function uploadMattermostFileCall() {
71+return mockCall(mockState.uploadMattermostFile, "uploadMattermostFile") as [
72+unknown,
73+MattermostUploadParams?,
74+];
75+}
76+77+function createMattermostPostParams() {
78+const params = mockCall(mockState.createMattermostPost, "createMattermostPost")[1] as
79+| MattermostPostParams
80+| undefined;
81+if (!params) {
82+throw new Error("Expected createMattermostPost params");
83+}
84+return params;
85+}
86+87+function createMattermostPostCall() {
88+return mockCall(mockState.createMattermostPost, "createMattermostPost") as [
89+unknown,
90+MattermostPostParams?,
91+];
92+}
93+94+function directChannelRetryCall() {
95+return mockCall(
96+mockState.createMattermostDirectChannelWithRetry,
97+"createMattermostDirectChannelWithRetry",
98+) as [unknown, unknown, MattermostDirectRetryOptions?];
99+}
100+37101vi.mock("../../runtime-api.js", () => ({
38102loadOutboundMediaFromUrl: mockState.loadOutboundMediaFromUrl,
39103}));
@@ -257,7 +321,7 @@ describe("sendMessageMattermost", () => {
257321mediaLocalRoots: ["/tmp/agent-workspace"],
258322},
259323);
260-const uploadCall = mockState.uploadMattermostFile.mock.calls[0];
324+const uploadCall = uploadMattermostFileCall();
261325expect(uploadCall?.[0]).toEqual({});
262326expect(uploadCall?.[1]?.channelId).toBe("town-square");
263327expect(uploadCall?.[1]?.fileName).toBe("photo.png");
@@ -277,7 +341,7 @@ describe("sendMessageMattermost", () => {
277341buttons: [[{ callback_data: "mdlprov", text: "Browse providers" }]],
278342});
279343280-const postCall = mockState.createMattermostPost.mock.calls[0];
344+const postCall = createMattermostPostCall();
281345expect(postCall?.[0]).toEqual({});
282346expect(postCall?.[1]?.channelId).toBe("town-square");
283347expect(postCall?.[1]?.message).toBe("Pick a model");
@@ -312,12 +376,12 @@ describe("sendMessageMattermost", () => {
312376});
313377314378expect(mockState.fetchMattermostUser).toHaveBeenCalledWith({}, userId);
315-const dmRetryCall = mockState.createMattermostDirectChannelWithRetry.mock.calls[0];
379+const dmRetryCall = directChannelRetryCall();
316380expect(dmRetryCall?.[0]).toEqual({});
317381expect(dmRetryCall?.[1]).toEqual(["bot-user", userId]);
318382expect(Object.keys(dmRetryCall?.[2] ?? {})).toEqual(["onRetry"]);
319383expect(dmRetryCall?.[2]?.onRetry).toBeTypeOf("function");
320-const uploadCall = mockState.uploadMattermostFile.mock.calls[0];
384+const uploadCall = uploadMattermostFileCall();
321385expect(uploadCall?.[0]).toEqual({});
322386expect(uploadCall?.[1]?.channelId).toBe("dm-channel-1");
323387expect(result.channelId).toBe("dm-channel-1");
@@ -349,7 +413,7 @@ describe("sendMessageMattermost", () => {
349413350414expect(mockState.fetchMattermostUser).toHaveBeenCalledWith({}, channelId);
351415expect(mockState.createMattermostDirectChannelWithRetry).not.toHaveBeenCalled();
352-const uploadCall = mockState.uploadMattermostFile.mock.calls[0];
416+const uploadCall = uploadMattermostFileCall();
353417expect(uploadCall?.[0]).toEqual({});
354418expect(uploadCall?.[1]?.channelId).toBe(channelId);
355419expect(result.channelId).toBe(channelId);
@@ -471,7 +535,7 @@ describe("sendMessageMattermost user-first resolution", () => {
471535472536expect(mockState.fetchMattermostUser).toHaveBeenCalledTimes(1);
473537expect(mockState.createMattermostDirectChannelWithRetry).toHaveBeenCalledTimes(1);
474-const params = mockState.createMattermostPost.mock.calls[0]?.[1];
538+const params = createMattermostPostParams();
475539expect(params.channelId).toBe("dm-channel-id");
476540expect(res.channelId).toBe("dm-channel-id");
477541expect(res.messageId).toBe("post-id");
@@ -490,7 +554,7 @@ describe("sendMessageMattermost user-first resolution", () => {
490554491555expect(mockState.fetchMattermostUser).toHaveBeenCalledTimes(1);
492556expect(mockState.createMattermostDirectChannelWithRetry).not.toHaveBeenCalled();
493-const params = mockState.createMattermostPost.mock.calls[0]?.[1];
557+const params = createMattermostPostParams();
494558expect(params.channelId).toBe(channelId);
495559expect(res.channelId).toBe(channelId);
496560});
@@ -545,7 +609,7 @@ describe("sendMessageMattermost user-first resolution", () => {
545609546610expect(mockState.fetchMattermostUser).not.toHaveBeenCalled();
547611expect(mockState.createMattermostDirectChannelWithRetry).not.toHaveBeenCalled();
548-const params = mockState.createMattermostPost.mock.calls[0]?.[1];
612+const params = createMattermostPostParams();
549613expect(params.channelId).toBe(chanId);
550614expect(res.channelId).toBe(chanId);
551615});
@@ -567,7 +631,7 @@ describe("sendMessageMattermost user-first resolution", () => {
567631dmRetryOptions: retryOptions,
568632});
569633570-const retryCall = mockState.createMattermostDirectChannelWithRetry.mock.calls[0];
634+const retryCall = directChannelRetryCall();
571635expect(retryCall?.[0]).toEqual({});
572636expect(retryCall?.[1]).toEqual(["bot-id", userId]);
573637expect(retryCall?.[2]?.maxRetries).toBe(retryOptions.maxRetries);
@@ -595,7 +659,7 @@ describe("sendMessageMattermost user-first resolution", () => {
595659596660await sendMessageMattermost(`user:${userId}`, "hello", { cfg: TEST_CFG });
597661598-const retryCall = mockState.createMattermostDirectChannelWithRetry.mock.calls[0];
662+const retryCall = directChannelRetryCall();
599663expect(retryCall?.[0]).toEqual({});
600664expect(retryCall?.[1]).toEqual(["bot-id", userId]);
601665expect(retryCall?.[2]?.maxRetries).toBe(4);
@@ -629,7 +693,7 @@ describe("sendMessageMattermost user-first resolution", () => {
629693dmRetryOptions: overrideOptions,
630694});
631695632-const retryCall = mockState.createMattermostDirectChannelWithRetry.mock.calls[0];
696+const retryCall = directChannelRetryCall();
633697expect(retryCall?.[0]).toEqual({});
634698expect(retryCall?.[1]).toEqual(["bot-id", userId]);
635699expect(retryCall?.[2]?.maxRetries).toBe(overrideOptions.maxRetries);
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。