@@ -1254,7 +1254,37 @@ func TestElicitationSchemaValidation(t *testing.T) {
12541254 "enabled" : {Type : "boolean" , Default : json .RawMessage (`"not-a-boolean"` )},
12551255 },
12561256 },
1257- expectedError : "elicit schema property \" enabled\" has invalid default value, must be a boolean" ,
1257+ expectedError : "elicit schema property \" enabled\" has invalid default value, must be a bool" ,
1258+ },
1259+ {
1260+ name : "string with invalid default" ,
1261+ schema : & jsonschema.Schema {
1262+ Type : "object" ,
1263+ Properties : map [string ]* jsonschema.Schema {
1264+ "enabled" : {Type : "string" , Default : json .RawMessage ("true" )},
1265+ },
1266+ },
1267+ expectedError : "elicit schema property \" enabled\" has invalid default value, must be a string" ,
1268+ },
1269+ {
1270+ name : "integer with invalid default" ,
1271+ schema : & jsonschema.Schema {
1272+ Type : "object" ,
1273+ Properties : map [string ]* jsonschema.Schema {
1274+ "enabled" : {Type : "integer" , Default : json .RawMessage ("true" )},
1275+ },
1276+ },
1277+ expectedError : "elicit schema property \" enabled\" has default value that cannot be interpreted as an int or float" ,
1278+ },
1279+ {
1280+ name : "number with invalid default" ,
1281+ schema : & jsonschema.Schema {
1282+ Type : "object" ,
1283+ Properties : map [string ]* jsonschema.Schema {
1284+ "enabled" : {Type : "number" , Default : json .RawMessage ("true" )},
1285+ },
1286+ },
1287+ expectedError : "elicit schema property \" enabled\" has default value that cannot be interpreted as an int or float" ,
12581288 },
12591289 {
12601290 name : "enum with mismatched enumNames length" ,
@@ -1459,6 +1489,100 @@ func TestElicitationCapabilityDeclaration(t *testing.T) {
14591489 })
14601490}
14611491
1492+ func TestElicitationDefaultValues (t * testing.T ) {
1493+ ctx := context .Background ()
1494+ ct , st := NewInMemoryTransports ()
1495+
1496+ s := NewServer (testImpl , nil )
1497+ ss , err := s .Connect (ctx , st , nil )
1498+ if err != nil {
1499+ t .Fatal (err )
1500+ }
1501+ defer ss .Close ()
1502+
1503+ c := NewClient (testImpl , & ClientOptions {
1504+ ElicitationHandler : func (context.Context , * ElicitRequest ) (* ElicitResult , error ) {
1505+ return & ElicitResult {Action : "accept" , Content : map [string ]any {"default" : "response" }}, nil
1506+ },
1507+ })
1508+ cs , err := c .Connect (ctx , ct , nil )
1509+ if err != nil {
1510+ t .Fatal (err )
1511+ }
1512+ defer cs .Close ()
1513+
1514+ testcases := []struct {
1515+ name string
1516+ schema * jsonschema.Schema
1517+ expected map [string ]any
1518+ }{
1519+ {
1520+ name : "boolean with default" ,
1521+ schema : & jsonschema.Schema {
1522+ Type : "object" ,
1523+ Properties : map [string ]* jsonschema.Schema {
1524+ "key" : {Type : "boolean" , Default : json .RawMessage ("true" )},
1525+ },
1526+ },
1527+ expected : map [string ]any {"key" : true , "default" : "response" },
1528+ },
1529+ {
1530+ name : "string with default" ,
1531+ schema : & jsonschema.Schema {
1532+ Type : "object" ,
1533+ Properties : map [string ]* jsonschema.Schema {
1534+ "key" : {Type : "string" , Default : json .RawMessage ("\" potato\" " )},
1535+ },
1536+ },
1537+ expected : map [string ]any {"key" : "potato" , "default" : "response" },
1538+ },
1539+ {
1540+ name : "integer with default" ,
1541+ schema : & jsonschema.Schema {
1542+ Type : "object" ,
1543+ Properties : map [string ]* jsonschema.Schema {
1544+ "key" : {Type : "integer" , Default : json .RawMessage ("123" )},
1545+ },
1546+ },
1547+ expected : map [string ]any {"key" : float64 (123 ), "default" : "response" },
1548+ },
1549+ {
1550+ name : "number with default" ,
1551+ schema : & jsonschema.Schema {
1552+ Type : "object" ,
1553+ Properties : map [string ]* jsonschema.Schema {
1554+ "key" : {Type : "number" , Default : json .RawMessage ("89.7" )},
1555+ },
1556+ },
1557+ expected : map [string ]any {"key" : float64 (89.7 ), "default" : "response" },
1558+ },
1559+ {
1560+ name : "enum with default" ,
1561+ schema : & jsonschema.Schema {
1562+ Type : "object" ,
1563+ Properties : map [string ]* jsonschema.Schema {
1564+ "key" : {Type : "string" , Enum : []any {"one" , "two" }, Default : json .RawMessage ("\" one\" " )},
1565+ },
1566+ },
1567+ expected : map [string ]any {"key" : "one" , "default" : "response" },
1568+ },
1569+ }
1570+ for _ , tc := range testcases {
1571+ t .Run (tc .name , func (t * testing.T ) {
1572+ res , err := ss .Elicit (ctx , & ElicitParams {
1573+ Message : "Test schema with defaults: " + tc .name ,
1574+ RequestedSchema : tc .schema ,
1575+ })
1576+ if err != nil {
1577+ t .Fatalf ("expected no error for default schema %q, got: %v" , tc .name , err )
1578+ }
1579+ if diff := cmp .Diff (tc .expected , res .Content ); diff != "" {
1580+ t .Errorf ("%s: did not get expected value, -want +got:\n %s" , tc .name , diff )
1581+ }
1582+ })
1583+ }
1584+ }
1585+
14621586func TestKeepAliveFailure (t * testing.T ) {
14631587 ctx , cancel := context .WithTimeout (context .Background (), 5 * time .Second )
14641588 defer cancel ()
0 commit comments