Skip to content

Commit cdd082c

Browse files
committed
test util
1 parent 1547300 commit cdd082c

File tree

1 file changed

+54
-0
lines changed

1 file changed

+54
-0
lines changed
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import { describe, expect, it } from "vitest";
2+
import { getAllInBatches } from "./utils.js";
3+
4+
describe("getAllInBatches", () => {
5+
it("should handle range with multiple batches", async () => {
6+
const mockFnCalls: { start: bigint; end: bigint }[] = [];
7+
const mockFn = async (start: bigint, end: bigint) => {
8+
mockFnCalls.push({ start, end });
9+
return { start, end };
10+
};
11+
12+
const options = {
13+
start: 1n,
14+
end: 20n,
15+
maxSize: 5n,
16+
};
17+
18+
const result = await getAllInBatches(mockFn, options);
19+
20+
expect(mockFnCalls.length).toEqual(4);
21+
expect(mockFnCalls[0]).toEqual({ start: 1n, end: 5n });
22+
expect(mockFnCalls[1]).toEqual({ start: 6n, end: 10n });
23+
expect(mockFnCalls[2]).toEqual({ start: 11n, end: 15n });
24+
expect(mockFnCalls[3]).toEqual({ start: 16n, end: 19n });
25+
26+
expect(result).toEqual([
27+
{ start: 1n, end: 5n },
28+
{ start: 6n, end: 10n },
29+
{ start: 11n, end: 15n },
30+
{ start: 16n, end: 19n },
31+
]);
32+
});
33+
34+
it("should handle single batch", async () => {
35+
const mockFnCalls: { start: bigint; end: bigint }[] = [];
36+
const mockFn = async (start: bigint, end: bigint) => {
37+
mockFnCalls.push({ start, end });
38+
return { start, end };
39+
};
40+
41+
const options = {
42+
start: 1n,
43+
end: 4n,
44+
maxSize: 10n,
45+
};
46+
47+
const result = await getAllInBatches(mockFn, options);
48+
49+
expect(mockFnCalls.length).toEqual(1);
50+
expect(mockFnCalls[0]).toEqual({ start: 1n, end: 3n });
51+
52+
expect(result).toEqual([{ start: 1n, end: 3n }]);
53+
});
54+
});

0 commit comments

Comments
 (0)