Skip to content

armgit / cindy

Public Imported from GitHub
0

Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。

6.3 KB
Raw Blame
// @vitest-environment jsdom
import React, { act, type ReactNode } from 'react';
import { afterEach, expect, it, vi } from 'vitest';
import {
  DEFAULT_APPEARANCE_SETTINGS,
  type AppearanceSettings,
} from '../../shared/appearanceSettings';

const h = vi.hoisted(() => ({ roots: [] as Array<{ unmount(): void }> }));
vi.mock('react-dom/client', async (original) => {
  const actual = await original<typeof import('react-dom/client')>();
  return {
    ...actual,
    createRoot: (...args: Parameters<typeof actual.createRoot>) => {
      const root = actual.createRoot(...args);
      h.roots.push(root);
      return root;
    },
  };
});
vi.mock('@/i18n', () => ({}));
vi.mock('../themes/colors', () => ({}));
vi.mock('../themes/local-themes', () => ({ bootstrapLocalThemesSync: vi.fn() }));
vi.mock('../themes/theme-service', () => ({ themeService: { applyTheme: vi.fn() } }));
vi.mock('../hooks/useTheme', () => ({ getInitialThemeVariant: () => ({ theme: 'test' }) }));
vi.mock('../hooks/useFontSettings', () => ({
  applyFontSettings: vi.fn(),
  getInitialFontSettings: vi.fn(),
}));
vi.mock('../hooks/useLocale', () => ({
  bootstrapInitialLocale: vi.fn(),
  LocaleProvider: ({ children }: { children: ReactNode }) => children,
}));
vi.mock('../components/error/TopLevelErrorBoundary', () => ({
  TopLevelErrorBoundary: ({ children }: { children: ReactNode }) => children,
}));
vi.mock('../components/ui/confirm-dialog-provider', () => ({
  ConfirmDialogProvider: ({ children }: { children: ReactNode }) => children,
}));
vi.mock('../contexts/AuthContext', () => ({
  AuthProvider: ({ children }: { children: ReactNode }) => children,
}));
vi.mock('../cindy-brain/ghostPanels', () => ({ ensureGhostPanelsRegistered: vi.fn() }));
vi.mock('../components/layout/SidebarWindowLayout', () => ({
  SidebarWindowLayout: () => <div>sidebar host</div>,
}));
vi.mock('../components/layout/SidebarTabWindowLayout', () => ({
  SidebarTabWindowLayout: () => <div>tab host</div>,
}));

afterEach(async () => {
  await act(async () => {
    for (const root of h.roots.splice(0)) root.unmount();
  });
  document.body.innerHTML = '';
  const gateWindow = window as typeof window & {
    __xdtHiddenAnimationGateDisposer?: () => void;
    __xdtHiddenAnimationGateLastHidden?: boolean;
  };
  gateWindow.__xdtHiddenAnimationGateDisposer?.();
  delete gateWindow.__xdtHiddenAnimationGateLastHidden;
  document.documentElement.classList.remove('dark');
  vi.unstubAllGlobals();
  vi.restoreAllMocks();
});

it.each(['sidebar'] as const)(
  'boots %s with a wallpaper using its read-only bridge and follows live changes',
  async (kind) => {
    vi.stubGlobal('IS_REACT_ACT_ENVIRONMENT', true);
    const play = vi.spyOn(HTMLMediaElement.prototype, 'play').mockResolvedValue();
    const pause = vi.spyOn(HTMLMediaElement.prototype, 'pause').mockImplementation(() => {});
    vi.spyOn(HTMLMediaElement.prototype, 'load').mockImplementation(() => {});
    vi.stubGlobal(
      'Image',
      class {
        src = '';
        decode = vi.fn().mockResolvedValue(undefined);
      },
    );
    const imageUrl = 'cindy-media://client-wallpaper/' + 'b'.repeat(64) + '.webp';
    const videoUrl = 'cindy-media://client-wallpaper/' + 'c'.repeat(64) + '.mp4';
    const ensureImage = vi.fn().mockResolvedValue(imageUrl);
    const ensureVideo = vi.fn().mockResolvedValue(videoUrl);
    document.body.innerHTML = '<div id="root"></div>';
    let changed!: (settings: AppearanceSettings) => void;
    let hiddenChanged!: (hidden: boolean) => void;
    // Electron can report visible while a prewarmed native window is hidden.
    vi.spyOn(document, 'hidden', 'get').mockReturnValue(false);
    vi.spyOn(document, 'visibilityState', 'get').mockReturnValue('visible');
    vi.stubGlobal('electronAPI', {
      platform: 'win32',
      onWindowHiddenChange: (fn: typeof hiddenChanged) => {
        hiddenChanged = fn;
        fn(true);
        return () => {};
      },
      appearanceSettings: {
        ensureWallpaperImage: ensureImage,
        ensureWallpaperVideo: ensureVideo,
        getSync: () => ({
          ...DEFAULT_APPEARANCE_SETTINGS,
          wallpaperId: 'cindy-window',
          wallpaperMotion: 'dynamic',
        }),
        onChanged: (fn: typeof changed) => {
          changed = fn;
          return () => {};
        },
      },
    });
    await act(async () => {
      await import('../sidebar-window-entry');
    });
    expect(document.body.textContent).toContain('sidebar host');
    expect(document.documentElement.dataset.wallpaperActive).toBe('true');
    expect(document.querySelectorAll('video')).toHaveLength(1);
    expect(document.querySelector('video')?.getAttribute('src')).toBe(videoUrl);
    expect(document.documentElement.style.getPropertyValue('--app-wallpaper-image')).toContain(
      imageUrl,
    );
    expect(ensureImage).toHaveBeenCalledWith('cindy-window');
    expect(ensureVideo).toHaveBeenCalledWith('cindy-window');
    if (kind === 'sidebar') {
      expect(play).not.toHaveBeenCalled();
      expect(pause).toHaveBeenCalled();
      const video = document.querySelector('video')!;
      await act(async () => hiddenChanged(false));
      expect(play).toHaveBeenCalledOnce();
      video.currentTime = 3;
      pause.mockClear();
      await act(async () => hiddenChanged(true));
      expect(pause).toHaveBeenCalledOnce();
      expect(play).toHaveBeenCalledOnce();
      await act(async () => hiddenChanged(false));
      expect(play).toHaveBeenCalledTimes(2);
      expect(document.querySelector('video')).toBe(video);
      expect(video.currentTime).toBe(3);
    }
    const lightVeil = document.documentElement.style.getPropertyValue('--app-wallpaper-veil');
    await act(async () => {
      document.documentElement.classList.add('dark');
    });
    expect(document.documentElement.style.getPropertyValue('--app-wallpaper-veil')).not.toBe(
      lightVeil,
    );
    const url = 'cindy-media://client-wallpaper/' + 'a'.repeat(64) + '.webp';
    await act(async () =>
      changed({ ...DEFAULT_APPEARANCE_SETTINGS, wallpaperId: 'custom', customWallpaperUrl: url }),
    );
    expect(document.querySelector('video')).toBeNull();
    expect(document.documentElement.style.getPropertyValue('--app-wallpaper-image')).toContain(url);
    await act(async () => changed(DEFAULT_APPEARANCE_SETTINGS));
    expect(document.documentElement.dataset.wallpaperActive).toBeUndefined();
  },
);