telltale-runtime 17.0.0

Choreographic programming for Telltale - effect-based distributed protocols
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
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
#![cfg(not(target_arch = "wasm32"))]
//! Integration tests for handlers, interpreter, and middleware.
//!
//! Verifies:
//! - Complete protocol execution through interpreter
//! - Middleware stacking (Trace, Retry, Metrics)
//! - Error propagation through the stack
//! - Complex programs with all effect types

#![allow(clippy::unwrap_used)]
#![allow(clippy::expect_used)]

use serde::{Deserialize, Serialize};
use std::collections::BTreeMap;
use std::sync::{Arc, Mutex};
use telltale_runtime::effects::{
    algebra::{InterpreterState, Program},
    handlers::in_memory::InMemoryHandler,
    interpreter::{interpret, testing::MockHandler},
    middleware::Trace,
    ChoreoHandler, LabelId, RoleId,
};
use telltale_runtime::RoleName;

// ============================================================================
// Test Role Setup
// ============================================================================

#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
enum TestRole {
    Alice,
    Bob,
    Charlie,
}

#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
enum TestLabel {
    Accept,
    Option1,
    Ok,
}

impl LabelId for TestLabel {
    fn as_str(&self) -> &'static str {
        match self {
            TestLabel::Accept => "Accept",
            TestLabel::Option1 => "Option1",
            TestLabel::Ok => "ok",
        }
    }

    fn from_str(label: &str) -> Option<Self> {
        match label {
            "Accept" => Some(TestLabel::Accept),
            "Option1" => Some(TestLabel::Option1),
            "ok" => Some(TestLabel::Ok),
            _ => None,
        }
    }
}

impl RoleId for TestRole {
    type Label = TestLabel;

    fn role_name(&self) -> RoleName {
        match self {
            TestRole::Alice => RoleName::from_static("Alice"),
            TestRole::Bob => RoleName::from_static("Bob"),
            TestRole::Charlie => RoleName::from_static("Charlie"),
        }
    }
}

// ============================================================================
// Test Message Types
// ============================================================================

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
struct Request {
    id: u32,
    payload: String,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
struct Response {
    id: u32,
    result: i64,
}

// ============================================================================
// Interpreter Basic Tests
// ============================================================================

#[tokio::test]
async fn test_interpreter_simple_send_program() {
    let mut handler = MockHandler::new(TestRole::Alice);
    let mut endpoint = ();

    let program = Program::new()
        .send(
            TestRole::Bob,
            Request {
                id: 1,
                payload: "hello".into(),
            },
        )
        .end();

    let result = interpret(&mut handler, &mut endpoint, program)
        .await
        .unwrap();

    assert_eq!(result.final_state, InterpreterState::Completed);

    let ops = handler.operations();
    assert_eq!(ops.len(), 1);
}

#[tokio::test]
async fn test_interpreter_send_recv_sequence() {
    use telltale_runtime::effects::interpreter::testing::{MockOperation, MockResponse};

    let mut handler = MockHandler::new(TestRole::Alice);

    // Script the response for the recv operation (same type as send for Program consistency)
    let response = Request {
        id: 100,
        payload: "reply".into(),
    };
    handler.add_response(MockResponse::Message(
        bincode::serialize(&response).unwrap(),
    ));

    let mut endpoint = ();

    // Note: Program<R, M> requires consistent message type M
    let program = Program::new()
        .send(
            TestRole::Bob,
            Request {
                id: 1,
                payload: "query".into(),
            },
        )
        .recv::<Request>(TestRole::Bob)
        .end();

    let result = interpret(&mut handler, &mut endpoint, program)
        .await
        .unwrap();

    assert_eq!(result.final_state, InterpreterState::Completed);
    assert_eq!(result.received_values.len(), 1);

    let ops = handler.operations();
    assert_eq!(ops.len(), 2);

    // Verify operation order
    match &ops[0] {
        MockOperation::Send { to, .. } => assert_eq!(*to, TestRole::Bob),
        _ => panic!("Expected Send operation first"),
    }
    match &ops[1] {
        MockOperation::Recv { from } => assert_eq!(*from, TestRole::Bob),
        _ => panic!("Expected Recv operation second"),
    }
}

#[tokio::test]
async fn test_interpreter_choose_offer_sequence() {
    use telltale_runtime::effects::interpreter::testing::{MockOperation, MockResponse};

    let mut handler = MockHandler::new(TestRole::Bob);

    // Script the label for the offer operation
    handler.add_response(MockResponse::Label(TestLabel::Accept));

    let mut endpoint = ();

    // Bob offers and receives Alice's choice
    let program: Program<TestRole, Request> = Program::new().offer(TestRole::Alice).end();

    let result = interpret(&mut handler, &mut endpoint, program)
        .await
        .unwrap();

    assert_eq!(result.final_state, InterpreterState::Completed);

    let ops = handler.operations();
    assert_eq!(ops.len(), 1);

    match &ops[0] {
        MockOperation::Offer { from } => assert_eq!(*from, TestRole::Alice),
        _ => panic!("Expected Offer operation"),
    }
}

// ============================================================================
// Interpreter Error Handling Tests
// ============================================================================

#[tokio::test]
async fn test_interpreter_recv_error_becomes_failed_state() {
    use telltale_runtime::effects::algebra::InterpretResult;

    let mut handler = MockHandler::new(TestRole::Alice);
    // Don't add a response - recv will fail
    let mut endpoint = ();

    let program: Program<TestRole, Request> = Program::new().recv::<Request>(TestRole::Bob).end();

    let result: InterpretResult<Request> = interpret(&mut handler, &mut endpoint, program)
        .await
        .unwrap();

    // Should complete with failed state, not panic
    match result.final_state {
        InterpreterState::Failed(msg) => {
            assert!(msg.contains("No scripted response"));
        }
        _ => panic!("Expected Failed state, got {:?}", result.final_state),
    }
}

#[tokio::test]
async fn test_interpreter_offer_error_becomes_failed_state() {
    use telltale_runtime::effects::algebra::InterpretResult;

    let mut handler = MockHandler::new(TestRole::Bob);
    // Don't add a response - offer will fail
    let mut endpoint = ();

    let program: Program<TestRole, Request> = Program::new().offer(TestRole::Alice).end();

    let result: InterpretResult<Request> = interpret(&mut handler, &mut endpoint, program)
        .await
        .unwrap();

    match result.final_state {
        InterpreterState::Failed(msg) => {
            assert!(msg.contains("No scripted label"));
        }
        _ => panic!("Expected Failed state"),
    }
}

// ============================================================================
// Trace Middleware Tests
// ============================================================================

#[tokio::test]
async fn test_trace_middleware_wraps_handler() {
    let inner = MockHandler::new(TestRole::Alice);
    let mut traced = Trace::new(inner);
    let mut endpoint = ();

    // Send through traced handler
    let msg = Request {
        id: 1,
        payload: "test".into(),
    };
    traced
        .send(&mut endpoint, TestRole::Bob, &msg)
        .await
        .expect("Send should succeed");

    // The inner handler should have recorded the operation
    // (We can't directly access inner here, but the test verifies no panic)
}

#[tokio::test]
async fn test_trace_middleware_with_custom_prefix() {
    let inner = MockHandler::new(TestRole::Alice);
    let mut traced = Trace::with_prefix(inner, "custom-prefix");
    let mut endpoint = ();

    traced
        .send(
            &mut endpoint,
            TestRole::Bob,
            &Request {
                id: 1,
                payload: "x".into(),
            },
        )
        .await
        .unwrap();
}

#[tokio::test]
async fn test_trace_middleware_choose_offer() {
    let inner = MockHandler::new(TestRole::Alice);
    let mut traced = Trace::new(inner);
    let mut endpoint = ();

    // Choose should work through trace
    traced
        .choose(&mut endpoint, TestRole::Bob, TestLabel::Option1)
        .await
        .expect("Choose should succeed");
}

// ============================================================================
// InMemoryHandler Integration Tests
// ============================================================================

#[tokio::test]
async fn test_in_memory_handler_bidirectional_protocol() {
    let channels = Arc::new(Mutex::new(BTreeMap::new()));
    let choice_channels = Arc::new(Mutex::new(BTreeMap::new()));

    let mut alice =
        InMemoryHandler::with_channels(TestRole::Alice, channels.clone(), choice_channels.clone());
    let mut bob =
        InMemoryHandler::with_channels(TestRole::Bob, channels.clone(), choice_channels.clone());

    // Alice sends request
    let request = Request {
        id: 42,
        payload: "hello".into(),
    };
    alice
        .send(&mut (), TestRole::Bob, &request)
        .await
        .expect("Alice send should succeed");

    // Bob receives request
    let received_request: Request = bob
        .recv(&mut (), TestRole::Alice)
        .await
        .expect("Bob recv should succeed");
    assert_eq!(received_request, request);

    // Bob sends response
    let response = Response {
        id: 42,
        result: 100,
    };
    bob.send(&mut (), TestRole::Alice, &response)
        .await
        .expect("Bob send should succeed");

    // Alice receives response
    let received_response: Response = alice
        .recv(&mut (), TestRole::Bob)
        .await
        .expect("Alice recv should succeed");
    assert_eq!(received_response, response);
}

// Tests that InMemoryHandler properly coordinates choose/offer through choice channels.
// The choose() method sends the label through the choice channel, and offer() receives it.
#[tokio::test]
async fn test_in_memory_handler_choice_protocol() {
    let channels = Arc::new(Mutex::new(BTreeMap::new()));
    let choice_channels = Arc::new(Mutex::new(BTreeMap::new()));

    let mut alice =
        InMemoryHandler::with_channels(TestRole::Alice, channels.clone(), choice_channels.clone());
    let mut bob =
        InMemoryHandler::with_channels(TestRole::Bob, channels.clone(), choice_channels.clone());

    // Alice makes a choice
    alice
        .choose(&mut (), TestRole::Bob, TestLabel::Accept)
        .await
        .expect("Alice choose should succeed");

    // Bob receives the choice
    let label = bob
        .offer(&mut (), TestRole::Alice)
        .await
        .expect("Bob offer should succeed");

    assert_eq!(label, TestLabel::Accept);
}

#[tokio::test]
async fn test_in_memory_handler_three_party_protocol() {
    let channels = Arc::new(Mutex::new(BTreeMap::new()));
    let choice_channels = Arc::new(Mutex::new(BTreeMap::new()));

    let mut alice =
        InMemoryHandler::with_channels(TestRole::Alice, channels.clone(), choice_channels.clone());
    let mut bob =
        InMemoryHandler::with_channels(TestRole::Bob, channels.clone(), choice_channels.clone());
    let mut charlie = InMemoryHandler::with_channels(
        TestRole::Charlie,
        channels.clone(),
        choice_channels.clone(),
    );

    // Alice -> Bob
    alice
        .send(
            &mut (),
            TestRole::Bob,
            &Request {
                id: 1,
                payload: "to bob".into(),
            },
        )
        .await
        .unwrap();

    // Alice -> Charlie
    alice
        .send(
            &mut (),
            TestRole::Charlie,
            &Request {
                id: 2,
                payload: "to charlie".into(),
            },
        )
        .await
        .unwrap();

    // Bob receives from Alice
    let bob_msg: Request = bob.recv(&mut (), TestRole::Alice).await.unwrap();
    assert_eq!(bob_msg.id, 1);

    // Charlie receives from Alice
    let charlie_msg: Request = charlie.recv(&mut (), TestRole::Alice).await.unwrap();
    assert_eq!(charlie_msg.id, 2);

    // Bob -> Charlie
    bob.send(&mut (), TestRole::Charlie, &Response { id: 1, result: 10 })
        .await
        .unwrap();

    // Charlie receives from Bob
    let from_bob: Response = charlie.recv(&mut (), TestRole::Bob).await.unwrap();
    assert_eq!(from_bob.result, 10);
}

// ============================================================================
// Traced InMemoryHandler Tests
// ============================================================================

#[tokio::test]
async fn test_traced_in_memory_handler() {
    let channels = Arc::new(Mutex::new(BTreeMap::new()));
    let choice_channels = Arc::new(Mutex::new(BTreeMap::new()));

    let alice_inner =
        InMemoryHandler::with_channels(TestRole::Alice, channels.clone(), choice_channels.clone());
    let bob_inner =
        InMemoryHandler::with_channels(TestRole::Bob, channels.clone(), choice_channels.clone());

    // Wrap handlers with tracing
    let mut alice = Trace::with_prefix(alice_inner, "alice");
    let mut bob = Trace::with_prefix(bob_inner, "bob");

    // Run a simple protocol through traced handlers
    alice
        .send(
            &mut (),
            TestRole::Bob,
            &Request {
                id: 1,
                payload: "test".into(),
            },
        )
        .await
        .unwrap();

    let msg: Request = bob.recv(&mut (), TestRole::Alice).await.unwrap();

    assert_eq!(msg.id, 1);
}

// ============================================================================
// Program Analysis Tests
// ============================================================================

#[test]
fn test_program_send_count() {
    let program: Program<TestRole, Request> = Program::new()
        .send(
            TestRole::Bob,
            Request {
                id: 1,
                payload: "a".into(),
            },
        )
        .send(
            TestRole::Charlie,
            Request {
                id: 2,
                payload: "b".into(),
            },
        )
        .end();

    assert_eq!(program.send_count(), 2);
}

#[test]
fn test_program_recv_count() {
    let program: Program<TestRole, Response> = Program::new()
        .recv::<Response>(TestRole::Bob)
        .recv::<Response>(TestRole::Charlie)
        .recv::<Response>(TestRole::Alice)
        .end();

    assert_eq!(program.recv_count(), 3);
}

#[test]
fn test_program_roles_involved() {
    let program: Program<TestRole, Request> = Program::new()
        .send(
            TestRole::Bob,
            Request {
                id: 1,
                payload: "x".into(),
            },
        )
        .recv::<Request>(TestRole::Charlie)
        .choose(TestRole::Alice, TestLabel::Ok)
        .end();

    let roles = program.roles_involved();
    assert!(roles.contains(&TestRole::Alice));
    assert!(roles.contains(&TestRole::Bob));
    assert!(roles.contains(&TestRole::Charlie));
}

#[test]
fn test_program_has_timeouts() {
    use std::time::Duration;

    let program_no_timeout: Program<TestRole, Request> = Program::new()
        .send(
            TestRole::Bob,
            Request {
                id: 1,
                payload: "x".into(),
            },
        )
        .end();

    assert!(!program_no_timeout.has_timeouts());

    let inner: Program<TestRole, Request> = Program::new()
        .send(
            TestRole::Bob,
            Request {
                id: 1,
                payload: "x".into(),
            },
        )
        .end();

    let program_with_timeout: Program<TestRole, Request> = Program::new()
        .with_timeout(TestRole::Alice, Duration::from_secs(5), inner)
        .end();

    assert!(program_with_timeout.has_timeouts());
}

#[test]
fn test_program_has_parallel() {
    let program_no_parallel: Program<TestRole, Request> = Program::new()
        .send(
            TestRole::Bob,
            Request {
                id: 1,
                payload: "x".into(),
            },
        )
        .end();

    assert!(!program_no_parallel.has_parallel());

    let p1: Program<TestRole, Request> = Program::new()
        .send(
            TestRole::Bob,
            Request {
                id: 1,
                payload: "a".into(),
            },
        )
        .end();
    let p2: Program<TestRole, Request> = Program::new()
        .send(
            TestRole::Charlie,
            Request {
                id: 2,
                payload: "b".into(),
            },
        )
        .end();

    let program_with_parallel: Program<TestRole, Request> =
        Program::new().parallel(vec![p1, p2]).end();

    assert!(program_with_parallel.has_parallel());
}

// ============================================================================
// Complex Protocol Tests
// ============================================================================

#[tokio::test]
async fn test_interpreter_multi_step_protocol() {
    use telltale_runtime::effects::interpreter::testing::MockResponse;

    let mut handler = MockHandler::new(TestRole::Alice);

    // Script responses for each recv (using Request type to match Program<_, Request>)
    handler.add_response(MockResponse::Message(
        bincode::serialize(&Request {
            id: 10,
            payload: "response1".into(),
        })
        .unwrap(),
    ));
    handler.add_response(MockResponse::Message(
        bincode::serialize(&Request {
            id: 20,
            payload: "response2".into(),
        })
        .unwrap(),
    ));

    let mut endpoint = ();

    // Note: Program<R, M> has single message type M, so we use Request for all
    let program: Program<TestRole, Request> = Program::new()
        .send(
            TestRole::Bob,
            Request {
                id: 1,
                payload: "first".into(),
            },
        )
        .recv::<Request>(TestRole::Bob)
        .send(
            TestRole::Bob,
            Request {
                id: 2,
                payload: "second".into(),
            },
        )
        .recv::<Request>(TestRole::Bob)
        .end();

    let result = interpret(&mut handler, &mut endpoint, program)
        .await
        .unwrap();

    assert_eq!(result.final_state, InterpreterState::Completed);
    assert_eq!(result.received_values.len(), 2);
}

// ============================================================================
// Loop Effect Tests
// ============================================================================

#[tokio::test]
async fn test_interpreter_loop_effect() {
    let mut handler = MockHandler::new(TestRole::Alice);
    let mut endpoint = ();

    // Create a loop that sends 3 times
    let body: Program<TestRole, Request> = Program::new()
        .send(
            TestRole::Bob,
            Request {
                id: 0,
                payload: "loop".into(),
            },
        )
        .end();

    let program: Program<TestRole, Request> = Program::new().loop_n(3, body).end();

    let result = interpret(&mut handler, &mut endpoint, program)
        .await
        .unwrap();

    assert_eq!(result.final_state, InterpreterState::Completed);

    // Should have 3 send operations
    let ops = handler.operations();
    assert_eq!(ops.len(), 3);
}

// ============================================================================
// Parallel Effect Tests
// ============================================================================

#[tokio::test]
async fn test_interpreter_parallel_effect() {
    let mut handler = MockHandler::new(TestRole::Alice);
    let mut endpoint = ();

    let p1: Program<TestRole, Request> = Program::new()
        .send(
            TestRole::Bob,
            Request {
                id: 1,
                payload: "to bob".into(),
            },
        )
        .end();
    let p2: Program<TestRole, Request> = Program::new()
        .send(
            TestRole::Charlie,
            Request {
                id: 2,
                payload: "to charlie".into(),
            },
        )
        .end();

    let program = Program::new().parallel(vec![p1, p2]).end();

    let result = interpret(&mut handler, &mut endpoint, program)
        .await
        .unwrap();

    assert_eq!(result.final_state, InterpreterState::Completed);

    // Should have 2 send operations (executed sequentially in current impl)
    let ops = handler.operations();
    assert_eq!(ops.len(), 2);
}

// ============================================================================
// End Effect Tests
// ============================================================================

#[tokio::test]
async fn test_interpreter_empty_program() {
    let mut handler = MockHandler::new(TestRole::Alice);
    let mut endpoint = ();

    let program: Program<TestRole, Request> = Program::new().end();

    let result = interpret(&mut handler, &mut endpoint, program)
        .await
        .unwrap();

    assert_eq!(result.final_state, InterpreterState::Completed);
    assert_eq!(result.received_values.len(), 0);
    assert_eq!(handler.operations().len(), 0);
}