import { describe, test, expect } from "bun:test";
import { mergeArrays } from "../../src/modules/tools/mergeArrays";

describe("mergeArrays", () => {
  test("merges two disjoint arrays", () => {
    const result = mergeArrays([1, 2], [3, 4]);
    expect(result).toEqual([1, 2, 3, 4]);
  });

  test("removes duplicates from second array", () => {
    const result = mergeArrays([1, 2, 3], [2, 3, 4]);
    expect(result).toEqual([1, 2, 3, 4]);
  });

  test("handles empty first array", () => {
    const result = mergeArrays([], [1, 2]);
    expect(result).toEqual([1, 2]);
  });

  test("handles empty second array", () => {
    const result = mergeArrays([1, 2], []);
    expect(result).toEqual([1, 2]);
  });

  test("handles both arrays empty", () => {
    const result = mergeArrays([], []);
    expect(result).toEqual([]);
  });

  test("works with strings", () => {
    const result = mergeArrays(["a", "b"], ["b", "c"]);
    expect(result).toEqual(["a", "b", "c"]);
  });

  test("does not mutate original arrays", () => {
    const a = [1, 2];
    const b = [2, 3];
    const result = mergeArrays(a, b);
    expect(a).toEqual([1, 2]);
    expect(b).toEqual([2, 3]);
    expect(result).toEqual([1, 2, 3]);
  });

  test("accepts custom predicate", () => {
    const a = [{ id: 1, n: "a" }];
    const b = [
      { id: 1, n: "b" },
      { id: 2, n: "c" },
    ];
    const result = mergeArrays(a, b, (x: any, y: any) => x.id === y.id);
    expect(result).toHaveLength(2);
    expect(result[0]).toEqual({ id: 1, n: "a" });
    expect(result[1]).toEqual({ id: 2, n: "c" });
  });

  test("keeps all elements when predicate never matches", () => {
    const result = mergeArrays([1, 2], [3, 4], () => false);
    expect(result).toEqual([1, 2, 3, 4]);
  });
});
