






















@@ -52,6 +52,17 @@ function createDriveToolApi(params: {
5252});
5353}
545455+function mockCallArg<T>(
56+mock: { mock: { calls: unknown[][] } },
57+callIndex: number,
58+argIndex: number,
59+_type?: (value: unknown) => value is T,
60+): T {
61+const call = mock.mock.calls[callIndex];
62+expect(call).toBeDefined();
63+return call?.[argIndex] as T;
64+}
65+5566describe("registerFeishuDriveTools", () => {
5667const requestMock = vi.fn();
5768@@ -154,25 +165,28 @@ describe("registerFeishuDriveTools", () => {
154165file_token: "doc_1",
155166file_type: "docx",
156167});
157-expect(requestMock).toHaveBeenNthCalledWith(
158-1,
159-expect.objectContaining({
160-method: "GET",
161-url: "/open-apis/drive/v1/files/doc_1/comments?file_type=docx&user_id_type=open_id",
162-}),
163-);
164-expect(listResult.details).toEqual(
165-expect.objectContaining({
166-comments: [
167-expect.objectContaining({
168-comment_id: "c1",
169-text: "root comment",
170-quote: "quoted text",
171-replies: [expect.objectContaining({ reply_id: "r2", text: "reply text" })],
172-}),
173-],
174-}),
175-);
168+const listRequest = mockCallArg<{ method?: string; url?: string }>(requestMock, 0, 0);
169+expect(listRequest.method).toBe("GET");
170+expect(listRequest.url).toBe(
171+"/open-apis/drive/v1/files/doc_1/comments?file_type=docx&user_id_type=open_id",
172+);
173+const listDetails = listResult.details as
174+| {
175+comments?: Array<{
176+comment_id?: string;
177+quote?: string;
178+replies?: Array<{ reply_id?: string; text?: string }>;
179+text?: string;
180+}>;
181+}
182+| undefined;
183+expect(listDetails?.comments).toHaveLength(1);
184+expect(listDetails?.comments?.[0]?.comment_id).toBe("c1");
185+expect(listDetails?.comments?.[0]?.text).toBe("root comment");
186+expect(listDetails?.comments?.[0]?.quote).toBe("quoted text");
187+expect(listDetails?.comments?.[0]?.replies).toHaveLength(1);
188+expect(listDetails?.comments?.[0]?.replies?.[0]?.reply_id).toBe("r2");
189+expect(listDetails?.comments?.[0]?.replies?.[0]?.text).toBe("reply text");
176190177191requestMock.mockResolvedValueOnce({
178192code: 0,
@@ -201,18 +215,17 @@ describe("registerFeishuDriveTools", () => {
201215file_type: "docx",
202216comment_id: "c1",
203217});
204-expect(requestMock).toHaveBeenNthCalledWith(
205-2,
206-expect.objectContaining({
207-method: "GET",
208-url: "/open-apis/drive/v1/files/doc_1/comments/c1/replies?file_type=docx&user_id_type=open_id",
209-}),
210-);
211-expect(repliesResult.details).toEqual(
212-expect.objectContaining({
213-replies: [expect.objectContaining({ reply_id: "r3", text: "reply from api" })],
214-}),
215-);
218+const repliesRequest = mockCallArg<{ method?: string; url?: string }>(requestMock, 1, 0);
219+expect(repliesRequest.method).toBe("GET");
220+expect(repliesRequest.url).toBe(
221+"/open-apis/drive/v1/files/doc_1/comments/c1/replies?file_type=docx&user_id_type=open_id",
222+);
223+const repliesDetails = repliesResult.details as
224+| { replies?: Array<{ reply_id?: string; text?: string }> }
225+| undefined;
226+expect(repliesDetails?.replies).toHaveLength(1);
227+expect(repliesDetails?.replies?.[0]?.reply_id).toBe("r3");
228+expect(repliesDetails?.replies?.[0]?.text).toBe("reply from api");
216229217230requestMock.mockResolvedValueOnce({
218231code: 0,
@@ -225,21 +238,26 @@ describe("registerFeishuDriveTools", () => {
225238block_id: "blk_1",
226239content: "please update this section",
227240});
228-expect(requestMock).toHaveBeenNthCalledWith(
229-3,
230-expect.objectContaining({
231-method: "POST",
232-url: "/open-apis/drive/v1/files/doc_1/new_comments",
233-data: {
234-file_type: "docx",
235-reply_elements: [{ type: "text", text: "please update this section" }],
236-anchor: { block_id: "blk_1" },
237-},
238-}),
239-);
240-expect(addCommentResult.details).toEqual(
241-expect.objectContaining({ success: true, comment_id: "c2" }),
241+const addRequest = mockCallArg<{
242+data?: {
243+anchor?: { block_id?: string };
244+file_type?: string;
245+reply_elements?: Array<{ text?: string; type?: string }>;
246+};
247+method?: string;
248+url?: string;
249+}>(requestMock, 2, 0);
250+expect(addRequest.method).toBe("POST");
251+expect(addRequest.url).toBe("/open-apis/drive/v1/files/doc_1/new_comments");
252+expect(addRequest.data).toEqual({
253+file_type: "docx",
254+reply_elements: [{ type: "text", text: "please update this section" }],
255+anchor: { block_id: "blk_1" },
256+});
257+expect((addCommentResult.details as { comment_id?: string; success?: boolean }).success).toBe(
258+true,
242259);
260+expect((addCommentResult.details as { comment_id?: string }).comment_id).toBe("c2");
243261244262requestMock
245263.mockResolvedValueOnce({
@@ -259,39 +277,41 @@ describe("registerFeishuDriveTools", () => {
259277comment_id: "c1",
260278content: "handled",
261279});
262-expect(requestMock).toHaveBeenNthCalledWith(
263-4,
264-expect.objectContaining({
265-method: "POST",
266-url: "/open-apis/drive/v1/files/doc_1/comments/batch_query?file_type=docx&user_id_type=open_id",
267-data: {
268-comment_ids: ["c1"],
269-},
270-}),
271-);
272-expect(requestMock).toHaveBeenNthCalledWith(
273-5,
274-expect.objectContaining({
275-method: "POST",
276-url: "/open-apis/drive/v1/files/doc_1/comments/c1/replies",
277-params: { file_type: "docx" },
278-data: {
279-content: {
280-elements: [
281-{
282-type: "text_run",
283-text_run: {
284-text: "handled",
285-},
286-},
287-],
280+const batchRequest = mockCallArg<{
281+data?: { comment_ids?: string[] };
282+method?: string;
283+url?: string;
284+}>(requestMock, 3, 0);
285+expect(batchRequest.method).toBe("POST");
286+expect(batchRequest.url).toBe(
287+"/open-apis/drive/v1/files/doc_1/comments/batch_query?file_type=docx&user_id_type=open_id",
288+);
289+expect(batchRequest.data).toEqual({ comment_ids: ["c1"] });
290+const replyRequest = mockCallArg<{
291+data?: { content?: { elements?: Array<{ text_run?: { text?: string }; type?: string }> } };
292+method?: string;
293+params?: { file_type?: string };
294+url?: string;
295+}>(requestMock, 4, 0);
296+expect(replyRequest.method).toBe("POST");
297+expect(replyRequest.url).toBe("/open-apis/drive/v1/files/doc_1/comments/c1/replies");
298+expect(replyRequest.params).toEqual({ file_type: "docx" });
299+expect(replyRequest.data).toEqual({
300+content: {
301+elements: [
302+{
303+type: "text_run",
304+text_run: {
305+text: "handled",
306+},
288307},
289-},
290-}),
291-);
292-expect(replyCommentResult.details).toEqual(
293-expect.objectContaining({ success: true, reply_id: "r4" }),
308+],
309+},
310+});
311+expect((replyCommentResult.details as { reply_id?: string; success?: boolean }).success).toBe(
312+true,
294313);
314+expect((replyCommentResult.details as { reply_id?: string }).reply_id).toBe("r4");
295315});
296316297317it("defaults add_comment file_type to docx when omitted", async () => {
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。