Files
paseo/packages/app/src/plugins/theme.test.ts
T
Mohamed BoudraandNathan Green 2a9395e2d1 Light plugin themes and Pure Black keep their selected and hovered rows readable (#9579)
* fix(app): derive theme states and plugin fills so light and Pure Black themes stay readable

Hover, selected, the neutral fill and tertiary text were colors a theme
picked by eye, and plugin palettes filled them by borrowing `ring` and
`border`. A light plugin theme got a dark selected row under dark text,
Pure Black's hover barely separated from its surface, and light terminal
ANSI white was white on white.

States are now fixed WCAG-contrast steps from the surface they sit on,
toward the foreground, derived in styles/derive-colors.ts. Plugin
themes derive their neutral fill and tertiary text instead of aliasing
`ring` and `border`, and may set an optional `sidebar` color. The web
focus outline follows the theme's `ring`, and light terminal white and
bright white are derived to read on the background. Tokens with no
consumers are removed.

Existing plugin themes change look with no author action, and the
composer placeholder uses foregroundMuted like every other placeholder.

Co-authored-by: Nathan Green <342098+nathansgreen@users.noreply.github.com>

* fix(app): accept alpha hex in plugin theme colors when deriving

The plugin schema accepts #rgba and #rrggbbaa, but contrast derivation parsed
only opaque hex and threw, which would crash rendering for such a theme.
Derivation now reads those colors as the opaque color they sit on.

* fix(app): keep derived plugin fills and tertiary text readable on low-contrast palettes

A fixed contrast step for the neutral fill left text on message bubbles, secondary
buttons and badges at 2.6:1 on a dark palette whose body text is 4.75:1, and the
fixed fade made Solarized Light tertiary text 1.92:1. The fill step is now bounded by
the text it carries and tertiary text has a floor. The plugin reference states the
minimum Paseo version for sidebar and that the terminal white is derived on light
themes only.

* fix(app): keep the derived neutral fill visible when a theme's own text is quiet

Bounding the fill by text contrast could reach a step of 1 or less on a palette whose
body text is under about 3:1, returning the page background itself. The step now has a
minimum.

* docs(app): state the trade-off between the neutral fill step and text contrast

Below about 3.8:1 body text the fill keeps its minimum step and text on it falls under
3:1. The comment now says so and a test pins both outcomes.

* fix(app): leave the light terminal's ANSI white as it was

ANSI white is both text and a cell background, so no single palette value is readable in
both roles; making it readable as text broke black text on white cells. That needs a
contrast rule in the terminal renderer, so light white and bright white return to their
previous values and the derivation and its test floors are removed.

* feat(plugins): make derived theme colors overridable

Sidebar passive text was derived against the page even though timestamps render on hover and selected surfaces. Derive it against the resolved state surface and let contributed themes override each derived semantic role explicitly.

---------

Co-authored-by: Nathan Green <342098+nathansgreen@users.noreply.github.com>
2026-11-14 10:35:43 +07:00

373 lines
13 KiB
TypeScript

import { PluginTranslationRuntime } from "@getpaseo/plugin/client/host";
import { QueryClient } from "@tanstack/react-query";
import type { PluginThemeContribution } from "@getpaseo/plugin";
import { describe, expect, it } from "vitest";
import { darkTheme, lightTheme } from "@/styles/theme";
import { contrastRatio } from "@/utils/color";
import {
buildPluginTheme,
collectPluginThemes,
parsePluginThemeContribution,
rememberPluginThemeHost,
} from "./themes";
import { toPluginTheme } from "./theme";
import type { InstalledPlugin } from "./types";
const MOCHA: PluginThemeContribution = {
id: "mocha",
name: "Catppuccin Mocha",
appearance: "dark",
colors: {
background: "#1e1e2e",
foreground: "#cdd6f4",
raised: "#313244",
control: "#45475a",
border: "#45475a",
accent: "#cba6f7",
mutedForeground: "#a6adc8",
ring: "#6c7086",
},
};
const MOCHA_FORK: PluginThemeContribution = {
...MOCHA,
colors: { ...MOCHA.colors, background: "#11111b", accent: "#f5c2e7" },
};
const LATTE: PluginThemeContribution = {
id: "latte",
name: "Catppuccin Latte",
appearance: "light",
colors: {
background: "#eff1f5",
foreground: "#4c4f69",
raised: "#e6e9ef",
control: "#dce0e8",
border: "#ccd0da",
accent: "#8839ef",
mutedForeground: "#6c6f85",
ring: "#9ca0b0",
},
};
function installed(serverId: string, themes: PluginThemeContribution[]): InstalledPlugin {
return {
id: "catppuccin",
cleanup: () => undefined,
serverId,
clientBundle: serverId,
lifetime: new AbortController(),
locale: "en",
i18n: new PluginTranslationRuntime({ getLocale: () => "en", subscribe: () => () => {} }),
queryClient: new QueryClient(),
paseo: {} as InstalledPlugin["paseo"],
invoke: async () => undefined,
subscribe: () => {
throw new Error("Subscriptions are not used here");
},
settingsScreens: [],
surfaces: [],
sidebarViews: [],
sidebarItems: { header: [], footer: [] },
legacySidebarItems: [],
workspaceRowBadgeItems: [],
filePanels: [],
composerItems: [],
newWorkspaceItems: [],
workspacePanels: [],
commandCenterItems: [],
clientSlashCommands: [],
attachmentSources: [],
themes,
timelineTransformers: [],
timelineRenderers: [],
};
}
describe("toPluginTheme", () => {
it("maps the app theme into the plugin color tokens", () => {
expect(toPluginTheme(lightTheme)).toEqual({
colors: {
surface0: lightTheme.colors.surface0,
surface1: lightTheme.colors.surface1,
surface2: lightTheme.colors.surface2,
border: lightTheme.colors.border,
foreground: lightTheme.colors.foreground,
foregroundMuted: lightTheme.colors.foregroundMuted,
accent: lightTheme.colors.accent,
accentForeground: lightTheme.colors.accentForeground,
statusSuccess: lightTheme.colors.statusSuccess,
statusWarning: lightTheme.colors.statusWarning,
statusDanger: lightTheme.colors.statusDanger,
},
});
});
});
describe("plugin theme palettes", () => {
it("expands a contributed palette onto the semantic and terminal tokens", () => {
const option = collectPluginThemes([installed("host-a", [MOCHA])], new Set(["host-a"]))[0];
const theme = option.theme;
expect(theme.colorScheme).toBe("dark");
expect(theme.colors).toMatchObject({
surface0: "#1e1e2e",
surface1: "#313244",
surface2: "#45475a",
surfaceSidebar: "#1e1e2e",
foreground: "#cdd6f4",
foregroundMuted: "#a6adc8",
border: "#45475a",
accent: "#cba6f7",
accentBright: "#cba6f7",
accentForeground: "#1e1e2e",
ring: "#6c7086",
terminal: {
background: "#1e1e2e",
foreground: "#cdd6f4",
cursor: "#cdd6f4",
cursorAccent: "#1e1e2e",
black: "#45475a",
brightBlack: "#6c7086",
},
});
});
it("carries the accent on the foreground when no accent is given", () => {
const { accent: _accent, ...colors } = MOCHA.colors;
const contribution = { ...MOCHA, colors };
const theme = collectPluginThemes([installed("host-a", [contribution])], new Set(["host-a"]))[0]
.theme;
expect(theme.colors.accent).toBe("#cdd6f4");
expect(theme.colors.accentBright).toBe("#cdd6f4");
});
it("keeps the status and syntax tokens the built-in dark themes use", () => {
const theme = collectPluginThemes([installed("host-a", [MOCHA])], new Set(["host-a"]))[0].theme;
expect(theme.colors.statusDanger).toBe(darkTheme.colors.statusDanger);
expect(theme.colors.syntax).toEqual(darkTheme.colors.syntax);
});
it("derives a complete light theme from the same palette contract", () => {
const theme = collectPluginThemes([installed("host-a", [LATTE])], new Set(["host-a"]))[0].theme;
expect(theme.colorScheme).toBe("light");
expect(theme.colors).toMatchObject({
surface0: "#eff1f5",
surface1: "#e6e9ef",
surface2: "#dce0e8",
surfaceSidebar: "#dce0e8",
foreground: "#4c4f69",
border: "#ccd0da",
accent: "#8839ef",
ring: "#9ca0b0",
terminal: {
background: "#eff1f5",
foreground: "#4c4f69",
black: "#4c4f69",
},
});
expect(theme.colors.statusDanger).toBe(lightTheme.colors.statusDanger);
expect(theme.colors.syntax).toEqual(lightTheme.colors.syntax);
});
});
function withColors(
contribution: PluginThemeContribution,
colors: Partial<PluginThemeContribution["colors"]>,
): PluginThemeContribution {
return { ...contribution, colors: { ...contribution.colors, ...colors } };
}
// A role only drives what it is named for. `ring` once filled the neutral fill, tertiary text and
// the light selected row, and `border` once filled the neutral fill, so a theme tuning either for
// its own purpose repainted unrelated surfaces.
describe("plugin theme roles", () => {
it("derives selected text against an overridden selected surface", () => {
const colors = buildPluginTheme(withColors(MOCHA, { sidebarSelected: "#68687c" })).colors;
expect(colors.surfaceSidebarSelected).toBe("#68687c");
expect(
contrastRatio(colors.foregroundSidebarSelected, colors.surfaceSidebarSelected),
).toBeGreaterThanOrEqual(2.5);
});
it("uses an explicit tertiary color unchanged on sidebar states", () => {
const colors = buildPluginTheme(withColors(MOCHA, { foregroundExtraMuted: "#123456" })).colors;
expect(colors.foregroundSidebarHover).toBe("#123456");
expect(colors.foregroundSidebarSelected).toBe("#123456");
});
it("keeps explicit semantic colors independent of ring and border", () => {
const overrides = {
sidebarHover: "#111111",
sidebarSelected: "#222222",
surface3: "#333333",
surface4: "#444444",
foregroundExtraMuted: "#aaaaaa",
};
const before = buildPluginTheme(withColors(MOCHA, overrides)).colors;
const after = buildPluginTheme(
withColors(MOCHA, { ...overrides, ring: "#ff0000", border: "#00ff00" }),
).colors;
for (const role of [
"surfaceSidebarHover",
"surfaceSidebarSelected",
"surface3",
"surface4",
"foregroundExtraMuted",
"foregroundSidebarHover",
"foregroundSidebarSelected",
] as const) {
expect(after[role]).toBe(before[role]);
}
});
it.each([
["sidebarHover", "surfaceSidebarHover"],
["sidebarSelected", "surfaceSidebarSelected"],
["surface3", "surface3"],
["surface4", "surface4"],
["foregroundExtraMuted", "foregroundExtraMuted"],
] as const)("accepts and applies %s independently", (input, output) => {
const contribution = withColors(MOCHA, { [input]: "#123456" });
const parsed = parsePluginThemeContribution(contribution);
const colors = buildPluginTheme(parsed).colors;
expect(colors[output]).toBe("#123456");
expect(() => parsePluginThemeContribution(withColors(MOCHA, { [input]: "no" }))).toThrow();
const baseline = buildPluginTheme(MOCHA).colors;
for (const other of [
"surfaceSidebarHover",
"surfaceSidebarSelected",
"surface3",
"surface4",
"foregroundExtraMuted",
] as const) {
if (other !== output) expect(colors[other]).toBe(baseline[other]);
}
});
it.each([LATTE, MOCHA])("keeps `ring` out of surfaces and text in $name", (contribution) => {
const base = buildPluginTheme(contribution).colors;
const changed = buildPluginTheme(withColors(contribution, { ring: "#ff0000" })).colors;
expect(changed.ring).toBe("#ff0000");
expect(changed.terminal.brightBlack).toBe("#ff0000");
for (const token of [
"surface3",
"surface4",
"foregroundExtraMuted",
"foregroundSidebarHover",
"foregroundSidebarSelected",
"surfaceSidebar",
"surfaceSidebarHover",
"surfaceSidebarSelected",
] as const) {
expect(changed[token], token).toBe(base[token]);
}
});
it.each([LATTE, MOCHA])("keeps `border` out of the neutral fill in $name", (contribution) => {
const base = buildPluginTheme(contribution).colors;
const changed = buildPluginTheme(withColors(contribution, { border: "#ff0000" })).colors;
expect(changed.border).toBe("#ff0000");
expect(changed.surface3).toBe(base.surface3);
expect(changed.surface4).toBe(base.surface4);
});
it("builds a theme from colors that carry an alpha channel", () => {
const theme = buildPluginTheme(
withColors(MOCHA, {
background: "#1e1e2eff",
foreground: "#cdd6f4ff",
mutedForeground: "#a6adc8ff",
}),
);
expect(theme.colors.surface3).toBe(buildPluginTheme(MOCHA).colors.surface3);
});
it("paints the sidebar with `sidebar` when the theme gives one", () => {
const withSidebar = buildPluginTheme(withColors(MOCHA, { sidebar: "#11111b" })).colors;
expect(withSidebar.surfaceSidebar).toBe("#11111b");
expect(withSidebar.surfaceSidebarHover).not.toBe("#11111b");
expect(withSidebar.surfaceSidebarSelected).not.toBe(withSidebar.surfaceSidebarHover);
});
it("leaves the sidebar where it was when the theme gives none", () => {
expect(buildPluginTheme(MOCHA).colors.surfaceSidebar).toBe(MOCHA.colors.background);
expect(buildPluginTheme(LATTE).colors.surfaceSidebar).toBe(LATTE.colors.control);
});
it("accepts an optional hex `sidebar` and rejects unknown colors", () => {
expect(parsePluginThemeContribution(withColors(MOCHA, { sidebar: "#11111b" }))).toMatchObject({
colors: { sidebar: "#11111b" },
});
expect(() => parsePluginThemeContribution(withColors(MOCHA, { sidebar: "darker" }))).toThrow();
expect(() =>
parsePluginThemeContribution({
...MOCHA,
colors: { ...MOCHA.colors, unknownColor: "#000000" },
}),
).toThrow();
});
});
const SUPPORTED = new Set(["host-a", "host-b", "host-z"]);
describe("collectPluginThemes", () => {
it("coalesces the same contribution across hosts", () => {
const options = collectPluginThemes(
[installed("host-a", [MOCHA]), installed("host-b", [MOCHA])],
new Set(["host-a", "host-b"]),
);
expect(options.map((option) => option.id)).toEqual(["catppuccin/theme/mocha"]);
});
// COMPAT(pluginThemes): a daemon without the capability keeps `addTheme` in the server bundle
// it compiles, so its themes are not offered at all.
it("drops themes from a host that predates the pluginThemes capability", () => {
expect(collectPluginThemes([installed("old-host", [MOCHA])], new Set())).toEqual([]);
});
it("keeps themes from the supported host when one peer is too old", () => {
const options = collectPluginThemes(
[installed("new-host", [MOCHA]), installed("old-host", [MOCHA])],
new Set(["new-host"]),
);
expect(options.map((option) => option.id)).toEqual(["catppuccin/theme/mocha"]);
});
// Two hosts, same plugin and theme id, different palettes. The picked host has to survive a peer
// connecting or dropping, or the app repaints with no settings change. The remembered host is
// module state, so this runs the whole sequence in one test rather than leaning on test order.
it("pins the palette to the picked host across peer connect and disconnect", () => {
const bothHosts = () =>
collectPluginThemes(
[installed("host-a", [MOCHA]), installed("host-z", [MOCHA_FORK])],
SUPPORTED,
);
const beforePick = bothHosts();
expect(beforePick).toHaveLength(1);
expect(beforePick[0]?.serverId).toBe("host-a");
expect(beforePick[0]?.theme.colors.surface0).toBe("#1e1e2e");
const onlyHostZ = collectPluginThemes([installed("host-z", [MOCHA_FORK])], SUPPORTED);
expect(onlyHostZ[0]).toBeDefined();
rememberPluginThemeHost(onlyHostZ[0]);
const afterPick = bothHosts();
expect(afterPick).toHaveLength(1);
expect(afterPick[0]?.serverId).toBe("host-z");
expect(afterPick[0]?.theme.colors.surface0).toBe("#11111b");
const withoutHostZ = collectPluginThemes([installed("host-a", [MOCHA])], SUPPORTED);
expect(withoutHostZ[0]?.serverId).toBe("host-a");
expect(withoutHostZ[0]?.theme.colors.surface0).toBe("#1e1e2e");
});
});