2
0

project.test.ts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. import { describe, expect, mock, test } from "bun:test"
  2. import { Project } from "../../src/project/project"
  3. import { Log } from "../../src/util/log"
  4. import { $ } from "bun"
  5. import path from "path"
  6. import { tmpdir } from "../fixture/fixture"
  7. import { Filesystem } from "../../src/util/filesystem"
  8. import { GlobalBus } from "../../src/bus/global"
  9. Log.init({ print: false })
  10. const gitModule = await import("../../src/util/git")
  11. const originalGit = gitModule.git
  12. type Mode = "none" | "rev-list-fail" | "top-fail" | "common-dir-fail"
  13. let mode: Mode = "none"
  14. mock.module("../../src/util/git", () => ({
  15. git: (args: string[], opts: { cwd: string; env?: Record<string, string> }) => {
  16. const cmd = ["git", ...args].join(" ")
  17. if (
  18. mode === "rev-list-fail" &&
  19. cmd.includes("git rev-list") &&
  20. cmd.includes("--max-parents=0") &&
  21. cmd.includes("--all")
  22. ) {
  23. return Promise.resolve({
  24. exitCode: 128,
  25. text: () => Promise.resolve(""),
  26. stdout: Buffer.from(""),
  27. stderr: Buffer.from("fatal"),
  28. })
  29. }
  30. if (mode === "top-fail" && cmd.includes("git rev-parse") && cmd.includes("--show-toplevel")) {
  31. return Promise.resolve({
  32. exitCode: 128,
  33. text: () => Promise.resolve(""),
  34. stdout: Buffer.from(""),
  35. stderr: Buffer.from("fatal"),
  36. })
  37. }
  38. if (mode === "common-dir-fail" && cmd.includes("git rev-parse") && cmd.includes("--git-common-dir")) {
  39. return Promise.resolve({
  40. exitCode: 128,
  41. text: () => Promise.resolve(""),
  42. stdout: Buffer.from(""),
  43. stderr: Buffer.from("fatal"),
  44. })
  45. }
  46. return originalGit(args, opts)
  47. },
  48. }))
  49. async function withMode(next: Mode, run: () => Promise<void>) {
  50. const prev = mode
  51. mode = next
  52. try {
  53. await run()
  54. } finally {
  55. mode = prev
  56. }
  57. }
  58. async function loadProject() {
  59. return (await import("../../src/project/project")).Project
  60. }
  61. describe("Project.fromDirectory", () => {
  62. test("should handle git repository with no commits", async () => {
  63. const p = await loadProject()
  64. await using tmp = await tmpdir()
  65. await $`git init`.cwd(tmp.path).quiet()
  66. const { project } = await p.fromDirectory(tmp.path)
  67. expect(project).toBeDefined()
  68. expect(project.id).toBe("global")
  69. expect(project.vcs).toBe("git")
  70. expect(project.worktree).toBe(tmp.path)
  71. const opencodeFile = path.join(tmp.path, ".git", "opencode")
  72. const fileExists = await Filesystem.exists(opencodeFile)
  73. expect(fileExists).toBe(false)
  74. })
  75. test("should handle git repository with commits", async () => {
  76. const p = await loadProject()
  77. await using tmp = await tmpdir({ git: true })
  78. const { project } = await p.fromDirectory(tmp.path)
  79. expect(project).toBeDefined()
  80. expect(project.id).not.toBe("global")
  81. expect(project.vcs).toBe("git")
  82. expect(project.worktree).toBe(tmp.path)
  83. const opencodeFile = path.join(tmp.path, ".git", "opencode")
  84. const fileExists = await Filesystem.exists(opencodeFile)
  85. expect(fileExists).toBe(true)
  86. })
  87. test("keeps git vcs when rev-list exits non-zero with empty output", async () => {
  88. const p = await loadProject()
  89. await using tmp = await tmpdir()
  90. await $`git init`.cwd(tmp.path).quiet()
  91. await withMode("rev-list-fail", async () => {
  92. const { project } = await p.fromDirectory(tmp.path)
  93. expect(project.vcs).toBe("git")
  94. expect(project.id).toBe("global")
  95. expect(project.worktree).toBe(tmp.path)
  96. })
  97. })
  98. test("keeps git vcs when show-toplevel exits non-zero with empty output", async () => {
  99. const p = await loadProject()
  100. await using tmp = await tmpdir({ git: true })
  101. await withMode("top-fail", async () => {
  102. const { project, sandbox } = await p.fromDirectory(tmp.path)
  103. expect(project.vcs).toBe("git")
  104. expect(project.worktree).toBe(tmp.path)
  105. expect(sandbox).toBe(tmp.path)
  106. })
  107. })
  108. test("keeps git vcs when git-common-dir exits non-zero with empty output", async () => {
  109. const p = await loadProject()
  110. await using tmp = await tmpdir({ git: true })
  111. await withMode("common-dir-fail", async () => {
  112. const { project, sandbox } = await p.fromDirectory(tmp.path)
  113. expect(project.vcs).toBe("git")
  114. expect(project.worktree).toBe(tmp.path)
  115. expect(sandbox).toBe(tmp.path)
  116. })
  117. })
  118. })
  119. describe("Project.fromDirectory with worktrees", () => {
  120. test("should set worktree to root when called from root", async () => {
  121. const p = await loadProject()
  122. await using tmp = await tmpdir({ git: true })
  123. const { project, sandbox } = await p.fromDirectory(tmp.path)
  124. expect(project.worktree).toBe(tmp.path)
  125. expect(sandbox).toBe(tmp.path)
  126. expect(project.sandboxes).not.toContain(tmp.path)
  127. })
  128. test("should set worktree to root when called from a worktree", async () => {
  129. const p = await loadProject()
  130. await using tmp = await tmpdir({ git: true })
  131. const worktreePath = path.join(tmp.path, "..", path.basename(tmp.path) + "-worktree")
  132. try {
  133. await $`git worktree add ${worktreePath} -b test-branch-${Date.now()}`.cwd(tmp.path).quiet()
  134. const { project, sandbox } = await p.fromDirectory(worktreePath)
  135. expect(project.worktree).toBe(tmp.path)
  136. expect(sandbox).toBe(worktreePath)
  137. expect(project.sandboxes).toContain(worktreePath)
  138. expect(project.sandboxes).not.toContain(tmp.path)
  139. } finally {
  140. await $`git worktree remove ${worktreePath}`
  141. .cwd(tmp.path)
  142. .quiet()
  143. .catch(() => {})
  144. }
  145. })
  146. test("should accumulate multiple worktrees in sandboxes", async () => {
  147. const p = await loadProject()
  148. await using tmp = await tmpdir({ git: true })
  149. const worktree1 = path.join(tmp.path, "..", path.basename(tmp.path) + "-wt1")
  150. const worktree2 = path.join(tmp.path, "..", path.basename(tmp.path) + "-wt2")
  151. try {
  152. await $`git worktree add ${worktree1} -b branch-${Date.now()}`.cwd(tmp.path).quiet()
  153. await $`git worktree add ${worktree2} -b branch-${Date.now() + 1}`.cwd(tmp.path).quiet()
  154. await p.fromDirectory(worktree1)
  155. const { project } = await p.fromDirectory(worktree2)
  156. expect(project.worktree).toBe(tmp.path)
  157. expect(project.sandboxes).toContain(worktree1)
  158. expect(project.sandboxes).toContain(worktree2)
  159. expect(project.sandboxes).not.toContain(tmp.path)
  160. } finally {
  161. await $`git worktree remove ${worktree1}`
  162. .cwd(tmp.path)
  163. .quiet()
  164. .catch(() => {})
  165. await $`git worktree remove ${worktree2}`
  166. .cwd(tmp.path)
  167. .quiet()
  168. .catch(() => {})
  169. }
  170. })
  171. })
  172. describe("Project.discover", () => {
  173. test("should discover favicon.png in root", async () => {
  174. const p = await loadProject()
  175. await using tmp = await tmpdir({ git: true })
  176. const { project } = await p.fromDirectory(tmp.path)
  177. const pngData = Buffer.from([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a])
  178. await Bun.write(path.join(tmp.path, "favicon.png"), pngData)
  179. await p.discover(project)
  180. const updated = Project.get(project.id)
  181. expect(updated).toBeDefined()
  182. expect(updated!.icon).toBeDefined()
  183. expect(updated!.icon?.url).toStartWith("data:")
  184. expect(updated!.icon?.url).toContain("base64")
  185. expect(updated!.icon?.color).toBeUndefined()
  186. })
  187. test("should not discover non-image files", async () => {
  188. const p = await loadProject()
  189. await using tmp = await tmpdir({ git: true })
  190. const { project } = await p.fromDirectory(tmp.path)
  191. await Bun.write(path.join(tmp.path, "favicon.txt"), "not an image")
  192. await p.discover(project)
  193. const updated = Project.get(project.id)
  194. expect(updated).toBeDefined()
  195. expect(updated!.icon).toBeUndefined()
  196. })
  197. })
  198. describe("Project.update", () => {
  199. test("should update name", async () => {
  200. await using tmp = await tmpdir({ git: true })
  201. const { project } = await Project.fromDirectory(tmp.path)
  202. const updated = await Project.update({
  203. projectID: project.id,
  204. name: "New Project Name",
  205. })
  206. expect(updated.name).toBe("New Project Name")
  207. const fromDb = Project.get(project.id)
  208. expect(fromDb?.name).toBe("New Project Name")
  209. })
  210. test("should update icon url", async () => {
  211. await using tmp = await tmpdir({ git: true })
  212. const { project } = await Project.fromDirectory(tmp.path)
  213. const updated = await Project.update({
  214. projectID: project.id,
  215. icon: { url: "https://example.com/icon.png" },
  216. })
  217. expect(updated.icon?.url).toBe("https://example.com/icon.png")
  218. const fromDb = Project.get(project.id)
  219. expect(fromDb?.icon?.url).toBe("https://example.com/icon.png")
  220. })
  221. test("should update icon color", async () => {
  222. await using tmp = await tmpdir({ git: true })
  223. const { project } = await Project.fromDirectory(tmp.path)
  224. const updated = await Project.update({
  225. projectID: project.id,
  226. icon: { color: "#ff0000" },
  227. })
  228. expect(updated.icon?.color).toBe("#ff0000")
  229. const fromDb = Project.get(project.id)
  230. expect(fromDb?.icon?.color).toBe("#ff0000")
  231. })
  232. test("should update commands", async () => {
  233. await using tmp = await tmpdir({ git: true })
  234. const { project } = await Project.fromDirectory(tmp.path)
  235. const updated = await Project.update({
  236. projectID: project.id,
  237. commands: { start: "npm run dev" },
  238. })
  239. expect(updated.commands?.start).toBe("npm run dev")
  240. const fromDb = Project.get(project.id)
  241. expect(fromDb?.commands?.start).toBe("npm run dev")
  242. })
  243. test("should throw error when project not found", async () => {
  244. await using tmp = await tmpdir({ git: true })
  245. await expect(
  246. Project.update({
  247. projectID: "nonexistent-project-id",
  248. name: "Should Fail",
  249. }),
  250. ).rejects.toThrow("Project not found: nonexistent-project-id")
  251. })
  252. test("should emit GlobalBus event on update", async () => {
  253. await using tmp = await tmpdir({ git: true })
  254. const { project } = await Project.fromDirectory(tmp.path)
  255. let eventFired = false
  256. let eventPayload: any = null
  257. GlobalBus.on("event", (data) => {
  258. eventFired = true
  259. eventPayload = data
  260. })
  261. await Project.update({
  262. projectID: project.id,
  263. name: "Updated Name",
  264. })
  265. expect(eventFired).toBe(true)
  266. expect(eventPayload.payload.type).toBe("project.updated")
  267. expect(eventPayload.payload.properties.name).toBe("Updated Name")
  268. })
  269. test("should update multiple fields at once", async () => {
  270. await using tmp = await tmpdir({ git: true })
  271. const { project } = await Project.fromDirectory(tmp.path)
  272. const updated = await Project.update({
  273. projectID: project.id,
  274. name: "Multi Update",
  275. icon: { url: "https://example.com/favicon.ico", color: "#00ff00" },
  276. commands: { start: "make start" },
  277. })
  278. expect(updated.name).toBe("Multi Update")
  279. expect(updated.icon?.url).toBe("https://example.com/favicon.ico")
  280. expect(updated.icon?.color).toBe("#00ff00")
  281. expect(updated.commands?.start).toBe("make start")
  282. })
  283. })