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
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
// SPDX-FileCopyrightText: 2025 Knitli Inc. <knitli@knit.li>
// SPDX-FileCopyrightText: 2026 Knitli Inc.
//
// SPDX-License-Identifier: AGPL-3.0-or-later

//! Minimal D1 Target Module Tests - Working subset for API-compatible coverage
//!
//! This is a reduced test suite focusing on functionality that works with the current
//! recoco API. The full d1_target_tests.rs requires extensive updates to match recoco's
//! API changes.
//!
//! ## Coverage Focus
//! - SQL generation (no recoco dependencies)
//! - Basic type conversions with simple types
//! - State management basics
//! - TargetFactoryBase core methods

use recoco::base::schema::{BasicValueType, EnrichedValueType, FieldSchema, ValueType};
use recoco::base::value::{BasicValue, FieldValues, KeyPart, Value};
use recoco::ops::factory_bases::TargetFactoryBase;
use recoco::setup::{ResourceSetupChange, SetupChangeType};
use thread_flow::targets::d1::{
    D1ExportContext, D1SetupChange, D1SetupState, D1TableId, D1TargetFactory, IndexSchema,
    basic_value_to_json, key_part_to_json, value_to_json, value_type_to_sql,
};

// ============================================================================
// Helper Functions
// ============================================================================

fn test_field_schema(name: &str, typ: BasicValueType, nullable: bool) -> FieldSchema {
    FieldSchema::new(
        name,
        EnrichedValueType {
            typ: ValueType::Basic(typ),
            nullable,
            attrs: Default::default(),
        },
    )
}

fn test_table_id() -> D1TableId {
    D1TableId {
        database_id: "test-db-456".to_string(),
        table_name: "test_table".to_string(),
    }
}

// ============================================================================
// Value Conversion Tests - Core Coverage
// ============================================================================

#[test]
fn test_key_part_to_json_str() {
    let key_part = KeyPart::Str("test_string".into());
    let json = key_part_to_json(&key_part).expect("Failed to convert str");
    assert_eq!(json, serde_json::json!("test_string"));
}

#[test]
fn test_key_part_to_json_bool() {
    let key_part_true = KeyPart::Bool(true);
    let json_true = key_part_to_json(&key_part_true).expect("Failed to convert bool");
    assert_eq!(json_true, serde_json::json!(true));

    let key_part_false = KeyPart::Bool(false);
    let json_false = key_part_to_json(&key_part_false).expect("Failed to convert bool");
    assert_eq!(json_false, serde_json::json!(false));
}

#[test]
fn test_key_part_to_json_int64() {
    let key_part = KeyPart::Int64(42);
    let json = key_part_to_json(&key_part).expect("Failed to convert int64");
    assert_eq!(json, serde_json::json!(42));

    let key_part_negative = KeyPart::Int64(-100);
    let json_negative =
        key_part_to_json(&key_part_negative).expect("Failed to convert negative int64");
    assert_eq!(json_negative, serde_json::json!(-100));
}

#[test]
fn test_basic_value_to_json_bool() {
    let value = BasicValue::Bool(true);
    let json = basic_value_to_json(&value).expect("Failed to convert bool");
    assert_eq!(json, serde_json::json!(true));
}

#[test]
fn test_basic_value_to_json_int64() {
    let value = BasicValue::Int64(9999);
    let json = basic_value_to_json(&value).expect("Failed to convert int64");
    assert_eq!(json, serde_json::json!(9999));
}

#[test]
fn test_basic_value_to_json_float32() {
    let value = BasicValue::Float32(std::f32::consts::PI);
    let json = basic_value_to_json(&value).expect("Failed to convert float32");
    assert!(json.is_number());

    // Test NaN handling
    let nan_value = BasicValue::Float32(f32::NAN);
    let json_nan = basic_value_to_json(&nan_value).expect("Failed to convert NaN");
    assert_eq!(json_nan, serde_json::json!(null));
}

#[test]
fn test_basic_value_to_json_float64() {
    let value = BasicValue::Float64(std::f64::consts::E);
    let json = basic_value_to_json(&value).expect("Failed to convert float64");
    assert!(json.is_number());

    // Test infinity handling
    let inf_value = BasicValue::Float64(f64::INFINITY);
    let json_inf = basic_value_to_json(&inf_value).expect("Failed to convert infinity");
    assert_eq!(json_inf, serde_json::json!(null));
}

#[test]
fn test_basic_value_to_json_str() {
    let value = BasicValue::Str("hello world".into());
    let json = basic_value_to_json(&value).expect("Failed to convert str");
    assert_eq!(json, serde_json::json!("hello world"));
}

#[test]
fn test_value_to_json_null() {
    let value = Value::Null;
    let json = value_to_json(&value).expect("Failed to convert null");
    assert_eq!(json, serde_json::json!(null));
}

#[test]
fn test_value_to_json_basic() {
    let value = Value::Basic(BasicValue::Str("test".into()));
    let json = value_to_json(&value).expect("Failed to convert basic value");
    assert_eq!(json, serde_json::json!("test"));
}

#[test]
fn test_value_to_json_struct() {
    let field_values = FieldValues {
        fields: vec![
            Value::Basic(BasicValue::Str("field1".into())),
            Value::Basic(BasicValue::Int64(42)),
        ],
    };
    let value = Value::Struct(field_values);
    let json = value_to_json(&value).expect("Failed to convert struct");
    assert_eq!(json, serde_json::json!(["field1", 42]));
}

// ============================================================================
// SQL Generation Tests - Core Coverage
// ============================================================================

#[test]
fn test_value_type_to_sql_bool() {
    let typ = ValueType::Basic(BasicValueType::Bool);
    assert_eq!(value_type_to_sql(&typ), "INTEGER");
}

#[test]
fn test_value_type_to_sql_int64() {
    let typ = ValueType::Basic(BasicValueType::Int64);
    assert_eq!(value_type_to_sql(&typ), "INTEGER");
}

#[test]
fn test_value_type_to_sql_float() {
    let typ32 = ValueType::Basic(BasicValueType::Float32);
    assert_eq!(value_type_to_sql(&typ32), "REAL");

    let typ64 = ValueType::Basic(BasicValueType::Float64);
    assert_eq!(value_type_to_sql(&typ64), "REAL");
}

#[test]
fn test_value_type_to_sql_str() {
    let typ = ValueType::Basic(BasicValueType::Str);
    assert_eq!(value_type_to_sql(&typ), "TEXT");
}

#[test]
fn test_value_type_to_sql_json() {
    let typ = ValueType::Basic(BasicValueType::Json);
    assert_eq!(value_type_to_sql(&typ), "TEXT");
}

#[test]
fn test_create_table_sql_simple() {
    let key_fields = vec![test_field_schema("id", BasicValueType::Int64, false)];
    let value_fields = vec![
        test_field_schema("name", BasicValueType::Str, false),
        test_field_schema("age", BasicValueType::Int64, true),
    ];

    let state = D1SetupState::new(&test_table_id(), &key_fields, &value_fields)
        .expect("Failed to create setup state");

    let sql = state.create_table_sql();

    assert!(sql.contains("CREATE TABLE IF NOT EXISTS test_table"));
    assert!(sql.contains("id INTEGER NOT NULL"));
    assert!(sql.contains("name TEXT NOT NULL"));
    assert!(sql.contains("age INTEGER"));
    assert!(!sql.contains("age INTEGER NOT NULL")); // age is nullable
    assert!(sql.contains("PRIMARY KEY (id)"));
}

#[test]
fn test_create_table_sql_composite_key() {
    let key_fields = vec![
        test_field_schema("tenant_id", BasicValueType::Str, false),
        test_field_schema("user_id", BasicValueType::Int64, false),
    ];
    let value_fields = vec![test_field_schema("email", BasicValueType::Str, false)];

    let state = D1SetupState::new(&test_table_id(), &key_fields, &value_fields)
        .expect("Failed to create setup state");

    let sql = state.create_table_sql();

    assert!(sql.contains("tenant_id TEXT NOT NULL"));
    assert!(sql.contains("user_id INTEGER NOT NULL"));
    assert!(sql.contains("PRIMARY KEY (tenant_id, user_id)"));
}

#[test]
fn test_create_indexes_sql_unique() {
    let state = D1SetupState {
        table_id: test_table_id(),
        key_columns: vec![],
        value_columns: vec![],
        indexes: vec![IndexSchema {
            name: "idx_unique_email".to_string(),
            columns: vec!["email".to_string()],
            unique: true,
        }],
    };

    let sqls = state.create_indexes_sql();
    assert_eq!(sqls.len(), 1);
    assert!(sqls[0].contains("CREATE UNIQUE INDEX IF NOT EXISTS idx_unique_email"));
    assert!(sqls[0].contains("ON test_table (email)"));
}

#[test]
fn test_create_indexes_sql_composite() {
    let state = D1SetupState {
        table_id: test_table_id(),
        key_columns: vec![],
        value_columns: vec![],
        indexes: vec![IndexSchema {
            name: "idx_tenant_user".to_string(),
            columns: vec!["tenant_id".to_string(), "user_id".to_string()],
            unique: false,
        }],
    };

    let sqls = state.create_indexes_sql();
    assert_eq!(sqls.len(), 1);
    assert!(sqls[0].contains("ON test_table (tenant_id, user_id)"));
}

// ============================================================================
// Setup State Management Tests
// ============================================================================

#[test]
fn test_d1_setup_state_new() {
    let key_fields = vec![test_field_schema("id", BasicValueType::Int64, false)];
    let value_fields = vec![
        test_field_schema("name", BasicValueType::Str, false),
        test_field_schema("score", BasicValueType::Float64, true),
    ];

    let state = D1SetupState::new(&test_table_id(), &key_fields, &value_fields)
        .expect("Failed to create setup state");

    assert_eq!(state.table_id, test_table_id());
    assert_eq!(state.key_columns.len(), 1);
    assert_eq!(state.key_columns[0].name, "id");
    assert_eq!(state.key_columns[0].sql_type, "INTEGER");
    assert!(state.key_columns[0].primary_key);
    assert!(!state.key_columns[0].nullable);

    assert_eq!(state.value_columns.len(), 2);
    assert_eq!(state.value_columns[0].name, "name");
    assert!(!state.value_columns[0].primary_key);
    assert_eq!(state.value_columns[1].name, "score");
    assert!(state.value_columns[1].nullable);
}

#[test]
fn test_d1_setup_change_describe_changes_create() {
    let change = D1SetupChange {
        table_id: test_table_id(),
        create_table_sql: Some("CREATE TABLE test_table (id INTEGER)".to_string()),
        create_indexes_sql: vec!["CREATE INDEX idx_id ON test_table (id)".to_string()],
        alter_table_sql: vec![],
    };

    let descriptions = change.describe_changes();
    assert_eq!(descriptions.len(), 2);

    // Check that descriptions contain expected SQL
    let desc_strings: Vec<String> = descriptions
        .iter()
        .map(|d| match d {
            recoco::setup::ChangeDescription::Action(s) => s.clone(),
            _ => String::new(),
        })
        .collect();

    assert!(desc_strings.iter().any(|s| s.contains("CREATE TABLE")));
    assert!(desc_strings.iter().any(|s| s.contains("CREATE INDEX")));
}

#[test]
fn test_d1_setup_change_type_create() {
    let change = D1SetupChange {
        table_id: test_table_id(),
        create_table_sql: Some("CREATE TABLE test_table (id INTEGER)".to_string()),
        create_indexes_sql: vec![],
        alter_table_sql: vec![],
    };

    assert_eq!(change.change_type(), SetupChangeType::Create);
}

#[test]
fn test_d1_setup_change_type_update() {
    let change = D1SetupChange {
        table_id: test_table_id(),
        create_table_sql: None,
        create_indexes_sql: vec!["CREATE INDEX idx ON test_table (col)".to_string()],
        alter_table_sql: vec![],
    };

    assert_eq!(change.change_type(), SetupChangeType::Update);
}

#[test]
fn test_d1_setup_change_type_invalid() {
    let change = D1SetupChange {
        table_id: test_table_id(),
        create_table_sql: None,
        create_indexes_sql: vec![],
        alter_table_sql: vec![],
    };

    assert_eq!(change.change_type(), SetupChangeType::Invalid);
}

// ============================================================================
// TargetFactoryBase Implementation Tests
// ============================================================================

#[test]
fn test_factory_name() {
    let factory = D1TargetFactory;
    assert_eq!(factory.name(), "d1");
}

#[test]
fn test_describe_resource() {
    let factory = D1TargetFactory;
    let table_id = D1TableId {
        database_id: "my-database".to_string(),
        table_name: "my_table".to_string(),
    };

    let description = factory
        .describe_resource(&table_id)
        .expect("Failed to describe resource");

    assert_eq!(description, "D1 table: my-database.my_table");
}

// ============================================================================
// D1ExportContext Tests
// ============================================================================

#[test]
fn test_d1_export_context_new() {
    let key_fields = vec![test_field_schema("id", BasicValueType::Int64, false)];
    let value_fields = vec![test_field_schema("name", BasicValueType::Str, false)];

    let metrics = thread_flow::monitoring::performance::PerformanceMetrics::new();
    let context = D1ExportContext::new_with_default_client(
        "test-db".to_string(),
        "test_table".to_string(),
        "test-account".to_string(),
        "test-token".to_string(),
        key_fields.clone(),
        value_fields.clone(),
        metrics,
    );

    assert!(context.is_ok());
    let context = context.unwrap();
    assert_eq!(context.database_id, "test-db");
    assert_eq!(context.table_name, "test_table");
    assert_eq!(context.account_id, "test-account");
    assert_eq!(context.api_token, "test-token");
    assert_eq!(context.key_fields_schema.len(), 1);
    assert_eq!(context.value_fields_schema.len(), 1);
}

#[test]
fn test_d1_export_context_api_url() {
    let key_fields = vec![test_field_schema("id", BasicValueType::Int64, false)];
    let value_fields = vec![test_field_schema("name", BasicValueType::Str, false)];

    let metrics = thread_flow::monitoring::performance::PerformanceMetrics::new();
    let context = D1ExportContext::new_with_default_client(
        "db-123".to_string(),
        "users".to_string(),
        "account-456".to_string(),
        "token-789".to_string(),
        key_fields,
        value_fields,
        metrics,
    )
    .expect("Failed to create context");

    let url = context.api_url();
    assert_eq!(
        url,
        "https://api.cloudflare.com/client/v4/accounts/account-456/d1/database/db-123/query"
    );
}

// ============================================================================
// Edge Cases and Error Handling Tests
// ============================================================================

#[test]
fn test_empty_field_values() {
    let empty_values = FieldValues { fields: vec![] };
    let json = value_to_json(&Value::Struct(empty_values)).expect("Failed to convert empty struct");
    assert_eq!(json, serde_json::json!([]));
}

#[test]
fn test_deeply_nested_struct() {
    let nested = Value::Struct(FieldValues {
        fields: vec![Value::Struct(FieldValues {
            fields: vec![Value::Basic(BasicValue::Str("deeply nested".into()))],
        })],
    });

    let json = value_to_json(&nested).expect("Failed to convert nested struct");
    assert_eq!(json, serde_json::json!([["deeply nested"]]));
}

#[test]
fn test_unicode_string_handling() {
    let unicode_str = "Hello δΈ–η•Œ 🌍 Ω…Ψ±Ψ­Ψ¨Ψ§";
    let value = BasicValue::Str(unicode_str.into());
    let json = basic_value_to_json(&value).expect("Failed to convert unicode string");
    assert_eq!(json, serde_json::json!(unicode_str));
}

#[test]
fn test_empty_table_name() {
    let table_id = D1TableId {
        database_id: "db".to_string(),
        table_name: "".to_string(),
    };

    let factory = D1TargetFactory;
    let description = factory
        .describe_resource(&table_id)
        .expect("Failed to describe");
    assert_eq!(description, "D1 table: db.");
}

// ============================================================================
// Test Coverage Summary
// ============================================================================

#[test]
fn test_minimal_coverage_summary() {
    println!("\n=== D1 Target Minimal Test Coverage Summary ===\n");

    println!("βœ… Value Conversion Functions (API-compatible):");
    println!("   - key_part_to_json: Str, Bool, Int64 tested");
    println!("   - basic_value_to_json: Bool, Int64, Float32, Float64, Str tested");
    println!("   - value_to_json: Null, Basic, Struct tested");

    println!("\nβœ… SQL Generation (No recoco dependencies):");
    println!("   - value_type_to_sql: 5 types tested");
    println!("   - create_table_sql: 2 scenarios tested");
    println!("   - create_indexes_sql: 2 scenarios tested");

    println!("\nβœ… Setup State Management:");
    println!("   - D1SetupState::new: tested");
    println!("   - D1SetupChange methods: 3 types tested");

    println!("\nβœ… TargetFactoryBase Implementation:");
    println!("   - name(): tested");
    println!("   - describe_resource(): tested");

    println!("\nβœ… D1ExportContext:");
    println!("   - Constructor validation: tested");
    println!("   - API URL generation: tested");

    println!("\n⚠️  Not Covered (requires recoco API update):");
    println!("   - Build operation with TypedExportDataCollectionSpec");
    println!("   - diff_setup_states with CombinedState");
    println!("   - check_state_compatibility tests");
    println!("   - build_upsert_stmt / build_delete_stmt (need recoco types)");
    println!("   - Complex value conversions (Bytes, Range, KTable with new types)");

    println!("\nπŸ“Š Estimated Coverage: 35-40% (API-compatible subset)");
    println!("   - Pure functions: ~70% coverage");
    println!("   - SQL generation: ~80% coverage");
    println!("   - recoco-dependent: <10% coverage");

    println!("\nπŸ’‘ To achieve 80%+ coverage:");
    println!("   - Update tests to match recoco API (Bytes, Arc, BTreeMap types)");
    println!("   - Complete build/mutation tests with proper type construction");
    println!("   - Add integration tests with mock D1 API\n");
}