File size: 3,617 Bytes
71174bc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
import { getSetting } from "@/Plugins/Core/Settings/LoadSaveSettings";
import { batchify } from "./BatchUtils";

// Mock the getSetting function
jest.mock("@/Plugins/Core/Settings/LoadSaveSettings", () => ({
  getSetting: jest.fn(),
}));

describe("batchify", () => {
  beforeEach(() => {
    jest.clearAllMocks();
  });

  it("should create batches with the specified number of batches", async () => {
    const list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
    const numBatches = 3;
    
    const result = await batchify(list, numBatches);
    
    // Should create 3 batches: [1,2,3,4], [5,6,7,8], [9,10]
    expect(result).toHaveLength(3);
    expect(result[0]).toEqual([1, 2, 3, 4]);
    expect(result[1]).toEqual([5, 6, 7, 8]);
    expect(result[2]).toEqual([9, 10]);
  });

  it("should use maxProcs setting when numBatches is not specified", async () => {
    const list = [1, 2, 3, 4, 5, 6, 7, 8];
    
    // Mock the getSetting function to return 4
    (getSetting as jest.Mock).mockResolvedValue(4);
    
    const result = await batchify(list);
    
    // Should have called getSetting
    expect(getSetting).toHaveBeenCalledWith("maxProcs");
    
    // Should create 4 batches: [1,2], [3,4], [5,6], [7,8]
    expect(result).toHaveLength(4);
    expect(result[0]).toEqual([1, 2]);
    expect(result[1]).toEqual([3, 4]);
    expect(result[2]).toEqual([5, 6]);
    expect(result[3]).toEqual([7, 8]);
  });

  it("should use maxProcs setting when numBatches is null", async () => {
    const list = [1, 2, 3, 4, 5, 6, 7, 8];
    
    // Mock the getSetting function to return 2
    (getSetting as jest.Mock).mockResolvedValue(2);
    
    const result = await batchify(list, null);
    
    // Should have called getSetting
    expect(getSetting).toHaveBeenCalledWith("maxProcs");
    
    // Should create 2 batches: [1,2,3,4], [5,6,7,8]
    expect(result).toHaveLength(2);
    expect(result[0]).toEqual([1, 2, 3, 4]);
    expect(result[1]).toEqual([5, 6, 7, 8]);
  });

  it("should handle empty lists", async () => {
    const list: number[] = [];
    const numBatches = 3;
    
    const result = await batchify(list, numBatches);
    
    // Should return an empty array
    expect(result).toHaveLength(0);
  });

  it("should handle lists smaller than the number of batches", async () => {
    const list = [1, 2, 3];
    const numBatches = 5;
    
    const result = await batchify(list, numBatches);
    
    // Should create 3 batches with 1 item each
    expect(result).toHaveLength(3);
    expect(result[0]).toEqual([1]);
    expect(result[1]).toEqual([2]);
    expect(result[2]).toEqual([3]);
  });

  it("should maintain correct types with generic lists", async () => {
    const list = ["a", "b", "c", "d", "e", "f"];
    const numBatches = 2;
    
    const result = await batchify(list, numBatches);
    
    // Should create 2 batches: ["a","b","c"], ["d","e","f"]
    expect(result).toHaveLength(2);
    expect(result[0]).toEqual(["a", "b", "c"]);
    expect(result[1]).toEqual(["d", "e", "f"]);
  });

  it("should handle complex object lists", async () => {
    const list = [
      { id: 1, name: "Item 1" },
      { id: 2, name: "Item 2" },
      { id: 3, name: "Item 3" },
      { id: 4, name: "Item 4" }
    ];
    const numBatches = 2;
    
    const result = await batchify(list, numBatches);
    
    // Should create 2 batches
    expect(result).toHaveLength(2);
    expect(result[0]).toEqual([
      { id: 1, name: "Item 1" },
      { id: 2, name: "Item 2" }
    ]);
    expect(result[1]).toEqual([
      { id: 3, name: "Item 3" },
      { id: 4, name: "Item 4" }
    ]);
  });
});