import { expect, test } from "vitest";
import { mergePaginatedItems } from "./usePaginatedQuery.js";

test("mergePaginatedItems keeps ledger rows with the same user but different entry ids", () => {
    const merged = mergePaginatedItems(
        [
            { entryId: 1, userId: "1001", amount: 100 },
            { entryId: 2, userId: "1001", amount: 50 },
        ],
        [
            { entryId: 3, userId: "1001", amount: 20 },
            { entryId: 4, userId: "1002", amount: 10 },
        ],
    );

    expect(merged.map((item) => item.entryId)).toEqual([1, 2, 3, 4]);
});

test("mergePaginatedItems still removes duplicated rows by record id", () => {
    const merged = mergePaginatedItems(
        [{ entryId: 1, userId: "1001", amount: 100 }],
        [{ entryId: 1, userId: "1001", amount: 100 }],
    );

    expect(merged).toHaveLength(1);
    expect(merged[0].entryId).toBe(1);
});

test("mergePaginatedItems can still merge entity lists by user id", () => {
    const merged = mergePaginatedItems(
        [{ userId: "1001", username: "A" }],
        [
            { userId: "1001", username: "A" },
            { userId: "1002", username: "B" },
        ],
    );

    expect(merged.map((item) => item.userId)).toEqual(["1001", "1002"]);
});

test("mergePaginatedItems keeps recharge products by product id", () => {
    const merged = mergePaginatedItems(
        [{ productId: 1, productName: "普通用户档位" }],
        [{ productId: 2, productName: "币商档位" }],
    );

    expect(merged.map((item) => item.productId)).toEqual([1, 2]);
});
