genUiTheme.test.ts 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. import { afterEach, expect, it, vi } from "vitest";
  2. import { readGenUiStyleContext } from "@/gen-ui/adapters/theme.js";
  3. import { GEN_UI_STYLE_CONTEXT_KEY } from "@zcode/shared/gen-ui";
  4. import { McpUiHostContextSchema } from "@modelcontextprotocol/ext-apps";
  5. afterEach(() => vi.unstubAllGlobals());
  6. it("projects Gen UI controls separately from MCP accent and follows the host font", () => {
  7. const tokens: Record<string, string> = {
  8. "--ui-font-size": "18px",
  9. "--color-primary": "#111111",
  10. "--color-brand": "#3366ff",
  11. "--color-input": "#fafafa",
  12. "--color-input-border-focused": "#999999",
  13. "--radius-xl": "12px",
  14. };
  15. vi.stubGlobal("document", { documentElement: {} });
  16. vi.stubGlobal("getComputedStyle", () => ({
  17. getPropertyValue: (key: string) => tokens[key] ?? "",
  18. }));
  19. const context = readGenUiStyleContext();
  20. expect(McpUiHostContextSchema.safeParse(context).success).toBe(true);
  21. const variables = context.styles.variables;
  22. const controls = context[GEN_UI_STYLE_CONTEXT_KEY];
  23. expect(variables["--color-text-info"]).toBe("#3366ff");
  24. expect(controls["--gen-ui-primary"]).toBe("#111111");
  25. expect(controls["--gen-ui-input"]).toBe("#fafafa");
  26. expect(controls["--gen-ui-input-border-focused"]).toBe("#999999");
  27. expect(Object.keys(variables).some((key) => key.startsWith("--gen-ui-"))).toBe(false);
  28. expect(variables["--font-heading-xs-size"]).toBe("18px");
  29. expect(variables["--font-heading-sm-size"]).toBe("20px");
  30. expect(variables["--font-heading-md-size"]).toBe("22px");
  31. expect(variables["--border-radius-xl"]).toBe("12px");
  32. tokens["--ui-font-size"] = "14px";
  33. expect(readGenUiStyleContext().styles.variables["--font-heading-md-size"]).toBe("18px");
  34. });
  35. it("leaves standalone CSS fallbacks in charge when a Gen UI token is absent", () => {
  36. vi.stubGlobal("document", { documentElement: {} });
  37. vi.stubGlobal("getComputedStyle", () => ({ getPropertyValue: () => "" }));
  38. const context = readGenUiStyleContext();
  39. const variables = context.styles.variables;
  40. expect(context[GEN_UI_STYLE_CONTEXT_KEY]["--gen-ui-input"]).toBeUndefined();
  41. expect(variables["--font-text-md-size"]).toBe("14px");
  42. });