-
-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathdefer-loading-parity.test.js
More file actions
134 lines (114 loc) · 3.84 KB
/
defer-loading-parity.test.js
File metadata and controls
134 lines (114 loc) · 3.84 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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
"use strict";
// This file keeps the defer_loading tool contract honest.
// It proves the node forwards deferred MCP tool definitions unchanged and that the docs/examples still describe the current Responses tool-search shape.
const assert = require("node:assert/strict");
const fs = require("node:fs");
const path = require("node:path");
const test = require("node:test");
function withMockedOpenAI(FakeOpenAI, callback) {
const openaiModule = require("openai");
const originalDescriptor = Object.getOwnPropertyDescriptor(openaiModule, "OpenAI");
Object.defineProperty(openaiModule, "OpenAI", {
value: FakeOpenAI,
configurable: true,
enumerable: true,
writable: true,
});
const run = async () => {
try {
return await callback();
} finally {
if (originalDescriptor) {
Object.defineProperty(openaiModule, "OpenAI", originalDescriptor);
}
}
};
return run();
}
const readme = fs.readFileSync(path.join(__dirname, "..", "README.md"), "utf8");
const responsesHelp = fs.readFileSync(
path.join(__dirname, "..", "src", "responses", "help.html"),
"utf8"
);
const toolSearchExample = JSON.parse(
fs.readFileSync(
path.join(__dirname, "..", "examples", "responses", "tool-search.json"),
"utf8"
)
);
test("responses create forwards deferred MCP tool definitions unchanged", async () => {
const calls = [];
const requestPayload = {
model: "gpt-5.4-mini",
tools: [
{ type: "tool_search" },
{
type: "mcp",
server_label: "deepwiki",
server_url: "https://mcp.deepwiki.com/mcp",
require_approval: "never",
defer_loading: true,
},
],
input: "Use the available documentation tools to summarize MCP transports.",
};
class FakeOpenAI {
constructor(clientParams) {
calls.push({ method: "ctor", clientParams });
this.responses = {
create: async (payload) => {
calls.push({ method: "responses.create", payload });
return { id: "resp_deferred", status: "completed" };
},
};
}
}
await withMockedOpenAI(FakeOpenAI, async () => {
const modulePath = require.resolve("../src/responses/methods.js");
delete require.cache[modulePath];
const responsesMethods = require("../src/responses/methods.js");
const clientContext = {
clientParams: {
apiKey: "sk-test",
baseURL: "https://api.example.com/v1",
},
};
const response = await responsesMethods.createModelResponse.call(clientContext, {
payload: requestPayload,
});
assert.deepEqual(response, { id: "resp_deferred", status: "completed" });
delete require.cache[modulePath];
});
assert.deepEqual(calls.filter((entry) => entry.method !== "ctor"), [
{
method: "responses.create",
payload: requestPayload,
},
]);
});
test("tool-search docs and example keep defer_loading explicit", () => {
assert.match(readme, /deferred MCP loading via `defer_loading`/);
assert.match(responsesHelp, /defer_loading: true/);
assert.match(responsesHelp, /Deferred tool loading is supported/);
const injectNode = toolSearchExample.find(
(entry) => entry.type === "inject" && entry.name === "Create Tool Search Request"
);
assert.ok(injectNode);
const toolSearchTool = JSON.parse(
injectNode.props.find((prop) => prop.p === "ai.tools[0]").v
);
const deferredMcpTool = JSON.parse(
injectNode.props.find((prop) => prop.p === "ai.tools[1]").v
);
assert.deepEqual(toolSearchTool, { type: "tool_search" });
assert.deepEqual(deferredMcpTool, {
type: "mcp",
server_label: "deepwiki",
server_url: "https://mcp.deepwiki.com/mcp",
require_approval: "never",
defer_loading: true,
});
const exampleTab = toolSearchExample.find((entry) => entry.type === "tab");
assert.ok(exampleTab);
assert.match(exampleTab.info, /defer_loading: true/);
});