From 5e2eec3b178bcad0988ba7086e31514a3fb7b5d5 Mon Sep 17 00:00:00 2001 From: kumaryash90 Date: Tue, 7 Jan 2025 22:18:18 +0000 Subject: [PATCH] Fix batch fetch util for marketplace-v3 (#5904) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- title: "[SDK/Dashboard/Portal] Feature/Fix: Concise title for the changes" --- CORE-680 ## Notes for the reviewer Anything important to call out? Be sure to also clarify these in your comments. ## How to test Unit tests, playground, etc. --- ## PR-Codex overview This PR focuses on fixing the batch fetching utility in the `marketplace-v3` and adding tests to ensure its functionality. ### Detailed summary - Fixed the calculation of the end index in the batch fetching logic in `packages/thirdweb/src/extensions/marketplace/utils.ts`. - Added tests for `getAllInBatches` in `packages/thirdweb/src/extensions/marketplace/getAllInBatches.test.ts` to validate handling of multiple and single batches. > ✨ Ask PR-Codex anything about this PR by commenting with `/codex {your question}` --- .changeset/pink-deers-fetch.md | 5 ++ .../marketplace/getAllInBatches.test.ts | 54 +++++++++++++++++++ .../src/extensions/marketplace/utils.ts | 2 +- 3 files changed, 60 insertions(+), 1 deletion(-) create mode 100644 .changeset/pink-deers-fetch.md create mode 100644 packages/thirdweb/src/extensions/marketplace/getAllInBatches.test.ts diff --git a/.changeset/pink-deers-fetch.md b/.changeset/pink-deers-fetch.md new file mode 100644 index 00000000000..bdf09c9acc8 --- /dev/null +++ b/.changeset/pink-deers-fetch.md @@ -0,0 +1,5 @@ +--- +"thirdweb": patch +--- + +Fix batch fetch util for marketplace-v3 diff --git a/packages/thirdweb/src/extensions/marketplace/getAllInBatches.test.ts b/packages/thirdweb/src/extensions/marketplace/getAllInBatches.test.ts new file mode 100644 index 00000000000..7aad59f4401 --- /dev/null +++ b/packages/thirdweb/src/extensions/marketplace/getAllInBatches.test.ts @@ -0,0 +1,54 @@ +import { describe, expect, it } from "vitest"; +import { getAllInBatches } from "./utils.js"; + +describe("getAllInBatches", () => { + it("should handle range with multiple batches", async () => { + const mockFnCalls: { start: bigint; end: bigint }[] = []; + const mockFn = async (start: bigint, end: bigint) => { + mockFnCalls.push({ start, end }); + return { start, end }; + }; + + const options = { + start: 1n, + end: 20n, + maxSize: 5n, + }; + + const result = await getAllInBatches(mockFn, options); + + expect(mockFnCalls.length).toEqual(4); + expect(mockFnCalls[0]).toEqual({ start: 1n, end: 5n }); + expect(mockFnCalls[1]).toEqual({ start: 6n, end: 10n }); + expect(mockFnCalls[2]).toEqual({ start: 11n, end: 15n }); + expect(mockFnCalls[3]).toEqual({ start: 16n, end: 19n }); + + expect(result).toEqual([ + { start: 1n, end: 5n }, + { start: 6n, end: 10n }, + { start: 11n, end: 15n }, + { start: 16n, end: 19n }, + ]); + }); + + it("should handle single batch", async () => { + const mockFnCalls: { start: bigint; end: bigint }[] = []; + const mockFn = async (start: bigint, end: bigint) => { + mockFnCalls.push({ start, end }); + return { start, end }; + }; + + const options = { + start: 1n, + end: 4n, + maxSize: 10n, + }; + + const result = await getAllInBatches(mockFn, options); + + expect(mockFnCalls.length).toEqual(1); + expect(mockFnCalls[0]).toEqual({ start: 1n, end: 3n }); + + expect(result).toEqual([{ start: 1n, end: 3n }]); + }); +}); diff --git a/packages/thirdweb/src/extensions/marketplace/utils.ts b/packages/thirdweb/src/extensions/marketplace/utils.ts index c7a1efa50c6..309d3fe1ae6 100644 --- a/packages/thirdweb/src/extensions/marketplace/utils.ts +++ b/packages/thirdweb/src/extensions/marketplace/utils.ts @@ -87,7 +87,7 @@ export async function getAllInBatches( let start = options.start; const batches: Promise[] = []; while (options.end - start > options.maxSize) { - batches.push(fn(start, options.end + options.maxSize - 1n)); + batches.push(fn(start, start + options.maxSize - 1n)); start += options.maxSize; } batches.push(fn(start, options.end - 1n));