thread-flow 0.1.0

Thread dataflow integration for data processing pipelines, using CocoIndex.
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
// SPDX-FileCopyrightText: 2025 Knitli Inc. <knitli@knit.li>
// SPDX-FileCopyrightText: 2026 Knitli Inc.
//
// SPDX-License-Identifier: AGPL-3.0-or-later

//! Integration tests for the incremental update system.
//!
//! Tests backend factory pattern, feature gating, and end-to-end
//! storage operations across all three backend implementations.

use std::path::{Path, PathBuf};
use thread_flow::incremental::DependencyGraph;
use thread_flow::incremental::backends::{BackendConfig, BackendType, create_backend};
use thread_flow::incremental::storage::StorageBackend;
use thread_flow::incremental::types::{
    AnalysisDefFingerprint, DependencyEdge, DependencyType, SymbolDependency, SymbolKind,
};

// ─── Backend Factory Tests ────────────────────────────────────────────────────

#[tokio::test]
async fn test_backend_factory_in_memory() {
    let result = create_backend(BackendType::InMemory, BackendConfig::InMemory).await;
    assert!(
        result.is_ok(),
        "InMemory backend should always be available"
    );
}

#[tokio::test]
async fn test_backend_factory_configuration_mismatch() {
    // Try to create InMemory backend with Postgres config
    let result = create_backend(
        BackendType::InMemory,
        BackendConfig::Postgres {
            database_url: "test".to_string(),
        },
    )
    .await;

    assert!(result.is_err());
    if let Err(err) = result {
        assert!(
            matches!(
                err,
                thread_flow::incremental::IncrementalError::InitializationFailed(_)
            ),
            "Configuration mismatch should return InitializationFailed"
        );
    }
}

// ─── Feature Gating Tests ─────────────────────────────────────────────────────

#[cfg(not(feature = "postgres-backend"))]
#[tokio::test]
async fn test_postgres_backend_unavailable_without_feature() {
    let result = create_backend(
        BackendType::Postgres,
        BackendConfig::Postgres {
            database_url: "postgresql://localhost/test".to_string(),
        },
    )
    .await;

    assert!(result.is_err());
    if let Err(err) = result {
        assert!(
            matches!(
                err,
                thread_flow::incremental::IncrementalError::UnsupportedBackend("postgres")
            ),
            "Should return UnsupportedBackend when postgres-backend feature is disabled"
        );
    }
}

#[cfg(not(feature = "d1-backend"))]
#[tokio::test]
async fn test_d1_backend_unavailable_without_feature() {
    let result = create_backend(
        BackendType::D1,
        BackendConfig::D1 {
            account_id: "test".to_string(),
            database_id: "test".to_string(),
            api_token: "test".to_string(),
        },
    )
    .await;

    assert!(result.is_err());
    if let Err(err) = result {
        assert!(
            matches!(
                err,
                thread_flow::incremental::IncrementalError::UnsupportedBackend("d1")
            ),
            "Should return UnsupportedBackend when d1-backend feature is disabled"
        );
    }
}

// ─── Runtime Backend Selection Tests ──────────────────────────────────────────

#[tokio::test]
async fn test_runtime_backend_selection_fallback() {
    // Test fallback logic when preferred backends are unavailable
    let backend = if cfg!(feature = "postgres-backend") {
        // Try Postgres first (but only if DATABASE_URL is set for testing)
        if let Ok(database_url) = std::env::var("DATABASE_URL") {
            create_backend(
                BackendType::Postgres,
                BackendConfig::Postgres { database_url },
            )
            .await
            .ok()
        } else {
            None
        }
    } else if cfg!(feature = "d1-backend") {
        // Fall back to D1 (but it won't work without real credentials)
        None
    } else {
        None
    };

    // Always fall back to InMemory if nothing else available
    let backend = if let Some(b) = backend {
        b
    } else {
        create_backend(BackendType::InMemory, BackendConfig::InMemory)
            .await
            .expect("InMemory should always work")
    };

    // Verify the backend is usable
    let fp = AnalysisDefFingerprint::new(b"test content");
    backend
        .save_fingerprint(Path::new("test.rs"), &fp)
        .await
        .expect("Should be able to save fingerprint");
}

// ─── End-to-End Integration Tests ─────────────────────────────────────────────

/// Test complete workflow: save fingerprint → load → verify → delete
#[tokio::test]
async fn test_e2e_fingerprint_lifecycle() {
    let backend = create_backend(BackendType::InMemory, BackendConfig::InMemory)
        .await
        .expect("Failed to create backend");

    let file_path = Path::new("src/main.rs");
    let fp1 = AnalysisDefFingerprint::new(b"version 1");

    // 1. Save initial fingerprint
    backend
        .save_fingerprint(file_path, &fp1)
        .await
        .expect("Failed to save fingerprint");

    // 2. Load and verify
    let loaded = backend
        .load_fingerprint(file_path)
        .await
        .expect("Failed to load fingerprint")
        .expect("Fingerprint should exist");

    assert!(loaded.content_matches(b"version 1"));

    // 3. Update fingerprint (upsert semantics)
    let fp2 = AnalysisDefFingerprint::new(b"version 2");
    backend
        .save_fingerprint(file_path, &fp2)
        .await
        .expect("Failed to update fingerprint");

    let loaded = backend
        .load_fingerprint(file_path)
        .await
        .expect("Failed to load updated fingerprint")
        .expect("Updated fingerprint should exist");

    assert!(loaded.content_matches(b"version 2"));
    assert!(!loaded.content_matches(b"version 1"));

    // 4. Delete fingerprint
    let deleted = backend
        .delete_fingerprint(file_path)
        .await
        .expect("Failed to delete fingerprint");

    assert!(
        deleted,
        "Should return true when deleting existing fingerprint"
    );

    // 5. Verify deletion
    let loaded = backend
        .load_fingerprint(file_path)
        .await
        .expect("Failed to check deleted fingerprint");

    assert!(loaded.is_none(), "Fingerprint should be deleted");
}

/// Test complete workflow: save edges → load → query → delete
#[tokio::test]
async fn test_e2e_dependency_edge_lifecycle() {
    let backend = create_backend(BackendType::InMemory, BackendConfig::InMemory)
        .await
        .expect("Failed to create backend");

    // Create dependency edges: main.rs → utils.rs → helpers.rs
    let edge1 = DependencyEdge::new(
        PathBuf::from("src/main.rs"),
        PathBuf::from("src/utils.rs"),
        DependencyType::Import,
    );

    let edge2 = DependencyEdge {
        from: PathBuf::from("src/utils.rs"),
        to: PathBuf::from("src/helpers.rs"),
        dep_type: DependencyType::Import,
        symbol: Some(SymbolDependency {
            from_symbol: "format_output".to_string(),
            to_symbol: "escape_html".to_string(),
            kind: SymbolKind::Function,
            strength: thread_flow::incremental::DependencyStrength::Strong,
        }),
    };

    // 1. Save edges
    backend
        .save_edge(&edge1)
        .await
        .expect("Failed to save edge1");
    backend
        .save_edge(&edge2)
        .await
        .expect("Failed to save edge2");

    // 2. Query edges from main.rs
    let edges_from_main = backend
        .load_edges_from(Path::new("src/main.rs"))
        .await
        .expect("Failed to load edges from main.rs");

    assert_eq!(edges_from_main.len(), 1);
    assert_eq!(edges_from_main[0].to, PathBuf::from("src/utils.rs"));

    // 3. Query edges to helpers.rs
    let edges_to_helpers = backend
        .load_edges_to(Path::new("src/helpers.rs"))
        .await
        .expect("Failed to load edges to helpers.rs");

    assert_eq!(edges_to_helpers.len(), 1);
    assert_eq!(edges_to_helpers[0].from, PathBuf::from("src/utils.rs"));
    assert!(edges_to_helpers[0].symbol.is_some());

    // 4. Delete all edges involving utils.rs
    let deleted_count = backend
        .delete_edges_for(Path::new("src/utils.rs"))
        .await
        .expect("Failed to delete edges");

    assert_eq!(
        deleted_count, 2,
        "Should delete both edges involving utils.rs"
    );

    // 5. Verify deletion
    let remaining_from_main = backend
        .load_edges_from(Path::new("src/main.rs"))
        .await
        .expect("Failed to verify deletion");

    assert_eq!(remaining_from_main.len(), 0, "All edges should be deleted");
}

/// Test full graph persistence: save → load → verify structure
#[tokio::test]
async fn test_e2e_full_graph_persistence() {
    let backend = create_backend(BackendType::InMemory, BackendConfig::InMemory)
        .await
        .expect("Failed to create backend");

    // 1. Create a dependency graph
    let mut graph = DependencyGraph::new();

    graph.add_edge(DependencyEdge::new(
        PathBuf::from("a.rs"),
        PathBuf::from("b.rs"),
        DependencyType::Import,
    ));
    graph.add_edge(DependencyEdge::new(
        PathBuf::from("b.rs"),
        PathBuf::from("c.rs"),
        DependencyType::Import,
    ));
    graph.add_edge(DependencyEdge::new(
        PathBuf::from("a.rs"),
        PathBuf::from("c.rs"),
        DependencyType::Type,
    ));

    // 2. Save full graph
    backend
        .save_full_graph(&graph)
        .await
        .expect("Failed to save graph");

    // 3. Load full graph
    let loaded_graph = backend
        .load_full_graph()
        .await
        .expect("Failed to load graph");

    // 4. Verify graph structure
    assert_eq!(
        loaded_graph.edge_count(),
        3,
        "All edges should be persisted"
    );
    assert!(
        loaded_graph.contains_node(Path::new("a.rs")),
        "Node a.rs should exist"
    );
    assert!(
        loaded_graph.contains_node(Path::new("b.rs")),
        "Node b.rs should exist"
    );
    assert!(
        loaded_graph.contains_node(Path::new("c.rs")),
        "Node c.rs should exist"
    );

    // 5. Verify affected files computation works after load
    let changed: thread_utilities::RapidSet<PathBuf> =
        [PathBuf::from("c.rs")].into_iter().collect();
    let affected = loaded_graph.find_affected_files(&changed);

    assert!(
        affected.contains(&PathBuf::from("b.rs")),
        "b.rs depends on c.rs"
    );
    assert!(
        affected.contains(&PathBuf::from("a.rs")),
        "a.rs depends on c.rs directly and via b.rs"
    );
}

/// Test incremental invalidation workflow
#[tokio::test]
async fn test_e2e_incremental_invalidation() {
    let backend = create_backend(BackendType::InMemory, BackendConfig::InMemory)
        .await
        .expect("Failed to create backend");

    // Setup: Create dependency chain
    let mut graph = DependencyGraph::new();
    graph.add_edge(DependencyEdge::new(
        PathBuf::from("main.rs"),
        PathBuf::from("utils.rs"),
        DependencyType::Import,
    ));
    graph.add_edge(DependencyEdge::new(
        PathBuf::from("utils.rs"),
        PathBuf::from("config.rs"),
        DependencyType::Import,
    ));

    backend
        .save_full_graph(&graph)
        .await
        .expect("Failed to save initial graph");

    // Save initial fingerprints
    backend
        .save_fingerprint(
            Path::new("main.rs"),
            &AnalysisDefFingerprint::new(b"main v1"),
        )
        .await
        .expect("Failed to save main.rs fingerprint");
    backend
        .save_fingerprint(
            Path::new("utils.rs"),
            &AnalysisDefFingerprint::new(b"utils v1"),
        )
        .await
        .expect("Failed to save utils.rs fingerprint");
    backend
        .save_fingerprint(
            Path::new("config.rs"),
            &AnalysisDefFingerprint::new(b"config v1"),
        )
        .await
        .expect("Failed to save config.rs fingerprint");

    // Simulate config.rs change
    let new_config_fp = AnalysisDefFingerprint::new(b"config v2");

    // Check if file changed
    let old_config_fp = backend
        .load_fingerprint(Path::new("config.rs"))
        .await
        .expect("Failed to load config.rs fingerprint")
        .expect("config.rs fingerprint should exist");

    assert!(
        !old_config_fp.content_matches(b"config v2"),
        "Content changed"
    );

    // Find affected files
    let changed: thread_utilities::RapidSet<PathBuf> =
        [PathBuf::from("config.rs")].into_iter().collect();
    let affected = graph.find_affected_files(&changed);

    assert!(
        affected.contains(&PathBuf::from("utils.rs")),
        "utils.rs imports config.rs"
    );
    assert!(
        affected.contains(&PathBuf::from("main.rs")),
        "main.rs transitively depends on config.rs"
    );

    // Update fingerprint after re-analysis
    backend
        .save_fingerprint(Path::new("config.rs"), &new_config_fp)
        .await
        .expect("Failed to update config.rs fingerprint");

    // Verify update
    let updated_fp = backend
        .load_fingerprint(Path::new("config.rs"))
        .await
        .expect("Failed to load updated fingerprint")
        .expect("Updated fingerprint should exist");

    assert!(updated_fp.content_matches(b"config v2"));
}

// ─── Multi-Backend Comparison Tests ───────────────────────────────────────────

/// Verify all backends implement the same behavior for basic operations
#[tokio::test]
async fn test_backend_behavior_consistency() {
    let backends: Vec<Box<dyn StorageBackend>> = vec![
        create_backend(BackendType::InMemory, BackendConfig::InMemory)
            .await
            .expect("InMemory should always work"),
        // Add Postgres and D1 when features are enabled
        #[cfg(feature = "postgres-backend")]
        {
            if let Ok(url) = std::env::var("TEST_DATABASE_URL") {
                create_backend(
                    BackendType::Postgres,
                    BackendConfig::Postgres { database_url: url },
                )
                .await
                .ok()
            } else {
                None
            }
        }
        .unwrap_or_else(|| {
            Box::new(thread_flow::incremental::storage::InMemoryStorage::new())
                as Box<dyn StorageBackend>
        }),
    ];

    for backend in backends {
        // Test basic fingerprint operations
        let fp = AnalysisDefFingerprint::new(b"test");
        backend
            .save_fingerprint(Path::new("test.rs"), &fp)
            .await
            .expect("All backends should support save");

        let loaded = backend
            .load_fingerprint(Path::new("test.rs"))
            .await
            .expect("All backends should support load")
            .expect("Fingerprint should exist");

        assert!(loaded.content_matches(b"test"));

        // Test edge operations
        let edge = DependencyEdge::new(
            PathBuf::from("a.rs"),
            PathBuf::from("b.rs"),
            DependencyType::Import,
        );

        backend
            .save_edge(&edge)
            .await
            .expect("All backends should support edge save");

        let edges = backend
            .load_edges_from(Path::new("a.rs"))
            .await
            .expect("All backends should support edge query");

        assert_eq!(edges.len(), 1);
    }
}