import { describe, expect, it } from 'vitest';
import {
  generateArrayMutations,
  generateArrayMutationsWithUpdates,
} from './generateArrayMutations';

describe('generateArrayMutations', () => {
  it.each([
    [[1, 2, 3], [1, 2], ['create0:3']],
    [[2, 3], [1, 2, 3], ['delete0:1']],
    [[], [1, 2, 3], ['delete0:1', 'delete1:2', 'delete2:3']],
    [[1, 2, 3], [], ['create0:1', 'create1:2', 'create2:3']],
    [[1], [2], ['delete0:2', 'create0:1']],
    [[], [2], ['delete0:2']],
    [[1], [], ['create0:1']],
    [[1, 2, 3], [1, 2, 3], []],
  ])(
    'finds differences in primitive type array correctly',
    (current, original, result) => {
      expect(
        generateArrayMutations({
          current,
          original,
          generateCreateMutation: (i) => String(i),
          generateDeleteMutation: (i) => String(i),
        }),
      ).toEqual(result);
    },
  );
});

describe('generateArrayMutationsWithUpdates', () => {
  it.each([
    [
      [{ id: 1 }, { id: 2 }, { id: 3 }],
      [{ id: 1 }, { id: 2 }],
      ['create0:{"id":3}'],
    ],
    [
      [{ id: 2 }, { id: 3 }],
      [{ id: 1 }, { id: 2 }, { id: 3 }],
      ['delete0:{"id":1}'],
    ],
    [
      [
        { id: 1, key: 'value1' },
        { id: 2, key: 'value2' },
      ],
      [
        { id: 1, key: 'value' },
        { id: 2, key: 'value' },
      ],
      ['updated0:{"id":1,"key":"value1"}', 'updated1:{"id":2,"key":"value2"}'],
    ],
  ])(
    'finds differences in object array correctly',
    (current, original, result) => {
      expect(
        generateArrayMutationsWithUpdates({
          current,
          original,
          generateCreateMutation: (i) => JSON.stringify(i),
          generateDeleteMutation: (i) => JSON.stringify(i),
          generateUpdateMutation: (i) => JSON.stringify(i),
          key: 'id',
        }),
      ).toEqual(result);
    },
  );
});
