| 1 | use codewhale_models::schema_sanitize::{sanitize_for_kimi_parameters, sanitize_for_responses}; |
| 2 | use serde_json::{Value, json}; |
| 3 | |
| 4 | fn root_one_of_apply_patch_fixture() -> Value { |
| 5 | json!({ |
| 6 | "type": "object", |
| 7 | "properties": { |
| 8 | "patch": { "type": "string" }, |
| 9 | "replace": { |
| 10 | "type": "array", |
| 11 | "items": { |
| 12 | "type": "object", |
| 13 | "properties": { |
| 14 | "path": { "type": "string" }, |
| 15 | "content": { "type": "string" } |
| 16 | }, |
| 17 | "required": ["path", "content"] |
| 18 | } |
| 19 | }, |
| 20 | "changes": { |
| 21 | "type": "array", |
| 22 | "items": { |
| 23 | "type": "object", |
| 24 | "properties": { |
| 25 | "path": { "type": "string" }, |
| 26 | "content": { "type": "string" } |
| 27 | }, |
| 28 | "required": ["path", "content"] |
| 29 | } |
| 30 | } |
| 31 | }, |
| 32 | "oneOf": [ |
| 33 | { "required": ["patch"] }, |
| 34 | { "required": ["replace"] }, |
| 35 | { "required": ["changes"] } |
| 36 | ] |
| 37 | }) |
| 38 | } |
| 39 | |
| 40 | #[test] |
| 41 | fn xai_sanitize_flattens_apply_patch_root_one_of() { |
| 42 | let mut schema = root_one_of_apply_patch_fixture(); |
| 43 | assert!(schema.get("oneOf").is_some(), "fixture must match the tool"); |
| 44 | |
| 45 | let note = sanitize_for_responses(&mut schema); |
| 46 | |
| 47 | assert_eq!(schema["type"], "object"); |
| 48 | assert!(schema.get("oneOf").is_none()); |
| 49 | assert!(schema.get("anyOf").is_none()); |
| 50 | assert!(schema["properties"].get("patch").is_some()); |
| 51 | assert!(schema["properties"].get("changes").is_some()); |
| 52 | assert_eq!( |
| 53 | note.as_deref(), |
| 54 | Some( |
| 55 | "Exactly one of these parameter groups must be provided: `changes` | `patch` | `replace`." |
| 56 | ) |
| 57 | ); |
| 58 | } |
| 59 | |
| 60 | #[test] |
| 61 | fn kimi_parameters_flattens_actual_apply_patch_root_and_returns_constraint_note() { |
| 62 | let mut schema = root_one_of_apply_patch_fixture(); |
| 63 | |
| 64 | let note = sanitize_for_kimi_parameters(&mut schema).unwrap(); |
| 65 | |
| 66 | assert_eq!(schema["type"], "object"); |
| 67 | assert!(schema.get("oneOf").is_none()); |
| 68 | assert!(schema.get("anyOf").is_none()); |
| 69 | assert!(schema.get("allOf").is_none()); |
| 70 | assert_eq!(schema["properties"]["patch"]["type"], "string"); |
| 71 | assert_eq!(schema["properties"]["replace"]["type"], "array"); |
| 72 | assert_eq!(schema["properties"]["changes"]["type"], "array"); |
| 73 | assert_eq!( |
| 74 | note.as_deref(), |
| 75 | Some( |
| 76 | "Exactly one of these parameter groups must be provided: `changes` | `patch` | `replace`." |
| 77 | ) |
| 78 | ); |
| 79 | } |
| 80 |