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.

7.8 KB
Raw Blame
import { ActivatorApiName, LobeActivatorIdentifier } from '@lobechat/builtin-tool-activator';
import { AgentDocumentsIdentifier } from '@lobechat/builtin-tool-agent-documents';
import { AuvIdentifier } from '@lobechat/builtin-tool-auv';
import { CloudSandboxManifest } from '@lobechat/builtin-tool-cloud-sandbox';
import { LobeAgentApiName, LobeAgentIdentifier } from '@lobechat/builtin-tool-lobe-agent';
import { WorkIdentifier } from '@lobechat/builtin-tool-work';
import { describe, expect, it } from 'vitest';

import {
  applyShareGateToAgentConfig,
  filterPluginsByShareGate,
  filterSkillsByShareGate,
  getShareGrantActivatedPluginIds,
  shareGateGrantsCloudSandbox,
} from '.';
import { resolveEffectiveShareToolGrants } from './grants';
import { buildGate } from './testUtils';

describe('filterPluginsByShareGate', () => {
  it('keeps only allowlisted plugin ids', () => {
    const gate = buildGate({
      toolGrants: [{ identifier: 'web-search' }, { identifier: 'mcp-github' }],
    });

    expect(filterPluginsByShareGate(['web-search', 'local-system', 'mcp-github'], gate)).toEqual([
      'web-search',
      'mcp-github',
    ]);
  });

  it('keeps the Works tool without any grant', () => {
    expect(filterPluginsByShareGate([WorkIdentifier, 'web-search'], buildGate())).toEqual([
      WorkIdentifier,
    ]);
  });

  it('exposes no tools when the allowlist is missing or empty', () => {
    expect(filterPluginsByShareGate(['web-search'], buildGate())).toEqual([]);
    expect(filterPluginsByShareGate(['web-search'], buildGate({ toolGrants: [] }))).toEqual([]);
  });

  it('keeps a pinned device builtin only under the device grant', () => {
    const ids = [AuvIdentifier, 'mcp-github'];

    expect(filterPluginsByShareGate(ids, buildGate())).toEqual([]);
    expect(filterPluginsByShareGate(ids, { ...buildGate(), canUseDevice: true })).toEqual([
      AuvIdentifier,
    ]);
  });

  it('treats a per-API grant as candidacy for the whole identifier', () => {
    // Narrowing down to the specific granted API happens later, in
    // `applyShareGateToToolSet`, once the real manifest is known β€” this pass
    // only decides whether the identifier is a candidate at all.
    const gate = buildGate({
      toolGrants: [{ apis: [LobeAgentApiName.analyzeMedia], identifier: LobeAgentIdentifier }],
    });

    expect(filterPluginsByShareGate([LobeAgentIdentifier, 'mcp-github'], gate)).toEqual([
      LobeAgentIdentifier,
    ]);
  });
});

describe('filterSkillsByShareGate', () => {
  it('keeps only the skills the creator named', () => {
    const gate = buildGate({ skillGrants: ['pdf-report', 'brand-voice'] });

    expect(filterSkillsByShareGate(['pdf-report', 'internal-audit', 'brand-voice'], gate)).toEqual([
      'pdf-report',
      'brand-voice',
    ]);
  });

  it('cannot turn an ordinary tool grant into a skill grant', () => {
    // `toolGrants` is never read as a skill list. Tool and skill ids share one
    // namespace, so a tool grant that happens to name a real skill id must not
    // widen the skill pool either.
    const gate = buildGate({ toolGrants: [{ identifier: 'pdf-report' }] });

    expect(filterSkillsByShareGate(['pdf-report'], gate)).toEqual([]);
  });

  it('exposes no skills when the share grants nothing at all', () => {
    expect(filterSkillsByShareGate(['pdf-report'], buildGate())).toEqual([]);
    expect(filterSkillsByShareGate(['pdf-report'], buildGate({ skillGrants: [] }))).toEqual([]);
  });
});

describe('getShareGrantActivatedPluginIds', () => {
  it('activates Agent Documents only after the owner grants it for this Share', () => {
    expect(getShareGrantActivatedPluginIds(buildGate())).not.toContain(AgentDocumentsIdentifier);
    expect(
      getShareGrantActivatedPluginIds(
        buildGate({ toolGrants: [{ identifier: AgentDocumentsIdentifier }] }),
      ),
    ).toContain(AgentDocumentsIdentifier);
  });

  it('activates the Works tool in every visitor run, granted or not', () => {
    expect(getShareGrantActivatedPluginIds(buildGate())).toEqual([WorkIdentifier]);
    expect(
      getShareGrantActivatedPluginIds(
        buildGate({ toolGrants: [{ identifier: AgentDocumentsIdentifier }] }),
      ),
    ).toEqual([AgentDocumentsIdentifier, WorkIdentifier]);
  });
});

describe('applyShareGateToAgentConfig', () => {
  it('always strips files and knowledge bases', () => {
    const agentConfig = {
      files: [{ enabled: true, id: 'f1' }],
      knowledgeBases: [{ enabled: true, id: 'kb1' }],
    };

    applyShareGateToAgentConfig(agentConfig);

    expect(agentConfig.files).toEqual([]);
    expect(agentConfig.knowledgeBases).toEqual([]);
  });
});

describe('shareGateGrantsCloudSandbox', () => {
  it('is false when the share does not grant lobe-cloud-sandbox', () => {
    expect(shareGateGrantsCloudSandbox(buildGate())).toBe(false);
    expect(
      shareGateGrantsCloudSandbox(buildGate({ toolGrants: [{ identifier: 'web-search' }] })),
    ).toBe(false);
  });

  it('is true for a whole-identifier grant', () => {
    expect(
      shareGateGrantsCloudSandbox(
        buildGate({ toolGrants: [{ identifier: CloudSandboxManifest.identifier }] }),
      ),
    ).toBe(true);
  });

  // The plan only needs to know the sandbox is in play at all β€” narrowing to
  // the granted APIs happens later in `applyShareGateToToolSet`.
  it('treats an apis-scoped grant as a grant of the identifier', () => {
    expect(
      shareGateGrantsCloudSandbox(
        buildGate({
          toolGrants: [{ apis: ['runCommand'], identifier: CloudSandboxManifest.identifier }],
        }),
      ),
    ).toBe(true);
  });
});

describe('resolveEffectiveShareToolGrants β€” lobe-activator', () => {
  it('implies the activator, scoped to activateTools, from any tool grant', () => {
    const grants = resolveEffectiveShareToolGrants({ toolGrants: [{ identifier: 'mcp-github' }] });

    expect(grants.get(LobeActivatorIdentifier)).toEqual(new Set([ActivatorApiName.activateTools]));
  });

  it('does not imply the activator when the share grants no tool', () => {
    // The implied Works grant is enabled directly; it never needs the activator.
    expect(resolveEffectiveShareToolGrants({}).has(LobeActivatorIdentifier)).toBe(false);
    // A skill-only share already has lobe-skills to load what it granted.
    expect(
      resolveEffectiveShareToolGrants({ skillGrants: ['pdf-report'] }).has(LobeActivatorIdentifier),
    ).toBe(false);
  });

  it('implies the activator from the device grant alone', () => {
    expect(
      resolveEffectiveShareToolGrants({ canUseDevice: true }).get(LobeActivatorIdentifier),
    ).toEqual(new Set([ActivatorApiName.activateTools]));
  });

  it('never lets a stored activator entry widen the implied scope or stand alone', () => {
    const widened = resolveEffectiveShareToolGrants({
      toolGrants: [{ identifier: LobeActivatorIdentifier }, { identifier: 'mcp-github' }],
    });
    expect(widened.get(LobeActivatorIdentifier)).toEqual(new Set([ActivatorApiName.activateTools]));

    // An activator entry is not itself a grant of anything to activate.
    const alone = resolveEffectiveShareToolGrants({
      toolGrants: [{ identifier: LobeActivatorIdentifier }],
    });
    expect(alone.has(LobeActivatorIdentifier)).toBe(false);
  });
});

describe('resolveEffectiveShareToolGrants β€” lobe-work', () => {
  it('grants the Works tool to every share, whatever the owner picked', () => {
    expect(resolveEffectiveShareToolGrants({}).get(WorkIdentifier)).toBe('all');
    expect(
      resolveEffectiveShareToolGrants({ toolGrants: [{ identifier: 'mcp-github' }] }).get(
        WorkIdentifier,
      ),
    ).toBe('all');
  });

  it('widens a stored per-API Works entry to the whole tool', () => {
    expect(
      resolveEffectiveShareToolGrants({
        toolGrants: [{ apis: ['createWork'], identifier: WorkIdentifier }],
      }).get(WorkIdentifier),
    ).toBe('all');
  });
});