~james/core

core/web/src/lib/components/lines/search.test.ts -rw-r--r-- 912 bytes
452f17b7 — james chore(import): snapshot james.dad.town/core at f70c71c7696a 16 hours ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import { describe, expect, it } from "vitest";
import { findMatches, wrapIndex } from "#lib/components/lines/search.ts";

describe("findMatches", () => {
	it("finds every case-insensitive hit with one-based lines", () => {
		expect(findMatches(["Foo foo", "bar", "xfoo"], "foo")).toEqual([
			{ line: 1, start: 0, end: 3 },
			{ line: 1, start: 4, end: 7 },
			{ line: 3, start: 1, end: 4 }
		]);
	});

	it("treats the query as text, not a pattern", () => {
		expect(findMatches(["a.b", "axb"], "a.b")).toEqual([{ line: 1, start: 0, end: 3 }]);
	});

	it("finds nothing for an empty query and stops at the limit", () => {
		expect(findMatches(["a"], "")).toEqual([]);
		expect(findMatches(["aaaa"], "a", 2)).toHaveLength(2);
	});
});

describe("wrapIndex", () => {
	it("wraps both ways", () => {
		expect(wrapIndex(-1, 3)).toBe(2);
		expect(wrapIndex(3, 3)).toBe(0);
		expect(wrapIndex(0, 0)).toBe(-1);
	});
});