import { describe, expect, it } from "vitest";
import { reconcileStableStringEntries } from "../stableListEntries";

describe("reconcileStableStringEntries", () => {
  it("preserves stable ids for unchanged values and generates new ids for inserted values", () => {
    const previous = [
      { id: "a", value: "foo" },
      { id: "b", value: "bar" },
      { id: "c", value: "foo" },
    ];

    const next = reconcileStableStringEntries(previous, ["foo", "baz", "foo"]);

    expect(next).toHaveLength(3);
    if (next.length !== 3) throw new Error("expected three entries");
    const first = next[0]!;
    const second = next[1]!;
    const third = next[2]!;

    expect(first.id).toBe("a");
    expect(first.value).toBe("foo");
    expect(second.value).toBe("baz");
    expect(second.id).not.toBe("b");
    expect(third.id).toBe("c");
  });

  it("reuses ids when string values move positions", () => {
    const previous = [
      { id: "a", value: "foo" },
      { id: "b", value: "bar" },
      { id: "c", value: "baz" },
    ];

    const next = reconcileStableStringEntries(previous, ["bar", "foo", "baz"]);

    if (next.length !== 3) throw new Error("expected three entries");
    const first = next[0]!;
    const second = next[1]!;
    const third = next[2]!;
    expect(first.id).toBe("b");
    expect(second.id).toBe("a");
    expect(third.id).toBe("c");
  });
});
