import { afterEach, describe, expect, it } from "bun:test"; import * as fs from "node:fs"; import * as os from "node:os"; import * as path from "node:path"; import { assertSafeGlob, createDirDoc, createFileDoc, createGlobDoc, expandPulseDocuments, globToRegExp, normalizeDoc, safeRelativePath, } from "../core.ts"; let tmp = ""; function cwd(): string { tmp = fs.mkdtempSync(path.join(os.tmpdir(), "pulse-")); return tmp; } function file(root: string, rel: string, text = rel): void { const p = path.join(root, rel); fs.mkdirSync(path.dirname(p), { recursive: true }); fs.writeFileSync(p, text); } afterEach(() => { if (tmp) fs.rmSync(tmp, { recursive: true, force: true }); tmp = ""; }); describe("safeRelativePath", () => { it("normalizes inside paths and rejects escapes", () => { const root = cwd(); expect(safeRelativePath(root, "docs/../README.md")).toBe("README.md"); expect(() => safeRelativePath(root, "../README.md")).toThrow("unsafe path"); expect(() => safeRelativePath(root, "/etc/passwd")).toThrow("unsafe path"); }); }); describe("globToRegExp", () => { it("makes **/ match zero or more dirs, not wishful nonsense", () => { const re = globToRegExp("docs/**/*.md"); expect(re.test("docs/a.md")).toBe(true); expect(re.test("docs/a/b.md")).toBe(true); expect(re.test("docs/a/b.txt")).toBe(false); expect(re.test("x/docs/a.md")).toBe(false); }); }); describe("doc creation", () => { it("refuses to smuggle dirs through file docs", () => { const root = cwd(); fs.mkdirSync(path.join(root, "docs")); expect(() => createFileDoc(root, "docs")).toThrow("not a file"); }); it("requires dir docs to be explicit and bounded", () => { const root = cwd(); fs.mkdirSync(path.join(root, "docs")); expect(createDirDoc(root, "docs", "**/*.md")).toEqual({ path: "docs", type: "dir", glob: "**/*.md" }); expect(() => createDirDoc(root, "../docs", "**/*.md")).toThrow("unsafe path"); expect(() => createDirDoc(root, "docs", "../*.md")).toThrow("unsafe glob"); }); it("keeps glob docs explicit and unable to escape", () => { expect(createGlobDoc("docs/**/*.md")).toEqual({ path: "docs/**/*.md", type: "glob" }); expect(() => createGlobDoc("../**/*.md")).toThrow("unsafe glob"); expect(() => assertSafeGlob("/tmp/*.md")).toThrow("unsafe glob"); }); }); describe("normalizeDoc", () => { it("drops invalid persisted specs instead of turning them into reads", () => { const root = cwd(); expect(normalizeDoc(root, { path: "../x.md" })).toBeNull(); expect(normalizeDoc(root, { path: "docs", type: "bogus" as never })).toBeNull(); }); }); describe("expandPulseDocuments", () => { it("expands dir+glob to sorted files only", () => { const root = cwd(); file(root, "docs/z.md"); file(root, "docs/a.md"); file(root, "docs/deep/b.md"); file(root, "docs/deep/b.txt"); expect(expandPulseDocuments(root, [{ path: "docs", type: "dir", glob: "**/*.md", instruction: "docs" }])).toEqual([ { path: "docs/a.md", type: "file", glob: "**/*.md", instruction: "docs" }, { path: "docs/deep/b.md", type: "file", glob: "**/*.md", instruction: "docs" }, { path: "docs/z.md", type: "file", glob: "**/*.md", instruction: "docs" }, ]); }); it("expands glob specs and keeps misses empty", () => { const root = cwd(); file(root, "a/one.md"); file(root, "b/two.md"); file(root, "b/two.txt"); expect(expandPulseDocuments(root, [{ path: "**/*.md", type: "glob" }]).map((d) => d.path)).toEqual(["a/one.md", "b/two.md"]); expect(expandPulseDocuments(root, [{ path: "**/*.rst", type: "glob" }])).toEqual([]); }); it("does not follow symlink dirs into loops", () => { const root = cwd(); file(root, "docs/a.md"); try { fs.symlinkSync(root, path.join(root, "docs/loop"), "dir"); } catch {} expect(expandPulseDocuments(root, [{ path: "docs", type: "dir", glob: "**/*.md" }]).map((d) => d.path)).toEqual(["docs/a.md"]); }); it("skips deleted files, escaped specs, and accidental file-globs", () => { const root = cwd(); fs.mkdirSync(path.join(root, "docs")); file(root, "ok.md"); expect(expandPulseDocuments(root, [ { path: "missing.md" }, { path: "docs" }, { path: "../evil.md" }, { path: "ok.md" }, ])).toEqual([{ path: "ok.md", type: "file" }]); }); it("dedupes overlapping file, dir, and glob specs by first hit", () => { const root = cwd(); file(root, "docs/a.md"); expect(expandPulseDocuments(root, [ { path: "docs/a.md", instruction: "first" }, { path: "docs", type: "dir", glob: "**/*.md", instruction: "second" }, { path: "docs/**/*.md", type: "glob", instruction: "third" }, ])).toEqual([{ path: "docs/a.md", instruction: "first", type: "file" }]); }); });