terraphim_persistence 1.16.31

Terraphim persistence layer
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
//! Comprehensive tests for persistence key generation consistency
//!
//! This test suite validates that:
//! 1. Key normalization works consistently for both Thesaurus and Document
//! 2. Save/load operations work correctly across different backends
//! 3. Edge cases with special characters, spaces, and unicode are handled
//! 4. Cross-backend consistency is maintained

use serial_test::serial;
use terraphim_persistence::{DeviceStorage, Persistable, Result};
use terraphim_types::{Document, NormalizedTerm, NormalizedTermValue, Thesaurus};

/// Initialize memory-only persistence for testing
async fn init_test_persistence() -> Result<()> {
    DeviceStorage::init_memory_only().await?;
    Ok(())
}

#[tokio::test]
#[serial]
async fn test_key_normalization_consistency() -> Result<()> {
    init_test_persistence().await?;

    // Test data with various challenging characters
    let test_cases = vec![
        ("Simple", "simple.json"),
        ("Test Name", "test_name.json"),
        ("Test-Name_123", "test_name_123.json"),
        ("TeSt NaMe", "test_name.json"),
        ("AI/ML Engineer", "ai_ml_engineer.json"),
        ("Data & Analytics", "data_analytics.json"),
        ("Role (v2.0)", "role_v2_0.json"),
        ("Engineer@Company", "engineer_company.json"),
        ("Terraphim Engineer", "terraphim_engineer.json"),
    ];

    for (input, expected_suffix) in test_cases {
        // Test Thesaurus key normalization
        let thesaurus = Thesaurus::new(input.to_string());
        let thesaurus_key = thesaurus.get_key();
        let expected_thesaurus_key = format!("thesaurus_{}", expected_suffix);
        assert_eq!(
            thesaurus_key, expected_thesaurus_key,
            "Thesaurus key mismatch for input '{}': got '{}', expected '{}'",
            input, thesaurus_key, expected_thesaurus_key
        );

        // Test Document key normalization (using input as document ID)
        let document = Document {
            id: input.to_string(),
            ..Default::default()
        };
        let document_key = document.get_key();
        let expected_document_key = format!("document_{}", expected_suffix);
        assert_eq!(
            document_key, expected_document_key,
            "Document key mismatch for input '{}': got '{}', expected '{}'",
            input, document_key, expected_document_key
        );
    }

    Ok(())
}

#[tokio::test]
#[serial]
async fn test_thesaurus_persistence_with_complex_names() -> Result<()> {
    init_test_persistence().await?;

    let test_names = vec![
        "Terraphim Engineer",
        "AI/ML Specialist",
        "Data & Analytics Expert",
        "Software Engineer (Senior)",
        "Full-Stack Developer",
        "Product Manager - Technical",
    ];

    for name in test_names {
        println!("Testing thesaurus persistence for name: '{}'", name);

        // Create and populate thesaurus
        let mut thesaurus = Thesaurus::new(name.to_string());
        let test_term =
            NormalizedTerm::new(1, NormalizedTermValue::from("test-concept".to_string()));
        thesaurus.insert(
            NormalizedTermValue::from("test".to_string()),
            test_term.clone(),
        );

        assert_eq!(thesaurus.len(), 1, "Thesaurus should have 1 entry");
        assert_eq!(thesaurus.name(), name, "Thesaurus name should match");

        // Save thesaurus
        println!("  Saving thesaurus with key: '{}'", thesaurus.get_key());
        thesaurus.save().await?;

        // Load with same name
        let mut loaded_thesaurus = Thesaurus::new(name.to_string());
        loaded_thesaurus = loaded_thesaurus.load().await?;

        // Verify loaded data
        assert_eq!(
            loaded_thesaurus.len(),
            1,
            "Loaded thesaurus should have 1 entry for name '{}'",
            name
        );
        assert_eq!(
            loaded_thesaurus.name(),
            name,
            "Loaded thesaurus name should match for '{}'",
            name
        );

        let loaded_term = loaded_thesaurus.get(&NormalizedTermValue::from("test".to_string()));
        assert!(
            loaded_term.is_some(),
            "Loaded thesaurus should contain test term for name '{}'",
            name
        );
        assert_eq!(
            loaded_term.unwrap(),
            &test_term,
            "Loaded term should match original for name '{}'",
            name
        );

        println!(
            "  ✅ Successfully persisted and loaded thesaurus for name: '{}'",
            name
        );
    }

    Ok(())
}

#[tokio::test]
#[serial]
async fn test_document_persistence_with_various_ids() -> Result<()> {
    init_test_persistence().await?;

    let test_ids = vec![
        "simple-id",
        "a33bd45bece9c7cb", // Hex hash format
        "http://example.com/document/123",
        "file:///path/to/document.txt",
        "document with spaces",
        "document-with-special@chars#123",
        "UPPERCASE_DOCUMENT_ID",
        "mixed_Case_Document_123",
    ];

    for id in test_ids {
        println!("Testing document persistence for ID: '{}'", id);

        // Create document
        let document = Document {
            id: id.to_string(),
            title: format!("Test Document for {}", id),
            body: format!("This is the body content for document {}", id),
            url: format!("https://example.com/{}", id),
            description: Some(format!("Description for document {}", id)),
            ..Default::default()
        };

        // Save document
        println!("  Saving document with key: '{}'", document.get_key());
        document.save().await?;

        // Load with same ID
        let mut loaded_document = Document {
            id: id.to_string(),
            ..Default::default()
        };
        loaded_document = loaded_document.load().await?;

        // Verify loaded data
        assert_eq!(
            loaded_document.id, id,
            "Loaded document ID should match for '{}'",
            id
        );
        assert_eq!(
            loaded_document.title,
            format!("Test Document for {}", id),
            "Loaded document title should match for '{}'",
            id
        );
        assert_eq!(
            loaded_document.body,
            format!("This is the body content for document {}", id),
            "Loaded document body should match for '{}'",
            id
        );
        assert_eq!(
            loaded_document.url,
            format!("https://example.com/{}", id),
            "Loaded document URL should match for '{}'",
            id
        );
        assert_eq!(
            loaded_document.description,
            Some(format!("Description for document {}", id)),
            "Loaded document description should match for '{}'",
            id
        );

        println!(
            "  ✅ Successfully persisted and loaded document for ID: '{}'",
            id
        );
    }

    Ok(())
}

#[tokio::test]
#[serial]
async fn test_cross_backend_consistency() -> Result<()> {
    init_test_persistence().await?;

    // Test thesaurus consistency across save/load cycles
    let thesaurus_name = "Cross Backend Test";
    let mut thesaurus = Thesaurus::new(thesaurus_name.to_string());

    // Add test data
    let term1 = NormalizedTerm::new(1, NormalizedTermValue::from("concept1".to_string()));
    let term2 = NormalizedTerm::new(2, NormalizedTermValue::from("concept2".to_string()));
    thesaurus.insert(
        NormalizedTermValue::from("term1".to_string()),
        term1.clone(),
    );
    thesaurus.insert(
        NormalizedTermValue::from("term2".to_string()),
        term2.clone(),
    );

    // Save to memory backend
    thesaurus.save_to_one("memory").await?;

    // Load from memory backend
    let mut loaded_thesaurus = Thesaurus::new(thesaurus_name.to_string());
    loaded_thesaurus = loaded_thesaurus.load().await?;

    assert_eq!(
        loaded_thesaurus.len(),
        2,
        "Thesaurus should have 2 entries after memory round-trip"
    );
    assert!(
        loaded_thesaurus
            .get(&NormalizedTermValue::from("term1".to_string()))
            .is_some(),
        "Term1 should exist after memory round-trip"
    );
    assert!(
        loaded_thesaurus
            .get(&NormalizedTermValue::from("term2".to_string()))
            .is_some(),
        "Term2 should exist after memory round-trip"
    );

    // Test document consistency
    let document_id = "cross-backend-test-doc";
    let document = Document {
        id: document_id.to_string(),
        title: "Cross Backend Test Document".to_string(),
        body: "This is a test document for cross-backend consistency.".to_string(),
        ..Default::default()
    };

    // Save to memory backend
    document.save_to_one("memory").await?;

    // Load from memory backend
    let mut loaded_document = Document {
        id: document_id.to_string(),
        ..Default::default()
    };
    loaded_document = loaded_document.load().await?;

    assert_eq!(
        loaded_document.id, document_id,
        "Document ID should match after memory round-trip"
    );
    assert_eq!(
        loaded_document.title, "Cross Backend Test Document",
        "Document title should match after memory round-trip"
    );
    assert_eq!(
        loaded_document.body, "This is a test document for cross-backend consistency.",
        "Document body should match after memory round-trip"
    );

    Ok(())
}

#[tokio::test]
#[serial]
async fn test_unicode_and_emoji_handling() -> Result<()> {
    init_test_persistence().await?;

    let unicode_test_cases = vec![
        ("Simple ASCII", "simple_ascii"),
        ("Unicode: café", "unicode_caf"),
        ("Emoji: 🚀 Engineer", "emoji_engineer"),
        ("Mixed: AI/ML 🤖 Expert", "mixed_ai_ml_expert"),
        ("Chinese: 人工智能", "chinese"), // Non-ASCII chars removed, colon and space become underscore
        ("Arabic: مهندس البرمجيات", "arabic"), // Non-ASCII chars removed, colon and space become underscore
    ];

    for (input, expected_normalized) in unicode_test_cases {
        println!("Testing unicode handling for: '{}'", input);

        // Test thesaurus with unicode name
        let thesaurus = Thesaurus::new(input.to_string());
        let key = thesaurus.get_key();
        let expected_key = if expected_normalized.is_empty() {
            "thesaurus_.json".to_string()
        } else {
            format!("thesaurus_{}.json", expected_normalized)
        };

        assert_eq!(
            key, expected_key,
            "Unicode normalization failed for '{}': got '{}', expected '{}'",
            input, key, expected_key
        );

        // Only test save/load for cases that result in valid keys
        if !expected_normalized.is_empty() {
            let mut test_thesaurus = Thesaurus::new(input.to_string());
            let term = NormalizedTerm::new(1, NormalizedTermValue::from("test".to_string()));
            test_thesaurus.insert(NormalizedTermValue::from("test".to_string()), term);

            test_thesaurus.save().await?;

            let mut loaded = Thesaurus::new(input.to_string());
            loaded = loaded.load().await?;

            assert_eq!(
                loaded.len(),
                1,
                "Unicode thesaurus should persist correctly for '{}'",
                input
            );
        }

        println!("  ✅ Unicode handling validated for: '{}'", input);
    }

    Ok(())
}

#[tokio::test]
#[serial]
async fn test_empty_and_edge_case_keys() -> Result<()> {
    init_test_persistence().await?;

    // Test edge cases
    let edge_cases = vec![
        ("", "fallback"),    // Empty name -> fallback hash
        ("   ", "fallback"), // Only spaces -> fallback hash
        ("!!!", "fallback"), // Only special chars -> fallback hash
        ("123", "123"),      // Only numbers
        ("a", "a"),          // Single character
    ];

    for (input, expected_normalized) in edge_cases {
        println!("Testing edge case: '{}'", input);

        let thesaurus = Thesaurus::new(input.to_string());
        let key = thesaurus.get_key();

        if expected_normalized == "fallback" {
            // For fallback cases, just check that it starts with the fallback prefix
            assert!(
                key.starts_with("thesaurus_fallback_"),
                "Edge case normalization should use fallback for '{}': got '{}'",
                input,
                key
            );
        } else {
            let expected_key = format!("thesaurus_{}.json", expected_normalized);
            assert_eq!(
                key, expected_key,
                "Edge case normalization failed for '{}': got '{}', expected '{}'",
                input, key, expected_key
            );
        }

        println!("  ✅ Edge case handled: '{}' → '{}'", input, key);
    }

    Ok(())
}

#[tokio::test]
#[serial]
#[ignore] // Flaky test - performance depends on environment (regex-based key normalization)
async fn test_key_generation_performance() -> Result<()> {
    init_test_persistence().await?;

    let start = std::time::Instant::now();

    // Generate many keys to test performance
    for i in 0..1000 {
        let name = format!("Performance Test Role {}", i);
        let thesaurus = Thesaurus::new(name);
        let _key = thesaurus.get_key();

        let id = format!("performance-test-doc-{}", i);
        let document = Document {
            id,
            ..Default::default()
        };
        let _doc_key = document.get_key();
    }

    let duration = start.elapsed();
    println!("Generated 2000 keys in {:?}", duration);

    // Performance should be reasonable (less than 5 seconds for 2000 keys)
    assert!(
        duration.as_millis() < 5000,
        "Key generation should be fast, took {:?}",
        duration
    );

    Ok(())
}

#[tokio::test]
#[serial]
async fn test_load_documents_by_ids_concurrency() -> Result<()> {
    init_test_persistence().await?;
    use terraphim_persistence::load_documents_by_ids;

    let mut ids = Vec::new();
    for i in 0..50 {
        let id = format!("concurrent-doc-{}", i);
        ids.push(id.clone());
        let document = Document {
            id: id.clone(),
            title: format!("Test Document {}", i),
            body: "content".to_string(),
            ..Default::default()
        };
        document.save().await?;
    }

    let loaded = load_documents_by_ids(&ids).await?;
    assert_eq!(loaded.len(), 50, "Should load all documents");

    // Verify content of a few documents
    for doc in loaded {
        assert!(ids.contains(&doc.id));
        assert_eq!(doc.body, "content");
    }

    Ok(())
}