snapshot.test.ts 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994
  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("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 with new file additions", 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 Bun.write(`${tmp.path}/new.txt`, "new content")
  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 newFileDiff = diffs[0]
  633. expect(newFileDiff.file).toBe("new.txt")
  634. expect(newFileDiff.before).toBe("")
  635. expect(newFileDiff.after).toBe("new content")
  636. expect(newFileDiff.additions).toBe(1)
  637. expect(newFileDiff.deletions).toBe(0)
  638. },
  639. })
  640. })
  641. test("diffFull with file modifications", 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}/b.txt`, "modified content")
  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 modifiedFileDiff = diffs[0]
  654. expect(modifiedFileDiff.file).toBe("b.txt")
  655. expect(modifiedFileDiff.before).toBe(tmp.extra.bContent)
  656. expect(modifiedFileDiff.after).toBe("modified content")
  657. expect(modifiedFileDiff.additions).toBeGreaterThan(0)
  658. expect(modifiedFileDiff.deletions).toBeGreaterThan(0)
  659. },
  660. })
  661. })
  662. test("diffFull with file deletions", 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 $`rm ${tmp.path}/a.txt`.quiet()
  670. const after = await Snapshot.track()
  671. expect(after).toBeTruthy()
  672. const diffs = await Snapshot.diffFull(before!, after!)
  673. expect(diffs.length).toBe(1)
  674. const removedFileDiff = diffs[0]
  675. expect(removedFileDiff.file).toBe("a.txt")
  676. expect(removedFileDiff.before).toBe(tmp.extra.aContent)
  677. expect(removedFileDiff.after).toBe("")
  678. expect(removedFileDiff.additions).toBe(0)
  679. expect(removedFileDiff.deletions).toBe(1)
  680. },
  681. })
  682. })
  683. test("diffFull with multiple line additions", async () => {
  684. await using tmp = await bootstrap()
  685. await Instance.provide({
  686. directory: tmp.path,
  687. fn: async () => {
  688. const before = await Snapshot.track()
  689. expect(before).toBeTruthy()
  690. await Bun.write(`${tmp.path}/multi.txt`, "line1\nline2\nline3")
  691. const after = await Snapshot.track()
  692. expect(after).toBeTruthy()
  693. const diffs = await Snapshot.diffFull(before!, after!)
  694. expect(diffs.length).toBe(1)
  695. const multiDiff = diffs[0]
  696. expect(multiDiff.file).toBe("multi.txt")
  697. expect(multiDiff.before).toBe("")
  698. expect(multiDiff.after).toBe("line1\nline2\nline3")
  699. expect(multiDiff.additions).toBe(3)
  700. expect(multiDiff.deletions).toBe(0)
  701. },
  702. })
  703. })
  704. test("diffFull with addition and deletion", async () => {
  705. await using tmp = await bootstrap()
  706. await Instance.provide({
  707. directory: tmp.path,
  708. fn: async () => {
  709. const before = await Snapshot.track()
  710. expect(before).toBeTruthy()
  711. await Bun.write(`${tmp.path}/added.txt`, "added content")
  712. await $`rm ${tmp.path}/a.txt`.quiet()
  713. const after = await Snapshot.track()
  714. expect(after).toBeTruthy()
  715. const diffs = await Snapshot.diffFull(before!, after!)
  716. expect(diffs.length).toBe(2)
  717. const addedFileDiff = diffs.find((d) => d.file === "added.txt")
  718. expect(addedFileDiff).toBeDefined()
  719. expect(addedFileDiff!.before).toBe("")
  720. expect(addedFileDiff!.after).toBe("added content")
  721. expect(addedFileDiff!.additions).toBe(1)
  722. expect(addedFileDiff!.deletions).toBe(0)
  723. const removedFileDiff = diffs.find((d) => d.file === "a.txt")
  724. expect(removedFileDiff).toBeDefined()
  725. expect(removedFileDiff!.before).toBe(tmp.extra.aContent)
  726. expect(removedFileDiff!.after).toBe("")
  727. expect(removedFileDiff!.additions).toBe(0)
  728. expect(removedFileDiff!.deletions).toBe(1)
  729. },
  730. })
  731. })
  732. test("diffFull with multiple additions and deletions", async () => {
  733. await using tmp = await bootstrap()
  734. await Instance.provide({
  735. directory: tmp.path,
  736. fn: async () => {
  737. const before = await Snapshot.track()
  738. expect(before).toBeTruthy()
  739. await Bun.write(`${tmp.path}/multi1.txt`, "line1\nline2\nline3")
  740. await Bun.write(`${tmp.path}/multi2.txt`, "single line")
  741. await $`rm ${tmp.path}/a.txt`.quiet()
  742. await $`rm ${tmp.path}/b.txt`.quiet()
  743. const after = await Snapshot.track()
  744. expect(after).toBeTruthy()
  745. const diffs = await Snapshot.diffFull(before!, after!)
  746. expect(diffs.length).toBe(4)
  747. const multi1Diff = diffs.find((d) => d.file === "multi1.txt")
  748. expect(multi1Diff).toBeDefined()
  749. expect(multi1Diff!.additions).toBe(3)
  750. expect(multi1Diff!.deletions).toBe(0)
  751. const multi2Diff = diffs.find((d) => d.file === "multi2.txt")
  752. expect(multi2Diff).toBeDefined()
  753. expect(multi2Diff!.additions).toBe(1)
  754. expect(multi2Diff!.deletions).toBe(0)
  755. const removedADiff = diffs.find((d) => d.file === "a.txt")
  756. expect(removedADiff).toBeDefined()
  757. expect(removedADiff!.additions).toBe(0)
  758. expect(removedADiff!.deletions).toBe(1)
  759. const removedBDiff = diffs.find((d) => d.file === "b.txt")
  760. expect(removedBDiff).toBeDefined()
  761. expect(removedBDiff!.additions).toBe(0)
  762. expect(removedBDiff!.deletions).toBe(1)
  763. },
  764. })
  765. })
  766. test("diffFull with no changes", async () => {
  767. await using tmp = await bootstrap()
  768. await Instance.provide({
  769. directory: tmp.path,
  770. fn: async () => {
  771. const before = await Snapshot.track()
  772. expect(before).toBeTruthy()
  773. const after = await Snapshot.track()
  774. expect(after).toBeTruthy()
  775. const diffs = await Snapshot.diffFull(before!, after!)
  776. expect(diffs.length).toBe(0)
  777. },
  778. })
  779. })
  780. test("diffFull with binary file changes", async () => {
  781. await using tmp = await bootstrap()
  782. await Instance.provide({
  783. directory: tmp.path,
  784. fn: async () => {
  785. const before = await Snapshot.track()
  786. expect(before).toBeTruthy()
  787. await Bun.write(`${tmp.path}/binary.bin`, new Uint8Array([0x00, 0x01, 0x02, 0x03]))
  788. const after = await Snapshot.track()
  789. expect(after).toBeTruthy()
  790. const diffs = await Snapshot.diffFull(before!, after!)
  791. expect(diffs.length).toBe(1)
  792. const binaryDiff = diffs[0]
  793. expect(binaryDiff.file).toBe("binary.bin")
  794. expect(binaryDiff.before).toBe("")
  795. },
  796. })
  797. })
  798. test("diffFull with whitespace changes", async () => {
  799. await using tmp = await bootstrap()
  800. await Instance.provide({
  801. directory: tmp.path,
  802. fn: async () => {
  803. await Bun.write(`${tmp.path}/whitespace.txt`, "line1\nline2")
  804. const before = await Snapshot.track()
  805. expect(before).toBeTruthy()
  806. await Bun.write(`${tmp.path}/whitespace.txt`, "line1\n\nline2\n")
  807. const after = await Snapshot.track()
  808. expect(after).toBeTruthy()
  809. const diffs = await Snapshot.diffFull(before!, after!)
  810. expect(diffs.length).toBe(1)
  811. const whitespaceDiff = diffs[0]
  812. expect(whitespaceDiff.file).toBe("whitespace.txt")
  813. expect(whitespaceDiff.additions).toBeGreaterThan(0)
  814. },
  815. })
  816. })