@@ -67,8 +67,9 @@ Commit: docs: update installation instructions for v2.0
6767 pub fn extract_commit_message ( response : & str ) -> String {
6868 // Try to extract from <commit> tags first
6969 if let Some ( start) = response. find ( "<commit>" ) {
70- if let Some ( end) = response. find ( "</commit>" ) {
71- let commit = response[ start + 8 ..end] . trim ( ) ;
70+ let after_tag = start + 8 ;
71+ if let Some ( end) = response[ after_tag..] . find ( "</commit>" ) {
72+ let commit = response[ after_tag..after_tag + end] . trim ( ) ;
7273 return commit. to_string ( ) ;
7374 }
7475 }
@@ -135,3 +136,32 @@ Analyze the changes and provide a PR title in <title> tags.
135136 ( system_prompt. to_string ( ) , user_prompt)
136137 }
137138}
139+
140+ #[ cfg( test) ]
141+ mod tests {
142+ use super :: * ;
143+
144+ #[ test]
145+ fn test_extract_commit_message_normal ( ) {
146+ let response = "<analysis>Changed auth</analysis>\n <commit>fix(auth): handle null token</commit>" ;
147+ assert_eq ! (
148+ CommitPromptBuilder :: extract_commit_message( response) ,
149+ "fix(auth): handle null token"
150+ ) ;
151+ }
152+
153+ #[ test]
154+ fn test_extract_commit_message_malformed_closing_before_opening ( ) {
155+ // Malformed: closing tag appears before opening tag — should NOT panic
156+ let response = "text</commit> more <commit>feat: real message</commit>" ;
157+ let result = CommitPromptBuilder :: extract_commit_message ( response) ;
158+ assert ! ( !result. is_empty( ) ) ;
159+ }
160+
161+ #[ test]
162+ fn test_extract_commit_message_no_tags ( ) {
163+ let response = "Some analysis\n feat(auth): add login\n More text" ;
164+ let result = CommitPromptBuilder :: extract_commit_message ( response) ;
165+ assert ! ( !result. is_empty( ) ) ;
166+ }
167+ }
0 commit comments