mrrc 0.7.6

A Rust library for reading, writing, and manipulating MARC bibliographic records in ISO 2709 binary format
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
//! BIBFRAME unit tests for individual MARC tag → BIBFRAME property mappings.
//!
//! These tests verify individual conversion mappings and indicator handling
//! without requiring full baseline comparison.

use mrrc::bibframe::{marc_to_bibframe, BibframeConfig, RdfFormat};
use mrrc::leader::Leader;
use mrrc::record::{Field, Record};

// ============================================================================
// Test Utilities
// ============================================================================

fn make_test_leader() -> Leader {
    Leader {
        record_length: 1000,
        record_status: 'n',
        record_type: 'a',
        bibliographic_level: 'm',
        control_record_type: ' ',
        character_coding: 'a',
        indicator_count: 2,
        subfield_code_count: 2,
        data_base_address: 100,
        encoding_level: ' ',
        cataloging_form: 'a',
        multipart_level: ' ',
        reserved: "4500".to_string(),
    }
}

fn make_config() -> BibframeConfig {
    BibframeConfig::new().with_base_uri("http://example.org/")
}

/// Checks if RDF output contains a specific property with expected value.
#[allow(dead_code)]
fn has_property(rdf: &str, property: &str, expected_value: &str) -> bool {
    rdf.contains(&format!("<{property}>")) && rdf.contains(expected_value)
}

/// Checks if RDF output contains a BIBFRAME class.
#[allow(dead_code)]
fn has_class(rdf: &str, class_name: &str) -> bool {
    rdf.contains(&format!("<{class_name}")) || rdf.contains(&format!("bf:{class_name}"))
}

// ============================================================================
// Unit Tests: Title Fields (245, 246)
// ============================================================================

#[test]
fn test_title_245_main_title() {
    let mut record = Record::new(make_test_leader());
    record.add_control_field("001".to_string(), "test-001".to_string());
    record.add_control_field(
        "008".to_string(),
        "040520s2001    xxu           000 0 eng  ".to_string(),
    );

    let mut f245 = Field::new("245".to_string(), '1', '0');
    f245.add_subfield('a', "The test record /".to_string());
    f245.add_subfield('c', "by Test Author.".to_string());
    record.add_field(f245);

    let graph = marc_to_bibframe(&record, &make_config());
    let rdf = graph.serialize(RdfFormat::RdfXml).unwrap();

    // Verify main title is present
    assert!(
        rdf.contains("The test record") || rdf.contains("mainTitle"),
        "Should contain main title"
    );
    assert!(!graph.is_empty(), "Graph should have triples for title");
}

#[test]
fn test_title_245_with_subtitle() {
    let mut record = Record::new(make_test_leader());
    record.add_control_field("001".to_string(), "test-002".to_string());
    record.add_control_field(
        "008".to_string(),
        "040520s2001    xxu           000 0 eng  ".to_string(),
    );

    let mut f245 = Field::new("245".to_string(), '1', '0');
    f245.add_subfield('a', "Main title :".to_string());
    f245.add_subfield('b', "subtitle ;".to_string());
    f245.add_subfield('c', "by Author.".to_string());
    record.add_field(f245);

    let graph = marc_to_bibframe(&record, &make_config());
    let rdf = graph.serialize(RdfFormat::RdfXml).unwrap();

    assert!(rdf.contains("Main title"), "Should contain main title part");
    assert!(rdf.contains("subtitle"), "Should contain subtitle");
}

#[test]
fn test_title_246_variant_title() {
    let mut record = Record::new(make_test_leader());
    record.add_control_field("001".to_string(), "test-003".to_string());
    record.add_control_field(
        "008".to_string(),
        "040520s2001    xxu           000 0 eng  ".to_string(),
    );

    let mut f245 = Field::new("245".to_string(), '1', '0');
    f245.add_subfield('a', "Primary Title.".to_string());
    record.add_field(f245);

    let mut f246 = Field::new("246".to_string(), ' ', '1');
    f246.add_subfield('a', "Alternative Title.".to_string());
    record.add_field(f246);

    let graph = marc_to_bibframe(&record, &make_config());
    let rdf = graph.serialize(RdfFormat::RdfXml).unwrap();

    assert!(
        rdf.contains("Primary Title"),
        "Should contain primary title"
    );
    // Variant title mapping depends on implementation
}

// ============================================================================
// Unit Tests: Creator/Contributor Fields (1XX, 7XX)
// ============================================================================

#[test]
fn test_creator_100_person() {
    let mut record = Record::new(make_test_leader());
    record.add_control_field("001".to_string(), "test-100".to_string());
    record.add_control_field(
        "008".to_string(),
        "040520s2001    xxu           000 0 eng  ".to_string(),
    );

    let mut f100 = Field::new("100".to_string(), '1', ' ');
    f100.add_subfield('a', "Smith, John,".to_string());
    f100.add_subfield('d', "1950-".to_string());
    record.add_field(f100);

    let graph = marc_to_bibframe(&record, &make_config());
    let rdf = graph.serialize(RdfFormat::RdfXml).unwrap();

    // Should mention person/creator
    assert!(
        rdf.contains("Smith, John") || rdf.contains("Person"),
        "Should contain creator name or Person type"
    );
}

#[test]
fn test_creator_100_with_relator() {
    let mut record = Record::new(make_test_leader());
    record.add_control_field("001".to_string(), "test-101".to_string());
    record.add_control_field(
        "008".to_string(),
        "040520s2001    xxu           000 0 eng  ".to_string(),
    );

    let mut f100 = Field::new("100".to_string(), '1', ' ');
    f100.add_subfield('a', "Doe, Jane,".to_string());
    f100.add_subfield('4', "aut".to_string());
    record.add_field(f100);

    let graph = marc_to_bibframe(&record, &make_config());
    let rdf = graph.serialize(RdfFormat::RdfXml).unwrap();

    assert!(
        rdf.contains("Doe, Jane") || rdf.contains("agent"),
        "Should contain creator agent"
    );
}

#[test]
fn test_contributor_700_person() {
    let mut record = Record::new(make_test_leader());
    record.add_control_field("001".to_string(), "test-700".to_string());
    record.add_control_field(
        "008".to_string(),
        "040520s2001    xxu           000 0 eng  ".to_string(),
    );

    let mut f700 = Field::new("700".to_string(), '1', ' ');
    f700.add_subfield('a', "Jones, Mary,".to_string());
    f700.add_subfield('e', "editor.".to_string());
    record.add_field(f700);

    let graph = marc_to_bibframe(&record, &make_config());
    let rdf = graph.serialize(RdfFormat::RdfXml).unwrap();

    assert!(
        rdf.contains("Jones, Mary") || rdf.contains("agent"),
        "Should contain contributor"
    );
}

// ============================================================================
// Unit Tests: Subject Fields (6XX)
// ============================================================================

#[test]
fn test_subject_650_topic() {
    let mut record = Record::new(make_test_leader());
    record.add_control_field("001".to_string(), "test-650".to_string());
    record.add_control_field(
        "008".to_string(),
        "040520s2001    xxu           000 0 eng  ".to_string(),
    );

    let mut f650 = Field::new("650".to_string(), ' ', '0');
    f650.add_subfield('a', "Computer science".to_string());
    f650.add_subfield('x', "Study and teaching.".to_string());
    record.add_field(f650);

    let graph = marc_to_bibframe(&record, &make_config());
    let rdf = graph.serialize(RdfFormat::RdfXml).unwrap();

    assert!(
        rdf.contains("Computer science") || rdf.contains("subject"),
        "Should contain subject"
    );
}

#[test]
fn test_subject_650_with_subdivisions() {
    let mut record = Record::new(make_test_leader());
    record.add_control_field("001".to_string(), "test-651".to_string());
    record.add_control_field(
        "008".to_string(),
        "040520s2001    xxu           000 0 eng  ".to_string(),
    );

    let mut f651 = Field::new("651".to_string(), ' ', '0');
    f651.add_subfield('a', "United States".to_string());
    f651.add_subfield('x', "History.".to_string());
    record.add_field(f651);

    let graph = marc_to_bibframe(&record, &make_config());
    let rdf = graph.serialize(RdfFormat::RdfXml).unwrap();

    assert!(
        rdf.contains("United States") || rdf.contains("Place"),
        "Should contain geographic subject"
    );
}

#[test]
fn test_subject_655_genre() {
    let mut record = Record::new(make_test_leader());
    record.add_control_field("001".to_string(), "test-655".to_string());
    record.add_control_field(
        "008".to_string(),
        "040520s2001    xxu           000 0 eng  ".to_string(),
    );

    let mut f655 = Field::new("655".to_string(), ' ', '0');
    f655.add_subfield('a', "Science fiction.".to_string());
    record.add_field(f655);

    let graph = marc_to_bibframe(&record, &make_config());
    let rdf = graph.serialize(RdfFormat::RdfXml).unwrap();

    // Genre mapping implementation-dependent
    assert!(!rdf.is_empty(), "Should produce RDF output");
}

// ============================================================================
// Unit Tests: Identifier Fields (020, 022, 024, 035)
// ============================================================================

#[test]
fn test_identifier_020_isbn() {
    let mut record = Record::new(make_test_leader());
    record.add_control_field("001".to_string(), "test-020".to_string());
    record.add_control_field(
        "008".to_string(),
        "040520s2001    xxu           000 0 eng  ".to_string(),
    );

    let mut f020 = Field::new("020".to_string(), ' ', ' ');
    f020.add_subfield('a', "0123456789".to_string());
    f020.add_subfield('q', "(hardcover)".to_string());
    record.add_field(f020);

    let graph = marc_to_bibframe(&record, &make_config());
    let rdf = graph.serialize(RdfFormat::RdfXml).unwrap();

    assert!(
        rdf.contains("0123456789") || rdf.contains("Isbn"),
        "Should contain ISBN identifier"
    );
}

#[test]
fn test_identifier_022_issn() {
    let mut record = Record::new(make_test_leader());
    record.add_control_field("001".to_string(), "test-022".to_string());
    record.add_control_field(
        "008".to_string(),
        "040520s2001    xxu           000 0 eng  ".to_string(),
    );

    let mut f022 = Field::new("022".to_string(), ' ', ' ');
    f022.add_subfield('a', "1234-5678".to_string());
    record.add_field(f022);

    let graph = marc_to_bibframe(&record, &make_config());
    let rdf = graph.serialize(RdfFormat::RdfXml).unwrap();

    assert!(
        rdf.contains("1234-5678") || rdf.contains("Issn"),
        "Should contain ISSN identifier"
    );
}

#[test]
fn test_identifier_024_multiple_types() {
    let mut record = Record::new(make_test_leader());
    record.add_control_field("001".to_string(), "test-024".to_string());
    record.add_control_field(
        "008".to_string(),
        "040520s2001    xxu           000 0 eng  ".to_string(),
    );

    // EAN (indicator 3)
    let mut f024 = Field::new("024".to_string(), '3', ' ');
    f024.add_subfield('a', "9780123456789".to_string());
    record.add_field(f024);

    let graph = marc_to_bibframe(&record, &make_config());
    let rdf = graph.serialize(RdfFormat::RdfXml).unwrap();

    assert!(rdf.contains("9780123456789"), "Should contain EAN");
}

#[test]
fn test_identifier_035_system_number() {
    let mut record = Record::new(make_test_leader());
    record.add_control_field("001".to_string(), "test-035".to_string());
    record.add_control_field(
        "008".to_string(),
        "040520s2001    xxu           000 0 eng  ".to_string(),
    );

    let mut f035 = Field::new("035".to_string(), ' ', ' ');
    f035.add_subfield('a', "(OCoLC)12345678".to_string());
    record.add_field(f035);

    let graph = marc_to_bibframe(&record, &make_config());
    let rdf = graph.serialize(RdfFormat::RdfXml).unwrap();

    assert!(
        rdf.contains("12345678") || rdf.contains("OCoLC"),
        "Should contain system number"
    );
}

// ============================================================================
// Unit Tests: Publication/Provision Activity Fields (260, 264)
// ============================================================================

#[test]
fn test_publication_260_basic() {
    let mut record = Record::new(make_test_leader());
    record.add_control_field("001".to_string(), "test-260".to_string());
    record.add_control_field(
        "008".to_string(),
        "040520s2001    xxu           000 0 eng  ".to_string(),
    );

    let mut f260 = Field::new("260".to_string(), ' ', ' ');
    f260.add_subfield('a', "New York :".to_string());
    f260.add_subfield('b', "Publisher,".to_string());
    f260.add_subfield('c', "2001.".to_string());
    record.add_field(f260);

    let graph = marc_to_bibframe(&record, &make_config());
    let rdf = graph.serialize(RdfFormat::RdfXml).unwrap();

    assert!(
        rdf.contains("New York") || rdf.contains("Publisher"),
        "Should contain publication info"
    );
}

#[test]
fn test_publication_264_rda() {
    let mut record = Record::new(make_test_leader());
    record.add_control_field("001".to_string(), "test-264".to_string());
    record.add_control_field(
        "008".to_string(),
        "040520s2001    xxu           000 0 eng  ".to_string(),
    );

    // 264 with indicator 1='1' (publication)
    let mut f264 = Field::new("264".to_string(), ' ', '1');
    f264.add_subfield('a', "London :".to_string());
    f264.add_subfield('b', "New Publisher,".to_string());
    f264.add_subfield('c', "2023.".to_string());
    record.add_field(f264);

    let graph = marc_to_bibframe(&record, &make_config());
    let rdf = graph.serialize(RdfFormat::RdfXml).unwrap();

    assert!(
        rdf.contains("London") || rdf.contains("New Publisher"),
        "Should contain RDA publication info"
    );
}

// ============================================================================
// Unit Tests: Physical Description Field (300)
// ============================================================================

#[test]
fn test_physical_description_300() {
    let mut record = Record::new(make_test_leader());
    record.add_control_field("001".to_string(), "test-300".to_string());
    record.add_control_field(
        "008".to_string(),
        "040520s2001    xxu           000 0 eng  ".to_string(),
    );

    let mut f300 = Field::new("300".to_string(), ' ', ' ');
    f300.add_subfield('a', "500 pages :".to_string());
    f300.add_subfield('b', "illustrations ;".to_string());
    f300.add_subfield('c', "24 cm.".to_string());
    record.add_field(f300);

    let graph = marc_to_bibframe(&record, &make_config());
    let rdf = graph.serialize(RdfFormat::RdfXml).unwrap();

    assert!(
        rdf.contains("500") || rdf.contains("pages") || rdf.contains("extent"),
        "Should contain extent/physical description"
    );
}

// ============================================================================
// Unit Tests: Notes Field (5XX)
// ============================================================================

#[test]
fn test_note_500_general() {
    let mut record = Record::new(make_test_leader());
    record.add_control_field("001".to_string(), "test-500".to_string());
    record.add_control_field(
        "008".to_string(),
        "040520s2001    xxu           000 0 eng  ".to_string(),
    );

    let mut f500 = Field::new("500".to_string(), ' ', ' ');
    f500.add_subfield(
        'a',
        "Based on the author's doctoral dissertation.".to_string(),
    );
    record.add_field(f500);

    let graph = marc_to_bibframe(&record, &make_config());
    let rdf = graph.serialize(RdfFormat::RdfXml).unwrap();

    assert!(
        rdf.contains("doctoral") || rdf.contains("note"),
        "Should contain general note"
    );
}

#[test]
fn test_note_520_summary() {
    let mut record = Record::new(make_test_leader());
    record.add_control_field("001".to_string(), "test-520".to_string());
    record.add_control_field(
        "008".to_string(),
        "040520s2001    xxu           000 0 eng  ".to_string(),
    );

    let mut f520 = Field::new("520".to_string(), ' ', ' ');
    f520.add_subfield('a', "A comprehensive overview of the subject.".to_string());
    record.add_field(f520);

    let graph = marc_to_bibframe(&record, &make_config());
    let rdf = graph.serialize(RdfFormat::RdfXml).unwrap();

    assert!(
        rdf.contains("overview") || rdf.contains("summary"),
        "Should contain summary note"
    );
}

// ============================================================================
// Unit Tests: Indicator Handling
// ============================================================================

#[test]
fn test_indicator_245_non_filing() {
    // Indicator 2 of 245 specifies non-filing characters
    let mut record = Record::new(make_test_leader());
    record.add_control_field("001".to_string(), "test-ind-245".to_string());
    record.add_control_field(
        "008".to_string(),
        "040520s2001    xxu           000 0 eng  ".to_string(),
    );

    let mut f245 = Field::new("245".to_string(), '1', '2');
    // Second indicator '2' means skip first 2 characters ("A ")
    f245.add_subfield('a', "A Test Title.".to_string());
    record.add_field(f245);

    let graph = marc_to_bibframe(&record, &make_config());
    let rdf = graph.serialize(RdfFormat::RdfXml).unwrap();

    // Should extract title (with or without non-filing processing)
    assert!(
        rdf.contains("Test") || rdf.contains("Title"),
        "Should contain title"
    );
}

#[test]
fn test_indicator_650_subject_source() {
    // Indicator 2 of 650 specifies controlled vocabulary source
    let mut record = Record::new(make_test_leader());
    record.add_control_field("001".to_string(), "test-ind-650".to_string());
    record.add_control_field(
        "008".to_string(),
        "040520s2001    xxu           000 0 eng  ".to_string(),
    );

    let mut f650 = Field::new("650".to_string(), ' ', '0');
    // Indicator 2 = '0' means LCSH
    f650.add_subfield('a', "Computer science".to_string());
    record.add_field(f650);

    let graph = marc_to_bibframe(&record, &make_config());
    let rdf = graph.serialize(RdfFormat::RdfXml).unwrap();

    assert!(
        rdf.contains("Computer science"),
        "Should contain subject with vocabulary source indicator"
    );
}

// ============================================================================
// Unit Tests: Subfield Combinations
// ============================================================================

#[test]
fn test_subfield_combination_100_dates() {
    let mut record = Record::new(make_test_leader());
    record.add_control_field("001".to_string(), "test-subfield-100".to_string());
    record.add_control_field(
        "008".to_string(),
        "040520s2001    xxu           000 0 eng  ".to_string(),
    );

    let mut f100 = Field::new("100".to_string(), '1', ' ');
    f100.add_subfield('a', "Darwin, Charles,".to_string());
    f100.add_subfield('d', "1809-1882.".to_string());
    record.add_field(f100);

    let graph = marc_to_bibframe(&record, &make_config());
    let rdf = graph.serialize(RdfFormat::RdfXml).unwrap();

    assert!(rdf.contains("Darwin"), "Should contain author name");
    // Date handling depends on implementation
}

#[test]
fn test_subfield_combination_650_subdivisions() {
    let mut record = Record::new(make_test_leader());
    record.add_control_field("001".to_string(), "test-subfield-650".to_string());
    record.add_control_field(
        "008".to_string(),
        "040520s2001    xxu           000 0 eng  ".to_string(),
    );

    let mut f650 = Field::new("650".to_string(), ' ', '0');
    f650.add_subfield('a', "History".to_string());
    f650.add_subfield('z', "United States.".to_string());
    f650.add_subfield('x', "20th century.".to_string());
    record.add_field(f650);

    let graph = marc_to_bibframe(&record, &make_config());
    let rdf = graph.serialize(RdfFormat::RdfXml).unwrap();

    assert!(
        rdf.contains("History") || rdf.contains("United States"),
        "Should handle subject subdivisions"
    );
}

// ============================================================================
// Unit Tests: Leader and Type Determination
// ============================================================================

#[test]
fn test_leader_type_book() {
    let mut leader = make_test_leader();
    leader.record_type = 'a'; // language material
    leader.bibliographic_level = 'm'; // monograph
    let mut record = Record::new(leader);
    record.add_control_field("001".to_string(), "test-type-book".to_string());
    record.add_control_field(
        "008".to_string(),
        "040520s2001    xxu           000 0 eng  ".to_string(),
    );

    let graph = marc_to_bibframe(&record, &make_config());
    let rdf = graph.serialize(RdfFormat::RdfXml).unwrap();

    assert!(
        rdf.contains("Text") || rdf.contains("bf:Text"),
        "Book should map to Text type"
    );
}

#[test]
fn test_leader_type_serial() {
    let mut leader = make_test_leader();
    leader.record_type = 'a'; // language material
    leader.bibliographic_level = 's'; // serial
    let mut record = Record::new(leader);
    record.add_control_field("001".to_string(), "test-type-serial".to_string());
    record.add_control_field(
        "008".to_string(),
        "040520c2001    xxu          0eng  ".to_string(),
    );

    let graph = marc_to_bibframe(&record, &make_config());
    let rdf = graph.serialize(RdfFormat::RdfXml).unwrap();

    assert!(
        rdf.contains("Serial") || rdf.contains("bf:Serial"),
        "Serial should map to Serial type"
    );
}

#[test]
fn test_leader_type_music() {
    let mut leader = make_test_leader();
    leader.record_type = 'c'; // notated music
    let mut record = Record::new(leader);
    record.add_control_field("001".to_string(), "test-type-music".to_string());
    record.add_control_field(
        "008".to_string(),
        "040520s2001    xxu  n       000 0 eng  ".to_string(),
    );

    let graph = marc_to_bibframe(&record, &make_config());
    let rdf = graph.serialize(RdfFormat::RdfXml).unwrap();

    assert!(
        rdf.contains("NotatedMusic") || rdf.contains("bf:NotatedMusic"),
        "Music should map to NotatedMusic type"
    );
}

#[test]
fn test_leader_type_map() {
    let mut leader = make_test_leader();
    leader.record_type = 'e'; // cartographic
    let mut record = Record::new(leader);
    record.add_control_field("001".to_string(), "test-type-map".to_string());
    record.add_control_field(
        "008".to_string(),
        "040520s2001    xxu           000 0 eng  ".to_string(),
    );

    let graph = marc_to_bibframe(&record, &make_config());
    let rdf = graph.serialize(RdfFormat::RdfXml).unwrap();

    assert!(
        rdf.contains("Cartography") || rdf.contains("bf:Cartography"),
        "Map should map to Cartography type"
    );
}

#[test]
fn test_leader_type_visual() {
    let mut leader = make_test_leader();
    leader.record_type = 'g'; // projected medium
    let mut record = Record::new(leader);
    record.add_control_field("001".to_string(), "test-type-visual".to_string());
    record.add_control_field(
        "008".to_string(),
        "040520s2001    xxu           000 0 eng  ".to_string(),
    );

    let graph = marc_to_bibframe(&record, &make_config());
    let rdf = graph.serialize(RdfFormat::RdfXml).unwrap();

    assert!(
        rdf.contains("MovingImage")
            || rdf.contains("bf:MovingImage")
            || rdf.contains("StillImage")
            || rdf.contains("bf:StillImage"),
        "Visual material should map to MovingImage or StillImage"
    );
}

// ============================================================================
// Unit Tests: Empty/Minimal Records
// ============================================================================

#[test]
fn test_empty_record() {
    let record = Record::new(make_test_leader());
    let graph = marc_to_bibframe(&record, &make_config());

    // Even empty record should produce Work and Instance
    assert!(
        !graph.is_empty(),
        "Empty record should still produce RDF triples"
    );
}

#[test]
fn test_record_with_control_fields_only() {
    let mut record = Record::new(make_test_leader());
    record.add_control_field("001".to_string(), "control-id".to_string());
    record.add_control_field(
        "008".to_string(),
        "040520s2001    xxu           000 0 eng  ".to_string(),
    );

    let graph = marc_to_bibframe(&record, &make_config());

    assert!(
        !graph.is_empty(),
        "Record with only control fields should produce RDF"
    );
}

// ============================================================================
// Unit Tests: Hub/Expression Support (240 Uniform Title)
// ============================================================================

#[test]
fn test_no_hub_without_240() {
    let mut record = Record::new(make_test_leader());
    record.add_control_field("001".to_string(), "test-no-hub".to_string());

    let mut field_245 = Field::new("245".to_string(), '1', '0');
    field_245.add_subfield('a', "Test Title".to_string());
    record.add_field(field_245);

    let graph = marc_to_bibframe(&record, &make_config());
    let rdf = graph.serialize(RdfFormat::NTriples).unwrap();

    // Without 240, no Hub should be created
    assert!(
        !rdf.contains("bibframe/Hub"),
        "Without 240 field, no Hub should be created"
    );

    // Work should link directly to Instance
    assert!(
        rdf.contains("hasInstance"),
        "Work should have hasInstance relationship"
    );
}

#[test]
fn test_hub_created_with_240() {
    let mut record = Record::new(make_test_leader());
    record.add_control_field("001".to_string(), "test-hub".to_string());

    // Add 240 - Uniform Title
    let mut field_240 = Field::new("240".to_string(), '1', '0');
    field_240.add_subfield('a', "Works.".to_string());
    field_240.add_subfield('l', "English".to_string());
    record.add_field(field_240);

    let mut field_245 = Field::new("245".to_string(), '1', '0');
    field_245.add_subfield('a', "Complete Works of Shakespeare".to_string());
    record.add_field(field_245);

    let graph = marc_to_bibframe(&record, &make_config());
    let rdf = graph.serialize(RdfFormat::NTriples).unwrap();

    // Hub should be created
    assert!(
        rdf.contains("bibframe/Hub"),
        "With 240 field, Hub should be created"
    );

    // Work should link to Hub via hasExpression
    assert!(
        rdf.contains("hasExpression"),
        "Work should have hasExpression relationship to Hub"
    );

    // Hub should link to Instance via hasInstance
    assert!(
        rdf.contains("hasInstance"),
        "Hub should have hasInstance relationship to Instance"
    );
}

#[test]
fn test_hub_has_uniform_title() {
    let mut record = Record::new(make_test_leader());
    record.add_control_field("001".to_string(), "test-hub-title".to_string());

    let mut field_240 = Field::new("240".to_string(), '1', '0');
    field_240.add_subfield('a', "Don Quixote".to_string());
    field_240.add_subfield('l', "English".to_string());
    field_240.add_subfield('f', "1605".to_string());
    record.add_field(field_240);

    let graph = marc_to_bibframe(&record, &make_config());
    let rdf = graph.serialize(RdfFormat::NTriples).unwrap();

    // Hub should have the uniform title
    assert!(
        rdf.contains("Don Quixote"),
        "Hub should contain uniform title from 240 $a"
    );

    // Hub should have language
    assert!(
        rdf.contains("English"),
        "Hub should contain language from 240 $l"
    );
}

#[test]
fn test_hub_uri_generation() {
    let mut record = Record::new(make_test_leader());
    record.add_control_field("001".to_string(), "test-hub-uri".to_string());

    let mut field_240 = Field::new("240".to_string(), '1', '0');
    field_240.add_subfield('a', "Works".to_string());
    record.add_field(field_240);

    let config = BibframeConfig::new().with_base_uri("http://example.org/");
    let graph = marc_to_bibframe(&record, &config);
    let rdf = graph.serialize(RdfFormat::NTriples).unwrap();

    // Hub should have a minted URI
    assert!(
        rdf.contains("http://example.org/hub/test-hub-uri"),
        "Hub should have minted URI with control number"
    );
}

#[test]
fn test_hub_with_music_240() {
    let mut record = Record::new(make_test_leader());
    record.leader.record_type = 'c'; // Notated music
    record.add_control_field("001".to_string(), "test-music-hub".to_string());

    // 240 for music with key, arrangement
    let mut field_240 = Field::new("240".to_string(), '1', '0');
    field_240.add_subfield('a', "Sonatas".to_string());
    field_240.add_subfield('n', "no. 14".to_string());
    field_240.add_subfield('p', "Moonlight".to_string());
    record.add_field(field_240);

    let graph = marc_to_bibframe(&record, &make_config());
    let rdf = graph.serialize(RdfFormat::NTriples).unwrap();

    assert!(rdf.contains("bibframe/Hub"), "Music 240 should create Hub");
    assert!(rdf.contains("Sonatas"), "Hub should contain uniform title");
    assert!(
        rdf.contains("no. 14") || rdf.contains("partNumber"),
        "Hub should contain part number"
    );
}

// ============================================================================
// Unit Tests: Item/Holdings Support (852, 876-878)
// ============================================================================

#[test]
fn test_no_item_without_holdings() {
    let mut record = Record::new(make_test_leader());
    record.add_control_field("001".to_string(), "test-no-item".to_string());

    let mut field_245 = Field::new("245".to_string(), '1', '0');
    field_245.add_subfield('a', "Test Title".to_string());
    record.add_field(field_245);

    let graph = marc_to_bibframe(&record, &make_config());
    let rdf = graph.serialize(RdfFormat::NTriples).unwrap();

    // Without holdings fields, no Item should be created
    assert!(
        !rdf.contains("bibframe/Item"),
        "Without holdings fields, no Item should be created"
    );
}

#[test]
fn test_item_created_with_852() {
    let mut record = Record::new(make_test_leader());
    record.add_control_field("001".to_string(), "test-item".to_string());

    // Add 852 - Location field
    let mut field_852 = Field::new("852".to_string(), '0', '0');
    field_852.add_subfield('a', "DLC".to_string()); // Library of Congress
    field_852.add_subfield('b', "General Collections".to_string());
    field_852.add_subfield('h', "QA76.73".to_string());
    field_852.add_subfield('i', ".P97".to_string());
    record.add_field(field_852);

    let graph = marc_to_bibframe(&record, &make_config());
    let rdf = graph.serialize(RdfFormat::NTriples).unwrap();

    // Item should be created
    assert!(
        rdf.contains("bibframe/Item"),
        "With 852 field, Item should be created"
    );

    // Instance should link to Item
    assert!(
        rdf.contains("hasItem"),
        "Instance should have hasItem relationship to Item"
    );

    // Item should have location
    assert!(
        rdf.contains("DLC"),
        "Item should contain location from 852 $a"
    );
}

#[test]
fn test_item_has_call_number() {
    let mut record = Record::new(make_test_leader());
    record.add_control_field("001".to_string(), "test-item-callno".to_string());

    let mut field_852 = Field::new("852".to_string(), '0', '1');
    field_852.add_subfield('a', "MH".to_string());
    field_852.add_subfield('h', "PS3537".to_string());
    field_852.add_subfield('i', ".T4244 Z7".to_string());
    record.add_field(field_852);

    let graph = marc_to_bibframe(&record, &make_config());
    let rdf = graph.serialize(RdfFormat::NTriples).unwrap();

    // Item should have shelfMark
    assert!(
        rdf.contains("shelfMark") || rdf.contains("PS3537"),
        "Item should contain call number from 852 $h/$i"
    );
}

#[test]
fn test_item_with_barcode() {
    let mut record = Record::new(make_test_leader());
    record.add_control_field("001".to_string(), "test-item-barcode".to_string());

    let mut field_852 = Field::new("852".to_string(), '0', '0');
    field_852.add_subfield('a', "DLC".to_string());
    field_852.add_subfield('p', "00001234567".to_string()); // Barcode
    record.add_field(field_852);

    let graph = marc_to_bibframe(&record, &make_config());
    let rdf = graph.serialize(RdfFormat::NTriples).unwrap();

    // Item should have barcode
    assert!(
        rdf.contains("00001234567"),
        "Item should contain barcode from 852 $p"
    );
    assert!(
        rdf.contains("Barcode"),
        "Item should have Barcode identifier type"
    );
}

#[test]
fn test_item_uri_generation() {
    let mut record = Record::new(make_test_leader());
    record.add_control_field("001".to_string(), "test-item-uri".to_string());

    let mut field_852 = Field::new("852".to_string(), '0', '0');
    field_852.add_subfield('a', "DLC".to_string());
    record.add_field(field_852);

    let config = BibframeConfig::new().with_base_uri("http://example.org/");
    let graph = marc_to_bibframe(&record, &config);
    let rdf = graph.serialize(RdfFormat::NTriples).unwrap();

    // Item should have a minted URI
    assert!(
        rdf.contains("http://example.org/item/test-item-uri-0"),
        "Item should have minted URI with control number and sequence"
    );
}

#[test]
fn test_multiple_items_from_852() {
    let mut record = Record::new(make_test_leader());
    record.add_control_field("001".to_string(), "test-multi-item".to_string());

    // Add multiple 852 fields (multiple copies)
    let mut field_852a = Field::new("852".to_string(), '0', '0');
    field_852a.add_subfield('a', "DLC".to_string());
    field_852a.add_subfield('p', "copy1".to_string());
    record.add_field(field_852a);

    let mut field_852b = Field::new("852".to_string(), '0', '0');
    field_852b.add_subfield('a', "MH".to_string());
    field_852b.add_subfield('p', "copy2".to_string());
    record.add_field(field_852b);

    let config = BibframeConfig::new().with_base_uri("http://example.org/");
    let graph = marc_to_bibframe(&record, &config);
    let rdf = graph.serialize(RdfFormat::NTriples).unwrap();

    // Should have two Items with different URIs
    assert!(
        rdf.contains("item/test-multi-item-0"),
        "First item should have URI with seq 0"
    );
    assert!(
        rdf.contains("item/test-multi-item-1"),
        "Second item should have URI with seq 1"
    );
}

// ===========================================================================
// Classification Tests (050, 060, 080, 082, 084)
// ===========================================================================

#[test]
fn test_classification_050_lcc() {
    // 050 - Library of Congress Classification
    let mut record = Record::new(make_test_leader());
    record.add_control_field("001".to_string(), "test-lcc".to_string());

    let mut field_050 = Field::new("050".to_string(), '0', '0');
    field_050.add_subfield('a', "QA76.73".to_string());
    field_050.add_subfield('b', ".P98 2023".to_string());
    record.add_field(field_050);

    let graph = marc_to_bibframe(&record, &make_config());
    let rdf = graph.serialize(RdfFormat::NTriples).unwrap();

    // Should create ClassificationLcc
    assert!(
        rdf.contains("ClassificationLcc"),
        "Should create bf:ClassificationLcc type"
    );
    // Should have classification portion
    assert!(
        rdf.contains("classificationPortion"),
        "Should include classificationPortion property"
    );
    assert!(
        rdf.contains("QA76.73"),
        "Should include the LC class number"
    );
    // Should have item portion (cutter)
    assert!(
        rdf.contains("itemPortion"),
        "Should include itemPortion property"
    );
    assert!(
        rdf.contains(".P98 2023"),
        "Should include the cutter number"
    );
    // Should be linked to Work via classification property
    assert!(
        rdf.contains("classification"),
        "Should link to Work via classification"
    );
}

#[test]
fn test_classification_082_dewey() {
    // 082 - Dewey Decimal Classification
    let mut record = Record::new(make_test_leader());
    record.add_control_field("001".to_string(), "test-dewey".to_string());

    let mut field_082 = Field::new("082".to_string(), '0', '4');
    field_082.add_subfield('a', "005.133".to_string());
    record.add_field(field_082);

    let graph = marc_to_bibframe(&record, &make_config());
    let rdf = graph.serialize(RdfFormat::NTriples).unwrap();

    // Should create ClassificationDdc
    assert!(
        rdf.contains("ClassificationDdc"),
        "Should create bf:ClassificationDdc type"
    );
    assert!(rdf.contains("005.133"), "Should include the Dewey number");
}

#[test]
fn test_classification_060_nlm() {
    // 060 - National Library of Medicine Classification
    let mut record = Record::new(make_test_leader());
    record.add_control_field("001".to_string(), "test-nlm".to_string());

    let mut field_060 = Field::new("060".to_string(), '1', '4');
    field_060.add_subfield('a', "WB 102".to_string());
    field_060.add_subfield('b', "S65m 2020".to_string());
    record.add_field(field_060);

    let graph = marc_to_bibframe(&record, &make_config());
    let rdf = graph.serialize(RdfFormat::NTriples).unwrap();

    // Should create ClassificationNlm
    assert!(
        rdf.contains("ClassificationNlm"),
        "Should create bf:ClassificationNlm type"
    );
    assert!(
        rdf.contains("WB 102"),
        "Should include the NLM class number"
    );
}

#[test]
fn test_classification_080_udc() {
    // 080 - Universal Decimal Classification
    let mut record = Record::new(make_test_leader());
    record.add_control_field("001".to_string(), "test-udc".to_string());

    let mut field_080 = Field::new("080".to_string(), ' ', ' ');
    field_080.add_subfield('a', "004.42".to_string());
    record.add_field(field_080);

    let graph = marc_to_bibframe(&record, &make_config());
    let rdf = graph.serialize(RdfFormat::NTriples).unwrap();

    // Should create ClassificationUdc
    assert!(
        rdf.contains("ClassificationUdc"),
        "Should create bf:ClassificationUdc type"
    );
    assert!(rdf.contains("004.42"), "Should include the UDC number");
}

#[test]
fn test_classification_084_other() {
    // 084 - Other Classification with source
    let mut record = Record::new(make_test_leader());
    record.add_control_field("001".to_string(), "test-other-class".to_string());

    let mut field_084 = Field::new("084".to_string(), ' ', ' ');
    field_084.add_subfield('a', "B2430".to_string());
    field_084.add_subfield('2', "bliss".to_string());
    record.add_field(field_084);

    let graph = marc_to_bibframe(&record, &make_config());
    let rdf = graph.serialize(RdfFormat::NTriples).unwrap();

    // Should create generic Classification
    assert!(
        rdf.contains("Classification"),
        "Should create bf:Classification type"
    );
    assert!(
        rdf.contains("B2430"),
        "Should include the classification number"
    );
    // Should include source
    assert!(rdf.contains("source"), "Should include source property");
    assert!(rdf.contains("bliss"), "Should include the source code");
}

#[test]
fn test_classification_linked_to_work() {
    // Classification should be linked to Work, not Instance
    let mut record = Record::new(make_test_leader());
    record.add_control_field("001".to_string(), "test-class-link".to_string());

    let mut field_050 = Field::new("050".to_string(), '0', '0');
    field_050.add_subfield('a', "PS3566.A822".to_string());
    record.add_field(field_050);

    let graph = marc_to_bibframe(&record, &make_config());
    let rdf = graph.serialize(RdfFormat::NTriples).unwrap();

    // The classification triple should reference the Work URI, not Instance
    // Work URI pattern: http://example.org/work/...
    // Instance URI pattern: http://example.org/instance/...
    assert!(
        rdf.contains("work/") && rdf.contains("classification"),
        "Classification should be linked to Work"
    );
}

#[test]
fn test_multiple_classifications() {
    // Record can have multiple classification fields
    let mut record = Record::new(make_test_leader());
    record.add_control_field("001".to_string(), "test-multi-class".to_string());

    let mut field_050 = Field::new("050".to_string(), '0', '0');
    field_050.add_subfield('a', "QA76.73.R87".to_string());
    record.add_field(field_050);

    let mut field_082 = Field::new("082".to_string(), '0', '4');
    field_082.add_subfield('a', "005.133".to_string());
    record.add_field(field_082);

    let graph = marc_to_bibframe(&record, &make_config());
    let rdf = graph.serialize(RdfFormat::NTriples).unwrap();

    // Should have both LCC and DDC
    assert!(
        rdf.contains("ClassificationLcc"),
        "Should have LC Classification"
    );
    assert!(
        rdf.contains("ClassificationDdc"),
        "Should have Dewey Classification"
    );
    assert!(rdf.contains("QA76.73.R87"), "Should have LC number");
    assert!(rdf.contains("005.133"), "Should have Dewey number");
}