rird 2.1.231 → 2.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (381) hide show
  1. package/AGENTS.md +86 -0
  2. package/COMPLETED_TEST_SUITE.txt +280 -0
  3. package/Dockerfile +18 -0
  4. package/README.md +397 -6
  5. package/RIRD_ERROR_HANDLING_SUMMARY.md +307 -0
  6. package/TESTING.md +512 -0
  7. package/TEST_IMPLEMENTATION_REPORT.md +463 -0
  8. package/TEST_SUITE.md +307 -0
  9. package/TEST_SUMMARY.txt +380 -0
  10. package/bin/rird-perf.js +37 -0
  11. package/bin/rird.js +43 -8
  12. package/bunfig.toml +4 -0
  13. package/create-wrapper.ps1 +51 -0
  14. package/docs/ARCHITECTURE.md +768 -0
  15. package/docs/CLI_REFERENCE.md +681 -0
  16. package/docs/DOCUMENTATION_MANIFEST.md +392 -0
  17. package/docs/INDEX.md +295 -0
  18. package/docs/PRODUCTION_SETUP.md +633 -0
  19. package/docs/TROUBLESHOOTING.md +914 -0
  20. package/facebook_ads_library.png +0 -0
  21. package/nul +0 -0
  22. package/nul`nif +0 -0
  23. package/package.json +104 -15
  24. package/parsers-config.ts +239 -0
  25. package/rird-1.0.199.tgz +0 -0
  26. package/rird-1.0.205.tgz +0 -0
  27. package/script/build-windows.ts +56 -0
  28. package/script/build.ts +165 -0
  29. package/{postinstall.mjs → script/postinstall.mjs} +47 -68
  30. package/script/publish-registries.ts +187 -0
  31. package/script/publish.ts +85 -0
  32. package/script/schema.ts +47 -0
  33. package/src/acp/README.md +164 -0
  34. package/src/acp/agent.ts +1063 -0
  35. package/src/acp/session.ts +101 -0
  36. package/src/acp/types.ts +22 -0
  37. package/src/agent/agent.ts +367 -0
  38. package/src/agent/generate.txt +75 -0
  39. package/src/agent/prompt/compaction.txt +12 -0
  40. package/src/agent/prompt/explore.txt +18 -0
  41. package/src/agent/prompt/summary.txt +10 -0
  42. package/src/agent/prompt/title.txt +36 -0
  43. package/src/auth/index.ts +70 -0
  44. package/src/bun/index.ts +114 -0
  45. package/src/bus/bus-event.ts +43 -0
  46. package/src/bus/global.ts +10 -0
  47. package/src/bus/index.ts +105 -0
  48. package/src/cli/bootstrap.ts +17 -0
  49. package/src/cli/cmd/acp.ts +104 -0
  50. package/src/cli/cmd/activate.ts +50 -0
  51. package/src/cli/cmd/agent.ts +256 -0
  52. package/src/cli/cmd/auth.ts +412 -0
  53. package/src/cli/cmd/cmd.ts +7 -0
  54. package/src/cli/cmd/debug/config.ts +15 -0
  55. package/src/cli/cmd/debug/file.ts +91 -0
  56. package/src/cli/cmd/debug/index.ts +43 -0
  57. package/src/cli/cmd/debug/lsp.ts +48 -0
  58. package/src/cli/cmd/debug/ripgrep.ts +83 -0
  59. package/src/cli/cmd/debug/scrap.ts +15 -0
  60. package/src/cli/cmd/debug/skill.ts +15 -0
  61. package/src/cli/cmd/debug/snapshot.ts +48 -0
  62. package/src/cli/cmd/export.ts +88 -0
  63. package/src/cli/cmd/generate.ts +38 -0
  64. package/src/cli/cmd/github.ts +1400 -0
  65. package/src/cli/cmd/import.ts +98 -0
  66. package/src/cli/cmd/mcp.ts +654 -0
  67. package/src/cli/cmd/models.ts +68 -0
  68. package/src/cli/cmd/pr.ts +112 -0
  69. package/src/cli/cmd/run.ts +434 -0
  70. package/src/cli/cmd/serve.ts +31 -0
  71. package/src/cli/cmd/session.ts +106 -0
  72. package/src/cli/cmd/stats.ts +298 -0
  73. package/src/cli/cmd/tui/app.tsx +694 -0
  74. package/src/cli/cmd/tui/attach.ts +30 -0
  75. package/src/cli/cmd/tui/component/border.tsx +21 -0
  76. package/src/cli/cmd/tui/component/dialog-agent.tsx +31 -0
  77. package/src/cli/cmd/tui/component/dialog-command.tsx +124 -0
  78. package/src/cli/cmd/tui/component/dialog-mcp.tsx +86 -0
  79. package/src/cli/cmd/tui/component/dialog-model.tsx +236 -0
  80. package/src/cli/cmd/tui/component/dialog-provider.tsx +240 -0
  81. package/src/cli/cmd/tui/component/dialog-session-list.tsx +102 -0
  82. package/src/cli/cmd/tui/component/dialog-session-rename.tsx +31 -0
  83. package/src/cli/cmd/tui/component/dialog-stash.tsx +86 -0
  84. package/src/cli/cmd/tui/component/dialog-status.tsx +162 -0
  85. package/src/cli/cmd/tui/component/dialog-tag.tsx +44 -0
  86. package/src/cli/cmd/tui/component/dialog-theme-list.tsx +50 -0
  87. package/src/cli/cmd/tui/component/did-you-know.tsx +85 -0
  88. package/src/cli/cmd/tui/component/logo.tsx +48 -0
  89. package/src/cli/cmd/tui/component/prompt/autocomplete.tsx +574 -0
  90. package/src/cli/cmd/tui/component/prompt/history.tsx +108 -0
  91. package/src/cli/cmd/tui/component/prompt/index.tsx +1087 -0
  92. package/src/cli/cmd/tui/component/prompt/stash.tsx +101 -0
  93. package/src/cli/cmd/tui/component/tips.ts +27 -0
  94. package/src/cli/cmd/tui/component/todo-item.tsx +32 -0
  95. package/src/cli/cmd/tui/context/args.tsx +14 -0
  96. package/src/cli/cmd/tui/context/directory.ts +13 -0
  97. package/src/cli/cmd/tui/context/exit.tsx +23 -0
  98. package/src/cli/cmd/tui/context/helper.tsx +25 -0
  99. package/src/cli/cmd/tui/context/keybind.tsx +101 -0
  100. package/src/cli/cmd/tui/context/kv.tsx +49 -0
  101. package/src/cli/cmd/tui/context/local.tsx +345 -0
  102. package/src/cli/cmd/tui/context/prompt.tsx +18 -0
  103. package/src/cli/cmd/tui/context/route.tsx +46 -0
  104. package/src/cli/cmd/tui/context/sdk.tsx +74 -0
  105. package/src/cli/cmd/tui/context/sync.tsx +372 -0
  106. package/src/cli/cmd/tui/context/theme/aura.json +69 -0
  107. package/src/cli/cmd/tui/context/theme/ayu.json +80 -0
  108. package/src/cli/cmd/tui/context/theme/catppuccin-frappe.json +233 -0
  109. package/src/cli/cmd/tui/context/theme/catppuccin-macchiato.json +233 -0
  110. package/src/cli/cmd/tui/context/theme/catppuccin.json +112 -0
  111. package/src/cli/cmd/tui/context/theme/cobalt2.json +228 -0
  112. package/src/cli/cmd/tui/context/theme/cursor.json +249 -0
  113. package/src/cli/cmd/tui/context/theme/dracula.json +219 -0
  114. package/src/cli/cmd/tui/context/theme/everforest.json +241 -0
  115. package/src/cli/cmd/tui/context/theme/flexoki.json +237 -0
  116. package/src/cli/cmd/tui/context/theme/github.json +233 -0
  117. package/src/cli/cmd/tui/context/theme/gruvbox.json +95 -0
  118. package/src/cli/cmd/tui/context/theme/kanagawa.json +77 -0
  119. package/src/cli/cmd/tui/context/theme/lucent-orng.json +227 -0
  120. package/src/cli/cmd/tui/context/theme/material.json +235 -0
  121. package/src/cli/cmd/tui/context/theme/matrix.json +77 -0
  122. package/src/cli/cmd/tui/context/theme/mercury.json +252 -0
  123. package/src/cli/cmd/tui/context/theme/monokai.json +221 -0
  124. package/src/cli/cmd/tui/context/theme/nightowl.json +221 -0
  125. package/src/cli/cmd/tui/context/theme/nord.json +223 -0
  126. package/src/cli/cmd/tui/context/theme/one-dark.json +84 -0
  127. package/src/cli/cmd/tui/context/theme/orng.json +245 -0
  128. package/src/cli/cmd/tui/context/theme/palenight.json +222 -0
  129. package/src/cli/cmd/tui/context/theme/rird.json +245 -0
  130. package/src/cli/cmd/tui/context/theme/rosepine.json +234 -0
  131. package/src/cli/cmd/tui/context/theme/solarized.json +223 -0
  132. package/src/cli/cmd/tui/context/theme/synthwave84.json +226 -0
  133. package/src/cli/cmd/tui/context/theme/tokyonight.json +243 -0
  134. package/src/cli/cmd/tui/context/theme/vercel.json +245 -0
  135. package/src/cli/cmd/tui/context/theme/vesper.json +218 -0
  136. package/src/cli/cmd/tui/context/theme/zenburn.json +223 -0
  137. package/src/cli/cmd/tui/context/theme.tsx +1109 -0
  138. package/src/cli/cmd/tui/event.ts +40 -0
  139. package/src/cli/cmd/tui/hooks/use-safe-terminal-dimensions.ts +12 -0
  140. package/src/cli/cmd/tui/routes/home.tsx +138 -0
  141. package/src/cli/cmd/tui/routes/session/dialog-fork-from-timeline.tsx +64 -0
  142. package/src/cli/cmd/tui/routes/session/dialog-message.tsx +109 -0
  143. package/src/cli/cmd/tui/routes/session/dialog-subagent.tsx +26 -0
  144. package/src/cli/cmd/tui/routes/session/dialog-timeline.tsx +47 -0
  145. package/src/cli/cmd/tui/routes/session/footer.tsx +88 -0
  146. package/src/cli/cmd/tui/routes/session/header.tsx +125 -0
  147. package/src/cli/cmd/tui/routes/session/index.tsx +1876 -0
  148. package/src/cli/cmd/tui/routes/session/sidebar.tsx +320 -0
  149. package/src/cli/cmd/tui/spawn.ts +60 -0
  150. package/src/cli/cmd/tui/thread.ts +142 -0
  151. package/src/cli/cmd/tui/ui/dialog-alert.tsx +57 -0
  152. package/src/cli/cmd/tui/ui/dialog-confirm.tsx +83 -0
  153. package/src/cli/cmd/tui/ui/dialog-help.tsx +38 -0
  154. package/src/cli/cmd/tui/ui/dialog-prompt.tsx +77 -0
  155. package/src/cli/cmd/tui/ui/dialog-select.tsx +333 -0
  156. package/src/cli/cmd/tui/ui/dialog.tsx +171 -0
  157. package/src/cli/cmd/tui/ui/spinner.ts +368 -0
  158. package/src/cli/cmd/tui/ui/toast.tsx +100 -0
  159. package/src/cli/cmd/tui/util/clipboard.ts +127 -0
  160. package/src/cli/cmd/tui/util/editor.ts +32 -0
  161. package/src/cli/cmd/tui/util/terminal.ts +146 -0
  162. package/src/cli/cmd/tui/worker.ts +63 -0
  163. package/src/cli/cmd/uninstall.ts +344 -0
  164. package/src/cli/cmd/upgrade.ts +127 -0
  165. package/src/cli/cmd/web.ts +84 -0
  166. package/src/cli/error.ts +69 -0
  167. package/src/cli/ui.ts +101 -0
  168. package/src/cli/upgrade.ts +28 -0
  169. package/src/command/index.ts +80 -0
  170. package/src/command/template/initialize.txt +10 -0
  171. package/src/command/template/review.txt +97 -0
  172. package/src/config/config.ts +994 -0
  173. package/src/config/markdown.ts +41 -0
  174. package/src/env/index.ts +26 -0
  175. package/src/file/ignore.ts +83 -0
  176. package/src/file/index.ts +328 -0
  177. package/src/file/ripgrep.ts +393 -0
  178. package/src/file/time.ts +64 -0
  179. package/src/file/watcher.ts +103 -0
  180. package/src/flag/flag.ts +84 -0
  181. package/src/format/formatter.ts +315 -0
  182. package/src/format/index.ts +137 -0
  183. package/src/global/index.ts +101 -0
  184. package/src/id/id.ts +73 -0
  185. package/src/ide/index.ts +76 -0
  186. package/src/index.ts +297 -0
  187. package/src/index.ts.backup +271 -0
  188. package/src/installation/index.ts +258 -0
  189. package/src/lib/IMPLEMENTATION_NOTES.md +345 -0
  190. package/src/lib/error-handler.ts +225 -0
  191. package/src/lib/error-testing-guide.md +258 -0
  192. package/src/lib/errors.ts +285 -0
  193. package/src/lib/performance.ts +70 -0
  194. package/src/lib/telemetry.ts +282 -0
  195. package/src/lsp/client.ts +229 -0
  196. package/src/lsp/index.ts +485 -0
  197. package/src/lsp/language.ts +116 -0
  198. package/src/lsp/server.ts +1895 -0
  199. package/src/mcp/auth.ts +135 -0
  200. package/src/mcp/index.ts +1117 -0
  201. package/src/mcp/intent-analyzer.ts +376 -0
  202. package/src/mcp/oauth-callback.ts +200 -0
  203. package/src/mcp/oauth-provider.ts +154 -0
  204. package/src/patch/index.ts +632 -0
  205. package/src/permission/index.ts +199 -0
  206. package/src/plugin/index.ts +91 -0
  207. package/src/project/bootstrap.ts +33 -0
  208. package/src/project/instance.ts +78 -0
  209. package/src/project/project.ts +236 -0
  210. package/src/project/state.ts +65 -0
  211. package/src/project/vcs.ts +76 -0
  212. package/src/provider/auth.ts +143 -0
  213. package/src/provider/models-macro.ts +55 -0
  214. package/src/provider/models.ts +161 -0
  215. package/src/provider/provider.ts +1109 -0
  216. package/src/provider/sdk/openai-compatible/src/README.md +5 -0
  217. package/src/provider/sdk/openai-compatible/src/index.ts +2 -0
  218. package/src/provider/sdk/openai-compatible/src/openai-compatible-provider.ts +100 -0
  219. package/src/provider/sdk/openai-compatible/src/responses/convert-to-openai-responses-input.ts +303 -0
  220. package/src/provider/sdk/openai-compatible/src/responses/map-openai-responses-finish-reason.ts +22 -0
  221. package/src/provider/sdk/openai-compatible/src/responses/openai-config.ts +18 -0
  222. package/src/provider/sdk/openai-compatible/src/responses/openai-error.ts +22 -0
  223. package/src/provider/sdk/openai-compatible/src/responses/openai-responses-api-types.ts +207 -0
  224. package/src/provider/sdk/openai-compatible/src/responses/openai-responses-language-model.ts +1713 -0
  225. package/src/provider/sdk/openai-compatible/src/responses/openai-responses-prepare-tools.ts +177 -0
  226. package/src/provider/sdk/openai-compatible/src/responses/openai-responses-settings.ts +1 -0
  227. package/src/provider/sdk/openai-compatible/src/responses/tool/code-interpreter.ts +88 -0
  228. package/src/provider/sdk/openai-compatible/src/responses/tool/file-search.ts +128 -0
  229. package/src/provider/sdk/openai-compatible/src/responses/tool/image-generation.ts +115 -0
  230. package/src/provider/sdk/openai-compatible/src/responses/tool/local-shell.ts +65 -0
  231. package/src/provider/sdk/openai-compatible/src/responses/tool/web-search-preview.ts +104 -0
  232. package/src/provider/sdk/openai-compatible/src/responses/tool/web-search.ts +103 -0
  233. package/src/provider/transform.ts +455 -0
  234. package/src/pty/index.ts +231 -0
  235. package/src/security/guardrails.test.ts +341 -0
  236. package/src/security/guardrails.ts +570 -0
  237. package/src/security/index.ts +19 -0
  238. package/src/server/error.ts +36 -0
  239. package/src/server/project.ts +79 -0
  240. package/src/server/server.ts +2641 -0
  241. package/src/server/tui.ts +71 -0
  242. package/src/session/compaction.ts +228 -0
  243. package/src/session/index.ts +464 -0
  244. package/src/session/llm.ts +201 -0
  245. package/src/session/message-v2.ts +695 -0
  246. package/src/session/message.ts +189 -0
  247. package/src/session/processor.ts +409 -0
  248. package/src/session/prompt/act-switch.txt +5 -0
  249. package/src/session/prompt/anthropic-20250930.txt +166 -0
  250. package/src/session/prompt/anthropic.txt +63 -0
  251. package/src/session/prompt/anthropic_spoof.txt +1 -0
  252. package/src/session/prompt/beast.txt +76 -0
  253. package/src/session/prompt/codex.txt +304 -0
  254. package/src/session/prompt/copilot-gpt-5.txt +137 -0
  255. package/src/session/prompt/gemini.txt +62 -0
  256. package/src/session/prompt/max-steps.txt +16 -0
  257. package/src/session/prompt/plan-reminder-anthropic.txt +35 -0
  258. package/src/session/prompt/plan.txt +24 -0
  259. package/src/session/prompt/polaris.txt +88 -0
  260. package/src/session/prompt/qwen.txt +59 -0
  261. package/src/session/prompt.ts +1552 -0
  262. package/src/session/retry.ts +86 -0
  263. package/src/session/revert.ts +108 -0
  264. package/src/session/sensitive-filter.test.ts +327 -0
  265. package/src/session/sensitive-filter.ts +466 -0
  266. package/src/session/status.ts +76 -0
  267. package/src/session/summary.ts +209 -0
  268. package/src/session/system.ts +122 -0
  269. package/src/session/todo.ts +37 -0
  270. package/src/share/share-next.ts +222 -0
  271. package/src/share/share.ts +87 -0
  272. package/src/shell/shell.ts +67 -0
  273. package/src/skill/index.ts +1 -0
  274. package/src/skill/skill.ts +83 -0
  275. package/src/snapshot/index.ts +197 -0
  276. package/src/storage/storage.ts +226 -0
  277. package/src/tests/agent.test.ts +308 -0
  278. package/src/tests/build-guards.test.ts +267 -0
  279. package/src/tests/config.test.ts +664 -0
  280. package/src/tests/tool-registry.test.ts +589 -0
  281. package/src/tool/bash.ts +314 -0
  282. package/src/tool/bash.txt +158 -0
  283. package/src/tool/batch.ts +175 -0
  284. package/src/tool/batch.txt +24 -0
  285. package/src/tool/codesearch.ts +184 -0
  286. package/src/tool/codesearch.txt +12 -0
  287. package/src/tool/edit.ts +675 -0
  288. package/src/tool/edit.txt +10 -0
  289. package/src/tool/glob.ts +65 -0
  290. package/src/tool/glob.txt +6 -0
  291. package/src/tool/grep.ts +121 -0
  292. package/src/tool/grep.txt +8 -0
  293. package/src/tool/invalid.ts +17 -0
  294. package/src/tool/ls.ts +110 -0
  295. package/src/tool/ls.txt +1 -0
  296. package/src/tool/lsp-diagnostics.ts +26 -0
  297. package/src/tool/lsp-diagnostics.txt +1 -0
  298. package/src/tool/lsp-hover.ts +31 -0
  299. package/src/tool/lsp-hover.txt +1 -0
  300. package/src/tool/lsp.ts +87 -0
  301. package/src/tool/lsp.txt +19 -0
  302. package/src/tool/multiedit.ts +46 -0
  303. package/src/tool/multiedit.txt +41 -0
  304. package/src/tool/patch.ts +233 -0
  305. package/src/tool/patch.txt +1 -0
  306. package/src/tool/read.ts +219 -0
  307. package/src/tool/read.txt +12 -0
  308. package/src/tool/registry.ts +162 -0
  309. package/src/tool/skill.ts +100 -0
  310. package/src/tool/task.ts +136 -0
  311. package/src/tool/task.txt +51 -0
  312. package/src/tool/todo.ts +39 -0
  313. package/src/tool/todoread.txt +14 -0
  314. package/src/tool/todowrite.txt +167 -0
  315. package/src/tool/tool.ts +71 -0
  316. package/src/tool/webfetch.ts +198 -0
  317. package/src/tool/webfetch.txt +13 -0
  318. package/src/tool/websearch.ts +268 -0
  319. package/src/tool/websearch.txt +13 -0
  320. package/src/tool/write.ts +110 -0
  321. package/src/tool/write.txt +8 -0
  322. package/src/util/archive.ts +16 -0
  323. package/src/util/color.ts +19 -0
  324. package/src/util/context.ts +25 -0
  325. package/src/util/defer.ts +12 -0
  326. package/src/util/eventloop.ts +20 -0
  327. package/src/util/filesystem.ts +83 -0
  328. package/src/util/fn.ts +11 -0
  329. package/src/util/iife.ts +3 -0
  330. package/src/util/keybind.ts +102 -0
  331. package/src/util/lazy.ts +11 -0
  332. package/src/util/license.ts +362 -0
  333. package/src/util/locale.ts +81 -0
  334. package/src/util/lock.ts +98 -0
  335. package/src/util/log.ts +180 -0
  336. package/src/util/queue.ts +32 -0
  337. package/src/util/rpc.ts +42 -0
  338. package/src/util/scrap.ts +10 -0
  339. package/src/util/signal.ts +12 -0
  340. package/src/util/timeout.ts +14 -0
  341. package/src/util/token.ts +7 -0
  342. package/src/util/wildcard.ts +54 -0
  343. package/sst-env.d.ts +9 -0
  344. package/test/agent/agent.test.ts +146 -0
  345. package/test/bun.test.ts +53 -0
  346. package/test/cli/cmd/acp.test.ts +144 -0
  347. package/test/cli/cmd/run.test.ts +250 -0
  348. package/test/cli/github-remote.test.ts +80 -0
  349. package/test/config/agent-color.test.ts +66 -0
  350. package/test/config/config.test.ts +536 -0
  351. package/test/config/markdown.test.ts +89 -0
  352. package/test/file/ignore.test.ts +10 -0
  353. package/test/fixture/fixture.ts +37 -0
  354. package/test/fixture/lsp/fake-lsp-server.js +77 -0
  355. package/test/helpers.ts +172 -0
  356. package/test/ide/ide.test.ts +82 -0
  357. package/test/installation/installation.test.ts +143 -0
  358. package/test/keybind.test.ts +421 -0
  359. package/test/lsp/client.test.ts +95 -0
  360. package/test/mcp/headers.test.ts +153 -0
  361. package/test/patch/patch.test.ts +348 -0
  362. package/test/preload.ts +57 -0
  363. package/test/project/project.test.ts +74 -0
  364. package/test/provider/provider.test.ts +74 -0
  365. package/test/provider/transform.test.ts +411 -0
  366. package/test/session/retry.test.ts +111 -0
  367. package/test/session/session.test.ts +71 -0
  368. package/test/skill/skill.test.ts +131 -0
  369. package/test/snapshot/snapshot.test.ts +940 -0
  370. package/test/tool/__snapshots__/tool.test.ts.snap +9 -0
  371. package/test/tool/bash.test.ts +434 -0
  372. package/test/tool/grep.test.ts +108 -0
  373. package/test/tool/patch.test.ts +259 -0
  374. package/test/tool/read.test.ts +42 -0
  375. package/test/util/iife.test.ts +36 -0
  376. package/test/util/lazy.test.ts +50 -0
  377. package/test/util/license.test.ts +235 -0
  378. package/test/util/timeout.test.ts +21 -0
  379. package/test/util/wildcard.test.ts +55 -0
  380. package/tsconfig.json +16 -0
  381. package/update-versions.ps1 +65 -0
@@ -0,0 +1,940 @@
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
+ import path from "path"
7
+
8
+ async function bootstrap() {
9
+ return tmpdir({
10
+ git: true,
11
+ init: async (dir) => {
12
+ const unique = Math.random().toString(36).slice(2)
13
+ const aContent = `A${unique}`
14
+ const bContent = `B${unique}`
15
+ await Bun.write(`${dir}/a.txt`, aContent)
16
+ await Bun.write(`${dir}/b.txt`, bContent)
17
+ await $`git add .`.cwd(dir).quiet()
18
+ await $`git commit --no-gpg-sign -m init`.cwd(dir).quiet()
19
+ return {
20
+ aContent,
21
+ bContent,
22
+ }
23
+ },
24
+ })
25
+ }
26
+
27
+ test("tracks deleted files correctly", async () => {
28
+ await using tmp = await bootstrap()
29
+ await Instance.provide({
30
+ directory: tmp.path,
31
+ fn: async () => {
32
+ const before = await Snapshot.track()
33
+ expect(before).toBeTruthy()
34
+
35
+ await $`rm ${tmp.path}/a.txt`.quiet()
36
+
37
+ expect((await Snapshot.patch(before!)).files).toContain(path.join(tmp.path, "a.txt"))
38
+ },
39
+ })
40
+ })
41
+
42
+ test("revert should remove new files", async () => {
43
+ await using tmp = await bootstrap()
44
+ await Instance.provide({
45
+ directory: tmp.path,
46
+ fn: async () => {
47
+ const before = await Snapshot.track()
48
+ expect(before).toBeTruthy()
49
+
50
+ await Bun.write(`${tmp.path}/new.txt`, "NEW")
51
+
52
+ await Snapshot.revert([await Snapshot.patch(before!)])
53
+
54
+ expect(await Bun.file(`${tmp.path}/new.txt`).exists()).toBe(false)
55
+ },
56
+ })
57
+ })
58
+
59
+ test("revert in subdirectory", async () => {
60
+ await using tmp = await bootstrap()
61
+ await Instance.provide({
62
+ directory: tmp.path,
63
+ fn: async () => {
64
+ const before = await Snapshot.track()
65
+ expect(before).toBeTruthy()
66
+
67
+ await $`mkdir -p ${tmp.path}/sub`.quiet()
68
+ await Bun.write(`${tmp.path}/sub/file.txt`, "SUB")
69
+
70
+ await Snapshot.revert([await Snapshot.patch(before!)])
71
+
72
+ expect(await Bun.file(`${tmp.path}/sub/file.txt`).exists()).toBe(false)
73
+ // Note: revert currently only removes files, not directories
74
+ // The empty subdirectory will remain
75
+ },
76
+ })
77
+ })
78
+
79
+ test("multiple file operations", async () => {
80
+ await using tmp = await bootstrap()
81
+ await Instance.provide({
82
+ directory: tmp.path,
83
+ fn: async () => {
84
+ const before = await Snapshot.track()
85
+ expect(before).toBeTruthy()
86
+
87
+ await $`rm ${tmp.path}/a.txt`.quiet()
88
+ await Bun.write(`${tmp.path}/c.txt`, "C")
89
+ await $`mkdir -p ${tmp.path}/dir`.quiet()
90
+ await Bun.write(`${tmp.path}/dir/d.txt`, "D")
91
+ await Bun.write(`${tmp.path}/b.txt`, "MODIFIED")
92
+
93
+ await Snapshot.revert([await Snapshot.patch(before!)])
94
+
95
+ expect(await Bun.file(`${tmp.path}/a.txt`).text()).toBe(tmp.extra.aContent)
96
+ expect(await Bun.file(`${tmp.path}/c.txt`).exists()).toBe(false)
97
+ // Note: revert currently only removes files, not directories
98
+ // The empty directory will remain
99
+ expect(await Bun.file(`${tmp.path}/b.txt`).text()).toBe(tmp.extra.bContent)
100
+ },
101
+ })
102
+ })
103
+
104
+ test("empty directory handling", async () => {
105
+ await using tmp = await bootstrap()
106
+ await Instance.provide({
107
+ directory: tmp.path,
108
+ fn: async () => {
109
+ const before = await Snapshot.track()
110
+ expect(before).toBeTruthy()
111
+
112
+ await $`mkdir ${tmp.path}/empty`.quiet()
113
+
114
+ expect((await Snapshot.patch(before!)).files.length).toBe(0)
115
+ },
116
+ })
117
+ })
118
+
119
+ test("binary file handling", async () => {
120
+ await using tmp = await bootstrap()
121
+ await Instance.provide({
122
+ directory: tmp.path,
123
+ fn: async () => {
124
+ const before = await Snapshot.track()
125
+ expect(before).toBeTruthy()
126
+
127
+ await Bun.write(`${tmp.path}/image.png`, new Uint8Array([0x89, 0x50, 0x4e, 0x47]))
128
+
129
+ const patch = await Snapshot.patch(before!)
130
+ expect(patch.files).toContain(path.join(tmp.path, "image.png"))
131
+
132
+ await Snapshot.revert([patch])
133
+ expect(await Bun.file(`${tmp.path}/image.png`).exists()).toBe(false)
134
+ },
135
+ })
136
+ })
137
+
138
+ test.if(process.platform !== "win32")("symlink handling", async () => {
139
+ await using tmp = await bootstrap()
140
+ await Instance.provide({
141
+ directory: tmp.path,
142
+ fn: async () => {
143
+ const before = await Snapshot.track()
144
+ expect(before).toBeTruthy()
145
+
146
+ await $`ln -s ${tmp.path}/a.txt ${tmp.path}/link.txt`.quiet()
147
+
148
+ expect((await Snapshot.patch(before!)).files).toContain(path.join(tmp.path, "link.txt"))
149
+ },
150
+ })
151
+ })
152
+
153
+ test("large file handling", async () => {
154
+ await using tmp = await bootstrap()
155
+ await Instance.provide({
156
+ directory: tmp.path,
157
+ fn: async () => {
158
+ const before = await Snapshot.track()
159
+ expect(before).toBeTruthy()
160
+
161
+ await Bun.write(`${tmp.path}/large.txt`, "x".repeat(1024 * 1024))
162
+
163
+ expect((await Snapshot.patch(before!)).files).toContain(path.join(tmp.path, "large.txt"))
164
+ },
165
+ })
166
+ })
167
+
168
+ test("nested directory revert", async () => {
169
+ await using tmp = await bootstrap()
170
+ await Instance.provide({
171
+ directory: tmp.path,
172
+ fn: async () => {
173
+ const before = await Snapshot.track()
174
+ expect(before).toBeTruthy()
175
+
176
+ await $`mkdir -p ${tmp.path}/level1/level2/level3`.quiet()
177
+ await Bun.write(`${tmp.path}/level1/level2/level3/deep.txt`, "DEEP")
178
+
179
+ await Snapshot.revert([await Snapshot.patch(before!)])
180
+
181
+ expect(await Bun.file(`${tmp.path}/level1/level2/level3/deep.txt`).exists()).toBe(false)
182
+ },
183
+ })
184
+ })
185
+
186
+ test("special characters in filenames", async () => {
187
+ await using tmp = await bootstrap()
188
+ await Instance.provide({
189
+ directory: tmp.path,
190
+ fn: async () => {
191
+ const before = await Snapshot.track()
192
+ expect(before).toBeTruthy()
193
+
194
+ await Bun.write(`${tmp.path}/file with spaces.txt`, "SPACES")
195
+ await Bun.write(`${tmp.path}/file-with-dashes.txt`, "DASHES")
196
+ await Bun.write(`${tmp.path}/file_with_underscores.txt`, "UNDERSCORES")
197
+
198
+ const files = (await Snapshot.patch(before!)).files
199
+ expect(files).toContain(path.join(tmp.path, "file with spaces.txt"))
200
+ expect(files).toContain(path.join(tmp.path, "file-with-dashes.txt"))
201
+ expect(files).toContain(path.join(tmp.path, "file_with_underscores.txt"))
202
+ },
203
+ })
204
+ })
205
+
206
+ test("revert with empty patches", async () => {
207
+ await using tmp = await bootstrap()
208
+ await Instance.provide({
209
+ directory: tmp.path,
210
+ fn: async () => {
211
+ // Should not crash with empty patches
212
+ expect(Snapshot.revert([])).resolves.toBeUndefined()
213
+
214
+ // Should not crash with patches that have empty file lists
215
+ expect(Snapshot.revert([{ hash: "dummy", files: [] }])).resolves.toBeUndefined()
216
+ },
217
+ })
218
+ })
219
+
220
+ test("patch with invalid hash", async () => {
221
+ await using tmp = await bootstrap()
222
+ await Instance.provide({
223
+ directory: tmp.path,
224
+ fn: async () => {
225
+ const before = await Snapshot.track()
226
+ expect(before).toBeTruthy()
227
+
228
+ // Create a change
229
+ await Bun.write(`${tmp.path}/test.txt`, "TEST")
230
+
231
+ // Try to patch with invalid hash - should handle gracefully
232
+ const patch = await Snapshot.patch("invalid-hash-12345")
233
+ expect(patch.files).toEqual([])
234
+ expect(patch.hash).toBe("invalid-hash-12345")
235
+ },
236
+ })
237
+ })
238
+
239
+ test("revert non-existent file", async () => {
240
+ await using tmp = await bootstrap()
241
+ await Instance.provide({
242
+ directory: tmp.path,
243
+ fn: async () => {
244
+ const before = await Snapshot.track()
245
+ expect(before).toBeTruthy()
246
+
247
+ // Try to revert a file that doesn't exist in the snapshot
248
+ // This should not crash
249
+ expect(
250
+ Snapshot.revert([
251
+ {
252
+ hash: before!,
253
+ files: [`${tmp.path}/nonexistent.txt`],
254
+ },
255
+ ]),
256
+ ).resolves.toBeUndefined()
257
+ },
258
+ })
259
+ })
260
+
261
+ test("unicode filenames", async () => {
262
+ await using tmp = await bootstrap()
263
+ await Instance.provide({
264
+ directory: tmp.path,
265
+ fn: async () => {
266
+ const before = await Snapshot.track()
267
+ expect(before).toBeTruthy()
268
+
269
+ const unicodeFiles = [
270
+ `${tmp.path}/文件.txt`,
271
+ `${tmp.path}/🚀rocket.txt`,
272
+ `${tmp.path}/café.txt`,
273
+ `${tmp.path}/файл.txt`,
274
+ ]
275
+
276
+ for (const file of unicodeFiles) {
277
+ await Bun.write(file, "unicode content")
278
+ }
279
+
280
+ const patch = await Snapshot.patch(before!)
281
+ // Note: git escapes unicode characters by default, so we just check that files are detected
282
+ // The actual filenames will be escaped like "caf\303\251.txt" but functionality works
283
+ expect(patch.files.length).toBe(4)
284
+
285
+ // Skip revert test due to git filename escaping issues
286
+ // The functionality works but git uses escaped filenames internally
287
+ },
288
+ })
289
+ })
290
+
291
+ test.if(process.platform !== "win32")("very long filenames", async () => {
292
+ await using tmp = await bootstrap()
293
+ await Instance.provide({
294
+ directory: tmp.path,
295
+ fn: async () => {
296
+ const before = await Snapshot.track()
297
+ expect(before).toBeTruthy()
298
+
299
+ const longName = "a".repeat(200) + ".txt"
300
+ const longFile = path.join(tmp.path, longName)
301
+
302
+ await Bun.write(longFile, "long filename content")
303
+
304
+ const patch = await Snapshot.patch(before!)
305
+ expect(patch.files).toContain(longFile)
306
+
307
+ await Snapshot.revert([patch])
308
+ expect(await Bun.file(longFile).exists()).toBe(false)
309
+ },
310
+ })
311
+ })
312
+
313
+ test("hidden files", async () => {
314
+ await using tmp = await bootstrap()
315
+ await Instance.provide({
316
+ directory: tmp.path,
317
+ fn: async () => {
318
+ const before = await Snapshot.track()
319
+ expect(before).toBeTruthy()
320
+
321
+ await Bun.write(`${tmp.path}/.hidden`, "hidden content")
322
+ await Bun.write(`${tmp.path}/.gitignore`, "*.log")
323
+ await Bun.write(`${tmp.path}/.config`, "config content")
324
+
325
+ const patch = await Snapshot.patch(before!)
326
+ expect(patch.files).toContain(path.join(tmp.path, ".hidden"))
327
+ expect(patch.files).toContain(path.join(tmp.path, ".gitignore"))
328
+ expect(patch.files).toContain(path.join(tmp.path, ".config"))
329
+ },
330
+ })
331
+ })
332
+
333
+ test.if(process.platform !== "win32")("nested symlinks", async () => {
334
+ await using tmp = await bootstrap()
335
+ await Instance.provide({
336
+ directory: tmp.path,
337
+ fn: async () => {
338
+ const before = await Snapshot.track()
339
+ expect(before).toBeTruthy()
340
+
341
+ await $`mkdir -p ${tmp.path}/sub/dir`.quiet()
342
+ await Bun.write(path.join(tmp.path, "sub", "dir", "target.txt"), "target content")
343
+ await $`ln -s ${tmp.path}/sub/dir/target.txt ${tmp.path}/sub/dir/link.txt`.quiet()
344
+ await $`ln -s ${tmp.path}/sub ${tmp.path}/sub-link`.quiet()
345
+
346
+ const patch = await Snapshot.patch(before!)
347
+ expect(patch.files).toContain(path.join(tmp.path, "sub", "dir", "link.txt"))
348
+ expect(patch.files).toContain(path.join(tmp.path, "sub-link"))
349
+ },
350
+ })
351
+ })
352
+
353
+ test.if(process.platform !== "win32")("file permissions and ownership changes", async () => {
354
+ await using tmp = await bootstrap()
355
+ await Instance.provide({
356
+ directory: tmp.path,
357
+ fn: async () => {
358
+ const before = await Snapshot.track()
359
+ expect(before).toBeTruthy()
360
+
361
+ // Change permissions multiple times
362
+ await $`chmod 600 ${tmp.path}/a.txt`.quiet()
363
+ await $`chmod 755 ${tmp.path}/a.txt`.quiet()
364
+ await $`chmod 644 ${tmp.path}/a.txt`.quiet()
365
+
366
+ const patch = await Snapshot.patch(before!)
367
+ // Note: git doesn't track permission changes on existing files by default
368
+ // Only tracks executable bit when files are first added
369
+ expect(patch.files.length).toBe(0)
370
+ },
371
+ })
372
+ })
373
+
374
+ test("circular symlinks", async () => {
375
+ await using tmp = await bootstrap()
376
+ await Instance.provide({
377
+ directory: tmp.path,
378
+ fn: async () => {
379
+ const before = await Snapshot.track()
380
+ expect(before).toBeTruthy()
381
+
382
+ // Create circular symlink
383
+ await $`ln -s ${tmp.path}/circular ${tmp.path}/circular`.quiet().nothrow()
384
+
385
+ const patch = await Snapshot.patch(before!)
386
+ expect(patch.files.length).toBeGreaterThanOrEqual(0) // Should not crash
387
+ },
388
+ })
389
+ })
390
+
391
+ test("gitignore changes", async () => {
392
+ await using tmp = await bootstrap()
393
+ await Instance.provide({
394
+ directory: tmp.path,
395
+ fn: async () => {
396
+ const before = await Snapshot.track()
397
+ expect(before).toBeTruthy()
398
+
399
+ await Bun.write(`${tmp.path}/.gitignore`, "*.ignored")
400
+ await Bun.write(`${tmp.path}/test.ignored`, "ignored content")
401
+ await Bun.write(`${tmp.path}/normal.txt`, "normal content")
402
+
403
+ const patch = await Snapshot.patch(before!)
404
+
405
+ // Should track gitignore itself
406
+ expect(patch.files).toContain(path.join(tmp.path, ".gitignore"))
407
+ // Should track normal files
408
+ expect(patch.files).toContain(path.join(tmp.path, "normal.txt"))
409
+ // Should not track ignored files (git won't see them)
410
+ expect(patch.files).not.toContain(path.join(tmp.path, "test.ignored"))
411
+ },
412
+ })
413
+ })
414
+
415
+ test("concurrent file operations during patch", async () => {
416
+ await using tmp = await bootstrap()
417
+ await Instance.provide({
418
+ directory: tmp.path,
419
+ fn: async () => {
420
+ const before = await Snapshot.track()
421
+ expect(before).toBeTruthy()
422
+
423
+ // Start creating files
424
+ const createPromise = (async () => {
425
+ for (let i = 0; i < 10; i++) {
426
+ await Bun.write(`${tmp.path}/concurrent${i}.txt`, `concurrent${i}`)
427
+ // Small delay to simulate concurrent operations
428
+ await new Promise((resolve) => setTimeout(resolve, 1))
429
+ }
430
+ })()
431
+
432
+ // Get patch while files are being created
433
+ const patchPromise = Snapshot.patch(before!)
434
+
435
+ await createPromise
436
+ const patch = await patchPromise
437
+
438
+ // Should capture some or all of the concurrent files
439
+ expect(patch.files.length).toBeGreaterThanOrEqual(0)
440
+ },
441
+ })
442
+ })
443
+
444
+ test("snapshot state isolation between projects", async () => {
445
+ // Test that different projects don't interfere with each other
446
+ await using tmp1 = await bootstrap()
447
+ await using tmp2 = await bootstrap()
448
+
449
+ await Instance.provide({
450
+ directory: tmp1.path,
451
+ fn: async () => {
452
+ const before1 = await Snapshot.track()
453
+ await Bun.write(`${tmp1.path}/project1.txt`, "project1 content")
454
+ const patch1 = await Snapshot.patch(before1!)
455
+ expect(patch1.files).toContain(path.join(tmp1.path, "project1.txt"))
456
+ },
457
+ })
458
+
459
+ await Instance.provide({
460
+ directory: tmp2.path,
461
+ fn: async () => {
462
+ const before2 = await Snapshot.track()
463
+ await Bun.write(`${tmp2.path}/project2.txt`, "project2 content")
464
+ const patch2 = await Snapshot.patch(before2!)
465
+ expect(patch2.files).toContain(path.join(tmp2.path, "project2.txt"))
466
+
467
+ // Ensure project1 files don't appear in project2
468
+ expect(patch2.files).not.toContain(path.join(tmp1?.path, "project1.txt"))
469
+ },
470
+ })
471
+ })
472
+
473
+ test("patch detects changes in secondary worktree", async () => {
474
+ await using tmp = await bootstrap()
475
+ const worktreePath = `${tmp.path}-worktree`
476
+ await $`git worktree add ${worktreePath} HEAD`.cwd(tmp.path).quiet()
477
+
478
+ try {
479
+ await Instance.provide({
480
+ directory: tmp.path,
481
+ fn: async () => {
482
+ expect(await Snapshot.track()).toBeTruthy()
483
+ },
484
+ })
485
+
486
+ await Instance.provide({
487
+ directory: worktreePath,
488
+ fn: async () => {
489
+ const before = await Snapshot.track()
490
+ expect(before).toBeTruthy()
491
+
492
+ const worktreeFile = path.join(worktreePath, "worktree.txt")
493
+ await Bun.write(worktreeFile, "worktree content")
494
+
495
+ const patch = await Snapshot.patch(before!)
496
+ expect(patch.files).toContain(worktreeFile)
497
+ },
498
+ })
499
+ } finally {
500
+ await $`git worktree remove --force ${worktreePath}`.cwd(tmp.path).quiet().nothrow()
501
+ await $`rm -rf ${worktreePath}`.quiet()
502
+ }
503
+ })
504
+
505
+ test("revert only removes files in invoking worktree", async () => {
506
+ await using tmp = await bootstrap()
507
+ const worktreePath = `${tmp.path}-worktree`
508
+ await $`git worktree add ${worktreePath} HEAD`.cwd(tmp.path).quiet()
509
+
510
+ try {
511
+ await Instance.provide({
512
+ directory: tmp.path,
513
+ fn: async () => {
514
+ expect(await Snapshot.track()).toBeTruthy()
515
+ },
516
+ })
517
+ const primaryFile = `${tmp.path}/worktree.txt`
518
+ await Bun.write(primaryFile, "primary content")
519
+
520
+ await Instance.provide({
521
+ directory: worktreePath,
522
+ fn: async () => {
523
+ const before = await Snapshot.track()
524
+ expect(before).toBeTruthy()
525
+
526
+ const worktreeFile = `${worktreePath}/worktree.txt`
527
+ await Bun.write(worktreeFile, "worktree content")
528
+
529
+ const patch = await Snapshot.patch(before!)
530
+ await Snapshot.revert([patch])
531
+
532
+ expect(await Bun.file(worktreeFile).exists()).toBe(false)
533
+ },
534
+ })
535
+
536
+ expect(await Bun.file(primaryFile).text()).toBe("primary content")
537
+ } finally {
538
+ await $`git worktree remove --force ${worktreePath}`.cwd(tmp.path).quiet().nothrow()
539
+ await $`rm -rf ${worktreePath}`.quiet()
540
+ await $`rm -f ${tmp.path}/worktree.txt`.quiet()
541
+ }
542
+ })
543
+
544
+ test("diff reports worktree-only/shared edits and ignores primary-only", async () => {
545
+ await using tmp = await bootstrap()
546
+ const worktreePath = `${tmp.path}-worktree`
547
+ await $`git worktree add ${worktreePath} HEAD`.cwd(tmp.path).quiet()
548
+
549
+ try {
550
+ await Instance.provide({
551
+ directory: tmp.path,
552
+ fn: async () => {
553
+ expect(await Snapshot.track()).toBeTruthy()
554
+ },
555
+ })
556
+
557
+ await Instance.provide({
558
+ directory: worktreePath,
559
+ fn: async () => {
560
+ const before = await Snapshot.track()
561
+ expect(before).toBeTruthy()
562
+
563
+ await Bun.write(`${worktreePath}/worktree-only.txt`, "worktree diff content")
564
+ await Bun.write(`${worktreePath}/shared.txt`, "worktree edit")
565
+ await Bun.write(`${tmp.path}/shared.txt`, "primary edit")
566
+ await Bun.write(`${tmp.path}/primary-only.txt`, "primary change")
567
+
568
+ const diff = await Snapshot.diff(before!)
569
+ expect(diff).toContain("worktree-only.txt")
570
+ expect(diff).toContain("shared.txt")
571
+ expect(diff).not.toContain("primary-only.txt")
572
+ },
573
+ })
574
+ } finally {
575
+ await $`git worktree remove --force ${worktreePath}`.cwd(tmp.path).quiet().nothrow()
576
+ await $`rm -rf ${worktreePath}`.quiet()
577
+ await $`rm -f ${tmp.path}/shared.txt`.quiet()
578
+ await $`rm -f ${tmp.path}/primary-only.txt`.quiet()
579
+ }
580
+ })
581
+
582
+ test("track with no changes returns same hash", async () => {
583
+ await using tmp = await bootstrap()
584
+ await Instance.provide({
585
+ directory: tmp.path,
586
+ fn: async () => {
587
+ const hash1 = await Snapshot.track()
588
+ expect(hash1).toBeTruthy()
589
+
590
+ // Track again with no changes
591
+ const hash2 = await Snapshot.track()
592
+ expect(hash2).toBe(hash1!)
593
+
594
+ // Track again
595
+ const hash3 = await Snapshot.track()
596
+ expect(hash3).toBe(hash1!)
597
+ },
598
+ })
599
+ })
600
+
601
+ test("diff function with various changes", async () => {
602
+ await using tmp = await bootstrap()
603
+ await Instance.provide({
604
+ directory: tmp.path,
605
+ fn: async () => {
606
+ const before = await Snapshot.track()
607
+ expect(before).toBeTruthy()
608
+
609
+ // Make various changes
610
+ await $`rm ${tmp.path}/a.txt`.quiet()
611
+ await Bun.write(`${tmp.path}/new.txt`, "new content")
612
+ await Bun.write(`${tmp.path}/b.txt`, "modified content")
613
+
614
+ const diff = await Snapshot.diff(before!)
615
+ expect(diff).toContain("a.txt")
616
+ expect(diff).toContain("b.txt")
617
+ expect(diff).toContain("new.txt")
618
+ },
619
+ })
620
+ })
621
+
622
+ test("restore function", async () => {
623
+ await using tmp = await bootstrap()
624
+ await Instance.provide({
625
+ directory: tmp.path,
626
+ fn: async () => {
627
+ const before = await Snapshot.track()
628
+ expect(before).toBeTruthy()
629
+
630
+ // Make changes
631
+ await $`rm ${tmp.path}/a.txt`.quiet()
632
+ await Bun.write(`${tmp.path}/new.txt`, "new content")
633
+ await Bun.write(`${tmp.path}/b.txt`, "modified")
634
+
635
+ // Restore to original state
636
+ await Snapshot.restore(before!)
637
+
638
+ expect(await Bun.file(`${tmp.path}/a.txt`).exists()).toBe(true)
639
+ expect(await Bun.file(`${tmp.path}/a.txt`).text()).toBe(tmp.extra.aContent)
640
+ expect(await Bun.file(`${tmp.path}/new.txt`).exists()).toBe(true) // New files should remain
641
+ expect(await Bun.file(`${tmp.path}/b.txt`).text()).toBe(tmp.extra.bContent)
642
+ },
643
+ })
644
+ })
645
+
646
+ test("revert should not delete files that existed but were deleted in snapshot", async () => {
647
+ await using tmp = await bootstrap()
648
+ await Instance.provide({
649
+ directory: tmp.path,
650
+ fn: async () => {
651
+ const snapshot1 = await Snapshot.track()
652
+ expect(snapshot1).toBeTruthy()
653
+
654
+ await $`rm ${tmp.path}/a.txt`.quiet()
655
+
656
+ const snapshot2 = await Snapshot.track()
657
+ expect(snapshot2).toBeTruthy()
658
+
659
+ await Bun.write(`${tmp.path}/a.txt`, "recreated content")
660
+
661
+ const patch = await Snapshot.patch(snapshot2!)
662
+ expect(patch.files).toContain(path.join(tmp.path, "a.txt"))
663
+
664
+ await Snapshot.revert([patch])
665
+
666
+ expect(await Bun.file(`${tmp.path}/a.txt`).exists()).toBe(false)
667
+ },
668
+ })
669
+ })
670
+
671
+ test("revert preserves file that existed in snapshot when deleted then recreated", async () => {
672
+ await using tmp = await bootstrap()
673
+ await Instance.provide({
674
+ directory: tmp.path,
675
+ fn: async () => {
676
+ await Bun.write(`${tmp.path}/existing.txt`, "original content")
677
+
678
+ const snapshot = await Snapshot.track()
679
+ expect(snapshot).toBeTruthy()
680
+
681
+ await $`rm ${tmp.path}/existing.txt`.quiet()
682
+ await Bun.write(`${tmp.path}/existing.txt`, "recreated")
683
+ await Bun.write(`${tmp.path}/newfile.txt`, "new")
684
+
685
+ const patch = await Snapshot.patch(snapshot!)
686
+ expect(patch.files).toContain(path.join(tmp.path, "existing.txt"))
687
+ expect(patch.files).toContain(path.join(tmp.path, "newfile.txt"))
688
+
689
+ await Snapshot.revert([patch])
690
+
691
+ expect(await Bun.file(`${tmp.path}/newfile.txt`).exists()).toBe(false)
692
+ expect(await Bun.file(`${tmp.path}/existing.txt`).exists()).toBe(true)
693
+ expect(await Bun.file(`${tmp.path}/existing.txt`).text()).toBe("original content")
694
+ },
695
+ })
696
+ })
697
+
698
+ test("diffFull with new file additions", async () => {
699
+ await using tmp = await bootstrap()
700
+ await Instance.provide({
701
+ directory: tmp.path,
702
+ fn: async () => {
703
+ const before = await Snapshot.track()
704
+ expect(before).toBeTruthy()
705
+
706
+ await Bun.write(`${tmp.path}/new.txt`, "new content")
707
+
708
+ const after = await Snapshot.track()
709
+ expect(after).toBeTruthy()
710
+
711
+ const diffs = await Snapshot.diffFull(before!, after!)
712
+ expect(diffs.length).toBe(1)
713
+
714
+ const newFileDiff = diffs[0]
715
+ expect(newFileDiff.file).toBe("new.txt")
716
+ expect(newFileDiff.before).toBe("")
717
+ expect(newFileDiff.after).toBe("new content")
718
+ expect(newFileDiff.additions).toBe(1)
719
+ expect(newFileDiff.deletions).toBe(0)
720
+ },
721
+ })
722
+ })
723
+
724
+ test("diffFull with file modifications", 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
+
732
+ await Bun.write(`${tmp.path}/b.txt`, "modified content")
733
+
734
+ const after = await Snapshot.track()
735
+ expect(after).toBeTruthy()
736
+
737
+ const diffs = await Snapshot.diffFull(before!, after!)
738
+ expect(diffs.length).toBe(1)
739
+
740
+ const modifiedFileDiff = diffs[0]
741
+ expect(modifiedFileDiff.file).toBe("b.txt")
742
+ expect(modifiedFileDiff.before).toBe(tmp.extra.bContent)
743
+ expect(modifiedFileDiff.after).toBe("modified content")
744
+ expect(modifiedFileDiff.additions).toBeGreaterThan(0)
745
+ expect(modifiedFileDiff.deletions).toBeGreaterThan(0)
746
+ },
747
+ })
748
+ })
749
+
750
+ test("diffFull with file deletions", async () => {
751
+ await using tmp = await bootstrap()
752
+ await Instance.provide({
753
+ directory: tmp.path,
754
+ fn: async () => {
755
+ const before = await Snapshot.track()
756
+ expect(before).toBeTruthy()
757
+
758
+ await $`rm ${tmp.path}/a.txt`.quiet()
759
+
760
+ const after = await Snapshot.track()
761
+ expect(after).toBeTruthy()
762
+
763
+ const diffs = await Snapshot.diffFull(before!, after!)
764
+ expect(diffs.length).toBe(1)
765
+
766
+ const removedFileDiff = diffs[0]
767
+ expect(removedFileDiff.file).toBe("a.txt")
768
+ expect(removedFileDiff.before).toBe(tmp.extra.aContent)
769
+ expect(removedFileDiff.after).toBe("")
770
+ expect(removedFileDiff.additions).toBe(0)
771
+ expect(removedFileDiff.deletions).toBe(1)
772
+ },
773
+ })
774
+ })
775
+
776
+ test("diffFull with multiple line additions", async () => {
777
+ await using tmp = await bootstrap()
778
+ await Instance.provide({
779
+ directory: tmp.path,
780
+ fn: async () => {
781
+ const before = await Snapshot.track()
782
+ expect(before).toBeTruthy()
783
+
784
+ await Bun.write(`${tmp.path}/multi.txt`, "line1\nline2\nline3")
785
+
786
+ const after = await Snapshot.track()
787
+ expect(after).toBeTruthy()
788
+
789
+ const diffs = await Snapshot.diffFull(before!, after!)
790
+ expect(diffs.length).toBe(1)
791
+
792
+ const multiDiff = diffs[0]
793
+ expect(multiDiff.file).toBe("multi.txt")
794
+ expect(multiDiff.before).toBe("")
795
+ expect(multiDiff.after).toBe("line1\nline2\nline3")
796
+ expect(multiDiff.additions).toBe(3)
797
+ expect(multiDiff.deletions).toBe(0)
798
+ },
799
+ })
800
+ })
801
+
802
+ test("diffFull with addition and deletion", async () => {
803
+ await using tmp = await bootstrap()
804
+ await Instance.provide({
805
+ directory: tmp.path,
806
+ fn: async () => {
807
+ const before = await Snapshot.track()
808
+ expect(before).toBeTruthy()
809
+
810
+ await Bun.write(`${tmp.path}/added.txt`, "added content")
811
+ await $`rm ${tmp.path}/a.txt`.quiet()
812
+
813
+ const after = await Snapshot.track()
814
+ expect(after).toBeTruthy()
815
+
816
+ const diffs = await Snapshot.diffFull(before!, after!)
817
+ expect(diffs.length).toBe(2)
818
+
819
+ const addedFileDiff = diffs.find((d) => d.file === "added.txt")
820
+ expect(addedFileDiff).toBeDefined()
821
+ expect(addedFileDiff!.before).toBe("")
822
+ expect(addedFileDiff!.after).toBe("added content")
823
+ expect(addedFileDiff!.additions).toBe(1)
824
+ expect(addedFileDiff!.deletions).toBe(0)
825
+
826
+ const removedFileDiff = diffs.find((d) => d.file === "a.txt")
827
+ expect(removedFileDiff).toBeDefined()
828
+ expect(removedFileDiff!.before).toBe(tmp.extra.aContent)
829
+ expect(removedFileDiff!.after).toBe("")
830
+ expect(removedFileDiff!.additions).toBe(0)
831
+ expect(removedFileDiff!.deletions).toBe(1)
832
+ },
833
+ })
834
+ })
835
+
836
+ test("diffFull with multiple additions and deletions", async () => {
837
+ await using tmp = await bootstrap()
838
+ await Instance.provide({
839
+ directory: tmp.path,
840
+ fn: async () => {
841
+ const before = await Snapshot.track()
842
+ expect(before).toBeTruthy()
843
+
844
+ await Bun.write(`${tmp.path}/multi1.txt`, "line1\nline2\nline3")
845
+ await Bun.write(`${tmp.path}/multi2.txt`, "single line")
846
+ await $`rm ${tmp.path}/a.txt`.quiet()
847
+ await $`rm ${tmp.path}/b.txt`.quiet()
848
+
849
+ const after = await Snapshot.track()
850
+ expect(after).toBeTruthy()
851
+
852
+ const diffs = await Snapshot.diffFull(before!, after!)
853
+ expect(diffs.length).toBe(4)
854
+
855
+ const multi1Diff = diffs.find((d) => d.file === "multi1.txt")
856
+ expect(multi1Diff).toBeDefined()
857
+ expect(multi1Diff!.additions).toBe(3)
858
+ expect(multi1Diff!.deletions).toBe(0)
859
+
860
+ const multi2Diff = diffs.find((d) => d.file === "multi2.txt")
861
+ expect(multi2Diff).toBeDefined()
862
+ expect(multi2Diff!.additions).toBe(1)
863
+ expect(multi2Diff!.deletions).toBe(0)
864
+
865
+ const removedADiff = diffs.find((d) => d.file === "a.txt")
866
+ expect(removedADiff).toBeDefined()
867
+ expect(removedADiff!.additions).toBe(0)
868
+ expect(removedADiff!.deletions).toBe(1)
869
+
870
+ const removedBDiff = diffs.find((d) => d.file === "b.txt")
871
+ expect(removedBDiff).toBeDefined()
872
+ expect(removedBDiff!.additions).toBe(0)
873
+ expect(removedBDiff!.deletions).toBe(1)
874
+ },
875
+ })
876
+ })
877
+
878
+ test("diffFull with no changes", async () => {
879
+ await using tmp = await bootstrap()
880
+ await Instance.provide({
881
+ directory: tmp.path,
882
+ fn: async () => {
883
+ const before = await Snapshot.track()
884
+ expect(before).toBeTruthy()
885
+
886
+ const after = await Snapshot.track()
887
+ expect(after).toBeTruthy()
888
+
889
+ const diffs = await Snapshot.diffFull(before!, after!)
890
+ expect(diffs.length).toBe(0)
891
+ },
892
+ })
893
+ })
894
+
895
+ test("diffFull with binary file changes", async () => {
896
+ await using tmp = await bootstrap()
897
+ await Instance.provide({
898
+ directory: tmp.path,
899
+ fn: async () => {
900
+ const before = await Snapshot.track()
901
+ expect(before).toBeTruthy()
902
+
903
+ await Bun.write(`${tmp.path}/binary.bin`, new Uint8Array([0x00, 0x01, 0x02, 0x03]))
904
+
905
+ const after = await Snapshot.track()
906
+ expect(after).toBeTruthy()
907
+
908
+ const diffs = await Snapshot.diffFull(before!, after!)
909
+ expect(diffs.length).toBe(1)
910
+
911
+ const binaryDiff = diffs[0]
912
+ expect(binaryDiff.file).toBe("binary.bin")
913
+ expect(binaryDiff.before).toBe("")
914
+ },
915
+ })
916
+ })
917
+
918
+ test("diffFull with whitespace changes", async () => {
919
+ await using tmp = await bootstrap()
920
+ await Instance.provide({
921
+ directory: tmp.path,
922
+ fn: async () => {
923
+ await Bun.write(`${tmp.path}/whitespace.txt`, "line1\nline2")
924
+ const before = await Snapshot.track()
925
+ expect(before).toBeTruthy()
926
+
927
+ await Bun.write(`${tmp.path}/whitespace.txt`, "line1\n\nline2\n")
928
+
929
+ const after = await Snapshot.track()
930
+ expect(after).toBeTruthy()
931
+
932
+ const diffs = await Snapshot.diffFull(before!, after!)
933
+ expect(diffs.length).toBe(1)
934
+
935
+ const whitespaceDiff = diffs[0]
936
+ expect(whitespaceDiff.file).toBe("whitespace.txt")
937
+ expect(whitespaceDiff.additions).toBeGreaterThan(0)
938
+ },
939
+ })
940
+ })