project.test.ts 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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 discover icon.svg in subdirectory", async () => {
  48. await using tmp = await tmpdir({ git: true })
  49. const project = await Project.fromDirectory(tmp.path)
  50. await $`mkdir -p ${path.join(tmp.path, "public")}`.quiet()
  51. await Bun.write(path.join(tmp.path, "public", "icon.svg"), "<svg></svg>")
  52. await Project.discover(project)
  53. const updated = await Storage.read<Project.Info>(["project", project.id])
  54. expect(updated.icon).toBeDefined()
  55. expect(updated.icon?.url).toStartWith("data:")
  56. expect(updated.icon?.url).toContain("base64")
  57. })
  58. test("should discover logo.ico", async () => {
  59. await using tmp = await tmpdir({ git: true })
  60. const project = await Project.fromDirectory(tmp.path)
  61. const icoData = Buffer.from([0x00, 0x00, 0x01, 0x00])
  62. await Bun.write(path.join(tmp.path, "logo.ico"), icoData)
  63. await Project.discover(project)
  64. const updated = await Storage.read<Project.Info>(["project", project.id])
  65. expect(updated.icon).toBeDefined()
  66. expect(updated.icon?.url).toStartWith("data:")
  67. })
  68. test("should not discover non-image files", async () => {
  69. await using tmp = await tmpdir({ git: true })
  70. const project = await Project.fromDirectory(tmp.path)
  71. await Bun.write(path.join(tmp.path, "favicon.txt"), "not an image")
  72. await Project.discover(project)
  73. const updated = await Storage.read<Project.Info>(["project", project.id])
  74. expect(updated.icon).toBeUndefined()
  75. })
  76. test("should preserve existing color when discovering icon", async () => {
  77. await using tmp = await tmpdir({ git: true })
  78. const project = await Project.fromDirectory(tmp.path)
  79. await Storage.update<Project.Info>(["project", project.id], (draft) => {
  80. draft.icon = { url: "", color: "#ff0000" }
  81. })
  82. const pngData = Buffer.from([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a])
  83. await Bun.write(path.join(tmp.path, "favicon.png"), pngData)
  84. await Project.discover(project)
  85. const updated = await Storage.read<Project.Info>(["project", project.id])
  86. expect(updated.icon?.color).toBe("#ff0000")
  87. })
  88. })