ignore: rework bootstrap so server lazy starts it

This commit is contained in:
Dax Raad
2025-09-19 05:11:29 -04:00
parent 0e19ca21ed
commit ae6154e1c3
13 changed files with 690 additions and 637 deletions

View File

@@ -27,19 +27,19 @@ async function bootstrap() {
test("tracks deleted files correctly", async () => {
await using tmp = await bootstrap()
await Instance.provide(tmp.dir, async () => {
await Instance.provide({ directory: tmp.dir, fn: async () => {
const before = await Snapshot.track()
expect(before).toBeTruthy()
await $`rm ${tmp.dir}/a.txt`.quiet()
expect((await Snapshot.patch(before!)).files).toContain(`${tmp.dir}/a.txt`)
})
}})
})
test("revert should remove new files", async () => {
await using tmp = await bootstrap()
await Instance.provide(tmp.dir, async () => {
await Instance.provide({ directory: tmp.dir, fn: async () => {
const before = await Snapshot.track()
expect(before).toBeTruthy()
@@ -48,12 +48,12 @@ test("revert should remove new files", async () => {
await Snapshot.revert([await Snapshot.patch(before!)])
expect(await Bun.file(`${tmp.dir}/new.txt`).exists()).toBe(false)
})
}})
})
test("revert in subdirectory", async () => {
await using tmp = await bootstrap()
await Instance.provide(tmp.dir, async () => {
await Instance.provide({ directory: tmp.dir, fn: async () => {
const before = await Snapshot.track()
expect(before).toBeTruthy()
@@ -65,12 +65,12 @@ test("revert in subdirectory", async () => {
expect(await Bun.file(`${tmp.dir}/sub/file.txt`).exists()).toBe(false)
// Note: revert currently only removes files, not directories
// The empty subdirectory will remain
})
}})
})
test("multiple file operations", async () => {
await using tmp = await bootstrap()
await Instance.provide(tmp.dir, async () => {
await Instance.provide({ directory: tmp.dir, fn: async () => {
const before = await Snapshot.track()
expect(before).toBeTruthy()
@@ -87,24 +87,24 @@ test("multiple file operations", async () => {
// Note: revert currently only removes files, not directories
// The empty directory will remain
expect(await Bun.file(`${tmp.dir}/b.txt`).text()).toBe(tmp.bContent)
})
}})
})
test("empty directory handling", async () => {
await using tmp = await bootstrap()
await Instance.provide(tmp.dir, async () => {
await Instance.provide({ directory: tmp.dir, fn: async () => {
const before = await Snapshot.track()
expect(before).toBeTruthy()
await $`mkdir ${tmp.dir}/empty`.quiet()
expect((await Snapshot.patch(before!)).files.length).toBe(0)
})
}})
})
test("binary file handling", async () => {
await using tmp = await bootstrap()
await Instance.provide(tmp.dir, async () => {
await Instance.provide({ directory: tmp.dir, fn: async () => {
const before = await Snapshot.track()
expect(before).toBeTruthy()
@@ -115,36 +115,36 @@ test("binary file handling", async () => {
await Snapshot.revert([patch])
expect(await Bun.file(`${tmp.dir}/image.png`).exists()).toBe(false)
})
}})
})
test("symlink handling", async () => {
await using tmp = await bootstrap()
await Instance.provide(tmp.dir, async () => {
await Instance.provide({ directory: tmp.dir, fn: async () => {
const before = await Snapshot.track()
expect(before).toBeTruthy()
await $`ln -s ${tmp.dir}/a.txt ${tmp.dir}/link.txt`.quiet()
expect((await Snapshot.patch(before!)).files).toContain(`${tmp.dir}/link.txt`)
})
}})
})
test("large file handling", async () => {
await using tmp = await bootstrap()
await Instance.provide(tmp.dir, async () => {
await Instance.provide({ directory: tmp.dir, fn: async () => {
const before = await Snapshot.track()
expect(before).toBeTruthy()
await Bun.write(`${tmp.dir}/large.txt`, "x".repeat(1024 * 1024))
expect((await Snapshot.patch(before!)).files).toContain(`${tmp.dir}/large.txt`)
})
}})
})
test("nested directory revert", async () => {
await using tmp = await bootstrap()
await Instance.provide(tmp.dir, async () => {
await Instance.provide({ directory: tmp.dir, fn: async () => {
const before = await Snapshot.track()
expect(before).toBeTruthy()
@@ -154,12 +154,12 @@ test("nested directory revert", async () => {
await Snapshot.revert([await Snapshot.patch(before!)])
expect(await Bun.file(`${tmp.dir}/level1/level2/level3/deep.txt`).exists()).toBe(false)
})
}})
})
test("special characters in filenames", async () => {
await using tmp = await bootstrap()
await Instance.provide(tmp.dir, async () => {
await Instance.provide({ directory: tmp.dir, fn: async () => {
const before = await Snapshot.track()
expect(before).toBeTruthy()
@@ -171,23 +171,23 @@ test("special characters in filenames", async () => {
expect(files).toContain(`${tmp.dir}/file with spaces.txt`)
expect(files).toContain(`${tmp.dir}/file-with-dashes.txt`)
expect(files).toContain(`${tmp.dir}/file_with_underscores.txt`)
})
}})
})
test("revert with empty patches", async () => {
await using tmp = await bootstrap()
await Instance.provide(tmp.dir, async () => {
await Instance.provide({ directory: tmp.dir, fn: async () => {
// Should not crash with empty patches
expect(Snapshot.revert([])).resolves.toBeUndefined()
// Should not crash with patches that have empty file lists
expect(Snapshot.revert([{ hash: "dummy", files: [] }])).resolves.toBeUndefined()
})
}})
})
test("patch with invalid hash", async () => {
await using tmp = await bootstrap()
await Instance.provide(tmp.dir, async () => {
await Instance.provide({ directory: tmp.dir, fn: async () => {
const before = await Snapshot.track()
expect(before).toBeTruthy()
@@ -198,12 +198,12 @@ test("patch with invalid hash", async () => {
const patch = await Snapshot.patch("invalid-hash-12345")
expect(patch.files).toEqual([])
expect(patch.hash).toBe("invalid-hash-12345")
})
}})
})
test("revert non-existent file", async () => {
await using tmp = await bootstrap()
await Instance.provide(tmp.dir, async () => {
await Instance.provide({ directory: tmp.dir, fn: async () => {
const before = await Snapshot.track()
expect(before).toBeTruthy()
@@ -217,12 +217,12 @@ test("revert non-existent file", async () => {
},
]),
).resolves.toBeUndefined()
})
}})
})
test("unicode filenames", async () => {
await using tmp = await bootstrap()
await Instance.provide(tmp.dir, async () => {
await Instance.provide({ directory: tmp.dir, fn: async () => {
const before = await Snapshot.track()
expect(before).toBeTruthy()
@@ -244,12 +244,12 @@ test("unicode filenames", async () => {
// Skip revert test due to git filename escaping issues
// The functionality works but git uses escaped filenames internally
})
}})
})
test("very long filenames", async () => {
await using tmp = await bootstrap()
await Instance.provide(tmp.dir, async () => {
await Instance.provide({ directory: tmp.dir, fn: async () => {
const before = await Snapshot.track()
expect(before).toBeTruthy()
@@ -263,12 +263,12 @@ test("very long filenames", async () => {
await Snapshot.revert([patch])
expect(await Bun.file(longFile).exists()).toBe(false)
})
}})
})
test("hidden files", async () => {
await using tmp = await bootstrap()
await Instance.provide(tmp.dir, async () => {
await Instance.provide({ directory: tmp.dir, fn: async () => {
const before = await Snapshot.track()
expect(before).toBeTruthy()
@@ -280,12 +280,12 @@ test("hidden files", async () => {
expect(patch.files).toContain(`${tmp.dir}/.hidden`)
expect(patch.files).toContain(`${tmp.dir}/.gitignore`)
expect(patch.files).toContain(`${tmp.dir}/.config`)
})
}})
})
test("nested symlinks", async () => {
await using tmp = await bootstrap()
await Instance.provide(tmp.dir, async () => {
await Instance.provide({ directory: tmp.dir, fn: async () => {
const before = await Snapshot.track()
expect(before).toBeTruthy()
@@ -297,12 +297,12 @@ test("nested symlinks", async () => {
const patch = await Snapshot.patch(before!)
expect(patch.files).toContain(`${tmp.dir}/sub/dir/link.txt`)
expect(patch.files).toContain(`${tmp.dir}/sub-link`)
})
}})
})
test("file permissions and ownership changes", async () => {
await using tmp = await bootstrap()
await Instance.provide(tmp.dir, async () => {
await Instance.provide({ directory: tmp.dir, fn: async () => {
const before = await Snapshot.track()
expect(before).toBeTruthy()
@@ -315,12 +315,12 @@ test("file permissions and ownership changes", async () => {
// Note: git doesn't track permission changes on existing files by default
// Only tracks executable bit when files are first added
expect(patch.files.length).toBe(0)
})
}})
})
test("circular symlinks", async () => {
await using tmp = await bootstrap()
await Instance.provide(tmp.dir, async () => {
await Instance.provide({ directory: tmp.dir, fn: async () => {
const before = await Snapshot.track()
expect(before).toBeTruthy()
@@ -329,12 +329,12 @@ test("circular symlinks", async () => {
const patch = await Snapshot.patch(before!)
expect(patch.files.length).toBeGreaterThanOrEqual(0) // Should not crash
})
}})
})
test("gitignore changes", async () => {
await using tmp = await bootstrap()
await Instance.provide(tmp.dir, async () => {
await Instance.provide({ directory: tmp.dir, fn: async () => {
const before = await Snapshot.track()
expect(before).toBeTruthy()
@@ -350,12 +350,12 @@ test("gitignore changes", async () => {
expect(patch.files).toContain(`${tmp.dir}/normal.txt`)
// Should not track ignored files (git won't see them)
expect(patch.files).not.toContain(`${tmp.dir}/test.ignored`)
})
}})
})
test("concurrent file operations during patch", async () => {
await using tmp = await bootstrap()
await Instance.provide(tmp.dir, async () => {
await Instance.provide({ directory: tmp.dir, fn: async () => {
const before = await Snapshot.track()
expect(before).toBeTruthy()
@@ -376,7 +376,7 @@ test("concurrent file operations during patch", async () => {
// Should capture some or all of the concurrent files
expect(patch.files.length).toBeGreaterThanOrEqual(0)
})
}})
})
test("snapshot state isolation between projects", async () => {
@@ -384,14 +384,14 @@ test("snapshot state isolation between projects", async () => {
await using tmp1 = await bootstrap()
await using tmp2 = await bootstrap()
await Instance.provide(tmp1.dir, async () => {
await Instance.provide({ directory: tmp1.dir, fn: async () => {
const before1 = await Snapshot.track()
await Bun.write(`${tmp1.dir}/project1.txt`, "project1 content")
const patch1 = await Snapshot.patch(before1!)
expect(patch1.files).toContain(`${tmp1.dir}/project1.txt`)
})
}})
await Instance.provide(tmp2.dir, async () => {
await Instance.provide({ directory: tmp2.dir, fn: async () => {
const before2 = await Snapshot.track()
await Bun.write(`${tmp2.dir}/project2.txt`, "project2 content")
const patch2 = await Snapshot.patch(before2!)
@@ -399,12 +399,12 @@ test("snapshot state isolation between projects", async () => {
// Ensure project1 files don't appear in project2
expect(patch2.files).not.toContain(`${tmp1?.dir}/project1.txt`)
})
}})
})
test("track with no changes returns same hash", async () => {
await using tmp = await bootstrap()
await Instance.provide(tmp.dir, async () => {
await Instance.provide({ directory: tmp.dir, fn: async () => {
const hash1 = await Snapshot.track()
expect(hash1).toBeTruthy()
@@ -415,12 +415,12 @@ test("track with no changes returns same hash", async () => {
// Track again
const hash3 = await Snapshot.track()
expect(hash3).toBe(hash1!)
})
}})
})
test("diff function with various changes", async () => {
await using tmp = await bootstrap()
await Instance.provide(tmp.dir, async () => {
await Instance.provide({ directory: tmp.dir, fn: async () => {
const before = await Snapshot.track()
expect(before).toBeTruthy()
@@ -433,12 +433,12 @@ test("diff function with various changes", async () => {
expect(diff).toContain("deleted")
expect(diff).toContain("modified")
// Note: git diff only shows changes to tracked files, not untracked files like new.txt
})
}})
})
test("restore function", async () => {
await using tmp = await bootstrap()
await Instance.provide(tmp.dir, async () => {
await Instance.provide({ directory: tmp.dir, fn: async () => {
const before = await Snapshot.track()
expect(before).toBeTruthy()
@@ -454,5 +454,5 @@ test("restore function", async () => {
expect(await Bun.file(`${tmp.dir}/a.txt`).text()).toBe(tmp.aContent)
expect(await Bun.file(`${tmp.dir}/new.txt`).exists()).toBe(true) // New files should remain
expect(await Bun.file(`${tmp.dir}/b.txt`).text()).toBe(tmp.bContent)
})
}})
})

View File

@@ -19,30 +19,36 @@ Log.init({ print: false })
describe("tool.bash", () => {
test("basic", async () => {
await Instance.provide(projectRoot, async () => {
const result = await bash.execute(
{
command: "echo 'test'",
description: "Echo test message",
},
ctx,
)
expect(result.metadata.exit).toBe(0)
expect(result.metadata.output).toContain("test")
await Instance.provide({
directory: projectRoot,
fn: async () => {
const result = await bash.execute(
{
command: "echo 'test'",
description: "Echo test message",
},
ctx,
)
expect(result.metadata.exit).toBe(0)
expect(result.metadata.output).toContain("test")
},
})
})
test("cd ../ should fail outside of project root", async () => {
await Instance.provide(projectRoot, async () => {
expect(
bash.execute(
{
command: "cd ../",
description: "Try to cd to parent directory",
},
ctx,
),
).rejects.toThrow("This command references paths outside of")
await Instance.provide({
directory: projectRoot,
fn: async () => {
expect(
bash.execute(
{
command: "cd ../",
description: "Try to cd to parent directory",
},
ctx,
),
).rejects.toThrow("This command references paths outside of")
},
})
})
})

View File

@@ -20,38 +20,47 @@ const fixturePath = path.join(__dirname, "../fixtures/example")
describe("tool.glob", () => {
test("truncate", async () => {
await Instance.provide(projectRoot, async () => {
let result = await glob.execute(
{
pattern: "**/*",
path: "../../node_modules",
},
ctx,
)
expect(result.metadata.truncated).toBe(true)
await Instance.provide({
directory: projectRoot,
fn: async () => {
let result = await glob.execute(
{
pattern: "**/*",
path: "../../node_modules",
},
ctx,
)
expect(result.metadata.truncated).toBe(true)
},
})
})
test("basic", async () => {
await Instance.provide(projectRoot, async () => {
let result = await glob.execute(
{
pattern: "*.json",
path: undefined,
},
ctx,
)
expect(result.metadata).toMatchObject({
truncated: false,
count: 2,
})
await Instance.provide({
directory: projectRoot,
fn: async () => {
let result = await glob.execute(
{
pattern: "*.json",
path: undefined,
},
ctx,
)
expect(result.metadata).toMatchObject({
truncated: false,
count: 2,
})
},
})
})
})
describe("tool.ls", () => {
test("basic", async () => {
const result = await Instance.provide(projectRoot, async () => {
return await list.execute({ path: fixturePath, ignore: [".git"] }, ctx)
const result = await Instance.provide({
directory: projectRoot,
fn: async () => {
return await list.execute({ path: fixturePath, ignore: [".git"] }, ctx)
},
})
// Normalize absolute path to relative for consistent snapshots