Skip to content

The fastest, cheapest, most efficient open-source agent harness. Run more than 500 agents on a $10 VPS.

10.2 KB
Raw Blame
import { REHYDRATE } from 'redux-persist';
import { describe, expect, it } from 'vitest';

import type { Theme } from '../lib/theme/types';
import themeReducer, {
  clampFontSizePx,
  deleteCustomTheme,
  FONT_SIZE_PX,
  type FontSize,
  MAX_FONT_SIZE_PX,
  MIN_FONT_SIZE_PX,
  resetActiveTheme,
  selectEffectiveFontSizePx,
  selectEffectiveTheme,
  selectRootFontSizePx,
  setActiveTheme,
  setCustomFontSizePx,
  setFontRole,
  setFontSize,
  setTabBarLabels,
  setThemeMode,
  setThemeToken,
  upsertCustomTheme,
} from './themeSlice';

const customTheme = (overrides: Partial<Theme> = {}): Theme => ({
  id: 'custom-1',
  name: 'My theme',
  isDark: false,
  builtIn: false,
  colors: {},
  fonts: {},
  ...overrides,
});

describe('themeSlice', () => {
  it('defaults fontSize to medium', () => {
    const state = themeReducer(undefined, { type: '@@INIT' });
    expect(state.fontSize).toBe('medium');
  });

  it('updates fontSize via setFontSize', () => {
    let state = themeReducer(undefined, { type: '@@INIT' });
    state = themeReducer(state, setFontSize('large'));
    expect(state.fontSize).toBe('large');
    state = themeReducer(state, setFontSize('small'));
    expect(state.fontSize).toBe('small');
  });

  it('leaves mode and tabBarLabels untouched when only fontSize changes', () => {
    let state = themeReducer(undefined, { type: '@@INIT' });
    state = themeReducer(state, setThemeMode('dark'));
    state = themeReducer(state, setTabBarLabels('always'));
    state = themeReducer(state, setFontSize('xlarge'));
    expect(state).toEqual({
      mode: 'dark',
      tabBarLabels: 'always',
      fontSize: 'xlarge',
      customFontSizePx: null,
      developerMode: false,
      // setThemeMode('dark') syncs the variant; the active family is unchanged.
      activeThemeId: 'classic',
      themeVariant: 'dark',
      customThemes: [],
      layout: {
        borderAreas: { cards: true, controls: true, dividers: true, frame: true },
        borderContrast: 'default',
        corners: 'default',
      },
    });
  });

  it('maps every font size to a concrete px value', () => {
    const sizes: FontSize[] = ['small', 'medium', 'large', 'xlarge'];
    expect(sizes.map(size => FONT_SIZE_PX[size])).toEqual(['14px', '16px', '18px', '20px']);
  });

  it('keeps medium aligned with the historical 16px root size', () => {
    expect(FONT_SIZE_PX.medium).toBe('16px');
  });

  describe('custom (fine-tuned) font size — issue #4246', () => {
    it('defaults customFontSizePx to null (presets drive the root size)', () => {
      const state = themeReducer(undefined, { type: '@@INIT' });
      expect(state.customFontSizePx).toBeNull();
    });

    it('clampFontSizePx bounds, rounds, and guards against non-finite input', () => {
      expect(clampFontSizePx(MIN_FONT_SIZE_PX - 5)).toBe(MIN_FONT_SIZE_PX);
      expect(clampFontSizePx(MAX_FONT_SIZE_PX + 5)).toBe(MAX_FONT_SIZE_PX);
      expect(clampFontSizePx(17.6)).toBe(18);
      expect(clampFontSizePx(Number.NaN)).toBe(16);
    });

    it('setCustomFontSizePx stores a clamped override and null clears it', () => {
      let state = themeReducer(undefined, { type: '@@INIT' });
      state = themeReducer(state, setCustomFontSizePx(24));
      expect(state.customFontSizePx).toBe(24);
      state = themeReducer(state, setCustomFontSizePx(999));
      expect(state.customFontSizePx).toBe(MAX_FONT_SIZE_PX);
      state = themeReducer(state, setCustomFontSizePx(null));
      expect(state.customFontSizePx).toBeNull();
    });

    it('picking a preset clears any fine-tuned override', () => {
      let state = themeReducer(undefined, { type: '@@INIT' });
      state = themeReducer(state, setCustomFontSizePx(26));
      state = themeReducer(state, setFontSize('large'));
      expect(state.fontSize).toBe('large');
      expect(state.customFontSizePx).toBeNull();
    });

    it('selectRootFontSizePx: custom px wins, else the active preset', () => {
      let state = themeReducer(undefined, { type: '@@INIT' });
      state = themeReducer(state, setFontSize('large'));
      expect(selectRootFontSizePx({ theme: state })).toBe('18px');
      state = themeReducer(state, setCustomFontSizePx(23));
      expect(selectRootFontSizePx({ theme: state })).toBe('23px');
      expect(selectEffectiveFontSizePx({ theme: state })).toBe(23);
    });

    it('selectRootFontSizePx falls back to medium when the slice is absent', () => {
      expect(selectRootFontSizePx({})).toBe('16px');
      expect(selectEffectiveFontSizePx({})).toBe(16);
    });
  });

  describe('runtime themes', () => {
    it('defaults to the classic family with the system variant', () => {
      const state = themeReducer(undefined, { type: '@@INIT' });
      expect(state.activeThemeId).toBe('classic');
      expect(state.themeVariant).toBe('system');
      expect(state.customThemes).toEqual([]);
    });

    it('setThemeMode keeps mode and themeVariant in sync (family unchanged)', () => {
      let state = themeReducer(undefined, { type: '@@INIT' });
      state = themeReducer(state, setActiveTheme('ocean'));
      state = themeReducer(state, setThemeMode('light'));
      expect(state.themeVariant).toBe('light');
      expect(state.activeThemeId).toBe('ocean'); // family preserved
      state = themeReducer(state, setThemeMode('system'));
      expect(state.themeVariant).toBe('system');
    });

    it('migrates legacy persisted mode into themeVariant on rehydrate', () => {
      const state = themeReducer(undefined, {
        type: REHYDRATE,
        key: 'theme',
        payload: { mode: 'dark' },
      } as never);
      expect(state.themeVariant).toBe('dark');
      expect(selectEffectiveTheme({ theme: state }).id).toBe('dark');
    });

    it('leaves explicit persisted themeVariant for redux-persist reconciliation', () => {
      const state = themeReducer(undefined, {
        type: REHYDRATE,
        key: 'theme',
        payload: { mode: 'dark', themeVariant: 'light' },
      } as never);
      expect(state.themeVariant).toBe('system');
    });

    it('upserts a custom theme and makes it active', () => {
      let state = themeReducer(undefined, { type: '@@INIT' });
      state = themeReducer(state, upsertCustomTheme(customTheme()));
      expect(state.customThemes).toHaveLength(1);
      expect(state.activeThemeId).toBe('custom-1');
    });

    it('edits colour tokens and font roles only on the active custom theme', () => {
      let state = themeReducer(undefined, { type: '@@INIT' });
      state = themeReducer(state, upsertCustomTheme(customTheme()));
      state = themeReducer(state, setThemeToken({ key: 'surface', value: '10 20 30' }));
      state = themeReducer(state, setFontRole({ role: 'body', stack: 'Comic Sans' }));
      expect(state.customThemes[0].colors.surface).toBe('10 20 30');
      expect(state.customThemes[0].fonts.body).toBe('Comic Sans');
    });

    it('auto-forks a custom theme when editing a built-in preset', () => {
      let state = themeReducer(undefined, { type: '@@INIT' });
      state = themeReducer(state, setActiveTheme('dark'));
      state = themeReducer(state, setThemeToken({ key: 'surface', value: '0 0 0' }));
      expect(state.customThemes).toHaveLength(1);
      expect(state.customThemes[0].id).toBe('custom-dark');
      expect(state.activeThemeId).toBe('custom-dark');
      expect(state.customThemes[0].colors.surface).toBe('0 0 0');
      // A second edit reuses the same fork (no duplicate).
      state = themeReducer(state, setThemeToken({ key: 'content', value: '1 1 1' }));
      expect(state.customThemes).toHaveLength(1);
    });

    it('resets overrides on the active custom theme', () => {
      let state = themeReducer(undefined, { type: '@@INIT' });
      state = themeReducer(state, upsertCustomTheme(customTheme({ colors: { surface: '1 2 3' } })));
      state = themeReducer(state, resetActiveTheme());
      expect(state.customThemes[0].colors).toEqual({});
    });

    it('reset restores the source preset base for a preset fork (not empty)', () => {
      let state = themeReducer(undefined, { type: '@@INIT' });
      // Fork Ocean (light) by editing it, then reset.
      state = themeReducer(state, setActiveTheme('ocean'));
      state = themeReducer(state, setThemeToken({ key: 'surface', value: '0 0 0' }));
      const fork = state.customThemes[0];
      expect(fork.basedOn).toBe('ocean');
      state = themeReducer(state, resetActiveTheme());
      // Ocean's base palette is restored (it defines surface-canvas), not wiped.
      expect(state.customThemes[0].colors['surface-canvas']).toBe('233 242 252');
      expect(state.customThemes[0].colors.surface).not.toBe('0 0 0');
    });

    it('deletes a custom theme and falls back to the default family when active', () => {
      let state = themeReducer(undefined, { type: '@@INIT' });
      state = themeReducer(state, upsertCustomTheme(customTheme()));
      state = themeReducer(state, deleteCustomTheme('custom-1'));
      expect(state.customThemes).toEqual([]);
      expect(state.activeThemeId).toBe('classic');
    });

    it('selectEffectiveTheme resolves a custom theme by id', () => {
      let state = themeReducer(undefined, { type: '@@INIT' });
      state = themeReducer(state, upsertCustomTheme(customTheme({ id: 'custom-x', name: 'X' })));
      expect(selectEffectiveTheme({ theme: state }).id).toBe('custom-x');
    });

    it('applies the selected light/dark variant to custom themes', () => {
      let state = themeReducer(undefined, { type: '@@INIT' });
      state = themeReducer(
        state,
        upsertCustomTheme(
          customTheme({ id: 'custom-x', name: 'X', isDark: false, colors: { surface: '1 2 3' } })
        )
      );

      state = themeReducer(state, setThemeMode('dark'));
      expect(selectEffectiveTheme({ theme: state })).toMatchObject({
        id: 'custom-x',
        isDark: true,
        colors: { surface: '1 2 3' },
      });

      state = themeReducer(state, setThemeMode('light'));
      expect(selectEffectiveTheme({ theme: state }).isDark).toBe(false);
    });

    it('selectEffectiveTheme falls back to a preset for an unknown id', () => {
      let state = themeReducer(undefined, { type: '@@INIT' });
      state = themeReducer(state, setActiveTheme('does-not-exist'));
      // Falls back to the Light preset rather than leaving the UI unthemed.
      expect(selectEffectiveTheme({ theme: state }).id).toBe('light');
    });
  });
});