Skip to content

🤯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.

8.4 KB
Raw Blame
import { type ToolManifest } from '@lobechat/types';
import { describe, expect, it } from 'vitest';

import { type ToolStoreState } from '../../initialState';
import { initialState } from '../../initialState';
import { pluginSelectors } from './selectors';

const mockState = {
  ...initialState,
  pluginInstallLoading: {
    'plugin-1': false,
    'plugin-2': true,
  },
  installedPlugins: [
    {
      identifier: 'plugin-1',
      type: 'plugin',
      manifest: {
        identifier: 'plugin-1',
        api: [{ name: 'api-1' }],
        type: 'default',
        meta: { avatar: 'avatar-url-1', title: 'Plugin 1' },
      } as ToolManifest,
      settings: { setting1: 'value1' },
    },
    {
      identifier: 'plugin-2',
      manifest: {
        identifier: 'plugin-2',
        api: [{ name: 'api-2' }],
        meta: { avatar: 'avatar-url-2', title: 'Plugin 2' },
      },
      type: 'plugin',
    },
    {
      identifier: 'plugin-3',
      manifest: {
        identifier: 'plugin-3',
        api: [{ name: 'api-3' }],
      },
      type: 'customPlugin',
    },
  ],
} as ToolStoreState;

describe('pluginSelectors', () => {
  describe('getToolManifestById', () => {
    it('getToolManifestById should return the correct manifest', () => {
      const result = pluginSelectors.getToolManifestById('plugin-1')(mockState);
      expect(result).toEqual(mockState.installedPlugins[0].manifest);
    });
  });

  describe('getPluginMetaById', () => {
    it('should return the plugin metadata by id', () => {
      const result = pluginSelectors.getPluginMetaById('plugin-1')(mockState);
      expect(result).toEqual({
        avatar: 'avatar-url-1',
        title: 'Plugin 1',
      });
    });

    it('derives meta from a marketplace-shaped manifest without `meta`', () => {
      // Community MCP rows installed by the server-side agent builder store the
      // marketplace manifest verbatim: flat name/description/icon, no `meta`.
      const state = {
        ...mockState,
        installedPlugins: [
          {
            identifier: 'alpaca-mcp',
            manifest: {
              description: 'Trade with Alpaca',
              icon: 'https://example.com/alpaca.png',
              identifier: 'alpaca-mcp',
              name: 'Alpaca MCP',
              tags: ['finance'],
              tools: [],
            } as unknown as ToolManifest,
            type: 'plugin',
          },
        ],
      } as ToolStoreState;

      expect(pluginSelectors.getPluginMetaById('alpaca-mcp')(state)).toEqual({
        avatar: 'https://example.com/alpaca.png',
        description: 'Trade with Alpaca',
        tags: ['finance'],
        title: 'Alpaca MCP',
      });
    });

    it('returns undefined when the manifest carries neither meta nor a market name', () => {
      const result = pluginSelectors.getPluginMetaById('plugin-3')(mockState);
      expect(result).toBeUndefined();
    });
  });

  describe('getDevPluginById', () => {
    it('should return undefined for non-existing custom plugin', () => {
      const result = pluginSelectors.getCustomPluginById('non-existing')(mockState);
      expect(result).toBeUndefined();
    });

    it('should return custom plugin by id if exists', () => {
      const customPlugin = {
        type: 'customPlugin',
        identifier: 'custom-plugin',
        manifest: {
          identifier: 'custom-plugin',
          author: 'Custom Author',
          createAt: '2023-03-03',
          meta: { avatar: 'avatar-url-custom', title: 'Custom Plugin' },
          homepage: 'http://homepage-custom.com',
        },
      };

      const stateWithCustomPlugin = {
        ...mockState,
        installedPlugins: [...mockState.installedPlugins, customPlugin],
      } as ToolStoreState;
      const result = pluginSelectors.getCustomPluginById('custom-plugin')(stateWithCustomPlugin);
      expect(result).toEqual(customPlugin);
    });
  });

  describe('getPluginSettingsById', () => {
    it('should return the plugin settings by id', () => {
      const mockSettings = { setting1: 'value1' };

      const result = pluginSelectors.getPluginSettingsById('plugin-1')(mockState);
      expect(result).toEqual(mockSettings);
    });
  });

  describe('hasPluginUI', () => {
    it('should return false when the plugin does not have a UI component', () => {
      const result = pluginSelectors.isPluginHasUI('non-ui-plugin')(mockState);
      expect(result).toBe(false);
    });

    it('should return true when the plugin has a UI component', () => {
      const stateWithUIPlugin = {
        ...mockState,
        installedPlugins: [
          ...mockState.installedPlugins,
          {
            ...mockState.installedPlugins[0],
            identifier: 'ui-plugin',
            manifest: {
              ...mockState.installedPlugins[0].manifest,
              identifier: 'ui-plugin',
              ui: true,
            },
          },
        ],
      } as ToolStoreState;
      const result = pluginSelectors.isPluginHasUI('ui-plugin')(stateWithUIPlugin);
      expect(result).toBe(true);
    });
  });

  describe('isPluginInstalled', () => {
    it('should return true if the plugin is installed', () => {
      const result = pluginSelectors.isPluginInstalled('plugin-1')(mockState);
      expect(result).toBe(true);
    });

    it('should return false if the plugin is not installed', () => {
      const result = pluginSelectors.isPluginInstalled('non-installed-plugin')(mockState);
      expect(result).toBe(false);
    });

    it('should count a marketplace MCP installed as a connector', () => {
      const connector = {
        identifier: 'market-mcp',
        mcpConnectionType: 'http',
        mcpServerUrl: 'https://mcp.example.com',
        sourceType: 'marketplace',
      };
      const withConnector = (metadata: Record<string, unknown> | null) =>
        ({ ...mockState, connectors: [{ ...connector, metadata }] }) as ToolStoreState;

      expect(
        pluginSelectors.isPluginInstalled('market-mcp')(
          withConnector({ marketplaceInstalled: true }),
        ),
      ).toBe(true);
      // An unfinished install (no mark) is not installed
      expect(pluginSelectors.isPluginInstalled('market-mcp')(withConnector(null))).toBe(false);
    });
  });

  describe('getInstalledPluginById', () => {
    it('should return the installed plugin by id', () => {
      const result = pluginSelectors.getInstalledPluginById('plugin-1')(mockState);
      expect(result).toEqual(mockState.installedPlugins[0]);
    });

    it('should return undefined if the plugin is not installed', () => {
      const result = pluginSelectors.getInstalledPluginById('non-installed-plugin')(mockState);
      expect(result).toBeUndefined();
    });
  });

  describe('storeAndInstallPluginsIdList', () => {
    it('should return a list of unique plugin identifiers from installed plugins', () => {
      const result = pluginSelectors.storeAndInstallPluginsIdList(mockState);
      expect(result).toEqual(['plugin-1', 'plugin-2', 'plugin-3']);
    });
  });

  describe('installedPluginManifestList', () => {
    it('should return a list of manifests for installed plugins', () => {
      const result = pluginSelectors.installedPluginManifestList(mockState);
      const expectedManifests = mockState.installedPlugins.map((p) => p.manifest);
      expect(result).toEqual(expectedManifests);
    });
  });

  describe('installedPluginMetaList', () => {
    it('should return a list of meta information for installed plugins', () => {
      const result = pluginSelectors.installedPluginMetaList(mockState);
      expect(result).toHaveLength(mockState.installedPlugins.length);
      expect(result[0].identifier).toBe('plugin-1');
      expect(result[0].type).toBe('plugin');
    });

    it('surfaces the market name as title for a meta-less community plugin', () => {
      const state = {
        ...mockState,
        installedPlugins: [
          {
            identifier: 'alpaca-mcp',
            manifest: { identifier: 'alpaca-mcp', name: 'Alpaca MCP' } as unknown as ToolManifest,
            type: 'plugin',
          },
        ],
      } as ToolStoreState;

      const [item] = pluginSelectors.installedPluginMetaList(state);
      expect(item.title).toBe('Alpaca MCP');
    });
  });

  describe('installedCustomPluginMetaList', () => {
    it('should return a list of meta information for installed plugins', () => {
      const result = pluginSelectors.installedCustomPluginMetaList(mockState);

      expect(result).toEqual([{ identifier: 'plugin-3', type: 'customPlugin' }]);
    });
  });
});