snapshot.test.ts 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939
  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. `${tmp.path}/文件.txt`,
  225. `${tmp.path}/🚀rocket.txt`,
  226. `${tmp.path}/café.txt`,
  227. `${tmp.path}/файл.txt`,
  228. ]
  229. for (const file of unicodeFiles) {
  230. await Bun.write(file, "unicode content")
  231. }
  232. const patch = await Snapshot.patch(before!)
  233. // Note: git escapes unicode characters by default, so we just check that files are detected
  234. // The actual filenames will be escaped like "caf\303\251.txt" but functionality works
  235. expect(patch.files.length).toBe(4)
  236. // Skip revert test due to git filename escaping issues
  237. // The functionality works but git uses escaped filenames internally
  238. },
  239. })
  240. })
  241. test("very long filenames", async () => {
  242. await using tmp = await bootstrap()
  243. await Instance.provide({
  244. directory: tmp.path,
  245. fn: async () => {
  246. const before = await Snapshot.track()
  247. expect(before).toBeTruthy()
  248. const longName = "a".repeat(200) + ".txt"
  249. const longFile = `${tmp.path}/${longName}`
  250. await Bun.write(longFile, "long filename content")
  251. const patch = await Snapshot.patch(before!)
  252. expect(patch.files).toContain(longFile)
  253. await Snapshot.revert([patch])
  254. expect(await Bun.file(longFile).exists()).toBe(false)
  255. },
  256. })
  257. })
  258. test("hidden files", async () => {
  259. await using tmp = await bootstrap()
  260. await Instance.provide({
  261. directory: tmp.path,
  262. fn: async () => {
  263. const before = await Snapshot.track()
  264. expect(before).toBeTruthy()
  265. await Bun.write(`${tmp.path}/.hidden`, "hidden content")
  266. await Bun.write(`${tmp.path}/.gitignore`, "*.log")
  267. await Bun.write(`${tmp.path}/.config`, "config content")
  268. const patch = await Snapshot.patch(before!)
  269. expect(patch.files).toContain(`${tmp.path}/.hidden`)
  270. expect(patch.files).toContain(`${tmp.path}/.gitignore`)
  271. expect(patch.files).toContain(`${tmp.path}/.config`)
  272. },
  273. })
  274. })
  275. test("nested symlinks", async () => {
  276. await using tmp = await bootstrap()
  277. await Instance.provide({
  278. directory: tmp.path,
  279. fn: async () => {
  280. const before = await Snapshot.track()
  281. expect(before).toBeTruthy()
  282. await $`mkdir -p ${tmp.path}/sub/dir`.quiet()
  283. await Bun.write(`${tmp.path}/sub/dir/target.txt`, "target content")
  284. await $`ln -s ${tmp.path}/sub/dir/target.txt ${tmp.path}/sub/dir/link.txt`.quiet()
  285. await $`ln -s ${tmp.path}/sub ${tmp.path}/sub-link`.quiet()
  286. const patch = await Snapshot.patch(before!)
  287. expect(patch.files).toContain(`${tmp.path}/sub/dir/link.txt`)
  288. expect(patch.files).toContain(`${tmp.path}/sub-link`)
  289. },
  290. })
  291. })
  292. test("file permissions and ownership changes", async () => {
  293. await using tmp = await bootstrap()
  294. await Instance.provide({
  295. directory: tmp.path,
  296. fn: async () => {
  297. const before = await Snapshot.track()
  298. expect(before).toBeTruthy()
  299. // Change permissions multiple times
  300. await $`chmod 600 ${tmp.path}/a.txt`.quiet()
  301. await $`chmod 755 ${tmp.path}/a.txt`.quiet()
  302. await $`chmod 644 ${tmp.path}/a.txt`.quiet()
  303. const patch = await Snapshot.patch(before!)
  304. // Note: git doesn't track permission changes on existing files by default
  305. // Only tracks executable bit when files are first added
  306. expect(patch.files.length).toBe(0)
  307. },
  308. })
  309. })
  310. test("circular symlinks", async () => {
  311. await using tmp = await bootstrap()
  312. await Instance.provide({
  313. directory: tmp.path,
  314. fn: async () => {
  315. const before = await Snapshot.track()
  316. expect(before).toBeTruthy()
  317. // Create circular symlink
  318. await $`ln -s ${tmp.path}/circular ${tmp.path}/circular`.quiet().nothrow()
  319. const patch = await Snapshot.patch(before!)
  320. expect(patch.files.length).toBeGreaterThanOrEqual(0) // Should not crash
  321. },
  322. })
  323. })
  324. test("gitignore changes", async () => {
  325. await using tmp = await bootstrap()
  326. await Instance.provide({
  327. directory: tmp.path,
  328. fn: async () => {
  329. const before = await Snapshot.track()
  330. expect(before).toBeTruthy()
  331. await Bun.write(`${tmp.path}/.gitignore`, "*.ignored")
  332. await Bun.write(`${tmp.path}/test.ignored`, "ignored content")
  333. await Bun.write(`${tmp.path}/normal.txt`, "normal content")
  334. const patch = await Snapshot.patch(before!)
  335. // Should track gitignore itself
  336. expect(patch.files).toContain(`${tmp.path}/.gitignore`)
  337. // Should track normal files
  338. expect(patch.files).toContain(`${tmp.path}/normal.txt`)
  339. // Should not track ignored files (git won't see them)
  340. expect(patch.files).not.toContain(`${tmp.path}/test.ignored`)
  341. },
  342. })
  343. })
  344. test("concurrent file operations during patch", async () => {
  345. await using tmp = await bootstrap()
  346. await Instance.provide({
  347. directory: tmp.path,
  348. fn: async () => {
  349. const before = await Snapshot.track()
  350. expect(before).toBeTruthy()
  351. // Start creating files
  352. const createPromise = (async () => {
  353. for (let i = 0; i < 10; i++) {
  354. await Bun.write(`${tmp.path}/concurrent${i}.txt`, `concurrent${i}`)
  355. // Small delay to simulate concurrent operations
  356. await new Promise((resolve) => setTimeout(resolve, 1))
  357. }
  358. })()
  359. // Get patch while files are being created
  360. const patchPromise = Snapshot.patch(before!)
  361. await createPromise
  362. const patch = await patchPromise
  363. // Should capture some or all of the concurrent files
  364. expect(patch.files.length).toBeGreaterThanOrEqual(0)
  365. },
  366. })
  367. })
  368. test("snapshot state isolation between projects", async () => {
  369. // Test that different projects don't interfere with each other
  370. await using tmp1 = await bootstrap()
  371. await using tmp2 = await bootstrap()
  372. await Instance.provide({
  373. directory: tmp1.path,
  374. fn: async () => {
  375. const before1 = await Snapshot.track()
  376. await Bun.write(`${tmp1.path}/project1.txt`, "project1 content")
  377. const patch1 = await Snapshot.patch(before1!)
  378. expect(patch1.files).toContain(`${tmp1.path}/project1.txt`)
  379. },
  380. })
  381. await Instance.provide({
  382. directory: tmp2.path,
  383. fn: async () => {
  384. const before2 = await Snapshot.track()
  385. await Bun.write(`${tmp2.path}/project2.txt`, "project2 content")
  386. const patch2 = await Snapshot.patch(before2!)
  387. expect(patch2.files).toContain(`${tmp2.path}/project2.txt`)
  388. // Ensure project1 files don't appear in project2
  389. expect(patch2.files).not.toContain(`${tmp1?.path}/project1.txt`)
  390. },
  391. })
  392. })
  393. test("patch detects changes in secondary worktree", async () => {
  394. await using tmp = await bootstrap()
  395. const worktreePath = `${tmp.path}-worktree`
  396. await $`git worktree add ${worktreePath} HEAD`.cwd(tmp.path).quiet()
  397. try {
  398. await Instance.provide({
  399. directory: tmp.path,
  400. fn: async () => {
  401. expect(await Snapshot.track()).toBeTruthy()
  402. },
  403. })
  404. await Instance.provide({
  405. directory: worktreePath,
  406. fn: async () => {
  407. const before = await Snapshot.track()
  408. expect(before).toBeTruthy()
  409. const worktreeFile = `${worktreePath}/worktree.txt`
  410. await Bun.write(worktreeFile, "worktree content")
  411. const patch = await Snapshot.patch(before!)
  412. expect(patch.files).toContain(worktreeFile)
  413. },
  414. })
  415. } finally {
  416. await $`git worktree remove --force ${worktreePath}`.cwd(tmp.path).quiet().nothrow()
  417. await $`rm -rf ${worktreePath}`.quiet()
  418. }
  419. })
  420. test("revert only removes files in invoking worktree", async () => {
  421. await using tmp = await bootstrap()
  422. const worktreePath = `${tmp.path}-worktree`
  423. await $`git worktree add ${worktreePath} HEAD`.cwd(tmp.path).quiet()
  424. try {
  425. await Instance.provide({
  426. directory: tmp.path,
  427. fn: async () => {
  428. expect(await Snapshot.track()).toBeTruthy()
  429. },
  430. })
  431. const primaryFile = `${tmp.path}/worktree.txt`
  432. await Bun.write(primaryFile, "primary content")
  433. await Instance.provide({
  434. directory: worktreePath,
  435. fn: async () => {
  436. const before = await Snapshot.track()
  437. expect(before).toBeTruthy()
  438. const worktreeFile = `${worktreePath}/worktree.txt`
  439. await Bun.write(worktreeFile, "worktree content")
  440. const patch = await Snapshot.patch(before!)
  441. await Snapshot.revert([patch])
  442. expect(await Bun.file(worktreeFile).exists()).toBe(false)
  443. },
  444. })
  445. expect(await Bun.file(primaryFile).text()).toBe("primary content")
  446. } finally {
  447. await $`git worktree remove --force ${worktreePath}`.cwd(tmp.path).quiet().nothrow()
  448. await $`rm -rf ${worktreePath}`.quiet()
  449. await $`rm -f ${tmp.path}/worktree.txt`.quiet()
  450. }
  451. })
  452. test("diff reports worktree-only/shared edits and ignores primary-only", async () => {
  453. await using tmp = await bootstrap()
  454. const worktreePath = `${tmp.path}-worktree`
  455. await $`git worktree add ${worktreePath} HEAD`.cwd(tmp.path).quiet()
  456. try {
  457. await Instance.provide({
  458. directory: tmp.path,
  459. fn: async () => {
  460. expect(await Snapshot.track()).toBeTruthy()
  461. },
  462. })
  463. await Instance.provide({
  464. directory: worktreePath,
  465. fn: async () => {
  466. const before = await Snapshot.track()
  467. expect(before).toBeTruthy()
  468. await Bun.write(`${worktreePath}/worktree-only.txt`, "worktree diff content")
  469. await Bun.write(`${worktreePath}/shared.txt`, "worktree edit")
  470. await Bun.write(`${tmp.path}/shared.txt`, "primary edit")
  471. await Bun.write(`${tmp.path}/primary-only.txt`, "primary change")
  472. const diff = await Snapshot.diff(before!)
  473. expect(diff).toContain("worktree-only.txt")
  474. expect(diff).toContain("shared.txt")
  475. expect(diff).not.toContain("primary-only.txt")
  476. },
  477. })
  478. } finally {
  479. await $`git worktree remove --force ${worktreePath}`.cwd(tmp.path).quiet().nothrow()
  480. await $`rm -rf ${worktreePath}`.quiet()
  481. await $`rm -f ${tmp.path}/shared.txt`.quiet()
  482. await $`rm -f ${tmp.path}/primary-only.txt`.quiet()
  483. }
  484. })
  485. test("track with no changes returns same hash", async () => {
  486. await using tmp = await bootstrap()
  487. await Instance.provide({
  488. directory: tmp.path,
  489. fn: async () => {
  490. const hash1 = await Snapshot.track()
  491. expect(hash1).toBeTruthy()
  492. // Track again with no changes
  493. const hash2 = await Snapshot.track()
  494. expect(hash2).toBe(hash1!)
  495. // Track again
  496. const hash3 = await Snapshot.track()
  497. expect(hash3).toBe(hash1!)
  498. },
  499. })
  500. })
  501. test("diff function with various changes", async () => {
  502. await using tmp = await bootstrap()
  503. await Instance.provide({
  504. directory: tmp.path,
  505. fn: async () => {
  506. const before = await Snapshot.track()
  507. expect(before).toBeTruthy()
  508. // Make various changes
  509. await $`rm ${tmp.path}/a.txt`.quiet()
  510. await Bun.write(`${tmp.path}/new.txt`, "new content")
  511. await Bun.write(`${tmp.path}/b.txt`, "modified content")
  512. const diff = await Snapshot.diff(before!)
  513. expect(diff).toContain("a.txt")
  514. expect(diff).toContain("b.txt")
  515. expect(diff).toContain("new.txt")
  516. },
  517. })
  518. })
  519. test("restore function", async () => {
  520. await using tmp = await bootstrap()
  521. await Instance.provide({
  522. directory: tmp.path,
  523. fn: async () => {
  524. const before = await Snapshot.track()
  525. expect(before).toBeTruthy()
  526. // Make changes
  527. await $`rm ${tmp.path}/a.txt`.quiet()
  528. await Bun.write(`${tmp.path}/new.txt`, "new content")
  529. await Bun.write(`${tmp.path}/b.txt`, "modified")
  530. // Restore to original state
  531. await Snapshot.restore(before!)
  532. expect(await Bun.file(`${tmp.path}/a.txt`).exists()).toBe(true)
  533. expect(await Bun.file(`${tmp.path}/a.txt`).text()).toBe(tmp.extra.aContent)
  534. expect(await Bun.file(`${tmp.path}/new.txt`).exists()).toBe(true) // New files should remain
  535. expect(await Bun.file(`${tmp.path}/b.txt`).text()).toBe(tmp.extra.bContent)
  536. },
  537. })
  538. })
  539. test("revert should not delete files that existed but were deleted in snapshot", async () => {
  540. await using tmp = await bootstrap()
  541. await Instance.provide({
  542. directory: tmp.path,
  543. fn: async () => {
  544. const snapshot1 = await Snapshot.track()
  545. expect(snapshot1).toBeTruthy()
  546. await $`rm ${tmp.path}/a.txt`.quiet()
  547. const snapshot2 = await Snapshot.track()
  548. expect(snapshot2).toBeTruthy()
  549. await Bun.write(`${tmp.path}/a.txt`, "recreated content")
  550. const patch = await Snapshot.patch(snapshot2!)
  551. expect(patch.files).toContain(`${tmp.path}/a.txt`)
  552. await Snapshot.revert([patch])
  553. expect(await Bun.file(`${tmp.path}/a.txt`).exists()).toBe(false)
  554. },
  555. })
  556. })
  557. test("revert preserves file that existed in snapshot when deleted then recreated", async () => {
  558. await using tmp = await bootstrap()
  559. await Instance.provide({
  560. directory: tmp.path,
  561. fn: async () => {
  562. await Bun.write(`${tmp.path}/existing.txt`, "original content")
  563. const snapshot = await Snapshot.track()
  564. expect(snapshot).toBeTruthy()
  565. await $`rm ${tmp.path}/existing.txt`.quiet()
  566. await Bun.write(`${tmp.path}/existing.txt`, "recreated")
  567. await Bun.write(`${tmp.path}/newfile.txt`, "new")
  568. const patch = await Snapshot.patch(snapshot!)
  569. expect(patch.files).toContain(`${tmp.path}/existing.txt`)
  570. expect(patch.files).toContain(`${tmp.path}/newfile.txt`)
  571. await Snapshot.revert([patch])
  572. expect(await Bun.file(`${tmp.path}/newfile.txt`).exists()).toBe(false)
  573. expect(await Bun.file(`${tmp.path}/existing.txt`).exists()).toBe(true)
  574. expect(await Bun.file(`${tmp.path}/existing.txt`).text()).toBe("original content")
  575. },
  576. })
  577. })
  578. test("diffFull with new file additions", async () => {
  579. await using tmp = await bootstrap()
  580. await Instance.provide({
  581. directory: tmp.path,
  582. fn: async () => {
  583. const before = await Snapshot.track()
  584. expect(before).toBeTruthy()
  585. await Bun.write(`${tmp.path}/new.txt`, "new content")
  586. const after = await Snapshot.track()
  587. expect(after).toBeTruthy()
  588. const diffs = await Snapshot.diffFull(before!, after!)
  589. expect(diffs.length).toBe(1)
  590. const newFileDiff = diffs[0]
  591. expect(newFileDiff.file).toBe("new.txt")
  592. expect(newFileDiff.before).toBe("")
  593. expect(newFileDiff.after).toBe("new content")
  594. expect(newFileDiff.additions).toBe(1)
  595. expect(newFileDiff.deletions).toBe(0)
  596. },
  597. })
  598. })
  599. test("diffFull with file modifications", async () => {
  600. await using tmp = await bootstrap()
  601. await Instance.provide({
  602. directory: tmp.path,
  603. fn: async () => {
  604. const before = await Snapshot.track()
  605. expect(before).toBeTruthy()
  606. await Bun.write(`${tmp.path}/b.txt`, "modified content")
  607. const after = await Snapshot.track()
  608. expect(after).toBeTruthy()
  609. const diffs = await Snapshot.diffFull(before!, after!)
  610. expect(diffs.length).toBe(1)
  611. const modifiedFileDiff = diffs[0]
  612. expect(modifiedFileDiff.file).toBe("b.txt")
  613. expect(modifiedFileDiff.before).toBe(tmp.extra.bContent)
  614. expect(modifiedFileDiff.after).toBe("modified content")
  615. expect(modifiedFileDiff.additions).toBeGreaterThan(0)
  616. expect(modifiedFileDiff.deletions).toBeGreaterThan(0)
  617. },
  618. })
  619. })
  620. test("diffFull with file deletions", async () => {
  621. await using tmp = await bootstrap()
  622. await Instance.provide({
  623. directory: tmp.path,
  624. fn: async () => {
  625. const before = await Snapshot.track()
  626. expect(before).toBeTruthy()
  627. await $`rm ${tmp.path}/a.txt`.quiet()
  628. const after = await Snapshot.track()
  629. expect(after).toBeTruthy()
  630. const diffs = await Snapshot.diffFull(before!, after!)
  631. expect(diffs.length).toBe(1)
  632. const removedFileDiff = diffs[0]
  633. expect(removedFileDiff.file).toBe("a.txt")
  634. expect(removedFileDiff.before).toBe(tmp.extra.aContent)
  635. expect(removedFileDiff.after).toBe("")
  636. expect(removedFileDiff.additions).toBe(0)
  637. expect(removedFileDiff.deletions).toBe(1)
  638. },
  639. })
  640. })
  641. test("diffFull with multiple line additions", async () => {
  642. await using tmp = await bootstrap()
  643. await Instance.provide({
  644. directory: tmp.path,
  645. fn: async () => {
  646. const before = await Snapshot.track()
  647. expect(before).toBeTruthy()
  648. await Bun.write(`${tmp.path}/multi.txt`, "line1\nline2\nline3")
  649. const after = await Snapshot.track()
  650. expect(after).toBeTruthy()
  651. const diffs = await Snapshot.diffFull(before!, after!)
  652. expect(diffs.length).toBe(1)
  653. const multiDiff = diffs[0]
  654. expect(multiDiff.file).toBe("multi.txt")
  655. expect(multiDiff.before).toBe("")
  656. expect(multiDiff.after).toBe("line1\nline2\nline3")
  657. expect(multiDiff.additions).toBe(3)
  658. expect(multiDiff.deletions).toBe(0)
  659. },
  660. })
  661. })
  662. test("diffFull with addition and deletion", async () => {
  663. await using tmp = await bootstrap()
  664. await Instance.provide({
  665. directory: tmp.path,
  666. fn: async () => {
  667. const before = await Snapshot.track()
  668. expect(before).toBeTruthy()
  669. await Bun.write(`${tmp.path}/added.txt`, "added content")
  670. await $`rm ${tmp.path}/a.txt`.quiet()
  671. const after = await Snapshot.track()
  672. expect(after).toBeTruthy()
  673. const diffs = await Snapshot.diffFull(before!, after!)
  674. expect(diffs.length).toBe(2)
  675. const addedFileDiff = diffs.find((d) => d.file === "added.txt")
  676. expect(addedFileDiff).toBeDefined()
  677. expect(addedFileDiff!.before).toBe("")
  678. expect(addedFileDiff!.after).toBe("added content")
  679. expect(addedFileDiff!.additions).toBe(1)
  680. expect(addedFileDiff!.deletions).toBe(0)
  681. const removedFileDiff = diffs.find((d) => d.file === "a.txt")
  682. expect(removedFileDiff).toBeDefined()
  683. expect(removedFileDiff!.before).toBe(tmp.extra.aContent)
  684. expect(removedFileDiff!.after).toBe("")
  685. expect(removedFileDiff!.additions).toBe(0)
  686. expect(removedFileDiff!.deletions).toBe(1)
  687. },
  688. })
  689. })
  690. test("diffFull with multiple additions and deletions", async () => {
  691. await using tmp = await bootstrap()
  692. await Instance.provide({
  693. directory: tmp.path,
  694. fn: async () => {
  695. const before = await Snapshot.track()
  696. expect(before).toBeTruthy()
  697. await Bun.write(`${tmp.path}/multi1.txt`, "line1\nline2\nline3")
  698. await Bun.write(`${tmp.path}/multi2.txt`, "single line")
  699. await $`rm ${tmp.path}/a.txt`.quiet()
  700. await $`rm ${tmp.path}/b.txt`.quiet()
  701. const after = await Snapshot.track()
  702. expect(after).toBeTruthy()
  703. const diffs = await Snapshot.diffFull(before!, after!)
  704. expect(diffs.length).toBe(4)
  705. const multi1Diff = diffs.find((d) => d.file === "multi1.txt")
  706. expect(multi1Diff).toBeDefined()
  707. expect(multi1Diff!.additions).toBe(3)
  708. expect(multi1Diff!.deletions).toBe(0)
  709. const multi2Diff = diffs.find((d) => d.file === "multi2.txt")
  710. expect(multi2Diff).toBeDefined()
  711. expect(multi2Diff!.additions).toBe(1)
  712. expect(multi2Diff!.deletions).toBe(0)
  713. const removedADiff = diffs.find((d) => d.file === "a.txt")
  714. expect(removedADiff).toBeDefined()
  715. expect(removedADiff!.additions).toBe(0)
  716. expect(removedADiff!.deletions).toBe(1)
  717. const removedBDiff = diffs.find((d) => d.file === "b.txt")
  718. expect(removedBDiff).toBeDefined()
  719. expect(removedBDiff!.additions).toBe(0)
  720. expect(removedBDiff!.deletions).toBe(1)
  721. },
  722. })
  723. })
  724. test("diffFull with no changes", async () => {
  725. await using tmp = await bootstrap()
  726. await Instance.provide({
  727. directory: tmp.path,
  728. fn: async () => {
  729. const before = await Snapshot.track()
  730. expect(before).toBeTruthy()
  731. const after = await Snapshot.track()
  732. expect(after).toBeTruthy()
  733. const diffs = await Snapshot.diffFull(before!, after!)
  734. expect(diffs.length).toBe(0)
  735. },
  736. })
  737. })
  738. test("diffFull with binary file changes", async () => {
  739. await using tmp = await bootstrap()
  740. await Instance.provide({
  741. directory: tmp.path,
  742. fn: async () => {
  743. const before = await Snapshot.track()
  744. expect(before).toBeTruthy()
  745. await Bun.write(`${tmp.path}/binary.bin`, new Uint8Array([0x00, 0x01, 0x02, 0x03]))
  746. const after = await Snapshot.track()
  747. expect(after).toBeTruthy()
  748. const diffs = await Snapshot.diffFull(before!, after!)
  749. expect(diffs.length).toBe(1)
  750. const binaryDiff = diffs[0]
  751. expect(binaryDiff.file).toBe("binary.bin")
  752. expect(binaryDiff.before).toBe("")
  753. },
  754. })
  755. })
  756. test("diffFull with whitespace changes", async () => {
  757. await using tmp = await bootstrap()
  758. await Instance.provide({
  759. directory: tmp.path,
  760. fn: async () => {
  761. await Bun.write(`${tmp.path}/whitespace.txt`, "line1\nline2")
  762. const before = await Snapshot.track()
  763. expect(before).toBeTruthy()
  764. await Bun.write(`${tmp.path}/whitespace.txt`, "line1\n\nline2\n")
  765. const after = await Snapshot.track()
  766. expect(after).toBeTruthy()
  767. const diffs = await Snapshot.diffFull(before!, after!)
  768. expect(diffs.length).toBe(1)
  769. const whitespaceDiff = diffs[0]
  770. expect(whitespaceDiff.file).toBe("whitespace.txt")
  771. expect(whitespaceDiff.additions).toBeGreaterThan(0)
  772. },
  773. })
  774. })