mirror of
https://github.com/aljazceru/opencode.git
synced 2025-12-25 03:34:22 +01:00
core: add session diff API to show file changes between snapshots
This commit is contained in:
@@ -33,7 +33,9 @@ test("tracks deleted files correctly", async () => {
|
||||
|
||||
await $`rm ${tmp.path}/a.txt`.quiet()
|
||||
|
||||
expect((await Snapshot.patch(before!)).files).toContain(`${tmp.path}/a.txt`)
|
||||
expect((await Snapshot.patch(before!)).files).toContain(
|
||||
`${tmp.path}/a.txt`,
|
||||
)
|
||||
},
|
||||
})
|
||||
})
|
||||
@@ -91,11 +93,15 @@ test("multiple file operations", async () => {
|
||||
|
||||
await Snapshot.revert([await Snapshot.patch(before!)])
|
||||
|
||||
expect(await Bun.file(`${tmp.path}/a.txt`).text()).toBe(tmp.extra.aContent)
|
||||
expect(await Bun.file(`${tmp.path}/a.txt`).text()).toBe(
|
||||
tmp.extra.aContent,
|
||||
)
|
||||
expect(await Bun.file(`${tmp.path}/c.txt`).exists()).toBe(false)
|
||||
// Note: revert currently only removes files, not directories
|
||||
// The empty directory will remain
|
||||
expect(await Bun.file(`${tmp.path}/b.txt`).text()).toBe(tmp.extra.bContent)
|
||||
expect(await Bun.file(`${tmp.path}/b.txt`).text()).toBe(
|
||||
tmp.extra.bContent,
|
||||
)
|
||||
},
|
||||
})
|
||||
})
|
||||
@@ -123,7 +129,10 @@ test("binary file handling", async () => {
|
||||
const before = await Snapshot.track()
|
||||
expect(before).toBeTruthy()
|
||||
|
||||
await Bun.write(`${tmp.path}/image.png`, new Uint8Array([0x89, 0x50, 0x4e, 0x47]))
|
||||
await Bun.write(
|
||||
`${tmp.path}/image.png`,
|
||||
new Uint8Array([0x89, 0x50, 0x4e, 0x47]),
|
||||
)
|
||||
|
||||
const patch = await Snapshot.patch(before!)
|
||||
expect(patch.files).toContain(`${tmp.path}/image.png`)
|
||||
@@ -144,7 +153,9 @@ test("symlink handling", async () => {
|
||||
|
||||
await $`ln -s ${tmp.path}/a.txt ${tmp.path}/link.txt`.quiet()
|
||||
|
||||
expect((await Snapshot.patch(before!)).files).toContain(`${tmp.path}/link.txt`)
|
||||
expect((await Snapshot.patch(before!)).files).toContain(
|
||||
`${tmp.path}/link.txt`,
|
||||
)
|
||||
},
|
||||
})
|
||||
})
|
||||
@@ -159,7 +170,9 @@ test("large file handling", async () => {
|
||||
|
||||
await Bun.write(`${tmp.path}/large.txt`, "x".repeat(1024 * 1024))
|
||||
|
||||
expect((await Snapshot.patch(before!)).files).toContain(`${tmp.path}/large.txt`)
|
||||
expect((await Snapshot.patch(before!)).files).toContain(
|
||||
`${tmp.path}/large.txt`,
|
||||
)
|
||||
},
|
||||
})
|
||||
})
|
||||
@@ -177,7 +190,9 @@ test("nested directory revert", async () => {
|
||||
|
||||
await Snapshot.revert([await Snapshot.patch(before!)])
|
||||
|
||||
expect(await Bun.file(`${tmp.path}/level1/level2/level3/deep.txt`).exists()).toBe(false)
|
||||
expect(
|
||||
await Bun.file(`${tmp.path}/level1/level2/level3/deep.txt`).exists(),
|
||||
).toBe(false)
|
||||
},
|
||||
})
|
||||
})
|
||||
@@ -211,7 +226,9 @@ test("revert with empty patches", async () => {
|
||||
expect(Snapshot.revert([])).resolves.toBeUndefined()
|
||||
|
||||
// Should not crash with patches that have empty file lists
|
||||
expect(Snapshot.revert([{ hash: "dummy", files: [] }])).resolves.toBeUndefined()
|
||||
expect(
|
||||
Snapshot.revert([{ hash: "dummy", files: [] }]),
|
||||
).resolves.toBeUndefined()
|
||||
},
|
||||
})
|
||||
})
|
||||
@@ -526,9 +543,13 @@ test("restore function", async () => {
|
||||
await Snapshot.restore(before!)
|
||||
|
||||
expect(await Bun.file(`${tmp.path}/a.txt`).exists()).toBe(true)
|
||||
expect(await Bun.file(`${tmp.path}/a.txt`).text()).toBe(tmp.extra.aContent)
|
||||
expect(await Bun.file(`${tmp.path}/a.txt`).text()).toBe(
|
||||
tmp.extra.aContent,
|
||||
)
|
||||
expect(await Bun.file(`${tmp.path}/new.txt`).exists()).toBe(true) // New files should remain
|
||||
expect(await Bun.file(`${tmp.path}/b.txt`).text()).toBe(tmp.extra.bContent)
|
||||
expect(await Bun.file(`${tmp.path}/b.txt`).text()).toBe(
|
||||
tmp.extra.bContent,
|
||||
)
|
||||
},
|
||||
})
|
||||
})
|
||||
@@ -580,7 +601,66 @@ test("revert preserves file that existed in snapshot when deleted then recreated
|
||||
|
||||
expect(await Bun.file(`${tmp.path}/newfile.txt`).exists()).toBe(false)
|
||||
expect(await Bun.file(`${tmp.path}/existing.txt`).exists()).toBe(true)
|
||||
expect(await Bun.file(`${tmp.path}/existing.txt`).text()).toBe("original content")
|
||||
expect(await Bun.file(`${tmp.path}/existing.txt`).text()).toBe(
|
||||
"original content",
|
||||
)
|
||||
},
|
||||
})
|
||||
})
|
||||
|
||||
test("diffFull function", async () => {
|
||||
await using tmp = await bootstrap()
|
||||
await Instance.provide({
|
||||
directory: tmp.path,
|
||||
fn: async () => {
|
||||
const before = await Snapshot.track()
|
||||
expect(before).toBeTruthy()
|
||||
|
||||
await Bun.write(`${tmp.path}/new.txt`, "new content")
|
||||
await Bun.write(`${tmp.path}/b.txt`, "modified content")
|
||||
|
||||
const after = await Snapshot.track()
|
||||
expect(after).toBeTruthy()
|
||||
|
||||
const diffs = await Snapshot.diffFull(before!, after!)
|
||||
expect(diffs.length).toBe(2)
|
||||
|
||||
const newFileDiff = diffs.find((d) => d.file === "new.txt")
|
||||
expect(newFileDiff).toBeDefined()
|
||||
expect(newFileDiff!.left).toBe("")
|
||||
expect(newFileDiff!.right).toBe("new content")
|
||||
|
||||
const modifiedFileDiff = diffs.find((d) => d.file === "b.txt")
|
||||
expect(modifiedFileDiff).toBeDefined()
|
||||
expect(modifiedFileDiff!.left).toBe(tmp.extra.bContent)
|
||||
expect(modifiedFileDiff!.right).toBe("modified content")
|
||||
},
|
||||
})
|
||||
|
||||
await Instance.provide({
|
||||
directory: tmp.path,
|
||||
fn: async () => {
|
||||
const before = await Snapshot.track()
|
||||
expect(before).toBeTruthy()
|
||||
|
||||
await Bun.write(`${tmp.path}/added.txt`, "added content")
|
||||
await $`rm ${tmp.path}/a.txt`.quiet()
|
||||
|
||||
const after = await Snapshot.track()
|
||||
expect(after).toBeTruthy()
|
||||
|
||||
const diffs = await Snapshot.diffFull(before!, after!)
|
||||
expect(diffs.length).toBe(2)
|
||||
|
||||
const addedFileDiff = diffs.find((d) => d.file === "added.txt")
|
||||
expect(addedFileDiff).toBeDefined()
|
||||
expect(addedFileDiff!.left).toBe("")
|
||||
expect(addedFileDiff!.right).toBe("added content")
|
||||
|
||||
const removedFileDiff = diffs.find((d) => d.file === "a.txt")
|
||||
expect(removedFileDiff).toBeDefined()
|
||||
expect(removedFileDiff!.left).toBe(tmp.extra.aContent)
|
||||
expect(removedFileDiff!.right).toBe("")
|
||||
},
|
||||
})
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user