project.test.ts 7.9 KB

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