import { describe, expect, it } from "vitest"; import { reinsertAt } from "./reinsert"; describe("reinsertAt", () => { it("puts a row back where it was when nothing changed", () => { expect(reinsertAt(["a", "b", "c"], ["b"], ["a", "c"])).toEqual({ keys: ["b"], positions: [1], }); }); it("follows its neighbour when a row was added above it meanwhile", () => { // "x" landed at the top between the removal and the Undo: index 1 would now sit before "a". expect(reinsertAt(["a", "b", "c"], ["b"], ["x", "a", "c"])).toEqual({ keys: ["b"], positions: [2], }); }); it("follows its neighbour through a reorder", () => { expect(reinsertAt(["a", "b", "c"], ["b"], ["c", "a"])).toEqual({ keys: ["b"], positions: [2] }); }); it("walks further back when its neighbour is gone too", () => { expect(reinsertAt(["a", "b", "c", "d"], ["c"], ["b", "d"])).toEqual({ keys: ["c"], positions: [1], }); }); it("goes to the start when it was first or no earlier row is left", () => { expect(reinsertAt(["a", "b"], ["a"], ["x", "b"])).toEqual({ keys: ["a"], positions: [0] }); expect(reinsertAt(["a", "b", "c"], ["b"], ["c"])).toEqual({ keys: ["b"], positions: [0] }); }); it("keeps scattered and adjacent removed rows in their order, ascending", () => { // A season's episodes interleaved with another's: b and d out, then "x" added at the top. expect(reinsertAt(["a", "b", "c", "d", "e"], ["d", "b"], ["x", "a", "c", "e"])).toEqual({ keys: ["b", "d"], positions: [2, 4], }); expect(reinsertAt(["a", "b", "c", "d"], ["b", "c"], ["a", "d"])).toEqual({ keys: ["b", "c"], positions: [1, 2], }); }); it("skips a row that is already back", () => { expect(reinsertAt(["a", "b", "c"], ["b", "c"], ["a", "c"])).toEqual({ keys: ["b"], positions: [1], }); }); });