Файли
openidea/src/workbench/projectIcon.test.ts
T
l0ng-ai 6cd463e9c1 feat(welcome): change any project's colour and icon from the project widget and Welcome screen (#5685)
Projects can show a custom icon instead of the coloured initials: an SVG or
PNG in `.openidea/icon.*` (or IntelliJ's `.idea/icon.*`, with `icon_dark.*`
used in dark themes). Change Project Icon… copies the chosen image there and
Reset Project Icon removes it.

The project context menu in the title bar project widget and on the Welcome
screen now offers Change Project Color…, Change Project Icon… and Reset
Project Icon for open projects as well as recent ones; the colour picker is
a workbench overlay so it opens from any of these places.

Fixes #5613
2026-11-25 05:36:17 +08:00

75 рядки
3.0 KiB
TypeScript

import { act, renderHook, waitFor } from "@testing-library/react";
import { afterEach, describe, expect, it, vi } from "vitest";
const files = vi.hoisted(() => new Map<string, string>());
const calls = vi.hoisted(() => [] as string[]);
vi.mock("@/platform/ipc", async (original) => {
const real = await original<typeof import("@/platform/ipc")>();
return {
...real,
commands: {
...real.commands,
fsReadImage: async (path: string) =>
files.has(path) ? { status: "ok", data: files.get(path)! } : { status: "error", error: { message: "missing" } },
fsDelete: async (path: string) => {
calls.push(`delete ${path}`);
files.delete(path);
return { status: "ok", data: null };
},
fsCopy: async (from: string, to: string) => {
calls.push(`copy ${from} ${to}`);
files.set(to, `data:image/svg+xml;base64,${from}`);
return { status: "ok", data: null };
},
},
};
});
const { hasCustomProjectIcon, projectIconCandidates, setProjectIcon, useProjectIcon } = await import("./projectIcon");
afterEach(() => {
files.clear();
calls.length = 0;
delete document.documentElement.dataset.theme;
});
describe("project icon", () => {
it("looks in .openidea, then IntelliJ's .idea; the dark variant first in a dark theme", () => {
expect(projectIconCandidates("/work/app", false)).toEqual([
"/work/app/.openidea/icon.svg",
"/work/app/.openidea/icon.png",
"/work/app/.idea/icon.svg",
"/work/app/.idea/icon.png",
]);
expect(projectIconCandidates("C:\\work\\app\\", true).slice(0, 2)).toEqual([
"C:\\work\\app\\.openidea\\icon_dark.svg",
"C:\\work\\app\\.openidea\\icon.svg",
]);
});
it("shows an IntelliJ project's icon, and one set by Change Project Icon… over it until reset", async () => {
files.set("/work/idea/.idea/icon.svg", "data:idea");
const { result } = renderHook(() => useProjectIcon("/work/idea"));
await waitFor(() => expect(result.current).toBe("data:idea"));
// IntelliJ's own icon is not ours to reset.
expect(hasCustomProjectIcon("/work/idea")).toBe(false);
await act(() => setProjectIcon("/work/idea", "/pics/logo.SVG"));
await waitFor(() => expect(result.current).toBe("data:image/svg+xml;base64,/pics/logo.SVG"));
expect(calls.at(-1)).toBe("copy /pics/logo.SVG /work/idea/.openidea/icon.svg");
expect(hasCustomProjectIcon("/work/idea")).toBe(true);
await act(() => setProjectIcon("/work/idea", null));
await waitFor(() => expect(result.current).toBe("data:idea"));
await expect(setProjectIcon("/work/idea", "/pics/logo.gif")).rejects.toThrow("SVG or PNG");
});
it("has none for a remote project or one without an icon", async () => {
const { result } = renderHook(() => useProjectIcon("ssh://host/work/app"));
const plain = renderHook(() => useProjectIcon("/work/plain"));
await waitFor(() => expect(plain.result.current).toBeNull());
expect(result.current).toBeNull();
});
});