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.

33.7 KB
Raw Blame
import { execFileSync } from 'node:child_process';
import {
  mkdir,
  mkdtemp,
  readdir,
  readFile,
  realpath,
  rm,
  stat,
  symlink,
  writeFile,
} from 'node:fs/promises';
import { tmpdir } from 'node:os';
import path from 'node:path';

import { afterAll, beforeAll, describe, expect, it, vi } from 'vitest';

import { getAntigravityQuota } from '../antigravityQuota';
import {
  APP_UPDATE_UNSUPPORTED_MESSAGE,
  CLI_UPDATE_UNSUPPORTED_MESSAGE,
  DEVICE_RPC_METHODS,
  deviceRpcClients,
  executeDeviceRpc,
  MCP_UNSUPPORTED_MESSAGE,
  TERMINAL_UNSUPPORTED_MESSAGE,
  TRASH_UNSUPPORTED_MESSAGE,
} from '../dispatch';
import type { DeviceControlDeps } from '../types';
import { WORKSPACE_ESCAPE_MESSAGE } from '../workspaceGuard';

let root: string;
let deviceHome: string;

beforeAll(async () => {
  root = await mkdtemp(path.join(tmpdir(), 'device-control-'));
  deviceHome = await mkdtemp(path.join(tmpdir(), 'device-control-home-'));
  vi.stubEnv('HOME', deviceHome);

  await mkdir(path.join(root, '.agents', 'skills', 'spa-routes'), { recursive: true });
  await writeFile(
    path.join(root, '.agents', 'skills', 'spa-routes', 'SKILL.md'),
    '---\nname: spa-routes\ndescription: SPA routing\n---\nbody',
  );
  await writeFile(path.join(root, 'AGENTS.md'), '# Agents');
});

afterAll(async () => {
  vi.unstubAllEnvs();
  await rm(root, { force: true, recursive: true });
  await rm(deviceHome, { force: true, recursive: true });
});

const makeDeps = (): DeviceControlDeps => ({
  approveProjectRoot: vi.fn(async () => {}),
  getLocalFilePreview: vi.fn(async () => ({ success: true })),
  getProjectFileIndex: vi.fn(async () => ({
    entries: [],
    indexedAt: '',
    root: '',
    source: 'glob' as const,
  })),
  copyAssetForPublish: vi.fn(async () => ({ success: true })),
  readExternalAssetForPublish: vi.fn(async () => ({
    base64: 'AQID',
    contentType: 'image/png',
    success: true,
  })),
  searchProjectFiles: vi.fn(async () => ({
    entries: [],
    root: '',
    searchedAt: '',
    source: 'glob' as const,
  })),
});

vi.mock('../antigravityQuota', () => ({
  getAntigravityQuota: vi.fn(async () => ({ status: 'ok', groups: [] })),
}));

describe('executeDeviceRpc', () => {
  it('routes Antigravity quota RPCs through the shared host sampler', async () => {
    const params = { command: '/custom/agy', env: { AGY_PROFILE: 'fixture' }, force: true };
    expect(DEVICE_RPC_METHODS).toContain('getAntigravityQuota');
    await expect(executeDeviceRpc('getAntigravityQuota', params, makeDeps())).resolves.toEqual({
      status: 'ok',
      groups: [],
    });
    expect(getAntigravityQuota).toHaveBeenCalledWith(params);
  });

  it.each(['getCliUpdateState', 'checkCliUpdate', 'restartCli'])(
    'rejects missing CLI handler %s with a stable reason',
    async (method) => {
      await expect(executeDeviceRpc(method, {}, makeDeps())).rejects.toThrow(
        CLI_UPDATE_UNSUPPORTED_MESSAGE,
      );
      expect(DEVICE_RPC_METHODS).toContain(method);
    },
  );

  it('dispatches CLI maintenance and preserves the restart idempotency key', async () => {
    const state = {
      activeTasks: 0,
      currentVersion: '1.0.0',
      instanceId: 'instance',
      supported: true,
    };
    const restart = { requestId: '89d177cf-52e5-4d55-b71c-13deef4ea366', update: true };
    const deps = {
      ...makeDeps(),
      checkCliUpdate: vi.fn(async () => state),
      getCliUpdateState: vi.fn(async () => state),
      restartCli: vi.fn(async () => state),
    };
    for (const method of ['getCliUpdateState', 'checkCliUpdate', 'restartCli']) {
      await expect(executeDeviceRpc(method, restart, deps)).resolves.toEqual(state);
    }
    expect(deps.restartCli).toHaveBeenCalledWith(restart);
  });

  it('throws on an unknown method', async () => {
    await expect(executeDeviceRpc('nope', {}, makeDeps())).rejects.toThrow(
      'Unknown device RPC method: nope',
    );
  });

  describe('session worktree files', () => {
    let repo: string;
    let first: string;
    let second: string;

    beforeAll(async () => {
      const base = await realpath(root);
      repo = path.join(base, 'repo');
      first = path.join(base, 'first-worktree');
      second = path.join(base, 'second-worktree');
      await mkdir(repo);
      const git = (...args: string[]) =>
        execFileSync('git', ['-C', repo, ...args], { stdio: 'pipe' });
      git('init', '-q');
      await writeFile(path.join(repo, 'notes.txt'), 'original');
      git('add', 'notes.txt');
      git('-c', 'user.name=Test', '-c', '[email protected]', 'commit', '-qm', 'seed');
      git('worktree', 'add', '-b', 'first', first);
      git('worktree', 'add', '-b', 'second', second);
    });

    const run = (
      worktreePath: string,
      method: string,
      params: Record<string, unknown>,
      sourcePath = repo,
    ) =>
      executeDeviceRpc(
        'runWorktreeFileOperation',
        { approvedRoot: repo, rpc: { method, params }, sourcePath, worktreePath },
        makeDeps(),
      );

    it('reads and writes two sibling Git worktrees without registering their paths', async () => {
      for (const checkout of [first, second]) {
        const filePath = path.join(checkout, 'notes.txt');
        const content = path.basename(checkout);
        await expect(
          run(checkout, 'writeLocalFile', { content, path: filePath, workspaceRoot: checkout }),
        ).resolves.toEqual({ success: true });
        await expect(
          run(checkout, 'getLocalFilePreview', { path: filePath, workingDirectory: checkout }),
        ).resolves.toMatchObject({ preview: { content, type: 'text' }, success: true });
      }
      expect(await readFile(path.join(repo, 'notes.txt'), 'utf8')).toBe('original');
    });

    it('refuses an unrelated directory and a source symlink that escapes the registered root', async () => {
      const unrelated = path.join(root, 'unrelated');
      const escapedSource = path.join(repo, 'escape');
      await mkdir(unrelated);
      await symlink(first, escapedSource, 'dir');
      await expect(
        run(unrelated, 'writeLocalFile', {
          content: 'must not write',
          path: path.join(unrelated, 'notes.txt'),
          workspaceRoot: unrelated,
        }),
      ).rejects.toThrow('not a registered worktree');
      await expect(
        run(
          second,
          'getLocalFilePreview',
          {
            path: path.join(second, 'notes.txt'),
            workingDirectory: second,
          },
          escapedSource,
        ),
      ).rejects.toThrow(WORKSPACE_ESCAPE_MESSAGE);
      expect(await readdir(unrelated)).toEqual([]);
    });

    it('refuses to inherit access to worktrees from a registered subdirectory of the source', async () => {
      const approvedRoot = path.join(repo, 'registered-subdirectory');
      await mkdir(approvedRoot);

      await expect(
        executeDeviceRpc(
          'runWorktreeFileOperation',
          {
            approvedRoot,
            rpc: {
              method: 'getLocalFilePreview',
              params: { path: path.join(first, 'notes.txt'), workingDirectory: first },
            },
            sourcePath: approvedRoot,
            worktreePath: first,
          },
          makeDeps(),
        ),
      ).rejects.toThrow('The source repository is outside the approved workspace');
    });

    it('refuses preview and write through a symlink to a sibling worktree', async () => {
      const link = path.join(first, 'linked.txt');
      await symlink(path.join(second, 'notes.txt'), link, 'file');
      await expect(
        run(first, 'getLocalFilePreview', { path: link, workingDirectory: first }),
      ).resolves.toMatchObject({ code: 'forbidden', success: false });
      await expect(
        run(first, 'writeLocalFile', { content: 'blocked', path: link, workspaceRoot: first }),
      ).rejects.toThrow(WORKSPACE_ESCAPE_MESSAGE);
    });
  });

  describe('app update', () => {
    const state = { currentVersion: '2.1.0', stage: 'downloaded' as const, targetVersion: '2.2.0' };

    it('routes each app-update method to its host handler', async () => {
      const deps: DeviceControlDeps = {
        ...makeDeps(),
        checkAppUpdate: vi.fn(async () => ({ ...state, stage: 'checking' as const })),
        getAppUpdateState: vi.fn(async () => state),
        installAppUpdate: vi.fn(async () => ({ targetVersion: '2.2.0' })),
      };

      await expect(executeDeviceRpc('getAppUpdateState', undefined, deps)).resolves.toEqual(state);
      await expect(executeDeviceRpc('checkAppUpdate', undefined, deps)).resolves.toMatchObject({
        stage: 'checking',
      });
      await expect(executeDeviceRpc('installAppUpdate', undefined, deps)).resolves.toEqual({
        targetVersion: '2.2.0',
      });
    });

    it.each(['getAppUpdateState', 'checkAppUpdate', 'installAppUpdate'])(
      'rejects %s with a stable reason on a host that cannot update itself',
      async (method) => {
        await expect(executeDeviceRpc(method, undefined, makeDeps())).rejects.toThrow(
          APP_UPDATE_UNSUPPORTED_MESSAGE,
        );
      },
    );
  });

  it('routes initWorkspace through the shared workspace scan and approves the root', async () => {
    const deps = makeDeps();
    const result = (await executeDeviceRpc('initWorkspace', { scope: root }, deps)) as {
      instructions: { content: string; source: string }[];
      skills: { name: string }[];
    };

    expect(result.skills.map((s) => s.name)).toEqual(['spa-routes']);
    expect(result.instructions).toEqual([
      { content: '# Agents', path: path.join(root, 'AGENTS.md'), source: 'AGENTS.md' },
    ]);
    expect(deps.approveProjectRoot).toHaveBeenCalledWith(root);
  });

  it('routes listProjectSkills to the .agents/skills source', async () => {
    const result = (await executeDeviceRpc('listProjectSkills', { scope: root }, makeDeps())) as {
      source: string | null;
    };
    expect(result.source).toBe('.agents/skills');
  });

  it('merges project and device skills with project taking name precedence', async () => {
    const deviceSkillRoot = path.join(deviceHome, '.agents', 'skills');

    await mkdir(path.join(deviceSkillRoot, 'device-writer'), { recursive: true });
    await writeFile(
      path.join(deviceSkillRoot, 'device-writer', 'SKILL.md'),
      '---\nname: device-writer\ndescription: Device writer\n---\nbody',
    );
    await mkdir(path.join(deviceSkillRoot, 'spa-routes'), { recursive: true });
    await writeFile(
      path.join(deviceSkillRoot, 'spa-routes', 'SKILL.md'),
      '---\nname: spa-routes\ndescription: Device duplicate\n---\nbody',
    );

    try {
      const deps = makeDeps();
      const result = (await executeDeviceRpc('listProjectSkills', { scope: root }, deps)) as {
        skills: { name: string; previewRoot: string; scope: 'device' | 'project' }[];
      };

      expect(result.skills.map((skill) => `${skill.name}:${skill.scope}`)).toEqual([
        'device-writer:device',
        'spa-routes:project',
      ]);
      expect(result.skills.find((skill) => skill.name === 'device-writer')?.previewRoot).toBe(
        deviceSkillRoot,
      );
      expect(deps.approveProjectRoot).toHaveBeenCalledWith(root);
      expect(deps.approveProjectRoot).toHaveBeenCalledWith(deviceSkillRoot);
    } finally {
      await rm(path.join(deviceSkillRoot, 'device-writer'), { force: true, recursive: true });
      await rm(path.join(deviceSkillRoot, 'spa-routes'), { force: true, recursive: true });
    }
  });

  it('parses folded skill descriptions from frontmatter', async () => {
    await mkdir(path.join(root, '.agents', 'skills', 'agent-testing'), { recursive: true });
    await writeFile(
      path.join(root, '.agents', 'skills', 'agent-testing', 'SKILL.md'),
      [
        '---',
        'name: agent-testing',
        'description: >',
        '  Agentic end-to-end testing for LobeHub: backend verification via the CLI,',
        '  frontend verification via agent-browser (Electron).',
        '---',
        'body',
      ].join('\n'),
    );

    const result = (await executeDeviceRpc('listProjectSkills', { scope: root }, makeDeps())) as {
      skills: { description?: string; name: string }[];
    };

    expect(result.skills.find((skill) => skill.name === 'agent-testing')?.description).toBe(
      'Agentic end-to-end testing for LobeHub: backend verification via the CLI, frontend verification via agent-browser (Electron).',
    );
  });

  it('routes statPath and reports a directory + repo type', async () => {
    const result = (await executeDeviceRpc('statPath', { path: root }, makeDeps())) as {
      exists: boolean;
      isDirectory: boolean;
    };
    expect(result.exists).toBe(true);
    expect(result.isDirectory).toBe(true);
  });

  it('resolves directory aliases on the device before an execution can lock the path', async () => {
    const link = path.join(root, 'channel-directory-alias');
    const directory = path.join(root, 'channel-directory');
    await mkdir(directory);
    await symlink(directory, link, 'dir');
    const direct = await executeDeviceRpc('statPath', { path: directory }, makeDeps());
    const alias = await executeDeviceRpc('statPath', { path: link }, makeDeps());
    expect(alias).toEqual(direct);
    expect(alias).toMatchObject({ canonicalPath: await realpath(directory), isDirectory: true });
  });

  it('browses one directory level with pagination and excludes files and hidden folders', async () => {
    const browseRoot = await mkdtemp(path.join(tmpdir(), 'device-control-browse-'));
    try {
      await mkdir(path.join(browseRoot, '.hidden'));
      await mkdir(path.join(browseRoot, 'alpha'));
      await mkdir(path.join(browseRoot, 'beta'));
      await writeFile(path.join(browseRoot, 'notes.txt'), 'not a directory');

      const first = (await executeDeviceRpc(
        'browseDirectory',
        { limit: 1, path: browseRoot },
        makeDeps(),
      )) as { entries: { name: string }[]; nextCursor?: string; truncated: boolean };
      expect(first.entries.map((entry) => entry.name)).toEqual(['alpha']);
      expect(first.truncated).toBe(true);

      const second = (await executeDeviceRpc(
        'browseDirectory',
        { cursor: first.nextCursor, limit: 1, path: browseRoot },
        makeDeps(),
      )) as { entries: { name: string }[]; truncated: boolean };
      expect(second.entries.map((entry) => entry.name)).toEqual(['beta']);
      expect(second.truncated).toBe(false);
    } finally {
      await rm(browseRoot, { force: true, recursive: true });
    }
  });

  it('routes heterogeneous agent model discovery to the execution host', async () => {
    const deps = makeDeps();
    deps.listHeterogeneousAgentModels = vi.fn(async () => ({
      models: [{ id: 'openai/gpt-5.6', modelId: 'gpt-5.6', providerId: 'openai' }],
      status: 'success' as const,
      updatedAt: 1,
    }));
    const params = {
      args: ['--feature=test'],
      command: '/custom/traecli',
      cwd: root,
      type: 'trae' as const,
    };

    const result = await executeDeviceRpc('listHeterogeneousAgentModels', params, deps);

    expect(deps.listHeterogeneousAgentModels).toHaveBeenCalledWith(params);
    expect(result).toMatchObject({ status: 'success' });
  });

  it('reports model discovery as unsupported when the device client is too old', async () => {
    await expect(
      executeDeviceRpc('listHeterogeneousAgentModels', { type: 'opencode' }, makeDeps()),
    ).rejects.toThrow('does not support heterogeneous agent model discovery');
  });

  it('routes MCP tool listing to the host MCP client', async () => {
    const deps = makeDeps();
    const tools = [{ inputSchema: { type: 'object' }, name: 'search' }];
    deps.listMcpTools = vi.fn(async () => tools);
    const params = {
      mcpApps: true,
      params: {
        headers: { 'X-Team': 'a' },
        name: 'nas',
        type: 'http' as const,
        url: 'https://nas.tail1234.ts.net/mcp',
      },
    };

    await expect(executeDeviceRpc('listMcpTools', params, deps)).resolves.toEqual(tools);
    expect(deps.listMcpTools).toHaveBeenCalledWith(params);
  });

  it('rejects MCP tool listing on a client that runs no MCP servers', async () => {
    await expect(
      executeDeviceRpc(
        'listMcpTools',
        { params: { name: 'x', type: 'http', url: 'http://x' } },
        makeDeps(),
      ),
    ).rejects.toThrow(MCP_UNSUPPORTED_MESSAGE);
  });

  it('delegates project file and preview methods to injected deps', async () => {
    const deps = makeDeps();
    await executeDeviceRpc('getProjectFileIndex', { scope: root }, deps);
    expect(deps.getProjectFileIndex).toHaveBeenCalledWith({ scope: root });

    await executeDeviceRpc('searchProjectFiles', { query: 'agent', scope: root }, deps);
    expect(deps.searchProjectFiles).toHaveBeenCalledWith({ query: 'agent', scope: root });

    const previewParams = { path: path.join(root, 'AGENTS.md'), workingDirectory: root };
    await executeDeviceRpc('getLocalFilePreview', previewParams, deps);
    expect(deps.getLocalFilePreview).toHaveBeenCalledWith(previewParams);

    await executeDeviceRpc('readExternalAssetForPublish', previewParams, deps);
    expect(deps.readExternalAssetForPublish).toHaveBeenCalledWith(previewParams);

    const copyParams = {
      from: previewParams.path,
      to: path.join(root, 'copy.md'),
      workingDirectory: root,
    };
    await executeDeviceRpc('copyAssetForPublish', copyParams, deps);
    expect(deps.copyAssetForPublish).toHaveBeenCalledWith(copyParams);
  });

  it('routes a git method (listGitBranches) without touching deps', async () => {
    // Not a git repo → the shared local-file-shell impl returns an empty list.
    const result = await executeDeviceRpc('listGitBranches', { path: root }, makeDeps());
    expect(Array.isArray(result)).toBe(true);
  });

  it('rejects a failed git status instead of reporting a clean working tree', async () => {
    // Not a git repo → `git status` fails. The client persists this overlay, so
    // an empty result would wipe every cached dirty marker as "clean".
    await expect(
      executeDeviceRpc('getGitWorkingTreeFiles', { path: root }, makeDeps()),
    ).rejects.toThrow();
  });

  it('routes moveLocalFiles to the shared local-file-shell impl', async () => {
    const oldPath = path.join(root, 'move-src.txt');
    const newPath = path.join(root, 'move-dst.txt');
    await writeFile(oldPath, 'hello');

    const result = (await executeDeviceRpc(
      'moveLocalFiles',
      { items: [{ newPath, oldPath }] },
      makeDeps(),
    )) as { newPath?: string; success: boolean }[];

    expect(result).toHaveLength(1);
    expect(result[0].success).toBe(true);
    expect(result[0].newPath).toBe(newPath);
  });

  it('routes renameLocalFile to the shared local-file-shell impl', async () => {
    const filePath = path.join(root, 'rename-src.txt');
    await writeFile(filePath, 'hello');

    const result = (await executeDeviceRpc(
      'renameLocalFile',
      { newName: 'rename-dst.txt', path: filePath },
      makeDeps(),
    )) as { newPath: string; success: boolean };

    expect(result.success).toBe(true);
    expect(result.newPath).toBe(path.join(root, 'rename-dst.txt'));
  });

  it('routes writeLocalFile to the shared local-file-shell impl', async () => {
    const filePath = path.join(root, 'write-target.txt');

    const result = (await executeDeviceRpc(
      'writeLocalFile',
      { content: 'remote edit', path: filePath },
      makeDeps(),
    )) as { success: boolean };

    expect(result.success).toBe(true);
    expect(await readFile(filePath, 'utf8')).toBe('remote edit');
  });

  it('refuses to overwrite an existing target when routing moveLocalFiles', async () => {
    const oldPath = path.join(root, 'move-clash-src.txt');
    const newPath = path.join(root, 'move-clash-dst.txt');
    await writeFile(oldPath, 'incoming');
    await writeFile(newPath, 'keep me');

    const [result] = (await executeDeviceRpc(
      'moveLocalFiles',
      { items: [{ newPath, oldPath }] },
      makeDeps(),
    )) as { error?: string; success: boolean }[];

    expect(result.success).toBe(false);
    expect(result.error).toContain('already exists');
    expect(await readFile(newPath, 'utf8')).toBe('keep me');
  });

  it('refuses to overwrite an existing sibling when routing renameLocalFile', async () => {
    const filePath = path.join(root, 'rename-clash-src.txt');
    await writeFile(filePath, 'incoming');
    await writeFile(path.join(root, 'rename-clash-dst.txt'), 'keep me');

    const result = (await executeDeviceRpc(
      'renameLocalFile',
      { newName: 'rename-clash-dst.txt', path: filePath },
      makeDeps(),
    )) as { error?: string; success: boolean };

    expect(result.success).toBe(false);
    expect(result.error).toContain('already exists');
    expect(await readFile(path.join(root, 'rename-clash-dst.txt'), 'utf8')).toBe('keep me');
  });

  describe('file tree mutations', () => {
    it('exposes create / mkdir / copy / trash as device RPC methods', () => {
      expect(DEVICE_RPC_METHODS).toEqual(
        expect.arrayContaining([
          'createLocalFile',
          'createLocalDirectory',
          'copyLocalFiles',
          'trashLocalFiles',
        ]),
      );
    });

    it('routes createLocalFile and refuses an existing file', async () => {
      const filePath = path.join(root, 'created.ts');

      await expect(
        executeDeviceRpc('createLocalFile', { path: filePath, workspaceRoot: root }, makeDeps()),
      ).resolves.toEqual({ path: filePath, success: true });
      await expect(
        executeDeviceRpc(
          'createLocalFile',
          { content: 'x', path: filePath, workspaceRoot: root },
          makeDeps(),
        ),
      ).resolves.toMatchObject({
        error: expect.stringContaining('already exists'),
        success: false,
      });
      expect(await readFile(filePath, 'utf8')).toBe('');
    });

    it('routes createLocalDirectory and refuses an existing folder', async () => {
      const dirPath = path.join(root, 'created-dir');

      await expect(
        executeDeviceRpc(
          'createLocalDirectory',
          { path: dirPath, workspaceRoot: root },
          makeDeps(),
        ),
      ).resolves.toEqual({ path: dirPath, success: true });
      expect((await stat(dirPath)).isDirectory()).toBe(true);
      await expect(
        executeDeviceRpc(
          'createLocalDirectory',
          { path: dirPath, workspaceRoot: root },
          makeDeps(),
        ),
      ).resolves.toMatchObject({
        error: expect.stringContaining('already exists'),
        success: false,
      });
    });

    it('routes copyLocalFiles, duplicating in place when targetPath is omitted', async () => {
      const src = path.join(root, 'dup.md');
      await writeFile(src, 'dup');

      const result = await executeDeviceRpc(
        'copyLocalFiles',
        { items: [{ sourcePath: src }], workspaceRoot: root },
        makeDeps(),
      );

      expect(result).toEqual([
        { sourcePath: src, success: true, targetPath: path.join(root, 'dup copy.md') },
      ]);
      expect(await readFile(path.join(root, 'dup copy.md'), 'utf8')).toBe('dup');
    });

    it('routes trashLocalFiles to the host trash handler', async () => {
      const target = path.join(root, 'a.txt');
      const trashLocalFiles = vi.fn(async () => ({
        items: [{ path: target, success: true }],
        success: true,
      }));

      await expect(
        executeDeviceRpc(
          'trashLocalFiles',
          { paths: [target], workspaceRoot: root },
          {
            ...makeDeps(),
            trashLocalFiles,
          },
        ),
      ).resolves.toEqual({ items: [{ path: target, success: true }], success: true });
      expect(trashLocalFiles).toHaveBeenCalledWith({ paths: [target] });
    });

    it('rejects trashLocalFiles on a host without a trash instead of hard-deleting', async () => {
      const filePath = path.join(root, 'must-survive.txt');
      await writeFile(filePath, 'still here');

      await expect(
        executeDeviceRpc('trashLocalFiles', { paths: [filePath], workspaceRoot: root }, makeDeps()),
      ).rejects.toThrow(TRASH_UNSUPPORTED_MESSAGE);
      expect(await readFile(filePath, 'utf8')).toBe('still here');
    });

    describe('workspace containment through symlinks', () => {
      let workspace: string;
      let outside: string;

      beforeAll(async () => {
        workspace = await mkdtemp(path.join(tmpdir(), 'device-control-ws-'));
        outside = await mkdtemp(path.join(tmpdir(), 'device-control-outside-'));
        await writeFile(path.join(outside, 'secret.txt'), 'secret');
        // `<workspace>/link` passes a lexical "inside the root" check but
        // points at a folder outside the workspace.
        await symlink(outside, path.join(workspace, 'link'));
      });

      afterAll(async () => {
        await rm(workspace, { force: true, recursive: true });
        await rm(outside, { force: true, recursive: true });
      });

      it('refuses to create through a symlinked folder that leaves the workspace', async () => {
        const escaped = path.join(workspace, 'link', 'planted.txt');

        await expect(
          executeDeviceRpc(
            'createLocalFile',
            { path: escaped, workspaceRoot: workspace },
            makeDeps(),
          ),
        ).rejects.toThrow(WORKSPACE_ESCAPE_MESSAGE);
        await expect(
          executeDeviceRpc(
            'createLocalDirectory',
            { path: path.join(workspace, 'link', 'planted-dir'), workspaceRoot: workspace },
            makeDeps(),
          ),
        ).rejects.toThrow(WORKSPACE_ESCAPE_MESSAGE);
        await expect(readdir(outside)).resolves.toEqual(['secret.txt']);
      });

      it('refuses to copy a file out through the symlink', async () => {
        await writeFile(path.join(workspace, 'inside.txt'), 'inside');

        await expect(
          executeDeviceRpc(
            'copyLocalFiles',
            {
              items: [
                {
                  sourcePath: path.join(workspace, 'inside.txt'),
                  targetPath: path.join(workspace, 'link', 'copied.txt'),
                },
              ],
              workspaceRoot: workspace,
            },
            makeDeps(),
          ),
        ).rejects.toThrow(WORKSPACE_ESCAPE_MESSAGE);
        await expect(readdir(outside)).resolves.toEqual(['secret.txt']);
      });

      it('refuses to trash a file reached through the symlink', async () => {
        const trashLocalFiles = vi.fn();

        await expect(
          executeDeviceRpc(
            'trashLocalFiles',
            { paths: [path.join(workspace, 'link', 'secret.txt')], workspaceRoot: workspace },
            { ...makeDeps(), trashLocalFiles },
          ),
        ).rejects.toThrow(WORKSPACE_ESCAPE_MESSAGE);
        expect(trashLocalFiles).not.toHaveBeenCalled();
      });

      it('still lets the symlink entry itself be trashed', async () => {
        const trashLocalFiles = vi.fn(async () => ({ items: [], success: true }));
        const link = path.join(workspace, 'link');

        await executeDeviceRpc(
          'trashLocalFiles',
          { paths: [link], workspaceRoot: workspace },
          { ...makeDeps(), trashLocalFiles },
        );
        expect(trashLocalFiles).toHaveBeenCalledWith({ paths: [link] });
      });

      it('applies the same check to move / rename / write once the root is sent', async () => {
        await writeFile(path.join(workspace, 'movable.txt'), 'm');

        await expect(
          executeDeviceRpc(
            'moveLocalFiles',
            {
              items: [
                {
                  newPath: path.join(workspace, 'link', 'moved.txt'),
                  oldPath: path.join(workspace, 'movable.txt'),
                },
              ],
              workspaceRoot: workspace,
            },
            makeDeps(),
          ),
        ).rejects.toThrow(WORKSPACE_ESCAPE_MESSAGE);
        await expect(
          executeDeviceRpc(
            'renameLocalFile',
            {
              newName: 'renamed.txt',
              path: path.join(workspace, 'link', 'secret.txt'),
              workspaceRoot: workspace,
            },
            makeDeps(),
          ),
        ).rejects.toThrow(WORKSPACE_ESCAPE_MESSAGE);
        await expect(
          executeDeviceRpc(
            'writeLocalFile',
            {
              content: 'overwritten',
              path: path.join(workspace, 'link', 'secret.txt'),
              workspaceRoot: workspace,
            },
            makeDeps(),
          ),
        ).rejects.toThrow(WORKSPACE_ESCAPE_MESSAGE);
        expect(await readFile(path.join(outside, 'secret.txt'), 'utf8')).toBe('secret');
      });

      it('refuses create / copy / trash that arrive without a workspace root', async () => {
        await expect(
          executeDeviceRpc('createLocalFile', { path: path.join(workspace, 'x.txt') }, makeDeps()),
        ).rejects.toThrow(WORKSPACE_ESCAPE_MESSAGE);
      });
    });
  });

  it('routes listGitWorktrees through the shared git dispatcher', async () => {
    // Not a git repo → the shared local-file-shell impl returns an empty list.
    const result = await executeDeviceRpc('listGitWorktrees', { path: root }, makeDeps());
    expect(Array.isArray(result)).toBe(true);
  });

  it('routes removeGitWorktree through the shared git dispatcher', async () => {
    const result = (await executeDeviceRpc(
      'removeGitWorktree',
      { path: root, worktreePath: root },
      makeDeps(),
    )) as { success: boolean };
    expect(result.success).toBe(false);
  });

  it('publishes the owned-worktree RPCs on the device surface', () => {
    expect(DEVICE_RPC_METHODS).toEqual(
      expect.arrayContaining(['cleanupOwnedWorktree', 'restoreOwnedWorktree']),
    );
  });

  it('never lets a request widen the owned-worktree guard root', async () => {
    // `rootDir` is a test-only knob of the shell function; arriving over RPC it
    // is dropped, so a folder outside `~/.lobehub/worktrees` stays refused.
    const target = path.join(root, 'wt');
    const cleanup = (await executeDeviceRpc(
      'cleanupOwnedWorktree',
      { discard: true, rootDir: root, sourcePath: root, worktreePath: target },
      makeDeps(),
    )) as { outcome: string };
    expect(cleanup.outcome).toBe('refused');

    const restore = (await executeDeviceRpc(
      'restoreOwnedWorktree',
      { branch: 'lobehub/x', rootDir: root, sourcePath: root, worktreePath: target },
      makeDeps(),
    )) as { outcome: string };
    expect(restore.outcome).toBe('refused');
  });

  describe('interactive terminal', () => {
    /** The PTY handlers a host opts into; absent they all refuse. */
    const terminalDeps = () => ({
      closeTerminal: vi.fn(async () => ({ closed: true })),
      createTerminalSession: vi.fn(async () => ({
        cwd: '/home/dev',
        id: 'term_1',
        pid: 4242,
        shell: '/bin/bash',
      })),
      readTerminal: vi.fn(async () => ({ chunk: 'aGk=', exited: false, nextCursor: 2 })),
      resizeTerminal: vi.fn(async () => {}),
      writeTerminal: vi.fn(async () => {}),
    });

    it('publishes the terminal RPCs on the device surface', () => {
      expect(DEVICE_RPC_METHODS).toEqual(
        expect.arrayContaining([
          'createTerminalSession',
          'writeTerminal',
          'readTerminal',
          'resizeTerminal',
          'closeTerminal',
        ]),
      );
    });

    it('routes each terminal RPC to its host handler', async () => {
      const terminals = terminalDeps();
      const deps: DeviceControlDeps = { ...makeDeps(), ...terminals };

      await executeDeviceRpc('createTerminalSession', { cols: 80, rows: 24 }, deps);
      expect(terminals.createTerminalSession).toHaveBeenCalledWith({ cols: 80, rows: 24 });

      await executeDeviceRpc('writeTerminal', { data: 'bHM=', id: 'term_1' }, deps);
      expect(terminals.writeTerminal).toHaveBeenCalledWith({ data: 'bHM=', id: 'term_1' });

      await executeDeviceRpc('readTerminal', { cursor: 0, id: 'term_1' }, deps);
      expect(terminals.readTerminal).toHaveBeenCalledWith({ cursor: 0, id: 'term_1' });

      await executeDeviceRpc('resizeTerminal', { cols: 120, id: 'term_1', rows: 40 }, deps);
      expect(terminals.resizeTerminal).toHaveBeenCalledWith({ cols: 120, id: 'term_1', rows: 40 });

      await executeDeviceRpc('closeTerminal', { id: 'term_1' }, deps);
      expect(terminals.closeTerminal).toHaveBeenCalledWith({ id: 'term_1' });
    });

    it('refuses every terminal RPC on a host that has no PTY handlers', async () => {
      // A desktop client serves its own terminal over IPC and does not opt in,
      // so a terminal opened against it must fail loudly rather than hang.
      const deps = makeDeps();
      for (const method of [
        'createTerminalSession',
        'writeTerminal',
        'readTerminal',
        'resizeTerminal',
        'closeTerminal',
      ] as const) {
        await expect(executeDeviceRpc(method, { id: 'term_1' }, deps)).rejects.toThrow(
          TERMINAL_UNSUPPORTED_MESSAGE,
        );
      }
    });
  });
});

describe('deviceRpcClients', () => {
  const desktopOnly = [
    'trashLocalFiles',
    'getAppUpdateState',
    'checkAppUpdate',
    'installAppUpdate',
  ];
  const cliOnly = ['getCliUpdateState', 'checkCliUpdate', 'restartCli'];

  it.each(desktopOnly)('routes %s, which the CLI rejects, to the desktop', (method) => {
    expect(deviceRpcClients(method)).toEqual(['desktop']);
  });

  it.each(cliOnly)('routes %s, which the desktop rejects, to the CLI', (method) => {
    expect(deviceRpcClients(method)).toEqual(['cli']);
  });

  // Both clients run MCP servers; the server pins the one it picked per device
  it('leaves MCP tool listing to the client the server pins', () => {
    expect(deviceRpcClients('listMcpTools')).toBeUndefined();
  });

  it('lets any client serve the other methods', () => {
    const shared = DEVICE_RPC_METHODS.filter(
      (method) => !desktopOnly.includes(method) && !cliOnly.includes(method),
    );
    for (const method of shared) expect(deviceRpcClients(method)).toBeUndefined();
    expect(deviceRpcClients('unknownMethod')).toBeUndefined();
  });
});