@@ -17,9 +17,11 @@ vi.mock("../config/runtime-snapshot.js", () => ({
1717
1818import {
1919 buildPluginToolDescriptorCacheKey ,
20+ capturePluginToolDescriptor ,
2021 createPluginToolDescriptorConfigCacheKeyMemo ,
2122 resetPluginToolDescriptorCache ,
2223} from "./tool-descriptor-cache.js" ;
24+ import type { AnyAgentTool } from "../agents/tools/common.js" ;
2325
2426describe ( "plugin tool descriptor cache keys" , ( ) => {
2527 afterEach ( ( ) => {
@@ -170,3 +172,110 @@ describe("plugin tool descriptor cache keys", () => {
170172 expect ( firstKey ) . toBe ( secondKey ) ;
171173 } ) ;
172174} ) ;
175+
176+ describe ( "capturePluginToolDescriptor" , ( ) => {
177+ it ( "preserves tool availability on captured plugin descriptors" , ( ) => {
178+ const tool = {
179+ name : "needs_secret" ,
180+ label : "Needs secret" ,
181+ description : "Needs secret description" ,
182+ parameters : { type : "object" } ,
183+ availability : { kind : "env" , name : "OPENCLAW_PLUGIN_SECRET" } ,
184+ async execute ( ) {
185+ return { content : [ ] , details : undefined } ;
186+ } ,
187+ } satisfies AnyAgentTool & {
188+ availability : { kind : "env" ; name : string } ;
189+ } ;
190+
191+ const captured = capturePluginToolDescriptor ( {
192+ pluginId : "demo" ,
193+ tool,
194+ optional : false ,
195+ } ) ;
196+
197+ expect ( captured . descriptor . availability ) . toStrictEqual ( {
198+ kind : "env" ,
199+ name : "OPENCLAW_PLUGIN_SECRET" ,
200+ } ) ;
201+ } ) ;
202+
203+ it ( "warns during capture when a plugin tool declares an empty availability group" , ( ) => {
204+ const logger = { warn : vi . fn ( ) } ;
205+ const tool = {
206+ name : "broken_tool" ,
207+ label : "Broken tool" ,
208+ description : "Broken tool description" ,
209+ parameters : { type : "object" } ,
210+ availability : { anyOf : [ ] } ,
211+ async execute ( ) {
212+ return { content : [ ] , details : undefined } ;
213+ } ,
214+ } satisfies AnyAgentTool & {
215+ availability : { anyOf : [ ] } ;
216+ } ;
217+
218+ capturePluginToolDescriptor ( {
219+ pluginId : "demo" ,
220+ tool,
221+ optional : false ,
222+ logger,
223+ } ) ;
224+
225+ expect ( logger . warn ) . toHaveBeenCalledWith (
226+ "plugin tool availability diagnostic (demo/broken_tool): Empty availability anyOf group" ,
227+ ) ;
228+ } ) ;
229+
230+ it ( "ignores malformed non-contract availability group fields during capture" , ( ) => {
231+ const logger = { warn : vi . fn ( ) } ;
232+ const tool = {
233+ name : "incidental_tool" ,
234+ label : "Incidental tool" ,
235+ description : "Incidental tool description" ,
236+ parameters : { type : "object" } ,
237+ availability : { anyOf : null } ,
238+ async execute ( ) {
239+ return { content : [ ] , details : undefined } ;
240+ } ,
241+ } satisfies AnyAgentTool & {
242+ availability : { anyOf : null } ;
243+ } ;
244+
245+ const captured = capturePluginToolDescriptor ( {
246+ pluginId : "demo" ,
247+ tool,
248+ optional : false ,
249+ logger,
250+ } ) ;
251+
252+ expect ( captured . descriptor . availability ) . toBeUndefined ( ) ;
253+ expect ( logger . warn ) . not . toHaveBeenCalled ( ) ;
254+ } ) ;
255+
256+ it ( "ignores malformed non-contract availability signal fields during capture" , ( ) => {
257+ const logger = { warn : vi . fn ( ) } ;
258+ const tool = {
259+ name : "incidental_signal_tool" ,
260+ label : "Incidental signal tool" ,
261+ description : "Incidental signal tool description" ,
262+ parameters : { type : "object" } ,
263+ availability : { kind : "config" } ,
264+ async execute ( ) {
265+ return { content : [ ] , details : undefined } ;
266+ } ,
267+ } satisfies AnyAgentTool & {
268+ availability : { kind : "config" } ;
269+ } ;
270+
271+ const captured = capturePluginToolDescriptor ( {
272+ pluginId : "demo" ,
273+ tool,
274+ optional : false ,
275+ logger,
276+ } ) ;
277+
278+ expect ( captured . descriptor . availability ) . toBeUndefined ( ) ;
279+ expect ( logger . warn ) . not . toHaveBeenCalled ( ) ;
280+ } ) ;
281+ } ) ;
0 commit comments