project.test.ts 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  1. import { describe, expect, 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 { GlobalBus } from "../../src/bus/global"
  8. import { ProjectID } from "../../src/project/schema"
  9. import { Effect, Layer, Stream } from "effect"
  10. import { ChildProcess, ChildProcessSpawner } from "effect/unstable/process"
  11. import { NodePath } from "@effect/platform-node"
  12. import { AppFileSystem } from "../../src/filesystem"
  13. import * as CrossSpawnSpawner from "../../src/effect/cross-spawn-spawner"
  14. Log.init({ print: false })
  15. const encoder = new TextEncoder()
  16. function run<A>(fn: (svc: Project.Interface) => Effect.Effect<A>, layer = Project.defaultLayer) {
  17. return Effect.runPromise(
  18. Effect.gen(function* () {
  19. const svc = yield* Project.Service
  20. return yield* fn(svc)
  21. }).pipe(Effect.provide(layer)),
  22. )
  23. }
  24. /**
  25. * Creates a mock ChildProcessSpawner layer that intercepts git subcommands
  26. * matching `failArg` and returns exit code 128, while delegating everything
  27. * else to the real CrossSpawnSpawner.
  28. */
  29. function mockGitFailure(failArg: string) {
  30. return Layer.effect(
  31. ChildProcessSpawner.ChildProcessSpawner,
  32. Effect.gen(function* () {
  33. const real = yield* ChildProcessSpawner.ChildProcessSpawner
  34. return ChildProcessSpawner.make(
  35. Effect.fnUntraced(function* (command) {
  36. const std = ChildProcess.isStandardCommand(command) ? command : undefined
  37. if (std?.command === "git" && std.args.some((a) => a === failArg)) {
  38. return ChildProcessSpawner.makeHandle({
  39. pid: ChildProcessSpawner.ProcessId(0),
  40. exitCode: Effect.succeed(ChildProcessSpawner.ExitCode(128)),
  41. isRunning: Effect.succeed(false),
  42. kill: () => Effect.void,
  43. stdin: { [Symbol.for("effect/Sink/TypeId")]: Symbol.for("effect/Sink/TypeId") } as any,
  44. stdout: Stream.empty,
  45. stderr: Stream.make(encoder.encode("fatal: simulated failure\n")),
  46. all: Stream.empty,
  47. getInputFd: () => ({ [Symbol.for("effect/Sink/TypeId")]: Symbol.for("effect/Sink/TypeId") }) as any,
  48. getOutputFd: () => Stream.empty,
  49. unref: Effect.succeed(Effect.void),
  50. })
  51. }
  52. return yield* real.spawn(command)
  53. }),
  54. )
  55. }),
  56. ).pipe(Layer.provide(CrossSpawnSpawner.defaultLayer))
  57. }
  58. function projectLayerWithFailure(failArg: string) {
  59. return Project.layer.pipe(
  60. Layer.provide(mockGitFailure(failArg)),
  61. Layer.provide(AppFileSystem.defaultLayer),
  62. Layer.provide(NodePath.layer),
  63. )
  64. }
  65. describe("Project.fromDirectory", () => {
  66. test("should handle git repository with no commits", async () => {
  67. await using tmp = await tmpdir()
  68. await $`git init`.cwd(tmp.path).quiet()
  69. const { project } = await run((svc) => svc.fromDirectory(tmp.path))
  70. expect(project).toBeDefined()
  71. expect(project.id).toBe(ProjectID.global)
  72. expect(project.vcs).toBe("git")
  73. expect(project.worktree).toBe(tmp.path)
  74. const opencodeFile = path.join(tmp.path, ".git", "opencode")
  75. expect(await Bun.file(opencodeFile).exists()).toBe(false)
  76. })
  77. test("should handle git repository with commits", async () => {
  78. await using tmp = await tmpdir({ git: true })
  79. const { project } = await run((svc) => svc.fromDirectory(tmp.path))
  80. expect(project).toBeDefined()
  81. expect(project.id).not.toBe(ProjectID.global)
  82. expect(project.vcs).toBe("git")
  83. expect(project.worktree).toBe(tmp.path)
  84. const kiloFile = path.join(tmp.path, ".git", "kilo")
  85. expect(await Bun.file(kiloFile).exists()).toBe(true)
  86. })
  87. test("returns global for non-git directory", async () => {
  88. await using tmp = await tmpdir()
  89. const { project } = await run((svc) => svc.fromDirectory(tmp.path))
  90. expect(project.id).toBe(ProjectID.global)
  91. })
  92. test("derives stable project ID from root commit", async () => {
  93. await using tmp = await tmpdir({ git: true })
  94. const { project: a } = await run((svc) => svc.fromDirectory(tmp.path))
  95. const { project: b } = await run((svc) => svc.fromDirectory(tmp.path))
  96. expect(b.id).toBe(a.id)
  97. })
  98. })
  99. describe("Project.fromDirectory git failure paths", () => {
  100. test("keeps vcs when rev-list exits non-zero (no commits)", async () => {
  101. await using tmp = await tmpdir()
  102. await $`git init`.cwd(tmp.path).quiet()
  103. // rev-list fails because HEAD doesn't exist yet — this is the natural scenario
  104. const { project } = await run((svc) => svc.fromDirectory(tmp.path))
  105. expect(project.vcs).toBe("git")
  106. expect(project.id).toBe(ProjectID.global)
  107. expect(project.worktree).toBe(tmp.path)
  108. })
  109. test("handles show-toplevel failure gracefully", async () => {
  110. await using tmp = await tmpdir({ git: true })
  111. const layer = projectLayerWithFailure("--show-toplevel")
  112. const { project, sandbox } = await run((svc) => svc.fromDirectory(tmp.path), layer)
  113. expect(project.worktree).toBe(tmp.path)
  114. expect(sandbox).toBe(tmp.path)
  115. })
  116. test("handles git-common-dir failure gracefully", async () => {
  117. await using tmp = await tmpdir({ git: true })
  118. const layer = projectLayerWithFailure("--git-common-dir")
  119. const { project, sandbox } = await run((svc) => svc.fromDirectory(tmp.path), layer)
  120. expect(project.worktree).toBe(tmp.path)
  121. expect(sandbox).toBe(tmp.path)
  122. })
  123. })
  124. describe("Project.fromDirectory with worktrees", () => {
  125. test("should set worktree to root when called from root", async () => {
  126. await using tmp = await tmpdir({ git: true })
  127. const { project, sandbox } = await run((svc) => svc.fromDirectory(tmp.path))
  128. expect(project.worktree).toBe(tmp.path)
  129. expect(sandbox).toBe(tmp.path)
  130. expect(project.sandboxes).not.toContain(tmp.path)
  131. })
  132. test("should set worktree to root when called from a worktree", async () => {
  133. await using tmp = await tmpdir({ git: true })
  134. const worktreePath = path.join(tmp.path, "..", path.basename(tmp.path) + "-worktree")
  135. try {
  136. await $`git worktree add ${worktreePath} -b test-branch-${Date.now()}`.cwd(tmp.path).quiet()
  137. const { project, sandbox } = await run((svc) => svc.fromDirectory(worktreePath))
  138. expect(project.worktree).toBe(tmp.path)
  139. expect(sandbox).toBe(worktreePath)
  140. expect(project.sandboxes).toContain(worktreePath)
  141. expect(project.sandboxes).not.toContain(tmp.path)
  142. } finally {
  143. await $`git worktree remove ${worktreePath}`
  144. .cwd(tmp.path)
  145. .quiet()
  146. .catch(() => {})
  147. }
  148. })
  149. test("worktree should share project ID with main repo", async () => {
  150. await using tmp = await tmpdir({ git: true })
  151. const { project: main } = await run((svc) => svc.fromDirectory(tmp.path))
  152. const worktreePath = path.join(tmp.path, "..", path.basename(tmp.path) + "-wt-shared")
  153. try {
  154. await $`git worktree add ${worktreePath} -b shared-${Date.now()}`.cwd(tmp.path).quiet()
  155. const { project: wt } = await run((svc) => svc.fromDirectory(worktreePath))
  156. expect(wt.id).toBe(main.id)
  157. // Cache should live in the common .git dir, not the worktree's .git file
  158. const cache = path.join(tmp.path, ".git", "kilo")
  159. const exists = await Bun.file(cache).exists()
  160. expect(exists).toBe(true)
  161. } finally {
  162. await $`git worktree remove ${worktreePath}`
  163. .cwd(tmp.path)
  164. .quiet()
  165. .catch(() => {})
  166. }
  167. })
  168. test("separate clones of the same repo should share project ID", async () => {
  169. await using tmp = await tmpdir({ git: true })
  170. // Create a bare remote, push, then clone into a second directory
  171. const bare = tmp.path + "-bare"
  172. const clone = tmp.path + "-clone"
  173. try {
  174. await $`git clone --bare ${tmp.path} ${bare}`.quiet()
  175. await $`git clone ${bare} ${clone}`.quiet()
  176. const { project: a } = await run((svc) => svc.fromDirectory(tmp.path))
  177. const { project: b } = await run((svc) => svc.fromDirectory(clone))
  178. expect(b.id).toBe(a.id)
  179. } finally {
  180. await $`rm -rf ${bare} ${clone}`.quiet().nothrow()
  181. }
  182. })
  183. test("should accumulate multiple worktrees in sandboxes", async () => {
  184. await using tmp = await tmpdir({ git: true })
  185. const worktree1 = path.join(tmp.path, "..", path.basename(tmp.path) + "-wt1")
  186. const worktree2 = path.join(tmp.path, "..", path.basename(tmp.path) + "-wt2")
  187. try {
  188. await $`git worktree add ${worktree1} -b branch-${Date.now()}`.cwd(tmp.path).quiet()
  189. await $`git worktree add ${worktree2} -b branch-${Date.now() + 1}`.cwd(tmp.path).quiet()
  190. await run((svc) => svc.fromDirectory(worktree1))
  191. const { project } = await run((svc) => svc.fromDirectory(worktree2))
  192. expect(project.worktree).toBe(tmp.path)
  193. expect(project.sandboxes).toContain(worktree1)
  194. expect(project.sandboxes).toContain(worktree2)
  195. expect(project.sandboxes).not.toContain(tmp.path)
  196. } finally {
  197. await $`git worktree remove ${worktree1}`
  198. .cwd(tmp.path)
  199. .quiet()
  200. .catch(() => {})
  201. await $`git worktree remove ${worktree2}`
  202. .cwd(tmp.path)
  203. .quiet()
  204. .catch(() => {})
  205. }
  206. })
  207. })
  208. describe("Project.discover", () => {
  209. test("should discover favicon.png in root", async () => {
  210. await using tmp = await tmpdir({ git: true })
  211. const { project } = await run((svc) => svc.fromDirectory(tmp.path))
  212. const pngData = Buffer.from([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a])
  213. await Bun.write(path.join(tmp.path, "favicon.png"), pngData)
  214. await run((svc) => svc.discover(project))
  215. const updated = Project.get(project.id)
  216. expect(updated).toBeDefined()
  217. expect(updated!.icon).toBeDefined()
  218. expect(updated!.icon?.url).toStartWith("data:")
  219. expect(updated!.icon?.url).toContain("base64")
  220. expect(updated!.icon?.color).toBeUndefined()
  221. })
  222. test("should not discover non-image files", async () => {
  223. await using tmp = await tmpdir({ git: true })
  224. const { project } = await run((svc) => svc.fromDirectory(tmp.path))
  225. await Bun.write(path.join(tmp.path, "favicon.txt"), "not an image")
  226. await run((svc) => svc.discover(project))
  227. const updated = Project.get(project.id)
  228. expect(updated).toBeDefined()
  229. expect(updated!.icon).toBeUndefined()
  230. })
  231. })
  232. describe("Project.update", () => {
  233. test("should update name", async () => {
  234. await using tmp = await tmpdir({ git: true })
  235. const { project } = await run((svc) => svc.fromDirectory(tmp.path))
  236. const updated = await run((svc) =>
  237. svc.update({
  238. projectID: project.id,
  239. name: "New Project Name",
  240. }),
  241. )
  242. expect(updated.name).toBe("New Project Name")
  243. const fromDb = Project.get(project.id)
  244. expect(fromDb?.name).toBe("New Project Name")
  245. })
  246. test("should update icon url", async () => {
  247. await using tmp = await tmpdir({ git: true })
  248. const { project } = await run((svc) => svc.fromDirectory(tmp.path))
  249. const updated = await run((svc) =>
  250. svc.update({
  251. projectID: project.id,
  252. icon: { url: "https://example.com/icon.png" },
  253. }),
  254. )
  255. expect(updated.icon?.url).toBe("https://example.com/icon.png")
  256. const fromDb = Project.get(project.id)
  257. expect(fromDb?.icon?.url).toBe("https://example.com/icon.png")
  258. })
  259. test("should update icon color", async () => {
  260. await using tmp = await tmpdir({ git: true })
  261. const { project } = await run((svc) => svc.fromDirectory(tmp.path))
  262. const updated = await run((svc) =>
  263. svc.update({
  264. projectID: project.id,
  265. icon: { color: "#ff0000" },
  266. }),
  267. )
  268. expect(updated.icon?.color).toBe("#ff0000")
  269. const fromDb = Project.get(project.id)
  270. expect(fromDb?.icon?.color).toBe("#ff0000")
  271. })
  272. test("should update commands", async () => {
  273. await using tmp = await tmpdir({ git: true })
  274. const { project } = await run((svc) => svc.fromDirectory(tmp.path))
  275. const updated = await run((svc) =>
  276. svc.update({
  277. projectID: project.id,
  278. commands: { start: "npm run dev" },
  279. }),
  280. )
  281. expect(updated.commands?.start).toBe("npm run dev")
  282. const fromDb = Project.get(project.id)
  283. expect(fromDb?.commands?.start).toBe("npm run dev")
  284. })
  285. test("should throw error when project not found", async () => {
  286. await expect(
  287. run((svc) =>
  288. svc.update({
  289. projectID: ProjectID.make("nonexistent-project-id"),
  290. name: "Should Fail",
  291. }),
  292. ),
  293. ).rejects.toThrow("Project not found: nonexistent-project-id")
  294. })
  295. test("should emit GlobalBus event on update", async () => {
  296. await using tmp = await tmpdir({ git: true })
  297. const { project } = await run((svc) => svc.fromDirectory(tmp.path))
  298. let eventPayload: any = null
  299. const on = (data: any) => {
  300. eventPayload = data
  301. }
  302. GlobalBus.on("event", on)
  303. try {
  304. await run((svc) => svc.update({ projectID: project.id, name: "Updated Name" }))
  305. expect(eventPayload).not.toBeNull()
  306. expect(eventPayload.payload.type).toBe("project.updated")
  307. expect(eventPayload.payload.properties.name).toBe("Updated Name")
  308. } finally {
  309. GlobalBus.off("event", on)
  310. }
  311. })
  312. test("should update multiple fields at once", async () => {
  313. await using tmp = await tmpdir({ git: true })
  314. const { project } = await run((svc) => svc.fromDirectory(tmp.path))
  315. const updated = await run((svc) =>
  316. svc.update({
  317. projectID: project.id,
  318. name: "Multi Update",
  319. icon: { url: "https://example.com/favicon.ico", color: "#00ff00" },
  320. commands: { start: "make start" },
  321. }),
  322. )
  323. expect(updated.name).toBe("Multi Update")
  324. expect(updated.icon?.url).toBe("https://example.com/favicon.ico")
  325. expect(updated.icon?.color).toBe("#00ff00")
  326. expect(updated.commands?.start).toBe("make start")
  327. })
  328. })
  329. describe("Project.list and Project.get", () => {
  330. test("list returns all projects", async () => {
  331. await using tmp = await tmpdir({ git: true })
  332. const { project } = await run((svc) => svc.fromDirectory(tmp.path))
  333. const all = Project.list()
  334. expect(all.length).toBeGreaterThan(0)
  335. expect(all.find((p) => p.id === project.id)).toBeDefined()
  336. })
  337. test("get returns project by id", async () => {
  338. await using tmp = await tmpdir({ git: true })
  339. const { project } = await run((svc) => svc.fromDirectory(tmp.path))
  340. const found = Project.get(project.id)
  341. expect(found).toBeDefined()
  342. expect(found!.id).toBe(project.id)
  343. })
  344. test("get returns undefined for unknown id", () => {
  345. const found = Project.get(ProjectID.make("nonexistent"))
  346. expect(found).toBeUndefined()
  347. })
  348. })
  349. describe("Project.setInitialized", () => {
  350. test("sets time_initialized on project", async () => {
  351. await using tmp = await tmpdir({ git: true })
  352. const { project } = await run((svc) => svc.fromDirectory(tmp.path))
  353. expect(project.time.initialized).toBeUndefined()
  354. Project.setInitialized(project.id)
  355. const updated = Project.get(project.id)
  356. expect(updated?.time.initialized).toBeDefined()
  357. })
  358. })
  359. describe("Project.addSandbox and Project.removeSandbox", () => {
  360. test("addSandbox adds directory and removeSandbox removes it", async () => {
  361. await using tmp = await tmpdir({ git: true })
  362. const { project } = await run((svc) => svc.fromDirectory(tmp.path))
  363. const sandboxDir = path.join(tmp.path, "sandbox-test")
  364. await run((svc) => svc.addSandbox(project.id, sandboxDir))
  365. let found = Project.get(project.id)
  366. expect(found?.sandboxes).toContain(sandboxDir)
  367. await run((svc) => svc.removeSandbox(project.id, sandboxDir))
  368. found = Project.get(project.id)
  369. expect(found?.sandboxes).not.toContain(sandboxDir)
  370. })
  371. test("addSandbox emits GlobalBus event", async () => {
  372. await using tmp = await tmpdir({ git: true })
  373. const { project } = await run((svc) => svc.fromDirectory(tmp.path))
  374. const sandboxDir = path.join(tmp.path, "sandbox-event")
  375. const events: any[] = []
  376. const on = (evt: any) => events.push(evt)
  377. GlobalBus.on("event", on)
  378. await run((svc) => svc.addSandbox(project.id, sandboxDir))
  379. GlobalBus.off("event", on)
  380. expect(events.some((e) => e.payload.type === Project.Event.Updated.type)).toBe(true)
  381. })
  382. })