project.test.ts 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. import { describe, expect, test } from "bun:test"
  2. import { Project } 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. describe("Project.fromDirectory", () => {
  10. test("should handle git repository with no commits", async () => {
  11. await using tmp = await tmpdir()
  12. await $`git init`.cwd(tmp.path).quiet()
  13. const { project } = await Project.fromDirectory(tmp.path)
  14. expect(project).toBeDefined()
  15. expect(project.id).toBe("global")
  16. expect(project.vcs).toBe("git")
  17. expect(project.worktree).toBe(tmp.path)
  18. const opencodeFile = path.join(tmp.path, ".git", "opencode")
  19. const fileExists = await Bun.file(opencodeFile).exists()
  20. expect(fileExists).toBe(false)
  21. })
  22. test("should handle git repository with commits", async () => {
  23. await using tmp = await tmpdir({ git: true })
  24. const { project } = await Project.fromDirectory(tmp.path)
  25. expect(project).toBeDefined()
  26. expect(project.id).not.toBe("global")
  27. expect(project.vcs).toBe("git")
  28. expect(project.worktree).toBe(tmp.path)
  29. const opencodeFile = path.join(tmp.path, ".git", "opencode")
  30. const fileExists = await Bun.file(opencodeFile).exists()
  31. expect(fileExists).toBe(true)
  32. })
  33. })
  34. describe("Project.fromDirectory with worktrees", () => {
  35. test("should set worktree to root when called from root", async () => {
  36. await using tmp = await tmpdir({ git: true })
  37. const { project, sandbox } = await Project.fromDirectory(tmp.path)
  38. expect(project.worktree).toBe(tmp.path)
  39. expect(sandbox).toBe(tmp.path)
  40. expect(project.sandboxes).not.toContain(tmp.path)
  41. })
  42. test("should set worktree to root when called from a worktree", async () => {
  43. await using tmp = await tmpdir({ git: true })
  44. const worktreePath = path.join(tmp.path, "..", "worktree-test")
  45. await $`git worktree add ${worktreePath} -b test-branch`.cwd(tmp.path).quiet()
  46. const { project, sandbox } = await Project.fromDirectory(worktreePath)
  47. expect(project.worktree).toBe(tmp.path)
  48. expect(sandbox).toBe(worktreePath)
  49. expect(project.sandboxes).toContain(worktreePath)
  50. expect(project.sandboxes).not.toContain(tmp.path)
  51. await $`git worktree remove ${worktreePath}`.cwd(tmp.path).quiet()
  52. })
  53. test("should accumulate multiple worktrees in sandboxes", async () => {
  54. await using tmp = await tmpdir({ git: true })
  55. const worktree1 = path.join(tmp.path, "..", "worktree-1")
  56. const worktree2 = path.join(tmp.path, "..", "worktree-2")
  57. await $`git worktree add ${worktree1} -b branch-1`.cwd(tmp.path).quiet()
  58. await $`git worktree add ${worktree2} -b branch-2`.cwd(tmp.path).quiet()
  59. await Project.fromDirectory(worktree1)
  60. const { project } = await Project.fromDirectory(worktree2)
  61. expect(project.worktree).toBe(tmp.path)
  62. expect(project.sandboxes).toContain(worktree1)
  63. expect(project.sandboxes).toContain(worktree2)
  64. expect(project.sandboxes).not.toContain(tmp.path)
  65. await $`git worktree remove ${worktree1}`.cwd(tmp.path).quiet()
  66. await $`git worktree remove ${worktree2}`.cwd(tmp.path).quiet()
  67. })
  68. })
  69. describe("Project.discover", () => {
  70. test("should discover favicon.png in root", async () => {
  71. await using tmp = await tmpdir({ git: true })
  72. const { project } = await Project.fromDirectory(tmp.path)
  73. const pngData = Buffer.from([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a])
  74. await Bun.write(path.join(tmp.path, "favicon.png"), pngData)
  75. await Project.discover(project)
  76. const updated = await Storage.read<Project.Info>(["project", project.id])
  77. expect(updated.icon).toBeDefined()
  78. expect(updated.icon?.url).toStartWith("data:")
  79. expect(updated.icon?.url).toContain("base64")
  80. expect(updated.icon?.color).toBeUndefined()
  81. })
  82. test("should not discover non-image files", async () => {
  83. await using tmp = await tmpdir({ git: true })
  84. const { project } = await Project.fromDirectory(tmp.path)
  85. await Bun.write(path.join(tmp.path, "favicon.txt"), "not an image")
  86. await Project.discover(project)
  87. const updated = await Storage.read<Project.Info>(["project", project.id])
  88. expect(updated.icon).toBeUndefined()
  89. })
  90. })