ddex-builder 0.4.5

Deterministic DDEX XML builder with smart normalization
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
//! Tests for DDEX semantic diff functionality

use super::*;
use crate::diff::formatter::DiffFormatter;

#[test]
fn test_identical_documents_no_changes() {
    let mut engine = DiffEngine::new();

    let ast1 = create_simple_ast("Root", "same content");
    let ast2 = create_simple_ast("Root", "same content");

    let changeset = engine.diff(&ast1, &ast2).unwrap();

    assert!(!changeset.has_changes());
    assert_eq!(changeset.summary.total_changes, 0);
    assert_eq!(changeset.impact_level(), types::ImpactLevel::None);
}

#[test]
fn test_simple_text_change() {
    let mut engine = DiffEngine::new();

    let ast1 = create_simple_ast("Root", "old content");
    let ast2 = create_simple_ast("Root", "new content");

    let changeset = engine.diff(&ast1, &ast2).unwrap();

    assert!(changeset.has_changes());
    assert_eq!(changeset.summary.total_changes, 1);
    assert_eq!(
        changeset.changes[0].change_type,
        types::ChangeType::TextModified
    );
    assert_eq!(
        changeset.changes[0].old_value,
        Some("old content".to_string())
    );
    assert_eq!(
        changeset.changes[0].new_value,
        Some("new content".to_string())
    );
}

#[test]
fn test_formatting_ignored_by_default() {
    let mut engine = DiffEngine::new();

    let ast1 = create_simple_ast("Root", "  content  ");
    let ast2 = create_simple_ast("Root", "content");

    let changeset = engine.diff(&ast1, &ast2).unwrap();

    // Should have no changes because formatting differences are ignored
    assert!(!changeset.has_changes());
}

#[test]
fn test_formatting_respected_when_configured() {
    let mut config = DiffConfig::default();
    config.ignore_formatting = false;
    let mut engine = DiffEngine::new_with_config(config);

    let ast1 = create_simple_ast("Root", "  content  ");
    let ast2 = create_simple_ast("Root", "content");

    let changeset = engine.diff(&ast1, &ast2).unwrap();

    // Should have changes because formatting differences are not ignored
    assert!(changeset.has_changes());
    assert_eq!(changeset.summary.total_changes, 1);
}

#[test]
fn test_attribute_changes() {
    let mut engine = DiffEngine::new();

    let ast1 = AST {
        root: Element::new("Release").with_attr("UPC", "123456789012"),
        namespaces: indexmap::IndexMap::new(),
        schema_location: None,
    };

    let ast2 = AST {
        root: Element::new("Release").with_attr("UPC", "987654321098"),
        namespaces: indexmap::IndexMap::new(),
        schema_location: None,
    };

    let changeset = engine.diff(&ast1, &ast2).unwrap();

    assert!(changeset.has_changes());
    assert_eq!(changeset.summary.total_changes, 1);
    assert_eq!(
        changeset.changes[0].change_type,
        types::ChangeType::AttributeModified
    );
    assert!(changeset.changes[0].is_critical); // UPC is a critical field
}

#[test]
fn test_critical_field_detection() {
    let mut engine = DiffEngine::new();

    // Test critical field (UPC)
    let ast1 = AST {
        root: Element::new("Release").with_attr("UPC", "123456789012"),
        namespaces: indexmap::IndexMap::new(),
        schema_location: None,
    };

    let ast2 = AST {
        root: Element::new("Release").with_attr("UPC", "987654321098"),
        namespaces: indexmap::IndexMap::new(),
        schema_location: None,
    };

    let changeset = engine.diff(&ast1, &ast2).unwrap();

    let critical_changes = changeset.critical_changes();
    assert_eq!(critical_changes.len(), 1);
    assert_eq!(changeset.impact_level(), types::ImpactLevel::High);
}

#[test]
fn test_ignored_fields() {
    let mut engine = DiffEngine::new();

    // MessageId is in ignored_fields by default
    let ast1 = AST {
        root: Element::new("MessageHeader").with_attr("MessageId", "MSG-001"),
        namespaces: indexmap::IndexMap::new(),
        schema_location: None,
    };

    let ast2 = AST {
        root: Element::new("MessageHeader").with_attr("MessageId", "MSG-002"),
        namespaces: indexmap::IndexMap::new(),
        schema_location: None,
    };

    let changeset = engine.diff(&ast1, &ast2).unwrap();

    // Should have no changes because MessageId is ignored
    assert!(!changeset.has_changes());
}

#[test]
fn test_element_addition_removal() {
    let mut engine = DiffEngine::new();

    // AST with no children
    let ast1 = AST {
        root: Element::new("Root"),
        namespaces: indexmap::IndexMap::new(),
        schema_location: None,
    };

    // AST with a child element
    let mut root_with_child = Element::new("Root");
    root_with_child.add_child(Element::new("Child").with_text("content"));
    let ast2 = AST {
        root: root_with_child,
        namespaces: indexmap::IndexMap::new(),
        schema_location: None,
    };

    let changeset = engine.diff(&ast1, &ast2).unwrap();

    assert!(changeset.has_changes());
    assert_eq!(changeset.summary.additions, 1);

    // Test removal (reverse order)
    let changeset_removal = engine.diff(&ast2, &ast1).unwrap();
    assert_eq!(changeset_removal.summary.deletions, 1);
}

#[test]
fn test_reference_equivalence() {
    let mut config = DiffConfig::default();
    config.ignore_reference_ids = true;
    let mut engine = DiffEngine::new_with_config(config);

    // Two elements with different reference IDs AND different content
    let mut resource1 = Element::new("Resource").with_attr("ResourceReference", "R001");
    resource1.add_child(Element::new("Title").with_text("Original Track"));

    let mut resource2 = Element::new("Resource").with_attr("ResourceReference", "R002");
    resource2.add_child(Element::new("Title").with_text("Remastered Track"));

    let ast1 = AST {
        root: resource1,
        namespaces: indexmap::IndexMap::new(),
        schema_location: None,
    };

    let ast2 = AST {
        root: resource2,
        namespaces: indexmap::IndexMap::new(),
        schema_location: None,
    };

    let changeset = engine.diff(&ast1, &ast2).unwrap();

    // Should have changes due to different content, even though reference IDs are ignored
    assert!(changeset.has_changes());
    assert_eq!(changeset.summary.total_changes, 1);
    assert_eq!(
        changeset.changes[0].change_type,
        types::ChangeType::TextModified
    );
}

#[test]
fn test_numeric_tolerance() {
    let mut config = DiffConfig::default();
    config.numeric_tolerance = Some(0.01);
    let mut engine = DiffEngine::new_with_config(config);

    // Test prices within tolerance
    let ast1 = AST {
        root: Element::new("Deal").with_attr("Price", "9.99"),
        namespaces: indexmap::IndexMap::new(),
        schema_location: None,
    };

    let ast2 = AST {
        root: Element::new("Deal").with_attr("Price", "9.999"),
        namespaces: indexmap::IndexMap::new(),
        schema_location: None,
    };

    let changeset = engine.diff(&ast1, &ast2).unwrap();

    // Should have no changes due to numeric tolerance
    assert!(!changeset.has_changes());
}

#[test]
fn test_diff_formatter_summary() {
    let mut changeset = types::ChangeSet::new();

    changeset.add_change(types::SemanticChange {
        path: types::DiffPath::root()
            .with_element("Release")
            .with_attribute("UPC"),
        change_type: types::ChangeType::AttributeModified,
        old_value: Some("123456789012".to_string()),
        new_value: Some("987654321098".to_string()),
        is_critical: true,
        description: "UPC changed from 123456789012 to 987654321098".to_string(),
    });

    let summary = DiffFormatter::format_summary(&changeset);

    assert!(summary.contains("DDEX Semantic Diff Summary"));
    assert!(summary.contains("Critical Changes"));
    assert!(summary.contains("UPC changed"));
    assert!(summary.contains("Impact Level: High"));
}

#[test]
fn test_diff_formatter_json() {
    let mut changeset = types::ChangeSet::new();

    changeset.add_change(types::SemanticChange {
        path: types::DiffPath::root().with_element("Title"),
        change_type: types::ChangeType::TextModified,
        old_value: Some("Old Title".to_string()),
        new_value: Some("New Title".to_string()),
        is_critical: false,
        description: "Title changed".to_string(),
    });

    let json_result = DiffFormatter::format_json(&changeset);
    assert!(json_result.is_ok());

    let json_str = json_result.unwrap();
    assert!(json_str.contains("total_changes"));
    assert!(json_str.contains("Title changed"));
    assert!(json_str.contains("Old Title"));
    assert!(json_str.contains("New Title"));
}

#[test]
fn test_diff_formatter_json_patch() {
    let mut changeset = types::ChangeSet::new();

    changeset.add_change(types::SemanticChange {
        path: types::DiffPath::root()
            .with_element("Release")
            .with_attribute("UPC"),
        change_type: types::ChangeType::AttributeModified,
        old_value: Some("123456789012".to_string()),
        new_value: Some("987654321098".to_string()),
        is_critical: true,
        description: "UPC modified".to_string(),
    });

    let patch_result = DiffFormatter::format_json_patch(&changeset);
    assert!(patch_result.is_ok());

    let patch_str = patch_result.unwrap();
    assert!(patch_str.contains("\"op\": \"replace\""));
    assert!(patch_str.contains("\"path\": \"/Release/@UPC\""));
    assert!(patch_str.contains("987654321098"));
}

#[test]
fn test_diff_formatter_html() {
    let mut changeset = types::ChangeSet::new();

    changeset.add_change(types::SemanticChange {
        path: types::DiffPath::root().with_element("Title"),
        change_type: types::ChangeType::TextModified,
        old_value: Some("Old Title".to_string()),
        new_value: Some("New Title".to_string()),
        is_critical: false,
        description: "Title changed".to_string(),
    });

    let html = DiffFormatter::format_html(&changeset);

    assert!(html.contains("<!DOCTYPE html>"));
    assert!(html.contains("DDEX Semantic Diff Report"));
    assert!(html.contains("Title changed"));
    assert!(html.contains("Old Title"));
    assert!(html.contains("New Title"));
}

#[test]
fn test_update_message_generation() {
    let mut changeset = types::ChangeSet::new();

    changeset.add_change(types::SemanticChange {
        path: types::DiffPath::root()
            .with_element("Release")
            .with_attribute("UPC"),
        change_type: types::ChangeType::AttributeModified,
        old_value: Some("123456789012".to_string()),
        new_value: Some("987654321098".to_string()),
        is_critical: true,
        description: "UPC changed".to_string(),
    });

    let update_msg_result =
        DiffFormatter::generate_update_message(&changeset, Some("TEST-UPDATE-001"));
    assert!(update_msg_result.is_ok());

    let update_msg = update_msg_result.unwrap();
    assert!(update_msg.contains("<?xml version=\"1.0\""));
    assert!(update_msg.contains("<UpdateReleaseMessage"));
    assert!(update_msg.contains("TEST-UPDATE-001"));
    assert!(update_msg.contains("<UpdateType>Modify</UpdateType>"));
    assert!(update_msg.contains("<IsCritical>true</IsCritical>"));
}

#[test]
fn test_business_impact_analysis() {
    let engine = DiffEngine::new();

    // Create changeset with mix of critical and non-critical changes
    let mut changeset = types::ChangeSet::new();

    // Critical change
    changeset.add_change(types::SemanticChange {
        path: types::DiffPath::root()
            .with_element("Release")
            .with_attribute("UPC"),
        change_type: types::ChangeType::AttributeModified,
        old_value: Some("123456789012".to_string()),
        new_value: Some("987654321098".to_string()),
        is_critical: true,
        description: "UPC changed".to_string(),
    });

    // Non-critical change
    changeset.add_change(types::SemanticChange {
        path: types::DiffPath::root().with_element("Title"),
        change_type: types::ChangeType::TextModified,
        old_value: Some("Old Title".to_string()),
        new_value: Some("New Title".to_string()),
        is_critical: false,
        description: "Title changed".to_string(),
    });

    // Simulate business impact analysis
    engine.analyze_business_impact(&mut changeset);

    assert_eq!(changeset.impact_level(), types::ImpactLevel::High);
    assert!(changeset.metadata.contains_key("critical_changes"));
    assert_eq!(changeset.metadata.get("critical_changes").unwrap(), "1");
    assert_eq!(changeset.metadata.get("impact_level").unwrap(), "HIGH");
}

// Helper function to create a simple AST for testing
fn create_simple_ast(element_name: &str, text_content: &str) -> AST {
    AST {
        root: Element::new(element_name).with_text(text_content),
        namespaces: indexmap::IndexMap::new(),
        schema_location: None,
    }
}

#[cfg(test)]
mod integration_tests {
    use super::*;

    /// Test using real DDEX samples
    #[test]
    fn test_real_ddex_diff_basic_changes() {
        let mut engine = DiffEngine::new();

        // These would normally be parsed from actual DDEX XML
        // For now, we create simplified ASTs to represent the key differences

        let ast_v1 = create_ddex_ast_v1();
        let ast_v2 = create_ddex_ast_v2();

        let changeset = engine.diff(&ast_v1, &ast_v2).unwrap();

        assert!(changeset.has_changes());

        // Expected changes: Title, UPC, LabelName, Genre, Duration, etc.
        assert!(changeset.summary.total_changes > 0);

        // Should detect critical changes (UPC change)
        let critical_changes = changeset.critical_changes();
        assert!(!critical_changes.is_empty());

        // Generate different output formats
        let summary = DiffFormatter::format_summary(&changeset);
        assert!(summary.contains("Critical Changes"));

        let json_result = DiffFormatter::format_json(&changeset);
        assert!(json_result.is_ok());

        let html = DiffFormatter::format_html(&changeset);
        assert!(html.contains("DDEX Semantic Diff Report"));
    }

    #[test]
    fn test_formatting_only_diff() {
        let mut engine = DiffEngine::new();

        // Test documents that differ only in formatting
        let ast_original = create_simple_ast("Root", "content");
        let ast_formatted = create_simple_ast("Root", "  content  "); // Extra whitespace

        let changeset = engine.diff(&ast_original, &ast_formatted).unwrap();

        // Should have no changes with default config (formatting ignored)
        assert!(!changeset.has_changes());
        assert_eq!(changeset.impact_level(), types::ImpactLevel::None);
    }

    // Helper functions to create test ASTs representing DDEX documents
    fn create_ddex_ast_v1() -> AST {
        let mut root = Element::new("NewReleaseMessage");

        // Add a simplified Release element
        let mut release = Element::new("Release");
        release = release.with_attr("UPC", "123456789012");
        release.add_child(Element::new("Title").with_text("Test Album"));
        release.add_child(Element::new("LabelName").with_text("Test Label"));
        release.add_child(Element::new("Genre").with_text("Pop"));

        root.add_child(release);

        AST {
            root,
            namespaces: indexmap::IndexMap::new(),
            schema_location: None,
        }
    }

    fn create_ddex_ast_v2() -> AST {
        let mut root = Element::new("NewReleaseMessage");

        // Add a Release element with changes
        let mut release = Element::new("Release");
        release = release.with_attr("UPC", "987654321098"); // Changed UPC (critical)
        release.add_child(Element::new("Title").with_text("Test Album (Deluxe Edition)")); // Changed title
        release.add_child(Element::new("LabelName").with_text("New Test Label")); // Changed label
        release.add_child(Element::new("Genre").with_text("Rock")); // Changed genre

        root.add_child(release);

        AST {
            root,
            namespaces: indexmap::IndexMap::new(),
            schema_location: None,
        }
    }
}