terraphim_service 1.16.33

Terraphim service for handling user requests and responses.
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
#[cfg(test)]
mod logical_operators_fix_validation_tests {
    use terraphim_config::{ConfigBuilder, ConfigId, ConfigState};
    use terraphim_service::TerraphimService;
    use terraphim_types::{
        Document, Layer, LogicalOperator, NormalizedTermValue, RoleName, SearchQuery,
    };

    async fn setup_test_service() -> TerraphimService {
        let mut config = ConfigBuilder::new_with_id(ConfigId::Embedded)
            .build_default_embedded()
            .build()
            .unwrap();
        let config_state = ConfigState::new(&mut config).await.unwrap();
        TerraphimService::new(config_state)
    }

    fn create_test_documents() -> Vec<Document> {
        vec![
            Document {
                id: "1".to_string(),
                title: "Rust programming".to_string(),
                body: "This document covers Rust programming concepts".to_string(),
                url: "http://example.com/rust".to_string(),
                description: Some("A guide to Rust".to_string()),
                rank: None,
                tags: None,
                summarization: None,
                stub: None,
                source_haystack: None,
                doc_type: terraphim_types::DocumentType::KgEntry,
                synonyms: None,
                route: None,
                priority: None,
            },
            Document {
                id: "2".to_string(),
                title: "Async programming in Rust".to_string(),
                body: "Learn about async and await in Rust programming language".to_string(),
                url: "http://example.com/async".to_string(),
                description: Some("Async Rust tutorial".to_string()),
                rank: None,
                tags: None,
                summarization: None,
                stub: None,
                source_haystack: None,
                doc_type: terraphim_types::DocumentType::KgEntry,
                synonyms: None,
                route: None,
                priority: None,
            },
            Document {
                id: "3".to_string(),
                title: "JavaScript async patterns".to_string(),
                body: "Modern async patterns in JavaScript development".to_string(),
                url: "http://example.com/js".to_string(),
                description: Some("JS async guide".to_string()),
                rank: None,
                tags: None,
                summarization: None,
                stub: None,
                source_haystack: None,
                doc_type: terraphim_types::DocumentType::KgEntry,
                synonyms: None,
                route: None,
                priority: None,
            },
            Document {
                id: "4".to_string(),
                title: "Python programming".to_string(),
                body: "Introduction to Python programming concepts and syntax".to_string(),
                url: "http://example.com/python".to_string(),
                description: Some("Python basics".to_string()),
                rank: None,
                tags: None,
                summarization: None,
                stub: None,
                source_haystack: None,
                doc_type: terraphim_types::DocumentType::KgEntry,
                synonyms: None,
                route: None,
                priority: None,
            },
        ]
    }

    #[tokio::test]
    async fn test_get_all_terms_no_duplication_and_operator() {
        let mut service = setup_test_service().await;
        let documents = create_test_documents();

        // Test query: "rust AND async" - should match only document 2
        let query = SearchQuery {
            search_term: NormalizedTermValue::from("rust"),
            search_terms: Some(vec![
                NormalizedTermValue::from("rust"),
                NormalizedTermValue::from("async"),
            ]),
            operator: Some(LogicalOperator::And),
            skip: Some(0),
            limit: Some(10),
            role: Some(RoleName::from("Default")),
            layer: Layer::default(),
            include_pinned: false,
        };

        // Test get_all_terms to ensure no duplication
        let all_terms = query.get_all_terms();
        let terms_as_strings: Vec<String> =
            all_terms.iter().map(|t| t.as_str().to_string()).collect();

        // Should contain both terms but no duplicates
        assert_eq!(
            terms_as_strings.len(),
            2,
            "Should have exactly 2 terms, got: {:?}",
            terms_as_strings
        );
        assert!(
            terms_as_strings.contains(&"rust".to_string()),
            "Should contain 'rust'"
        );
        assert!(
            terms_as_strings.contains(&"async".to_string()),
            "Should contain 'async'"
        );

        // Test the filtering with documents
        let result = service
            .apply_logical_operators_to_documents(&query, documents)
            .await;
        assert!(result.is_ok(), "Filtering should not fail");

        let filtered_docs = result.unwrap();
        // Should match only document 2 (contains both "rust" and "async")
        assert_eq!(
            filtered_docs.len(),
            1,
            "Should match exactly 1 document for 'rust AND async'"
        );
        assert_eq!(
            filtered_docs[0].id, "2",
            "Should match the async Rust document"
        );
    }

    #[tokio::test]
    async fn test_get_all_terms_no_duplication_or_operator() {
        let mut service = setup_test_service().await;
        let documents = create_test_documents();

        // Test query: "rust OR python" - should match documents 1, 2, and 4
        let query = SearchQuery {
            search_term: NormalizedTermValue::from("rust"),
            search_terms: Some(vec![
                NormalizedTermValue::from("rust"),
                NormalizedTermValue::from("python"),
            ]),
            operator: Some(LogicalOperator::Or),
            skip: Some(0),
            limit: Some(10),
            role: Some(RoleName::from("Default")),
            layer: Layer::default(),
            include_pinned: false,
        };

        // Test get_all_terms to ensure no duplication
        let all_terms = query.get_all_terms();
        let terms_as_strings: Vec<String> =
            all_terms.iter().map(|t| t.as_str().to_string()).collect();

        assert_eq!(
            terms_as_strings.len(),
            2,
            "Should have exactly 2 terms, got: {:?}",
            terms_as_strings
        );
        assert!(
            terms_as_strings.contains(&"rust".to_string()),
            "Should contain 'rust'"
        );
        assert!(
            terms_as_strings.contains(&"python".to_string()),
            "Should contain 'python'"
        );

        // Test the filtering
        let result = service
            .apply_logical_operators_to_documents(&query, documents)
            .await;
        assert!(result.is_ok(), "Filtering should not fail");

        let filtered_docs = result.unwrap();
        // Should match documents 1, 2 (rust), and 4 (python)
        assert_eq!(
            filtered_docs.len(),
            3,
            "Should match exactly 3 documents for 'rust OR python'"
        );

        let matched_ids: Vec<&String> = filtered_docs.iter().map(|d| &d.id).collect();
        assert!(
            matched_ids.contains(&&"1".to_string()),
            "Should match document 1 (Rust programming)"
        );
        assert!(
            matched_ids.contains(&&"2".to_string()),
            "Should match document 2 (Async Rust)"
        );
        assert!(
            matched_ids.contains(&&"4".to_string()),
            "Should match document 4 (Python)"
        );
    }

    #[tokio::test]
    async fn test_single_term_query_backward_compatibility() {
        let mut service = setup_test_service().await;
        let _documents = create_test_documents();

        // Test single term query (no operator)
        let query = SearchQuery {
            search_term: NormalizedTermValue::from("async"),
            search_terms: None,
            operator: None,
            skip: Some(0),
            limit: Some(10),
            role: Some(RoleName::from("Default")),
            layer: Layer::default(),
            include_pinned: false,
        };

        // Test get_all_terms for single term
        let all_terms = query.get_all_terms();
        let terms_as_strings: Vec<String> =
            all_terms.iter().map(|t| t.as_str().to_string()).collect();

        assert_eq!(
            terms_as_strings.len(),
            1,
            "Single term query should have exactly 1 term"
        );
        assert_eq!(
            terms_as_strings[0], "async",
            "Should contain the single search term"
        );

        // Test search functionality
        let result = service.search(&query).await;
        assert!(result.is_ok(), "Single term search should not fail");

        let filtered_docs = result.unwrap();
        // In test environment might have no documents, just verify it doesn't crash
        println!(
            "Single term search returned {} documents",
            filtered_docs.len()
        );
    }

    #[tokio::test]
    async fn test_word_boundary_matching() {
        let mut service = setup_test_service().await;

        // Create documents with partial word matches that should NOT match
        let documents = vec![
            Document {
                id: "1".to_string(),
                title: "JavaScript programming".to_string(),
                body: "Learn JavaScript fundamentals".to_string(),
                url: "http://example.com/js".to_string(),
                description: Some("JS guide".to_string()),
                rank: None,
                tags: None,
                summarization: None,
                stub: None,
                source_haystack: None,
                doc_type: terraphim_types::DocumentType::KgEntry,
                synonyms: None,
                route: None,
                priority: None,
            },
            Document {
                id: "2".to_string(),
                title: "Java programming".to_string(),
                body: "Object-oriented programming in Java language".to_string(),
                url: "http://example.com/java".to_string(),
                description: Some("Java tutorial".to_string()),
                rank: None,
                tags: None,
                summarization: None,
                stub: None,
                source_haystack: None,
                doc_type: terraphim_types::DocumentType::KgEntry,
                synonyms: None,
                route: None,
                priority: None,
            },
        ];

        // Search for "java" should match document 2 but NOT document 1 (JavaScript)
        let query = SearchQuery {
            search_term: NormalizedTermValue::from("java"),
            search_terms: None,
            operator: None,
            skip: Some(0),
            limit: Some(10),
            role: Some(RoleName::from("Default")),
            layer: Layer::default(),
            include_pinned: false,
        };

        let result = service
            .apply_logical_operators_to_documents(&query, documents)
            .await;
        assert!(result.is_ok(), "Word boundary filtering should not fail");

        let filtered_docs = result.unwrap();
        // Should match only document 2 (Java), not document 1 (JavaScript)
        assert_eq!(
            filtered_docs.len(),
            1,
            "Word boundary matching should be precise"
        );
        assert_eq!(
            filtered_docs[0].id, "2",
            "Should match only the Java document, not JavaScript"
        );
    }

    #[tokio::test]
    async fn test_multiple_terms_and_operator_strict_matching() {
        let mut service = setup_test_service().await;
        let documents = create_test_documents();

        // Test query with 3 terms: "rust AND async AND programming"
        let query = SearchQuery {
            search_term: NormalizedTermValue::from("rust"),
            search_terms: Some(vec![
                NormalizedTermValue::from("rust"),
                NormalizedTermValue::from("async"),
                NormalizedTermValue::from("programming"),
            ]),
            operator: Some(LogicalOperator::And),
            skip: Some(0),
            limit: Some(10),
            role: Some(RoleName::from("Default")),
            layer: Layer::default(),
            include_pinned: false,
        };

        // Test get_all_terms
        let all_terms = query.get_all_terms();
        assert_eq!(all_terms.len(), 3, "Should have exactly 3 terms");

        // Test the filtering - should match only document 2 (has all three terms)
        let result = service
            .apply_logical_operators_to_documents(&query, documents)
            .await;
        assert!(result.is_ok(), "Multi-term AND filtering should not fail");

        let filtered_docs = result.unwrap();
        assert_eq!(
            filtered_docs.len(),
            1,
            "Should match exactly 1 document with all 3 terms"
        );
        assert_eq!(
            filtered_docs[0].id, "2",
            "Should match the async Rust document"
        );
    }

    #[tokio::test]
    async fn test_multiple_terms_or_operator_inclusive_matching() {
        let mut service = setup_test_service().await;
        let documents = create_test_documents();

        // Test query: "rust OR javascript OR python"
        let query = SearchQuery {
            search_term: NormalizedTermValue::from("rust"),
            search_terms: Some(vec![
                NormalizedTermValue::from("rust"),
                NormalizedTermValue::from("javascript"),
                NormalizedTermValue::from("python"),
            ]),
            operator: Some(LogicalOperator::Or),
            skip: Some(0),
            limit: Some(10),
            role: Some(RoleName::from("Default")),
            layer: Layer::default(),
            include_pinned: false,
        };

        // Test get_all_terms
        let all_terms = query.get_all_terms();
        assert_eq!(all_terms.len(), 3, "Should have exactly 3 terms");

        // Test the filtering - should match documents 1, 2, 3, 4
        let result = service
            .apply_logical_operators_to_documents(&query, documents)
            .await;
        assert!(result.is_ok(), "Multi-term OR filtering should not fail");

        let filtered_docs = result.unwrap();
        assert_eq!(
            filtered_docs.len(),
            4,
            "Should match all 4 documents (each contains at least one term)"
        );
    }
}