@@ -11,6 +11,16 @@ const baseDescriptor: ToolDescriptor = {
1111 executor : { kind : "core" , executorId : "example" } ,
1212} ;
1313
14+ function descriptorWithAvailability ( availability : unknown ) : ToolDescriptor {
15+ return { ...baseDescriptor , availability } as ToolDescriptor ;
16+ }
17+
18+ function sparseArray ( ) : unknown [ ] {
19+ const values : unknown [ ] = [ ] ;
20+ values . length = 1 ;
21+ return values ;
22+ }
23+
1424function isRecord ( value : unknown ) : value is Record < string , unknown > {
1525 return Boolean ( value && typeof value === "object" && ! Array . isArray ( value ) ) ;
1626}
@@ -255,4 +265,68 @@ describe("evaluateToolAvailability", () => {
255265 } ) . map ( ( entry ) => entry . reason ) ,
256266 ) . toEqual ( [ "unsupported-signal" ] ) ;
257267 } ) ;
268+
269+ it . each ( [
270+ null ,
271+ "invalid" ,
272+ [ ] ,
273+ { kind : "auth" } ,
274+ { kind : "config" , path : "plugins.demo" } ,
275+ { kind : "config" , path : [ 1 ] } ,
276+ { kind : "config" , path : [ ] , check : "invalid" } ,
277+ { kind : "context" , key : "" , equals : { } } ,
278+ { allOf : "invalid" } ,
279+ { anyOf : [ null ] } ,
280+ { allOf : [ ] , anyOf : [ ] } ,
281+ { allOf : sparseArray ( ) } ,
282+ { anyOf : sparseArray ( ) } ,
283+ { kind : "config" , path : sparseArray ( ) } ,
284+ ] ) ( "rejects malformed availability without throwing: %j" , ( availability ) => {
285+ expect (
286+ evaluateToolAvailability ( { descriptor : descriptorWithAvailability ( availability ) } ) ,
287+ ) . toStrictEqual ( [
288+ {
289+ reason : "unsupported-signal" ,
290+ message : "Unsupported availability expression" ,
291+ } ,
292+ ] ) ;
293+ } ) ;
294+
295+ it ( "rejects cyclic availability expressions without overflowing" , ( ) => {
296+ const availability : { allOf : unknown [ ] } = { allOf : [ ] } ;
297+ availability . allOf . push ( availability ) ;
298+
299+ expect (
300+ evaluateToolAvailability ( { descriptor : descriptorWithAvailability ( availability ) } ) ,
301+ ) . toStrictEqual ( [
302+ {
303+ reason : "unsupported-signal" ,
304+ message : "Unsupported availability expression" ,
305+ } ,
306+ ] ) ;
307+ } ) ;
308+
309+ it ( "allows one availability expression to be shared between sibling branches" , ( ) => {
310+ const signal = { kind : "auth" , providerId : "openai" } as const ;
311+ const descriptor : ToolDescriptor = {
312+ ...baseDescriptor ,
313+ availability : { allOf : [ signal , signal ] } ,
314+ } ;
315+
316+ expect (
317+ evaluateToolAvailability ( {
318+ descriptor,
319+ context : { authProviderIds : new Set ( [ "openai" ] ) } ,
320+ } ) ,
321+ ) . toStrictEqual ( [ ] ) ;
322+ } ) ;
323+
324+ it . each ( [
325+ [ { allOf : [ ] } , "Empty availability allOf group" ] ,
326+ [ { anyOf : [ ] } , "Empty availability anyOf group" ] ,
327+ ] as const ) ( "preserves precise empty-group diagnostics" , ( availability , message ) => {
328+ expect (
329+ evaluateToolAvailability ( { descriptor : descriptorWithAvailability ( availability ) } ) ,
330+ ) . toStrictEqual ( [ { reason : "unsupported-signal" , message } ] ) ;
331+ } ) ;
258332} ) ;
0 commit comments