test cleanup

This commit is contained in:
Dax Raad
2025-10-24 09:29:38 -04:00
parent 7003efd2da
commit 4cab66da6c
2 changed files with 54 additions and 59 deletions

View File

@@ -1,7 +1,6 @@
import { describe, expect, test } from "bun:test" import { describe, expect, test } from "bun:test"
import path from "path" import path from "path"
import { BashTool } from "../../src/tool/bash" import { BashTool } from "../../src/tool/bash"
import { Log } from "../../src/util/log"
import { Instance } from "../../src/project/instance" import { Instance } from "../../src/project/instance"
const ctx = { const ctx = {
@@ -15,7 +14,6 @@ const ctx = {
const bash = await BashTool.init() const bash = await BashTool.init()
const projectRoot = path.join(__dirname, "../..") const projectRoot = path.join(__dirname, "../..")
Log.init({ print: false })
describe("tool.bash", () => { describe("tool.bash", () => {
test("basic", async () => { test("basic", async () => {

View File

@@ -1,7 +1,6 @@
import { describe, expect, test } from "bun:test" import { describe, expect, test } from "bun:test"
import path from "path" import path from "path"
import { PatchTool } from "../../src/tool/patch" import { PatchTool } from "../../src/tool/patch"
import { Log } from "../../src/util/log"
import { Instance } from "../../src/project/instance" import { Instance } from "../../src/project/instance"
import { tmpdir } from "../fixture/fixture" import { tmpdir } from "../fixture/fixture"
import * as fs from "fs/promises" import * as fs from "fs/promises"
@@ -10,51 +9,46 @@ const ctx = {
sessionID: "test", sessionID: "test",
messageID: "", messageID: "",
toolCallID: "", toolCallID: "",
agent: "build", agent: "build",
abort: AbortSignal.any([]), abort: AbortSignal.any([]),
metadata: () => {}, metadata: () => {},
} }
const patchTool = await PatchTool.init() const patchTool = await PatchTool.init()
Log.init({ print: false })
describe("tool.patch", () => { describe("tool.patch", () => {
test("should validate required parameters", async () => { test("should validate required parameters", async () => {
await Instance.provide({ await Instance.provide({
directory: "/tmp", directory: "/tmp",
fn: async () => { fn: async () => {
await expect( await expect(patchTool.execute({ patchText: "" }, ctx)).rejects.toThrow("patchText is required")
patchTool.execute({ patchText: "" }, ctx)
).rejects.toThrow("patchText is required")
}, },
}) })
}) })
test("should validate patch format", async () => { test("should validate patch format", async () => {
await Instance.provide({ await Instance.provide({
directory: "/tmp", directory: "/tmp",
fn: async () => { fn: async () => {
await expect( await expect(patchTool.execute({ patchText: "invalid patch" }, ctx)).rejects.toThrow("Failed to parse patch")
patchTool.execute({ patchText: "invalid patch" }, ctx)
).rejects.toThrow("Failed to parse patch")
}, },
}) })
}) })
test("should handle empty patch", async () => { test("should handle empty patch", async () => {
await Instance.provide({ await Instance.provide({
directory: "/tmp", directory: "/tmp",
fn: async () => { fn: async () => {
const emptyPatch = `*** Begin Patch const emptyPatch = `*** Begin Patch
*** End Patch` *** End Patch`
await expect( await expect(patchTool.execute({ patchText: emptyPatch }, ctx)).rejects.toThrow(
patchTool.execute({ patchText: emptyPatch }, ctx) "No file changes found in patch",
).rejects.toThrow("No file changes found in patch") )
}, },
}) })
}) })
test("should reject files outside working directory", async () => { test("should reject files outside working directory", async () => {
await Instance.provide({ await Instance.provide({
directory: "/tmp", directory: "/tmp",
@@ -63,17 +57,17 @@ describe("tool.patch", () => {
*** Add File: /etc/passwd *** Add File: /etc/passwd
+malicious content +malicious content
*** End Patch` *** End Patch`
await expect( await expect(patchTool.execute({ patchText: maliciousPatch }, ctx)).rejects.toThrow(
patchTool.execute({ patchText: maliciousPatch }, ctx) "is not in the current working directory",
).rejects.toThrow("is not in the current working directory") )
}, },
}) })
}) })
test("should handle simple add file operation", async () => { test("should handle simple add file operation", async () => {
await using fixture = await tmpdir() await using fixture = await tmpdir()
await Instance.provide({ await Instance.provide({
directory: fixture.path, directory: fixture.path,
fn: async () => { fn: async () => {
@@ -82,13 +76,13 @@ describe("tool.patch", () => {
+Hello World +Hello World
+This is a test file +This is a test file
*** End Patch` *** End Patch`
const result = await patchTool.execute({ patchText }, ctx) const result = await patchTool.execute({ patchText }, ctx)
expect(result.title).toContain("files changed") expect(result.title).toContain("files changed")
expect(result.metadata.diff).toBeDefined() expect(result.metadata.diff).toBeDefined()
expect(result.output).toContain("Patch applied successfully") expect(result.output).toContain("Patch applied successfully")
// Verify file was created // Verify file was created
const filePath = path.join(fixture.path, "test-file.txt") const filePath = path.join(fixture.path, "test-file.txt")
const content = await fs.readFile(filePath, "utf-8") const content = await fs.readFile(filePath, "utf-8")
@@ -96,10 +90,10 @@ describe("tool.patch", () => {
}, },
}) })
}) })
test("should handle file with context update", async () => { test("should handle file with context update", async () => {
await using fixture = await tmpdir() await using fixture = await tmpdir()
await Instance.provide({ await Instance.provide({
directory: fixture.path, directory: fixture.path,
fn: async () => { fn: async () => {
@@ -109,24 +103,24 @@ describe("tool.patch", () => {
+const DEBUG = false +const DEBUG = false
+const VERSION = "1.0" +const VERSION = "1.0"
*** End Patch` *** End Patch`
const result = await patchTool.execute({ patchText }, ctx) const result = await patchTool.execute({ patchText }, ctx)
expect(result.title).toContain("files changed") expect(result.title).toContain("files changed")
expect(result.metadata.diff).toBeDefined() expect(result.metadata.diff).toBeDefined()
expect(result.output).toContain("Patch applied successfully") expect(result.output).toContain("Patch applied successfully")
// Verify file was created with correct content // Verify file was created with correct content
const filePath = path.join(fixture.path, "config.js") const filePath = path.join(fixture.path, "config.js")
const content = await fs.readFile(filePath, "utf-8") const content = await fs.readFile(filePath, "utf-8")
expect(content).toBe("const API_KEY = \"test-key\"\nconst DEBUG = false\nconst VERSION = \"1.0\"") expect(content).toBe('const API_KEY = "test-key"\nconst DEBUG = false\nconst VERSION = "1.0"')
}, },
}) })
}) })
test("should handle multiple file operations", async () => { test("should handle multiple file operations", async () => {
await using fixture = await tmpdir() await using fixture = await tmpdir()
await Instance.provide({ await Instance.provide({
directory: fixture.path, directory: fixture.path,
fn: async () => { fn: async () => {
@@ -138,13 +132,13 @@ describe("tool.patch", () => {
*** Add File: file3.txt *** Add File: file3.txt
+Content of file 3 +Content of file 3
*** End Patch` *** End Patch`
const result = await patchTool.execute({ patchText }, ctx) const result = await patchTool.execute({ patchText }, ctx)
expect(result.title).toContain("3 files changed") expect(result.title).toContain("3 files changed")
expect(result.metadata.diff).toBeDefined() expect(result.metadata.diff).toBeDefined()
expect(result.output).toContain("Patch applied successfully") expect(result.output).toContain("Patch applied successfully")
// Verify all files were created // Verify all files were created
for (let i = 1; i <= 3; i++) { for (let i = 1; i <= 3; i++) {
const filePath = path.join(fixture.path, `file${i}.txt`) const filePath = path.join(fixture.path, `file${i}.txt`)
@@ -154,10 +148,10 @@ describe("tool.patch", () => {
}, },
}) })
}) })
test("should create parent directories when adding nested files", async () => { test("should create parent directories when adding nested files", async () => {
await using fixture = await tmpdir() await using fixture = await tmpdir()
await Instance.provide({ await Instance.provide({
directory: fixture.path, directory: fixture.path,
fn: async () => { fn: async () => {
@@ -165,26 +159,29 @@ describe("tool.patch", () => {
*** Add File: deep/nested/file.txt *** Add File: deep/nested/file.txt
+Deep nested content +Deep nested content
*** End Patch` *** End Patch`
const result = await patchTool.execute({ patchText }, ctx) const result = await patchTool.execute({ patchText }, ctx)
expect(result.title).toContain("files changed") expect(result.title).toContain("files changed")
expect(result.output).toContain("Patch applied successfully") expect(result.output).toContain("Patch applied successfully")
// Verify nested file was created // Verify nested file was created
const nestedPath = path.join(fixture.path, "deep", "nested", "file.txt") const nestedPath = path.join(fixture.path, "deep", "nested", "file.txt")
const exists = await fs.access(nestedPath).then(() => true).catch(() => false) const exists = await fs
.access(nestedPath)
.then(() => true)
.catch(() => false)
expect(exists).toBe(true) expect(exists).toBe(true)
const content = await fs.readFile(nestedPath, "utf-8") const content = await fs.readFile(nestedPath, "utf-8")
expect(content).toBe("Deep nested content") expect(content).toBe("Deep nested content")
}, },
}) })
}) })
test("should generate proper unified diff in metadata", async () => { test("should generate proper unified diff in metadata", async () => {
await using fixture = await tmpdir() await using fixture = await tmpdir()
await Instance.provide({ await Instance.provide({
directory: fixture.path, directory: fixture.path,
fn: async () => { fn: async () => {
@@ -195,9 +192,9 @@ describe("tool.patch", () => {
+line 2 +line 2
+line 3 +line 3
*** End Patch` *** End Patch`
await patchTool.execute({ patchText: patchText1 }, ctx) await patchTool.execute({ patchText: patchText1 }, ctx)
// Now create an update patch // Now create an update patch
const patchText2 = `*** Begin Patch const patchText2 = `*** Begin Patch
*** Update File: test.txt *** Update File: test.txt
@@ -207,9 +204,9 @@ describe("tool.patch", () => {
+line 2 updated +line 2 updated
line 3 line 3
*** End Patch` *** End Patch`
const result = await patchTool.execute({ patchText: patchText2 }, ctx) const result = await patchTool.execute({ patchText: patchText2 }, ctx)
expect(result.metadata.diff).toBeDefined() expect(result.metadata.diff).toBeDefined()
expect(result.metadata.diff).toContain("@@") expect(result.metadata.diff).toContain("@@")
expect(result.metadata.diff).toContain("-line 2") expect(result.metadata.diff).toContain("-line 2")
@@ -217,10 +214,10 @@ describe("tool.patch", () => {
}, },
}) })
}) })
test("should handle complex patch with multiple operations", async () => { test("should handle complex patch with multiple operations", async () => {
await using fixture = await tmpdir() await using fixture = await tmpdir()
await Instance.provide({ await Instance.provide({
directory: fixture.path, directory: fixture.path,
fn: async () => { fn: async () => {
@@ -238,26 +235,26 @@ describe("tool.patch", () => {
+ "debug": true + "debug": true
+} +}
*** End Patch` *** End Patch`
const result = await patchTool.execute({ patchText }, ctx) const result = await patchTool.execute({ patchText }, ctx)
expect(result.title).toContain("3 files changed") expect(result.title).toContain("3 files changed")
expect(result.metadata.diff).toBeDefined() expect(result.metadata.diff).toBeDefined()
expect(result.output).toContain("Patch applied successfully") expect(result.output).toContain("Patch applied successfully")
// Verify all files were created // Verify all files were created
const newPath = path.join(fixture.path, "new.txt") const newPath = path.join(fixture.path, "new.txt")
const newContent = await fs.readFile(newPath, "utf-8") const newContent = await fs.readFile(newPath, "utf-8")
expect(newContent).toBe("This is a new file\nwith multiple lines") expect(newContent).toBe("This is a new file\nwith multiple lines")
const existingPath = path.join(fixture.path, "existing.txt") const existingPath = path.join(fixture.path, "existing.txt")
const existingContent = await fs.readFile(existingPath, "utf-8") const existingContent = await fs.readFile(existingPath, "utf-8")
expect(existingContent).toBe("old content\nnew line\nmore content") expect(existingContent).toBe("old content\nnew line\nmore content")
const configPath = path.join(fixture.path, "config.json") const configPath = path.join(fixture.path, "config.json")
const configContent = await fs.readFile(configPath, "utf-8") const configContent = await fs.readFile(configPath, "utf-8")
expect(configContent).toBe('{\n "version": "1.0",\n "debug": true\n}') expect(configContent).toBe('{\n "version": "1.0",\n "debug": true\n}')
}, },
}) })
}) })
}) })