torc 0.20.7

Workflow management system
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
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
mod common;

use common::{
    ServerProcess, create_test_event, create_test_workflow, run_cli_with_json, start_server,
};
use rstest::rstest;
use serde_json::json;

#[rstest]
fn test_events_add_command_json(start_server: &ServerProcess) {
    let config = &start_server.config;

    // Create test workflow
    let workflow = create_test_workflow(config, "test_events_add_workflow");
    let workflow_id = workflow.id.unwrap();

    // Test the CLI create command with JSON output
    let test_data = r#"{"event_type": "test", "message": "Hello World", "level": "info"}"#;
    let args = [
        "events",
        "create",
        &workflow_id.to_string(),
        "--data",
        test_data,
    ];

    let json_output =
        run_cli_with_json(&args, start_server, None).expect("Failed to run events create command");

    assert!(json_output.get("id").is_some());
    assert_eq!(json_output.get("workflow_id").unwrap(), &json!(workflow_id));
    assert!(json_output.get("timestamp").is_some());

    let expected_data: serde_json::Value = serde_json::from_str(test_data).unwrap();
    assert_eq!(json_output.get("data").unwrap(), &expected_data);

    // Verify timestamp is a valid integer (milliseconds since epoch)
    let timestamp = json_output.get("timestamp").unwrap().as_i64().unwrap();
    // Timestamp should be a reasonable value (after year 2020 and before year 2100)
    let min_timestamp: i64 = 1577836800000; // 2020-01-01 00:00:00 UTC
    let max_timestamp: i64 = 4102444800000; // 2100-01-01 00:00:00 UTC
    assert!(
        timestamp > min_timestamp,
        "Timestamp {} should be after year 2020",
        timestamp
    );
    assert!(
        timestamp < max_timestamp,
        "Timestamp {} should be before year 2100",
        timestamp
    );
}

#[rstest]
fn test_events_add_complex_data(start_server: &ServerProcess) {
    let config = &start_server.config;

    let workflow = create_test_workflow(config, "test_complex_data_workflow");
    let workflow_id = workflow.id.unwrap();

    // Test with complex nested JSON data
    let complex_data = r#"{
        "event_type": "job_status_change",
        "job_info": {
            "id": 123,
            "name": "test_job",
            "status": "running"
        },
        "metadata": {
            "timestamp": "2024-01-01T12:00:00Z",
            "source": "job_runner",
            "tags": ["production", "critical"],
            "metrics": {
                "cpu_usage": 75.5,
                "memory_mb": 512
            }
        },
        "changes": [
            {"field": "status", "from": "pending", "to": "running"},
            {"field": "start_time", "from": null, "to": "2024-01-01T12:00:00Z"}
        ]
    }"#;

    let args = [
        "events",
        "create",
        &workflow_id.to_string(),
        "--data",
        complex_data,
    ];

    let json_output = run_cli_with_json(&args, start_server, None)
        .expect("Failed to run events create with complex data");

    assert_eq!(json_output.get("workflow_id").unwrap(), &json!(workflow_id));

    let expected_data: serde_json::Value = serde_json::from_str(complex_data).unwrap();
    assert_eq!(json_output.get("data").unwrap(), &expected_data);
}

#[rstest]
fn test_events_list_command_json(start_server: &ServerProcess) {
    let config = &start_server.config;

    // Create test workflow and events
    let workflow = create_test_workflow(config, "test_events_list_workflow");
    let workflow_id = workflow.id.unwrap();

    let _event1 = create_test_event(
        config,
        workflow_id,
        json!({"type": "start", "message": "Workflow started"}),
    );
    std::thread::sleep(std::time::Duration::from_millis(10));
    let _event2 = create_test_event(
        config,
        workflow_id,
        json!({"type": "progress", "message": "Job completed", "job_id": 123}),
    );

    // Test the CLI list command
    let args = ["events", "list", &workflow_id.to_string(), "--limit", "10"];

    let json_output =
        run_cli_with_json(&args, start_server, None).expect("Failed to run events list command");

    // Verify JSON structure is an object with "events" field
    assert!(
        json_output.is_object(),
        "Events list should return an object"
    );
    assert!(
        json_output.get("events").is_some(),
        "Response should have 'events' field"
    );

    let events_array = json_output.get("events").unwrap().as_array().unwrap();
    assert!(events_array.len() >= 2, "Should have at least 2 events");

    // Verify each event has the expected structure
    for event in events_array {
        assert!(event.get("id").is_some());
        assert!(event.get("workflow_id").is_some());
        assert!(event.get("timestamp").is_some());
        assert!(event.get("data").is_some());
    }

    // Timestamps are now integers (milliseconds since epoch)
    let first_timestamp = events_array[0].get("timestamp").unwrap().as_i64().unwrap();
    let second_timestamp = events_array[1].get("timestamp").unwrap().as_i64().unwrap();
    assert!(
        second_timestamp > first_timestamp,
        "Events should be sorted oldest first"
    );
}

#[rstest]
fn test_events_list_pagination(start_server: &ServerProcess) {
    let config = &start_server.config;

    let workflow = create_test_workflow(config, "test_pagination_workflow");
    let workflow_id = workflow.id.unwrap();

    // Create multiple events
    for i in 0..5 {
        let _event = create_test_event(
            config,
            workflow_id,
            json!({"index": i, "message": format!("Event {}", i)}),
        );
        // Small delay to ensure different timestamps
        std::thread::sleep(std::time::Duration::from_millis(10));
    }

    // Test with limit
    let args = ["events", "list", &workflow_id.to_string(), "--limit", "3"];

    let json_output =
        run_cli_with_json(&args, start_server, None).expect("Failed to run paginated events list");

    let events_array = json_output.get("events").unwrap().as_array().unwrap();
    assert!(events_array.len() <= 3, "Should respect limit parameter");
    assert!(!events_array.is_empty(), "Should have at least one event");

    // Test with offset
    let args_with_offset = [
        "events",
        "list",
        &workflow_id.to_string(),
        "--limit",
        "2",
        "--offset",
        "2",
    ];

    let json_output_offset = run_cli_with_json(&args_with_offset, start_server, None)
        .expect("Failed to run events list with offset");

    let events_with_offset = json_output_offset
        .get("events")
        .unwrap()
        .as_array()
        .unwrap();
    assert!(
        !events_with_offset.is_empty(),
        "Should have events with offset"
    );
}

#[rstest]
fn test_events_list_sorting(start_server: &ServerProcess) {
    let config = &start_server.config;

    let workflow = create_test_workflow(config, "test_sorting_workflow");
    let workflow_id = workflow.id.unwrap();

    // Create events with different data for sorting
    let _event_a = create_test_event(
        config,
        workflow_id,
        json!({"priority": 1, "name": "aaa_event"}),
    );
    std::thread::sleep(std::time::Duration::from_millis(10));
    let _event_b = create_test_event(
        config,
        workflow_id,
        json!({"priority": 2, "name": "bbb_event"}),
    );
    std::thread::sleep(std::time::Duration::from_millis(10));
    let _event_c = create_test_event(
        config,
        workflow_id,
        json!({"priority": 3, "name": "ccc_event"}),
    );

    // Test default sorting (by timestamp, newest first)
    let args_default = ["events", "list", &workflow_id.to_string()];

    let json_output_default = run_cli_with_json(&args_default, start_server, None)
        .expect("Failed to run default sorted events list");

    let events_array_default = json_output_default
        .get("events")
        .unwrap()
        .as_array()
        .unwrap();
    assert!(events_array_default.len() >= 3);

    // Test reverse sorting
    let args_reverse = [
        "events",
        "list",
        &workflow_id.to_string(),
        "--sort-by",
        "timestamp",
        "--reverse-sort",
    ];

    let json_output_reverse = run_cli_with_json(&args_reverse, start_server, None)
        .expect("Failed to run reverse sorted events list");

    let events_array_reverse = json_output_reverse
        .get("events")
        .unwrap()
        .as_array()
        .unwrap();
    assert!(events_array_reverse.len() >= 3);

    // With reverse-sort flag, we should get oldest first
    // Timestamps are now integers (milliseconds since epoch)
    if events_array_reverse.len() >= 2 {
        let first_timestamp = events_array_reverse[0]
            .get("timestamp")
            .unwrap()
            .as_i64()
            .unwrap();
        let second_timestamp = events_array_reverse[1]
            .get("timestamp")
            .unwrap()
            .as_i64()
            .unwrap();
        assert!(
            first_timestamp >= second_timestamp,
            "With reverse-sort, events should be newest first"
        );
    }
}

#[rstest]
fn test_events_get_latest_event_json(start_server: &ServerProcess) {
    let config = &start_server.config;

    // Create test workflow and events
    let workflow = create_test_workflow(config, "test_latest_event_workflow");
    let workflow_id = workflow.id.unwrap();

    // Create multiple events with delays to ensure different timestamps
    let _event1 = create_test_event(
        config,
        workflow_id,
        json!({"type": "first", "message": "First event"}),
    );
    std::thread::sleep(std::time::Duration::from_millis(10));

    let _event2 = create_test_event(
        config,
        workflow_id,
        json!({"type": "second", "message": "Second event"}),
    );
    std::thread::sleep(std::time::Duration::from_millis(10));

    let latest_event = create_test_event(
        config,
        workflow_id,
        json!({"type": "latest", "message": "Latest event", "final": true}),
    );

    // Test the CLI get-latest-event command
    let args = ["events", "get-latest-event", &workflow_id.to_string()];

    let json_output = run_cli_with_json(&args, start_server, None)
        .expect("Failed to run events get-latest-event command");

    // Verify we got the latest event
    assert_eq!(
        json_output.get("id").unwrap(),
        &json!(latest_event.id.unwrap())
    );
    assert_eq!(json_output.get("workflow_id").unwrap(), &json!(workflow_id));
    assert_eq!(
        json_output.get("data").unwrap().get("type").unwrap(),
        &json!("latest")
    );
    assert_eq!(
        json_output.get("data").unwrap().get("final").unwrap(),
        &json!(true)
    );
    assert!(json_output.get("timestamp").is_some());
}

#[rstest]
fn test_events_get_latest_event_empty_workflow(start_server: &ServerProcess) {
    let config = &start_server.config;

    // Create test workflow with no events
    let workflow = create_test_workflow(config, "test_empty_workflow");
    let workflow_id = workflow.id.unwrap();

    // Test the CLI get-latest-event command on empty workflow
    let args = ["events", "get-latest-event", &workflow_id.to_string()];

    let result = run_cli_with_json(&args, start_server, None);
    // This might succeed with empty output or fail - both are acceptable behaviors
    // The command should handle empty workflows gracefully
    if let Ok(json_output) = result {
        // If it succeeds, it should return valid JSON (even if empty/null)
        assert!(json_output.is_null() || json_output.is_object());
    }
    // If it fails, that's also acceptable for an empty workflow
}

#[rstest]
fn test_events_remove_command_json(start_server: &ServerProcess) {
    let config = &start_server.config;

    // Create test data
    let workflow = create_test_workflow(config, "test_events_remove_workflow");
    let workflow_id = workflow.id.unwrap();
    let event = create_test_event(
        config,
        workflow_id,
        json!({"type": "to_remove", "message": "This will be removed"}),
    );
    let event_id = event.id.unwrap();

    // Test the CLI delete command
    let args = ["events", "delete", &event_id.to_string()];

    let json_output =
        run_cli_with_json(&args, start_server, None).expect("Failed to run events delete command");

    // Verify JSON structure shows the removed event
    assert_eq!(json_output.get("id").unwrap(), &json!(event_id));
    assert_eq!(json_output.get("workflow_id").unwrap(), &json!(workflow_id));
    assert_eq!(
        json_output.get("data").unwrap().get("type").unwrap(),
        &json!("to_remove")
    );

    // Verify the event is actually removed by trying to get it via list
    let list_args = ["events", "list", &workflow_id.to_string()];

    let list_output = run_cli_with_json(&list_args, start_server, None)
        .expect("Failed to list events after removal");

    let events_array = list_output.get("events").unwrap().as_array().unwrap();
    let removed_event_exists = events_array
        .iter()
        .any(|event| event.get("id").unwrap() == &json!(event_id));

    assert!(
        !removed_event_exists,
        "Removed event should not appear in list"
    );
}

#[rstest]
fn test_events_various_data_types(start_server: &ServerProcess) {
    let config = &start_server.config;

    let workflow = create_test_workflow(config, "test_data_types_workflow");
    let workflow_id = workflow.id.unwrap();

    // Test with different JSON data types
    let test_cases = vec![
        ("string_data", json!("simple string")),
        ("number_data", json!(42)),
        ("boolean_data", json!(true)),
        ("null_data", json!(null)),
        ("array_data", json!([1, 2, 3, "four"])),
        (
            "object_data",
            json!({"key": "value", "nested": {"deep": true}}),
        ),
        (
            "mixed_array",
            json!([{"id": 1}, {"id": 2}, "mixed", 123, null]),
        ),
    ];

    for (test_name, test_data) in test_cases {
        let data_str = serde_json::to_string(&test_data).unwrap();
        let args = [
            "events",
            "create",
            &workflow_id.to_string(),
            "--data",
            &data_str,
        ];

        let json_output = run_cli_with_json(&args, start_server, None)
            .unwrap_or_else(|_| panic!("Failed to create event with {}", test_name));

        assert_eq!(json_output.get("data").unwrap(), &test_data);
        assert_eq!(json_output.get("workflow_id").unwrap(), &json!(workflow_id));
    }
}

#[rstest]
fn test_events_timestamp_ordering(start_server: &ServerProcess) {
    let config = &start_server.config;

    let workflow = create_test_workflow(config, "test_timestamp_workflow");
    let workflow_id = workflow.id.unwrap();

    // Create events with deliberate delays to test timestamp ordering
    let mut event_data = Vec::new();
    for i in 0..3 {
        let event = create_test_event(
            config,
            workflow_id,
            json!({"sequence": i, "message": format!("Event {}", i)}),
        );
        event_data.push(event);
        std::thread::sleep(std::time::Duration::from_millis(50)); // Ensure different timestamps
    }

    // List events (should be newest first by default)
    let args = ["events", "list", &workflow_id.to_string()];

    let json_output = run_cli_with_json(&args, start_server, None)
        .expect("Failed to list events for timestamp test");

    let events_array = json_output.get("events").unwrap().as_array().unwrap();
    assert!(events_array.len() >= 3);

    // Verify events are in correct order (oldest first)
    // Timestamps are now integers (milliseconds since epoch)
    let mut previous_timestamp: Option<i64> = None;
    for event in events_array {
        let current_timestamp = event.get("timestamp").unwrap().as_i64().unwrap();
        if let Some(prev) = previous_timestamp {
            assert!(
                prev < current_timestamp,
                "Events should be ordered oldest first"
            );
        }
        previous_timestamp = Some(current_timestamp);
    }
}

#[rstest]
fn test_events_large_data_handling(start_server: &ServerProcess) {
    let config = &start_server.config;

    let workflow = create_test_workflow(config, "test_large_data_workflow");
    let workflow_id = workflow.id.unwrap();

    // Create event with large JSON data
    let mut large_array = Vec::new();
    for i in 0..100 {
        large_array.push(json!({
            "id": i,
            "name": format!("item_{}", i),
            "data": format!("This is item number {} with some additional text to make it larger", i),
            "metadata": {
                "created": "2024-01-01T12:00:00Z",
                "tags": ["tag1", "tag2", "tag3"],
                "values": [i, i*2, i*3]
            }
        }));
    }

    let large_data = json!({
        "type": "bulk_data",
        "count": large_array.len(),
        "items": large_array
    });

    let data_str = serde_json::to_string(&large_data).unwrap();
    let args = [
        "events",
        "create",
        &workflow_id.to_string(),
        "--data",
        &data_str,
    ];

    let json_output = run_cli_with_json(&args, start_server, None)
        .expect("Failed to create event with large data");

    assert_eq!(json_output.get("data").unwrap(), &large_data);
    assert_eq!(json_output.get("workflow_id").unwrap(), &json!(workflow_id));

    // Verify we can retrieve it
    let event_id = json_output.get("id").unwrap().as_i64().unwrap();

    // List and find our event
    let list_args = ["events", "list", &workflow_id.to_string(), "--limit", "10"];

    let list_output = run_cli_with_json(&list_args, start_server, None)
        .expect("Failed to list events with large data");

    let events_array = list_output.get("events").unwrap().as_array().unwrap();
    let found_event = events_array
        .iter()
        .find(|event| event.get("id").unwrap() == &json!(event_id));

    assert!(
        found_event.is_some(),
        "Should find the large data event in list"
    );
    let found = found_event.unwrap();
    assert_eq!(
        found.get("data").unwrap().get("count").unwrap(),
        &json!(100)
    );
}

#[rstest]
fn test_events_error_handling(start_server: &ServerProcess) {
    let config = &start_server.config;
    let workflow = create_test_workflow(config, "test_error_workflow");
    let workflow_id = workflow.id.unwrap();

    // Test with invalid JSON
    let invalid_json = r#"{"key": "value", "incomplete": }"#;
    let args_invalid = [
        "events",
        "create",
        &workflow_id.to_string(),
        "--data",
        invalid_json,
    ];

    let result = run_cli_with_json(&args_invalid, start_server, None);
    assert!(result.is_err(), "Should fail with invalid JSON data");

    // Test removing non-existent event
    let args_remove = ["events", "delete", "999999"];

    let result = run_cli_with_json(&args_remove, start_server, None);
    assert!(
        result.is_err(),
        "Should fail when removing non-existent event"
    );

    // Test listing events for non-existent workflow
    let args_list = ["events", "list", "999999"];

    let result = run_cli_with_json(&args_list, start_server, None);
    // This might succeed with empty results or fail - both are acceptable
    if let Ok(json_output) = result {
        let events_array = json_output.get("events").unwrap().as_array().unwrap();
        assert!(
            events_array.is_empty(),
            "Should return empty array for non-existent workflow"
        );
    }
}

#[rstest]
fn test_events_unicode_and_special_characters(start_server: &ServerProcess) {
    let config = &start_server.config;

    let workflow = create_test_workflow(config, "test_unicode_workflow");
    let workflow_id = workflow.id.unwrap();

    // Test with Unicode and special characters
    let unicode_data = json!({
        "message": "Hello 世界 🌍",
        "symbols": "!@#$%^&*()_+-={}[]|\\:;\"'<>?,./ ",
        "unicode_text": "Ñoël 北京 москва العربية हिंदी 日本語",
        "emoji": "🚀 ⭐ 🎯 📊 💻 🔥 ✅ ❌ 🔔 📱",
        "newlines": "Line 1\nLine 2\nLine 3",
        "tabs": "Column1\tColumn2\tColumn3"
    });

    let data_str = serde_json::to_string(&unicode_data).unwrap();
    let args = [
        "events",
        "create",
        &workflow_id.to_string(),
        "--data",
        &data_str,
    ];

    let json_output = run_cli_with_json(&args, start_server, None)
        .expect("Failed to create event with Unicode data");

    assert_eq!(json_output.get("data").unwrap(), &unicode_data);

    // Verify Unicode characters are preserved
    assert_eq!(
        json_output.get("data").unwrap().get("message").unwrap(),
        &json!("Hello 世界 🌍")
    );
    assert_eq!(
        json_output.get("data").unwrap().get("emoji").unwrap(),
        &json!("🚀 ⭐ 🎯 📊 💻 🔥 ✅ ❌ 🔔 📱")
    );
}

#[rstest]
fn test_events_concurrent_additions(start_server: &ServerProcess) {
    let config = &start_server.config;

    let workflow = create_test_workflow(config, "test_concurrent_workflow");
    let workflow_id = workflow.id.unwrap();

    // Create multiple events in quick succession
    let mut event_ids = Vec::new();
    for i in 0..5 {
        let event_data = json!({
            "batch": "concurrent_test",
            "index": i,
            "timestamp": chrono::Utc::now().timestamp_millis()
        });

        let data_str = serde_json::to_string(&event_data).unwrap();
        let args = [
            "events",
            "create",
            &workflow_id.to_string(),
            "--data",
            &data_str,
        ];

        let json_output = run_cli_with_json(&args, start_server, None)
            .unwrap_or_else(|_| panic!("Failed to create concurrent event {}", i));

        event_ids.push(json_output.get("id").unwrap().as_i64().unwrap());
    }

    // Verify all events were created
    assert_eq!(event_ids.len(), 5);

    // Verify all IDs are unique
    let mut sorted_ids = event_ids.clone();
    sorted_ids.sort();
    sorted_ids.dedup();
    assert_eq!(
        sorted_ids.len(),
        event_ids.len(),
        "All event IDs should be unique"
    );

    // List and verify all events are present
    let list_args = ["events", "list", &workflow_id.to_string(), "--limit", "10"];

    let list_output = run_cli_with_json(&list_args, start_server, None)
        .expect("Failed to list concurrent events");

    let events_array = list_output.get("events").unwrap().as_array().unwrap();
    let batch_events: Vec<_> = events_array
        .iter()
        .filter(|event| {
            event
                .get("data")
                .and_then(|data| data.get("batch"))
                .map(|batch| batch == "concurrent_test")
                .unwrap_or(false)
        })
        .collect();

    assert_eq!(batch_events.len(), 5, "Should find all concurrent events");
}

#[rstest]
fn test_events_list_with_category_filter(start_server: &ServerProcess) {
    let config = &start_server.config;

    // Create test workflow
    let workflow = create_test_workflow(config, "test_category_filter_workflow");
    let workflow_id = workflow.id.unwrap();

    // Create events with different categories in their data
    let _event1 = create_test_event(
        config,
        workflow_id,
        json!({"category": "system", "message": "System event"}),
    );
    let _event2 = create_test_event(
        config,
        workflow_id,
        json!({"category": "user", "message": "User event"}),
    );
    let _event3 = create_test_event(
        config,
        workflow_id,
        json!({"category": "system", "message": "Another system event"}),
    );

    // Test the CLI list command with category filter
    let args = [
        "events",
        "list",
        &workflow_id.to_string(),
        "--category",
        "system",
    ];

    // This test mainly verifies that the CLI accepts the new parameter without errors
    // The actual filtering behavior depends on the backend implementation
    let json_output = run_cli_with_json(&args, start_server, None)
        .expect("Failed to run events list command with category filter");

    // Verify the response structure is correct
    assert!(
        json_output.is_object(),
        "Events list should return an object"
    );
    assert!(
        json_output.get("events").is_some(),
        "Response should have 'events' field"
    );

    // The command should execute without error
    // The actual filtering depends on how the backend implements category matching
}