1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
#[cfg(feature = "derive")]
mod tests {
use llm_toolkit::ToPrompt;
#[derive(ToPrompt, Debug, PartialEq)]
#[allow(dead_code)]
enum TestEnum {
/// First variant with doc
First,
#[prompt("Custom description")]
Second,
#[prompt(skip)]
Third,
Fourth,
}
#[test]
fn test_enum_prompt_generation() {
// Test instance prompt (single variant)
let instance = TestEnum::First;
let prompt = instance.to_prompt();
assert_eq!(prompt, "First: First variant with doc");
// Test schema prompt (all variants) - TypeScript union type format
let schema = TestEnum::prompt_schema();
assert!(schema.contains("type TestEnum ="));
assert!(schema.contains("| \"First\" // First variant with doc"));
assert!(schema.contains("| \"Second\" // Custom description"));
assert!(schema.contains("| \"Fourth\""));
assert!(schema.contains("Example value: \"First\""));
// Check that skipped variant is not included
assert!(!schema.contains("Third"));
}
#[test]
fn test_all_fallback_priorities() {
#[derive(ToPrompt)]
#[allow(dead_code)]
enum PriorityTest {
#[prompt(skip)]
SkipMe,
#[prompt("Custom override")]
/// This doc comment should be ignored
CustomOverride,
/// Uses doc comment
WithDoc,
PlainName,
}
// Test instance prompt
let instance = PriorityTest::WithDoc;
let prompt = instance.to_prompt();
assert_eq!(prompt, "WithDoc: Uses doc comment");
// Test schema prompt (all variants) - TypeScript union type format
let schema = PriorityTest::prompt_schema();
// Verify skip works
assert!(!schema.contains("SkipMe"));
// Verify variants are present in TypeScript format
assert!(schema.contains("type PriorityTest ="));
assert!(schema.contains("| \"CustomOverride\" // Custom override"));
assert!(schema.contains("| \"WithDoc\" // Uses doc comment"));
assert!(schema.contains("| \"PlainName\""));
assert!(schema.contains("Example value:"));
// Verify custom description overrides doc comment
assert!(!schema.contains("This doc comment should be ignored"));
}
#[test]
fn test_empty_enum() {
#[derive(ToPrompt)]
#[allow(dead_code)]
enum EmptyEnum {}
// This should compile and generate a prompt with no values
// (Though an empty enum isn't very useful in practice)
}
#[test]
fn test_all_variants_skipped() {
#[derive(ToPrompt)]
#[allow(dead_code)]
enum AllSkipped {
#[prompt(skip)]
A,
#[prompt(skip)]
B,
#[prompt(skip)]
C,
}
// Test instance prompt (even skipped variants have names)
let instance = AllSkipped::A;
let prompt = instance.to_prompt();
assert_eq!(prompt, "A");
// Test schema prompt (all variants are skipped, so schema shows empty type definition)
let schema = AllSkipped::prompt_schema();
assert!(schema.contains("type AllSkipped ="));
// With all variants skipped, there should be no variant definitions
// The type line should exist but with no variants following it
// Note: "AllSkipped" type name contains "A", so check for quoted variants specifically
assert!(!schema.contains("\"A\""));
assert!(!schema.contains("\"B\""));
assert!(!schema.contains("\"C\""));
}
#[test]
fn test_struct_default_prompt_generation() {
#[derive(ToPrompt)]
struct TestStruct {
name: String,
age: u32,
active: bool,
}
let instance = TestStruct {
name: "Yui".to_string(),
age: 28,
active: true,
};
let prompt = instance.to_prompt();
let expected_prompt = "name: Yui\nage: 28\nactive: true";
assert_eq!(prompt, expected_prompt);
}
#[test]
fn test_struct_skip_attribute() {
#[derive(ToPrompt)]
#[allow(dead_code)]
struct UserProfile {
username: String,
display_name: String,
#[prompt(skip)]
internal_id: u64,
}
let profile = UserProfile {
username: "yui".to_string(),
display_name: "Yui".to_string(),
internal_id: 12345,
};
let prompt = profile.to_prompt();
let expected_prompt = "username: yui\ndisplay_name: Yui";
assert_eq!(prompt, expected_prompt);
assert!(!prompt.contains("internal_id"));
}
#[test]
fn test_struct_priority_based_keys() {
#[derive(ToPrompt)]
#[allow(dead_code)]
struct PriorityTestStruct {
// Priority 3: Field name fallback
plain_field: String,
// Priority 2: Doc comment
/// User's full name
documented_field: String,
// Priority 1: Rename attribute (highest priority)
#[prompt(rename = "custom_key")]
renamed_field: String,
// Combined: rename takes priority over doc comment
/// This doc should be ignored
#[prompt(rename = "overridden")]
both_attrs: String,
// Skip attribute
#[prompt(skip)]
skipped_field: String,
}
let instance = PriorityTestStruct {
plain_field: "plain".to_string(),
documented_field: "doc".to_string(),
renamed_field: "renamed".to_string(),
both_attrs: "both".to_string(),
skipped_field: "skip".to_string(),
};
let prompt = instance.to_prompt();
// Test field name fallback
assert!(prompt.contains("plain_field: plain"));
// Test doc comment as key
assert!(prompt.contains("User's full name: doc"));
assert!(!prompt.contains("documented_field: doc"));
// Test rename attribute
assert!(prompt.contains("custom_key: renamed"));
assert!(!prompt.contains("renamed_field: renamed"));
// Test rename overrides doc comment
assert!(prompt.contains("overridden: both"));
assert!(!prompt.contains("This doc should be ignored"));
assert!(!prompt.contains("both_attrs: both"));
// Test skip
assert!(!prompt.contains("skipped_field"));
assert!(!prompt.contains("skip"));
}
#[test]
fn test_struct_multiple_attributes() {
#[derive(ToPrompt)]
struct ComplexStruct {
#[prompt(rename = "id")]
user_id: u32,
#[prompt(skip)]
#[allow(dead_code)]
_internal: String,
/// API endpoint URL
#[prompt(rename = "endpoint")]
api_url: String,
}
let instance = ComplexStruct {
user_id: 42,
_internal: "secret".to_string(),
api_url: "https://api.example.com".to_string(),
};
let prompt = instance.to_prompt();
assert!(prompt.contains("id: 42"));
assert!(prompt.contains("endpoint: https://api.example.com"));
assert!(!prompt.contains("user_id"));
assert!(!prompt.contains("api_url"));
assert!(!prompt.contains("_internal"));
assert!(!prompt.contains("secret"));
assert!(!prompt.contains("API endpoint URL"));
}
// TODO: Fix type inference issue when all fields are skipped
// #[test]
// fn test_struct_all_fields_skipped() {
// #[derive(ToPrompt)]
// struct AllSkippedStruct {
// #[prompt(skip)]
// field1: String,
// #[prompt(skip)]
// field2: u32,
// }
// let instance = AllSkippedStruct {
// field1: "test".to_string(),
// field2: 123,
// };
// let prompt = instance.to_prompt();
// assert_eq!(prompt, "");
// }
}