project.test.ts 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. import { describe, expect } from "bun:test"
  2. import { $ } from "bun"
  3. import fs from "fs/promises"
  4. import path from "path"
  5. import { Effect, Layer, Schema } from "effect"
  6. import { ProjectV2 } from "@opencode-ai/core/project"
  7. import { Database } from "@opencode-ai/core/database/database"
  8. import { FSUtil } from "@opencode-ai/core/fs-util"
  9. import { Git } from "@opencode-ai/core/git"
  10. import { AbsolutePath } from "@opencode-ai/core/schema"
  11. import { Hash } from "@opencode-ai/core/util/hash"
  12. import { ProjectDirectories } from "@opencode-ai/core/project/directories"
  13. import { tmpdir } from "./fixture/tmpdir"
  14. import { testEffect } from "./lib/effect"
  15. const it = testEffect(Layer.mergeAll(ProjectV2.defaultLayer, Database.defaultLayer, ProjectDirectories.defaultLayer))
  16. function remoteID(remote: string) {
  17. return ProjectV2.ID.make(Hash.fast(`git-remote:${remote}`))
  18. }
  19. function abs(value: string) {
  20. return AbsolutePath.make(value)
  21. }
  22. function real(value: string) {
  23. return Effect.promise(() => fs.realpath(value)).pipe(Effect.map((value) => AbsolutePath.make(value)))
  24. }
  25. async function initRepo(dir: string, opts?: { commit?: boolean; remote?: string }) {
  26. await $`git init`.cwd(dir).quiet()
  27. await $`git config core.fsmonitor false`.cwd(dir).quiet()
  28. await $`git config commit.gpgsign false`.cwd(dir).quiet()
  29. await $`git config user.email test@opencode.test`.cwd(dir).quiet()
  30. await $`git config user.name Test`.cwd(dir).quiet()
  31. if (opts?.commit) await $`git commit --allow-empty -m root`.cwd(dir).quiet()
  32. if (opts?.remote) await $`git remote add origin ${opts.remote}`.cwd(dir).quiet()
  33. }
  34. async function rootCommit(dir: string) {
  35. return (await $`git rev-list --max-parents=0 HEAD`.cwd(dir).text()).trim()
  36. }
  37. describe("ProjectV2.resolve", () => {
  38. it.live("returns global for non-git directory", () =>
  39. Effect.gen(function* () {
  40. const tmp = yield* Effect.acquireRelease(
  41. Effect.promise(() => tmpdir()),
  42. (tmp) => Effect.promise(() => tmp[Symbol.asyncDispose]()),
  43. )
  44. const project = yield* ProjectV2.Service
  45. const result = yield* project.resolve(abs(tmp.path))
  46. expect(result.id).toBe(ProjectV2.ID.make("global"))
  47. expect(path.resolve(result.directory)).toBe(path.parse(tmp.path).root)
  48. expect(result.previous).toBeUndefined()
  49. expect(result.vcs).toBeUndefined()
  50. }),
  51. )
  52. it.live("returns git global for repo with no commits and no remote", () =>
  53. Effect.gen(function* () {
  54. const tmp = yield* Effect.acquireRelease(
  55. Effect.promise(() => tmpdir()),
  56. (tmp) => Effect.promise(() => tmp[Symbol.asyncDispose]()),
  57. )
  58. yield* Effect.promise(() => initRepo(tmp.path))
  59. const project = yield* ProjectV2.Service
  60. const result = yield* project.resolve(abs(tmp.path))
  61. expect(result.id).toBe(ProjectV2.ID.make("global"))
  62. expect(result.directory).toBe(yield* real(tmp.path))
  63. expect(result.previous).toBeUndefined()
  64. expect(result.vcs?.type).toBe("git")
  65. }),
  66. )
  67. it.live("falls back to root commit when origin is missing", () =>
  68. Effect.gen(function* () {
  69. const tmp = yield* Effect.acquireRelease(
  70. Effect.promise(() => tmpdir()),
  71. (tmp) => Effect.promise(() => tmp[Symbol.asyncDispose]()),
  72. )
  73. yield* Effect.promise(() => initRepo(tmp.path, { commit: true }))
  74. const project = yield* ProjectV2.Service
  75. const result = yield* project.resolve(abs(tmp.path))
  76. expect(result.id).toBe(ProjectV2.ID.make(yield* Effect.promise(() => rootCommit(tmp.path))))
  77. expect(result.directory).toBe(yield* real(tmp.path))
  78. expect(result.previous).toBeUndefined()
  79. expect(result.vcs?.type).toBe("git")
  80. }),
  81. )
  82. it.live("prefers normalized origin over root commit", () =>
  83. Effect.gen(function* () {
  84. const tmp = yield* Effect.acquireRelease(
  85. Effect.promise(() => tmpdir()),
  86. (tmp) => Effect.promise(() => tmp[Symbol.asyncDispose]()),
  87. )
  88. yield* Effect.promise(() => initRepo(tmp.path, { commit: true, remote: "git@github.com:Acme/App.git" }))
  89. const project = yield* ProjectV2.Service
  90. const result = yield* project.resolve(abs(tmp.path))
  91. expect(result.id).toBe(remoteID("github.com/Acme/App"))
  92. expect(result.id).not.toBe(ProjectV2.ID.make(yield* Effect.promise(() => rootCommit(tmp.path))))
  93. expect(result.directory).toBe(yield* real(tmp.path))
  94. expect(result.vcs?.type).toBe("git")
  95. }),
  96. )
  97. it.live("normalizes ssh and https remotes to the same id", () =>
  98. Effect.gen(function* () {
  99. const ssh = yield* Effect.acquireRelease(
  100. Effect.promise(() => tmpdir()),
  101. (tmp) => Effect.promise(() => tmp[Symbol.asyncDispose]()),
  102. )
  103. const https = yield* Effect.acquireRelease(
  104. Effect.promise(() => tmpdir()),
  105. (tmp) => Effect.promise(() => tmp[Symbol.asyncDispose]()),
  106. )
  107. yield* Effect.promise(() => initRepo(ssh.path, { commit: true, remote: "git@github.com:owner/repo.git" }))
  108. yield* Effect.promise(() => initRepo(https.path, { commit: true, remote: "https://github.com/owner/repo.git" }))
  109. const project = yield* ProjectV2.Service
  110. const a = yield* project.resolve(abs(ssh.path))
  111. const b = yield* project.resolve(abs(https.path))
  112. expect(a.id).toBe(remoteID("github.com/owner/repo"))
  113. expect(b.id).toBe(a.id)
  114. }),
  115. )
  116. it.live("ignores file remotes and falls back to root commit", () =>
  117. Effect.gen(function* () {
  118. const tmp = yield* Effect.acquireRelease(
  119. Effect.promise(() => tmpdir()),
  120. (tmp) => Effect.promise(() => tmp[Symbol.asyncDispose]()),
  121. )
  122. yield* Effect.promise(() => initRepo(tmp.path, { commit: true, remote: `file://${tmp.path}` }))
  123. const project = yield* ProjectV2.Service
  124. const result = yield* project.resolve(abs(tmp.path))
  125. expect(result.id).toBe(ProjectV2.ID.make(yield* Effect.promise(() => rootCommit(tmp.path))))
  126. }),
  127. )
  128. it.live("returns previous cached id from common dir", () =>
  129. Effect.gen(function* () {
  130. const tmp = yield* Effect.acquireRelease(
  131. Effect.promise(() => tmpdir()),
  132. (tmp) => Effect.promise(() => tmp[Symbol.asyncDispose]()),
  133. )
  134. yield* Effect.promise(() => initRepo(tmp.path, { commit: true, remote: "git@github.com:owner/repo.git" }))
  135. yield* Effect.promise(() => Bun.write(path.join(tmp.path, ".git", "opencode"), "old-id"))
  136. const project = yield* ProjectV2.Service
  137. const result = yield* project.resolve(abs(tmp.path))
  138. expect(result.previous).toBe(ProjectV2.ID.make("old-id"))
  139. expect(result.id).toBe(remoteID("github.com/owner/repo"))
  140. }),
  141. )
  142. it.live("does not write the cache while resolving", () =>
  143. Effect.gen(function* () {
  144. const tmp = yield* Effect.acquireRelease(
  145. Effect.promise(() => tmpdir()),
  146. (tmp) => Effect.promise(() => tmp[Symbol.asyncDispose]()),
  147. )
  148. yield* Effect.promise(() => initRepo(tmp.path, { commit: true, remote: "git@github.com:owner/repo.git" }))
  149. const project = yield* ProjectV2.Service
  150. yield* project.resolve(abs(tmp.path))
  151. expect(yield* Effect.promise(() => Bun.file(path.join(tmp.path, ".git", "opencode")).exists())).toBe(false)
  152. }),
  153. )
  154. it.live("resolves from nested directories to repo root", () =>
  155. Effect.gen(function* () {
  156. const tmp = yield* Effect.acquireRelease(
  157. Effect.promise(() => tmpdir()),
  158. (tmp) => Effect.promise(() => tmp[Symbol.asyncDispose]()),
  159. )
  160. yield* Effect.promise(() => initRepo(tmp.path, { commit: true }))
  161. yield* Effect.promise(() => fs.mkdir(path.join(tmp.path, "a", "b"), { recursive: true }))
  162. const project = yield* ProjectV2.Service
  163. const result = yield* project.resolve(abs(path.join(tmp.path, "a", "b")))
  164. expect(result.directory).toBe(yield* real(tmp.path))
  165. }),
  166. )
  167. it.live("linked worktree returns opened worktree directory and previous from common dir", () =>
  168. Effect.gen(function* () {
  169. const tmp = yield* Effect.acquireRelease(
  170. Effect.promise(() => tmpdir()),
  171. (tmp) => Effect.promise(() => tmp[Symbol.asyncDispose]()),
  172. )
  173. const worktree = `${tmp.path}-worktree`
  174. yield* Effect.addFinalizer(() =>
  175. Effect.promise(() => $`rm -rf ${worktree}`.quiet().nothrow()).pipe(Effect.ignore),
  176. )
  177. yield* Effect.promise(() => initRepo(tmp.path, { commit: true, remote: "git@github.com:owner/repo.git" }))
  178. yield* Effect.promise(() => Bun.write(path.join(tmp.path, ".git", "opencode"), "old-id"))
  179. yield* Effect.promise(() => $`git worktree add ${worktree} -b test-${Date.now()}`.cwd(tmp.path).quiet())
  180. const project = yield* ProjectV2.Service
  181. const result = yield* project.resolve(abs(worktree))
  182. expect(result.directory).toBe(yield* real(worktree))
  183. expect(result.previous).toBe(ProjectV2.ID.make("old-id"))
  184. expect(result.id).toBe(remoteID("github.com/owner/repo"))
  185. expect(result.vcs?.type).toBe("git")
  186. }),
  187. )
  188. })