feat(coding-agent): implemented gitignore-aware scan command to ttsr cli
- Added the `scan` action to `ttsr` CLI with support for gitignore-aware file globbing. - Integrated AST pre-filtering and optimized AST/regex matching to evaluate scan rules. - Implemented file size limits, binary file detection, and custom `--no-gitignore` and `--max-bytes` flags. - Introduced comprehensive test suites validating directory mapping, exclusions, and size-limit enforcement.
This commit is contained in:
@@ -1,12 +1,15 @@
|
||||
import { afterEach, describe, expect, it } from "bun:test";
|
||||
import { afterEach, beforeEach, describe, expect, it } from "bun:test";
|
||||
import * as fs from "node:fs";
|
||||
import * as path from "node:path";
|
||||
import {
|
||||
runTtsrCommand,
|
||||
TTSR_SOURCES,
|
||||
type TtsrCommandArgs,
|
||||
type TtsrScanArgs,
|
||||
type TtsrTestArgs,
|
||||
} from "@oh-my-pi/pi-coding-agent/cli/ttsr-cli";
|
||||
import { resetSettingsForTest } from "@oh-my-pi/pi-coding-agent/config/settings";
|
||||
import { getProjectAgentDir, getProjectDir, setProjectDir } from "@oh-my-pi/pi-utils";
|
||||
|
||||
// Capture stdout writes so assertions don't leak to the test runner.
|
||||
let stdout = "";
|
||||
@@ -24,9 +27,12 @@ class ExitSignal extends Error {
|
||||
|
||||
function captureStreams(): void {
|
||||
stdout = "";
|
||||
process.exitCode = undefined;
|
||||
process.exitCode = 0;
|
||||
process.stdout.write = ((chunk: string | Uint8Array) => {
|
||||
stdout += chunk.toString();
|
||||
// Strip ANSI so assertions match the rendered text rather than chalk's
|
||||
// color escapes; JSON output paths emit no color codes, so this is a
|
||||
// no-op for those tests.
|
||||
stdout += Bun.stripANSI(chunk.toString());
|
||||
return true;
|
||||
}) as typeof process.stdout.write;
|
||||
process.exit = ((code?: number) => {
|
||||
@@ -186,6 +192,300 @@ describe("omp ttsr", () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe("scan — directory scanning", () => {
|
||||
let originalCwd: string;
|
||||
|
||||
beforeEach(() => {
|
||||
originalCwd = getProjectDir();
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
setProjectDir(originalCwd);
|
||||
resetSettingsForTest();
|
||||
});
|
||||
|
||||
it("finds matching rules for files in directory matching rules globs/scopes", async () => {
|
||||
captureStreams();
|
||||
|
||||
// 1. Create a temporary project root directory
|
||||
const projectDir = path.join(import.meta.dir, `.tmp-ttsr-project-${Math.random().toString(36).slice(2)}`);
|
||||
fs.mkdirSync(path.join(projectDir, "src"), { recursive: true });
|
||||
|
||||
// 2. Set project root
|
||||
setProjectDir(projectDir);
|
||||
|
||||
// 3. Create a test file in projectDir/src/foo.ts
|
||||
const testFile = path.join(projectDir, "src/foo.ts");
|
||||
await Bun.write(testFile, "const x: any = 1;");
|
||||
|
||||
// 4. Create isolated rule with glob/scope
|
||||
const rulePath = await writeTempRule(": any", ["tool:edit(src/**/*.ts)"]);
|
||||
|
||||
const scan: TtsrScanArgs = {
|
||||
directory: "src",
|
||||
rule: rulePath,
|
||||
verbose: true,
|
||||
};
|
||||
|
||||
await run({ action: "scan", scan });
|
||||
|
||||
expect(stdout).toContain("TTSR scan");
|
||||
expect(stdout).toContain("Found violations/matches");
|
||||
expect(stdout).toContain("src/foo.ts");
|
||||
expect(stdout).toContain("test-rule");
|
||||
expect(process.exitCode).toBeFalsy();
|
||||
});
|
||||
|
||||
it("keeps default scan output summary-only", async () => {
|
||||
captureStreams();
|
||||
|
||||
const projectDir = path.join(import.meta.dir, `.tmp-ttsr-project-${Math.random().toString(36).slice(2)}`);
|
||||
fs.mkdirSync(path.join(projectDir, "src"), { recursive: true });
|
||||
setProjectDir(projectDir);
|
||||
|
||||
await Bun.write(path.join(projectDir, "src/foo.ts"), "const x: any = 1;");
|
||||
|
||||
const rulePath = await writeTempRule(": any", ["tool:edit(src/**/*.ts)"]);
|
||||
const scan: TtsrScanArgs = {
|
||||
directory: "src",
|
||||
rule: rulePath,
|
||||
};
|
||||
|
||||
await run({ action: "scan", scan });
|
||||
|
||||
expect(stdout).toContain("Found violations/matches");
|
||||
expect(stdout).toContain("rerun with --verbose");
|
||||
expect(stdout).not.toContain("src/foo.ts");
|
||||
expect(stdout).not.toContain("test-rule");
|
||||
});
|
||||
|
||||
it("outputs json format correctly when json flag is set", async () => {
|
||||
captureStreams();
|
||||
|
||||
const projectDir = path.join(import.meta.dir, `.tmp-ttsr-project-${Math.random().toString(36).slice(2)}`);
|
||||
fs.mkdirSync(path.join(projectDir, "src"), { recursive: true });
|
||||
setProjectDir(projectDir);
|
||||
|
||||
const testFile = path.join(projectDir, "src/foo.ts");
|
||||
await Bun.write(testFile, "const x: any = 1;");
|
||||
|
||||
const rulePath = await writeTempRule(": any", ["tool:edit(src/**/*.ts)"]);
|
||||
|
||||
const scan: TtsrScanArgs = {
|
||||
directory: "src",
|
||||
rule: rulePath,
|
||||
};
|
||||
|
||||
await run({ action: "scan", scan, json: true });
|
||||
|
||||
const result = JSON.parse(stdout);
|
||||
expect(result.files).toHaveLength(1);
|
||||
expect(result.files[0].filePath).toBe("src/foo.ts");
|
||||
expect(result.files[0].matches).toHaveLength(1);
|
||||
expect(result.files[0].matches[0].name).toBe("test-rule");
|
||||
expect(result.summary.totalMatches).toBe(1);
|
||||
expect(process.exitCode).toBeFalsy();
|
||||
});
|
||||
|
||||
it("reports no matches when files do not match rule conditions", async () => {
|
||||
captureStreams();
|
||||
|
||||
const projectDir = path.join(import.meta.dir, `.tmp-ttsr-project-${Math.random().toString(36).slice(2)}`);
|
||||
fs.mkdirSync(path.join(projectDir, "src"), { recursive: true });
|
||||
setProjectDir(projectDir);
|
||||
|
||||
const testFile = path.join(projectDir, "src/foo.ts");
|
||||
await Bun.write(testFile, "const x: unknown = 1;");
|
||||
|
||||
const rulePath = await writeTempRule(": any", ["tool:edit(src/**/*.ts)"]);
|
||||
|
||||
const scan: TtsrScanArgs = {
|
||||
directory: "src",
|
||||
rule: rulePath,
|
||||
};
|
||||
|
||||
await run({ action: "scan", scan });
|
||||
|
||||
expect(stdout).toContain("No rule matches found");
|
||||
expect(process.exitCode).toBeFalsy();
|
||||
});
|
||||
|
||||
it("respects project gitignore when scanning a subdirectory", async () => {
|
||||
captureStreams();
|
||||
|
||||
const projectDir = path.join(import.meta.dir, `.tmp-ttsr-project-${Math.random().toString(36).slice(2)}`);
|
||||
fs.mkdirSync(path.join(projectDir, ".git"), { recursive: true });
|
||||
fs.mkdirSync(path.join(projectDir, "packages/generated"), { recursive: true });
|
||||
fs.mkdirSync(path.join(projectDir, "packages/src"), { recursive: true });
|
||||
setProjectDir(projectDir);
|
||||
|
||||
await Bun.write(path.join(projectDir, ".gitignore"), "packages/generated/\n");
|
||||
await Bun.write(path.join(projectDir, "packages/generated/ignored.ts"), "const x: any = 1;");
|
||||
await Bun.write(path.join(projectDir, "packages/src/kept.ts"), "const x: unknown = 1;");
|
||||
|
||||
const rulePath = await writeTempRule(": any", ["tool:edit(packages/**/*.ts)"]);
|
||||
const scan: TtsrScanArgs = {
|
||||
directory: "packages",
|
||||
rule: rulePath,
|
||||
};
|
||||
|
||||
await run({ action: "scan", scan, json: true });
|
||||
|
||||
const result = JSON.parse(stdout);
|
||||
expect(result.files).toHaveLength(0);
|
||||
expect(result.summary.totalFiles).toBe(1);
|
||||
expect(result.summary.scannedFiles).toBe(1);
|
||||
expect(result.summary.totalMatches).toBe(0);
|
||||
expect(result.summary.gitignore).toBe(true);
|
||||
});
|
||||
|
||||
it("honors ttsr.enabled=false for project scans", async () => {
|
||||
captureStreams();
|
||||
|
||||
const projectDir = path.join(import.meta.dir, `.tmp-ttsr-project-${Math.random().toString(36).slice(2)}`);
|
||||
fs.mkdirSync(path.join(projectDir, "src"), { recursive: true });
|
||||
fs.mkdirSync(getProjectAgentDir(projectDir), { recursive: true });
|
||||
setProjectDir(projectDir);
|
||||
|
||||
await Bun.write(
|
||||
path.join(getProjectAgentDir(projectDir), "settings.json"),
|
||||
JSON.stringify({ ttsr: { enabled: false } }),
|
||||
);
|
||||
await Bun.write(path.join(projectDir, "src/foo.ts"), "const x: any = 1;");
|
||||
|
||||
await run({ action: "scan", scan: { directory: "src" }, json: true });
|
||||
|
||||
const result = JSON.parse(stdout);
|
||||
expect(result.error).toBe("No TTSR rules registered for this project.");
|
||||
});
|
||||
|
||||
it("includes ignored files when gitignore filtering is disabled", async () => {
|
||||
captureStreams();
|
||||
|
||||
const projectDir = path.join(import.meta.dir, `.tmp-ttsr-project-${Math.random().toString(36).slice(2)}`);
|
||||
fs.mkdirSync(path.join(projectDir, ".git"), { recursive: true });
|
||||
fs.mkdirSync(path.join(projectDir, "packages/generated"), { recursive: true });
|
||||
setProjectDir(projectDir);
|
||||
|
||||
await Bun.write(path.join(projectDir, ".gitignore"), "packages/generated/\n");
|
||||
await Bun.write(path.join(projectDir, "packages/generated/ignored.ts"), "const x: any = 1;");
|
||||
|
||||
const rulePath = await writeTempRule(": any", ["tool:edit(packages/**/*.ts)"]);
|
||||
const scan: TtsrScanArgs = {
|
||||
directory: "packages",
|
||||
rule: rulePath,
|
||||
gitignore: false,
|
||||
};
|
||||
|
||||
await run({ action: "scan", scan, json: true });
|
||||
|
||||
const result = JSON.parse(stdout);
|
||||
expect(result.files).toHaveLength(1);
|
||||
expect(result.files[0].filePath).toBe("packages/generated/ignored.ts");
|
||||
expect(result.summary.gitignore).toBe(false);
|
||||
});
|
||||
|
||||
it("includes hidden source files like GitHub workflows", async () => {
|
||||
captureStreams();
|
||||
|
||||
const projectDir = path.join(import.meta.dir, `.tmp-ttsr-project-${Math.random().toString(36).slice(2)}`);
|
||||
fs.mkdirSync(path.join(projectDir, ".github/workflows"), { recursive: true });
|
||||
setProjectDir(projectDir);
|
||||
|
||||
await Bun.write(path.join(projectDir, ".github/workflows/ci.yml"), "needs-ttsr: true\n");
|
||||
|
||||
const rulePath = await writeTempRule("needs-ttsr", ["tool:edit(.github/**/*.yml)"]);
|
||||
const scan: TtsrScanArgs = {
|
||||
rule: rulePath,
|
||||
};
|
||||
|
||||
await run({ action: "scan", scan, json: true });
|
||||
|
||||
const result = JSON.parse(stdout);
|
||||
expect(result.files).toHaveLength(1);
|
||||
expect(result.files[0].filePath).toBe(".github/workflows/ci.yml");
|
||||
expect(result.summary.scannedFiles).toBe(1);
|
||||
expect(result.summary.totalMatches).toBe(1);
|
||||
});
|
||||
|
||||
it("keeps AST-only matches in default summary when type assertion spans whitespace", async () => {
|
||||
captureStreams();
|
||||
|
||||
const projectDir = path.join(import.meta.dir, `.tmp-ttsr-project-${Math.random().toString(36).slice(2)}`);
|
||||
fs.mkdirSync(path.join(projectDir, "src"), { recursive: true });
|
||||
setProjectDir(projectDir);
|
||||
|
||||
await Bun.write(path.join(projectDir, "src/foo.ts"), "const y = (x as\n{ z }).z;");
|
||||
const rulePath = await writeTempRule(
|
||||
"never-matches-regex-zzz",
|
||||
["tool:edit(src/**/*.ts)"],
|
||||
"($X as { $$$BODY }).$PROP",
|
||||
);
|
||||
const scan: TtsrScanArgs = {
|
||||
directory: "src",
|
||||
rule: rulePath,
|
||||
};
|
||||
|
||||
await run({ action: "scan", scan });
|
||||
|
||||
expect(stdout).toContain("Found violations/matches");
|
||||
expect(stdout).toContain("1 matches across 1 files");
|
||||
expect(stdout).not.toContain("src/foo.ts");
|
||||
});
|
||||
|
||||
it("skips files larger than the scan byte limit", async () => {
|
||||
captureStreams();
|
||||
|
||||
const projectDir = path.join(import.meta.dir, `.tmp-ttsr-project-${Math.random().toString(36).slice(2)}`);
|
||||
fs.mkdirSync(path.join(projectDir, "src"), { recursive: true });
|
||||
setProjectDir(projectDir);
|
||||
|
||||
await Bun.write(path.join(projectDir, "src/large.ts"), "const x: any = 1;");
|
||||
|
||||
const rulePath = await writeTempRule(": any", ["tool:edit(src/**/*.ts)"]);
|
||||
const scan: TtsrScanArgs = {
|
||||
directory: "src",
|
||||
rule: rulePath,
|
||||
maxBytes: 4,
|
||||
};
|
||||
|
||||
await run({ action: "scan", scan, json: true });
|
||||
|
||||
const result = JSON.parse(stdout);
|
||||
expect(result.files).toHaveLength(0);
|
||||
expect(result.summary.totalFiles).toBe(1);
|
||||
expect(result.summary.scannedFiles).toBe(0);
|
||||
expect(result.summary.skipped.large).toBe(1);
|
||||
expect(result.summary.totalMatches).toBe(0);
|
||||
expect(result.summary.maxBytes).toBe(4);
|
||||
});
|
||||
|
||||
it("skips binary-looking files before text matching", async () => {
|
||||
captureStreams();
|
||||
|
||||
const projectDir = path.join(import.meta.dir, `.tmp-ttsr-project-${Math.random().toString(36).slice(2)}`);
|
||||
fs.mkdirSync(path.join(projectDir, "src"), { recursive: true });
|
||||
setProjectDir(projectDir);
|
||||
|
||||
await Bun.write(path.join(projectDir, "src/blob.ts"), new Uint8Array([0x63, 0x6f, 0x6e, 0x73, 0x74, 0x00]));
|
||||
|
||||
const rulePath = await writeTempRule("const", ["tool:edit(src/**/*.ts)"]);
|
||||
const scan: TtsrScanArgs = {
|
||||
directory: "src",
|
||||
rule: rulePath,
|
||||
};
|
||||
|
||||
await run({ action: "scan", scan, json: true });
|
||||
|
||||
const result = JSON.parse(stdout);
|
||||
expect(result.files).toHaveLength(0);
|
||||
expect(result.summary.totalFiles).toBe(1);
|
||||
expect(result.summary.scannedFiles).toBe(0);
|
||||
expect(result.summary.skipped.binary).toBe(1);
|
||||
expect(result.summary.totalMatches).toBe(0);
|
||||
});
|
||||
});
|
||||
|
||||
describe("exports", () => {
|
||||
it("TTSR_SOURCES lists all three match sources", () => {
|
||||
expect(TTSR_SOURCES).toEqual(["text", "thinking", "tool"]);
|
||||
|
||||
Reference in New Issue
Block a user