53 lines · 1.7 KB
import { describe, expect, test } from "bun:test";
import { matchingLines, matchRanges } from "./code-search";

describe("matchRanges", () => {
  test("finds every term case-insensitively and merges overlaps", () => {
    expect(matchRanges("Foo foobar FOO", ["foo"])).toEqual([
      [0, 3],
      [4, 7],
      [11, 14],
    ]);
    expect(matchRanges("abcdef", ["abc", "bcd"])).toEqual([[0, 4]]);
  });
});

describe("matchingLines", () => {
  const content = [
    "import x",
    "const fetchUser = 1;",
    "noop",
    "await fetchUser();",
    "FETCHUSER",
    "fetchUser again",
  ].join("\n");

  test("returns numbered matching lines up to the limit and counts the rest", () => {
    const { lines, matchCount } = matchingLines(content, ["fetchUser"], 3);
    expect(matchCount).toBe(4);
    expect(lines.map((line) => line.number)).toEqual([2, 4, 5]);
    expect(lines[0]).toMatchObject({
      text: "const fetchUser = 1;",
      ranges: [{ start: 6, end: 15 }],
      clippedStart: false,
    });
  });

  test("clips long lines around the first match and shifts the ranges", () => {
    const long = `${"x".repeat(500)}needle${"y".repeat(500)}`;
    const [line] = matchingLines(long, ["needle"], 1).lines;
    expect(line?.clippedStart).toBe(true);
    expect(line?.clippedEnd).toBe(true);