From b2d8a92f482e72ae06462d027e68e7481dab4671 Mon Sep 17 00:00:00 2001 From: Peter Steinberger Date: Tue, 12 May 2026 16:09:15 +0100 Subject: [PATCH] test: dedupe feishu docx mock calls --- .../feishu/src/docx-batch-insert.test.ts | 37 ++++++++++++++++--- 1 file changed, 31 insertions(+), 6 deletions(-) diff --git a/extensions/feishu/src/docx-batch-insert.test.ts b/extensions/feishu/src/docx-batch-insert.test.ts index a2177744538..5f642cbd106 100644 --- a/extensions/feishu/src/docx-batch-insert.test.ts +++ b/extensions/feishu/src/docx-batch-insert.test.ts @@ -7,6 +7,9 @@ type InsertBlocksClient = Parameters[0]; type DocxDescendantCreate = Lark.Client["docx"]["documentBlockDescendant"]["create"]; type DocxDescendantCreateParams = Parameters[0]; type DocxDescendantCreateResponse = Awaited>; +type RequiredDocxDescendantCreateParams = NonNullable & { + data: NonNullable["data"]>; +}; function createDocxDescendantClient(create: DocxDescendantCreate): InsertBlocksClient { return { @@ -45,6 +48,24 @@ function createSuccessfulDocxDescendantCreateMock() { ); } +function createCallParams( + createMock: ReturnType, + index = 0, +): RequiredDocxDescendantCreateParams { + const call = createMock.mock.calls.at(index); + if (!call) { + throw new Error(`Expected DOCX descendant create call ${index}`); + } + const params = call.at(0); + if (!params) { + throw new Error(`Expected DOCX descendant create params ${index}`); + } + if (!params.data) { + throw new Error(`Expected DOCX descendant create data ${index}`); + } + return params as RequiredDocxDescendantCreateParams; +} + describe("insertBlocksInBatches", () => { it("builds the source block map once for large flat trees", async () => { const blockCount = BATCH_SIZE + 200; @@ -65,8 +86,8 @@ describe("insertBlocksInBatches", () => { expect(counting.getIterations()).toBe(1); expect(createMock).toHaveBeenCalledTimes(2); - expect(createMock.mock.calls[0]?.[0]?.data.children_id).toHaveLength(BATCH_SIZE); - expect(createMock.mock.calls[1]?.[0]?.data.children_id).toHaveLength(200); + expect(createCallParams(createMock).data.children_id).toHaveLength(BATCH_SIZE); + expect(createCallParams(createMock, 1).data.children_id).toHaveLength(200); expect(result.children).toHaveLength(blockCount); }); @@ -83,9 +104,13 @@ describe("insertBlocksInBatches", () => { await insertBlocksInBatches(client, "doc_1", blocks, ["root_a", "root_b"]); expect(createMock).toHaveBeenCalledTimes(1); - expect(createMock.mock.calls[0]?.[0]?.data.children_id).toEqual(["root_a", "root_b"]); - expect( - createMock.mock.calls[0]?.[0]?.data.descendants.map((block) => block.block_id ?? ""), - ).toEqual(["root_a", "child_a", "root_b", "child_b"]); + const createParams = createCallParams(createMock); + expect(createParams.data.children_id).toEqual(["root_a", "root_b"]); + expect(createParams.data.descendants.map((block) => block.block_id ?? "")).toEqual([ + "root_a", + "child_a", + "root_b", + "child_b", + ]); }); });