project.test.ts 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. import { describe, expect, mock, test } from "bun:test"
  2. import type { Project as ProjectNS } from "../../src/project/project"
  3. import { Log } from "../../src/util/log"
  4. import { Storage } from "../../src/storage/storage"
  5. import { $ } from "bun"
  6. import path from "path"
  7. import { tmpdir } from "../fixture/fixture"
  8. Log.init({ print: false })
  9. const gitModule = await import("../../src/util/git")
  10. const originalGit = gitModule.git
  11. type Mode = "none" | "rev-list-fail" | "top-fail" | "common-dir-fail"
  12. let mode: Mode = "none"
  13. mock.module("../../src/util/git", () => ({
  14. git: (args: string[], opts: { cwd: string; env?: Record<string, string> }) => {
  15. const cmd = ["git", ...args].join(" ")
  16. if (
  17. mode === "rev-list-fail" &&
  18. cmd.includes("git rev-list") &&
  19. cmd.includes("--max-parents=0") &&
  20. cmd.includes("--all")
  21. ) {
  22. return Promise.resolve({
  23. exitCode: 128,
  24. text: () => Promise.resolve(""),
  25. stdout: Buffer.from(""),
  26. stderr: Buffer.from("fatal"),
  27. })
  28. }
  29. if (mode === "top-fail" && cmd.includes("git rev-parse") && cmd.includes("--show-toplevel")) {
  30. return Promise.resolve({
  31. exitCode: 128,
  32. text: () => Promise.resolve(""),
  33. stdout: Buffer.from(""),
  34. stderr: Buffer.from("fatal"),
  35. })
  36. }
  37. if (mode === "common-dir-fail" && cmd.includes("git rev-parse") && cmd.includes("--git-common-dir")) {
  38. return Promise.resolve({
  39. exitCode: 128,
  40. text: () => Promise.resolve(""),
  41. stdout: Buffer.from(""),
  42. stderr: Buffer.from("fatal"),
  43. })
  44. }
  45. return originalGit(args, opts)
  46. },
  47. }))
  48. async function withMode(next: Mode, run: () => Promise<void>) {
  49. const prev = mode
  50. mode = next
  51. try {
  52. await run()
  53. } finally {
  54. mode = prev
  55. }
  56. }
  57. async function loadProject() {
  58. return (await import("../../src/project/project")).Project
  59. }
  60. describe("Project.fromDirectory", () => {
  61. test("should handle git repository with no commits", async () => {
  62. const p = await loadProject()
  63. await using tmp = await tmpdir()
  64. await $`git init`.cwd(tmp.path).quiet()
  65. const { project } = await p.fromDirectory(tmp.path)
  66. expect(project).toBeDefined()
  67. expect(project.id).toBe("global")
  68. expect(project.vcs).toBe("git")
  69. expect(project.worktree).toBe(tmp.path)
  70. const opencodeFile = path.join(tmp.path, ".git", "opencode")
  71. const fileExists = await Bun.file(opencodeFile).exists()
  72. expect(fileExists).toBe(false)
  73. })
  74. test("should handle git repository with commits", async () => {
  75. const p = await loadProject()
  76. await using tmp = await tmpdir({ git: true })
  77. const { project } = await p.fromDirectory(tmp.path)
  78. expect(project).toBeDefined()
  79. expect(project.id).not.toBe("global")
  80. expect(project.vcs).toBe("git")
  81. expect(project.worktree).toBe(tmp.path)
  82. const opencodeFile = path.join(tmp.path, ".git", "opencode")
  83. const fileExists = await Bun.file(opencodeFile).exists()
  84. expect(fileExists).toBe(true)
  85. })
  86. test("keeps git vcs when rev-list exits non-zero with empty output", async () => {
  87. const p = await loadProject()
  88. await using tmp = await tmpdir()
  89. await $`git init`.cwd(tmp.path).quiet()
  90. await withMode("rev-list-fail", async () => {
  91. const { project } = await p.fromDirectory(tmp.path)
  92. expect(project.vcs).toBe("git")
  93. expect(project.id).toBe("global")
  94. expect(project.worktree).toBe(tmp.path)
  95. })
  96. })
  97. test("keeps git vcs when show-toplevel exits non-zero with empty output", async () => {
  98. const p = await loadProject()
  99. await using tmp = await tmpdir({ git: true })
  100. await withMode("top-fail", async () => {
  101. const { project, sandbox } = await p.fromDirectory(tmp.path)
  102. expect(project.vcs).toBe("git")
  103. expect(project.worktree).toBe(tmp.path)
  104. expect(sandbox).toBe(tmp.path)
  105. })
  106. })
  107. test("keeps git vcs when git-common-dir exits non-zero with empty output", async () => {
  108. const p = await loadProject()
  109. await using tmp = await tmpdir({ git: true })
  110. await withMode("common-dir-fail", async () => {
  111. const { project, sandbox } = await p.fromDirectory(tmp.path)
  112. expect(project.vcs).toBe("git")
  113. expect(project.worktree).toBe(tmp.path)
  114. expect(sandbox).toBe(tmp.path)
  115. })
  116. })
  117. })
  118. describe("Project.fromDirectory with worktrees", () => {
  119. test("should set worktree to root when called from root", async () => {
  120. const p = await loadProject()
  121. await using tmp = await tmpdir({ git: true })
  122. const { project, sandbox } = await p.fromDirectory(tmp.path)
  123. expect(project.worktree).toBe(tmp.path)
  124. expect(sandbox).toBe(tmp.path)
  125. expect(project.sandboxes).not.toContain(tmp.path)
  126. })
  127. test("should set worktree to root when called from a worktree", async () => {
  128. const p = await loadProject()
  129. await using tmp = await tmpdir({ git: true })
  130. const worktreePath = path.join(tmp.path, "..", "worktree-test")
  131. await $`git worktree add ${worktreePath} -b test-branch`.cwd(tmp.path).quiet()
  132. const { project, sandbox } = await p.fromDirectory(worktreePath)
  133. expect(project.worktree).toBe(tmp.path)
  134. expect(sandbox).toBe(worktreePath)
  135. expect(project.sandboxes).toContain(worktreePath)
  136. expect(project.sandboxes).not.toContain(tmp.path)
  137. await $`git worktree remove ${worktreePath}`.cwd(tmp.path).quiet()
  138. })
  139. test("should accumulate multiple worktrees in sandboxes", async () => {
  140. const p = await loadProject()
  141. await using tmp = await tmpdir({ git: true })
  142. const worktree1 = path.join(tmp.path, "..", "worktree-1")
  143. const worktree2 = path.join(tmp.path, "..", "worktree-2")
  144. await $`git worktree add ${worktree1} -b branch-1`.cwd(tmp.path).quiet()
  145. await $`git worktree add ${worktree2} -b branch-2`.cwd(tmp.path).quiet()
  146. await p.fromDirectory(worktree1)
  147. const { project } = await p.fromDirectory(worktree2)
  148. expect(project.worktree).toBe(tmp.path)
  149. expect(project.sandboxes).toContain(worktree1)
  150. expect(project.sandboxes).toContain(worktree2)
  151. expect(project.sandboxes).not.toContain(tmp.path)
  152. await $`git worktree remove ${worktree1}`.cwd(tmp.path).quiet()
  153. await $`git worktree remove ${worktree2}`.cwd(tmp.path).quiet()
  154. })
  155. })
  156. describe("Project.discover", () => {
  157. test("should discover favicon.png in root", async () => {
  158. const p = await loadProject()
  159. await using tmp = await tmpdir({ git: true })
  160. const { project } = await p.fromDirectory(tmp.path)
  161. const pngData = Buffer.from([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a])
  162. await Bun.write(path.join(tmp.path, "favicon.png"), pngData)
  163. await p.discover(project)
  164. const updated = await Storage.read<ProjectNS.Info>(["project", project.id])
  165. expect(updated.icon).toBeDefined()
  166. expect(updated.icon?.url).toStartWith("data:")
  167. expect(updated.icon?.url).toContain("base64")
  168. expect(updated.icon?.color).toBeUndefined()
  169. })
  170. test("should not discover non-image files", async () => {
  171. const p = await loadProject()
  172. await using tmp = await tmpdir({ git: true })
  173. const { project } = await p.fromDirectory(tmp.path)
  174. await Bun.write(path.join(tmp.path, "favicon.txt"), "not an image")
  175. await p.discover(project)
  176. const updated = await Storage.read<ProjectNS.Info>(["project", project.id])
  177. expect(updated.icon).toBeUndefined()
  178. })
  179. })