-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathprompt.spec.ts
More file actions
79 lines (64 loc) · 2.56 KB
/
prompt.spec.ts
File metadata and controls
79 lines (64 loc) · 2.56 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
import { expect } from "chai";
import * as sinon from "sinon";
import { prompt } from "./prompt";
import * as availability from "./util/availability";
import { McpContext } from "./types";
describe("prompt", () => {
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 prompt with the correct shape and properties", () => {
const testFn = async () => [];
const testPrompt = prompt(
"core",
{
name: "test_prompt",
description: "A test prompt",
},
testFn,
);
expect(testPrompt.mcp.name).to.equal("test_prompt");
expect(testPrompt.mcp.description).to.equal("A test prompt");
expect(testPrompt.fn).to.equal(testFn);
});
it("should use the default availability check for the feature if none is provided", () => {
// Arrange: Prepare a fake default check function to be returned by our stub.
const fakeDefaultCheck = async () => true;
getDefaultFeatureAvailabilityCheckStub.withArgs("core").returns(fakeDefaultCheck);
// Act: Create a prompt WITHOUT providing an isAvailable function.
const testPrompt = prompt("core", { name: "test_prompt" }, async () => []);
// Assert: The prompt's isAvailable function should be the one our stub provided.
expect(testPrompt.isAvailable).to.equal(fakeDefaultCheck);
// Assert: The factory function should have called the stub to get the default.
expect(getDefaultFeatureAvailabilityCheckStub.calledOnceWith("core")).to.be.true;
});
it("should override the default and use the provided availability check", async () => {
const fakeDefaultCheck = async () => true;
const overrideCheck = async () => false;
getDefaultFeatureAvailabilityCheckStub.withArgs("core").returns(fakeDefaultCheck);
const testPrompt = prompt(
"core",
{
name: "test_prompt",
},
async () => [],
overrideCheck,
);
expect(testPrompt.isAvailable).to.equal(overrideCheck);
const isAvailable = await testPrompt.isAvailable(mockContext);
expect(isAvailable).to.be.false;
expect(getDefaultFeatureAvailabilityCheckStub.notCalled).to.be.true;
});
});