@@ -121,3 +121,91 @@ impl HistoryItem {
121121 }
122122 }
123123}
124+
125+ #[ cfg( test) ]
126+ mod tests {
127+ use super :: * ;
128+
129+ /// Example custom extra info for testing.
130+ /// Downstream crates can implement their own types like this.
131+ #[ derive( Debug , Clone , Default , PartialEq , Eq , Serialize , Deserialize ) ]
132+ struct CustomExtraInfo {
133+ mode : String ,
134+ tags : Vec < String > ,
135+ }
136+
137+ impl HistoryItemExtraInfo for CustomExtraInfo { }
138+
139+ #[ test]
140+ fn test_history_item_with_default_extra_info ( ) {
141+ let item = HistoryItem :: from_command_line ( "echo hello" ) ;
142+ assert_eq ! ( item. command_line, "echo hello" ) ;
143+ assert ! ( item. more_info. is_none( ) ) ;
144+ }
145+
146+ #[ test]
147+ fn test_history_item_with_custom_extra_info ( ) {
148+ let item: HistoryItem < CustomExtraInfo > = HistoryItem {
149+ id : None ,
150+ start_timestamp : None ,
151+ command_line : "echo hello" . to_string ( ) ,
152+ session_id : None ,
153+ hostname : None ,
154+ cwd : None ,
155+ duration : None ,
156+ exit_status : None ,
157+ more_info : Some ( CustomExtraInfo {
158+ mode : "shell" . to_string ( ) ,
159+ tags : vec ! [ "test" . to_string( ) ] ,
160+ } ) ,
161+ } ;
162+
163+ assert_eq ! ( item. command_line, "echo hello" ) ;
164+ let extra = item. more_info . unwrap ( ) ;
165+ assert_eq ! ( extra. mode, "shell" ) ;
166+ assert_eq ! ( extra. tags, vec![ "test" . to_string( ) ] ) ;
167+ }
168+
169+ #[ test]
170+ fn test_custom_extra_info_serialization ( ) {
171+ let item: HistoryItem < CustomExtraInfo > = HistoryItem {
172+ id : Some ( HistoryItemId :: new ( 1 ) ) ,
173+ start_timestamp : None ,
174+ command_line : "ls -la" . to_string ( ) ,
175+ session_id : None ,
176+ hostname : None ,
177+ cwd : Some ( "/home/user" . to_string ( ) ) ,
178+ duration : None ,
179+ exit_status : Some ( 0 ) ,
180+ more_info : Some ( CustomExtraInfo {
181+ mode : "r" . to_string ( ) ,
182+ tags : vec ! [ "data" . to_string( ) , "analysis" . to_string( ) ] ,
183+ } ) ,
184+ } ;
185+
186+ // Serialize to JSON
187+ let json = serde_json:: to_string ( & item) . expect ( "serialization should succeed" ) ;
188+ assert ! ( json. contains( "\" mode\" :\" r\" " ) ) ;
189+ assert ! ( json. contains( "\" tags\" :[\" data\" ,\" analysis\" ]" ) ) ;
190+
191+ // Deserialize back
192+ let deserialized: HistoryItem < CustomExtraInfo > =
193+ serde_json:: from_str ( & json) . expect ( "deserialization should succeed" ) ;
194+ assert_eq ! ( deserialized. command_line, "ls -la" ) ;
195+ assert_eq ! ( deserialized. more_info. as_ref( ) . unwrap( ) . mode, "r" ) ;
196+ }
197+
198+ #[ test]
199+ fn test_ignore_all_extra_info_serialization ( ) {
200+ let item = HistoryItem :: from_command_line ( "pwd" ) ;
201+
202+ // Serialize - more_info should be null
203+ let json = serde_json:: to_string ( & item) . expect ( "serialization should succeed" ) ;
204+ assert ! ( json. contains( "\" more_info\" :null" ) ) ;
205+
206+ // Deserialize back
207+ let deserialized: HistoryItem =
208+ serde_json:: from_str ( & json) . expect ( "deserialization should succeed" ) ;
209+ assert_eq ! ( deserialized. command_line, "pwd" ) ;
210+ }
211+ }
0 commit comments