-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathtool.spec.ts
More file actions
84 lines (69 loc) · 2.59 KB
/
tool.spec.ts
File metadata and controls
84 lines (69 loc) · 2.59 KB
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
76
77
78
79
80
81
82
83
84
import { expect } from "chai";
import * as sinon from "sinon";
import { z } from "zod";
import { tool } from "./tool";
import * as availability from "./util/availability";
import { McpContext } from "./types";
describe("tool", () => {
let sandbox: sinon.SinonSandbox;
let getDefaultFeatureAvailabilityCheckStub: sinon.SinonStub;
// A mock context object for calling isAvailable functions.
const mockContext = {} as McpContext;
beforeEach(() => {
sandbox = sinon.createSandbox();
// Stub the function that provides the default availability checks.
getDefaultFeatureAvailabilityCheckStub = sandbox.stub(
availability,
"getDefaultFeatureAvailabilityCheck",
);
});
afterEach(() => {
sandbox.restore();
});
it("should create a tool with the correct shape and properties", () => {
const testFn = async () => ({ content: [] });
const testTool = tool(
"core",
{
name: "test_tool",
description: "A test tool",
inputSchema: z.object({}),
},
testFn,
);
expect(testTool.mcp.name).to.equal("test_tool");
expect(testTool.mcp.description).to.equal("A test tool");
expect(testTool.fn).to.equal(testFn);
});
it("should use the default availability check for the feature if none is provided", async () => {
const fakeDefaultCheck = sandbox.stub().returns(true);
getDefaultFeatureAvailabilityCheckStub.withArgs("core").returns(fakeDefaultCheck);
const testTool = tool("core", { name: "test_tool", inputSchema: z.object({}) }, async () => ({
content: [],
}));
const isAvailable = await testTool.isAvailable(mockContext);
expect(isAvailable).to.be.true;
expect(fakeDefaultCheck.called).to.be.true;
expect(getDefaultFeatureAvailabilityCheckStub.calledOnceWith("core")).to.be.true;
});
it("should override the default and use the provided availability check", async () => {
const fakeDefaultCheck = sandbox.stub().returns(true);
const overrideCheck = sandbox.stub().returns(false);
// This will be the override.
getDefaultFeatureAvailabilityCheckStub.withArgs("core").returns(fakeDefaultCheck);
const testTool = tool(
"core",
{
name: "test_tool",
inputSchema: z.object({}),
isAvailable: overrideCheck,
},
async () => ({ content: [] }),
);
const isAvailable = await testTool.isAvailable(mockContext);
expect(isAvailable).to.be.false;
expect(fakeDefaultCheck.notCalled).to.be.true;
expect(overrideCheck.called).to.be.true;
expect(getDefaultFeatureAvailabilityCheckStub.notCalled).to.be.true;
});
});