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 { describe, expect, it } from 'vitest';

import { SkillsManifest } from './manifest';
import { resolveSkillsManifest } from './resolveManifest';
import { deviceSystemPrompt, systemPrompt } from './systemRole';
import { SkillsApiName } from './types';

const EXEC_APIS = [SkillsApiName.runCommand, SkillsApiName.execScript, SkillsApiName.exportFile];
const NON_EXEC_APIS = [SkillsApiName.activateSkill, SkillsApiName.readReference];

const apiByName = (
  manifest: { api: { description: string; humanIntervention?: unknown; name: string }[] },
  name: string,
) => manifest.api.find((a) => a.name === name)!;

describe('resolveSkillsManifest', () => {
  it('returns the static manifest reference when no executionEnv is set', () => {
    expect(resolveSkillsManifest({})).toBe(SkillsManifest);
    expect(resolveSkillsManifest({ isSubAgent: true, scope: 'main' })).toBe(SkillsManifest);
  });

  it('returns the static manifest reference for executionEnv none', () => {
    expect(resolveSkillsManifest({ executionEnv: 'none' })).toBe(SkillsManifest);
  });

  // Device runs execute execScript ON the device (serverRuntimes/skills.ts
  // device branch) — the description must say so, and the sandbox-only APIs
  // (runCommand / exportFile) are dropped, restoring the original desktop
  // manifest shape. `local` is the same routed device (the user's own desktop,
  // kept distinct only for local-system image reads): the server runtime still
  // takes the device branch and refuses sandbox runCommand / exportFile.
  it.each(['device', 'local'] as const)(
    'declares device execution and hides sandbox-only APIs for executionEnv %s',
    (executionEnv) => {
      const result = resolveSkillsManifest({ executionEnv })!;

      const description = apiByName(result, SkillsApiName.execScript).description;
      expect(description).toMatch(/^Execution environment: the user's selected device/);
      expect(description).toContain('NOT injected');
      // the original mechanics stay after the preamble
      expect(description).toContain(
        apiByName(SkillsManifest, SkillsApiName.execScript).description,
      );

      const apiNames = result.api.map((a) => a.name);
      expect(apiNames).not.toContain(SkillsApiName.runCommand);
      expect(apiNames).not.toContain(SkillsApiName.exportFile);
      expect(apiNames).toContain(SkillsApiName.activateSkill);
      expect(apiNames).toContain(SkillsApiName.readReference);

      // cross-tool arbitration rides the tool systemRole, not the descriptions,
      // and the device role omits cloud-sandbox runCommand / exportFile / sandbox_environment
      expect(result.systemRole).toContain(deviceSystemPrompt);
      expect(result.systemRole).not.toContain('exportFile');
      expect(result.systemRole).not.toContain('<sandbox_environment>');
      expect(result.systemRole).not.toContain('{{sandbox_workspace}}');
      expect(result.systemRole).toContain('`execScript` runs skill scripts on the device');
      expect(result.systemRole).toContain('`lobe-local-system` runCommand');
    },
  );

  it.each(['bound-device-offline', 'no-online-device'] as const)(
    'warns about the offline device when device-unrouted for %s',
    (executionEnvUnroutedReason) => {
      const result = resolveSkillsManifest({
        executionEnv: 'device-unrouted',
        executionEnvUnroutedReason,
      })!;

      for (const name of EXEC_APIS) {
        const description = apiByName(result, name).description;
        expect(description).toMatch(/^Fallback execution environment: an isolated cloud sandbox/);
        expect(description).toContain('local device but it is offline');
      }
      // the offline fact rides the tool systemRole into the prompt
      expect(result.systemRole).toContain(systemPrompt);
      expect(result.systemRole).toContain(
        'Bound device offline; shell commands execute in the cloud sandbox this run.',
      );
    },
  );

  it.each(['no-bound-device', 'ambiguous-online-devices'] as const)(
    'steers toward device selection when device-unrouted for %s',
    (executionEnvUnroutedReason) => {
      const result = resolveSkillsManifest({
        executionEnv: 'device-unrouted',
        executionEnvUnroutedReason,
      })!;

      for (const name of EXEC_APIS) {
        const description = apiByName(result, name).description;
        expect(description).toMatch(/^Fallback execution environment: an isolated cloud sandbox/);
        expect(description).toContain('No local device is selected yet');
        expect(description).not.toContain('offline');
      }
      expect(result.systemRole).toContain(systemPrompt);
      expect(result.systemRole).toContain('select a device via the remote-device tool');
      expect(result.systemRole).not.toContain('Bound device offline');
    },
  );

  it('keeps reason-neutral wording when device-unrouted has no reason', () => {
    const result = resolveSkillsManifest({ executionEnv: 'device-unrouted' })!;

    for (const name of EXEC_APIS) {
      const description = apiByName(result, name).description;
      expect(description).toContain('no device is routed this run');
      expect(description).not.toContain('offline');
    }
    expect(result.systemRole).toContain(
      'No local device is routed; shell commands execute in the cloud sandbox this run.',
    );
  });

  it('clarifies the sandbox location for an explicit sandbox target', () => {
    const result = resolveSkillsManifest({ executionEnv: 'sandbox' })!;

    for (const name of EXEC_APIS) {
      const description = apiByName(result, name).description;
      expect(description).toMatch(
        /^Execution environment: an isolated cloud sandbox, not the user's machine\./,
      );
    }
    expect(result.systemRole).toBe(systemPrompt);
  });

  it.each(['device-unrouted', 'sandbox'] as const)(
    'keeps non-exec APIs, api shape, and humanIntervention intact for %s',
    (executionEnv) => {
      const result = resolveSkillsManifest({ executionEnv })!;

      expect(result.api.map((a) => a.name)).toEqual(SkillsManifest.api.map((a) => a.name));
      for (const name of NON_EXEC_APIS) {
        expect(apiByName(result, name)).toBe(apiByName(SkillsManifest, name));
      }
      for (const name of [SkillsApiName.runCommand, SkillsApiName.execScript]) {
        expect(apiByName(result, name).humanIntervention).toBe('required');
      }
      expect(result.identifier).toBe(SkillsManifest.identifier);
    },
  );

  it('keeps non-exec APIs and humanIntervention intact for device', () => {
    const result = resolveSkillsManifest({ executionEnv: 'device' })!;

    for (const name of NON_EXEC_APIS) {
      expect(apiByName(result, name)).toBe(apiByName(SkillsManifest, name));
    }
    expect(apiByName(result, SkillsApiName.execScript).humanIntervention).toBe('required');
    expect(result.identifier).toBe(SkillsManifest.identifier);
  });

  it.each([undefined, 'sandbox', 'device'] as const)(
    'leaves only the Cloud Sandbox runCommand in page scope (executionEnv %s)',
    (executionEnv) => {
      const result = resolveSkillsManifest({ executionEnv, scope: 'page' })!;
      const names = result.api.map((a) => a.name);

      expect(names).not.toContain(SkillsApiName.runCommand);
      expect(names).toContain(SkillsApiName.execScript);
      expect(names).toContain(SkillsApiName.activateSkill);
    },
  );

  it('keeps runCommand outside page scope', () => {
    for (const scope of ['main', 'task']) {
      const names = resolveSkillsManifest({ executionEnv: 'sandbox', scope })!.api.map(
        (a) => a.name,
      );
      expect(names).toContain(SkillsApiName.runCommand);
    }
  });

  it('does not mutate the static manifest', () => {
    const staticDescriptions = SkillsManifest.api.map((a) => a.description);

    resolveSkillsManifest({ executionEnv: 'device-unrouted' });

    expect(SkillsManifest.api.map((a) => a.description)).toEqual(staticDescriptions);
    expect(SkillsManifest.systemRole).toBe(systemPrompt);
  });
});