move all tests under tests/ (#3)

This commit is contained in:
Thibault Sottiaux
2025-04-16 10:21:48 -07:00
committed by GitHub
parent 1c26c272c8
commit 8794df3c08
3 changed files with 4 additions and 4 deletions

View File

@@ -0,0 +1,92 @@
import type { SafetyAssessment } from "../src/lib/approvals";
import { canAutoApprove } from "../src/lib/approvals";
import { describe, test, expect } from "vitest";
describe("canAutoApprove()", () => {
const env = {
PATH: "/usr/local/bin:/usr/bin:/bin",
HOME: "/home/user",
};
const writeablePaths: Array<string> = [];
const check = (command: ReadonlyArray<string>): SafetyAssessment =>
canAutoApprove(command, "suggest", writeablePaths, env);
test("simple safe commands", () => {
expect(check(["ls"])).toEqual({
type: "auto-approve",
reason: "List directory",
group: "Searching",
runInSandbox: false,
});
expect(check(["cat", "file.txt"])).toEqual({
type: "auto-approve",
reason: "View file contents",
group: "Reading files",
runInSandbox: false,
});
expect(check(["pwd"])).toEqual({
type: "auto-approve",
reason: "Print working directory",
group: "Navigating",
runInSandbox: false,
});
});
test("simple safe commands within a `bash -lc` call", () => {
expect(check(["bash", "-lc", "ls"])).toEqual({
type: "auto-approve",
reason: "List directory",
group: "Searching",
runInSandbox: false,
});
expect(check(["bash", "-lc", "ls $HOME"])).toEqual({
type: "auto-approve",
reason: "List directory",
group: "Searching",
runInSandbox: false,
});
expect(check(["bash", "-lc", "git show ab9811cb90"])).toEqual({
type: "auto-approve",
reason: "Git show",
group: "Using git",
runInSandbox: false,
});
});
test("bash -lc commands with unsafe redirects", () => {
expect(check(["bash", "-lc", "echo hello > file.txt"])).toEqual({
type: "ask-user",
});
// In theory, we could make our checker more sophisticated to auto-approve
// This previously required approval, but now that we consider safe
// operators like "&&" the entire expression can be autoapproved.
expect(check(["bash", "-lc", "ls && pwd"])).toEqual({
type: "auto-approve",
reason: "List directory",
group: "Searching",
runInSandbox: false,
});
});
test("true command is considered safe", () => {
expect(check(["true"])).toEqual({
type: "auto-approve",
reason: "Noop (true)",
group: "Utility",
runInSandbox: false,
});
});
test("commands that should require approval", () => {
// Should this be on the auto-approved list?
expect(check(["printenv"])).toEqual({ type: "ask-user" });
expect(check(["git", "commit"])).toEqual({ type: "ask-user" });
expect(check(["pytest"])).toEqual({ type: "ask-user" });
expect(check(["cargo", "build"])).toEqual({ type: "ask-user" });
});
});

View File

@@ -0,0 +1,21 @@
import { formatCommandForDisplay } from "../src/lib/format-command";
import { describe, test, expect } from "vitest";
describe("formatCommandForDisplay()", () => {
test("ensure empty string arg appears in output", () => {
expect(formatCommandForDisplay(["echo", ""])).toEqual("echo ''");
});
test("ensure special characters are properly escaped", () => {
expect(formatCommandForDisplay(["echo", "$HOME"])).toEqual("echo \\$HOME");
});
test("ensure quotes are properly escaped", () => {
expect(formatCommandForDisplay(["echo", "I can't believe this."])).toEqual(
'echo "I can\'t believe this."',
);
expect(
formatCommandForDisplay(["echo", 'So I said, "No ma\'am!"']),
).toEqual('echo "So I said, \\"No ma\'am\\!\\""');
});
});

View File

@@ -0,0 +1,45 @@
import { parseApplyPatch } from "../src/lib/parse-apply-patch";
import { expect, test, describe } from "vitest";
// Helper function to unwrap a nonnull result in tests that expect success.
function mustParse(patch: string) {
const parsed = parseApplyPatch(patch);
if (parsed == null) {
throw new Error(
"Expected patch to be valid, but parseApplyPatch returned null",
);
}
return parsed;
}
describe("parseApplyPatch", () => {
test("parses create, update and delete operations in a single patch", () => {
const patch = `*** Begin Patch\n*** Add File: created.txt\n+hello\n+world\n*** Update File: updated.txt\n@@\n-old\n+new\n*** Delete File: removed.txt\n*** End Patch`;
const ops = mustParse(patch);
expect(ops).toEqual([
{
type: "create",
path: "created.txt",
content: "hello\nworld",
},
{
type: "update",
path: "updated.txt",
update: "@@\n-old\n+new",
added: 1,
deleted: 1,
},
{
type: "delete",
path: "removed.txt",
},
]);
});
test("returns null for an invalid patch (missing prefix)", () => {
const invalid = `*** Add File: foo.txt\n+bar\n*** End Patch`;
expect(parseApplyPatch(invalid)).toBeNull();
});
});