import * as fs from "node:fs"; import * as os from "node:os"; import * as path from "node:path"; import { afterEach, describe, expect, it } from "vitest"; import { assertSafeGlob, createDirDoc, createFileDoc, createGlobDoc, expandPulseDocuments, globToRegExp, normalizeDoc, safeRelativePath, stripUnsafeText, } 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"); }); it("rejects existing symlinks that resolve outside the workspace", () => { const root = cwd(); const outside = fs.mkdtempSync(path.join(os.tmpdir(), "pulse-outside-")); try { fs.writeFileSync(path.join(outside, "secret.md"), "secret"); fs.symlinkSync( path.join(outside, "secret.md"), path.join(root, "link.md"), ); fs.symlinkSync(outside, path.join(root, "outside-dir"), "dir"); expect(() => createFileDoc(root, "link.md")).toThrow("unsafe path"); expect(() => createDirDoc(root, "outside-dir", "**/*.md")).toThrow( "unsafe path", ); expect(expandPulseDocuments(root, [{ path: "link.md" }])).toEqual([]); expect( expandPulseDocuments(root, [ { path: "outside-dir", type: "dir", glob: "**/*.md" }, ]), ).toEqual([]); } finally { fs.rmSync(outside, { recursive: true, force: true }); } }); }); 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(); }); it("drops invalid doc model overrides instead of silently falling back", () => { const root = cwd(); expect( normalizeDoc(root, { path: "README.md", model: "anthropic/claude" }), ).toEqual({ path: "README.md", model: "anthropic/claude" }); expect( normalizeDoc(root, { path: "README.md", model: "missing-slash" }), ).toBeNull(); expect( normalizeDoc(root, { path: "README.md", model: "bad/model/extra" }), ).toBeNull(); }); }); describe("display-safe formatting", () => { it("strips terminal controls", () => { expect(stripUnsafeText("ok\u001b[31mred\u001b[0m\u009b31m")).toBe("okred"); }); }); 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" }]); }); });