import { describe, expect, it } from "vitest";
import { lineAndColumnsAt } from "./line-offset-map";

describe("targeted line and column resolution", () => {
  it("resolves sorted and duplicate offsets without retaining every line", () => {
    const result = lineAndColumnsAt("a\n😀\nlast", [9, 2, 0, 5, 2]);

    expect(result.size).toBe(4);
    expect(result.get(0)).toEqual({ line: 1, column: 1 });
    expect(result.get(2)).toEqual({ line: 2, column: 1 });
    expect(result.get(5)).toEqual({ line: 3, column: 1 });
    expect(result.get(9)).toEqual({ line: 3, column: 5 });
  });

  it("rejects invalid offsets", () => {
    expect(() => lineAndColumnsAt("a", [-1])).toThrow(/outside/u);
    expect(() => lineAndColumnsAt("a", [2])).toThrow(/outside/u);
  });

  it("recognizes every ECMAScript line terminator and treats CRLF as one", () => {
    const source = "a\r\nb\rc\u2028d\u2029e";
    const result = lineAndColumnsAt(source, [0, 2, 3, 5, 7, 9, 10]);

    expect(result.get(0)).toEqual({ line: 1, column: 1 });
    expect(result.get(2)).toEqual({ line: 1, column: 3 });
    expect(result.get(3)).toEqual({ line: 2, column: 1 });
    expect(result.get(5)).toEqual({ line: 3, column: 1 });
    expect(result.get(7)).toEqual({ line: 4, column: 1 });
    expect(result.get(9)).toEqual({ line: 5, column: 1 });
    expect(result.get(10)).toEqual({ line: 5, column: 2 });
  });
});
