vera2-core 0.12.16-aristoddle.1

Core engine for Vera: parsing, indexing, retrieval, storage, and embedding
Documentation
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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
use super::assets::*;
use super::cuda::*;
use super::ort::*;
use super::*;
use crate::config::OnnxExecutionProvider;
use std::io::Write;
use std::net::TcpListener;
use std::sync::{Arc, Mutex};

// Environment-dependent tests run in child processes via
// crate::test_env::run_env_test; no set_var/remove_var in test threads.

#[test]
fn normalize_huggingface_repo_accepts_repo_ids_and_urls() {
    assert_eq!(
        normalize_huggingface_repo("Zenabius/CodeRankEmbed-onnx").unwrap(),
        "Zenabius/CodeRankEmbed-onnx"
    );
    assert_eq!(
        normalize_huggingface_repo("https://huggingface.co/Zenabius/CodeRankEmbed-onnx").unwrap(),
        "Zenabius/CodeRankEmbed-onnx"
    );
}

#[test]
fn legacy_model_config_deserializes_without_revision() {
    let config: LocalEmbeddingModelConfig = serde_json::from_str(
        r#"{
            "source": {"source": "hugging-face", "repo": "org/model"},
            "onnx_file": "model.onnx",
            "tokenizer_file": "tokenizer.json",
            "embedding_dim": 3,
            "pooling": "mean"
        }"#,
    )
    .unwrap();

    assert_eq!(config.revision, None);
    assert!(!serde_json::to_string(&config).unwrap().contains("revision"));
}

#[test]
fn revisions_validate_and_shard_only_non_main_cache_paths() {
    let mut config = LocalEmbeddingModelConfig::from_huggingface_repo("org/model");
    let legacy = config.cached_asset_paths().unwrap();
    config.revision = Some("refs/pr/42".to_string());
    let pinned = config.cached_asset_paths().unwrap();
    assert!(
        legacy
            .onnx_path
            .ends_with("models/org/model/onnx/model_quantized.onnx")
    );
    assert!(
        pinned
            .onnx_path
            .ends_with("models/org/model/revisions/refs/pr/42/onnx/model_quantized.onnx")
    );

    config.revision = Some("main".to_string());
    assert_eq!(
        config.cached_asset_paths().unwrap().onnx_path,
        legacy.onnx_path
    );

    for revision in [
        "",
        "../escape",
        "refs//main",
        "/absolute",
        "trailing/",
        "refs/../main",
        "v1|pooling=mean",
        "has?query",
        "has%percent",
    ] {
        assert!(normalize_model_revision(revision).is_err(), "{revision:?}");
    }
    assert_eq!(
        normalize_model_revision(" refs/pr/42 ").unwrap(),
        "refs/pr/42"
    );
}

#[test]
fn local_reranker_config_defaults_match_jina_assets() {
    crate::test_env::run_env_test(
        "local_models::tests::local_reranker_config_defaults_match_jina_assets_probe",
        &[
            (LOCAL_RERANKER_REPO_ENV, None),
            (LOCAL_RERANKER_REVISION_ENV, None),
            (LOCAL_RERANKER_ONNX_FILE_ENV, None),
            (LOCAL_RERANKER_TOKENIZER_FILE_ENV, None),
        ],
    );
}

#[test]
#[ignore = "driven by local_reranker_config_defaults_match_jina_assets"]
fn local_reranker_config_defaults_match_jina_assets_probe() {
    let config = LocalRerankerConfig::from_env().unwrap();

    assert_eq!(config.repo, RERANKER_REPO);
    assert_eq!(config.revision.as_deref(), Some(RERANKER_REVISION));
    assert_eq!(config.onnx_file, RERANKER_ONNX_FILE);
    assert_eq!(config.tokenizer_file, RERANKER_TOKENIZER_FILE);
}

#[test]
fn local_reranker_config_reads_env_overrides() {
    crate::test_env::run_env_test(
        "local_models::tests::local_reranker_config_reads_env_overrides_probe",
        &[
            (
                LOCAL_RERANKER_REPO_ENV,
                Some("https://huggingface.co/cross-encoder/ettin-reranker-32m-v1"),
            ),
            (LOCAL_RERANKER_REVISION_ENV, Some(" b33e5ceb ")),
            (
                LOCAL_RERANKER_ONNX_FILE_ENV,
                Some("onnx/model_qint8_avx2.onnx"),
            ),
            (LOCAL_RERANKER_TOKENIZER_FILE_ENV, Some("tokenizer.json")),
        ],
    );
}

#[test]
#[ignore = "driven by local_reranker_config_reads_env_overrides"]
fn local_reranker_config_reads_env_overrides_probe() {
    let config = LocalRerankerConfig::from_env().unwrap();

    assert_eq!(config.repo, "cross-encoder/ettin-reranker-32m-v1");
    assert_eq!(config.revision.as_deref(), Some("b33e5ceb"));
    assert_eq!(config.onnx_file, "onnx/model_qint8_avx2.onnx");
    assert_eq!(config.tokenizer_file, "tokenizer.json");
}

#[test]
fn local_reranker_config_revision_shards_cache_paths() {
    crate::test_env::run_env_test(
        "local_models::tests::local_reranker_config_revision_shards_cache_paths_probe",
        &[
            (LOCAL_RERANKER_REPO_ENV, Some("org/model")),
            (LOCAL_RERANKER_REVISION_ENV, Some("abc123")),
            (LOCAL_RERANKER_ONNX_FILE_ENV, Some("onnx/model.onnx")),
            (LOCAL_RERANKER_TOKENIZER_FILE_ENV, Some("tokenizer.json")),
        ],
    );
}

#[test]
#[ignore = "driven by local_reranker_config_revision_shards_cache_paths"]
fn local_reranker_config_revision_shards_cache_paths_probe() {
    let paths = LocalRerankerConfig::from_env()
        .unwrap()
        .cached_asset_paths()
        .unwrap();

    assert!(
        paths
            .onnx_path
            .ends_with("models/org/model/revisions/abc123/onnx/model.onnx")
    );
    assert!(
        paths
            .tokenizer_path
            .ends_with("models/org/model/revisions/abc123/tokenizer.json")
    );
}

#[test]
fn model_identity_differs_for_non_main_revisions_but_not_main() {
    let base = LocalEmbeddingModelConfig::jina();
    let mut pinned = base.clone();
    pinned.revision = Some("abc123".to_string());
    assert_ne!(base.model_identity(), pinned.model_identity());

    let mut explicit_main = base;
    explicit_main.revision = Some("main".to_string());
    assert_eq!(
        explicit_main.model_identity(),
        pinned.model_identity().replace("|revision=abc123", "")
    );
}

#[test]
fn directory_models_reject_revisions() {
    let mut config = LocalEmbeddingModelConfig::from_directory(PathBuf::from("/models/custom"));
    config.revision = Some("abc123".to_string());
    assert!(config.cached_asset_paths().is_err());
}

#[test]
fn potion_code_identity_carries_the_pinned_revision() {
    // The staleness guard in `indexing::update` compares the stored model name
    // against this identity, so the pin has to be part of it: an index frozen
    // on potion v1 or on the jina ONNX preset must trip the re-index error
    // rather than answer queries with incompatible vectors.
    let name = potion_code_model_name();
    assert_eq!(
        name,
        format!("{POTION_CODE_REPO}|revision={POTION_CODE_REVISION}")
    );
    assert!(!crate::config::model_names_match_with_aliases(
        "minishlab/potion-code-16M",
        &name,
        &[]
    ));
    assert!(!crate::config::model_names_match_with_aliases(
        &name,
        "jinaai/jina-embeddings-v5-text-nano-retrieval|pooling=last-token|qp=6:Query:|dp=9:Document:",
        &[]
    ));
}

#[test]
fn potion_code_assets_live_under_the_revision_shard() {
    // Same sharded cache layout the revisioned ONNX lanes use, so a potion
    // re-pin lands beside the old assets instead of overwriting them.
    let home = Path::new("/tmp/vera-test-home");
    let dir = model_cache_dir(home, POTION_CODE_REPO, Some(POTION_CODE_REVISION)).unwrap();
    assert_eq!(
        dir,
        home.join("models")
            .join(POTION_CODE_REPO)
            .join("revisions")
            .join(POTION_CODE_REVISION)
    );
}

#[test]
fn coderankembed_preset_sets_required_query_prefix() {
    let config = LocalEmbeddingModelConfig::coderankembed();
    assert_eq!(
        config.query_text("find router code"),
        "Represent this query for searching relevant code: find router code"
    );
}

#[test]
fn coderankembed_repo_uses_coderank_defaults() {
    let config = LocalEmbeddingModelConfig::from_huggingface_repo("Zenabius/CodeRankEmbed-onnx");
    assert_eq!(config.pooling, LocalEmbeddingPooling::Cls);
    assert!(config.onnx_data_file.is_none());
}

#[test]
fn parse_cuda_major_version_handles_paths_and_versions() {
    assert_eq!(
        parse_cuda_major_version(r#"C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v13.2"#),
        Some(13)
    );
    assert_eq!(parse_cuda_major_version("/opt/cuda-13.2"), Some(13));
    assert_eq!(parse_cuda_major_version("12.9"), Some(12));
    assert_eq!(parse_cuda_major_version("12_6"), Some(12));
    assert_eq!(parse_cuda_major_version("CUDA Version 13.2.1"), Some(13));
}

#[test]
fn parse_cuda_major_from_cuda_version_metadata_supports_json_and_text() {
    assert_eq!(
        parse_cuda_major_from_cuda_version_metadata(
            r#"{"cuda":{"name":"CUDA SDK","version":"13.2.1"}}"#
        ),
        Some(13)
    );
    assert_eq!(
        parse_cuda_major_from_cuda_version_metadata("CUDA Version 12.8.0"),
        Some(12)
    );
}

#[test]
fn detect_cuda_major_from_cuda_path_env_vars_ignores_unrelated_values() {
    let vars = [
        ("SHLVL", "1"),
        ("TERM_PROGRAM_VERSION", "3.5.1"),
        ("PATH", "/usr/bin"),
    ];
    assert_eq!(detect_cuda_major_from_cuda_path_env_vars(vars), None);
}

#[test]
fn detect_cuda_major_from_cuda_path_env_vars_prefers_versioned_cuda_vars() {
    let vars = [
        ("SHLVL", "1"),
        ("CUDA_PATH_V13_2", "/opt/cuda"),
        ("TERM_PROGRAM_VERSION", "3.5.1"),
    ];
    assert_eq!(detect_cuda_major_from_cuda_path_env_vars(vars), Some(13));
}

#[test]
fn detect_cuda_major_from_cuda_path_value_reads_cuda_version_file() {
    let temp_dir = tempfile::tempdir().unwrap();
    std::fs::write(
        temp_dir.path().join("version.json"),
        r#"{"cuda":{"version":"13.2.1"}}"#,
    )
    .unwrap();

    assert_eq!(
        detect_cuda_major_from_cuda_path_value(temp_dir.path().to_string_lossy().as_ref()),
        Some(13)
    );
}

#[test]
fn parse_cuda_major_from_runtime_library_entry_handles_ldconfig_output() {
    assert_eq!(
        parse_cuda_major_from_runtime_library_entry(
            "libcudart.so.13 (libc6,x86-64) => /opt/cuda/lib64/libcudart.so.13"
        ),
        Some(13)
    );
    assert_eq!(
        parse_cuda_major_from_runtime_library_entry("/opt/cuda/lib64/libcublasLt.so.13"),
        Some(13)
    );
    assert_eq!(
        parse_cuda_major_from_runtime_library_entry("libcufft.so.12"),
        None
    );
}

#[test]
fn detect_cuda_major_from_library_entries_prefers_supported_runtime_libs() {
    let entries = [
        "libcufft.so.12 (libc6,x86-64) => /opt/cuda/lib64/libcufft.so.12",
        "libcublas.so.13 (libc6,x86-64) => /opt/cuda/lib64/libcublas.so.13",
        "libcublasLt.so.13 (libc6,x86-64) => /opt/cuda/lib64/libcublasLt.so.13",
        "libcudart.so.13 (libc6,x86-64) => /opt/cuda/lib64/libcudart.so.13",
    ];
    assert_eq!(detect_cuda_major_from_library_entries(entries), Some(13));
}

#[cfg(target_os = "linux")]
#[test]
fn detect_cuda_major_from_library_dir_groups_respects_group_order() {
    let temp_dir = tempfile::tempdir().unwrap();
    let cuda12_dir = temp_dir.path().join("cuda12");
    let cuda13_dir = temp_dir.path().join("cuda13");
    std::fs::create_dir_all(&cuda12_dir).unwrap();
    std::fs::create_dir_all(&cuda13_dir).unwrap();
    std::fs::write(cuda12_dir.join("libcudart.so.12"), b"").unwrap();
    std::fs::write(cuda13_dir.join("libcudart.so.13"), b"").unwrap();

    let groups = vec![vec![cuda12_dir], vec![cuda13_dir]];
    assert_eq!(detect_cuda_major_from_library_dir_groups(&groups), Some(12));
}

#[cfg(all(target_os = "linux", target_arch = "x86_64"))]
#[test]
fn detect_cuda_major_from_ldconfig_entries_filters_non_native_arch_entries() {
    let entries = [
        "libcudart.so.13 (libc6,AArch64) => /usr/lib/aarch64-linux-gnu/libcudart.so.13",
        "libcudart.so.12 (libc6,x86-64) => /usr/lib/x86_64-linux-gnu/libcudart.so.12",
    ];
    assert_eq!(detect_cuda_major_from_ldconfig_entries(entries), Some(12));
}

#[test]
fn cuda_ort_cache_dir_name_separates_cuda13_runtime() {
    assert_eq!(cuda_ort_cache_dir_name(None), "cuda");
    assert_eq!(cuda_ort_cache_dir_name(Some(12)), "cuda");
    assert_eq!(cuda_ort_cache_dir_name(Some(13)), "cuda13");
    assert_eq!(cuda_ort_cache_dir_name(Some(14)), "cuda13");
}

#[test]
fn cached_ort_library_path_reuses_cuda13_cache_when_detection_is_unknown() {
    let temp_dir = tempfile::tempdir().unwrap();
    let expected_path = temp_dir
        .path()
        .join("lib")
        .join("cuda13")
        .join(platform_ort_lib_name());
    std::fs::create_dir_all(expected_path.parent().unwrap()).unwrap();
    std::fs::write(&expected_path, b"").unwrap();

    let resolved =
        cached_ort_library_path_for_ep_in_home(temp_dir.path(), OnnxExecutionProvider::Cuda, None);
    assert_eq!(resolved, expected_path);
}

#[cfg(all(target_os = "linux", target_arch = "x86_64"))]
#[test]
fn ort_platform_info_uses_cuda13_archive_and_plain_internal_gpu_dir() {
    let (_, archive_name, internal_path, _, _) =
        ort_platform_info_with_cuda_major(OnnxExecutionProvider::Cuda, Some(13)).unwrap();
    assert!(archive_name.contains("-gpu_cuda13-"));
    assert!(internal_path.contains("onnxruntime-linux-x64-gpu-"));
    assert!(!internal_path.contains("_cuda13"));
}

#[test]
fn wrap_ort_error_keeps_model_load_failures_specific() {
    let message = wrap_ort_error("failed to load embedding model C:\\Users\\me\\.vera\\model.onnx");
    assert!(message.contains("Failed to initialize ONNX session"));
    assert!(!message.contains("shared library not found"));
}

#[test]
fn wrap_ort_error_still_flags_missing_dlls() {
    let message = wrap_ort_error(
        "LoadLibrary failed for onnxruntime.dll: The specified module could not be found",
    );
    assert!(message.contains("ONNX Runtime shared library not found"));
}

#[test]
fn onnx_integrity_check_rejects_truncated_and_garbage_files() {
    let temp_dir = tempfile::tempdir().unwrap();
    let truncated = temp_dir.path().join("truncated.onnx");
    let garbage = temp_dir.path().join("garbage.onnx");
    std::fs::write(&truncated, [0x08]).unwrap();
    std::fs::write(&garbage, b"not an onnx model").unwrap();

    assert!(validate_file(&truncated, LocalModelAssetKind::Onnx).is_err());
    assert!(validate_file(&garbage, LocalModelAssetKind::Onnx).is_err());
}

#[test]
fn onnx_integrity_check_scans_fields_before_ir_version() {
    let temp_dir = tempfile::tempdir().unwrap();
    let path = temp_dir.path().join("model.onnx");
    // ModelProto.producer (field 2) may precede ir_version (field 1).
    std::fs::write(
        &path,
        [
            0x12, 0x08, b'p', b'r', b'o', b'd', b'u', b'c', b'e', b'r', 0x08, 0x01,
        ],
    )
    .unwrap();

    assert!(validate_file(&path, LocalModelAssetKind::Onnx).is_ok());
}

#[test]
fn onnx_integrity_check_accepts_multibyte_ir_version() {
    let temp_dir = tempfile::tempdir().unwrap();
    let path = temp_dir.path().join("model.onnx");
    std::fs::write(&path, [0x08, 0xac, 0x02]).unwrap();

    assert!(validate_file(&path, LocalModelAssetKind::Onnx).is_ok());
}

#[test]
fn onnx_integrity_check_rejects_zero_and_truncated_fields() {
    let temp_dir = tempfile::tempdir().unwrap();
    let zero = temp_dir.path().join("zero.onnx");
    let truncated = temp_dir.path().join("truncated-field.onnx");
    std::fs::write(&zero, [0x08, 0x00]).unwrap();
    std::fs::write(&truncated, [0x12, 0x03, b'p']).unwrap();

    assert!(validate_file(&zero, LocalModelAssetKind::Onnx).is_err());
    assert!(validate_file(&truncated, LocalModelAssetKind::Onnx).is_err());
}

#[test]
fn explicit_onnx_asset_kind_validates_custom_filename() {
    let temp_dir = tempfile::tempdir().unwrap();
    let path = temp_dir.path().join("model.bin");
    std::fs::write(&path, b"not an onnx model").unwrap();

    assert!(validate_file(&path, LocalModelAssetKind::Onnx).is_err());
}

#[test]
fn onnx_integrity_check_accepts_a_valid_model_proto_header() {
    let temp_dir = tempfile::tempdir().unwrap();
    let path = temp_dir.path().join("model.onnx");
    std::fs::write(&path, [0x08, 0x01]).unwrap();

    let status = inspect_asset("embedding-onnx", path, LocalModelAssetKind::Onnx);
    assert!(status.exists);
    assert_eq!(status.state, LocalModelAssetState::Valid);
    assert!(status.detail.is_none());
}

#[test]
fn inspect_asset_reports_missing_files_distinctly() {
    let temp_dir = tempfile::tempdir().unwrap();
    let status = inspect_asset(
        "embedding-onnx",
        temp_dir.path().join("missing.onnx"),
        LocalModelAssetKind::Onnx,
    );

    assert!(!status.exists);
    assert_eq!(status.state, LocalModelAssetState::Missing);
    assert_eq!(status.detail.as_deref(), Some("file not found"));
}

#[test]
fn required_dependency_status_ignores_optional_cuda_tensorrt_libraries() {
    let status = SharedLibraryDependencyStatus {
        inspected_files: Vec::new(),
        missing_details: vec![
            "libonnxruntime_providers_tensorrt.so: libnvinfer.so.10".to_string(),
            "libonnxruntime_providers_tensorrt.so: libnvonnxparser.so.10".to_string(),
            "libonnxruntime_providers_cuda.so: libcudnn.so.9".to_string(),
        ],
        missing_libraries: vec![
            "libcudnn.so.9".to_string(),
            "libnvinfer.so.10".to_string(),
            "libnvonnxparser.so.10".to_string(),
        ],
    };

    let filtered = required_dependency_status(OnnxExecutionProvider::Cuda, status);

    assert_eq!(
        filtered.missing_details,
        vec!["libonnxruntime_providers_cuda.so: libcudnn.so.9"]
    );
    assert_eq!(filtered.missing_libraries, vec!["libcudnn.so.9"]);
}

#[tokio::test]
async fn test_download_failure_cleanup() {
    let temp_dir = tempfile::tempdir().unwrap();
    let home = temp_dir.path().join(".vera");

    let listener = match TcpListener::bind("127.0.0.1:0") {
        Ok(listener) => listener,
        Err(err) if err.kind() == std::io::ErrorKind::PermissionDenied => return,
        Err(err) => panic!("failed to bind test listener: {err}"),
    };
    let port = listener.local_addr().unwrap().port();

    std::thread::spawn(move || {
        if let Ok((mut stream, _)) = listener.accept() {
            // Return a valid HTTP response header but truncate the body
            let response = "HTTP/1.1 200 OK\r\nContent-Length: 1000\r\n\r\nPartialData";
            let _ = stream.write_all(response.as_bytes());
            // abruptly close the connection
        }
    });

    let base_url = format!("http://127.0.0.1:{}", port);

    let res = ensure_model_file_impl(
        "test-repo",
        "test-file.bin",
        LocalModelAssetKind::Other,
        &base_url,
        Some(&home),
    )
    .await;

    assert!(res.is_err(), "Download should fail due to truncated stream");

    let target_dir = home.join("models").join("test-repo");
    let has_part_file = std::fs::read_dir(&target_dir)
        .unwrap()
        .filter_map(Result::ok)
        .any(|entry| {
            entry
                .file_name()
                .to_string_lossy()
                .starts_with("test-file.part.")
        });
    assert!(
        !has_part_file,
        "Partial file should be cleaned up on failure"
    );
}

#[tokio::test]
async fn failed_onnx_download_preserves_invalid_cached_file() {
    let temp_dir = tempfile::tempdir().unwrap();
    let home = temp_dir.path().join(".vera");
    let target = home.join("models").join("test-repo").join("model.onnx");
    std::fs::create_dir_all(target.parent().unwrap()).unwrap();
    let cached = b"cached-corrupt-model";
    std::fs::write(&target, cached).unwrap();

    let listener = match TcpListener::bind("127.0.0.1:0") {
        Ok(listener) => listener,
        Err(err) if err.kind() == std::io::ErrorKind::PermissionDenied => return,
        Err(err) => panic!("failed to bind test listener: {err}"),
    };
    let port = listener.local_addr().unwrap().port();
    std::thread::spawn(move || {
        if let Ok((mut stream, _)) = listener.accept() {
            let response = b"HTTP/1.1 500 Internal Server Error\r\nContent-Length: 0\r\n\r\n";
            let _ = stream.write_all(response);
        }
    });

    let base_url = format!("http://127.0.0.1:{port}");
    let result = ensure_model_file_impl(
        "test-repo",
        "model.onnx",
        LocalModelAssetKind::Onnx,
        &base_url,
        Some(&home),
    )
    .await;

    assert!(result.is_err());
    assert_eq!(std::fs::read(&target).unwrap(), cached);
}

#[tokio::test]
async fn successful_onnx_download_replaces_invalid_cached_file() {
    let temp_dir = tempfile::tempdir().unwrap();
    let home = temp_dir.path().join(".vera");
    let target = home.join("models").join("test-repo").join("model.bin");
    std::fs::create_dir_all(target.parent().unwrap()).unwrap();
    std::fs::write(&target, b"cached-corrupt-model").unwrap();

    let listener = match TcpListener::bind("127.0.0.1:0") {
        Ok(listener) => listener,
        Err(err) if err.kind() == std::io::ErrorKind::PermissionDenied => return,
        Err(err) => panic!("failed to bind test listener: {err}"),
    };
    let port = listener.local_addr().unwrap().port();
    std::thread::spawn(move || {
        if let Ok((mut stream, _)) = listener.accept() {
            let mut request = [0u8; 4096];
            let _ = std::io::Read::read(&mut stream, &mut request);
            let response =
                b"HTTP/1.1 200 OK\r\nContent-Length: 2\r\nConnection: close\r\n\r\n\x08\x01";
            let _ = stream.write_all(response);
            let _ = stream.flush();
            let _ = stream.shutdown(std::net::Shutdown::Write);
        }
    });

    let base_url = format!("http://127.0.0.1:{port}");
    let result = ensure_model_file_impl(
        "test-repo",
        "model.bin",
        LocalModelAssetKind::Onnx,
        &base_url,
        Some(&home),
    )
    .await
    .unwrap();

    assert_eq!(result, target);
    assert_eq!(std::fs::read(&target).unwrap(), [0x08, 0x01]);
}

#[tokio::test]
async fn download_uses_pinned_revision_url_and_sharded_cache() {
    let temp_dir = tempfile::tempdir().unwrap();
    let home = temp_dir.path().join(".vera");
    let revision = "d59c919d0159aea2c19ed7d04288fcdd048d0f9c";
    let request_line = Arc::new(Mutex::new(String::new()));

    let listener = match TcpListener::bind("127.0.0.1:0") {
        Ok(listener) => listener,
        Err(err) if err.kind() == std::io::ErrorKind::PermissionDenied => return,
        Err(err) => panic!("failed to bind test listener: {err}"),
    };
    let port = listener.local_addr().unwrap().port();
    let request_line_for_server = Arc::clone(&request_line);
    let server = std::thread::spawn(move || {
        if let Ok((mut stream, _)) = listener.accept() {
            let mut request = [0u8; 4096];
            let bytes_read = std::io::Read::read(&mut stream, &mut request).unwrap_or(0);
            let line_end = request[..bytes_read]
                .windows(2)
                .position(|window| window == b"\r\n")
                .unwrap_or(bytes_read);
            *request_line_for_server.lock().unwrap() =
                String::from_utf8_lossy(&request[..line_end]).into_owned();
            let response =
                b"HTTP/1.1 200 OK\r\nContent-Length: 2\r\nConnection: close\r\n\r\n\x08\x01";
            let _ = stream.write_all(response);
            let _ = stream.flush();
            let _ = stream.shutdown(std::net::Shutdown::Write);
        }
    });

    let base_url = format!("http://127.0.0.1:{port}");
    let result = ensure_model_file_impl_with_revision(
        "org/model",
        "onnx/model.onnx",
        LocalModelAssetKind::Onnx,
        &base_url,
        Some(&home),
        Some(revision),
    )
    .await
    .unwrap();
    server.join().unwrap();

    assert!(request_line.lock().unwrap().starts_with(&format!(
        "GET /org/model/resolve/{revision}/onnx/model.onnx "
    )));
    let expected = home
        .join("models")
        .join("org")
        .join("model")
        .join("revisions")
        .join(revision)
        .join("onnx")
        .join("model.onnx");
    assert_eq!(result, expected);
    assert_eq!(std::fs::read(result).unwrap(), [0x08, 0x01]);
}

#[tokio::test]
async fn cached_digest_mismatch_forces_redownload() {
    let temp_dir = tempfile::tempdir().unwrap();
    let home = temp_dir.path().join(".vera");
    let revision = "d59c919d0159aea2c19ed7d04288fcdd048d0f9c";
    let target = home
        .join("models")
        .join("org")
        .join("model")
        .join("revisions")
        .join(revision)
        .join("model.bin");
    std::fs::create_dir_all(target.parent().unwrap()).unwrap();
    std::fs::write(&target, b"cached").unwrap();
    std::fs::write(
        target.with_file_name("model.bin.sha256"),
        "fb8da7eb5b1b399e7321179dac9e9f65773d7331e1e30554e3911e4325e1ef19\n",
    )
    .unwrap();

    let listener = match TcpListener::bind("127.0.0.1:0") {
        Ok(listener) => listener,
        Err(err) if err.kind() == std::io::ErrorKind::PermissionDenied => return,
        Err(err) => panic!("failed to bind test listener: {err}"),
    };
    let port = listener.local_addr().unwrap().port();
    let request_line = Arc::new(Mutex::new(String::new()));
    let request_line_for_server = Arc::clone(&request_line);
    let server = std::thread::spawn(move || {
        if let Ok((mut stream, _)) = listener.accept() {
            let mut request = [0u8; 4096];
            let bytes_read = std::io::Read::read(&mut stream, &mut request).unwrap_or(0);
            let line_end = request[..bytes_read]
                .windows(2)
                .position(|window| window == b"\r\n")
                .unwrap_or(bytes_read);
            *request_line_for_server.lock().unwrap() =
                String::from_utf8_lossy(&request[..line_end]).into_owned();
            let response =
                b"HTTP/1.1 200 OK\r\nContent-Length: 2\r\nConnection: close\r\n\r\n\x08\x01";
            let _ = stream.write_all(response);
            let _ = stream.flush();
            let _ = stream.shutdown(std::net::Shutdown::Write);
        }
    });

    let base_url = format!("http://127.0.0.1:{port}");
    let result = ensure_model_file_impl_with_revision(
        "org/model",
        "model.bin",
        LocalModelAssetKind::Other,
        &base_url,
        Some(&home),
        Some(revision),
    )
    .await
    .unwrap();
    server.join().unwrap();

    assert_eq!(result, target);
    assert_eq!(std::fs::read(&target).unwrap(), [0x08, 0x01]);
    assert!(
        request_line
            .lock()
            .unwrap()
            .starts_with("GET /org/model/resolve/")
    );
}

#[tokio::test]
async fn download_without_revision_uses_main_url_and_legacy_cache() {
    let temp_dir = tempfile::tempdir().unwrap();
    let home = temp_dir.path().join(".vera");
    let request_line = Arc::new(Mutex::new(String::new()));

    let listener = match TcpListener::bind("127.0.0.1:0") {
        Ok(listener) => listener,
        Err(err) if err.kind() == std::io::ErrorKind::PermissionDenied => return,
        Err(err) => panic!("failed to bind test listener: {err}"),
    };
    let port = listener.local_addr().unwrap().port();
    let request_line_for_server = Arc::clone(&request_line);
    let server = std::thread::spawn(move || {
        if let Ok((mut stream, _)) = listener.accept() {
            let mut request = [0u8; 4096];
            let bytes_read = std::io::Read::read(&mut stream, &mut request).unwrap_or(0);
            let line_end = request[..bytes_read]
                .windows(2)
                .position(|window| window == b"\r\n")
                .unwrap_or(bytes_read);
            *request_line_for_server.lock().unwrap() =
                String::from_utf8_lossy(&request[..line_end]).into_owned();
            let response =
                b"HTTP/1.1 200 OK\r\nContent-Length: 2\r\nConnection: close\r\n\r\n\x08\x01";
            let _ = stream.write_all(response);
            let _ = stream.flush();
            let _ = stream.shutdown(std::net::Shutdown::Write);
        }
    });

    let base_url = format!("http://127.0.0.1:{port}");
    let result = ensure_model_file_impl_with_revision(
        "org/model",
        "onnx/model.onnx",
        LocalModelAssetKind::Onnx,
        &base_url,
        Some(&home),
        None,
    )
    .await
    .unwrap();
    server.join().unwrap();

    assert!(
        request_line
            .lock()
            .unwrap()
            .starts_with("GET /org/model/resolve/main/onnx/model.onnx ")
    );
    let expected = home
        .join("models")
        .join("org")
        .join("model")
        .join("onnx")
        .join("model.onnx");
    assert_eq!(result, expected);
    assert_eq!(std::fs::read(result).unwrap(), [0x08, 0x01]);
    assert!(
        !home
            .join("models")
            .join("org")
            .join("model")
            .join("revisions")
            .exists()
    );
}

#[test]
fn local_embedding_config_from_env_trims_revision() {
    crate::test_env::run_env_test(
        "local_models::tests::local_embedding_config_from_env_trims_revision_probe",
        &[(LOCAL_EMBEDDING_REVISION_ENV, Some(" refs/pr/42 "))],
    );
}

#[test]
#[ignore = "driven by local_embedding_config_from_env_trims_revision"]
fn local_embedding_config_from_env_trims_revision_probe() {
    let config = LocalEmbeddingModelConfig::from_env().unwrap();

    assert_eq!(config.revision.as_deref(), Some("refs/pr/42"));
}

#[test]
fn local_embedding_config_from_env_rejects_invalid_revision() {
    crate::test_env::run_env_test(
        "local_models::tests::local_embedding_config_from_env_rejects_invalid_revision_probe",
        &[(LOCAL_EMBEDDING_REVISION_ENV, Some("../escape"))],
    );
}

#[test]
#[ignore = "driven by local_embedding_config_from_env_rejects_invalid_revision"]
fn local_embedding_config_from_env_rejects_invalid_revision_probe() {
    assert!(LocalEmbeddingModelConfig::from_env().is_err());
}

#[cfg(target_os = "macos")]
#[test]
fn resolve_macos_rpath_loader_path_without_slash() {
    let inspected = std::path::Path::new("/tmp/vera/lib/libonnxruntime.dylib");
    assert_eq!(
        resolve_macos_rpath("@loader_path", inspected),
        std::path::Path::new("/tmp/vera/lib")
    );
}

#[cfg(target_os = "macos")]
#[test]
fn resolve_macos_rpath_loader_path_with_slash() {
    let inspected = std::path::Path::new("/tmp/vera/lib/libonnxruntime.dylib");
    assert_eq!(
        resolve_macos_rpath("@loader_path/subdir", inspected),
        std::path::Path::new("/tmp/vera/lib/subdir")
    );
}

#[cfg(target_os = "macos")]
#[test]
fn resolve_macos_rpath_executable_path_without_slash() {
    let inspected = std::path::Path::new("/tmp/vera/lib/libonnxruntime.dylib");
    let expected = std::env::current_exe()
        .ok()
        .and_then(|exe| exe.parent().map(std::path::Path::to_path_buf))
        .unwrap_or_else(|| std::path::PathBuf::from("@executable_path"));
    assert_eq!(resolve_macos_rpath("@executable_path", inspected), expected);
}

#[cfg(target_os = "macos")]
#[test]
fn resolve_macos_rpath_executable_path_with_slash() {
    let inspected = std::path::Path::new("/tmp/vera/lib/libonnxruntime.dylib");
    let expected = std::env::current_exe()
        .ok()
        .and_then(|exe| exe.parent().map(|p| p.join("subdir")))
        .unwrap_or_else(|| std::path::PathBuf::from("@executable_path/subdir"));
    assert_eq!(
        resolve_macos_rpath("@executable_path/subdir", inspected),
        expected
    );
}

#[test]
fn pooling_display_and_from_str_round_trip() {
    // `state.rs` writes pooling to the environment via Display and reads it
    // back through FromStr, so any spelling those two disagree on silently
    // fails to reload a saved config.
    for mode in [
        LocalEmbeddingPooling::Mean,
        LocalEmbeddingPooling::Cls,
        LocalEmbeddingPooling::LastToken,
    ] {
        let rendered = mode.to_string();
        assert_eq!(
            rendered.parse::<LocalEmbeddingPooling>(),
            Ok(mode),
            "Display emitted `{rendered}`, which FromStr does not accept"
        );
    }
}

#[test]
fn pooling_from_str_accepts_last_token_spellings() {
    for spelling in [
        "last-token",
        "lasttoken",
        "last_token",
        "LAST-TOKEN",
        " last-token ",
    ] {
        assert_eq!(
            spelling.parse::<LocalEmbeddingPooling>(),
            Ok(LocalEmbeddingPooling::LastToken),
            "rejected `{spelling}`"
        );
    }
}

#[test]
fn pooling_from_str_error_lists_every_accepted_mode() {
    let err = "bogus".parse::<LocalEmbeddingPooling>().unwrap_err();
    for mode in [
        LocalEmbeddingPooling::Mean,
        LocalEmbeddingPooling::Cls,
        LocalEmbeddingPooling::LastToken,
    ] {
        assert!(
            err.contains(&mode.to_string()),
            "error `{err}` omits `{mode}`, so the CLI cannot advertise it"
        );
    }
}

#[test]
fn jina_preset_pools_on_the_last_token() {
    // jina-embeddings-v5-text-nano-retrieval declares `pooling_mode_lasttoken`
    // in 1_Pooling/config.json. Mean pooling of its last_hidden_state scores
    // cos 0.59-0.66 against the graph's own `sentence_embedding` output;
    // last-token pooling reproduces it exactly.
    assert_eq!(
        LocalEmbeddingModelConfig::jina().pooling,
        LocalEmbeddingPooling::LastToken
    );
    assert_eq!(
        LocalEmbeddingModelConfig::default().pooling,
        LocalEmbeddingPooling::LastToken
    );
    assert_eq!(
        LocalEmbeddingModelConfig::from_huggingface_repo(EMBEDDING_REPO).pooling,
        LocalEmbeddingPooling::LastToken
    );
}

#[test]
fn unknown_repo_still_defaults_to_mean_pooling() {
    // Fixing jina's pooling must not reach through the shared fallback and
    // repool every custom model.
    let config = LocalEmbeddingModelConfig::from_huggingface_repo("some-org/some-encoder");
    assert_eq!(config.pooling, LocalEmbeddingPooling::Mean);
    assert_eq!(
        LocalEmbeddingModelConfig::coderankembed().pooling,
        LocalEmbeddingPooling::Cls
    );
}

#[test]
fn directory_source_still_defaults_to_mean_pooling() {
    // `--embedding-dir` and VERA_LOCAL_EMBEDDING_DIR point at a model Vera has
    // no preset for, so they must inherit the generic fallback. Deriving them
    // from `Default` instead handed every custom directory model jina's
    // last-token pooling the moment jina's own pooling was fixed.
    let path = PathBuf::from("/models/some-encoder");
    let config = LocalEmbeddingModelConfig::from_directory(path.clone());

    assert_eq!(config.pooling, LocalEmbeddingPooling::Mean);
    assert_eq!(config.source, LocalEmbeddingSource::Directory { path });
    assert!(
        config.model_identity().contains("pooling=mean"),
        "identity `{}` should record mean pooling",
        config.model_identity()
    );
}

#[test]
fn preset_identity_changes_with_pooling_so_stale_indexes_are_detected() {
    // Vectors pooled two ways are not comparable. If the preset identity
    // ignored pooling, upgrading would query mean-pooled rows with last-token
    // vectors and silently return worse results instead of asking for a
    // re-index.
    // Assert the exact preset form. Comparing jina's identity against a
    // mean-pooled clone proves nothing on its own: changing `pooling` breaks
    // struct equality with `Self::jina()`, so the clone leaves the preset
    // branch of `model_identity` and renders through the generic one. The two
    // strings then differ because of the branch, not because pooling is in the
    // preset identity, and the assertion still passes with pooling dropped
    // from that branch entirely.
    let jina = LocalEmbeddingModelConfig::jina();
    assert_eq!(
        jina.model_identity(),
        format!(
            "{}|pooling=last-token|qp={}:{JINA_QUERY_PREFIX}|dp={}:{JINA_DOCUMENT_PREFIX}|revision={EMBEDDING_REVISION}",
            jina.display_name(),
            JINA_QUERY_PREFIX.len(),
            JINA_DOCUMENT_PREFIX.len()
        )
    );
    // The repo still has to be recognisable in the stored name.
    assert!(jina.model_identity().starts_with(&jina.display_name()));

    let mut mean_pooled = jina.clone();
    mean_pooled.pooling = LocalEmbeddingPooling::Mean;
    assert_ne!(jina.model_identity(), mean_pooled.model_identity());
    assert!(mean_pooled.model_identity().contains("pooling=mean"));
}

#[test]
fn coderank_preset_identity_records_its_pooling() {
    // The preset branch of `model_identity` covers CodeRankEmbed as well as
    // jina, so it carries the same obligation: pooling stays in the identity,
    // and the repo stays readable in it. Asserting the exact preset form keeps
    // this from passing on a CodeRankEmbed that quietly fell through to the
    // generic branch. CodeRankEmbed prefixes queries only, so it is also the
    // preset that pins how the absent document side is spelled.
    let coderank = LocalEmbeddingModelConfig::coderankembed();
    assert_eq!(
        coderank.model_identity(),
        format!(
            "{}|pooling=cls|qp={}:{CODERANK_QUERY_PREFIX}|dp=none|revision={CODERANK_EMBEDDING_REVISION}",
            coderank.display_name(),
            CODERANK_QUERY_PREFIX.len()
        )
    );
    assert!(
        coderank
            .model_identity()
            .starts_with(&coderank.display_name())
    );

    // CodeRankEmbed is CLS-pooled, so mean is the contrasting choice.
    let mut mean_pooled = coderank.clone();
    mean_pooled.pooling = LocalEmbeddingPooling::Mean;
    assert_ne!(coderank.model_identity(), mean_pooled.model_identity());
    assert!(mean_pooled.model_identity().contains("pooling=mean"));
}

#[test]
fn stored_legacy_jina_config_is_repaired_to_last_token() {
    // What `vera setup` wrote to config.json before the pooling fix.
    let stored: LocalEmbeddingModelConfig = serde_json::from_str(
        r#"{
            "source": {"source": "hugging-face",
                       "repo": "jinaai/jina-embeddings-v5-text-nano-retrieval"},
            "onnx_file": "onnx/model_quantized.onnx",
            "onnx_data_file": "onnx/model_quantized.onnx_data",
            "tokenizer_file": "tokenizer.json",
            "embedding_dim": 768,
            "pooling": "mean",
            "max_length": 512
        }"#,
    )
    .expect("legacy config should still deserialize");
    assert_eq!(stored.pooling, LocalEmbeddingPooling::Mean);

    let repaired = stored.repair_stored_defaults();
    assert_eq!(repaired.pooling, LocalEmbeddingPooling::LastToken);
    assert_eq!(repaired, LocalEmbeddingModelConfig::jina());
}

#[test]
fn legacy_jina_literal_stays_pinned_when_the_constants_move() {
    // Half one: the literal itself. It describes a config.json an older Vera
    // already wrote, so editing it changes which installs the migration
    // reaches.
    let legacy = LocalEmbeddingModelConfig::legacy_jina_before_pooling_fix();
    assert_eq!(
        legacy,
        LocalEmbeddingModelConfig {
            source: LocalEmbeddingSource::HuggingFace {
                repo: "jinaai/jina-embeddings-v5-text-nano-retrieval".to_string(),
            },
            revision: None,
            onnx_file: "onnx/model_quantized.onnx".to_string(),
            onnx_data_file: Some("onnx/model_quantized.onnx_data".to_string()),
            tokenizer_file: "tokenizer.json".to_string(),
            embedding_dim: 768,
            pooling: LocalEmbeddingPooling::Mean,
            max_length: 512,
            query_prefix: None,
            document_prefix: None,
        }
    );

    // Half two: the tripwire. Today's constants still happen to describe that
    // same file. When one of them moves, this fails and the reader decides.
    const GUIDANCE: &str = "A live EMBEDDING_* constant no longer matches the pre-fix \
         config.json that `repair_stored_defaults` migrates. Leave \
         `legacy_jina_before_pooling_fix` exactly as it is — it is a historical value, not a \
         preset — and update this assertion to record the divergence. Only edit the frozen \
         literal if you have confirmed real pre-fix installs carried the new value: making it \
         follow the constant stops it matching any of them, and every pre-fix install then \
         stays mean-pooled with no error.";
    assert_eq!(
        legacy.source,
        LocalEmbeddingSource::HuggingFace {
            repo: EMBEDDING_REPO.to_string(),
        },
        "{GUIDANCE}"
    );
    assert_eq!(legacy.onnx_file, EMBEDDING_ONNX_FILE, "{GUIDANCE}");
    assert_eq!(
        legacy.onnx_data_file.as_deref(),
        Some(EMBEDDING_ONNX_DATA_FILE),
        "{GUIDANCE}"
    );
    assert_eq!(
        legacy.tokenizer_file, EMBEDDING_TOKENIZER_FILE,
        "{GUIDANCE}"
    );
    assert_eq!(legacy.embedding_dim, EMBEDDING_DIM, "{GUIDANCE}");
    assert_eq!(legacy.max_length, EMBEDDING_MAX_LENGTH, "{GUIDANCE}");

    // Half three: the two fields that deliberately do *not* track the preset.
    // A pre-fix `config.json` predates the retrieval prefixes, so it carries
    // neither. Copying `JINA_QUERY_PREFIX`/`JINA_DOCUMENT_PREFIX` in here to
    // "match the preset" would stop the literal matching any file on any real
    // install, and every pre-fix install would stay mean-pooled with no error.
    assert_eq!(legacy.query_prefix, None, "{GUIDANCE}");
    assert_eq!(legacy.document_prefix, None, "{GUIDANCE}");
}

#[test]
fn explicit_mean_on_the_default_repo_is_no_longer_the_legacy_default() {
    // Before the retrieval prefixes, `--embedding-pooling mean` on the default
    // repo wrote byte-for-byte the config.json an older `vera setup` wrote on
    // its own, so the migration could not spare this user's choice without
    // sparing every pre-fix install with it. The prefixes end that collision:
    // a pre-fix file predates them and carries neither, so an explicit mean
    // written today is distinguishable and is left alone.
    let mut explicit_mean = LocalEmbeddingModelConfig::jina();
    explicit_mean.pooling = LocalEmbeddingPooling::Mean;
    assert_ne!(
        explicit_mean,
        LocalEmbeddingModelConfig::legacy_jina_before_pooling_fix()
    );
    assert_eq!(
        explicit_mean.clone().repair_stored_defaults(),
        explicit_mean
    );

    // Pooling aside, that config differs from the historical file in the
    // prefixes and in nothing else — so the prefixes are what spares it, and
    // the historical file itself is still migrated.
    let mut without_prefixes = explicit_mean;
    without_prefixes.revision = None;
    without_prefixes.query_prefix = None;
    without_prefixes.document_prefix = None;
    assert_eq!(
        without_prefixes,
        LocalEmbeddingModelConfig::legacy_jina_before_pooling_fix()
    );
    assert_eq!(
        without_prefixes.repair_stored_defaults(),
        LocalEmbeddingModelConfig::jina()
    );
}

#[test]
fn repair_leaves_customised_and_unrelated_configs_alone() {
    // A deliberate non-default choice on the same repo survives; `max_length`
    // here differs from the historical default on top of the pooling. Pooling
    // alone survives too, now that the retrieval prefixes separate a config
    // written today from the pre-fix file — see
    // `explicit_mean_on_the_default_repo_is_no_longer_the_legacy_default`.
    let mut customised = LocalEmbeddingModelConfig::jina();
    customised.pooling = LocalEmbeddingPooling::Mean;
    customised.max_length = 256;
    assert_eq!(customised.clone().repair_stored_defaults(), customised);

    // A different repo that happens to be mean-pooled must survive.
    let other = LocalEmbeddingModelConfig::from_huggingface_repo("some-org/some-encoder");
    assert_eq!(other.pooling, LocalEmbeddingPooling::Mean);
    assert_eq!(other.clone().repair_stored_defaults(), other);

    // Repair is idempotent and does not touch the other preset.
    let jina = LocalEmbeddingModelConfig::jina();
    assert_eq!(jina.clone().repair_stored_defaults(), jina);
    let coderank = LocalEmbeddingModelConfig::coderankembed();
    assert_eq!(coderank.clone().repair_stored_defaults(), coderank);
}

#[test]
fn jina_preset_carries_both_retrieval_prefixes() {
    // config_sentence_transformers.json declares
    // {"query": "Query: ", "document": "Document: "} and the card requires both
    // sides for the retrieval variant.
    let config = LocalEmbeddingModelConfig::jina();
    assert_eq!(
        config.query_text("find router code"),
        "Query: find router code"
    );
    assert_eq!(
        config.document_text("fn route() {}"),
        "Document: fn route() {}"
    );
}

#[test]
fn document_prefix_is_independent_of_query_prefix() {
    // CodeRankEmbed is query-prefixed only. A document prefix must not appear
    // from nowhere, and the query prefix must not leak onto passages.
    let coderank = LocalEmbeddingModelConfig::coderankembed();
    assert!(coderank.document_prefix.is_none());
    assert_eq!(coderank.document_text("fn route() {}"), "fn route() {}");
    assert_eq!(
        coderank.query_text("find router code"),
        "Represent this query for searching relevant code: find router code"
    );

    // An unprefixed model leaves both sides untouched.
    let plain = LocalEmbeddingModelConfig::from_huggingface_repo("some-org/some-encoder");
    assert_eq!(plain.query_text("q"), "q");
    assert_eq!(plain.document_text("d"), "d");
}

#[test]
fn prefixes_are_part_of_the_model_identity() {
    // Prefixes change the embedded text, so they change the vector space and
    // must invalidate an index the same way pooling does. Mutating a preset
    // would only prove that the identity switches to its custom-config form,
    // so the comparison is between configs that already take that form and
    // differ in nothing but a prefix.
    let base = LocalEmbeddingModelConfig::from_huggingface_repo("some-org/some-encoder");

    let mut query_prefixed = base.clone();
    query_prefixed.query_prefix = Some("Query:".to_string());
    assert_ne!(base.model_identity(), query_prefixed.model_identity());

    let mut document_prefixed = base.clone();
    document_prefixed.document_prefix = Some("Document:".to_string());
    assert_ne!(base.model_identity(), document_prefixed.model_identity());

    // The two sides are recorded separately, so swapping which one is set is
    // not the same model.
    assert_ne!(
        query_prefixed.model_identity(),
        document_prefixed.model_identity()
    );

    // The preset form has to name both too, or jina's own identity would not
    // move when the prefixes were introduced.
    let jina = LocalEmbeddingModelConfig::jina();
    assert!(jina.model_identity().contains("qp=6:Query:"));
    assert!(jina.model_identity().contains("dp=9:Document:"));
}

#[test]
fn an_absent_prefix_is_not_the_same_identity_as_a_literal_dash() {
    // `unwrap_or("-")` spelled both of these `-`, so an index embedded with no
    // prefix at all passed the staleness check against a config whose prefix is
    // the literal `-`, and `vera update` appended incompatible vectors to it.
    let base = LocalEmbeddingModelConfig::from_huggingface_repo("acme/prefix-identity-fixture");
    assert!(base.query_prefix.is_none());
    assert!(base.document_prefix.is_none());

    let mut dash_query = base.clone();
    dash_query.query_prefix = Some("-".to_string());
    // The two configs really do embed different text, so the identity has to
    // move with them.
    assert_ne!(base.query_text("q"), dash_query.query_text("q"));
    assert_ne!(base.model_identity(), dash_query.model_identity());

    let mut dash_document = base.clone();
    dash_document.document_prefix = Some("-".to_string());
    assert_ne!(base.document_text("d"), dash_document.document_text("d"));
    assert_ne!(base.model_identity(), dash_document.model_identity());

    // And a dash on one side is not a dash on the other.
    assert_ne!(dash_query.model_identity(), dash_document.model_identity());
}

#[test]
fn a_prefix_cannot_forge_an_identity_field_boundary() {
    // `|qp=` and `|dp=` were ordinary text inside a prefix, so a value carrying
    // one shifted where the next field started. These two configs prefix
    // different text on both sides and encoded to a single identity string.
    let base = LocalEmbeddingModelConfig::from_huggingface_repo("acme/prefix-identity-fixture");

    let mut carried_by_query = base.clone();
    carried_by_query.query_prefix = Some("alpha|dp=beta|qp=gamma".to_string());
    carried_by_query.document_prefix = Some("delta".to_string());

    let mut carried_by_document = base.clone();
    carried_by_document.query_prefix = Some("alpha".to_string());
    carried_by_document.document_prefix = Some("beta|qp=gamma|dp=delta".to_string());

    assert_ne!(
        carried_by_query.query_text("q"),
        carried_by_document.query_text("q")
    );
    assert_ne!(
        carried_by_query.model_identity(),
        carried_by_document.model_identity()
    );
}

#[test]
fn identity_matches_on_prefixes_that_embed_the_same_text() {
    // The identity is a staleness guard, not a config diff. Trimming and the
    // empty-to-absent fold happen before the text is embedded, so configs that
    // feed the model byte-identical input must agree, or every one of these
    // spellings would demand its own full re-index for nothing.
    let base = LocalEmbeddingModelConfig::from_huggingface_repo("acme/prefix-identity-fixture");

    for equivalent_to_absent in ["", "   ", "\t\n"] {
        let mut blank = base.clone();
        blank.query_prefix = Some(equivalent_to_absent.to_string());
        blank.document_prefix = Some(equivalent_to_absent.to_string());
        assert_eq!(blank.query_text("q"), base.query_text("q"));
        assert_eq!(blank.document_text("d"), base.document_text("d"));
        assert_eq!(
            blank.model_identity(),
            base.model_identity(),
            "prefix {equivalent_to_absent:?} embeds exactly what no prefix embeds"
        );
    }

    let mut tight = base.clone();
    tight.query_prefix = Some("Ask:".to_string());
    let mut padded = base.clone();
    padded.query_prefix = Some("  Ask:  ".to_string());
    assert_eq!(tight.query_text("q"), padded.query_text("q"));
    assert_eq!(tight.model_identity(), padded.model_identity());
}

#[test]
fn prefix_joins_with_exactly_one_space_however_it_is_written() {
    let mut config = LocalEmbeddingModelConfig::jina();
    for spelling in ["Query:", "Query: ", "  Query:  "] {
        config.query_prefix = Some(spelling.to_string());
        assert_eq!(config.query_text("q"), "Query: q", "spelling {spelling:?}");
    }
    config.query_prefix = Some("   ".to_string());
    assert_eq!(
        config.query_text("q"),
        "q",
        "whitespace-only prefix should be ignored"
    );
}