summaryrefslogtreecommitdiffhomepage
path: root/packages/session-orchestrator/src/tools-filter.test.ts
blob: a3c439bebcf592d909a3a3dcfb16098b5dcfb675 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import type { ToolContract } from "@dispatch/kernel";
import { describe, expect, it } from "vitest";
import { filterRemoteIncompatibleTools, type ToolAssembly } from "./tools-filter.js";

function fakeTool(name: string): ToolContract {
  return {
    name,
    description: `Fake tool: ${name}`,
    parameters: { type: "object" },
    execute: async () => ({ content: "ok" }),
  };
}

const baseAssembly: ToolAssembly = {
  tools: [fakeTool("lsp"), fakeTool("mcp__x"), fakeTool("run_shell")],
  conversationId: "conv-1",
};

describe("filterRemoteIncompatibleTools", () => {
  it("REMOTE (computerId set): drops 'lsp' and any '__' namespaced tool, keeps 'run_shell'", () => {
    const remote: ToolAssembly = { ...baseAssembly, computerId: "my-server" };
    const result = filterRemoteIncompatibleTools(remote);
    const names = result.tools.map((t) => t.name);
    expect(names).not.toContain("lsp");
    expect(names).not.toContain("mcp__x");
    expect(names).toContain("run_shell");
    expect(result.tools).toHaveLength(1);
  });

  it("REMOTE: preserves computerId + cwd + conversationId in the returned assembly", () => {
    const remote: ToolAssembly = {
      tools: [fakeTool("lsp"), fakeTool("run_shell")],
      conversationId: "conv-2",
      cwd: "/work",
      computerId: "ssh-host",
    };
    const result = filterRemoteIncompatibleTools(remote);
    expect(result.computerId).toBe("ssh-host");
    expect(result.cwd).toBe("/work");
    expect(result.conversationId).toBe("conv-2");
  });

  it("LOCAL (computerId undefined): passthrough — nothing is dropped", () => {
    const local: ToolAssembly = { ...baseAssembly };
    const result = filterRemoteIncompatibleTools(local);
    expect(result.tools).toHaveLength(3);
    const names = result.tools.map((t) => t.name);
    expect(names).toContain("lsp");
    expect(names).toContain("mcp__x");
    expect(names).toContain("run_shell");
  });

  it("LOCAL: returns the exact same assembly object (byte-identical)", () => {
    const local: ToolAssembly = { ...baseAssembly };
    const result = filterRemoteIncompatibleTools(local);
    expect(result).toBe(local);
  });

  it("REMOTE: drops multiple MCP-namespaced tools (serverId__toolName pattern)", () => {
    const remote: ToolAssembly = {
      tools: [
        fakeTool("lsp"),
        fakeTool("filesystem__read"),
        fakeTool("github__create_issue"),
        fakeTool("run_shell"),
        fakeTool("write_file"),
      ],
      conversationId: "conv-3",
      computerId: "host",
    };
    const result = filterRemoteIncompatibleTools(remote);
    const names = result.tools.map((t) => t.name);
    expect(names).toEqual(["run_shell", "write_file"]);
  });
});