konnektoren_core/challenges/custom/
custom_impl.rs

1use schemars::JsonSchema;
2use serde::{Deserialize, Serialize};
3
4#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, JsonSchema)]
5pub struct Custom {
6    /// Unique identifier for the custom challenge
7    pub id: String,
8    /// Display name of the challenge
9    pub name: String,
10    /// Description of the challenge
11    pub description: String,
12    /// HTML content for the challenge
13    pub html: String,
14    /// Optional HTML content for results display
15    pub results_html: Option<String>,
16    /// CSS styles for the challenge
17    pub css: String,
18    /// JavaScript code for the challenge
19    pub js: String,
20    /// Optional internationalization data
21    pub i18n: Option<String>,
22    /// Custom data for the challenge
23    pub data: serde_json::Value,
24    /// Optional task IDs for selection
25    pub task_ids: Option<Vec<usize>>,
26    /// Optional package URL for external content
27    pub package_url: Option<String>,
28}
29
30impl Default for Custom {
31    fn default() -> Self {
32        let data = include_str!("../../../assets/custom_default.yml");
33        serde_yaml::from_str(data).unwrap()
34    }
35}
36
37#[cfg(test)]
38mod tests {
39    use super::*;
40
41    #[test]
42    fn new_dataset() {
43        let id = "123".to_string();
44        let name = "Test".to_string();
45        let data = serde_json::json!({
46            "key": "value"
47        });
48        let dataset = Custom {
49            id: id.clone(),
50            name: name.clone(),
51            description: "".to_string(),
52            html: "".to_string(),
53            results_html: None,
54            css: "".to_string(),
55            js: "".to_string(),
56            i18n: None,
57            data: data.clone(),
58            task_ids: None,
59            package_url: None,
60        };
61
62        assert_eq!(dataset.id, id);
63        assert_eq!(dataset.name, name);
64        assert_eq!(dataset.data, data);
65    }
66
67    #[test]
68    fn serialize_dataset() {
69        let json_str = r#"{"id":"123","name":"Test","description":"","html":"","results_html":null,"css":"","js":"","i18n":null,"data":{"key":"value"},"task_ids":null,"package_url":null}"#;
70        let dataset = Custom {
71            id: "123".to_string(),
72            name: "Test".to_string(),
73            description: "".to_string(),
74            html: "".to_string(),
75            results_html: None,
76            css: "".to_string(),
77            js: "".to_string(),
78            i18n: None,
79            data: serde_json::json!({
80                "key": "value"
81            }),
82            task_ids: None,
83            package_url: None,
84        };
85
86        let serialized = serde_json::to_string(&dataset).unwrap();
87        assert_eq!(serialized, json_str);
88    }
89}