project-copy.test.ts 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  1. import { describe, expect } from "bun:test"
  2. import { $ } from "bun"
  3. import fs from "fs/promises"
  4. import path from "path"
  5. import { eq } from "drizzle-orm"
  6. import { Effect, Fiber, Layer, Stream } from "effect"
  7. import { AbsolutePath } from "@opencode-ai/core/schema"
  8. import { FSUtil } from "@opencode-ai/core/fs-util"
  9. import { Git } from "@opencode-ai/core/git"
  10. import { Database } from "@opencode-ai/core/database/database"
  11. import { EventV2 } from "@opencode-ai/core/event"
  12. import { Project } from "@opencode-ai/core/project"
  13. import { ProjectDirectoryTable, ProjectTable } from "@opencode-ai/core/project/sql"
  14. import { ProjectCopy } from "@opencode-ai/core/project/copy"
  15. import { ProjectDirectories } from "@opencode-ai/core/project/directories"
  16. import { tmpdir } from "./fixture/tmpdir"
  17. import { testEffect } from "./lib/effect"
  18. const databaseLayer = Database.layerFromPath(":memory:")
  19. const eventLayer = EventV2.layer.pipe(Layer.provide(databaseLayer))
  20. const directoriesLayer = ProjectDirectories.layer.pipe(Layer.provide(databaseLayer))
  21. const copyLayer = ProjectCopy.layer.pipe(
  22. Layer.provide(databaseLayer),
  23. Layer.provide(directoriesLayer),
  24. Layer.provide(eventLayer),
  25. Layer.provide(FSUtil.defaultLayer),
  26. Layer.provide(Git.defaultLayer),
  27. )
  28. const it = testEffect(Layer.mergeAll(copyLayer, databaseLayer, eventLayer, directoriesLayer))
  29. function abs(input: string) {
  30. return AbsolutePath.make(input)
  31. }
  32. const gitWorktree = ProjectCopy.StrategyID.make("git_worktree")
  33. async function initRepo(directory: string) {
  34. await $`git init`.cwd(directory).quiet()
  35. await $`git config core.fsmonitor false`.cwd(directory).quiet()
  36. await $`git config commit.gpgsign false`.cwd(directory).quiet()
  37. await $`git config user.email test@opencode.test`.cwd(directory).quiet()
  38. await $`git config user.name Test`.cwd(directory).quiet()
  39. await $`git commit --allow-empty -m root`.cwd(directory).quiet()
  40. }
  41. function setup() {
  42. return Effect.gen(function* () {
  43. const root = yield* Effect.acquireRelease(
  44. Effect.promise(() => tmpdir()),
  45. (dir) => Effect.promise(() => dir[Symbol.asyncDispose]()),
  46. )
  47. yield* Effect.promise(() => initRepo(root.path))
  48. const sourceDirectory = abs(yield* Effect.promise(() => fs.realpath(root.path)))
  49. const projectID = Project.ID.make("copy-project")
  50. const { db } = yield* Database.Service
  51. yield* db
  52. .insert(ProjectTable)
  53. .values({ id: projectID, worktree: sourceDirectory, sandboxes: [], time_created: 1, time_updated: 1 })
  54. .run()
  55. .pipe(Effect.orDie)
  56. yield* db
  57. .insert(ProjectDirectoryTable)
  58. .values({ project_id: projectID, directory: sourceDirectory })
  59. .run()
  60. .pipe(Effect.orDie)
  61. return { root, sourceDirectory, projectID, db }
  62. })
  63. }
  64. function stored(projectID: Project.ID) {
  65. return Database.Service.use(({ db }) =>
  66. db
  67. .select({ directory: ProjectDirectoryTable.directory, strategy: ProjectDirectoryTable.strategy })
  68. .from(ProjectDirectoryTable)
  69. .where(eq(ProjectDirectoryTable.project_id, projectID))
  70. .all()
  71. .pipe(
  72. Effect.orDie,
  73. Effect.map((rows) => rows.toSorted((a, b) => a.directory.localeCompare(b.directory))),
  74. ),
  75. )
  76. }
  77. describe("ProjectCopy", () => {
  78. it.effect("accepts arbitrary non-empty strategy ids", () =>
  79. Effect.sync(() => {
  80. expect(String(ProjectCopy.StrategyID.make("acme/snapshot"))).toBe("acme/snapshot")
  81. expect(() => ProjectCopy.StrategyID.make(" acme/snapshot ")).toThrow()
  82. expect(() => ProjectCopy.StrategyID.make(" ")).toThrow()
  83. }),
  84. )
  85. it.effect("rejects duplicate strategies and reports unavailable ids", () =>
  86. Effect.gen(function* () {
  87. const input = yield* setup()
  88. const copy = yield* ProjectCopy.Service
  89. const strategy: ProjectCopy.Strategy = {
  90. id: ProjectCopy.StrategyID.make("test/duplicate"),
  91. create: () => Effect.die("unused"),
  92. remove: () => Effect.die("unused"),
  93. list: () => Effect.succeed([]),
  94. }
  95. yield* copy.register(strategy)
  96. expect(yield* copy.register(strategy).pipe(Effect.flip)).toBeInstanceOf(ProjectCopy.DuplicateStrategyError)
  97. const unavailable = ProjectCopy.StrategyID.make("acme/missing")
  98. const error = yield* copy
  99. .create({
  100. projectID: input.projectID,
  101. strategy: unavailable,
  102. sourceDirectory: input.sourceDirectory,
  103. directory: abs(`${input.root.path}-missing-strategy`),
  104. name: "copy",
  105. })
  106. .pipe(Effect.flip)
  107. expect(error).toBeInstanceOf(ProjectCopy.StrategyUnavailableError)
  108. if (error instanceof ProjectCopy.StrategyUnavailableError) expect(error.strategy).toBe(unavailable)
  109. }),
  110. )
  111. it.live("creates and removes a git worktree directory", () =>
  112. Effect.gen(function* () {
  113. const input = yield* setup()
  114. const copy = yield* ProjectCopy.Service
  115. const events = yield* EventV2.Service
  116. const temp = yield* Effect.promise(() => fs.realpath(path.dirname(input.root.path)))
  117. const parent = abs(path.join(temp, path.basename(input.root.path) + "-copy-created"))
  118. const target = abs(path.join(parent, "copy"))
  119. yield* Effect.addFinalizer(() =>
  120. Effect.promise(() => fs.rm(parent, { recursive: true, force: true })).pipe(Effect.ignore),
  121. )
  122. const fiber = yield* events
  123. .subscribe(ProjectCopy.Event.Updated)
  124. .pipe(Stream.take(1), Stream.runCollect, Effect.forkScoped)
  125. yield* Effect.yieldNow
  126. const created = yield* copy.create({
  127. projectID: input.projectID,
  128. strategy: gitWorktree,
  129. sourceDirectory: input.sourceDirectory,
  130. directory: parent,
  131. name: "copy",
  132. })
  133. expect(created.directory).toBe(target)
  134. expect(yield* stored(input.projectID)).toEqual(
  135. [
  136. { directory: input.sourceDirectory, strategy: null },
  137. { directory: created.directory, strategy: "git_worktree" },
  138. ].toSorted((a, b) => a.directory.localeCompare(b.directory)),
  139. )
  140. expect(Array.from(yield* Fiber.join(fiber))[0]?.data).toEqual({ projectID: input.projectID })
  141. yield* copy.remove({ projectID: input.projectID, directory: created.directory, force: false })
  142. expect(yield* stored(input.projectID)).toEqual([{ directory: input.sourceDirectory, strategy: null }])
  143. expect(yield* Effect.promise(() => Bun.file(target).exists())).toBe(false)
  144. }),
  145. )
  146. it.live("requires force to remove a dirty git worktree", () =>
  147. Effect.gen(function* () {
  148. const input = yield* setup()
  149. const copy = yield* ProjectCopy.Service
  150. const temp = yield* Effect.promise(() => fs.realpath(path.dirname(input.root.path)))
  151. const parent = abs(path.join(temp, path.basename(input.root.path) + "-copy-dirty"))
  152. yield* Effect.addFinalizer(() =>
  153. Effect.promise(() => fs.rm(parent, { recursive: true, force: true })).pipe(Effect.ignore),
  154. )
  155. const created = yield* copy.create({
  156. projectID: input.projectID,
  157. strategy: gitWorktree,
  158. sourceDirectory: input.sourceDirectory,
  159. directory: parent,
  160. name: "copy",
  161. })
  162. yield* Effect.promise(() => Bun.write(path.join(created.directory, "dirty.txt"), "dirty"))
  163. const error = yield* copy
  164. .remove({ projectID: input.projectID, directory: created.directory, force: false })
  165. .pipe(Effect.flip)
  166. expect(error).toBeInstanceOf(Git.WorktreeError)
  167. if (error instanceof Git.WorktreeError) {
  168. expect(error.operation).toBe("remove")
  169. expect(error.forceRequired).toBe(true)
  170. }
  171. expect(yield* stored(input.projectID)).toContainEqual({ directory: created.directory, strategy: "git_worktree" })
  172. expect(yield* Effect.promise(() => Bun.file(path.join(created.directory, "dirty.txt")).exists())).toBe(true)
  173. yield* copy.remove({ projectID: input.projectID, directory: created.directory, force: true })
  174. expect(yield* Effect.promise(() => Bun.file(created.directory).exists())).toBe(false)
  175. }),
  176. )
  177. it.live("preserves copies whose stored strategy is unavailable", () =>
  178. Effect.gen(function* () {
  179. const input = yield* setup()
  180. const copy = yield* ProjectCopy.Service
  181. const unavailable = abs(`${input.root.path}-copy-unavailable`)
  182. yield* Effect.promise(() => fs.mkdir(unavailable))
  183. yield* Effect.addFinalizer(() => Effect.promise(() => fs.rm(unavailable, { recursive: true, force: true })))
  184. yield* input.db
  185. .insert(ProjectDirectoryTable)
  186. .values({ project_id: input.projectID, directory: unavailable, strategy: "acme/missing" })
  187. .run()
  188. .pipe(Effect.orDie)
  189. const error = yield* copy
  190. .remove({ projectID: input.projectID, directory: unavailable, force: false })
  191. .pipe(Effect.flip)
  192. expect(error).toBeInstanceOf(ProjectCopy.StrategyUnavailableError)
  193. expect(yield* stored(input.projectID)).toContainEqual({ directory: unavailable, strategy: "acme/missing" })
  194. }),
  195. )
  196. it.live("adds a numeric suffix when a copy directory already exists", () =>
  197. Effect.gen(function* () {
  198. const input = yield* setup()
  199. const copy = yield* ProjectCopy.Service
  200. const temp = yield* Effect.promise(() => fs.realpath(path.dirname(input.root.path)))
  201. const parent = abs(path.join(temp, path.basename(input.root.path) + "-copy-suffix"))
  202. const target = abs(path.join(parent, "copy-3"))
  203. yield* Effect.addFinalizer(() =>
  204. Effect.promise(() => fs.rm(parent, { recursive: true, force: true })).pipe(Effect.ignore),
  205. )
  206. yield* Effect.promise(() => fs.mkdir(path.join(parent, "copy"), { recursive: true }))
  207. yield* Effect.promise(() => fs.mkdir(path.join(parent, "copy-2")))
  208. const created = yield* copy.create({
  209. projectID: input.projectID,
  210. strategy: gitWorktree,
  211. sourceDirectory: input.sourceDirectory,
  212. directory: parent,
  213. name: "copy",
  214. })
  215. expect(created.directory).toBe(target)
  216. expect(yield* Effect.promise(() => fs.stat(path.join(parent, "copy")).then((item) => item.isDirectory()))).toBe(
  217. true,
  218. )
  219. expect(yield* Effect.promise(() => fs.stat(path.join(parent, "copy-2")).then((item) => item.isDirectory()))).toBe(
  220. true,
  221. )
  222. yield* copy.remove({ projectID: input.projectID, directory: created.directory, force: false })
  223. }),
  224. )
  225. it.live("fails after ten copy directory conflicts", () =>
  226. Effect.gen(function* () {
  227. const input = yield* setup()
  228. const copy = yield* ProjectCopy.Service
  229. const temp = yield* Effect.promise(() => fs.realpath(path.dirname(input.root.path)))
  230. const parent = abs(path.join(temp, path.basename(input.root.path) + "-copy-conflicts"))
  231. yield* Effect.addFinalizer(() =>
  232. Effect.promise(() => fs.rm(parent, { recursive: true, force: true })).pipe(Effect.ignore),
  233. )
  234. yield* Effect.promise(() =>
  235. Promise.all(
  236. Array.from({ length: 10 }, (_, index) =>
  237. fs.mkdir(path.join(parent, index === 0 ? "copy" : `copy-${index + 1}`), { recursive: true }),
  238. ),
  239. ),
  240. )
  241. const error = yield* copy
  242. .create({
  243. projectID: input.projectID,
  244. strategy: gitWorktree,
  245. sourceDirectory: input.sourceDirectory,
  246. directory: parent,
  247. name: "copy",
  248. })
  249. .pipe(Effect.flip)
  250. expect(error).toBeInstanceOf(ProjectCopy.DestinationExistsError)
  251. if (error instanceof ProjectCopy.DestinationExistsError)
  252. expect(error.directory).toBe(abs(path.join(parent, "copy-10")))
  253. }),
  254. )
  255. it.live("does not publish an event when refresh finds no directory changes", () =>
  256. Effect.gen(function* () {
  257. const input = yield* setup()
  258. const copy = yield* ProjectCopy.Service
  259. const events = yield* EventV2.Service
  260. const event = yield* events.subscribe(ProjectCopy.Event.Updated).pipe(
  261. Stream.take(1),
  262. Stream.runCollect,
  263. Effect.forkScoped,
  264. Effect.flatMap((fiber) =>
  265. Effect.gen(function* () {
  266. yield* Effect.yieldNow
  267. yield* copy.refresh({ projectID: input.projectID })
  268. return yield* Fiber.join(fiber).pipe(Effect.timeoutOption("50 millis"))
  269. }),
  270. ),
  271. )
  272. expect(event._tag).toBe("None")
  273. }),
  274. )
  275. it.live("refresh discovers and prunes an externally managed git worktree", () =>
  276. Effect.gen(function* () {
  277. const input = yield* setup()
  278. const copy = yield* ProjectCopy.Service
  279. const events = yield* EventV2.Service
  280. const target = abs(`${input.root.path}-copy-external`)
  281. yield* Effect.addFinalizer(() =>
  282. Effect.promise(() => fs.rm(target, { recursive: true, force: true })).pipe(Effect.ignore),
  283. )
  284. yield* Effect.promise(() => $`git worktree add --detach ${target} HEAD`.cwd(input.root.path).quiet())
  285. yield* input.db
  286. .insert(ProjectDirectoryTable)
  287. .values({ project_id: input.projectID, directory: target })
  288. .run()
  289. .pipe(Effect.orDie)
  290. const fiber = yield* events
  291. .subscribe(ProjectCopy.Event.Updated)
  292. .pipe(Stream.take(1), Stream.runCollect, Effect.forkScoped)
  293. yield* Effect.yieldNow
  294. const discovered = abs(yield* Effect.promise(() => fs.realpath(target)))
  295. expect(yield* copy.refresh({ projectID: input.projectID })).toEqual({ updated: [discovered], removed: [] })
  296. expect(yield* stored(input.projectID)).toEqual(
  297. [
  298. { directory: input.sourceDirectory, strategy: null },
  299. { directory: discovered, strategy: "git_worktree" },
  300. ].toSorted((a, b) => a.directory.localeCompare(b.directory)),
  301. )
  302. expect(Array.from(yield* Fiber.join(fiber))[0]?.data).toEqual({ projectID: input.projectID })
  303. yield* Effect.promise(() => $`git worktree remove --force ${target}`.cwd(input.root.path).quiet())
  304. expect(yield* copy.refresh({ projectID: input.projectID })).toEqual({ updated: [], removed: [discovered] })
  305. expect(yield* stored(input.projectID)).toEqual([{ directory: input.sourceDirectory, strategy: null }])
  306. }),
  307. )
  308. it.live("refresh ignores stale git worktree registrations", () =>
  309. Effect.gen(function* () {
  310. const input = yield* setup()
  311. const copy = yield* ProjectCopy.Service
  312. const stale = abs(`${input.root.path}-copy-stale`)
  313. const target = abs(`${input.root.path}-copy-after-stale`)
  314. yield* Effect.addFinalizer(() =>
  315. Effect.promise(() => fs.rm(target, { recursive: true, force: true })).pipe(Effect.ignore),
  316. )
  317. yield* Effect.promise(() => $`git worktree add --detach ${stale} HEAD`.cwd(input.root.path).quiet())
  318. yield* Effect.promise(() => fs.rm(stale, { recursive: true, force: true }))
  319. yield* Effect.promise(() => $`git worktree add --detach ${target} HEAD`.cwd(input.root.path).quiet())
  320. yield* copy.refresh({ projectID: input.projectID })
  321. const discovered = abs(yield* Effect.promise(() => fs.realpath(target)))
  322. expect(yield* stored(input.projectID)).toEqual(
  323. [
  324. { directory: input.sourceDirectory, strategy: null },
  325. { directory: discovered, strategy: "git_worktree" },
  326. ].toSorted((a, b) => a.directory.localeCompare(b.directory)),
  327. )
  328. }),
  329. )
  330. it.live("refresh ignores existing directories that are no longer git checkouts", () =>
  331. Effect.gen(function* () {
  332. const input = yield* setup()
  333. yield* Effect.promise(() => fs.rm(path.join(input.sourceDirectory, ".git"), { recursive: true }))
  334. const copy = yield* ProjectCopy.Service
  335. yield* copy.refresh({ projectID: input.projectID })
  336. expect(yield* stored(input.projectID)).toEqual([{ directory: input.sourceDirectory, strategy: null }])
  337. }),
  338. )
  339. it.live("refresh with no roots is a no-op", () =>
  340. Effect.gen(function* () {
  341. const copy = yield* ProjectCopy.Service
  342. expect(yield* copy.refresh({ projectID: Project.ID.make("missing-project") })).toEqual({
  343. updated: [],
  344. removed: [],
  345. })
  346. }),
  347. )
  348. it.live("refresh removes missing ordinary checkouts", () =>
  349. Effect.gen(function* () {
  350. const input = yield* setup()
  351. const missing = abs(`${input.root.path}-missing-checkout`)
  352. yield* input.db
  353. .insert(ProjectDirectoryTable)
  354. .values({ project_id: input.projectID, directory: missing })
  355. .run()
  356. .pipe(Effect.orDie)
  357. const copy = yield* ProjectCopy.Service
  358. expect(yield* copy.refresh({ projectID: input.projectID })).toEqual({ updated: [], removed: [missing] })
  359. expect(yield* stored(input.projectID)).not.toContainEqual({ directory: missing, strategy: null })
  360. }),
  361. )
  362. })