@@ -48,3 +48,74 @@ pub async fn extract_document_content(
4848 status : "completed" . to_string ( ) ,
4949 } )
5050}
51+
52+ #[ cfg( test) ]
53+ mod tests {
54+ use super :: extract_document_content;
55+ use base64:: { engine:: general_purpose:: STANDARD as BASE64 , Engine } ;
56+
57+ #[ tokio:: test]
58+ async fn extract_document_content_text_plain_success ( ) {
59+ let file_base64 = BASE64 . encode ( b"Hello, Maple!" ) ;
60+
61+ let resp = extract_document_content (
62+ file_base64,
63+ "hello.txt" . to_string ( ) ,
64+ "text/plain" . to_string ( ) ,
65+ )
66+ . await
67+ . expect ( "expected text/plain extraction to succeed" ) ;
68+
69+ assert_eq ! ( resp. status, "completed" ) ;
70+ assert_eq ! ( resp. document. filename, "hello.txt" ) ;
71+ assert_eq ! ( resp. document. text_content, "Hello, Maple!" ) ;
72+ }
73+
74+ #[ tokio:: test]
75+ async fn extract_document_content_rejects_unsupported_file_type ( ) {
76+ let file_base64 = BASE64 . encode ( b"whatever" ) ;
77+
78+ let err = extract_document_content (
79+ file_base64,
80+ "file.bin" . to_string ( ) ,
81+ "application/octet-stream" . to_string ( ) ,
82+ )
83+ . await
84+ . expect_err ( "expected unsupported file type to error" ) ;
85+
86+ assert ! (
87+ err. contains( "Unsupported file type" ) ,
88+ "unexpected error: {err}"
89+ ) ;
90+ }
91+
92+ #[ tokio:: test]
93+ async fn extract_document_content_rejects_invalid_base64 ( ) {
94+ let err = extract_document_content (
95+ "not base64" . to_string ( ) ,
96+ "file.txt" . to_string ( ) ,
97+ "txt" . to_string ( ) ,
98+ )
99+ . await
100+ . expect_err ( "expected invalid base64 to error" ) ;
101+
102+ assert ! (
103+ err. contains( "Failed to decode base64 file" ) ,
104+ "unexpected error: {err}"
105+ ) ;
106+ }
107+
108+ #[ tokio:: test]
109+ async fn extract_document_content_rejects_invalid_utf8_for_text_files ( ) {
110+ let file_base64 = BASE64 . encode ( [ 0xff , 0xfe , 0xfd ] ) ;
111+
112+ let err = extract_document_content ( file_base64, "bad.txt" . to_string ( ) , "txt" . to_string ( ) )
113+ . await
114+ . expect_err ( "expected invalid utf-8 to error" ) ;
115+
116+ assert ! (
117+ err. contains( "Failed to decode text file" ) ,
118+ "unexpected error: {err}"
119+ ) ;
120+ }
121+ }
0 commit comments