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.

3.7 KB
Raw Blame
import SiGithub from '@icons-pack/react-simple-icons/icons/SiGithub';
import type { TaskTemplate } from '@lobechat/const';
import { BookOpen, type LucideIcon, Sparkles } from 'lucide-react';
import { describe, expect, it } from 'vitest';

import { resolveTemplateIcon } from './resolveTemplateIcon';

const baseTemplate = {
  category: 'engineering',
  connectors: [],
  cronPattern: '0 9 * * *',
  description: 'Description',
  id: 101,
  identifier: 'daily-engineering',
  instruction: 'Instruction',
  interests: [],
  title: 'Title',
} satisfies TaskTemplate;

const make = (overrides: Partial<TaskTemplate>): TaskTemplate =>
  ({ ...baseTemplate, ...overrides }) as TaskTemplate;

const interestMap = new Map<string, LucideIcon>([['coding', BookOpen]]);

describe('resolveTemplateIcon', () => {
  it('uses self icon when template.icon is set', () => {
    const spec = resolveTemplateIcon(make({ icon: 'github', interests: ['coding'] }), interestMap);
    expect(spec).toEqual({ Comp: SiGithub, kind: 'component' });
  });

  it('falls back to a required connector provider icon (component form)', () => {
    const spec = resolveTemplateIcon(
      make({
        connectors: [{ identifier: 'github', required: true, source: 'lobehub' }],
        interests: ['coding'],
      }),
      interestMap,
    );
    expect(spec).toEqual({ Comp: SiGithub, kind: 'component' });
  });

  it('falls back to a required connector provider icon (URL form)', () => {
    const spec = resolveTemplateIcon(
      make({
        connectors: [{ identifier: 'gmail', required: true, source: 'composio' }],
        interests: ['coding'],
      }),
      interestMap,
    );
    expect(spec.kind).toBe('url');
    if (spec.kind === 'url') expect(spec.src).toMatch(/gmail/);
  });

  it('prefers required over optional when both are present', () => {
    const spec = resolveTemplateIcon(
      make({
        connectors: [
          { identifier: 'notion', required: false, source: 'lobehub' },
          { identifier: 'github', required: true, source: 'lobehub' },
        ],
      }),
      interestMap,
    );
    expect(spec).toEqual({ Comp: SiGithub, kind: 'component' });
  });

  it('falls back to optional connector icon when required is absent', () => {
    const spec = resolveTemplateIcon(
      make({
        connectors: [{ identifier: 'notion', required: false, source: 'lobehub' }],
        interests: ['coding'],
      }),
      interestMap,
    );
    expect(spec.kind).toBe('url');
    if (spec.kind === 'url') expect(spec.src).toMatch(/notion/);
  });

  it('skips unresolvable required spec and tries optional', () => {
    const spec = resolveTemplateIcon(
      make({
        connectors: [
          { identifier: 'nonexistent-x', required: true, source: 'lobehub' },
          { identifier: 'notion', required: false, source: 'lobehub' },
        ],
      }),
      interestMap,
    );
    expect(spec.kind).toBe('url');
    if (spec.kind === 'url') expect(spec.src).toMatch(/notion/);
  });

  it('uses interest icon when self and connector icons are absent', () => {
    const spec = resolveTemplateIcon(make({ interests: ['coding'] }), interestMap);
    expect(spec).toEqual({ Comp: BookOpen, kind: 'component' });
  });

  it('falls back to Sparkles when nothing else resolves', () => {
    const spec = resolveTemplateIcon(make({ interests: [] }), interestMap);
    expect(spec).toEqual({ Comp: Sparkles, kind: 'component' });
  });

  it('falls back to Sparkles when interest key is unknown to the map', () => {
    const spec = resolveTemplateIcon(
      make({ interests: ['interest-not-in-map'] as unknown as TaskTemplate['interests'] }),
      interestMap,
    );
    expect(spec).toEqual({ Comp: Sparkles, kind: 'component' });
  });
});