snapshot.test.ts 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040
  1. import { test, expect } from "bun:test"
  2. import { $ } from "bun"
  3. import { Snapshot } from "../../src/snapshot"
  4. import { Instance } from "../../src/project/instance"
  5. import { tmpdir } from "../fixture/fixture"
  6. async function bootstrap() {
  7. return tmpdir({
  8. git: true,
  9. init: async (dir) => {
  10. const unique = Math.random().toString(36).slice(2)
  11. const aContent = `A${unique}`
  12. const bContent = `B${unique}`
  13. await Bun.write(`${dir}/a.txt`, aContent)
  14. await Bun.write(`${dir}/b.txt`, bContent)
  15. await $`git add .`.cwd(dir).quiet()
  16. await $`git commit --no-gpg-sign -m init`.cwd(dir).quiet()
  17. return {
  18. aContent,
  19. bContent,
  20. }
  21. },
  22. })
  23. }
  24. test("tracks deleted files correctly", async () => {
  25. await using tmp = await bootstrap()
  26. await Instance.provide({
  27. directory: tmp.path,
  28. fn: async () => {
  29. const before = await Snapshot.track()
  30. expect(before).toBeTruthy()
  31. await $`rm ${tmp.path}/a.txt`.quiet()
  32. expect((await Snapshot.patch(before!)).files).toContain(`${tmp.path}/a.txt`)
  33. },
  34. })
  35. })
  36. test("revert should remove new files", async () => {
  37. await using tmp = await bootstrap()
  38. await Instance.provide({
  39. directory: tmp.path,
  40. fn: async () => {
  41. const before = await Snapshot.track()
  42. expect(before).toBeTruthy()
  43. await Bun.write(`${tmp.path}/new.txt`, "NEW")
  44. await Snapshot.revert([await Snapshot.patch(before!)])
  45. expect(await Bun.file(`${tmp.path}/new.txt`).exists()).toBe(false)
  46. },
  47. })
  48. })
  49. test("revert in subdirectory", async () => {
  50. await using tmp = await bootstrap()
  51. await Instance.provide({
  52. directory: tmp.path,
  53. fn: async () => {
  54. const before = await Snapshot.track()
  55. expect(before).toBeTruthy()
  56. await $`mkdir -p ${tmp.path}/sub`.quiet()
  57. await Bun.write(`${tmp.path}/sub/file.txt`, "SUB")
  58. await Snapshot.revert([await Snapshot.patch(before!)])
  59. expect(await Bun.file(`${tmp.path}/sub/file.txt`).exists()).toBe(false)
  60. // Note: revert currently only removes files, not directories
  61. // The empty subdirectory will remain
  62. },
  63. })
  64. })
  65. test("multiple file operations", async () => {
  66. await using tmp = await bootstrap()
  67. await Instance.provide({
  68. directory: tmp.path,
  69. fn: async () => {
  70. const before = await Snapshot.track()
  71. expect(before).toBeTruthy()
  72. await $`rm ${tmp.path}/a.txt`.quiet()
  73. await Bun.write(`${tmp.path}/c.txt`, "C")
  74. await $`mkdir -p ${tmp.path}/dir`.quiet()
  75. await Bun.write(`${tmp.path}/dir/d.txt`, "D")
  76. await Bun.write(`${tmp.path}/b.txt`, "MODIFIED")
  77. await Snapshot.revert([await Snapshot.patch(before!)])
  78. expect(await Bun.file(`${tmp.path}/a.txt`).text()).toBe(tmp.extra.aContent)
  79. expect(await Bun.file(`${tmp.path}/c.txt`).exists()).toBe(false)
  80. // Note: revert currently only removes files, not directories
  81. // The empty directory will remain
  82. expect(await Bun.file(`${tmp.path}/b.txt`).text()).toBe(tmp.extra.bContent)
  83. },
  84. })
  85. })
  86. test("empty directory handling", async () => {
  87. await using tmp = await bootstrap()
  88. await Instance.provide({
  89. directory: tmp.path,
  90. fn: async () => {
  91. const before = await Snapshot.track()
  92. expect(before).toBeTruthy()
  93. await $`mkdir ${tmp.path}/empty`.quiet()
  94. expect((await Snapshot.patch(before!)).files.length).toBe(0)
  95. },
  96. })
  97. })
  98. test("binary file handling", async () => {
  99. await using tmp = await bootstrap()
  100. await Instance.provide({
  101. directory: tmp.path,
  102. fn: async () => {
  103. const before = await Snapshot.track()
  104. expect(before).toBeTruthy()
  105. await Bun.write(`${tmp.path}/image.png`, new Uint8Array([0x89, 0x50, 0x4e, 0x47]))
  106. const patch = await Snapshot.patch(before!)
  107. expect(patch.files).toContain(`${tmp.path}/image.png`)
  108. await Snapshot.revert([patch])
  109. expect(await Bun.file(`${tmp.path}/image.png`).exists()).toBe(false)
  110. },
  111. })
  112. })
  113. test("symlink handling", async () => {
  114. await using tmp = await bootstrap()
  115. await Instance.provide({
  116. directory: tmp.path,
  117. fn: async () => {
  118. const before = await Snapshot.track()
  119. expect(before).toBeTruthy()
  120. await $`ln -s ${tmp.path}/a.txt ${tmp.path}/link.txt`.quiet()
  121. expect((await Snapshot.patch(before!)).files).toContain(`${tmp.path}/link.txt`)
  122. },
  123. })
  124. })
  125. test("large file handling", async () => {
  126. await using tmp = await bootstrap()
  127. await Instance.provide({
  128. directory: tmp.path,
  129. fn: async () => {
  130. const before = await Snapshot.track()
  131. expect(before).toBeTruthy()
  132. await Bun.write(`${tmp.path}/large.txt`, "x".repeat(1024 * 1024))
  133. expect((await Snapshot.patch(before!)).files).toContain(`${tmp.path}/large.txt`)
  134. },
  135. })
  136. })
  137. test("nested directory revert", async () => {
  138. await using tmp = await bootstrap()
  139. await Instance.provide({
  140. directory: tmp.path,
  141. fn: async () => {
  142. const before = await Snapshot.track()
  143. expect(before).toBeTruthy()
  144. await $`mkdir -p ${tmp.path}/level1/level2/level3`.quiet()
  145. await Bun.write(`${tmp.path}/level1/level2/level3/deep.txt`, "DEEP")
  146. await Snapshot.revert([await Snapshot.patch(before!)])
  147. expect(await Bun.file(`${tmp.path}/level1/level2/level3/deep.txt`).exists()).toBe(false)
  148. },
  149. })
  150. })
  151. test("special characters in filenames", async () => {
  152. await using tmp = await bootstrap()
  153. await Instance.provide({
  154. directory: tmp.path,
  155. fn: async () => {
  156. const before = await Snapshot.track()
  157. expect(before).toBeTruthy()
  158. await Bun.write(`${tmp.path}/file with spaces.txt`, "SPACES")
  159. await Bun.write(`${tmp.path}/file-with-dashes.txt`, "DASHES")
  160. await Bun.write(`${tmp.path}/file_with_underscores.txt`, "UNDERSCORES")
  161. const files = (await Snapshot.patch(before!)).files
  162. expect(files).toContain(`${tmp.path}/file with spaces.txt`)
  163. expect(files).toContain(`${tmp.path}/file-with-dashes.txt`)
  164. expect(files).toContain(`${tmp.path}/file_with_underscores.txt`)
  165. },
  166. })
  167. })
  168. test("revert with empty patches", async () => {
  169. await using tmp = await bootstrap()
  170. await Instance.provide({
  171. directory: tmp.path,
  172. fn: async () => {
  173. // Should not crash with empty patches
  174. expect(Snapshot.revert([])).resolves.toBeUndefined()
  175. // Should not crash with patches that have empty file lists
  176. expect(Snapshot.revert([{ hash: "dummy", files: [] }])).resolves.toBeUndefined()
  177. },
  178. })
  179. })
  180. test("patch with invalid hash", async () => {
  181. await using tmp = await bootstrap()
  182. await Instance.provide({
  183. directory: tmp.path,
  184. fn: async () => {
  185. const before = await Snapshot.track()
  186. expect(before).toBeTruthy()
  187. // Create a change
  188. await Bun.write(`${tmp.path}/test.txt`, "TEST")
  189. // Try to patch with invalid hash - should handle gracefully
  190. const patch = await Snapshot.patch("invalid-hash-12345")
  191. expect(patch.files).toEqual([])
  192. expect(patch.hash).toBe("invalid-hash-12345")
  193. },
  194. })
  195. })
  196. test("revert non-existent file", async () => {
  197. await using tmp = await bootstrap()
  198. await Instance.provide({
  199. directory: tmp.path,
  200. fn: async () => {
  201. const before = await Snapshot.track()
  202. expect(before).toBeTruthy()
  203. // Try to revert a file that doesn't exist in the snapshot
  204. // This should not crash
  205. expect(
  206. Snapshot.revert([
  207. {
  208. hash: before!,
  209. files: [`${tmp.path}/nonexistent.txt`],
  210. },
  211. ]),
  212. ).resolves.toBeUndefined()
  213. },
  214. })
  215. })
  216. test("unicode filenames", async () => {
  217. await using tmp = await bootstrap()
  218. await Instance.provide({
  219. directory: tmp.path,
  220. fn: async () => {
  221. const before = await Snapshot.track()
  222. expect(before).toBeTruthy()
  223. const unicodeFiles = [
  224. { path: `${tmp.path}/文件.txt`, content: "chinese content" },
  225. { path: `${tmp.path}/🚀rocket.txt`, content: "emoji content" },
  226. { path: `${tmp.path}/café.txt`, content: "accented content" },
  227. { path: `${tmp.path}/файл.txt`, content: "cyrillic content" },
  228. ]
  229. for (const file of unicodeFiles) {
  230. await Bun.write(file.path, file.content)
  231. }
  232. const patch = await Snapshot.patch(before!)
  233. expect(patch.files.length).toBe(4)
  234. for (const file of unicodeFiles) {
  235. expect(patch.files).toContain(file.path)
  236. }
  237. await Snapshot.revert([patch])
  238. for (const file of unicodeFiles) {
  239. expect(await Bun.file(file.path).exists()).toBe(false)
  240. }
  241. },
  242. })
  243. })
  244. test.skip("unicode filenames modification and restore", async () => {
  245. await using tmp = await bootstrap()
  246. await Instance.provide({
  247. directory: tmp.path,
  248. fn: async () => {
  249. const chineseFile = `${tmp.path}/文件.txt`
  250. const cyrillicFile = `${tmp.path}/файл.txt`
  251. await Bun.write(chineseFile, "original chinese")
  252. await Bun.write(cyrillicFile, "original cyrillic")
  253. const before = await Snapshot.track()
  254. expect(before).toBeTruthy()
  255. await Bun.write(chineseFile, "modified chinese")
  256. await Bun.write(cyrillicFile, "modified cyrillic")
  257. const patch = await Snapshot.patch(before!)
  258. expect(patch.files).toContain(chineseFile)
  259. expect(patch.files).toContain(cyrillicFile)
  260. await Snapshot.revert([patch])
  261. expect(await Bun.file(chineseFile).text()).toBe("original chinese")
  262. expect(await Bun.file(cyrillicFile).text()).toBe("original cyrillic")
  263. },
  264. })
  265. })
  266. test("unicode filenames in subdirectories", async () => {
  267. await using tmp = await bootstrap()
  268. await Instance.provide({
  269. directory: tmp.path,
  270. fn: async () => {
  271. const before = await Snapshot.track()
  272. expect(before).toBeTruthy()
  273. await $`mkdir -p "${tmp.path}/目录/подкаталог"`.quiet()
  274. const deepFile = `${tmp.path}/目录/подкаталог/文件.txt`
  275. await Bun.write(deepFile, "deep unicode content")
  276. const patch = await Snapshot.patch(before!)
  277. expect(patch.files).toContain(deepFile)
  278. await Snapshot.revert([patch])
  279. expect(await Bun.file(deepFile).exists()).toBe(false)
  280. },
  281. })
  282. })
  283. test("very long filenames", async () => {
  284. await using tmp = await bootstrap()
  285. await Instance.provide({
  286. directory: tmp.path,
  287. fn: async () => {
  288. const before = await Snapshot.track()
  289. expect(before).toBeTruthy()
  290. const longName = "a".repeat(200) + ".txt"
  291. const longFile = `${tmp.path}/${longName}`
  292. await Bun.write(longFile, "long filename content")
  293. const patch = await Snapshot.patch(before!)
  294. expect(patch.files).toContain(longFile)
  295. await Snapshot.revert([patch])
  296. expect(await Bun.file(longFile).exists()).toBe(false)
  297. },
  298. })
  299. })
  300. test("hidden files", async () => {
  301. await using tmp = await bootstrap()
  302. await Instance.provide({
  303. directory: tmp.path,
  304. fn: async () => {
  305. const before = await Snapshot.track()
  306. expect(before).toBeTruthy()
  307. await Bun.write(`${tmp.path}/.hidden`, "hidden content")
  308. await Bun.write(`${tmp.path}/.gitignore`, "*.log")
  309. await Bun.write(`${tmp.path}/.config`, "config content")
  310. const patch = await Snapshot.patch(before!)
  311. expect(patch.files).toContain(`${tmp.path}/.hidden`)
  312. expect(patch.files).toContain(`${tmp.path}/.gitignore`)
  313. expect(patch.files).toContain(`${tmp.path}/.config`)
  314. },
  315. })
  316. })
  317. test("nested symlinks", async () => {
  318. await using tmp = await bootstrap()
  319. await Instance.provide({
  320. directory: tmp.path,
  321. fn: async () => {
  322. const before = await Snapshot.track()
  323. expect(before).toBeTruthy()
  324. await $`mkdir -p ${tmp.path}/sub/dir`.quiet()
  325. await Bun.write(`${tmp.path}/sub/dir/target.txt`, "target content")
  326. await $`ln -s ${tmp.path}/sub/dir/target.txt ${tmp.path}/sub/dir/link.txt`.quiet()
  327. await $`ln -s ${tmp.path}/sub ${tmp.path}/sub-link`.quiet()
  328. const patch = await Snapshot.patch(before!)
  329. expect(patch.files).toContain(`${tmp.path}/sub/dir/link.txt`)
  330. expect(patch.files).toContain(`${tmp.path}/sub-link`)
  331. },
  332. })
  333. })
  334. test("file permissions and ownership changes", async () => {
  335. await using tmp = await bootstrap()
  336. await Instance.provide({
  337. directory: tmp.path,
  338. fn: async () => {
  339. const before = await Snapshot.track()
  340. expect(before).toBeTruthy()
  341. // Change permissions multiple times
  342. await $`chmod 600 ${tmp.path}/a.txt`.quiet()
  343. await $`chmod 755 ${tmp.path}/a.txt`.quiet()
  344. await $`chmod 644 ${tmp.path}/a.txt`.quiet()
  345. const patch = await Snapshot.patch(before!)
  346. // Note: git doesn't track permission changes on existing files by default
  347. // Only tracks executable bit when files are first added
  348. expect(patch.files.length).toBe(0)
  349. },
  350. })
  351. })
  352. test("circular symlinks", async () => {
  353. await using tmp = await bootstrap()
  354. await Instance.provide({
  355. directory: tmp.path,
  356. fn: async () => {
  357. const before = await Snapshot.track()
  358. expect(before).toBeTruthy()
  359. // Create circular symlink
  360. await $`ln -s ${tmp.path}/circular ${tmp.path}/circular`.quiet().nothrow()
  361. const patch = await Snapshot.patch(before!)
  362. expect(patch.files.length).toBeGreaterThanOrEqual(0) // Should not crash
  363. },
  364. })
  365. })
  366. test("gitignore changes", async () => {
  367. await using tmp = await bootstrap()
  368. await Instance.provide({
  369. directory: tmp.path,
  370. fn: async () => {
  371. const before = await Snapshot.track()
  372. expect(before).toBeTruthy()
  373. await Bun.write(`${tmp.path}/.gitignore`, "*.ignored")
  374. await Bun.write(`${tmp.path}/test.ignored`, "ignored content")
  375. await Bun.write(`${tmp.path}/normal.txt`, "normal content")
  376. const patch = await Snapshot.patch(before!)
  377. // Should track gitignore itself
  378. expect(patch.files).toContain(`${tmp.path}/.gitignore`)
  379. // Should track normal files
  380. expect(patch.files).toContain(`${tmp.path}/normal.txt`)
  381. // Should not track ignored files (git won't see them)
  382. expect(patch.files).not.toContain(`${tmp.path}/test.ignored`)
  383. },
  384. })
  385. })
  386. test("concurrent file operations during patch", async () => {
  387. await using tmp = await bootstrap()
  388. await Instance.provide({
  389. directory: tmp.path,
  390. fn: async () => {
  391. const before = await Snapshot.track()
  392. expect(before).toBeTruthy()
  393. // Start creating files
  394. const createPromise = (async () => {
  395. for (let i = 0; i < 10; i++) {
  396. await Bun.write(`${tmp.path}/concurrent${i}.txt`, `concurrent${i}`)
  397. // Small delay to simulate concurrent operations
  398. await new Promise((resolve) => setTimeout(resolve, 1))
  399. }
  400. })()
  401. // Get patch while files are being created
  402. const patchPromise = Snapshot.patch(before!)
  403. await createPromise
  404. const patch = await patchPromise
  405. // Should capture some or all of the concurrent files
  406. expect(patch.files.length).toBeGreaterThanOrEqual(0)
  407. },
  408. })
  409. })
  410. test("snapshot state isolation between projects", async () => {
  411. // Test that different projects don't interfere with each other
  412. await using tmp1 = await bootstrap()
  413. await using tmp2 = await bootstrap()
  414. await Instance.provide({
  415. directory: tmp1.path,
  416. fn: async () => {
  417. const before1 = await Snapshot.track()
  418. await Bun.write(`${tmp1.path}/project1.txt`, "project1 content")
  419. const patch1 = await Snapshot.patch(before1!)
  420. expect(patch1.files).toContain(`${tmp1.path}/project1.txt`)
  421. },
  422. })
  423. await Instance.provide({
  424. directory: tmp2.path,
  425. fn: async () => {
  426. const before2 = await Snapshot.track()
  427. await Bun.write(`${tmp2.path}/project2.txt`, "project2 content")
  428. const patch2 = await Snapshot.patch(before2!)
  429. expect(patch2.files).toContain(`${tmp2.path}/project2.txt`)
  430. // Ensure project1 files don't appear in project2
  431. expect(patch2.files).not.toContain(`${tmp1?.path}/project1.txt`)
  432. },
  433. })
  434. })
  435. test("patch detects changes in secondary worktree", async () => {
  436. await using tmp = await bootstrap()
  437. const worktreePath = `${tmp.path}-worktree`
  438. await $`git worktree add ${worktreePath} HEAD`.cwd(tmp.path).quiet()
  439. try {
  440. await Instance.provide({
  441. directory: tmp.path,
  442. fn: async () => {
  443. expect(await Snapshot.track()).toBeTruthy()
  444. },
  445. })
  446. await Instance.provide({
  447. directory: worktreePath,
  448. fn: async () => {
  449. const before = await Snapshot.track()
  450. expect(before).toBeTruthy()
  451. const worktreeFile = `${worktreePath}/worktree.txt`
  452. await Bun.write(worktreeFile, "worktree content")
  453. const patch = await Snapshot.patch(before!)
  454. expect(patch.files).toContain(worktreeFile)
  455. },
  456. })
  457. } finally {
  458. await $`git worktree remove --force ${worktreePath}`.cwd(tmp.path).quiet().nothrow()
  459. await $`rm -rf ${worktreePath}`.quiet()
  460. }
  461. })
  462. test("revert only removes files in invoking worktree", async () => {
  463. await using tmp = await bootstrap()
  464. const worktreePath = `${tmp.path}-worktree`
  465. await $`git worktree add ${worktreePath} HEAD`.cwd(tmp.path).quiet()
  466. try {
  467. await Instance.provide({
  468. directory: tmp.path,
  469. fn: async () => {
  470. expect(await Snapshot.track()).toBeTruthy()
  471. },
  472. })
  473. const primaryFile = `${tmp.path}/worktree.txt`
  474. await Bun.write(primaryFile, "primary content")
  475. await Instance.provide({
  476. directory: worktreePath,
  477. fn: async () => {
  478. const before = await Snapshot.track()
  479. expect(before).toBeTruthy()
  480. const worktreeFile = `${worktreePath}/worktree.txt`
  481. await Bun.write(worktreeFile, "worktree content")
  482. const patch = await Snapshot.patch(before!)
  483. await Snapshot.revert([patch])
  484. expect(await Bun.file(worktreeFile).exists()).toBe(false)
  485. },
  486. })
  487. expect(await Bun.file(primaryFile).text()).toBe("primary content")
  488. } finally {
  489. await $`git worktree remove --force ${worktreePath}`.cwd(tmp.path).quiet().nothrow()
  490. await $`rm -rf ${worktreePath}`.quiet()
  491. await $`rm -f ${tmp.path}/worktree.txt`.quiet()
  492. }
  493. })
  494. test("diff reports worktree-only/shared edits and ignores primary-only", async () => {
  495. await using tmp = await bootstrap()
  496. const worktreePath = `${tmp.path}-worktree`
  497. await $`git worktree add ${worktreePath} HEAD`.cwd(tmp.path).quiet()
  498. try {
  499. await Instance.provide({
  500. directory: tmp.path,
  501. fn: async () => {
  502. expect(await Snapshot.track()).toBeTruthy()
  503. },
  504. })
  505. await Instance.provide({
  506. directory: worktreePath,
  507. fn: async () => {
  508. const before = await Snapshot.track()
  509. expect(before).toBeTruthy()
  510. await Bun.write(`${worktreePath}/worktree-only.txt`, "worktree diff content")
  511. await Bun.write(`${worktreePath}/shared.txt`, "worktree edit")
  512. await Bun.write(`${tmp.path}/shared.txt`, "primary edit")
  513. await Bun.write(`${tmp.path}/primary-only.txt`, "primary change")
  514. const diff = await Snapshot.diff(before!)
  515. expect(diff).toContain("worktree-only.txt")
  516. expect(diff).toContain("shared.txt")
  517. expect(diff).not.toContain("primary-only.txt")
  518. },
  519. })
  520. } finally {
  521. await $`git worktree remove --force ${worktreePath}`.cwd(tmp.path).quiet().nothrow()
  522. await $`rm -rf ${worktreePath}`.quiet()
  523. await $`rm -f ${tmp.path}/shared.txt`.quiet()
  524. await $`rm -f ${tmp.path}/primary-only.txt`.quiet()
  525. }
  526. })
  527. test("track with no changes returns same hash", async () => {
  528. await using tmp = await bootstrap()
  529. await Instance.provide({
  530. directory: tmp.path,
  531. fn: async () => {
  532. const hash1 = await Snapshot.track()
  533. expect(hash1).toBeTruthy()
  534. // Track again with no changes
  535. const hash2 = await Snapshot.track()
  536. expect(hash2).toBe(hash1!)
  537. // Track again
  538. const hash3 = await Snapshot.track()
  539. expect(hash3).toBe(hash1!)
  540. },
  541. })
  542. })
  543. test("diff function with various changes", async () => {
  544. await using tmp = await bootstrap()
  545. await Instance.provide({
  546. directory: tmp.path,
  547. fn: async () => {
  548. const before = await Snapshot.track()
  549. expect(before).toBeTruthy()
  550. // Make various changes
  551. await $`rm ${tmp.path}/a.txt`.quiet()
  552. await Bun.write(`${tmp.path}/new.txt`, "new content")
  553. await Bun.write(`${tmp.path}/b.txt`, "modified content")
  554. const diff = await Snapshot.diff(before!)
  555. expect(diff).toContain("a.txt")
  556. expect(diff).toContain("b.txt")
  557. expect(diff).toContain("new.txt")
  558. },
  559. })
  560. })
  561. test("restore function", async () => {
  562. await using tmp = await bootstrap()
  563. await Instance.provide({
  564. directory: tmp.path,
  565. fn: async () => {
  566. const before = await Snapshot.track()
  567. expect(before).toBeTruthy()
  568. // Make changes
  569. await $`rm ${tmp.path}/a.txt`.quiet()
  570. await Bun.write(`${tmp.path}/new.txt`, "new content")
  571. await Bun.write(`${tmp.path}/b.txt`, "modified")
  572. // Restore to original state
  573. await Snapshot.restore(before!)
  574. expect(await Bun.file(`${tmp.path}/a.txt`).exists()).toBe(true)
  575. expect(await Bun.file(`${tmp.path}/a.txt`).text()).toBe(tmp.extra.aContent)
  576. expect(await Bun.file(`${tmp.path}/new.txt`).exists()).toBe(true) // New files should remain
  577. expect(await Bun.file(`${tmp.path}/b.txt`).text()).toBe(tmp.extra.bContent)
  578. },
  579. })
  580. })
  581. test("revert should not delete files that existed but were deleted in snapshot", async () => {
  582. await using tmp = await bootstrap()
  583. await Instance.provide({
  584. directory: tmp.path,
  585. fn: async () => {
  586. const snapshot1 = await Snapshot.track()
  587. expect(snapshot1).toBeTruthy()
  588. await $`rm ${tmp.path}/a.txt`.quiet()
  589. const snapshot2 = await Snapshot.track()
  590. expect(snapshot2).toBeTruthy()
  591. await Bun.write(`${tmp.path}/a.txt`, "recreated content")
  592. const patch = await Snapshot.patch(snapshot2!)
  593. expect(patch.files).toContain(`${tmp.path}/a.txt`)
  594. await Snapshot.revert([patch])
  595. expect(await Bun.file(`${tmp.path}/a.txt`).exists()).toBe(false)
  596. },
  597. })
  598. })
  599. test("revert preserves file that existed in snapshot when deleted then recreated", async () => {
  600. await using tmp = await bootstrap()
  601. await Instance.provide({
  602. directory: tmp.path,
  603. fn: async () => {
  604. await Bun.write(`${tmp.path}/existing.txt`, "original content")
  605. const snapshot = await Snapshot.track()
  606. expect(snapshot).toBeTruthy()
  607. await $`rm ${tmp.path}/existing.txt`.quiet()
  608. await Bun.write(`${tmp.path}/existing.txt`, "recreated")
  609. await Bun.write(`${tmp.path}/newfile.txt`, "new")
  610. const patch = await Snapshot.patch(snapshot!)
  611. expect(patch.files).toContain(`${tmp.path}/existing.txt`)
  612. expect(patch.files).toContain(`${tmp.path}/newfile.txt`)
  613. await Snapshot.revert([patch])
  614. expect(await Bun.file(`${tmp.path}/newfile.txt`).exists()).toBe(false)
  615. expect(await Bun.file(`${tmp.path}/existing.txt`).exists()).toBe(true)
  616. expect(await Bun.file(`${tmp.path}/existing.txt`).text()).toBe("original content")
  617. },
  618. })
  619. })
  620. test("diffFull sets status based on git change type", async () => {
  621. await using tmp = await bootstrap()
  622. await Instance.provide({
  623. directory: tmp.path,
  624. fn: async () => {
  625. await Bun.write(`${tmp.path}/grow.txt`, "one\n")
  626. await Bun.write(`${tmp.path}/trim.txt`, "line1\nline2\n")
  627. await Bun.write(`${tmp.path}/delete.txt`, "gone")
  628. const before = await Snapshot.track()
  629. expect(before).toBeTruthy()
  630. await Bun.write(`${tmp.path}/grow.txt`, "one\ntwo\n")
  631. await Bun.write(`${tmp.path}/trim.txt`, "line1\n")
  632. await $`rm ${tmp.path}/delete.txt`.quiet()
  633. await Bun.write(`${tmp.path}/added.txt`, "new")
  634. const after = await Snapshot.track()
  635. expect(after).toBeTruthy()
  636. const diffs = await Snapshot.diffFull(before!, after!)
  637. expect(diffs.length).toBe(4)
  638. const added = diffs.find((d) => d.file === "added.txt")
  639. expect(added).toBeDefined()
  640. expect(added!.status).toBe("added")
  641. const deleted = diffs.find((d) => d.file === "delete.txt")
  642. expect(deleted).toBeDefined()
  643. expect(deleted!.status).toBe("deleted")
  644. const grow = diffs.find((d) => d.file === "grow.txt")
  645. expect(grow).toBeDefined()
  646. expect(grow!.status).toBe("modified")
  647. expect(grow!.additions).toBeGreaterThan(0)
  648. expect(grow!.deletions).toBe(0)
  649. const trim = diffs.find((d) => d.file === "trim.txt")
  650. expect(trim).toBeDefined()
  651. expect(trim!.status).toBe("modified")
  652. expect(trim!.additions).toBe(0)
  653. expect(trim!.deletions).toBeGreaterThan(0)
  654. },
  655. })
  656. })
  657. test("diffFull with new file additions", async () => {
  658. await using tmp = await bootstrap()
  659. await Instance.provide({
  660. directory: tmp.path,
  661. fn: async () => {
  662. const before = await Snapshot.track()
  663. expect(before).toBeTruthy()
  664. await Bun.write(`${tmp.path}/new.txt`, "new content")
  665. const after = await Snapshot.track()
  666. expect(after).toBeTruthy()
  667. const diffs = await Snapshot.diffFull(before!, after!)
  668. expect(diffs.length).toBe(1)
  669. const newFileDiff = diffs[0]
  670. expect(newFileDiff.file).toBe("new.txt")
  671. expect(newFileDiff.before).toBe("")
  672. expect(newFileDiff.after).toBe("new content")
  673. expect(newFileDiff.additions).toBe(1)
  674. expect(newFileDiff.deletions).toBe(0)
  675. },
  676. })
  677. })
  678. test("diffFull with file modifications", async () => {
  679. await using tmp = await bootstrap()
  680. await Instance.provide({
  681. directory: tmp.path,
  682. fn: async () => {
  683. const before = await Snapshot.track()
  684. expect(before).toBeTruthy()
  685. await Bun.write(`${tmp.path}/b.txt`, "modified content")
  686. const after = await Snapshot.track()
  687. expect(after).toBeTruthy()
  688. const diffs = await Snapshot.diffFull(before!, after!)
  689. expect(diffs.length).toBe(1)
  690. const modifiedFileDiff = diffs[0]
  691. expect(modifiedFileDiff.file).toBe("b.txt")
  692. expect(modifiedFileDiff.before).toBe(tmp.extra.bContent)
  693. expect(modifiedFileDiff.after).toBe("modified content")
  694. expect(modifiedFileDiff.additions).toBeGreaterThan(0)
  695. expect(modifiedFileDiff.deletions).toBeGreaterThan(0)
  696. },
  697. })
  698. })
  699. test("diffFull with file deletions", async () => {
  700. await using tmp = await bootstrap()
  701. await Instance.provide({
  702. directory: tmp.path,
  703. fn: async () => {
  704. const before = await Snapshot.track()
  705. expect(before).toBeTruthy()
  706. await $`rm ${tmp.path}/a.txt`.quiet()
  707. const after = await Snapshot.track()
  708. expect(after).toBeTruthy()
  709. const diffs = await Snapshot.diffFull(before!, after!)
  710. expect(diffs.length).toBe(1)
  711. const removedFileDiff = diffs[0]
  712. expect(removedFileDiff.file).toBe("a.txt")
  713. expect(removedFileDiff.before).toBe(tmp.extra.aContent)
  714. expect(removedFileDiff.after).toBe("")
  715. expect(removedFileDiff.additions).toBe(0)
  716. expect(removedFileDiff.deletions).toBe(1)
  717. },
  718. })
  719. })
  720. test("diffFull with multiple line additions", async () => {
  721. await using tmp = await bootstrap()
  722. await Instance.provide({
  723. directory: tmp.path,
  724. fn: async () => {
  725. const before = await Snapshot.track()
  726. expect(before).toBeTruthy()
  727. await Bun.write(`${tmp.path}/multi.txt`, "line1\nline2\nline3")
  728. const after = await Snapshot.track()
  729. expect(after).toBeTruthy()
  730. const diffs = await Snapshot.diffFull(before!, after!)
  731. expect(diffs.length).toBe(1)
  732. const multiDiff = diffs[0]
  733. expect(multiDiff.file).toBe("multi.txt")
  734. expect(multiDiff.before).toBe("")
  735. expect(multiDiff.after).toBe("line1\nline2\nline3")
  736. expect(multiDiff.additions).toBe(3)
  737. expect(multiDiff.deletions).toBe(0)
  738. },
  739. })
  740. })
  741. test("diffFull with addition and deletion", async () => {
  742. await using tmp = await bootstrap()
  743. await Instance.provide({
  744. directory: tmp.path,
  745. fn: async () => {
  746. const before = await Snapshot.track()
  747. expect(before).toBeTruthy()
  748. await Bun.write(`${tmp.path}/added.txt`, "added content")
  749. await $`rm ${tmp.path}/a.txt`.quiet()
  750. const after = await Snapshot.track()
  751. expect(after).toBeTruthy()
  752. const diffs = await Snapshot.diffFull(before!, after!)
  753. expect(diffs.length).toBe(2)
  754. const addedFileDiff = diffs.find((d) => d.file === "added.txt")
  755. expect(addedFileDiff).toBeDefined()
  756. expect(addedFileDiff!.before).toBe("")
  757. expect(addedFileDiff!.after).toBe("added content")
  758. expect(addedFileDiff!.additions).toBe(1)
  759. expect(addedFileDiff!.deletions).toBe(0)
  760. const removedFileDiff = diffs.find((d) => d.file === "a.txt")
  761. expect(removedFileDiff).toBeDefined()
  762. expect(removedFileDiff!.before).toBe(tmp.extra.aContent)
  763. expect(removedFileDiff!.after).toBe("")
  764. expect(removedFileDiff!.additions).toBe(0)
  765. expect(removedFileDiff!.deletions).toBe(1)
  766. },
  767. })
  768. })
  769. test("diffFull with multiple additions and deletions", async () => {
  770. await using tmp = await bootstrap()
  771. await Instance.provide({
  772. directory: tmp.path,
  773. fn: async () => {
  774. const before = await Snapshot.track()
  775. expect(before).toBeTruthy()
  776. await Bun.write(`${tmp.path}/multi1.txt`, "line1\nline2\nline3")
  777. await Bun.write(`${tmp.path}/multi2.txt`, "single line")
  778. await $`rm ${tmp.path}/a.txt`.quiet()
  779. await $`rm ${tmp.path}/b.txt`.quiet()
  780. const after = await Snapshot.track()
  781. expect(after).toBeTruthy()
  782. const diffs = await Snapshot.diffFull(before!, after!)
  783. expect(diffs.length).toBe(4)
  784. const multi1Diff = diffs.find((d) => d.file === "multi1.txt")
  785. expect(multi1Diff).toBeDefined()
  786. expect(multi1Diff!.additions).toBe(3)
  787. expect(multi1Diff!.deletions).toBe(0)
  788. const multi2Diff = diffs.find((d) => d.file === "multi2.txt")
  789. expect(multi2Diff).toBeDefined()
  790. expect(multi2Diff!.additions).toBe(1)
  791. expect(multi2Diff!.deletions).toBe(0)
  792. const removedADiff = diffs.find((d) => d.file === "a.txt")
  793. expect(removedADiff).toBeDefined()
  794. expect(removedADiff!.additions).toBe(0)
  795. expect(removedADiff!.deletions).toBe(1)
  796. const removedBDiff = diffs.find((d) => d.file === "b.txt")
  797. expect(removedBDiff).toBeDefined()
  798. expect(removedBDiff!.additions).toBe(0)
  799. expect(removedBDiff!.deletions).toBe(1)
  800. },
  801. })
  802. })
  803. test("diffFull with no changes", async () => {
  804. await using tmp = await bootstrap()
  805. await Instance.provide({
  806. directory: tmp.path,
  807. fn: async () => {
  808. const before = await Snapshot.track()
  809. expect(before).toBeTruthy()
  810. const after = await Snapshot.track()
  811. expect(after).toBeTruthy()
  812. const diffs = await Snapshot.diffFull(before!, after!)
  813. expect(diffs.length).toBe(0)
  814. },
  815. })
  816. })
  817. test("diffFull with binary file changes", async () => {
  818. await using tmp = await bootstrap()
  819. await Instance.provide({
  820. directory: tmp.path,
  821. fn: async () => {
  822. const before = await Snapshot.track()
  823. expect(before).toBeTruthy()
  824. await Bun.write(`${tmp.path}/binary.bin`, new Uint8Array([0x00, 0x01, 0x02, 0x03]))
  825. const after = await Snapshot.track()
  826. expect(after).toBeTruthy()
  827. const diffs = await Snapshot.diffFull(before!, after!)
  828. expect(diffs.length).toBe(1)
  829. const binaryDiff = diffs[0]
  830. expect(binaryDiff.file).toBe("binary.bin")
  831. expect(binaryDiff.before).toBe("")
  832. },
  833. })
  834. })
  835. test("diffFull with whitespace changes", async () => {
  836. await using tmp = await bootstrap()
  837. await Instance.provide({
  838. directory: tmp.path,
  839. fn: async () => {
  840. await Bun.write(`${tmp.path}/whitespace.txt`, "line1\nline2")
  841. const before = await Snapshot.track()
  842. expect(before).toBeTruthy()
  843. await Bun.write(`${tmp.path}/whitespace.txt`, "line1\n\nline2\n")
  844. const after = await Snapshot.track()
  845. expect(after).toBeTruthy()
  846. const diffs = await Snapshot.diffFull(before!, after!)
  847. expect(diffs.length).toBe(1)
  848. const whitespaceDiff = diffs[0]
  849. expect(whitespaceDiff.file).toBe("whitespace.txt")
  850. expect(whitespaceDiff.additions).toBeGreaterThan(0)
  851. },
  852. })
  853. })