ftui-runtime 0.4.0

Elm-style runtime loop and subscriptions for FrankenTUI.
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
//! Cross-component lifecycle contract tests (bd-1dg21).
//!
//! These integration tests capture the observable behavioral contract of the
//! runtime lifecycle that MUST be preserved during the Asupersync migration.
//! They test through public APIs only (ProgramSimulator, CancellationToken,
//! effect system metrics). Tests requiring internal APIs (StopSignal::new,
//! SubscriptionManager) live in the crate-internal unit test modules.

#![forbid(unsafe_code)]

use ftui_core::event::Event;
use ftui_render::frame::Frame;
use ftui_runtime::program::{Cmd, Model};
use ftui_runtime::simulator::ProgramSimulator;
use std::sync::Arc;
use std::sync::atomic::{AtomicUsize, Ordering};
use std::time::Duration;

// ============================================================================
// Test model: LifecycleTracker
//
// Records the exact sequence of lifecycle events to verify ordering contracts.
// ============================================================================

struct LifecycleTracker {
    trace: Vec<String>,
}

#[derive(Debug)]
enum LMsg {
    Init,
    Tick,
    Quit,
    TaskResult(String),
    SpawnTask,
}

impl From<Event> for LMsg {
    fn from(_: Event) -> Self {
        LMsg::Tick
    }
}

impl Model for LifecycleTracker {
    type Message = LMsg;

    fn init(&mut self) -> Cmd<Self::Message> {
        self.trace.push("init".into());
        Cmd::msg(LMsg::Init)
    }

    fn update(&mut self, msg: Self::Message) -> Cmd<Self::Message> {
        match msg {
            LMsg::Init => {
                self.trace.push("update:init".into());
                Cmd::none()
            }
            LMsg::Tick => {
                self.trace.push("update:tick".into());
                Cmd::none()
            }
            LMsg::Quit => {
                self.trace.push("update:quit".into());
                Cmd::quit()
            }
            LMsg::TaskResult(s) => {
                self.trace.push(format!("update:task-result:{s}"));
                Cmd::none()
            }
            LMsg::SpawnTask => {
                self.trace.push("update:spawn-task".into());
                Cmd::task(|| LMsg::TaskResult("done".into()))
            }
        }
    }

    fn view(&self, _frame: &mut Frame) {}

    fn on_shutdown(&mut self) -> Cmd<Self::Message> {
        self.trace.push("on_shutdown".into());
        Cmd::none()
    }
}

// ============================================================================
// CONTRACT: Full lifecycle trace ordering
// ============================================================================

/// CONTRACT: The lifecycle must follow: init -> update(init_cmd) -> updates
/// -> quit -> on_shutdown. This ordering is critical for the Asupersync
/// migration to preserve.
#[test]
fn contract_lifecycle_ordering_init_update_shutdown() {
    let mut sim = ProgramSimulator::new(LifecycleTracker { trace: vec![] });

    sim.init();
    sim.send(LMsg::SpawnTask);
    sim.send(LMsg::Quit);

    // Manually invoke on_shutdown (simulator doesn't auto-call it;
    // the real runtime does it in the shutdown sequence).
    let _shutdown_cmd = sim.model_mut().on_shutdown();

    let trace = &sim.model().trace;
    assert_eq!(
        trace,
        &[
            "init",
            "update:init",
            "update:spawn-task",
            "update:task-result:done",
            "update:quit",
            "on_shutdown",
        ],
        "lifecycle must follow: init -> update(init_cmd) -> updates -> quit -> on_shutdown"
    );
}

/// CONTRACT: Cmd::Task results are routed through Model::update() synchronously
/// in the simulator, and the ordering is deterministic.
#[test]
fn contract_task_result_ordering_is_deterministic() {
    struct MultiTaskModel {
        trace: Vec<String>,
    }

    #[derive(Debug)]
    enum MTMsg {
        SpawnAll,
        Result(String),
    }

    impl From<Event> for MTMsg {
        fn from(_: Event) -> Self {
            MTMsg::SpawnAll
        }
    }

    impl Model for MultiTaskModel {
        type Message = MTMsg;

        fn update(&mut self, msg: Self::Message) -> Cmd<Self::Message> {
            match msg {
                MTMsg::SpawnAll => {
                    self.trace.push("spawn-all".into());
                    Cmd::batch(vec![
                        Cmd::task(|| MTMsg::Result("task-a".into())),
                        Cmd::task(|| MTMsg::Result("task-b".into())),
                        Cmd::task(|| MTMsg::Result("task-c".into())),
                    ])
                }
                MTMsg::Result(s) => {
                    self.trace.push(format!("result:{s}"));
                    Cmd::none()
                }
            }
        }

        fn view(&self, _frame: &mut Frame) {}
    }

    // Run twice and verify deterministic ordering
    for _ in 0..3 {
        let mut sim = ProgramSimulator::new(MultiTaskModel { trace: vec![] });
        sim.init();
        sim.send(MTMsg::SpawnAll);

        assert_eq!(
            sim.model().trace,
            vec![
                "spawn-all",
                "result:task-a",
                "result:task-b",
                "result:task-c",
            ],
            "task results must be processed in batch submission order"
        );
    }
}

/// CONTRACT: Cmd::Batch stops executing after Cmd::Quit.
#[test]
fn contract_batch_halts_on_quit() {
    struct HaltModel {
        steps: Vec<&'static str>,
    }

    #[derive(Debug)]
    enum HMsg {
        Step(&'static str),
        TriggerBatchWithQuit,
    }

    impl From<Event> for HMsg {
        fn from(_: Event) -> Self {
            HMsg::Step("event")
        }
    }

    impl Model for HaltModel {
        type Message = HMsg;

        fn update(&mut self, msg: Self::Message) -> Cmd<Self::Message> {
            match msg {
                HMsg::Step(s) => {
                    self.steps.push(s);
                    Cmd::none()
                }
                HMsg::TriggerBatchWithQuit => Cmd::batch(vec![
                    Cmd::msg(HMsg::Step("before-quit")),
                    Cmd::quit(),
                    Cmd::msg(HMsg::Step("after-quit")),
                ]),
            }
        }

        fn view(&self, _frame: &mut Frame) {}
    }

    let mut sim = ProgramSimulator::new(HaltModel { steps: vec![] });
    sim.init();
    sim.send(HMsg::TriggerBatchWithQuit);

    assert!(!sim.is_running());
    assert_eq!(
        sim.model().steps,
        vec!["before-quit"],
        "commands after Quit in a Batch must not execute"
    );
}

/// CONTRACT: Cmd::Sequence stops executing after Cmd::Quit.
#[test]
fn contract_sequence_halts_on_quit() {
    struct SeqModel {
        steps: Vec<&'static str>,
    }

    #[derive(Debug)]
    enum SMsg {
        Step(&'static str),
        TriggerSeqWithQuit,
    }

    impl From<Event> for SMsg {
        fn from(_: Event) -> Self {
            SMsg::Step("event")
        }
    }

    impl Model for SeqModel {
        type Message = SMsg;

        fn update(&mut self, msg: Self::Message) -> Cmd<Self::Message> {
            match msg {
                SMsg::Step(s) => {
                    self.steps.push(s);
                    Cmd::none()
                }
                SMsg::TriggerSeqWithQuit => Cmd::sequence(vec![
                    Cmd::msg(SMsg::Step("before-quit")),
                    Cmd::quit(),
                    Cmd::msg(SMsg::Step("after-quit")),
                ]),
            }
        }

        fn view(&self, _frame: &mut Frame) {}
    }

    let mut sim = ProgramSimulator::new(SeqModel { steps: vec![] });
    sim.init();
    sim.send(SMsg::TriggerSeqWithQuit);

    assert!(!sim.is_running());
    assert_eq!(
        sim.model().steps,
        vec!["before-quit"],
        "commands after Quit in a Sequence must not execute"
    );
}

// ============================================================================
// CONTRACT: CancellationToken integration
// ============================================================================

/// CONTRACT: CancellationToken must cooperatively stop background work.
/// cancel() must wake threads blocked in wait_timeout().
#[test]
fn contract_cancellation_token_stops_background_work() {
    use ftui_runtime::cancellation::CancellationSource;

    let source = CancellationSource::new();
    let token = source.token();
    let work_done = Arc::new(AtomicUsize::new(0));
    let work_clone = work_done.clone();

    let handle = std::thread::spawn(move || {
        while !token.is_cancelled() {
            work_clone.fetch_add(1, Ordering::SeqCst);
            std::thread::sleep(Duration::from_millis(10));
        }
    });

    std::thread::sleep(Duration::from_millis(50));
    let before_cancel = work_done.load(Ordering::SeqCst);
    assert!(before_cancel > 0, "work should have started");

    source.cancel();
    handle.join().unwrap();

    let at_cancel = work_done.load(Ordering::SeqCst);
    std::thread::sleep(Duration::from_millis(50));
    let after_wait = work_done.load(Ordering::SeqCst);
    assert_eq!(
        at_cancel, after_wait,
        "no work should happen after cancellation"
    );
}

/// CONTRACT: Dropping CancellationSource does NOT cancel the token.
/// Cancellation must be explicit.
#[test]
fn contract_cancellation_drop_does_not_cancel() {
    use ftui_runtime::cancellation::CancellationSource;

    let source = CancellationSource::new();
    let token = source.token();
    drop(source);
    assert!(
        !token.is_cancelled(),
        "dropping source must not cancel token"
    );
}

// ============================================================================
// CONTRACT: Effect system metrics
// ============================================================================

/// CONTRACT: Effect counters are monotonic and increment by exactly 1 per call.
/// The combined total must always equal command_total + subscription_total.
#[test]
fn contract_effect_metrics_increment_monotonically() {
    let before_cmd = ftui_runtime::effect_system::effects_command_total();
    let before_sub = ftui_runtime::effect_system::effects_subscription_total();

    ftui_runtime::effect_system::record_command_effect("test_cmd", 100);
    ftui_runtime::effect_system::record_command_effect("test_cmd", 200);

    let after_cmd = ftui_runtime::effect_system::effects_command_total();
    assert_eq!(
        after_cmd - before_cmd,
        2,
        "command counter must increment by exactly 1 per call"
    );

    ftui_runtime::effect_system::record_subscription_start("test_sub", 1);

    let after_sub = ftui_runtime::effect_system::effects_subscription_total();
    assert_eq!(
        after_sub - before_sub,
        1,
        "subscription counter must increment by exactly 1 per call"
    );

    let total = ftui_runtime::effect_system::effects_executed_total();
    assert_eq!(
        total,
        after_cmd + after_sub,
        "combined total must equal cmd + sub"
    );
}

/// CONTRACT: No messages are processed after Cmd::Quit.
#[test]
fn contract_no_processing_after_quit() {
    struct CountModel {
        value: i32,
    }

    #[derive(Debug)]
    enum CMsg {
        Inc,
        Quit,
    }

    impl From<Event> for CMsg {
        fn from(_: Event) -> Self {
            CMsg::Inc
        }
    }

    impl Model for CountModel {
        type Message = CMsg;

        fn update(&mut self, msg: Self::Message) -> Cmd<Self::Message> {
            match msg {
                CMsg::Inc => {
                    self.value += 1;
                    Cmd::none()
                }
                CMsg::Quit => Cmd::quit(),
            }
        }

        fn view(&self, _frame: &mut Frame) {}
    }

    let mut sim = ProgramSimulator::new(CountModel { value: 0 });
    sim.init();

    sim.send(CMsg::Inc); // value = 1
    sim.send(CMsg::Quit);
    sim.send(CMsg::Inc); // must be ignored
    sim.send(CMsg::Inc); // must be ignored

    assert_eq!(sim.model().value, 1, "messages after Quit must be ignored");
    assert!(!sim.is_running());
}