repositories / pi-ext

pi-ext

bugabingas pi extensions

owned by bugabinga

extensions/llmiterate/__tests__/gitignore.test.ts

Raw
File outline
import { describe, expect, test } from "vitest";
import { GitIgnoreMatcher } from "../gitignore";

function m(): GitIgnoreMatcher {
	return new GitIgnoreMatcher();
}

describe("GitIgnoreMatcher — basics", () => {
	test("empty matcher ignores nothing", () => {
		const matcher = m();
		expect(matcher.ignores("a.ts")).toBe(false);
		expect(matcher.ignores("src/a.ts")).toBe(false);
		expect(matcher.ignores("")).toBe(false);
	});

	test("blank lines are skipped", () => {
		const matcher = m().add("\n\n  \n\t\n*.log\n");
		expect(matcher.ignores("a.log")).toBe(true);
	});

	test("comments starting with # are skipped", () => {
		const matcher = m().add("# ignore logs\n*.log\n");
		expect(matcher.ignores("a.log")).toBe(true);
		expect(matcher.ignores("# ignore logs")).toBe(false);
	});

	test("escaped hash at start is treated as literal", () => {
		const matcher = m().add("\\#secret\n");
		expect(matcher.ignores("#secret")).toBe(true);
		expect(matcher.ignores("secret")).toBe(false);
	});

	test("trailing spaces are ignored", () => {
		const matcher = m().add("*.log \n");
		expect(matcher.ignores("a.log")).toBe(true);
		expect(matcher.ignores("a.log ")).toBe(false);
	});

	test("escaped trailing space is literal", () => {
		const matcher = m().add("foo\\ \n");
		expect(matcher.ignores("foo ")).toBe(true);
		expect(matcher.ignores("foo")).toBe(false);
	});

	test("add returns this for chaining", () => {
		const matcher = m();
		expect(matcher.add("*.log")).toBe(matcher);
	});
});

describe("GitIgnoreMatcher — glob * and ?", () => {
	test("* matches single path component", () => {
		const matcher = m().add("*\n");
		expect(matcher.ignores("a")).toBe(true);
		expect(matcher.ignores("abc")).toBe(true);
		expect(matcher.ignores("a/b")).toBe(true); // matches component b
	});

	test("* does not match slash", () => {
		// Pattern * alone matches any single path component at any depth
		const matcher = m().add("foo*\n");
		expect(matcher.ignores("foobar")).toBe(true);
		expect(matcher.ignores("foo/bar")).toBe(false);
	});

	test("? matches single char except slash", () => {
		const matcher = m().add("?.tmp\n");
		expect(matcher.ignores("a.tmp")).toBe(true);
		expect(matcher.ignores("ab.tmp")).toBe(false);
		// Basename pattern at any depth: ? matches b in a/b.tmp
		expect(matcher.ignores("a/b.tmp")).toBe(true);
	});

	test("multiple * in pattern", () => {
		const matcher = m().add("*.*\n");
		expect(matcher.ignores("a.b")).toBe(true);
		expect(matcher.ignores("a.b.c")).toBe(true);
		expect(matcher.ignores("nodots")).toBe(false);
	});
});

describe("GitIgnoreMatcher — character classes", () => {
	test("[abc] matches single char from set", () => {
		const matcher = m().add("*.[to]mp\n");
		expect(matcher.ignores("a.tmp")).toBe(true);
		expect(matcher.ignores("a.omp")).toBe(true);
		expect(matcher.ignores("a.xmp")).toBe(false);
	});

	test("[a-z] range matches", () => {
		const matcher = m().add("file[0-9].txt\n");
		expect(matcher.ignores("file1.txt")).toBe(true);
		expect(matcher.ignores("file9.txt")).toBe(true);
		expect(matcher.ignores("filea.txt")).toBe(false);
	});

	test("negated class [!a-z] matches outside range", () => {
		const matcher = m().add("file[!0-9].txt\n");
		expect(matcher.ignores("filea.txt")).toBe(true);
		expect(matcher.ignores("file1.txt")).toBe(false);
	});

	test("multiple ranges in one class", () => {
		const matcher = m().add("file[a-zA-Z].txt\n");
		expect(matcher.ignores("filea.txt")).toBe(true);
		expect(matcher.ignores("fileZ.txt")).toBe(true);
		expect(matcher.ignores("file1.txt")).toBe(false);
	});

	test("unclosed [ is treated as literal", () => {
		const matcher = m().add("a[b\n");
		expect(matcher.ignores("a[b")).toBe(true);
		expect(matcher.ignores("ab")).toBe(false);
	});
});

describe("GitIgnoreMatcher — escaping", () => {
	test("escaped specials are literals", () => {
		const matcher = m().add("\\#file\n\\!important\nstar\\*.txt\n");
		expect(matcher.ignores("#file")).toBe(true);
		expect(matcher.ignores("!important")).toBe(true);
		expect(matcher.ignores("star*.txt")).toBe(true);
		expect(matcher.ignores("#other")).toBe(false);
	});

	test("escaped backslash is literal", () => {
		const matcher = m().add("a\\\\b\n");
		expect(matcher.ignores("a\\b")).toBe(true);
	});

	test("backslash escapes any character", () => {
		const matcher = m().add("\\a\n");
		expect(matcher.ignores("a")).toBe(true);
	});

	test("trailing backslash is invalid and never matches", () => {
		const matcher = m().add("foo\\\n");
		expect(matcher.ignores("foo")).toBe(false);
		expect(matcher.ignores("foo\\")).toBe(false);
		// Verify a valid pattern still works in same matcher
		const m2 = m().add("foo\\\nbar\n");
		expect(m2.ignores("bar")).toBe(true);
	});

	test("literal asterisk via escape", () => {
		const matcher = m().add("\\*.tmp\n");
		expect(matcher.ignores("*.tmp")).toBe(true);
		expect(matcher.ignores("a.tmp")).toBe(false);
	});

	test("literal question mark via escape", () => {
		const matcher = m().add("\\?.tmp\n");
		expect(matcher.ignores("?.tmp")).toBe(true);
		expect(matcher.ignores("a.tmp")).toBe(false);
	});
});

describe("GitIgnoreMatcher — anchoring /", () => {
	test("leading / anchors to context root", () => {
		const matcher = m().add("/build/\n");
		expect(matcher.ignores("build/")).toBe(true);
		expect(matcher.ignores("build/output.js")).toBe(true);
		expect(matcher.ignores("src/build/")).toBe(false);
	});

	test("anchored file pattern", () => {
		const matcher = m().add("/Makefile\n");
		expect(matcher.ignores("Makefile")).toBe(true);
		expect(matcher.ignores("src/Makefile")).toBe(false);
	});

	test("middle slash makes pattern relative to gitignore dir", () => {
		const matcher = m().add("doc/frotz/\n");
		expect(matcher.ignores("doc/frotz/")).toBe(true);
		expect(matcher.ignores("a/doc/frotz/")).toBe(false);
	});
});

describe("GitIgnoreMatcher — directory-only trailing /", () => {
	test("build/ ignores only directories", () => {
		const matcher = m().add("build/\n");
		expect(matcher.ignores("build/")).toBe(true);
		expect(matcher.ignores("build/output.js")).toBe(true);
		expect(matcher.ignores("build")).toBe(false);
	});

	test("pattern without trailing slash ignores files and directories", () => {
		const matcher = m().add("build\n");
		expect(matcher.ignores("build")).toBe(true);
		expect(matcher.ignores("build/")).toBe(true);
		expect(matcher.ignores("src/build")).toBe(true);
		expect(matcher.ignores("src/build/")).toBe(true);
	});

	test("frotz/ matches frotz and a/frotz directories", () => {
		const matcher = m().add("frotz/\n");
		expect(matcher.ignores("frotz/")).toBe(true);
		expect(matcher.ignores("a/frotz/")).toBe(true);
		expect(matcher.ignores("frotz")).toBe(false);
	});
});

describe("GitIgnoreMatcher — double-star **", () => {
	test("**/foo matches foo anywhere", () => {
		const matcher = m().add("**/foo\n");
		expect(matcher.ignores("foo")).toBe(true);
		expect(matcher.ignores("a/foo")).toBe(true);
		expect(matcher.ignores("a/b/foo")).toBe(true);
		expect(matcher.ignores("a/foobar")).toBe(false);
	});

	test("**/foo/bar matches bar directly under foo anywhere", () => {
		const matcher = m().add("**/foo/bar\n");
		expect(matcher.ignores("foo/bar")).toBe(true);
		expect(matcher.ignores("a/foo/bar")).toBe(true);
		expect(matcher.ignores("a/foo/b/bar")).toBe(false);
	});

	test("abc/** matches everything inside abc (relative)", () => {
		const matcher = m().add("abc/**\n");
		expect(matcher.ignores("abc/foo")).toBe(true);
		expect(matcher.ignores("abc/foo/bar")).toBe(true);
		expect(matcher.ignores("abc/")).toBe(false);
		expect(matcher.ignores("abc")).toBe(false);
		// Relative pattern: does NOT match x/abc/foo
		expect(matcher.ignores("x/abc/foo")).toBe(false);
	});

	test("a/**/b matches zero or more directories (relative)", () => {
		const matcher = m().add("a/**/b\n");
		expect(matcher.ignores("a/b")).toBe(true);
		expect(matcher.ignores("a/x/b")).toBe(true);
		expect(matcher.ignores("a/x/y/b")).toBe(true);
		expect(matcher.ignores("a/c")).toBe(false);
		// Relative pattern: does NOT match z/a/x/b
		expect(matcher.ignores("z/a/x/b")).toBe(false);
	});

	test("foo/**/bar/baz matches nested", () => {
		const matcher = m().add("foo/**/bar/baz\n");
		expect(matcher.ignores("foo/bar/baz")).toBe(true);
		expect(matcher.ignores("foo/x/bar/baz")).toBe(true);
		expect(matcher.ignores("foo/x/y/bar/baz")).toBe(true);
		expect(matcher.ignores("foo/bar/baz/qux")).toBe(true);
		expect(matcher.ignores("foo/bar/qux")).toBe(false);
	});

	test("** alone matches everything", () => {
		const matcher = m().add("**\n");
		expect(matcher.ignores("a")).toBe(true);
		expect(matcher.ignores("a/b/c")).toBe(true);
	});
});

describe("GitIgnoreMatcher — negation !", () => {
	test("!important.log negates previous ignore", () => {
		const matcher = m().add("*.log\n!important.log\n");
		expect(matcher.ignores("debug.log")).toBe(true);
		expect(matcher.ignores("important.log")).toBe(false);
	});

	test("multiple negations in sequence", () => {
		const matcher = m().add("*.log\n!a.log\n!b.log\n");
		expect(matcher.ignores("c.log")).toBe(true);
		expect(matcher.ignores("a.log")).toBe(false);
		expect(matcher.ignores("b.log")).toBe(false);
	});

	test("negation cannot re-include if parent dir is excluded", () => {
		const matcher = m().add("dist/\n!dist/keep.txt\n");
		expect(matcher.ignores("dist/keep.txt")).toBe(true);
	});

	test("anchored negation !/build", () => {
		const matcher = m().add("/build/\n!/build/keep/\n");
		expect(matcher.ignores("build/")).toBe(true);
		// Cannot re-include if parent is excluded per gitignore spec
		expect(matcher.ignores("build/keep/")).toBe(true);
	});

	test("negation with directory-only", () => {
		const matcher = m().add("logs/\n!logs/important/\n");
		expect(matcher.ignores("logs/debug/")).toBe(true);
		// Cannot re-include if parent is excluded per gitignore spec
		expect(matcher.ignores("logs/important/")).toBe(true);
	});
});

describe("GitIgnoreMatcher — parent directory propagation", () => {
	test("parent directory ignored ignores children", () => {
		const matcher = m().add("dist/\n");
		expect(matcher.ignores("dist/bundle.js")).toBe(true);
		expect(matcher.ignores("dist/assets/logo.png")).toBe(true);
		expect(matcher.ignores("dist/sub/deep/file")).toBe(true);
	});

	test("deep parent directory ignored", () => {
		const matcher = m().add("node_modules/\n");
		expect(matcher.ignores("node_modules/.package-lock.json")).toBe(true);
		expect(matcher.ignores("a/b/node_modules/c/d")).toBe(true);
	});
});

describe("GitIgnoreMatcher — real-world patterns", () => {
	test("node_modules", () => {
		const matcher = m().add("node_modules/\n");
		expect(matcher.ignores("node_modules/lodash/index.js")).toBe(true);
		expect(matcher.ignores("node_modules/.package-lock.json")).toBe(true);
		expect(matcher.ignores("src/node_modules/")).toBe(true);
	});

	test("dotenv files", () => {
		const matcher = m().add(".env\n.env.*\n!.env.example\n");
		expect(matcher.ignores(".env")).toBe(true);
		expect(matcher.ignores(".env.local")).toBe(true);
		expect(matcher.ignores(".env.production")).toBe(true);
		expect(matcher.ignores(".env.example")).toBe(false);
	});

	test("IDE directories", () => {
		const matcher = m().add(".idea/\n.vscode/\n*.swp\n*~\n");
		expect(matcher.ignores(".idea/workspace.xml")).toBe(true);
		expect(matcher.ignores(".vscode/settings.json")).toBe(true);
		expect(matcher.ignores("file.swp")).toBe(true);
		expect(matcher.ignores("backup~")).toBe(true);
	});

	test("build artifacts", () => {
		const matcher = m().add("dist/\nbuild/\ncoverage/\n*.min.js\n*.map\n");
		expect(matcher.ignores("dist/app.js")).toBe(true);
		expect(matcher.ignores("build/index.html")).toBe(true);
		expect(matcher.ignores("coverage/lcov-report")).toBe(true);
		expect(matcher.ignores("app.min.js")).toBe(true);
		expect(matcher.ignores("app.js.map")).toBe(true);
	});

	test("OS files", () => {
		const matcher = m().add(".DS_Store\nThumbs.db\n");
		expect(matcher.ignores(".DS_Store")).toBe(true);
		expect(matcher.ignores("src/.DS_Store")).toBe(true);
	});

	test("lockfiles", () => {
		const matcher = m().add("package-lock.json\nyarn.lock\n");
		expect(matcher.ignores("package-lock.json")).toBe(true);
		expect(matcher.ignores("yarn.lock")).toBe(true);
		expect(matcher.ignores("package.json")).toBe(false);
	});

	test("python artifacts", () => {
		const matcher = m().add("__pycache__/\n*.pyc\n*.pyo\n.pytest_cache/\n");
		expect(matcher.ignores("__pycache__/foo.cpython-311.pyc")).toBe(true);
		expect(matcher.ignores("module.pyc")).toBe(true);
		expect(matcher.ignores(".pytest_cache/v/cache/nodeids")).toBe(true);
	});

	test("Rust target", () => {
		const matcher = m().add("target/\n");
		expect(matcher.ignores("target/debug/app")).toBe(true);
		expect(matcher.ignores("target/release/libfoo.so")).toBe(true);
	});
});

describe("GitIgnoreMatcher — brutal edge cases", () => {
	test("pattern with only slash is invalid", () => {
		const matcher = m().add("/\n");
		expect(matcher.ignores("a")).toBe(false);
	});

	test("pattern . is invalid", () => {
		const matcher = m().add(".\n");
		expect(matcher.ignores(".")).toBe(false);
	});

	test("pattern .. is invalid", () => {
		const matcher = m().add("..\n");
		expect(matcher.ignores("..")).toBe(false);
	});

	test("pattern starting with ./ is invalid", () => {
		const matcher = m().add("./foo\n");
		expect(matcher.ignores("foo")).toBe(false);
	});

	test("pattern starting with ../ is invalid", () => {
		const matcher = m().add("../foo\n");
		expect(matcher.ignores("foo")).toBe(false);
	});

	test("consecutive slashes in pattern", () => {
		const matcher = m().add("a//b\n");
		expect(matcher.ignores("a//b")).toBe(true);
	});

	test("pattern with spaces in filename", () => {
		const matcher = m().add("my\\ file.txt\n");
		expect(matcher.ignores("my file.txt")).toBe(true);
	});

	test("unicode in pattern", () => {
		const matcher = m().add("*.äöü\n");
		expect(matcher.ignores("file.äöü")).toBe(true);
	});

	test("very long pattern still works", () => {
		const longName = "a".repeat(200);
		const matcher = m().add(`${longName}\n`);
		expect(matcher.ignores(longName)).toBe(true);
	});

	test("CRLF line endings", () => {
		const matcher = m().add("*.log\r\n*.tmp\r\n");
		expect(matcher.ignores("a.log")).toBe(true);
		expect(matcher.ignores("a.tmp")).toBe(true);
	});

	test("mixed line endings", () => {
		const matcher = m().add("*.log\n*.tmp\r\n*.bak\n");
		expect(matcher.ignores("a.log")).toBe(true);
		expect(matcher.ignores("a.tmp")).toBe(true);
		expect(matcher.ignores("a.bak")).toBe(true);
	});

	test("empty string add is no-op", () => {
		const matcher = m().add("");
		expect(matcher.ignores("a")).toBe(false);
	});

	test("pattern ending with just / after stripping is invalid", () => {
		const matcher = m().add("/\n");
		expect(matcher.ignores("/")).toBe(false);
	});

	test("negation of non-ignored file is no-op", () => {
		const matcher = m().add("!keep.txt\n");
		expect(matcher.ignores("keep.txt")).toBe(false);
	});

	test("pattern that matches after being negated then re-ignored", () => {
		const matcher = m().add("*.log\n!important.log\n*.log\n");
		expect(matcher.ignores("important.log")).toBe(true);
	});

	test("directory check on file pattern", () => {
		const matcher = m().add("build\n");
		expect(matcher.ignores("build")).toBe(true);
		expect(matcher.ignores("build/")).toBe(true);
		expect(matcher.ignores("src/build/")).toBe(true);
	});

	test("multiple add calls accumulate", () => {
		const matcher = m().add("*.log\n").add("*.tmp\n");
		expect(matcher.ignores("a.log")).toBe(true);
		expect(matcher.ignores("a.tmp")).toBe(true);
	});

	test("foo*bar matches foobar and fooXbar", () => {
		const matcher = m().add("foo*bar\n");
		expect(matcher.ignores("foobar")).toBe(true);
		expect(matcher.ignores("fooXbar")).toBe(true);
		expect(matcher.ignores("foo/bar")).toBe(false);
	});

	test("**/ prefix matches basename anywhere", () => {
		const matcher = m().add("**/Makefile\n");
		expect(matcher.ignores("Makefile")).toBe(true);
		expect(matcher.ignores("src/Makefile")).toBe(true);
		// Does NOT match Makefile.am — different basename
		expect(matcher.ignores("src/Makefile.am")).toBe(false);
	});

	test("deep nested dirs with **", () => {
		const matcher = m().add("**/node_modules/**\n");
		expect(matcher.ignores("node_modules/foo")).toBe(true);
		expect(matcher.ignores("a/node_modules/foo")).toBe(true);
		expect(matcher.ignores("a/node_modules/foo/bar")).toBe(true);
	});

	test("pattern with all special chars escaped", () => {
		const matcher = m().add("\\.\\[\\*\\?\\#\\!\\/\\]\n");
		expect(matcher.ignores(".[*?#!/]")).toBe(true);
	});
});

describe("GitIgnoreMatcher — git docs compliance", () => {
	test("doc/frotz/ matches doc/frotz but not a/doc/frotz", () => {
		const matcher = m().add("doc/frotz/\n");
		expect(matcher.ignores("doc/frotz/")).toBe(true);
		expect(matcher.ignores("a/doc/frotz/")).toBe(false);
	});

	test("frotz/ matches frotz and a/frotz directories", () => {
		const matcher = m().add("frotz/\n");
		expect(matcher.ignores("frotz/")).toBe(true);
		expect(matcher.ignores("a/frotz/")).toBe(true);
		expect(matcher.ignores("frotz")).toBe(false);
	});

	test("**/foo same as foo", () => {
		const m1 = m().add("**/foo\n");
		const m2 = m().add("foo\n");
		const paths = ["foo", "a/foo", "a/b/foo", "foobar"];
		for (const p of paths) {
			expect(m1.ignores(p)).toBe(m2.ignores(p));
		}
	});

	test("**/foo/bar matches bar directly under foo anywhere", () => {
		const matcher = m().add("**/foo/bar\n");
		expect(matcher.ignores("foo/bar")).toBe(true);
		expect(matcher.ignores("a/foo/bar")).toBe(true);
		expect(matcher.ignores("a/foo/b/bar")).toBe(false);
		expect(matcher.ignores("foo/bar/baz")).toBe(true);
	});

	test("abc/** matches all files inside abc with infinite depth", () => {
		const matcher = m().add("abc/**\n");
		expect(matcher.ignores("abc/foo")).toBe(true);
		expect(matcher.ignores("abc/foo/bar")).toBe(true);
		expect(matcher.ignores("abc/foo/bar/baz")).toBe(true);
		expect(matcher.ignores("abc/")).toBe(false);
		expect(matcher.ignores("abc")).toBe(false);
	});

	test("a/**/b matches a/b a/x/b a/x/y/b", () => {
		const matcher = m().add("a/**/b\n");
		expect(matcher.ignores("a/b")).toBe(true);
		expect(matcher.ignores("a/x/b")).toBe(true);
		expect(matcher.ignores("a/x/y/b")).toBe(true);
	});
});