project.test.ts 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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.discover", () => {
  35. test("should discover favicon.png in root", async () => {
  36. await using tmp = await tmpdir({ git: true })
  37. const project = await Project.fromDirectory(tmp.path)
  38. const pngData = Buffer.from([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a])
  39. await Bun.write(path.join(tmp.path, "favicon.png"), pngData)
  40. await Project.discover(project)
  41. const updated = await Storage.read<Project.Info>(["project", project.id])
  42. expect(updated.icon).toBeDefined()
  43. expect(updated.icon?.url).toStartWith("data:")
  44. expect(updated.icon?.url).toContain("base64")
  45. expect(updated.icon?.color).toBeUndefined()
  46. })
  47. test("should not discover non-image files", async () => {
  48. await using tmp = await tmpdir({ git: true })
  49. const project = await Project.fromDirectory(tmp.path)
  50. await Bun.write(path.join(tmp.path, "favicon.txt"), "not an image")
  51. await Project.discover(project)
  52. const updated = await Storage.read<Project.Info>(["project", project.id])
  53. expect(updated.icon).toBeUndefined()
  54. })
  55. })