kreuzberg 4.0.0.rc1 → 4.0.0.rc2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (342) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +14 -8
  3. data/.rspec +3 -3
  4. data/.rubocop.yaml +1 -534
  5. data/.rubocop.yml +538 -0
  6. data/Gemfile +8 -9
  7. data/Gemfile.lock +9 -109
  8. data/README.md +426 -421
  9. data/Rakefile +25 -25
  10. data/Steepfile +47 -47
  11. data/examples/async_patterns.rb +341 -340
  12. data/ext/kreuzberg_rb/extconf.rb +45 -35
  13. data/ext/kreuzberg_rb/native/Cargo.lock +6535 -0
  14. data/ext/kreuzberg_rb/native/Cargo.toml +44 -36
  15. data/ext/kreuzberg_rb/native/README.md +425 -425
  16. data/ext/kreuzberg_rb/native/build.rs +15 -17
  17. data/ext/kreuzberg_rb/native/include/ieeefp.h +11 -11
  18. data/ext/kreuzberg_rb/native/include/msvc_compat/strings.h +14 -14
  19. data/ext/kreuzberg_rb/native/include/strings.h +20 -20
  20. data/ext/kreuzberg_rb/native/include/unistd.h +47 -47
  21. data/ext/kreuzberg_rb/native/src/lib.rs +2998 -2939
  22. data/extconf.rb +28 -28
  23. data/kreuzberg.gemspec +148 -105
  24. data/lib/kreuzberg/api_proxy.rb +142 -142
  25. data/lib/kreuzberg/cache_api.rb +46 -45
  26. data/lib/kreuzberg/cli.rb +55 -55
  27. data/lib/kreuzberg/cli_proxy.rb +127 -127
  28. data/lib/kreuzberg/config.rb +691 -684
  29. data/lib/kreuzberg/error_context.rb +32 -0
  30. data/lib/kreuzberg/errors.rb +118 -50
  31. data/lib/kreuzberg/extraction_api.rb +85 -84
  32. data/lib/kreuzberg/mcp_proxy.rb +186 -186
  33. data/lib/kreuzberg/ocr_backend_protocol.rb +113 -113
  34. data/lib/kreuzberg/post_processor_protocol.rb +86 -86
  35. data/lib/kreuzberg/result.rb +216 -216
  36. data/lib/kreuzberg/setup_lib_path.rb +80 -79
  37. data/lib/kreuzberg/validator_protocol.rb +89 -89
  38. data/lib/kreuzberg/version.rb +5 -5
  39. data/lib/kreuzberg.rb +103 -82
  40. data/sig/kreuzberg/internal.rbs +184 -184
  41. data/sig/kreuzberg.rbs +520 -468
  42. data/spec/binding/cache_spec.rb +227 -227
  43. data/spec/binding/cli_proxy_spec.rb +85 -87
  44. data/spec/binding/cli_spec.rb +55 -54
  45. data/spec/binding/config_spec.rb +345 -345
  46. data/spec/binding/config_validation_spec.rb +283 -283
  47. data/spec/binding/error_handling_spec.rb +213 -213
  48. data/spec/binding/errors_spec.rb +66 -66
  49. data/spec/binding/plugins/ocr_backend_spec.rb +307 -307
  50. data/spec/binding/plugins/postprocessor_spec.rb +269 -269
  51. data/spec/binding/plugins/validator_spec.rb +274 -274
  52. data/spec/fixtures/config.toml +39 -39
  53. data/spec/fixtures/config.yaml +41 -42
  54. data/spec/fixtures/invalid_config.toml +4 -4
  55. data/spec/smoke/package_spec.rb +178 -178
  56. data/spec/spec_helper.rb +42 -42
  57. data/vendor/kreuzberg/Cargo.toml +204 -134
  58. data/vendor/kreuzberg/README.md +175 -175
  59. data/vendor/kreuzberg/benches/otel_overhead.rs +48 -0
  60. data/vendor/kreuzberg/build.rs +474 -460
  61. data/vendor/kreuzberg/src/api/error.rs +81 -81
  62. data/vendor/kreuzberg/src/api/handlers.rs +199 -199
  63. data/vendor/kreuzberg/src/api/mod.rs +79 -79
  64. data/vendor/kreuzberg/src/api/server.rs +353 -353
  65. data/vendor/kreuzberg/src/api/types.rs +170 -170
  66. data/vendor/kreuzberg/src/cache/mod.rs +1167 -1143
  67. data/vendor/kreuzberg/src/chunking/mod.rs +677 -677
  68. data/vendor/kreuzberg/src/core/batch_mode.rs +95 -35
  69. data/vendor/kreuzberg/src/core/config.rs +1032 -1032
  70. data/vendor/kreuzberg/src/core/extractor.rs +1024 -903
  71. data/vendor/kreuzberg/src/core/io.rs +329 -327
  72. data/vendor/kreuzberg/src/core/mime.rs +605 -615
  73. data/vendor/kreuzberg/src/core/mod.rs +45 -42
  74. data/vendor/kreuzberg/src/core/pipeline.rs +984 -906
  75. data/vendor/kreuzberg/src/embeddings.rs +432 -323
  76. data/vendor/kreuzberg/src/error.rs +431 -431
  77. data/vendor/kreuzberg/src/extraction/archive.rs +954 -954
  78. data/vendor/kreuzberg/src/extraction/docx.rs +40 -40
  79. data/vendor/kreuzberg/src/extraction/email.rs +854 -854
  80. data/vendor/kreuzberg/src/extraction/excel.rs +688 -688
  81. data/vendor/kreuzberg/src/extraction/html.rs +553 -553
  82. data/vendor/kreuzberg/src/extraction/image.rs +368 -368
  83. data/vendor/kreuzberg/src/extraction/libreoffice.rs +563 -564
  84. data/vendor/kreuzberg/src/extraction/markdown.rs +213 -0
  85. data/vendor/kreuzberg/src/extraction/mod.rs +81 -77
  86. data/vendor/kreuzberg/src/extraction/office_metadata/app_properties.rs +398 -398
  87. data/vendor/kreuzberg/src/extraction/office_metadata/core_properties.rs +247 -247
  88. data/vendor/kreuzberg/src/extraction/office_metadata/custom_properties.rs +240 -240
  89. data/vendor/kreuzberg/src/extraction/office_metadata/mod.rs +130 -128
  90. data/vendor/kreuzberg/src/extraction/office_metadata/odt_properties.rs +287 -0
  91. data/vendor/kreuzberg/src/extraction/pptx.rs +3000 -3000
  92. data/vendor/kreuzberg/src/extraction/structured.rs +490 -490
  93. data/vendor/kreuzberg/src/extraction/table.rs +328 -328
  94. data/vendor/kreuzberg/src/extraction/text.rs +269 -269
  95. data/vendor/kreuzberg/src/extraction/xml.rs +333 -333
  96. data/vendor/kreuzberg/src/extractors/archive.rs +446 -425
  97. data/vendor/kreuzberg/src/extractors/bibtex.rs +469 -0
  98. data/vendor/kreuzberg/src/extractors/docbook.rs +502 -0
  99. data/vendor/kreuzberg/src/extractors/docx.rs +367 -479
  100. data/vendor/kreuzberg/src/extractors/email.rs +143 -129
  101. data/vendor/kreuzberg/src/extractors/epub.rs +707 -0
  102. data/vendor/kreuzberg/src/extractors/excel.rs +343 -344
  103. data/vendor/kreuzberg/src/extractors/fictionbook.rs +491 -0
  104. data/vendor/kreuzberg/src/extractors/fictionbook.rs.backup2 +738 -0
  105. data/vendor/kreuzberg/src/extractors/html.rs +393 -410
  106. data/vendor/kreuzberg/src/extractors/image.rs +198 -195
  107. data/vendor/kreuzberg/src/extractors/jats.rs +1051 -0
  108. data/vendor/kreuzberg/src/extractors/jupyter.rs +367 -0
  109. data/vendor/kreuzberg/src/extractors/latex.rs +652 -0
  110. data/vendor/kreuzberg/src/extractors/markdown.rs +700 -0
  111. data/vendor/kreuzberg/src/extractors/mod.rs +365 -268
  112. data/vendor/kreuzberg/src/extractors/odt.rs +628 -0
  113. data/vendor/kreuzberg/src/extractors/opml.rs +634 -0
  114. data/vendor/kreuzberg/src/extractors/orgmode.rs +528 -0
  115. data/vendor/kreuzberg/src/extractors/pdf.rs +493 -496
  116. data/vendor/kreuzberg/src/extractors/pptx.rs +248 -234
  117. data/vendor/kreuzberg/src/extractors/rst.rs +576 -0
  118. data/vendor/kreuzberg/src/extractors/rtf.rs +810 -0
  119. data/vendor/kreuzberg/src/extractors/security.rs +484 -0
  120. data/vendor/kreuzberg/src/extractors/security_tests.rs +367 -0
  121. data/vendor/kreuzberg/src/extractors/structured.rs +140 -126
  122. data/vendor/kreuzberg/src/extractors/text.rs +260 -242
  123. data/vendor/kreuzberg/src/extractors/typst.rs +650 -0
  124. data/vendor/kreuzberg/src/extractors/xml.rs +135 -128
  125. data/vendor/kreuzberg/src/image/dpi.rs +164 -164
  126. data/vendor/kreuzberg/src/image/mod.rs +6 -6
  127. data/vendor/kreuzberg/src/image/preprocessing.rs +417 -417
  128. data/vendor/kreuzberg/src/image/resize.rs +89 -89
  129. data/vendor/kreuzberg/src/keywords/config.rs +154 -154
  130. data/vendor/kreuzberg/src/keywords/mod.rs +237 -237
  131. data/vendor/kreuzberg/src/keywords/processor.rs +267 -267
  132. data/vendor/kreuzberg/src/keywords/rake.rs +293 -294
  133. data/vendor/kreuzberg/src/keywords/types.rs +68 -68
  134. data/vendor/kreuzberg/src/keywords/yake.rs +163 -163
  135. data/vendor/kreuzberg/src/language_detection/mod.rs +942 -942
  136. data/vendor/kreuzberg/src/lib.rs +105 -102
  137. data/vendor/kreuzberg/src/mcp/mod.rs +32 -32
  138. data/vendor/kreuzberg/src/mcp/server.rs +1968 -1966
  139. data/vendor/kreuzberg/src/ocr/cache.rs +469 -469
  140. data/vendor/kreuzberg/src/ocr/error.rs +37 -37
  141. data/vendor/kreuzberg/src/ocr/hocr.rs +216 -216
  142. data/vendor/kreuzberg/src/ocr/mod.rs +58 -58
  143. data/vendor/kreuzberg/src/ocr/processor.rs +863 -847
  144. data/vendor/kreuzberg/src/ocr/table/mod.rs +4 -4
  145. data/vendor/kreuzberg/src/ocr/table/tsv_parser.rs +144 -144
  146. data/vendor/kreuzberg/src/ocr/tesseract_backend.rs +450 -450
  147. data/vendor/kreuzberg/src/ocr/types.rs +393 -393
  148. data/vendor/kreuzberg/src/ocr/utils.rs +47 -47
  149. data/vendor/kreuzberg/src/ocr/validation.rs +206 -206
  150. data/vendor/kreuzberg/src/panic_context.rs +154 -0
  151. data/vendor/kreuzberg/src/pdf/error.rs +122 -122
  152. data/vendor/kreuzberg/src/pdf/images.rs +139 -139
  153. data/vendor/kreuzberg/src/pdf/metadata.rs +346 -346
  154. data/vendor/kreuzberg/src/pdf/mod.rs +50 -50
  155. data/vendor/kreuzberg/src/pdf/rendering.rs +369 -369
  156. data/vendor/kreuzberg/src/pdf/table.rs +393 -420
  157. data/vendor/kreuzberg/src/pdf/text.rs +158 -161
  158. data/vendor/kreuzberg/src/plugins/extractor.rs +1013 -1010
  159. data/vendor/kreuzberg/src/plugins/mod.rs +209 -209
  160. data/vendor/kreuzberg/src/plugins/ocr.rs +620 -629
  161. data/vendor/kreuzberg/src/plugins/processor.rs +642 -641
  162. data/vendor/kreuzberg/src/plugins/registry.rs +1337 -1324
  163. data/vendor/kreuzberg/src/plugins/traits.rs +258 -258
  164. data/vendor/kreuzberg/src/plugins/validator.rs +956 -955
  165. data/vendor/kreuzberg/src/stopwords/mod.rs +1470 -1470
  166. data/vendor/kreuzberg/src/text/mod.rs +19 -19
  167. data/vendor/kreuzberg/src/text/quality.rs +697 -697
  168. data/vendor/kreuzberg/src/text/string_utils.rs +217 -217
  169. data/vendor/kreuzberg/src/text/token_reduction/cjk_utils.rs +164 -164
  170. data/vendor/kreuzberg/src/text/token_reduction/config.rs +100 -100
  171. data/vendor/kreuzberg/src/text/token_reduction/core.rs +796 -796
  172. data/vendor/kreuzberg/src/text/token_reduction/filters.rs +902 -902
  173. data/vendor/kreuzberg/src/text/token_reduction/mod.rs +160 -160
  174. data/vendor/kreuzberg/src/text/token_reduction/semantic.rs +619 -619
  175. data/vendor/kreuzberg/src/text/token_reduction/simd_text.rs +147 -147
  176. data/vendor/kreuzberg/src/types.rs +903 -873
  177. data/vendor/kreuzberg/src/utils/mod.rs +17 -17
  178. data/vendor/kreuzberg/src/utils/quality.rs +959 -959
  179. data/vendor/kreuzberg/src/utils/string_utils.rs +381 -381
  180. data/vendor/kreuzberg/stopwords/af_stopwords.json +53 -53
  181. data/vendor/kreuzberg/stopwords/ar_stopwords.json +482 -482
  182. data/vendor/kreuzberg/stopwords/bg_stopwords.json +261 -261
  183. data/vendor/kreuzberg/stopwords/bn_stopwords.json +400 -400
  184. data/vendor/kreuzberg/stopwords/br_stopwords.json +1205 -1205
  185. data/vendor/kreuzberg/stopwords/ca_stopwords.json +280 -280
  186. data/vendor/kreuzberg/stopwords/cs_stopwords.json +425 -425
  187. data/vendor/kreuzberg/stopwords/da_stopwords.json +172 -172
  188. data/vendor/kreuzberg/stopwords/de_stopwords.json +622 -622
  189. data/vendor/kreuzberg/stopwords/el_stopwords.json +849 -849
  190. data/vendor/kreuzberg/stopwords/en_stopwords.json +1300 -1300
  191. data/vendor/kreuzberg/stopwords/eo_stopwords.json +175 -175
  192. data/vendor/kreuzberg/stopwords/es_stopwords.json +734 -734
  193. data/vendor/kreuzberg/stopwords/et_stopwords.json +37 -37
  194. data/vendor/kreuzberg/stopwords/eu_stopwords.json +100 -100
  195. data/vendor/kreuzberg/stopwords/fa_stopwords.json +801 -801
  196. data/vendor/kreuzberg/stopwords/fi_stopwords.json +849 -849
  197. data/vendor/kreuzberg/stopwords/fr_stopwords.json +693 -693
  198. data/vendor/kreuzberg/stopwords/ga_stopwords.json +111 -111
  199. data/vendor/kreuzberg/stopwords/gl_stopwords.json +162 -162
  200. data/vendor/kreuzberg/stopwords/gu_stopwords.json +226 -226
  201. data/vendor/kreuzberg/stopwords/ha_stopwords.json +41 -41
  202. data/vendor/kreuzberg/stopwords/he_stopwords.json +196 -196
  203. data/vendor/kreuzberg/stopwords/hi_stopwords.json +227 -227
  204. data/vendor/kreuzberg/stopwords/hr_stopwords.json +181 -181
  205. data/vendor/kreuzberg/stopwords/hu_stopwords.json +791 -791
  206. data/vendor/kreuzberg/stopwords/hy_stopwords.json +47 -47
  207. data/vendor/kreuzberg/stopwords/id_stopwords.json +760 -760
  208. data/vendor/kreuzberg/stopwords/it_stopwords.json +634 -634
  209. data/vendor/kreuzberg/stopwords/ja_stopwords.json +136 -136
  210. data/vendor/kreuzberg/stopwords/kn_stopwords.json +84 -84
  211. data/vendor/kreuzberg/stopwords/ko_stopwords.json +681 -681
  212. data/vendor/kreuzberg/stopwords/ku_stopwords.json +64 -64
  213. data/vendor/kreuzberg/stopwords/la_stopwords.json +51 -51
  214. data/vendor/kreuzberg/stopwords/lt_stopwords.json +476 -476
  215. data/vendor/kreuzberg/stopwords/lv_stopwords.json +163 -163
  216. data/vendor/kreuzberg/stopwords/ml_stopwords.json +1 -1
  217. data/vendor/kreuzberg/stopwords/mr_stopwords.json +101 -101
  218. data/vendor/kreuzberg/stopwords/ms_stopwords.json +477 -477
  219. data/vendor/kreuzberg/stopwords/ne_stopwords.json +490 -490
  220. data/vendor/kreuzberg/stopwords/nl_stopwords.json +415 -415
  221. data/vendor/kreuzberg/stopwords/no_stopwords.json +223 -223
  222. data/vendor/kreuzberg/stopwords/pl_stopwords.json +331 -331
  223. data/vendor/kreuzberg/stopwords/pt_stopwords.json +562 -562
  224. data/vendor/kreuzberg/stopwords/ro_stopwords.json +436 -436
  225. data/vendor/kreuzberg/stopwords/ru_stopwords.json +561 -561
  226. data/vendor/kreuzberg/stopwords/si_stopwords.json +193 -193
  227. data/vendor/kreuzberg/stopwords/sk_stopwords.json +420 -420
  228. data/vendor/kreuzberg/stopwords/sl_stopwords.json +448 -448
  229. data/vendor/kreuzberg/stopwords/so_stopwords.json +32 -32
  230. data/vendor/kreuzberg/stopwords/st_stopwords.json +33 -33
  231. data/vendor/kreuzberg/stopwords/sv_stopwords.json +420 -420
  232. data/vendor/kreuzberg/stopwords/sw_stopwords.json +76 -76
  233. data/vendor/kreuzberg/stopwords/ta_stopwords.json +129 -129
  234. data/vendor/kreuzberg/stopwords/te_stopwords.json +54 -54
  235. data/vendor/kreuzberg/stopwords/th_stopwords.json +118 -118
  236. data/vendor/kreuzberg/stopwords/tl_stopwords.json +149 -149
  237. data/vendor/kreuzberg/stopwords/tr_stopwords.json +506 -506
  238. data/vendor/kreuzberg/stopwords/uk_stopwords.json +75 -75
  239. data/vendor/kreuzberg/stopwords/ur_stopwords.json +519 -519
  240. data/vendor/kreuzberg/stopwords/vi_stopwords.json +647 -647
  241. data/vendor/kreuzberg/stopwords/yo_stopwords.json +62 -62
  242. data/vendor/kreuzberg/stopwords/zh_stopwords.json +796 -796
  243. data/vendor/kreuzberg/stopwords/zu_stopwords.json +31 -31
  244. data/vendor/kreuzberg/tests/api_extract_multipart.rs +52 -0
  245. data/vendor/kreuzberg/tests/api_tests.rs +966 -966
  246. data/vendor/kreuzberg/tests/archive_integration.rs +543 -543
  247. data/vendor/kreuzberg/tests/batch_orchestration.rs +556 -542
  248. data/vendor/kreuzberg/tests/batch_processing.rs +316 -304
  249. data/vendor/kreuzberg/tests/bibtex_parity_test.rs +421 -0
  250. data/vendor/kreuzberg/tests/concurrency_stress.rs +525 -509
  251. data/vendor/kreuzberg/tests/config_features.rs +598 -580
  252. data/vendor/kreuzberg/tests/config_loading_tests.rs +415 -439
  253. data/vendor/kreuzberg/tests/core_integration.rs +510 -493
  254. data/vendor/kreuzberg/tests/csv_integration.rs +414 -424
  255. data/vendor/kreuzberg/tests/docbook_extractor_tests.rs +498 -0
  256. data/vendor/kreuzberg/tests/docx_metadata_extraction_test.rs +122 -124
  257. data/vendor/kreuzberg/tests/docx_vs_pandoc_comparison.rs +370 -0
  258. data/vendor/kreuzberg/tests/email_integration.rs +325 -325
  259. data/vendor/kreuzberg/tests/epub_native_extractor_tests.rs +275 -0
  260. data/vendor/kreuzberg/tests/error_handling.rs +393 -393
  261. data/vendor/kreuzberg/tests/fictionbook_extractor_tests.rs +228 -0
  262. data/vendor/kreuzberg/tests/format_integration.rs +159 -159
  263. data/vendor/kreuzberg/tests/helpers/mod.rs +142 -142
  264. data/vendor/kreuzberg/tests/html_table_test.rs +551 -0
  265. data/vendor/kreuzberg/tests/image_integration.rs +253 -253
  266. data/vendor/kreuzberg/tests/instrumentation_test.rs +139 -0
  267. data/vendor/kreuzberg/tests/jats_extractor_tests.rs +639 -0
  268. data/vendor/kreuzberg/tests/jupyter_extractor_tests.rs +704 -0
  269. data/vendor/kreuzberg/tests/keywords_integration.rs +479 -479
  270. data/vendor/kreuzberg/tests/keywords_quality.rs +509 -509
  271. data/vendor/kreuzberg/tests/latex_extractor_tests.rs +496 -0
  272. data/vendor/kreuzberg/tests/markdown_extractor_tests.rs +490 -0
  273. data/vendor/kreuzberg/tests/mime_detection.rs +428 -428
  274. data/vendor/kreuzberg/tests/ocr_configuration.rs +510 -510
  275. data/vendor/kreuzberg/tests/ocr_errors.rs +676 -676
  276. data/vendor/kreuzberg/tests/ocr_quality.rs +627 -627
  277. data/vendor/kreuzberg/tests/ocr_stress.rs +469 -469
  278. data/vendor/kreuzberg/tests/odt_extractor_tests.rs +695 -0
  279. data/vendor/kreuzberg/tests/opml_extractor_tests.rs +616 -0
  280. data/vendor/kreuzberg/tests/orgmode_extractor_tests.rs +822 -0
  281. data/vendor/kreuzberg/tests/pdf_integration.rs +43 -43
  282. data/vendor/kreuzberg/tests/pipeline_integration.rs +1411 -1412
  283. data/vendor/kreuzberg/tests/plugin_ocr_backend_test.rs +771 -771
  284. data/vendor/kreuzberg/tests/plugin_postprocessor_test.rs +560 -561
  285. data/vendor/kreuzberg/tests/plugin_system.rs +921 -921
  286. data/vendor/kreuzberg/tests/plugin_validator_test.rs +783 -783
  287. data/vendor/kreuzberg/tests/registry_integration_tests.rs +586 -607
  288. data/vendor/kreuzberg/tests/rst_extractor_tests.rs +692 -0
  289. data/vendor/kreuzberg/tests/rtf_extractor_tests.rs +776 -0
  290. data/vendor/kreuzberg/tests/security_validation.rs +415 -404
  291. data/vendor/kreuzberg/tests/stopwords_integration_test.rs +888 -888
  292. data/vendor/kreuzberg/tests/test_fastembed.rs +609 -609
  293. data/vendor/kreuzberg/tests/typst_behavioral_tests.rs +1259 -0
  294. data/vendor/kreuzberg/tests/typst_extractor_tests.rs +647 -0
  295. data/vendor/kreuzberg/tests/xlsx_metadata_extraction_test.rs +87 -87
  296. data/vendor/rb-sys/.cargo-ok +1 -0
  297. data/vendor/rb-sys/.cargo_vcs_info.json +6 -0
  298. data/vendor/rb-sys/Cargo.lock +393 -0
  299. data/vendor/rb-sys/Cargo.toml +70 -0
  300. data/vendor/rb-sys/Cargo.toml.orig +57 -0
  301. data/vendor/rb-sys/LICENSE-APACHE +190 -0
  302. data/vendor/rb-sys/LICENSE-MIT +21 -0
  303. data/vendor/rb-sys/bin/release.sh +21 -0
  304. data/vendor/rb-sys/build/features.rs +108 -0
  305. data/vendor/rb-sys/build/main.rs +246 -0
  306. data/vendor/rb-sys/build/stable_api_config.rs +153 -0
  307. data/vendor/rb-sys/build/version.rs +48 -0
  308. data/vendor/rb-sys/readme.md +36 -0
  309. data/vendor/rb-sys/src/bindings.rs +21 -0
  310. data/vendor/rb-sys/src/hidden.rs +11 -0
  311. data/vendor/rb-sys/src/lib.rs +34 -0
  312. data/vendor/rb-sys/src/macros.rs +371 -0
  313. data/vendor/rb-sys/src/memory.rs +53 -0
  314. data/vendor/rb-sys/src/ruby_abi_version.rs +38 -0
  315. data/vendor/rb-sys/src/special_consts.rs +31 -0
  316. data/vendor/rb-sys/src/stable_api/compiled.c +179 -0
  317. data/vendor/rb-sys/src/stable_api/compiled.rs +257 -0
  318. data/vendor/rb-sys/src/stable_api/ruby_2_6.rs +316 -0
  319. data/vendor/rb-sys/src/stable_api/ruby_2_7.rs +316 -0
  320. data/vendor/rb-sys/src/stable_api/ruby_3_0.rs +324 -0
  321. data/vendor/rb-sys/src/stable_api/ruby_3_1.rs +317 -0
  322. data/vendor/rb-sys/src/stable_api/ruby_3_2.rs +315 -0
  323. data/vendor/rb-sys/src/stable_api/ruby_3_3.rs +326 -0
  324. data/vendor/rb-sys/src/stable_api/ruby_3_4.rs +327 -0
  325. data/vendor/rb-sys/src/stable_api.rs +261 -0
  326. data/vendor/rb-sys/src/symbol.rs +31 -0
  327. data/vendor/rb-sys/src/tracking_allocator.rs +332 -0
  328. data/vendor/rb-sys/src/utils.rs +89 -0
  329. data/vendor/rb-sys/src/value_type.rs +7 -0
  330. metadata +90 -95
  331. data/pkg/kreuzberg-4.0.0.rc1.gem +0 -0
  332. data/spec/examples.txt +0 -104
  333. data/vendor/kreuzberg/src/bin/profile_extract.rs +0 -455
  334. data/vendor/kreuzberg/src/extraction/pandoc/batch.rs +0 -275
  335. data/vendor/kreuzberg/src/extraction/pandoc/mime_types.rs +0 -178
  336. data/vendor/kreuzberg/src/extraction/pandoc/mod.rs +0 -491
  337. data/vendor/kreuzberg/src/extraction/pandoc/server.rs +0 -496
  338. data/vendor/kreuzberg/src/extraction/pandoc/subprocess.rs +0 -1188
  339. data/vendor/kreuzberg/src/extraction/pandoc/version.rs +0 -162
  340. data/vendor/kreuzberg/src/extractors/pandoc.rs +0 -201
  341. data/vendor/kreuzberg/tests/chunking_offset_demo.rs +0 -92
  342. data/vendor/kreuzberg/tests/pandoc_integration.rs +0 -503
@@ -1,966 +1,966 @@
1
- //! Integration tests for the API module.
2
-
3
- #![cfg(feature = "api")]
4
-
5
- use axum::{
6
- body::Body,
7
- http::{Request, StatusCode},
8
- };
9
- use serde_json::json;
10
- use tower::ServiceExt;
11
-
12
- use kreuzberg::{
13
- ExtractionConfig,
14
- api::{HealthResponse, InfoResponse, create_router},
15
- };
16
-
17
- /// Test the health check endpoint.
18
- #[tokio::test]
19
- async fn test_health_endpoint() {
20
- let app = create_router(ExtractionConfig::default());
21
-
22
- let response = app
23
- .oneshot(Request::builder().uri("/health").body(Body::empty()).unwrap())
24
- .await
25
- .unwrap();
26
-
27
- assert_eq!(response.status(), StatusCode::OK);
28
-
29
- let body = axum::body::to_bytes(response.into_body(), usize::MAX).await.unwrap();
30
- let health: HealthResponse = serde_json::from_slice(&body).unwrap();
31
-
32
- assert_eq!(health.status, "healthy");
33
- assert!(!health.version.is_empty());
34
- }
35
-
36
- /// Test the info endpoint.
37
- #[tokio::test]
38
- async fn test_info_endpoint() {
39
- let app = create_router(ExtractionConfig::default());
40
-
41
- let response = app
42
- .oneshot(Request::builder().uri("/info").body(Body::empty()).unwrap())
43
- .await
44
- .unwrap();
45
-
46
- assert_eq!(response.status(), StatusCode::OK);
47
-
48
- let body = axum::body::to_bytes(response.into_body(), usize::MAX).await.unwrap();
49
- let info: InfoResponse = serde_json::from_slice(&body).unwrap();
50
-
51
- assert!(!info.version.is_empty());
52
- assert!(info.rust_backend);
53
- }
54
-
55
- /// Test extract endpoint with no files returns 400.
56
- #[tokio::test]
57
- async fn test_extract_no_files() {
58
- let app = create_router(ExtractionConfig::default());
59
-
60
- let boundary = "----boundary";
61
- let body_content = format!("--{}--\r\n", boundary);
62
-
63
- let response = app
64
- .oneshot(
65
- Request::builder()
66
- .method("POST")
67
- .uri("/extract")
68
- .header("content-type", format!("multipart/form-data; boundary={}", boundary))
69
- .body(Body::from(body_content))
70
- .unwrap(),
71
- )
72
- .await
73
- .unwrap();
74
-
75
- assert_eq!(response.status(), StatusCode::BAD_REQUEST);
76
- }
77
-
78
- /// Test extract endpoint with a simple text file.
79
- #[tokio::test]
80
- async fn test_extract_text_file() {
81
- let app = create_router(ExtractionConfig::default());
82
-
83
- let boundary = "----boundary";
84
- let file_content = "Hello, world!";
85
-
86
- let body_content = format!(
87
- "--{}\r\n\
88
- Content-Disposition: form-data; name=\"files\"; filename=\"test.txt\"\r\n\
89
- Content-Type: text/plain\r\n\
90
- \r\n\
91
- {}\r\n\
92
- --{}--\r\n",
93
- boundary, file_content, boundary
94
- );
95
-
96
- let response = app
97
- .oneshot(
98
- Request::builder()
99
- .method("POST")
100
- .uri("/extract")
101
- .header("content-type", format!("multipart/form-data; boundary={}", boundary))
102
- .body(Body::from(body_content))
103
- .unwrap(),
104
- )
105
- .await
106
- .unwrap();
107
-
108
- assert_eq!(response.status(), StatusCode::OK);
109
-
110
- let body = axum::body::to_bytes(response.into_body(), usize::MAX).await.unwrap();
111
- let results: Vec<serde_json::Value> = serde_json::from_slice(&body).unwrap();
112
-
113
- assert_eq!(results.len(), 1);
114
- assert_eq!(results[0]["mime_type"], "text/plain");
115
- assert!(results[0]["content"].as_str().unwrap().contains("Hello, world!"));
116
-
117
- assert!(
118
- results[0]["chunks"].is_null(),
119
- "Chunks should be null without chunking config"
120
- );
121
- assert!(
122
- results[0]["detected_languages"].is_null(),
123
- "Language detection not enabled"
124
- );
125
- assert!(results[0]["tables"].is_array(), "Tables field should be present");
126
- assert!(results[0]["metadata"].is_object(), "Metadata field should be present");
127
- }
128
-
129
- /// Test extract endpoint with JSON config.
130
- #[tokio::test]
131
- async fn test_extract_with_config() {
132
- let app = create_router(ExtractionConfig::default());
133
-
134
- let boundary = "----boundary";
135
- let file_content = "Hello, world!";
136
- let config = json!({
137
- "force_ocr": false
138
- });
139
-
140
- let body_content = format!(
141
- "--{}\r\n\
142
- Content-Disposition: form-data; name=\"files\"; filename=\"test.txt\"\r\n\
143
- Content-Type: text/plain\r\n\
144
- \r\n\
145
- {}\r\n\
146
- --{}\r\n\
147
- Content-Disposition: form-data; name=\"config\"\r\n\
148
- \r\n\
149
- {}\r\n\
150
- --{}--\r\n",
151
- boundary, file_content, boundary, config, boundary
152
- );
153
-
154
- let response = app
155
- .oneshot(
156
- Request::builder()
157
- .method("POST")
158
- .uri("/extract")
159
- .header("content-type", format!("multipart/form-data; boundary={}", boundary))
160
- .body(Body::from(body_content))
161
- .unwrap(),
162
- )
163
- .await
164
- .unwrap();
165
-
166
- assert_eq!(response.status(), StatusCode::OK);
167
-
168
- let body = axum::body::to_bytes(response.into_body(), usize::MAX).await.unwrap();
169
- let results: Vec<serde_json::Value> = serde_json::from_slice(&body).unwrap();
170
-
171
- assert_eq!(results.len(), 1);
172
- assert_eq!(results[0]["mime_type"], "text/plain");
173
- assert!(results[0]["content"].as_str().unwrap().contains("Hello, world!"));
174
-
175
- assert!(
176
- results[0]["chunks"].is_null(),
177
- "Chunks should be null without chunking config"
178
- );
179
- assert!(
180
- results[0]["detected_languages"].is_null(),
181
- "Language detection not enabled"
182
- );
183
- assert!(results[0]["tables"].is_array(), "Tables field should be present");
184
- assert!(results[0]["metadata"].is_object(), "Metadata field should be present");
185
- }
186
-
187
- /// Test extract endpoint with invalid config returns 400.
188
- #[tokio::test]
189
- async fn test_extract_invalid_config() {
190
- let app = create_router(ExtractionConfig::default());
191
-
192
- let boundary = "----boundary";
193
- let file_content = "Hello, world!";
194
- let invalid_config = "not valid json";
195
-
196
- let body_content = format!(
197
- "--{}\r\n\
198
- Content-Disposition: form-data; name=\"files\"; filename=\"test.txt\"\r\n\
199
- Content-Type: text/plain\r\n\
200
- \r\n\
201
- {}\r\n\
202
- --{}\r\n\
203
- Content-Disposition: form-data; name=\"config\"\r\n\
204
- \r\n\
205
- {}\r\n\
206
- --{}--\r\n",
207
- boundary, file_content, boundary, invalid_config, boundary
208
- );
209
-
210
- let response = app
211
- .oneshot(
212
- Request::builder()
213
- .method("POST")
214
- .uri("/extract")
215
- .header("content-type", format!("multipart/form-data; boundary={}", boundary))
216
- .body(Body::from(body_content))
217
- .unwrap(),
218
- )
219
- .await
220
- .unwrap();
221
-
222
- assert_eq!(response.status(), StatusCode::BAD_REQUEST);
223
- }
224
-
225
- /// Test extract endpoint with multiple files.
226
- #[tokio::test]
227
- async fn test_extract_multiple_files() {
228
- let app = create_router(ExtractionConfig::default());
229
-
230
- let boundary = "----boundary";
231
- let file1_content = "First file content";
232
- let file2_content = "Second file content";
233
-
234
- let body_content = format!(
235
- "--{}\r\n\
236
- Content-Disposition: form-data; name=\"files\"; filename=\"test1.txt\"\r\n\
237
- Content-Type: text/plain\r\n\
238
- \r\n\
239
- {}\r\n\
240
- --{}\r\n\
241
- Content-Disposition: form-data; name=\"files\"; filename=\"test2.txt\"\r\n\
242
- Content-Type: text/plain\r\n\
243
- \r\n\
244
- {}\r\n\
245
- --{}--\r\n",
246
- boundary, file1_content, boundary, file2_content, boundary
247
- );
248
-
249
- let response = app
250
- .oneshot(
251
- Request::builder()
252
- .method("POST")
253
- .uri("/extract")
254
- .header("content-type", format!("multipart/form-data; boundary={}", boundary))
255
- .body(Body::from(body_content))
256
- .unwrap(),
257
- )
258
- .await
259
- .unwrap();
260
-
261
- assert_eq!(response.status(), StatusCode::OK);
262
-
263
- let body = axum::body::to_bytes(response.into_body(), usize::MAX).await.unwrap();
264
- let results: Vec<serde_json::Value> = serde_json::from_slice(&body).unwrap();
265
-
266
- assert_eq!(results.len(), 2);
267
- assert!(results[0]["content"].as_str().unwrap().contains("First file"));
268
- assert!(results[1]["content"].as_str().unwrap().contains("Second file"));
269
-
270
- for result in &results {
271
- assert!(
272
- result["chunks"].is_null(),
273
- "Chunks should be null without chunking config"
274
- );
275
- assert!(result["detected_languages"].is_null(), "Language detection not enabled");
276
- assert!(result["tables"].is_array(), "Tables field should be present");
277
- assert!(result["metadata"].is_object(), "Metadata field should be present");
278
- assert_eq!(result["mime_type"], "text/plain");
279
- }
280
- }
281
-
282
- /// Test extract endpoint with markdown content.
283
- #[tokio::test]
284
- async fn test_extract_markdown_file() {
285
- let app = create_router(ExtractionConfig::default());
286
-
287
- let boundary = "----boundary";
288
- let file_content = "# Heading\n\nSome **bold** text.";
289
-
290
- let body_content = format!(
291
- "--{}\r\n\
292
- Content-Disposition: form-data; name=\"files\"; filename=\"test.md\"\r\n\
293
- Content-Type: text/markdown\r\n\
294
- \r\n\
295
- {}\r\n\
296
- --{}--\r\n",
297
- boundary, file_content, boundary
298
- );
299
-
300
- let response = app
301
- .oneshot(
302
- Request::builder()
303
- .method("POST")
304
- .uri("/extract")
305
- .header("content-type", format!("multipart/form-data; boundary={}", boundary))
306
- .body(Body::from(body_content))
307
- .unwrap(),
308
- )
309
- .await
310
- .unwrap();
311
-
312
- assert_eq!(response.status(), StatusCode::OK);
313
-
314
- let body = axum::body::to_bytes(response.into_body(), usize::MAX).await.unwrap();
315
- let results: Vec<serde_json::Value> = serde_json::from_slice(&body).unwrap();
316
-
317
- assert_eq!(results.len(), 1);
318
- assert_eq!(results[0]["mime_type"], "text/markdown");
319
- assert!(results[0]["content"].as_str().unwrap().contains("Heading"));
320
- }
321
-
322
- /// Test extract endpoint with JSON content.
323
- #[tokio::test]
324
- async fn test_extract_json_file() {
325
- let app = create_router(ExtractionConfig::default());
326
-
327
- let boundary = "----boundary";
328
- let file_content = r#"{"key": "value", "number": 42}"#;
329
-
330
- let body_content = format!(
331
- "--{}\r\n\
332
- Content-Disposition: form-data; name=\"files\"; filename=\"test.json\"\r\n\
333
- Content-Type: application/json\r\n\
334
- \r\n\
335
- {}\r\n\
336
- --{}--\r\n",
337
- boundary, file_content, boundary
338
- );
339
-
340
- let response = app
341
- .oneshot(
342
- Request::builder()
343
- .method("POST")
344
- .uri("/extract")
345
- .header("content-type", format!("multipart/form-data; boundary={}", boundary))
346
- .body(Body::from(body_content))
347
- .unwrap(),
348
- )
349
- .await
350
- .unwrap();
351
-
352
- assert_eq!(response.status(), StatusCode::OK);
353
-
354
- let body = axum::body::to_bytes(response.into_body(), usize::MAX).await.unwrap();
355
- let results: Vec<serde_json::Value> = serde_json::from_slice(&body).unwrap();
356
-
357
- assert_eq!(results.len(), 1);
358
- assert_eq!(results[0]["mime_type"], "application/json");
359
- }
360
-
361
- /// Test extract endpoint with XML content.
362
- #[tokio::test]
363
- #[cfg(feature = "xml")]
364
- async fn test_extract_xml_file() {
365
- let app = create_router(ExtractionConfig::default());
366
-
367
- let boundary = "----boundary";
368
- let file_content = r#"<?xml version="1.0"?><root><item>test</item></root>"#;
369
-
370
- let body_content = format!(
371
- "--{}\r\n\
372
- Content-Disposition: form-data; name=\"files\"; filename=\"test.xml\"\r\n\
373
- Content-Type: application/xml\r\n\
374
- \r\n\
375
- {}\r\n\
376
- --{}--\r\n",
377
- boundary, file_content, boundary
378
- );
379
-
380
- let response = app
381
- .oneshot(
382
- Request::builder()
383
- .method("POST")
384
- .uri("/extract")
385
- .header("content-type", format!("multipart/form-data; boundary={}", boundary))
386
- .body(Body::from(body_content))
387
- .unwrap(),
388
- )
389
- .await
390
- .unwrap();
391
-
392
- assert_eq!(response.status(), StatusCode::OK);
393
-
394
- let body = axum::body::to_bytes(response.into_body(), usize::MAX).await.unwrap();
395
- let results: Vec<serde_json::Value> = serde_json::from_slice(&body).unwrap();
396
-
397
- assert_eq!(results.len(), 1);
398
- assert_eq!(results[0]["mime_type"], "application/xml");
399
- assert!(results[0]["content"].as_str().unwrap().contains("test"));
400
- }
401
-
402
- /// Test extract endpoint with HTML content.
403
- #[tokio::test]
404
- #[cfg(feature = "html")]
405
- async fn test_extract_html_file() {
406
- let app = create_router(ExtractionConfig::default());
407
-
408
- let boundary = "----boundary";
409
- let file_content = r#"<html><body><h1>Title</h1><p>Content</p></body></html>"#;
410
-
411
- let body_content = format!(
412
- "--{}\r\n\
413
- Content-Disposition: form-data; name=\"files\"; filename=\"test.html\"\r\n\
414
- Content-Type: text/html\r\n\
415
- \r\n\
416
- {}\r\n\
417
- --{}--\r\n",
418
- boundary, file_content, boundary
419
- );
420
-
421
- let response = app
422
- .oneshot(
423
- Request::builder()
424
- .method("POST")
425
- .uri("/extract")
426
- .header("content-type", format!("multipart/form-data; boundary={}", boundary))
427
- .body(Body::from(body_content))
428
- .unwrap(),
429
- )
430
- .await
431
- .unwrap();
432
-
433
- assert_eq!(response.status(), StatusCode::OK);
434
-
435
- let body = axum::body::to_bytes(response.into_body(), usize::MAX).await.unwrap();
436
- let results: Vec<serde_json::Value> = serde_json::from_slice(&body).unwrap();
437
-
438
- assert_eq!(results.len(), 1);
439
- assert_eq!(results[0]["mime_type"], "text/html");
440
- assert!(results[0]["content"].as_str().unwrap().contains("Title"));
441
- }
442
-
443
- /// Test extract endpoint with missing Content-Type header.
444
- #[tokio::test]
445
- async fn test_extract_missing_content_type() {
446
- let app = create_router(ExtractionConfig::default());
447
-
448
- let response = app
449
- .oneshot(
450
- Request::builder()
451
- .method("POST")
452
- .uri("/extract")
453
- .body(Body::from("some data"))
454
- .unwrap(),
455
- )
456
- .await
457
- .unwrap();
458
-
459
- assert!(response.status() == StatusCode::BAD_REQUEST || response.status() == StatusCode::UNSUPPORTED_MEDIA_TYPE);
460
- }
461
-
462
- /// Test extract endpoint with empty file.
463
- #[tokio::test]
464
- async fn test_extract_empty_file() {
465
- let app = create_router(ExtractionConfig::default());
466
-
467
- let boundary = "----boundary";
468
- let file_content = "";
469
-
470
- let body_content = format!(
471
- "--{}\r\n\
472
- Content-Disposition: form-data; name=\"files\"; filename=\"empty.txt\"\r\n\
473
- Content-Type: text/plain\r\n\
474
- \r\n\
475
- {}\r\n\
476
- --{}--\r\n",
477
- boundary, file_content, boundary
478
- );
479
-
480
- let response = app
481
- .oneshot(
482
- Request::builder()
483
- .method("POST")
484
- .uri("/extract")
485
- .header("content-type", format!("multipart/form-data; boundary={}", boundary))
486
- .body(Body::from(body_content))
487
- .unwrap(),
488
- )
489
- .await
490
- .unwrap();
491
-
492
- assert_eq!(response.status(), StatusCode::OK);
493
-
494
- let body = axum::body::to_bytes(response.into_body(), usize::MAX).await.unwrap();
495
- let results: Vec<serde_json::Value> = serde_json::from_slice(&body).unwrap();
496
-
497
- assert_eq!(results.len(), 1);
498
- assert_eq!(results[0]["mime_type"], "text/plain");
499
- }
500
-
501
- /// Test extract endpoint with unsupported MIME type.
502
- #[tokio::test]
503
- async fn test_extract_unsupported_mime_type() {
504
- let app = create_router(ExtractionConfig::default());
505
-
506
- let boundary = "----boundary";
507
- let file_content = "Binary data";
508
-
509
- let body_content = format!(
510
- "--{}\r\n\
511
- Content-Disposition: form-data; name=\"files\"; filename=\"test.bin\"\r\n\
512
- Content-Type: application/x-unknown-binary\r\n\
513
- \r\n\
514
- {}\r\n\
515
- --{}--\r\n",
516
- boundary, file_content, boundary
517
- );
518
-
519
- let response = app
520
- .oneshot(
521
- Request::builder()
522
- .method("POST")
523
- .uri("/extract")
524
- .header("content-type", format!("multipart/form-data; boundary={}", boundary))
525
- .body(Body::from(body_content))
526
- .unwrap(),
527
- )
528
- .await
529
- .unwrap();
530
-
531
- assert!(
532
- response.status() == StatusCode::UNPROCESSABLE_ENTITY || response.status() == StatusCode::INTERNAL_SERVER_ERROR
533
- );
534
- }
535
-
536
- /// Test extract endpoint without filename in multipart field.
537
- #[tokio::test]
538
- async fn test_extract_without_filename() {
539
- let app = create_router(ExtractionConfig::default());
540
-
541
- let boundary = "----boundary";
542
- let file_content = "Test content";
543
-
544
- let body_content = format!(
545
- "--{}\r\n\
546
- Content-Disposition: form-data; name=\"files\"\r\n\
547
- Content-Type: text/plain\r\n\
548
- \r\n\
549
- {}\r\n\
550
- --{}--\r\n",
551
- boundary, file_content, boundary
552
- );
553
-
554
- let response = app
555
- .oneshot(
556
- Request::builder()
557
- .method("POST")
558
- .uri("/extract")
559
- .header("content-type", format!("multipart/form-data; boundary={}", boundary))
560
- .body(Body::from(body_content))
561
- .unwrap(),
562
- )
563
- .await
564
- .unwrap();
565
-
566
- assert_eq!(response.status(), StatusCode::OK);
567
- }
568
-
569
- /// Test extract endpoint with malformed multipart data.
570
- #[tokio::test]
571
- async fn test_extract_malformed_multipart() {
572
- let app = create_router(ExtractionConfig::default());
573
-
574
- let boundary = "----boundary";
575
- let body_content = format!("--{}\r\nmalformed data\r\n", boundary);
576
-
577
- let response = app
578
- .oneshot(
579
- Request::builder()
580
- .method("POST")
581
- .uri("/extract")
582
- .header("content-type", format!("multipart/form-data; boundary={}", boundary))
583
- .body(Body::from(body_content))
584
- .unwrap(),
585
- )
586
- .await
587
- .unwrap();
588
-
589
- assert!(response.status().is_client_error() || response.status().is_server_error());
590
- }
591
-
592
- /// Test CORS headers are present.
593
- #[tokio::test]
594
- async fn test_cors_headers() {
595
- let app = create_router(ExtractionConfig::default());
596
-
597
- let response = app
598
- .oneshot(Request::builder().uri("/health").body(Body::empty()).unwrap())
599
- .await
600
- .unwrap();
601
-
602
- assert_eq!(response.status(), StatusCode::OK);
603
-
604
- let headers = response.headers();
605
- assert!(headers.contains_key("access-control-allow-origin") || headers.contains_key("Access-Control-Allow-Origin"));
606
- }
607
-
608
- /// Test OPTIONS preflight request for CORS.
609
- #[tokio::test]
610
- async fn test_cors_preflight() {
611
- let app = create_router(ExtractionConfig::default());
612
-
613
- let response = app
614
- .oneshot(
615
- Request::builder()
616
- .method("OPTIONS")
617
- .uri("/extract")
618
- .header("origin", "http://example.com")
619
- .header("access-control-request-method", "POST")
620
- .body(Body::empty())
621
- .unwrap(),
622
- )
623
- .await
624
- .unwrap();
625
-
626
- assert!(response.status().is_success() || response.status() == StatusCode::NO_CONTENT);
627
- }
628
-
629
- /// Test error response format for validation errors.
630
- #[tokio::test]
631
- async fn test_error_response_format_validation() {
632
- let app = create_router(ExtractionConfig::default());
633
-
634
- let boundary = "----boundary";
635
- let body_content = format!("--{}--\r\n", boundary);
636
-
637
- let response = app
638
- .oneshot(
639
- Request::builder()
640
- .method("POST")
641
- .uri("/extract")
642
- .header("content-type", format!("multipart/form-data; boundary={}", boundary))
643
- .body(Body::from(body_content))
644
- .unwrap(),
645
- )
646
- .await
647
- .unwrap();
648
-
649
- assert_eq!(response.status(), StatusCode::BAD_REQUEST);
650
-
651
- let body = axum::body::to_bytes(response.into_body(), usize::MAX).await.unwrap();
652
- let error: serde_json::Value = serde_json::from_slice(&body).unwrap();
653
-
654
- assert!(error["error_type"].is_string());
655
- assert!(error["message"].is_string());
656
- assert_eq!(error["status_code"], 400);
657
- assert_eq!(error["error_type"], "ValidationError");
658
- }
659
-
660
- /// Test error response format for parsing errors.
661
- #[tokio::test]
662
- async fn test_error_response_format_parsing() {
663
- let app = create_router(ExtractionConfig::default());
664
-
665
- let boundary = "----boundary";
666
- let invalid_config = "not valid json";
667
-
668
- let body_content = format!(
669
- "--{}\r\n\
670
- Content-Disposition: form-data; name=\"files\"; filename=\"test.txt\"\r\n\
671
- Content-Type: text/plain\r\n\
672
- \r\n\
673
- content\r\n\
674
- --{}\r\n\
675
- Content-Disposition: form-data; name=\"config\"\r\n\
676
- \r\n\
677
- {}\r\n\
678
- --{}--\r\n",
679
- boundary, boundary, invalid_config, boundary
680
- );
681
-
682
- let response = app
683
- .oneshot(
684
- Request::builder()
685
- .method("POST")
686
- .uri("/extract")
687
- .header("content-type", format!("multipart/form-data; boundary={}", boundary))
688
- .body(Body::from(body_content))
689
- .unwrap(),
690
- )
691
- .await
692
- .unwrap();
693
-
694
- assert_eq!(response.status(), StatusCode::BAD_REQUEST);
695
-
696
- let body = axum::body::to_bytes(response.into_body(), usize::MAX).await.unwrap();
697
- let error: serde_json::Value = serde_json::from_slice(&body).unwrap();
698
-
699
- assert_eq!(error["error_type"], "ValidationError");
700
- assert!(error["message"].as_str().unwrap().contains("configuration"));
701
- }
702
-
703
- /// Test 404 error for non-existent endpoint.
704
- #[tokio::test]
705
- async fn test_not_found_endpoint() {
706
- let app = create_router(ExtractionConfig::default());
707
-
708
- let response = app
709
- .oneshot(Request::builder().uri("/nonexistent").body(Body::empty()).unwrap())
710
- .await
711
- .unwrap();
712
-
713
- assert_eq!(response.status(), StatusCode::NOT_FOUND);
714
- }
715
-
716
- /// Test extract endpoint with very large text content.
717
- #[tokio::test]
718
- async fn test_extract_large_file() {
719
- let app = create_router(ExtractionConfig::default());
720
-
721
- let boundary = "----boundary";
722
- let large_content = "A".repeat(1024 * 1024);
723
-
724
- let body_content = format!(
725
- "--{}\r\n\
726
- Content-Disposition: form-data; name=\"files\"; filename=\"large.txt\"\r\n\
727
- Content-Type: text/plain\r\n\
728
- \r\n\
729
- {}\r\n\
730
- --{}--\r\n",
731
- boundary, large_content, boundary
732
- );
733
-
734
- let response = app
735
- .oneshot(
736
- Request::builder()
737
- .method("POST")
738
- .uri("/extract")
739
- .header("content-type", format!("multipart/form-data; boundary={}", boundary))
740
- .body(Body::from(body_content))
741
- .unwrap(),
742
- )
743
- .await
744
- .unwrap();
745
-
746
- assert_eq!(response.status(), StatusCode::OK);
747
-
748
- let body = axum::body::to_bytes(response.into_body(), usize::MAX).await.unwrap();
749
- let results: Vec<serde_json::Value> = serde_json::from_slice(&body).unwrap();
750
-
751
- assert_eq!(results.len(), 1);
752
- assert_eq!(results[0]["mime_type"], "text/plain");
753
- }
754
-
755
- /// Test concurrent requests to extract endpoint.
756
- #[tokio::test]
757
- async fn test_concurrent_requests() {
758
- use tower::ServiceExt;
759
-
760
- let app = create_router(ExtractionConfig::default());
761
-
762
- let boundary = "----boundary";
763
- let file_content = "Concurrent test";
764
-
765
- let body_content = format!(
766
- "--{}\r\n\
767
- Content-Disposition: form-data; name=\"files\"; filename=\"test.txt\"\r\n\
768
- Content-Type: text/plain\r\n\
769
- \r\n\
770
- {}\r\n\
771
- --{}--\r\n",
772
- boundary, file_content, boundary
773
- );
774
-
775
- let mut handles = vec![];
776
-
777
- for _ in 0..5 {
778
- let app_clone = app.clone();
779
- let body_clone = body_content.clone();
780
-
781
- let handle = tokio::spawn(async move {
782
- app_clone
783
- .oneshot(
784
- Request::builder()
785
- .method("POST")
786
- .uri("/extract")
787
- .header("content-type", format!("multipart/form-data; boundary={}", boundary))
788
- .body(Body::from(body_clone))
789
- .unwrap(),
790
- )
791
- .await
792
- });
793
-
794
- handles.push(handle);
795
- }
796
-
797
- for handle in handles {
798
- let response = handle.await.unwrap().unwrap();
799
- assert_eq!(response.status(), StatusCode::OK);
800
- }
801
- }
802
-
803
- /// Test cache stats endpoint.
804
- #[tokio::test]
805
- async fn test_cache_stats_endpoint() {
806
- let app = create_router(ExtractionConfig::default());
807
-
808
- let response = app
809
- .oneshot(Request::builder().uri("/cache/stats").body(Body::empty()).unwrap())
810
- .await
811
- .unwrap();
812
-
813
- assert_eq!(response.status(), StatusCode::OK);
814
-
815
- let body = axum::body::to_bytes(response.into_body(), usize::MAX).await.unwrap();
816
- let stats: serde_json::Value = serde_json::from_slice(&body).unwrap();
817
-
818
- assert!(stats["directory"].is_string());
819
- assert!(stats["total_files"].is_number());
820
- assert!(stats["total_size_mb"].is_number());
821
- }
822
-
823
- /// Test cache clear endpoint.
824
- #[tokio::test]
825
- async fn test_cache_clear_endpoint() {
826
- let app = create_router(ExtractionConfig::default());
827
-
828
- let response = app
829
- .oneshot(
830
- Request::builder()
831
- .method("DELETE")
832
- .uri("/cache/clear")
833
- .body(Body::empty())
834
- .unwrap(),
835
- )
836
- .await
837
- .unwrap();
838
-
839
- assert_eq!(response.status(), StatusCode::OK);
840
-
841
- let body = axum::body::to_bytes(response.into_body(), usize::MAX).await.unwrap();
842
- let clear_result: serde_json::Value = serde_json::from_slice(&body).unwrap();
843
-
844
- assert!(clear_result["directory"].is_string());
845
- assert!(clear_result["removed_files"].is_number());
846
- assert!(clear_result["freed_mb"].is_number());
847
- }
848
-
849
- /// Test extract endpoint with mixed content types.
850
- #[tokio::test]
851
- async fn test_extract_mixed_content_types() {
852
- let app = create_router(ExtractionConfig::default());
853
-
854
- let boundary = "----boundary";
855
- let text_content = "Text file";
856
- let json_content = r#"{"test": "data"}"#;
857
-
858
- let body_content = format!(
859
- "--{}\r\n\
860
- Content-Disposition: form-data; name=\"files\"; filename=\"test.txt\"\r\n\
861
- Content-Type: text/plain\r\n\
862
- \r\n\
863
- {}\r\n\
864
- --{}\r\n\
865
- Content-Disposition: form-data; name=\"files\"; filename=\"test.json\"\r\n\
866
- Content-Type: application/json\r\n\
867
- \r\n\
868
- {}\r\n\
869
- --{}--\r\n",
870
- boundary, text_content, boundary, json_content, boundary
871
- );
872
-
873
- let response = app
874
- .oneshot(
875
- Request::builder()
876
- .method("POST")
877
- .uri("/extract")
878
- .header("content-type", format!("multipart/form-data; boundary={}", boundary))
879
- .body(Body::from(body_content))
880
- .unwrap(),
881
- )
882
- .await
883
- .unwrap();
884
-
885
- assert_eq!(response.status(), StatusCode::OK);
886
-
887
- let body = axum::body::to_bytes(response.into_body(), usize::MAX).await.unwrap();
888
- let results: Vec<serde_json::Value> = serde_json::from_slice(&body).unwrap();
889
-
890
- assert_eq!(results.len(), 2);
891
- assert_eq!(results[0]["mime_type"], "text/plain");
892
- assert_eq!(results[1]["mime_type"], "application/json");
893
- }
894
-
895
- /// Test extract endpoint with unknown field in multipart.
896
- #[tokio::test]
897
- async fn test_extract_unknown_multipart_field() {
898
- let app = create_router(ExtractionConfig::default());
899
-
900
- let boundary = "----boundary";
901
- let file_content = "Test content";
902
-
903
- let body_content = format!(
904
- "--{}\r\n\
905
- Content-Disposition: form-data; name=\"files\"; filename=\"test.txt\"\r\n\
906
- Content-Type: text/plain\r\n\
907
- \r\n\
908
- {}\r\n\
909
- --{}\r\n\
910
- Content-Disposition: form-data; name=\"unknown_field\"\r\n\
911
- \r\n\
912
- some value\r\n\
913
- --{}--\r\n",
914
- boundary, file_content, boundary, boundary
915
- );
916
-
917
- let response = app
918
- .oneshot(
919
- Request::builder()
920
- .method("POST")
921
- .uri("/extract")
922
- .header("content-type", format!("multipart/form-data; boundary={}", boundary))
923
- .body(Body::from(body_content))
924
- .unwrap(),
925
- )
926
- .await
927
- .unwrap();
928
-
929
- assert_eq!(response.status(), StatusCode::OK);
930
- }
931
-
932
- /// Test extract endpoint with default MIME type (application/octet-stream).
933
- #[tokio::test]
934
- async fn test_extract_default_mime_type() {
935
- let app = create_router(ExtractionConfig::default());
936
-
937
- let boundary = "----boundary";
938
- let file_content = "Test content";
939
-
940
- let body_content = format!(
941
- "--{}\r\n\
942
- Content-Disposition: form-data; name=\"files\"; filename=\"test.txt\"\r\n\
943
- \r\n\
944
- {}\r\n\
945
- --{}--\r\n",
946
- boundary, file_content, boundary
947
- );
948
-
949
- let response = app
950
- .oneshot(
951
- Request::builder()
952
- .method("POST")
953
- .uri("/extract")
954
- .header("content-type", format!("multipart/form-data; boundary={}", boundary))
955
- .body(Body::from(body_content))
956
- .unwrap(),
957
- )
958
- .await
959
- .unwrap();
960
-
961
- assert!(
962
- response.status() == StatusCode::OK
963
- || response.status() == StatusCode::UNPROCESSABLE_ENTITY
964
- || response.status() == StatusCode::INTERNAL_SERVER_ERROR
965
- );
966
- }
1
+ //! Integration tests for the API module.
2
+
3
+ #![cfg(feature = "api")]
4
+
5
+ use axum::{
6
+ body::Body,
7
+ http::{Request, StatusCode},
8
+ };
9
+ use serde_json::json;
10
+ use tower::ServiceExt;
11
+
12
+ use kreuzberg::{
13
+ ExtractionConfig,
14
+ api::{HealthResponse, InfoResponse, create_router},
15
+ };
16
+
17
+ /// Test the health check endpoint.
18
+ #[tokio::test]
19
+ async fn test_health_endpoint() {
20
+ let app = create_router(ExtractionConfig::default());
21
+
22
+ let response = app
23
+ .oneshot(Request::builder().uri("/health").body(Body::empty()).unwrap())
24
+ .await
25
+ .unwrap();
26
+
27
+ assert_eq!(response.status(), StatusCode::OK);
28
+
29
+ let body = axum::body::to_bytes(response.into_body(), usize::MAX).await.unwrap();
30
+ let health: HealthResponse = serde_json::from_slice(&body).unwrap();
31
+
32
+ assert_eq!(health.status, "healthy");
33
+ assert!(!health.version.is_empty());
34
+ }
35
+
36
+ /// Test the info endpoint.
37
+ #[tokio::test]
38
+ async fn test_info_endpoint() {
39
+ let app = create_router(ExtractionConfig::default());
40
+
41
+ let response = app
42
+ .oneshot(Request::builder().uri("/info").body(Body::empty()).unwrap())
43
+ .await
44
+ .unwrap();
45
+
46
+ assert_eq!(response.status(), StatusCode::OK);
47
+
48
+ let body = axum::body::to_bytes(response.into_body(), usize::MAX).await.unwrap();
49
+ let info: InfoResponse = serde_json::from_slice(&body).unwrap();
50
+
51
+ assert!(!info.version.is_empty());
52
+ assert!(info.rust_backend);
53
+ }
54
+
55
+ /// Test extract endpoint with no files returns 400.
56
+ #[tokio::test]
57
+ async fn test_extract_no_files() {
58
+ let app = create_router(ExtractionConfig::default());
59
+
60
+ let boundary = "----boundary";
61
+ let body_content = format!("--{}--\r\n", boundary);
62
+
63
+ let response = app
64
+ .oneshot(
65
+ Request::builder()
66
+ .method("POST")
67
+ .uri("/extract")
68
+ .header("content-type", format!("multipart/form-data; boundary={}", boundary))
69
+ .body(Body::from(body_content))
70
+ .unwrap(),
71
+ )
72
+ .await
73
+ .unwrap();
74
+
75
+ assert_eq!(response.status(), StatusCode::BAD_REQUEST);
76
+ }
77
+
78
+ /// Test extract endpoint with a simple text file.
79
+ #[tokio::test]
80
+ async fn test_extract_text_file() {
81
+ let app = create_router(ExtractionConfig::default());
82
+
83
+ let boundary = "----boundary";
84
+ let file_content = "Hello, world!";
85
+
86
+ let body_content = format!(
87
+ "--{}\r\n\
88
+ Content-Disposition: form-data; name=\"files\"; filename=\"test.txt\"\r\n\
89
+ Content-Type: text/plain\r\n\
90
+ \r\n\
91
+ {}\r\n\
92
+ --{}--\r\n",
93
+ boundary, file_content, boundary
94
+ );
95
+
96
+ let response = app
97
+ .oneshot(
98
+ Request::builder()
99
+ .method("POST")
100
+ .uri("/extract")
101
+ .header("content-type", format!("multipart/form-data; boundary={}", boundary))
102
+ .body(Body::from(body_content))
103
+ .unwrap(),
104
+ )
105
+ .await
106
+ .unwrap();
107
+
108
+ assert_eq!(response.status(), StatusCode::OK);
109
+
110
+ let body = axum::body::to_bytes(response.into_body(), usize::MAX).await.unwrap();
111
+ let results: Vec<serde_json::Value> = serde_json::from_slice(&body).unwrap();
112
+
113
+ assert_eq!(results.len(), 1);
114
+ assert_eq!(results[0]["mime_type"], "text/plain");
115
+ assert!(results[0]["content"].as_str().unwrap().contains("Hello, world!"));
116
+
117
+ assert!(
118
+ results[0]["chunks"].is_null(),
119
+ "Chunks should be null without chunking config"
120
+ );
121
+ assert!(
122
+ results[0]["detected_languages"].is_null(),
123
+ "Language detection not enabled"
124
+ );
125
+ assert!(results[0]["tables"].is_array(), "Tables field should be present");
126
+ assert!(results[0]["metadata"].is_object(), "Metadata field should be present");
127
+ }
128
+
129
+ /// Test extract endpoint with JSON config.
130
+ #[tokio::test]
131
+ async fn test_extract_with_config() {
132
+ let app = create_router(ExtractionConfig::default());
133
+
134
+ let boundary = "----boundary";
135
+ let file_content = "Hello, world!";
136
+ let config = json!({
137
+ "force_ocr": false
138
+ });
139
+
140
+ let body_content = format!(
141
+ "--{}\r\n\
142
+ Content-Disposition: form-data; name=\"files\"; filename=\"test.txt\"\r\n\
143
+ Content-Type: text/plain\r\n\
144
+ \r\n\
145
+ {}\r\n\
146
+ --{}\r\n\
147
+ Content-Disposition: form-data; name=\"config\"\r\n\
148
+ \r\n\
149
+ {}\r\n\
150
+ --{}--\r\n",
151
+ boundary, file_content, boundary, config, boundary
152
+ );
153
+
154
+ let response = app
155
+ .oneshot(
156
+ Request::builder()
157
+ .method("POST")
158
+ .uri("/extract")
159
+ .header("content-type", format!("multipart/form-data; boundary={}", boundary))
160
+ .body(Body::from(body_content))
161
+ .unwrap(),
162
+ )
163
+ .await
164
+ .unwrap();
165
+
166
+ assert_eq!(response.status(), StatusCode::OK);
167
+
168
+ let body = axum::body::to_bytes(response.into_body(), usize::MAX).await.unwrap();
169
+ let results: Vec<serde_json::Value> = serde_json::from_slice(&body).unwrap();
170
+
171
+ assert_eq!(results.len(), 1);
172
+ assert_eq!(results[0]["mime_type"], "text/plain");
173
+ assert!(results[0]["content"].as_str().unwrap().contains("Hello, world!"));
174
+
175
+ assert!(
176
+ results[0]["chunks"].is_null(),
177
+ "Chunks should be null without chunking config"
178
+ );
179
+ assert!(
180
+ results[0]["detected_languages"].is_null(),
181
+ "Language detection not enabled"
182
+ );
183
+ assert!(results[0]["tables"].is_array(), "Tables field should be present");
184
+ assert!(results[0]["metadata"].is_object(), "Metadata field should be present");
185
+ }
186
+
187
+ /// Test extract endpoint with invalid config returns 400.
188
+ #[tokio::test]
189
+ async fn test_extract_invalid_config() {
190
+ let app = create_router(ExtractionConfig::default());
191
+
192
+ let boundary = "----boundary";
193
+ let file_content = "Hello, world!";
194
+ let invalid_config = "not valid json";
195
+
196
+ let body_content = format!(
197
+ "--{}\r\n\
198
+ Content-Disposition: form-data; name=\"files\"; filename=\"test.txt\"\r\n\
199
+ Content-Type: text/plain\r\n\
200
+ \r\n\
201
+ {}\r\n\
202
+ --{}\r\n\
203
+ Content-Disposition: form-data; name=\"config\"\r\n\
204
+ \r\n\
205
+ {}\r\n\
206
+ --{}--\r\n",
207
+ boundary, file_content, boundary, invalid_config, boundary
208
+ );
209
+
210
+ let response = app
211
+ .oneshot(
212
+ Request::builder()
213
+ .method("POST")
214
+ .uri("/extract")
215
+ .header("content-type", format!("multipart/form-data; boundary={}", boundary))
216
+ .body(Body::from(body_content))
217
+ .unwrap(),
218
+ )
219
+ .await
220
+ .unwrap();
221
+
222
+ assert_eq!(response.status(), StatusCode::BAD_REQUEST);
223
+ }
224
+
225
+ /// Test extract endpoint with multiple files.
226
+ #[tokio::test]
227
+ async fn test_extract_multiple_files() {
228
+ let app = create_router(ExtractionConfig::default());
229
+
230
+ let boundary = "----boundary";
231
+ let file1_content = "First file content";
232
+ let file2_content = "Second file content";
233
+
234
+ let body_content = format!(
235
+ "--{}\r\n\
236
+ Content-Disposition: form-data; name=\"files\"; filename=\"test1.txt\"\r\n\
237
+ Content-Type: text/plain\r\n\
238
+ \r\n\
239
+ {}\r\n\
240
+ --{}\r\n\
241
+ Content-Disposition: form-data; name=\"files\"; filename=\"test2.txt\"\r\n\
242
+ Content-Type: text/plain\r\n\
243
+ \r\n\
244
+ {}\r\n\
245
+ --{}--\r\n",
246
+ boundary, file1_content, boundary, file2_content, boundary
247
+ );
248
+
249
+ let response = app
250
+ .oneshot(
251
+ Request::builder()
252
+ .method("POST")
253
+ .uri("/extract")
254
+ .header("content-type", format!("multipart/form-data; boundary={}", boundary))
255
+ .body(Body::from(body_content))
256
+ .unwrap(),
257
+ )
258
+ .await
259
+ .unwrap();
260
+
261
+ assert_eq!(response.status(), StatusCode::OK);
262
+
263
+ let body = axum::body::to_bytes(response.into_body(), usize::MAX).await.unwrap();
264
+ let results: Vec<serde_json::Value> = serde_json::from_slice(&body).unwrap();
265
+
266
+ assert_eq!(results.len(), 2);
267
+ assert!(results[0]["content"].as_str().unwrap().contains("First file"));
268
+ assert!(results[1]["content"].as_str().unwrap().contains("Second file"));
269
+
270
+ for result in &results {
271
+ assert!(
272
+ result["chunks"].is_null(),
273
+ "Chunks should be null without chunking config"
274
+ );
275
+ assert!(result["detected_languages"].is_null(), "Language detection not enabled");
276
+ assert!(result["tables"].is_array(), "Tables field should be present");
277
+ assert!(result["metadata"].is_object(), "Metadata field should be present");
278
+ assert_eq!(result["mime_type"], "text/plain");
279
+ }
280
+ }
281
+
282
+ /// Test extract endpoint with markdown content.
283
+ #[tokio::test]
284
+ async fn test_extract_markdown_file() {
285
+ let app = create_router(ExtractionConfig::default());
286
+
287
+ let boundary = "----boundary";
288
+ let file_content = "# Heading\n\nSome **bold** text.";
289
+
290
+ let body_content = format!(
291
+ "--{}\r\n\
292
+ Content-Disposition: form-data; name=\"files\"; filename=\"test.md\"\r\n\
293
+ Content-Type: text/markdown\r\n\
294
+ \r\n\
295
+ {}\r\n\
296
+ --{}--\r\n",
297
+ boundary, file_content, boundary
298
+ );
299
+
300
+ let response = app
301
+ .oneshot(
302
+ Request::builder()
303
+ .method("POST")
304
+ .uri("/extract")
305
+ .header("content-type", format!("multipart/form-data; boundary={}", boundary))
306
+ .body(Body::from(body_content))
307
+ .unwrap(),
308
+ )
309
+ .await
310
+ .unwrap();
311
+
312
+ assert_eq!(response.status(), StatusCode::OK);
313
+
314
+ let body = axum::body::to_bytes(response.into_body(), usize::MAX).await.unwrap();
315
+ let results: Vec<serde_json::Value> = serde_json::from_slice(&body).unwrap();
316
+
317
+ assert_eq!(results.len(), 1);
318
+ assert_eq!(results[0]["mime_type"], "text/markdown");
319
+ assert!(results[0]["content"].as_str().unwrap().contains("Heading"));
320
+ }
321
+
322
+ /// Test extract endpoint with JSON content.
323
+ #[tokio::test]
324
+ async fn test_extract_json_file() {
325
+ let app = create_router(ExtractionConfig::default());
326
+
327
+ let boundary = "----boundary";
328
+ let file_content = r#"{"key": "value", "number": 42}"#;
329
+
330
+ let body_content = format!(
331
+ "--{}\r\n\
332
+ Content-Disposition: form-data; name=\"files\"; filename=\"test.json\"\r\n\
333
+ Content-Type: application/json\r\n\
334
+ \r\n\
335
+ {}\r\n\
336
+ --{}--\r\n",
337
+ boundary, file_content, boundary
338
+ );
339
+
340
+ let response = app
341
+ .oneshot(
342
+ Request::builder()
343
+ .method("POST")
344
+ .uri("/extract")
345
+ .header("content-type", format!("multipart/form-data; boundary={}", boundary))
346
+ .body(Body::from(body_content))
347
+ .unwrap(),
348
+ )
349
+ .await
350
+ .unwrap();
351
+
352
+ assert_eq!(response.status(), StatusCode::OK);
353
+
354
+ let body = axum::body::to_bytes(response.into_body(), usize::MAX).await.unwrap();
355
+ let results: Vec<serde_json::Value> = serde_json::from_slice(&body).unwrap();
356
+
357
+ assert_eq!(results.len(), 1);
358
+ assert_eq!(results[0]["mime_type"], "application/json");
359
+ }
360
+
361
+ /// Test extract endpoint with XML content.
362
+ #[tokio::test]
363
+ #[cfg(feature = "xml")]
364
+ async fn test_extract_xml_file() {
365
+ let app = create_router(ExtractionConfig::default());
366
+
367
+ let boundary = "----boundary";
368
+ let file_content = r#"<?xml version="1.0"?><root><item>test</item></root>"#;
369
+
370
+ let body_content = format!(
371
+ "--{}\r\n\
372
+ Content-Disposition: form-data; name=\"files\"; filename=\"test.xml\"\r\n\
373
+ Content-Type: application/xml\r\n\
374
+ \r\n\
375
+ {}\r\n\
376
+ --{}--\r\n",
377
+ boundary, file_content, boundary
378
+ );
379
+
380
+ let response = app
381
+ .oneshot(
382
+ Request::builder()
383
+ .method("POST")
384
+ .uri("/extract")
385
+ .header("content-type", format!("multipart/form-data; boundary={}", boundary))
386
+ .body(Body::from(body_content))
387
+ .unwrap(),
388
+ )
389
+ .await
390
+ .unwrap();
391
+
392
+ assert_eq!(response.status(), StatusCode::OK);
393
+
394
+ let body = axum::body::to_bytes(response.into_body(), usize::MAX).await.unwrap();
395
+ let results: Vec<serde_json::Value> = serde_json::from_slice(&body).unwrap();
396
+
397
+ assert_eq!(results.len(), 1);
398
+ assert_eq!(results[0]["mime_type"], "application/xml");
399
+ assert!(results[0]["content"].as_str().unwrap().contains("test"));
400
+ }
401
+
402
+ /// Test extract endpoint with HTML content.
403
+ #[tokio::test]
404
+ #[cfg(feature = "html")]
405
+ async fn test_extract_html_file() {
406
+ let app = create_router(ExtractionConfig::default());
407
+
408
+ let boundary = "----boundary";
409
+ let file_content = r#"<html><body><h1>Title</h1><p>Content</p></body></html>"#;
410
+
411
+ let body_content = format!(
412
+ "--{}\r\n\
413
+ Content-Disposition: form-data; name=\"files\"; filename=\"test.html\"\r\n\
414
+ Content-Type: text/html\r\n\
415
+ \r\n\
416
+ {}\r\n\
417
+ --{}--\r\n",
418
+ boundary, file_content, boundary
419
+ );
420
+
421
+ let response = app
422
+ .oneshot(
423
+ Request::builder()
424
+ .method("POST")
425
+ .uri("/extract")
426
+ .header("content-type", format!("multipart/form-data; boundary={}", boundary))
427
+ .body(Body::from(body_content))
428
+ .unwrap(),
429
+ )
430
+ .await
431
+ .unwrap();
432
+
433
+ assert_eq!(response.status(), StatusCode::OK);
434
+
435
+ let body = axum::body::to_bytes(response.into_body(), usize::MAX).await.unwrap();
436
+ let results: Vec<serde_json::Value> = serde_json::from_slice(&body).unwrap();
437
+
438
+ assert_eq!(results.len(), 1);
439
+ assert_eq!(results[0]["mime_type"], "text/html");
440
+ assert!(results[0]["content"].as_str().unwrap().contains("Title"));
441
+ }
442
+
443
+ /// Test extract endpoint with missing Content-Type header.
444
+ #[tokio::test]
445
+ async fn test_extract_missing_content_type() {
446
+ let app = create_router(ExtractionConfig::default());
447
+
448
+ let response = app
449
+ .oneshot(
450
+ Request::builder()
451
+ .method("POST")
452
+ .uri("/extract")
453
+ .body(Body::from("some data"))
454
+ .unwrap(),
455
+ )
456
+ .await
457
+ .unwrap();
458
+
459
+ assert!(response.status() == StatusCode::BAD_REQUEST || response.status() == StatusCode::UNSUPPORTED_MEDIA_TYPE);
460
+ }
461
+
462
+ /// Test extract endpoint with empty file.
463
+ #[tokio::test]
464
+ async fn test_extract_empty_file() {
465
+ let app = create_router(ExtractionConfig::default());
466
+
467
+ let boundary = "----boundary";
468
+ let file_content = "";
469
+
470
+ let body_content = format!(
471
+ "--{}\r\n\
472
+ Content-Disposition: form-data; name=\"files\"; filename=\"empty.txt\"\r\n\
473
+ Content-Type: text/plain\r\n\
474
+ \r\n\
475
+ {}\r\n\
476
+ --{}--\r\n",
477
+ boundary, file_content, boundary
478
+ );
479
+
480
+ let response = app
481
+ .oneshot(
482
+ Request::builder()
483
+ .method("POST")
484
+ .uri("/extract")
485
+ .header("content-type", format!("multipart/form-data; boundary={}", boundary))
486
+ .body(Body::from(body_content))
487
+ .unwrap(),
488
+ )
489
+ .await
490
+ .unwrap();
491
+
492
+ assert_eq!(response.status(), StatusCode::OK);
493
+
494
+ let body = axum::body::to_bytes(response.into_body(), usize::MAX).await.unwrap();
495
+ let results: Vec<serde_json::Value> = serde_json::from_slice(&body).unwrap();
496
+
497
+ assert_eq!(results.len(), 1);
498
+ assert_eq!(results[0]["mime_type"], "text/plain");
499
+ }
500
+
501
+ /// Test extract endpoint with unsupported MIME type.
502
+ #[tokio::test]
503
+ async fn test_extract_unsupported_mime_type() {
504
+ let app = create_router(ExtractionConfig::default());
505
+
506
+ let boundary = "----boundary";
507
+ let file_content = "Binary data";
508
+
509
+ let body_content = format!(
510
+ "--{}\r\n\
511
+ Content-Disposition: form-data; name=\"files\"; filename=\"test.bin\"\r\n\
512
+ Content-Type: application/x-unknown-binary\r\n\
513
+ \r\n\
514
+ {}\r\n\
515
+ --{}--\r\n",
516
+ boundary, file_content, boundary
517
+ );
518
+
519
+ let response = app
520
+ .oneshot(
521
+ Request::builder()
522
+ .method("POST")
523
+ .uri("/extract")
524
+ .header("content-type", format!("multipart/form-data; boundary={}", boundary))
525
+ .body(Body::from(body_content))
526
+ .unwrap(),
527
+ )
528
+ .await
529
+ .unwrap();
530
+
531
+ assert!(
532
+ response.status() == StatusCode::UNPROCESSABLE_ENTITY || response.status() == StatusCode::INTERNAL_SERVER_ERROR
533
+ );
534
+ }
535
+
536
+ /// Test extract endpoint without filename in multipart field.
537
+ #[tokio::test]
538
+ async fn test_extract_without_filename() {
539
+ let app = create_router(ExtractionConfig::default());
540
+
541
+ let boundary = "----boundary";
542
+ let file_content = "Test content";
543
+
544
+ let body_content = format!(
545
+ "--{}\r\n\
546
+ Content-Disposition: form-data; name=\"files\"\r\n\
547
+ Content-Type: text/plain\r\n\
548
+ \r\n\
549
+ {}\r\n\
550
+ --{}--\r\n",
551
+ boundary, file_content, boundary
552
+ );
553
+
554
+ let response = app
555
+ .oneshot(
556
+ Request::builder()
557
+ .method("POST")
558
+ .uri("/extract")
559
+ .header("content-type", format!("multipart/form-data; boundary={}", boundary))
560
+ .body(Body::from(body_content))
561
+ .unwrap(),
562
+ )
563
+ .await
564
+ .unwrap();
565
+
566
+ assert_eq!(response.status(), StatusCode::OK);
567
+ }
568
+
569
+ /// Test extract endpoint with malformed multipart data.
570
+ #[tokio::test]
571
+ async fn test_extract_malformed_multipart() {
572
+ let app = create_router(ExtractionConfig::default());
573
+
574
+ let boundary = "----boundary";
575
+ let body_content = format!("--{}\r\nmalformed data\r\n", boundary);
576
+
577
+ let response = app
578
+ .oneshot(
579
+ Request::builder()
580
+ .method("POST")
581
+ .uri("/extract")
582
+ .header("content-type", format!("multipart/form-data; boundary={}", boundary))
583
+ .body(Body::from(body_content))
584
+ .unwrap(),
585
+ )
586
+ .await
587
+ .unwrap();
588
+
589
+ assert!(response.status().is_client_error() || response.status().is_server_error());
590
+ }
591
+
592
+ /// Test CORS headers are present.
593
+ #[tokio::test]
594
+ async fn test_cors_headers() {
595
+ let app = create_router(ExtractionConfig::default());
596
+
597
+ let response = app
598
+ .oneshot(Request::builder().uri("/health").body(Body::empty()).unwrap())
599
+ .await
600
+ .unwrap();
601
+
602
+ assert_eq!(response.status(), StatusCode::OK);
603
+
604
+ let headers = response.headers();
605
+ assert!(headers.contains_key("access-control-allow-origin") || headers.contains_key("Access-Control-Allow-Origin"));
606
+ }
607
+
608
+ /// Test OPTIONS preflight request for CORS.
609
+ #[tokio::test]
610
+ async fn test_cors_preflight() {
611
+ let app = create_router(ExtractionConfig::default());
612
+
613
+ let response = app
614
+ .oneshot(
615
+ Request::builder()
616
+ .method("OPTIONS")
617
+ .uri("/extract")
618
+ .header("origin", "http://example.com")
619
+ .header("access-control-request-method", "POST")
620
+ .body(Body::empty())
621
+ .unwrap(),
622
+ )
623
+ .await
624
+ .unwrap();
625
+
626
+ assert!(response.status().is_success() || response.status() == StatusCode::NO_CONTENT);
627
+ }
628
+
629
+ /// Test error response format for validation errors.
630
+ #[tokio::test]
631
+ async fn test_error_response_format_validation() {
632
+ let app = create_router(ExtractionConfig::default());
633
+
634
+ let boundary = "----boundary";
635
+ let body_content = format!("--{}--\r\n", boundary);
636
+
637
+ let response = app
638
+ .oneshot(
639
+ Request::builder()
640
+ .method("POST")
641
+ .uri("/extract")
642
+ .header("content-type", format!("multipart/form-data; boundary={}", boundary))
643
+ .body(Body::from(body_content))
644
+ .unwrap(),
645
+ )
646
+ .await
647
+ .unwrap();
648
+
649
+ assert_eq!(response.status(), StatusCode::BAD_REQUEST);
650
+
651
+ let body = axum::body::to_bytes(response.into_body(), usize::MAX).await.unwrap();
652
+ let error: serde_json::Value = serde_json::from_slice(&body).unwrap();
653
+
654
+ assert!(error["error_type"].is_string());
655
+ assert!(error["message"].is_string());
656
+ assert_eq!(error["status_code"], 400);
657
+ assert_eq!(error["error_type"], "ValidationError");
658
+ }
659
+
660
+ /// Test error response format for parsing errors.
661
+ #[tokio::test]
662
+ async fn test_error_response_format_parsing() {
663
+ let app = create_router(ExtractionConfig::default());
664
+
665
+ let boundary = "----boundary";
666
+ let invalid_config = "not valid json";
667
+
668
+ let body_content = format!(
669
+ "--{}\r\n\
670
+ Content-Disposition: form-data; name=\"files\"; filename=\"test.txt\"\r\n\
671
+ Content-Type: text/plain\r\n\
672
+ \r\n\
673
+ content\r\n\
674
+ --{}\r\n\
675
+ Content-Disposition: form-data; name=\"config\"\r\n\
676
+ \r\n\
677
+ {}\r\n\
678
+ --{}--\r\n",
679
+ boundary, boundary, invalid_config, boundary
680
+ );
681
+
682
+ let response = app
683
+ .oneshot(
684
+ Request::builder()
685
+ .method("POST")
686
+ .uri("/extract")
687
+ .header("content-type", format!("multipart/form-data; boundary={}", boundary))
688
+ .body(Body::from(body_content))
689
+ .unwrap(),
690
+ )
691
+ .await
692
+ .unwrap();
693
+
694
+ assert_eq!(response.status(), StatusCode::BAD_REQUEST);
695
+
696
+ let body = axum::body::to_bytes(response.into_body(), usize::MAX).await.unwrap();
697
+ let error: serde_json::Value = serde_json::from_slice(&body).unwrap();
698
+
699
+ assert_eq!(error["error_type"], "ValidationError");
700
+ assert!(error["message"].as_str().unwrap().contains("configuration"));
701
+ }
702
+
703
+ /// Test 404 error for non-existent endpoint.
704
+ #[tokio::test]
705
+ async fn test_not_found_endpoint() {
706
+ let app = create_router(ExtractionConfig::default());
707
+
708
+ let response = app
709
+ .oneshot(Request::builder().uri("/nonexistent").body(Body::empty()).unwrap())
710
+ .await
711
+ .unwrap();
712
+
713
+ assert_eq!(response.status(), StatusCode::NOT_FOUND);
714
+ }
715
+
716
+ /// Test extract endpoint with very large text content.
717
+ #[tokio::test]
718
+ async fn test_extract_large_file() {
719
+ let app = create_router(ExtractionConfig::default());
720
+
721
+ let boundary = "----boundary";
722
+ let large_content = "A".repeat(1024 * 1024);
723
+
724
+ let body_content = format!(
725
+ "--{}\r\n\
726
+ Content-Disposition: form-data; name=\"files\"; filename=\"large.txt\"\r\n\
727
+ Content-Type: text/plain\r\n\
728
+ \r\n\
729
+ {}\r\n\
730
+ --{}--\r\n",
731
+ boundary, large_content, boundary
732
+ );
733
+
734
+ let response = app
735
+ .oneshot(
736
+ Request::builder()
737
+ .method("POST")
738
+ .uri("/extract")
739
+ .header("content-type", format!("multipart/form-data; boundary={}", boundary))
740
+ .body(Body::from(body_content))
741
+ .unwrap(),
742
+ )
743
+ .await
744
+ .unwrap();
745
+
746
+ assert_eq!(response.status(), StatusCode::OK);
747
+
748
+ let body = axum::body::to_bytes(response.into_body(), usize::MAX).await.unwrap();
749
+ let results: Vec<serde_json::Value> = serde_json::from_slice(&body).unwrap();
750
+
751
+ assert_eq!(results.len(), 1);
752
+ assert_eq!(results[0]["mime_type"], "text/plain");
753
+ }
754
+
755
+ /// Test concurrent requests to extract endpoint.
756
+ #[tokio::test]
757
+ async fn test_concurrent_requests() {
758
+ use tower::ServiceExt;
759
+
760
+ let app = create_router(ExtractionConfig::default());
761
+
762
+ let boundary = "----boundary";
763
+ let file_content = "Concurrent test";
764
+
765
+ let body_content = format!(
766
+ "--{}\r\n\
767
+ Content-Disposition: form-data; name=\"files\"; filename=\"test.txt\"\r\n\
768
+ Content-Type: text/plain\r\n\
769
+ \r\n\
770
+ {}\r\n\
771
+ --{}--\r\n",
772
+ boundary, file_content, boundary
773
+ );
774
+
775
+ let mut handles = vec![];
776
+
777
+ for _ in 0..5 {
778
+ let app_clone = app.clone();
779
+ let body_clone = body_content.clone();
780
+
781
+ let handle = tokio::spawn(async move {
782
+ app_clone
783
+ .oneshot(
784
+ Request::builder()
785
+ .method("POST")
786
+ .uri("/extract")
787
+ .header("content-type", format!("multipart/form-data; boundary={}", boundary))
788
+ .body(Body::from(body_clone))
789
+ .unwrap(),
790
+ )
791
+ .await
792
+ });
793
+
794
+ handles.push(handle);
795
+ }
796
+
797
+ for handle in handles {
798
+ let response = handle.await.unwrap().unwrap();
799
+ assert_eq!(response.status(), StatusCode::OK);
800
+ }
801
+ }
802
+
803
+ /// Test cache stats endpoint.
804
+ #[tokio::test]
805
+ async fn test_cache_stats_endpoint() {
806
+ let app = create_router(ExtractionConfig::default());
807
+
808
+ let response = app
809
+ .oneshot(Request::builder().uri("/cache/stats").body(Body::empty()).unwrap())
810
+ .await
811
+ .unwrap();
812
+
813
+ assert_eq!(response.status(), StatusCode::OK);
814
+
815
+ let body = axum::body::to_bytes(response.into_body(), usize::MAX).await.unwrap();
816
+ let stats: serde_json::Value = serde_json::from_slice(&body).unwrap();
817
+
818
+ assert!(stats["directory"].is_string());
819
+ assert!(stats["total_files"].is_number());
820
+ assert!(stats["total_size_mb"].is_number());
821
+ }
822
+
823
+ /// Test cache clear endpoint.
824
+ #[tokio::test]
825
+ async fn test_cache_clear_endpoint() {
826
+ let app = create_router(ExtractionConfig::default());
827
+
828
+ let response = app
829
+ .oneshot(
830
+ Request::builder()
831
+ .method("DELETE")
832
+ .uri("/cache/clear")
833
+ .body(Body::empty())
834
+ .unwrap(),
835
+ )
836
+ .await
837
+ .unwrap();
838
+
839
+ assert_eq!(response.status(), StatusCode::OK);
840
+
841
+ let body = axum::body::to_bytes(response.into_body(), usize::MAX).await.unwrap();
842
+ let clear_result: serde_json::Value = serde_json::from_slice(&body).unwrap();
843
+
844
+ assert!(clear_result["directory"].is_string());
845
+ assert!(clear_result["removed_files"].is_number());
846
+ assert!(clear_result["freed_mb"].is_number());
847
+ }
848
+
849
+ /// Test extract endpoint with mixed content types.
850
+ #[tokio::test]
851
+ async fn test_extract_mixed_content_types() {
852
+ let app = create_router(ExtractionConfig::default());
853
+
854
+ let boundary = "----boundary";
855
+ let text_content = "Text file";
856
+ let json_content = r#"{"test": "data"}"#;
857
+
858
+ let body_content = format!(
859
+ "--{}\r\n\
860
+ Content-Disposition: form-data; name=\"files\"; filename=\"test.txt\"\r\n\
861
+ Content-Type: text/plain\r\n\
862
+ \r\n\
863
+ {}\r\n\
864
+ --{}\r\n\
865
+ Content-Disposition: form-data; name=\"files\"; filename=\"test.json\"\r\n\
866
+ Content-Type: application/json\r\n\
867
+ \r\n\
868
+ {}\r\n\
869
+ --{}--\r\n",
870
+ boundary, text_content, boundary, json_content, boundary
871
+ );
872
+
873
+ let response = app
874
+ .oneshot(
875
+ Request::builder()
876
+ .method("POST")
877
+ .uri("/extract")
878
+ .header("content-type", format!("multipart/form-data; boundary={}", boundary))
879
+ .body(Body::from(body_content))
880
+ .unwrap(),
881
+ )
882
+ .await
883
+ .unwrap();
884
+
885
+ assert_eq!(response.status(), StatusCode::OK);
886
+
887
+ let body = axum::body::to_bytes(response.into_body(), usize::MAX).await.unwrap();
888
+ let results: Vec<serde_json::Value> = serde_json::from_slice(&body).unwrap();
889
+
890
+ assert_eq!(results.len(), 2);
891
+ assert_eq!(results[0]["mime_type"], "text/plain");
892
+ assert_eq!(results[1]["mime_type"], "application/json");
893
+ }
894
+
895
+ /// Test extract endpoint with unknown field in multipart.
896
+ #[tokio::test]
897
+ async fn test_extract_unknown_multipart_field() {
898
+ let app = create_router(ExtractionConfig::default());
899
+
900
+ let boundary = "----boundary";
901
+ let file_content = "Test content";
902
+
903
+ let body_content = format!(
904
+ "--{}\r\n\
905
+ Content-Disposition: form-data; name=\"files\"; filename=\"test.txt\"\r\n\
906
+ Content-Type: text/plain\r\n\
907
+ \r\n\
908
+ {}\r\n\
909
+ --{}\r\n\
910
+ Content-Disposition: form-data; name=\"unknown_field\"\r\n\
911
+ \r\n\
912
+ some value\r\n\
913
+ --{}--\r\n",
914
+ boundary, file_content, boundary, boundary
915
+ );
916
+
917
+ let response = app
918
+ .oneshot(
919
+ Request::builder()
920
+ .method("POST")
921
+ .uri("/extract")
922
+ .header("content-type", format!("multipart/form-data; boundary={}", boundary))
923
+ .body(Body::from(body_content))
924
+ .unwrap(),
925
+ )
926
+ .await
927
+ .unwrap();
928
+
929
+ assert_eq!(response.status(), StatusCode::OK);
930
+ }
931
+
932
+ /// Test extract endpoint with default MIME type (application/octet-stream).
933
+ #[tokio::test]
934
+ async fn test_extract_default_mime_type() {
935
+ let app = create_router(ExtractionConfig::default());
936
+
937
+ let boundary = "----boundary";
938
+ let file_content = "Test content";
939
+
940
+ let body_content = format!(
941
+ "--{}\r\n\
942
+ Content-Disposition: form-data; name=\"files\"; filename=\"test.txt\"\r\n\
943
+ \r\n\
944
+ {}\r\n\
945
+ --{}--\r\n",
946
+ boundary, file_content, boundary
947
+ );
948
+
949
+ let response = app
950
+ .oneshot(
951
+ Request::builder()
952
+ .method("POST")
953
+ .uri("/extract")
954
+ .header("content-type", format!("multipart/form-data; boundary={}", boundary))
955
+ .body(Body::from(body_content))
956
+ .unwrap(),
957
+ )
958
+ .await
959
+ .unwrap();
960
+
961
+ assert!(
962
+ response.status() == StatusCode::OK
963
+ || response.status() == StatusCode::UNPROCESSABLE_ENTITY
964
+ || response.status() == StatusCode::INTERNAL_SERVER_ERROR
965
+ );
966
+ }