Dax Raad 3c21735b35 refactor: migrate from Bun.Glob to npm glob package
Replace Bun.Glob usage with a new Glob utility wrapper around the npm 'glob' package.
This moves us off Bun-specific APIs toward standard Node.js compatible solutions.

Changes:
- Add new src/util/glob.ts utility module with scan(), scanSync(), and match()
- Default include option is 'file' (only returns files, not directories)
- Add symlink option (default: false) to control symlink following
- Migrate all 12 files using Bun.Glob to use the new Glob utility
- Add comprehensive tests for the glob utility

Breaking changes:
- Removed support for include: 'dir' option (use include: 'all' and filter manually)
- symlink now defaults to false (was true in most Bun.Glob usages)

Files migrated:
- src/util/log.ts
- src/util/filesystem.ts
- src/tool/truncation.ts
- src/session/instruction.ts
- src/storage/json-migration.ts
- src/storage/storage.ts
- src/project/project.ts
- src/cli/cmd/tui/context/theme.tsx
- src/config/config.ts
- src/tool/registry.ts
- src/skill/skill.ts
- src/file/ignore.ts
2026-02-19 12:34:18 -05:00

92 lines
3.2 KiB
TypeScript

import { describe, test, expect } from "bun:test"
import path from "path"
import fs from "fs/promises"
import { Glob } from "../../src/util/glob"
import { tmpdir } from "../fixture/fixture"
describe("glob", () => {
describe("glob()", () => {
test("finds files matching pattern", async () => {
await using tmp = await tmpdir()
await fs.writeFile(path.join(tmp.path, "test.txt"), "content", "utf-8")
await fs.writeFile(path.join(tmp.path, "other.txt"), "content", "utf-8")
await fs.writeFile(path.join(tmp.path, "skip.md"), "content", "utf-8")
const results = await Glob.scan("*.txt", { cwd: tmp.path })
expect(results.sort()).toEqual(["other.txt", "test.txt"])
})
test("returns absolute paths when absolute option is true", async () => {
await using tmp = await tmpdir()
await fs.writeFile(path.join(tmp.path, "test.txt"), "content", "utf-8")
const results = await Glob.scan("*.txt", { cwd: tmp.path, absolute: true })
expect(results[0]).toStartWith(tmp.path)
expect(path.isAbsolute(results[0])).toBe(true)
})
test("filters to only files when include is 'file'", async () => {
await using tmp = await tmpdir()
await fs.mkdir(path.join(tmp.path, "subdir"))
await fs.writeFile(path.join(tmp.path, "file.txt"), "content", "utf-8")
const results = await Glob.scan("*", { cwd: tmp.path, include: "file" })
expect(results).toEqual(["file.txt"])
})
test("includes both files and directories when include is 'all'", async () => {
await using tmp = await tmpdir()
await fs.mkdir(path.join(tmp.path, "subdir"))
await fs.writeFile(path.join(tmp.path, "file.txt"), "content", "utf-8")
const results = await Glob.scan("*", { cwd: tmp.path, include: "all" })
expect(results.sort()).toEqual(["file.txt", "subdir"])
})
test("handles nested patterns", async () => {
await using tmp = await tmpdir()
await fs.mkdir(path.join(tmp.path, "nested"), { recursive: true })
await fs.writeFile(path.join(tmp.path, "nested", "deep.txt"), "content", "utf-8")
const results = await Glob.scan("**/*.txt", { cwd: tmp.path })
expect(results).toEqual(["nested/deep.txt"])
})
test("returns empty array for no matches", async () => {
await using tmp = await tmpdir()
const results = await Glob.scan("*.nonexistent", { cwd: tmp.path })
expect(results).toEqual([])
})
})
describe("match()", () => {
test("matches simple patterns", () => {
expect(Glob.match("*.txt", "file.txt")).toBe(true)
expect(Glob.match("*.txt", "file.js")).toBe(false)
})
test("matches directory patterns", () => {
expect(Glob.match("**/*.js", "src/index.js")).toBe(true)
expect(Glob.match("**/*.js", "src/index.ts")).toBe(false)
})
test("matches dot files", () => {
expect(Glob.match(".*", ".gitignore")).toBe(true)
expect(Glob.match("**/*.md", ".github/README.md")).toBe(true)
})
test("matches brace expansion", () => {
expect(Glob.match("*.{js,ts}", "file.js")).toBe(true)
expect(Glob.match("*.{js,ts}", "file.ts")).toBe(true)
expect(Glob.match("*.{js,ts}", "file.py")).toBe(false)
})
})
})