import { describe, expect, it } from "vitest";
import { rangeFor, type RangeMode } from "#lib/components/ui/rangeMode.ts";
// 5 rows, the range sits on 1..3, and nothing shorter than 3 rows is legal
const at = (mode: RangeMode, row: number, moving: "start" | "end" = "start") =>
rangeFor(mode, row, { cur: { start: 1, end: 3 }, minLength: 3, count: 5, moving });
describe("rangeFor", () => {
it("resizes the moving end and keeps the other fixed", () => {
expect(at("resize", 0)).toEqual({ start: 0, end: 3 });
expect(at("resize", 1)).toEqual({ start: 1, end: 3 });
expect(at("resize", 4, "end")).toEqual({ start: 1, end: 4 });
// too close to the fixed end
expect(at("resize", 2)).toBeNull();
});
it("lets the moving end cross the other in resize", () => {
const crossed = rangeFor("resize", 4, {
cur: { start: 1, end: 3 },
minLength: 1,
count: 5,
moving: "start"
});
expect(crossed).toEqual({ start: 3, end: 4 });
});
it("keeps resize-top and resize-bottom one-sided", () => {
expect(at("resize-top", 0)).toEqual({ start: 0, end: 3 });
expect(at("resize-top", 1)).toEqual({ start: 1, end: 3 });
expect(at("resize-top", 2)).toBeNull();
expect(at("resize-bottom", 4)).toEqual({ start: 1, end: 4 });
expect(at("resize-bottom", 3)).toEqual({ start: 1, end: 3 });
expect(at("resize-bottom", 2)).toBeNull();
});
it("pushes the far end only as far as minLength needs", () => {
expect(at("resize-top-push", 0)).toEqual({ start: 0, end: 3 });
expect(at("resize-top-push", 2)).toEqual({ start: 2, end: 4 });
// pushing the bottom to row 5 would run off the list
expect(at("resize-top-push", 3)).toBeNull();
expect(at("resize-bottom-push", 4)).toEqual({ start: 1, end: 4 });
expect(at("resize-bottom-push", 2)).toEqual({ start: 0, end: 2 });
expect(at("resize-bottom-push", 1)).toBeNull();
});
it("selects a minLength span anchored on the pointed row", () => {
expect(at("select-top", 0)).toEqual({ start: 0, end: 2 });
expect(at("select-top", 2)).toEqual({ start: 2, end: 4 });
expect(at("select-top", 3)).toBeNull();
expect(at("select-top", 4)).toBeNull();
expect(at("select-bottom", 4)).toEqual({ start: 2, end: 4 });
expect(at("select-bottom", 2)).toEqual({ start: 0, end: 2 });
expect(at("select-bottom", 1)).toBeNull();
});
it("selects the single pointed row at the default minLength", () => {
const one = { cur: { start: 1, end: 3 }, minLength: 1, count: 5, moving: "start" } as const;
expect(rangeFor("select-top", 4, one)).toEqual({ start: 4, end: 4 });
expect(rangeFor("select-bottom", 0, one)).toEqual({ start: 0, end: 0 });
});
});