|
1 | 1 | import { describe, expect, test } from "bun:test" |
2 | 2 | import { Project } from "../../src/project/project" |
3 | 3 | import { Log } from "../../src/util/log" |
| 4 | +import { Storage } from "../../src/storage/storage" |
4 | 5 | import { $ } from "bun" |
5 | 6 | import path from "path" |
6 | 7 | import { tmpdir } from "../fixture/fixture" |
@@ -39,3 +40,79 @@ describe("Project.fromDirectory", () => { |
39 | 40 | expect(fileExists).toBe(true) |
40 | 41 | }) |
41 | 42 | }) |
| 43 | + |
| 44 | +describe("Project.discover", () => { |
| 45 | + test("should discover favicon.png in root", async () => { |
| 46 | + await using tmp = await tmpdir({ git: true }) |
| 47 | + const project = await Project.fromDirectory(tmp.path) |
| 48 | + |
| 49 | + const pngData = Buffer.from([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a]) |
| 50 | + await Bun.write(path.join(tmp.path, "favicon.png"), pngData) |
| 51 | + |
| 52 | + await Project.discover({ id: project.id, worktree: tmp.path }) |
| 53 | + |
| 54 | + const updated = await Storage.read<Project.Info>(["project", project.id]) |
| 55 | + expect(updated.icon).toBeDefined() |
| 56 | + expect(updated.icon?.url).toStartWith("data:") |
| 57 | + expect(updated.icon?.url).toContain("base64") |
| 58 | + expect(updated.icon?.color).toBe("#000000") |
| 59 | + }) |
| 60 | + |
| 61 | + test("should discover icon.svg in subdirectory", async () => { |
| 62 | + await using tmp = await tmpdir({ git: true }) |
| 63 | + const project = await Project.fromDirectory(tmp.path) |
| 64 | + |
| 65 | + await $`mkdir -p ${path.join(tmp.path, "public")}`.quiet() |
| 66 | + await Bun.write(path.join(tmp.path, "public", "icon.svg"), "<svg></svg>") |
| 67 | + |
| 68 | + await Project.discover({ id: project.id, worktree: tmp.path }) |
| 69 | + |
| 70 | + const updated = await Storage.read<Project.Info>(["project", project.id]) |
| 71 | + expect(updated.icon).toBeDefined() |
| 72 | + expect(updated.icon?.url).toStartWith("data:") |
| 73 | + expect(updated.icon?.url).toContain("base64") |
| 74 | + }) |
| 75 | + |
| 76 | + test("should discover logo.ico", async () => { |
| 77 | + await using tmp = await tmpdir({ git: true }) |
| 78 | + const project = await Project.fromDirectory(tmp.path) |
| 79 | + |
| 80 | + const icoData = Buffer.from([0x00, 0x00, 0x01, 0x00]) |
| 81 | + await Bun.write(path.join(tmp.path, "logo.ico"), icoData) |
| 82 | + |
| 83 | + await Project.discover({ id: project.id, worktree: tmp.path }) |
| 84 | + |
| 85 | + const updated = await Storage.read<Project.Info>(["project", project.id]) |
| 86 | + expect(updated.icon).toBeDefined() |
| 87 | + expect(updated.icon?.url).toStartWith("data:") |
| 88 | + }) |
| 89 | + |
| 90 | + test("should not discover non-image files", async () => { |
| 91 | + await using tmp = await tmpdir({ git: true }) |
| 92 | + const project = await Project.fromDirectory(tmp.path) |
| 93 | + |
| 94 | + await Bun.write(path.join(tmp.path, "favicon.txt"), "not an image") |
| 95 | + |
| 96 | + await Project.discover({ id: project.id, worktree: tmp.path }) |
| 97 | + |
| 98 | + const updated = await Storage.read<Project.Info>(["project", project.id]) |
| 99 | + expect(updated.icon).toBeUndefined() |
| 100 | + }) |
| 101 | + |
| 102 | + test("should preserve existing color when discovering icon", async () => { |
| 103 | + await using tmp = await tmpdir({ git: true }) |
| 104 | + const project = await Project.fromDirectory(tmp.path) |
| 105 | + |
| 106 | + await Storage.update<Project.Info>(["project", project.id], (draft) => { |
| 107 | + draft.icon = { url: "", color: "#ff0000" } |
| 108 | + }) |
| 109 | + |
| 110 | + const pngData = Buffer.from([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a]) |
| 111 | + await Bun.write(path.join(tmp.path, "favicon.png"), pngData) |
| 112 | + |
| 113 | + await Project.discover({ id: project.id, worktree: tmp.path }) |
| 114 | + |
| 115 | + const updated = await Storage.read<Project.Info>(["project", project.id]) |
| 116 | + expect(updated.icon?.color).toBe("#ff0000") |
| 117 | + }) |
| 118 | +}) |
0 commit comments