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.

4.4 KB
Raw Blame
import { AuvManifest } from '@lobechat/builtin-tool-auv';
import { BrowserManifest } from '@lobechat/builtin-tool-browser';
import { LocalSystemManifest } from '@lobechat/builtin-tool-local-system';
import { RemoteDeviceManifest } from '@lobechat/builtin-tool-remote-device';
import { builtinTools } from '@lobechat/builtin-tools';
import { describe, expect, it } from 'vitest';

import { buildAllowedBuiltinTools, isDeviceToolIdentifier } from './deviceToolRegistry';

describe('deviceToolRegistry', () => {
  it('isDeviceToolIdentifier recognises the device tools', () => {
    expect(isDeviceToolIdentifier(LocalSystemManifest.identifier)).toBe(true);
    expect(isDeviceToolIdentifier(RemoteDeviceManifest.identifier)).toBe(true);
    expect(isDeviceToolIdentifier(BrowserManifest.identifier)).toBe(true);
    expect(isDeviceToolIdentifier(AuvManifest.identifier)).toBe(true);
    expect(isDeviceToolIdentifier('web-browsing')).toBe(false);
    expect(isDeviceToolIdentifier('')).toBe(false);
  });

  describe('buildAllowedBuiltinTools', () => {
    // https://github.com/lobehub/lobehub/pull/19051
    it('hides Computer Use when a device does not report support', () => {
      expect(
        buildAllowedBuiltinTools({ canUseDevice: true, supportedDeviceTools: [] }).map(
          (tool) => tool.identifier,
        ),
      ).not.toContain(AuvManifest.identifier);
    });

    it('returns the full builtin list when canUseDevice=true and disableLocalSystem=false', () => {
      const result = buildAllowedBuiltinTools({
        canUseDevice: true,
        supportedDeviceTools: [AuvManifest.identifier],
        disableLocalSystem: false,
      });
      expect(result.map((t) => t.identifier).sort()).toEqual(
        builtinTools.map((t) => t.identifier).sort(),
      );
    });

    it('strips BOTH device tools when canUseDevice=false (closes B1)', () => {
      const result = buildAllowedBuiltinTools({
        canUseDevice: false,
        disableLocalSystem: false,
      });
      const ids = result.map((t) => t.identifier);
      expect(ids).not.toContain(LocalSystemManifest.identifier);
      expect(ids).not.toContain(AuvManifest.identifier);
      expect(ids).not.toContain(RemoteDeviceManifest.identifier);
      // Non-device builtin tools are still present.
      expect(ids.length).toBeGreaterThan(0);
    });

    it('strips local-system and AUV when disableLocalSystem=true', () => {
      const result = buildAllowedBuiltinTools({
        canUseDevice: true,
        supportedDeviceTools: [AuvManifest.identifier],
        disableLocalSystem: true,
      });
      const ids = result.map((t) => t.identifier);
      expect(ids).not.toContain(LocalSystemManifest.identifier);
      expect(ids).not.toContain(AuvManifest.identifier);
      expect(ids).toContain(RemoteDeviceManifest.identifier);
    });

    it('strips both when both flags say strip (canUseDevice=false dominates)', () => {
      const result = buildAllowedBuiltinTools({
        canUseDevice: false,
        disableLocalSystem: true,
      });
      const ids = result.map((t) => t.identifier);
      expect(ids).not.toContain(LocalSystemManifest.identifier);
      expect(ids).not.toContain(AuvManifest.identifier);
      expect(ids).not.toContain(RemoteDeviceManifest.identifier);
    });

    it('treats omitted disableLocalSystem as false', () => {
      const result = buildAllowedBuiltinTools({
        canUseDevice: true,
        supportedDeviceTools: [AuvManifest.identifier],
      });
      const ids = result.map((t) => t.identifier);
      expect(ids).toContain(LocalSystemManifest.identifier);
      expect(ids).toContain(AuvManifest.identifier);
    });

    it('strips only remote-device when deviceLocked=true — local-system stays for the routed device', () => {
      const result = buildAllowedBuiltinTools({
        canUseDevice: true,
        supportedDeviceTools: [AuvManifest.identifier],
        deviceLocked: true,
      });
      const ids = result.map((t) => t.identifier);
      expect(ids).not.toContain(RemoteDeviceManifest.identifier);
      expect(ids).toContain(LocalSystemManifest.identifier);
      expect(ids).toContain(AuvManifest.identifier);
    });

    it('keeps remote-device when deviceLocked is omitted', () => {
      const result = buildAllowedBuiltinTools({
        canUseDevice: true,
        supportedDeviceTools: [AuvManifest.identifier],
      });
      const ids = result.map((t) => t.identifier);
      expect(ids).toContain(RemoteDeviceManifest.identifier);
    });
  });
});