rattery 0.3.2

rattery: a sandboxed terminal host that runs ratatui apps delivered over HTTP, as a CLI and as a library
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
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
//! End-to-end: build the example guests and server, run them headless.
//!
//! Needs the `wasm32-wasip2` target; skips with a message if it is missing.

use std::io::{BufRead, BufReader};
use std::path::{Path, PathBuf};
use std::process::{Child, Command, Stdio};
use std::sync::{Mutex, OnceLock};
use std::time::Duration;

use rattery::{
    App, AppStatus, CookiePolicy, HeadlessOptions, LogLevel, Phase, ReloadPolicy, Report, Script,
    StoragePolicy,
};

fn workspace_root() -> PathBuf {
    Path::new(env!("CARGO_MANIFEST_DIR"))
        .join("../..")
        .canonicalize()
        .unwrap()
}

fn cargo() -> Command {
    let mut cmd = Command::new(std::env::var("CARGO").unwrap_or_else(|_| "cargo".into()));
    cmd.current_dir(workspace_root());
    cmd
}

fn wasip2_available() -> bool {
    let out = Command::new(std::env::var("RUSTC").unwrap_or_else(|_| "rustc".into()))
        .args(["--print", "target-libdir", "--target", "wasm32-wasip2"])
        .output();
    match out {
        Ok(out) if out.status.success() => {
            let dir = PathBuf::from(String::from_utf8_lossy(&out.stdout).trim());
            dir.read_dir()
                .map(|mut d| d.next().is_some())
                .unwrap_or(false)
        }
        _ => false,
    }
}

/// Builds happen once per process; cargo serialises concurrent ones anyway.
fn build(args: &[&str]) {
    static LOCK: Mutex<()> = Mutex::new(());
    let _guard = LOCK.lock().unwrap();
    let status = cargo().args(args).status().expect("failed to run cargo");
    assert!(status.success(), "cargo {} failed", args.join(" "));
}

fn guest(package: &str) -> PathBuf {
    static BUILT: OnceLock<()> = OnceLock::new();
    BUILT.get_or_init(|| {
        build(&[
            "build",
            "-p",
            "counter-app",
            "-p",
            "spin-app",
            "-p",
            "bench-app",
            "--target",
            "wasm32-wasip2",
        ])
    });
    workspace_root().join(format!("target/wasm32-wasip2/debug/{package}.wasm"))
}

struct Server {
    child: Child,
    url: String,
}

impl Server {
    fn start(extra_args: &[&str]) -> Self {
        Self::start_with(&guest("counter-app"), extra_args)
    }

    fn start_serving(app: &Path) -> Self {
        Self::start_with(app, &[])
    }

    fn start_with(app: &Path, extra_args: &[&str]) -> Self {
        static BUILT: OnceLock<()> = OnceLock::new();
        BUILT.get_or_init(|| build(&["build", "-p", "counter-server"]));
        let _ = guest("counter-app");
        let mut child = Command::new(workspace_root().join("target/debug/counter-server"))
            .arg("--bind")
            .arg("127.0.0.1:0")
            .arg("--app")
            .arg(app)
            .args(extra_args)
            .stdout(Stdio::piped())
            .stderr(Stdio::inherit())
            .spawn()
            .expect("failed to start counter-server");
        let stdout = child.stdout.take().unwrap();
        let mut lines = BufReader::new(stdout).lines();
        let mut url = None;
        for line in lines.by_ref() {
            let line = line.unwrap();
            if let Some(rest) = line.strip_prefix("counter-server listening on ") {
                url = Some(rest.trim().to_owned());
                break;
            }
        }
        // Keep draining stdout: dropping the pipe would make the server's next
        // println! fail with a broken pipe and kill it.
        std::thread::spawn(move || for _ in lines.by_ref() {});
        Self {
            child,
            url: url.expect("server did not report its address"),
        }
    }
}

impl Drop for Server {
    fn drop(&mut self) {
        let _ = self.child.kill();
        let _ = self.child.wait();
    }
}

/// The big number in the counter box: the first box segment that is only digits.
fn count_on(screen: &rattery::Screen) -> Option<String> {
    screen
        .lines
        .iter()
        .flat_map(|l| l.split('│'))
        .map(str::trim)
        .find(|seg| !seg.is_empty() && seg.chars().all(|c| c.is_ascii_digit() || c == '-'))
        .map(str::to_owned)
}

fn headless(script: &str, timeout_secs: u64) -> HeadlessOptions {
    HeadlessOptions {
        width: 80,
        height: 24,
        script: Script::parse(script).unwrap(),
        timeout: Some(Duration::from_secs(timeout_secs)),
    }
}

fn dump(report: &Report) -> String {
    let mut out = format!("status: {:?}\nstderr: {}\n", report.status, report.stderr);
    for (i, s) in report.snapshots.iter().enumerate() {
        out.push_str(&format!("--- snapshot {} ---\n{s}", i + 1));
    }
    if let Some(s) = &report.final_screen {
        out.push_str(&format!("--- final ---\n{s}"));
    }
    out
}

macro_rules! require_wasip2 {
    () => {
        if !wasip2_available() {
            eprintln!(
                "skipping: wasm32-wasip2 target not installed (rustup target add wasm32-wasip2)"
            );
            return;
        }
    };
}

#[tokio::test(flavor = "multi_thread")]
async fn counter_round_trip_and_state_on_server() {
    require_wasip2!();
    let server = Server::start(&[]);
    let jar = std::env::temp_dir().join(format!("rattery-e2e-jar-{}.json", std::process::id()));

    let report = App::from_url(format!("{}/app.wasm", server.url))
        .unwrap()
        .cookies(CookiePolicy::File(jar.clone()))
        .headless(headless(
            "sleep 2500\nkey k\nsleep 700\nkey k\nkey w\nkey u\nsleep 1000\nsnapshot\nkey q",
            20,
        ))
        .run()
        .await
        .unwrap();
    let text = dump(&report);
    assert_eq!(report.status, AppStatus::Exited(0), "{text}");
    let snap = &report.snapshots[0];
    assert!(snap.contains("served from http://127.0.0.1:"), "{text}");
    assert_eq!(count_on(snap).as_deref(), Some("2"), "{text}");
    assert!(snap.contains("3 calls"), "{text}");
    assert!(snap.contains("session "), "{text}");
    // The streaming server function has been pushing a line every 500ms.
    let ticks = snap.lines.iter().filter(|l| l.contains("tick ")).count();
    assert!(
        ticks >= 3,
        "expected streamed feed lines, got {ticks}\n{text}"
    );
    assert!(
        snap.contains("count 2  up"),
        "feed should reflect the increments\n{text}"
    );
    assert!(
        snap.contains("ws: pong 1"),
        "websocket reply should arrive\n{text}"
    );
    assert!(
        snap.contains("upload: note: count was") && snap.contains("upload: report: report.txt 24B"),
        "multipart upload should round-trip\n{text}"
    );

    // A second run with the same cookie jar is the same session: the count
    // it sees is the one the first run left behind.
    let report = App::from_url(format!("{}/app.wasm", server.url))
        .unwrap()
        .cookies(CookiePolicy::File(jar.clone()))
        .headless(headless("sleep 2500\nsnapshot\nkey q", 20))
        .run()
        .await
        .unwrap();
    let text = dump(&report);
    assert_eq!(report.status, AppStatus::Exited(0), "{text}");
    assert_eq!(
        count_on(&report.snapshots[0]).as_deref(),
        Some("2"),
        "{text}"
    );

    // A private-window run gets a fresh session and starts from zero.
    let report = App::from_url(format!("{}/app.wasm", server.url))
        .unwrap()
        .cookies(CookiePolicy::Ephemeral)
        .headless(headless("sleep 2500\nsnapshot\nkey q", 20))
        .run()
        .await
        .unwrap();
    let text = dump(&report);
    assert_eq!(
        count_on(&report.snapshots[0]).as_deref(),
        Some("0"),
        "{text}"
    );
    let _ = std::fs::remove_file(&jar);
}

#[tokio::test(flavor = "multi_thread")]
async fn file_without_origin_fails_cleanly() {
    require_wasip2!();
    let report = App::from_path(guest("counter-app"))
        .headless(headless("sleep 2500\nsnapshot\nkey q", 20))
        .run()
        .await
        .unwrap();
    let text = dump(&report);
    assert_eq!(report.status, AppStatus::Exited(0), "{text}");
    assert!(report.snapshots[0].contains("loaded from a file"), "{text}");
    assert!(report.snapshots[0].contains("error"), "{text}");
}

#[tokio::test(flavor = "multi_thread")]
async fn cross_origin_is_denied_unless_allow_listed() {
    require_wasip2!();
    let host = Server::start(&[]);
    let api = Server::start(&[]);

    // The app comes from `host` but is pointed at `api`: cross-origin, denied.
    let denied = std::sync::Arc::new(std::sync::Mutex::new(Vec::new()));
    let report = App::from_url(format!("{}/app.wasm", host.url))
        .unwrap()
        .origin(&api.url)
        .cookies(CookiePolicy::Ephemeral)
        .on_phase({
            let denied = denied.clone();
            move |phase| {
                if let rattery::Phase::RequestDenied { url, reason } = phase {
                    denied.lock().unwrap().push(format!("{url}: {reason}"));
                }
            }
        })
        .headless(headless("sleep 2500\nsnapshot\nkey q", 20))
        .run()
        .await
        .unwrap();
    let text = dump(&report);
    assert_eq!(report.status, AppStatus::Exited(0), "{text}");
    assert!(report.snapshots[0].contains("error"), "{text}");
    assert!(
        !denied.lock().unwrap().is_empty(),
        "the embedder is told about denials"
    );

    // Explicitly allowing the origin works.
    let report = App::from_url(format!("{}/app.wasm", host.url))
        .unwrap()
        .origin(&api.url)
        .allow_origin(&api.url)
        .cookies(CookiePolicy::Ephemeral)
        .headless(headless(
            "sleep 2500\nkey k\nsleep 700\nsnapshot\nkey q",
            20,
        ))
        .run()
        .await
        .unwrap();
    let text = dump(&report);
    assert!(!report.snapshots[0].contains("error"), "{text}");
    assert!(report.snapshots[0].contains("2 calls"), "{text}");
}

/// A policy that refuses one route and stamps every other request.
struct RoutePolicy;

impl rattery::RequestPolicy for RoutePolicy {
    fn on_request<'a>(
        &'a self,
        request: &'a mut http::request::Parts,
        info: &'a rattery::RequestInfo,
    ) -> futures::future::BoxFuture<'a, Result<rattery::PolicyGuard, rattery::PolicyError>> {
        Box::pin(async move {
            assert!(!info.cross_origin);
            if request.uri.path().contains("adjust_count") {
                return Err(rattery::PolicyError::new("adjusting is not allowed here"));
            }
            request
                .headers
                .insert("x-shim", http::HeaderValue::from_static("1"));
            Ok(None)
        })
    }
}

#[tokio::test(flavor = "multi_thread")]
async fn request_policy_can_refuse_routes() {
    require_wasip2!();
    let server = Server::start(&[]);
    let denied = std::sync::Arc::new(std::sync::Mutex::new(Vec::new()));
    let report = App::from_url(format!("{}/app.wasm", server.url))
        .unwrap()
        .cookies(CookiePolicy::Ephemeral)
        .request_policy(std::sync::Arc::new(RoutePolicy))
        .on_phase({
            let denied = denied.clone();
            move |phase| {
                if let rattery::Phase::RequestDenied { reason, .. } = phase {
                    denied.lock().unwrap().push(reason);
                }
            }
        })
        .headless(headless(
            "sleep 2500\nkey k\nsleep 700\nsnapshot\nkey q",
            20,
        ))
        .run()
        .await
        .unwrap();
    let text = dump(&report);
    assert_eq!(report.status, AppStatus::Exited(0), "{text}");
    // The first snapshot succeeded (policy allowed it), the increment did not.
    assert_eq!(
        count_on(&report.snapshots[0]).as_deref(),
        Some("0"),
        "{text}"
    );
    assert!(report.snapshots[0].contains("error"), "{text}");
    assert_eq!(
        denied.lock().unwrap().as_slice(),
        ["adjusting is not allowed here"]
    );
}

#[tokio::test(flavor = "multi_thread")]
async fn escape_sequences_from_the_guest_are_contained() {
    require_wasip2!();
    let report = App::from_path(guest("bench-app"))
        .location("bench://local/app.wasm?mode=evil")
        .cookies(CookiePolicy::Ephemeral)
        .headless(headless("sleep 1500\nsnapshot", 10))
        .run()
        .await
        .unwrap();
    let text = dump(&report);
    assert_eq!(report.status, AppStatus::Exited(1), "{text}");
    // The app exits at once, so the screen it left behind is the evidence.
    let screen = report.final_screen.as_ref().unwrap();
    for (y, expected) in ["\u{FFFD}", "\u{FFFD}", "\u{FFFD}", "\u{FFFD}", "é"]
        .iter()
        .enumerate()
    {
        let row = &screen.lines[y];
        assert!(row.starts_with(expected), "row {y} was {row:?}\n{text}");
        assert!(
            !row.contains('\u{1b}') && !row.contains('\u{7}') && !row.contains('\u{9b}'),
            "{row:?}"
        );
    }
    assert_eq!(
        report.stats.cells_rejected, 5,
        "four bad symbols and one off-screen cell\n{text}"
    );
    // Raw output still carries the bytes; the sanitiser is for printing.
    assert!(report.stdout.contains('\u{1b}'));
    assert!(!rattery::sanitize::text(&report.stdout).contains('\u{1b}'));
    assert!(
        rattery::sanitize::text(&report.stderr).contains("\\u{1b}[31mred"),
        "{text}"
    );
}

#[tokio::test(flavor = "multi_thread")]
async fn cpu_budget_stops_a_spinning_app() {
    require_wasip2!();
    let report = App::from_path(guest("spin-app"))
        .cookies(CookiePolicy::Ephemeral)
        .limits(rattery::Limits {
            cpu_time: Some(Duration::from_millis(500)),
            ..Default::default()
        })
        .headless(headless("", 10))
        .run()
        .await
        .unwrap();
    assert!(
        matches!(report.status, AppStatus::LimitExceeded(ref what) if what.contains("CPU")),
        "{}",
        dump(&report)
    );
    assert!(
        report.timings.total < Duration::from_secs(5),
        "{:?}",
        report.timings
    );
}

#[tokio::test(flavor = "multi_thread")]
async fn memory_limit_is_enforced() {
    require_wasip2!();
    let result = App::from_path(guest("counter-app"))
        .cookies(CookiePolicy::Ephemeral)
        .limits(rattery::Limits {
            memory_bytes: 1 << 16,
            ..Default::default()
        })
        .headless(headless("sleep 500", 5))
        .run()
        .await;
    match result {
        Ok(report) => assert!(
            !matches!(report.status, AppStatus::Exited(0) | AppStatus::TimedOut),
            "{}",
            dump(&report)
        ),
        Err(err) => {
            let text = format!("{err:#}");
            assert!(
                text.to_lowercase().contains("memory") || text.contains("limit"),
                "{text}"
            );
        }
    }
}

#[tokio::test(flavor = "multi_thread")]
async fn watch_reloads_when_the_served_component_changes() {
    require_wasip2!();
    let dir = std::env::temp_dir().join(format!("rattery-watch-{}", std::process::id()));
    std::fs::create_dir_all(&dir).unwrap();
    let served = dir.join("app.wasm");
    std::fs::copy(guest("counter-app"), &served).unwrap();
    let server = Server::start_serving(&served);

    // The swap below is on a wall-clock timer, so make sure neither component
    // needs a cold compile inside the timed window: run each once (cheap when
    // wasmtime's cache is warm, a few seconds when it is not).
    for package in ["counter-app", "spin-app"] {
        App::from_path(guest(package))
            .cookies(CookiePolicy::Ephemeral)
            .headless(headless("", 1))
            .run()
            .await
            .unwrap();
    }

    // Deferred policy: the app hears about the update and gets three seconds
    // to act on it; the counter app only shows a banner, so the host reloads
    // it when the deadline passes.
    let phases = std::sync::Arc::new(Mutex::new(Vec::new()));
    let seen = phases.clone();
    let run = tokio::spawn(
        App::from_url(format!("{}/app.wasm", server.url))
            .unwrap()
            .watch(true)
            .reload_policy(ReloadPolicy::Deferred {
                grace: Duration::from_secs(3),
            })
            .on_phase(move |phase| {
                if matches!(phase, Phase::UpdateAvailable { .. } | Phase::Reloading) {
                    seen.lock().unwrap().push(phase);
                }
            })
            .cookies(CookiePolicy::Ephemeral)
            .headless(headless("sleep 2500\nsnapshot\nsleep 4000\nsnapshot", 16))
            .run(),
    );
    tokio::time::sleep(Duration::from_secs(4)).await;
    // Publish a different app; the running one is told, then replaced.
    std::fs::copy(guest("spin-app"), &served).unwrap();

    let report = run.await.unwrap().unwrap();
    let text = dump(&report);
    assert_eq!(report.status, AppStatus::TimedOut, "{text}");
    assert!(report.snapshots[0].contains("rattery counter"), "{text}");
    let banner = &report.snapshots[1];
    assert!(
        banner.contains("rattery counter") && banner.contains("available: R reloads, forced in"),
        "the app should still be running with the banner up\n{text}"
    );
    let phases = phases.lock().unwrap();
    assert!(
        matches!(phases.first(), Some(Phase::UpdateAvailable { version: Some(v) }) if !v.is_empty()),
        "{phases:?}"
    );
    assert!(phases.contains(&Phase::Reloading), "{phases:?}");
    assert!(
        report
            .final_screen
            .as_ref()
            .unwrap()
            .contains("spinning forever"),
        "{text}"
    );
    let _ = std::fs::remove_dir_all(&dir);
}

#[tokio::test(flavor = "multi_thread")]
async fn location_carries_query_parameters() {
    require_wasip2!();
    let server = Server::start(&[]);
    let report = App::from_url(format!("{}/app.wasm?title=Hello+from+the+URL", server.url))
        .unwrap()
        .cookies(CookiePolicy::Ephemeral)
        .headless(headless("sleep 2500\nsnapshot\nkey q", 20))
        .run()
        .await
        .unwrap();
    let text = dump(&report);
    assert!(report.snapshots[0].contains("Hello+from+the+URL"), "{text}");
}

#[tokio::test(flavor = "multi_thread")]
async fn kill_switch_interrupts_a_spinning_app() {
    require_wasip2!();
    let report = App::from_path(guest("spin-app"))
        .headless(headless(
            "sleep 800\nkey ctrl-c\nkey ctrl-c\nkey ctrl-c",
            30,
        ))
        .run()
        .await
        .unwrap();
    let text = dump(&report);
    assert_eq!(report.status, AppStatus::Killed, "{text}");
    assert!(
        report.final_screen.unwrap().contains("spinning forever"),
        "{text}"
    );
}

#[tokio::test(flavor = "multi_thread")]
async fn headless_timeout_stops_a_spinning_app() {
    require_wasip2!();
    let report = App::from_path(guest("spin-app"))
        .headless(headless("", 2))
        .run()
        .await
        .unwrap();
    assert_eq!(report.status, AppStatus::TimedOut, "{}", dump(&report));
}

#[tokio::test(flavor = "multi_thread")]
async fn timeout_stops_an_app_waiting_for_input() {
    require_wasip2!();
    let report = App::from_path(guest("counter-app"))
        .headless(headless("", 2))
        .run()
        .await
        .unwrap();
    assert_eq!(report.status, AppStatus::TimedOut, "{}", dump(&report));
}

type Logs = std::sync::Arc<Mutex<Vec<(LogLevel, String, String)>>>;

/// Collect the app's log records through the phase hook.
fn log_sink() -> (Logs, impl Fn(Phase) + Send + Sync + 'static) {
    let logs = std::sync::Arc::new(Mutex::new(Vec::new()));
    let sink = logs.clone();
    (logs, move |phase| {
        if let Phase::Log {
            level,
            target,
            message,
        } = phase
        {
            sink.lock().unwrap().push((level, target, message));
        }
    })
}

#[tokio::test(flavor = "multi_thread")]
async fn storage_is_per_origin_and_persists_across_runs() {
    require_wasip2!();
    let server = Server::start(&[]);
    let dir = std::env::temp_dir().join(format!("rattery-e2e-storage-{}", std::process::id()));
    let launch = |snap: &rattery::Screen| -> Option<u64> {
        let line = snap.lines.iter().find(|l| l.contains("launch "))?;
        let rest = line.split("launch ").nth(1)?;
        rest.split_whitespace().next()?.parse().ok()
    };

    for expected in [1, 2] {
        let (logs, sink) = log_sink();
        let report = App::from_url(format!("{}/app.wasm", server.url))
            .unwrap()
            .cookies(CookiePolicy::Ephemeral)
            .storage(StoragePolicy::Dir(dir.clone()))
            .on_phase(sink)
            .headless(headless("sleep 2500\nsnapshot\nkey q", 20))
            .run()
            .await
            .unwrap();
        let text = dump(&report);
        assert_eq!(report.status, AppStatus::Exited(0), "{text}");
        assert_eq!(launch(&report.snapshots[0]), Some(expected), "{text}");
        let logs = logs.lock().unwrap();
        assert!(
            logs.iter()
                .any(|(level, target, message)| *level == LogLevel::Info
                    && target == "counter_app::app"
                    && *message == format!("launch {expected} from Some({:?})", server.url)),
            "{logs:?}\n{text}"
        );
        assert!(
            logs.iter().any(|(_, _, m)| m.starts_with("count is ")),
            "{logs:?}"
        );
        assert_eq!(report.stats.logs, logs.len() as u64, "{text}");
        assert_eq!(report.stats.logs_dropped, 0, "{text}");
    }

    // Ephemeral storage starts empty every run; a different origin is a
    // different store even under the same directory.
    let report = App::from_url(format!("{}/app.wasm", server.url))
        .unwrap()
        .cookies(CookiePolicy::Ephemeral)
        .storage(StoragePolicy::Ephemeral)
        .headless(headless("sleep 2500\nsnapshot\nkey q", 20))
        .run()
        .await
        .unwrap();
    assert_eq!(launch(&report.snapshots[0]), Some(1), "{}", dump(&report));
    let report = App::from_path(guest("counter-app"))
        .origin(&server.url)
        .cookies(CookiePolicy::Ephemeral)
        .storage(StoragePolicy::Dir(dir.clone()))
        .headless(headless("sleep 2500\nsnapshot\nkey q", 20))
        .run()
        .await
        .unwrap();
    assert_eq!(
        launch(&report.snapshots[0]),
        Some(3),
        "same origin, same store\n{}",
        dump(&report)
    );
    let files: Vec<_> = std::fs::read_dir(&dir)
        .unwrap()
        .flatten()
        .map(|e| e.file_name())
        .collect();
    let files: Vec<_> = files
        .into_iter()
        .filter(|f| f.to_string_lossy().ends_with(".json"))
        .collect();
    assert_eq!(files.len(), 1, "{files:?}");
    let _ = std::fs::remove_dir_all(&dir);
}

#[tokio::test(flavor = "multi_thread")]
async fn storage_quotas_and_log_limits_hold() {
    require_wasip2!();
    let (logs, sink) = log_sink();
    let limits = rattery::Limits {
        storage_bytes: 4096,
        storage_entries: 64,
        logs_per_second: 100,
        ..rattery::Limits::default()
    };
    let run = |storage: StoragePolicy| {
        App::from_path(guest("bench-app"))
            .origin("http://storage.test")
            .location("http://storage.test/app.wasm?mode=storage")
            .cookies(CookiePolicy::Ephemeral)
            .storage(storage)
            .limits(limits.clone())
    };
    let report = run(StoragePolicy::Ephemeral)
        .on_phase(sink)
        .headless(headless("sleep 3000", 10))
        .run()
        .await
        .unwrap();
    let text = dump(&report);
    assert_eq!(report.status, AppStatus::Exited(0), "{text}");
    assert!(
        report.stdout.contains("storage runs=1 used=5 quota=4096 big=Err(QuotaExceeded) many=Some(Err(QuotaExceeded)) keys=64"),
        "{text}"
    );
    let logs = std::mem::take(&mut *logs.lock().unwrap());
    let warn = logs
        .iter()
        .find(|(level, _, _)| *level == LogLevel::Warn)
        .unwrap();
    assert_eq!(warn.1, "bench_app::bench");
    assert!(
        !warn.2.contains('\u{1b}') && warn.2.contains("\\u{1b}]0;pwned"),
        "{warn:?}"
    );
    assert_eq!(logs.len(), 100, "rate limit\n{text}");
    assert_eq!(report.stats.logs_dropped, 2901, "{text}");

    // Disabled storage refuses writes, so the app's `?` fails it.
    let report = run(StoragePolicy::Disabled)
        .headless(headless("sleep 3000", 10))
        .run()
        .await
        .unwrap();
    let text = dump(&report);
    assert_eq!(report.status, AppStatus::Exited(1), "{text}");
    assert!(report.stderr.contains("storage is disabled"), "{text}");
}

#[tokio::test(flavor = "multi_thread")]
async fn precompiled_components_skip_compilation() {
    require_wasip2!();
    let wasm = std::fs::read(guest("bench-app")).unwrap();
    let native = rattery::precompile(&wasm, None).unwrap();
    let run = |app: App| async move {
        app.location("bench://local/app.wasm?mode=sparse&frames=5")
            .cookies(CookiePolicy::Ephemeral)
            .cache(false)
            .headless(headless("sleep 2000", 10))
            .run()
            .await
            .unwrap()
    };
    // SAFETY: produced a few lines up by `precompile`.
    let fast = run(unsafe { App::from_precompiled(native.clone()) }).await;
    let slow = run(App::from_bytes(wasm.clone())).await;
    eprintln!(
        "compile: {:?} precompiled vs {:?} from source",
        fast.timings.compile, slow.timings.compile
    );
    assert_eq!(fast.status, AppStatus::Exited(0), "{}", dump(&fast));
    assert!(
        fast.stdout.contains("bench mode=sparse frames=5"),
        "{}",
        dump(&fast)
    );
    assert!(
        fast.timings.compile * 10 < slow.timings.compile,
        "deserialising should be much cheaper than compiling: {:?} vs {:?}",
        fast.timings.compile,
        slow.timings.compile
    );

    // Mixing the two up fails cleanly before anything runs.
    let err = run_err(App::from_bytes(native.clone())).await;
    assert!(err.contains("not a valid component"), "{err}");
    // SAFETY: a wasm binary is refused by the header check; nothing executes.
    let err = run_err(unsafe { App::from_precompiled(wasm) }).await;
    assert!(err.contains("not a component precompiled"), "{err}");
}

async fn run_err(app: App) -> String {
    let err = app
        .cookies(CookiePolicy::Ephemeral)
        .headless(headless("sleep 100", 5))
        .run()
        .await
        .expect_err("should fail");
    format!("{err:#}")
}

#[tokio::test(flavor = "multi_thread")]
async fn the_app_reloads_itself_when_it_is_ready() {
    require_wasip2!();
    let server = Server::start(&[]);
    let dir = std::env::temp_dir().join(format!("rattery-e2e-reload-{}", std::process::id()));
    let phases = std::sync::Arc::new(Mutex::new(Vec::new()));
    let seen = phases.clone();
    // The headless `update` command plays the watcher; `R` is the app's
    // reload key. The launch counter in storage proves a new instance ran.
    let report = App::from_path(guest("counter-app"))
        .origin(&server.url)
        .cookies(CookiePolicy::Ephemeral)
        .storage(StoragePolicy::Dir(dir.clone()))
        .on_phase(move |phase| seen.lock().unwrap().push(format!("{phase:?}")))
        .headless(headless(
            "sleep 2500\nupdate v2\nsleep 400\nsnapshot\nkey R\nsleep 2500\nsnapshot\nkey q",
            20,
        ))
        .run()
        .await
        .unwrap();
    let text = dump(&report);
    assert_eq!(report.status, AppStatus::Exited(0), "{text}");
    let before = &report.snapshots[0];
    assert!(before.contains("update v2 available: R reloads"), "{text}");
    assert!(before.contains("launch 1"), "{text}");
    let after = &report.snapshots[1];
    assert!(!after.contains("available"), "{text}");
    assert!(after.contains("launch 2"), "{text}");
    let phases = phases.lock().unwrap().join("\n");
    assert!(phases.contains("Reloading"), "{phases}");
    assert_eq!(phases.matches("Ready").count(), 2, "{phases}");
    let _ = std::fs::remove_dir_all(&dir);
}

#[tokio::test(flavor = "multi_thread")]
async fn a_broken_update_never_interrupts_the_app() {
    require_wasip2!();
    let dir = std::env::temp_dir().join(format!("rattery-broken-{}", std::process::id()));
    std::fs::create_dir_all(&dir).unwrap();
    let served = dir.join("app.wasm");
    std::fs::copy(guest("counter-app"), &served).unwrap();
    let server = Server::start_serving(&served);
    for package in ["counter-app", "spin-app"] {
        App::from_path(guest(package))
            .cookies(CookiePolicy::Ephemeral)
            .headless(headless("", 1))
            .run()
            .await
            .unwrap();
    }

    let phases = std::sync::Arc::new(Mutex::new(Vec::new()));
    let seen = phases.clone();
    let run = tokio::spawn(
        App::from_url(format!("{}/app.wasm", server.url))
            .unwrap()
            .watch(true)
            .reload_policy(ReloadPolicy::Immediate)
            .on_phase(move |phase| seen.lock().unwrap().push(phase))
            .cookies(CookiePolicy::Ephemeral)
            .headless(headless("sleep 2500\nsnapshot\nsleep 5000\nsnapshot", 16))
            .run(),
    );
    tokio::time::sleep(Duration::from_secs(4)).await;
    // Garbage goes out first: the running app must not notice.
    std::fs::write(&served, b"this is not a component").unwrap();
    tokio::time::sleep(Duration::from_millis(2000)).await;
    // Then a component built for a future ABI.
    std::fs::write(&served, future_abi_component()).unwrap();
    tokio::time::sleep(Duration::from_millis(2500)).await;
    // A real component afterwards still gets picked up.
    std::fs::copy(guest("spin-app"), &served).unwrap();

    let report = run.await.unwrap().unwrap();
    let text = dump(&report);
    assert_eq!(report.status, AppStatus::TimedOut, "{text}");
    assert!(report.snapshots[0].contains("rattery counter"), "{text}");
    let later = &report.snapshots[1];
    assert!(
        later.contains("rattery counter") && later.contains("launch 1"),
        "the app should have run on undisturbed\n{text}"
    );
    assert!(
        report
            .final_screen
            .as_ref()
            .unwrap()
            .contains("spinning forever"),
        "{text}"
    );
    let phases = phases.lock().unwrap();
    let rejected = phases
        .iter()
        .position(|p| matches!(p, Phase::UpdateRejected { reason, requires_abi: None } if reason.contains("not a valid component")))
        .unwrap_or_else(|| panic!("{phases:?}"));
    assert!(
        phases.iter().any(|p| matches!(
            p,
            Phase::UpdateRejected { reason, requires_abi: Some(abi) }
                if abi == "rattery:tui@0.9.0" && reason.contains("upgrade the host")
        )),
        "{phases:?}"
    );
    let reloaded = phases.iter().position(|p| *p == Phase::Reloading).unwrap();
    assert!(rejected < reloaded, "{phases:?}");
    assert_eq!(
        phases.iter().filter(|p| **p == Phase::Reloading).count(),
        1,
        "{phases:?}"
    );
    let _ = std::fs::remove_dir_all(&dir);
}

/// The counter app with its rattery imports renamed to a future version:
/// the same length, so the binary stays well-formed, and a host of today
/// cannot link it.
fn future_abi_component() -> Vec<u8> {
    let mut bytes = std::fs::read(guest("counter-app")).unwrap();
    for interface in ["terminal", "websocket", "storage"] {
        let from = format!("rattery:tui/{interface}@0.3.0");
        let to = format!("rattery:tui/{interface}@0.9.0");
        let mut at = 0;
        while let Some(pos) = bytes[at..]
            .windows(from.len())
            .position(|w| w == from.as_bytes())
        {
            let start = at + pos;
            bytes[start..start + to.len()].copy_from_slice(to.as_bytes());
            at = start + to.len();
        }
    }
    bytes
}

#[tokio::test(flavor = "multi_thread")]
async fn a_component_for_another_abi_is_refused_before_the_terminal() {
    require_wasip2!();
    let future = future_abi_component();
    let info = rattery::inspect(&future).unwrap();
    assert_eq!(info.abi.as_deref(), Some("rattery:tui@0.9.0"), "{info:?}");
    assert!(!info.compatible, "{info:?}");
    assert!(
        rattery::inspect(&std::fs::read(guest("counter-app")).unwrap())
            .unwrap()
            .compatible
    );

    let err = run_err(App::from_bytes(future)).await;
    assert!(
        err.contains("built for rattery:tui@0.9.0") && err.contains("upgrade the host"),
        "{err}"
    );
}

#[tokio::test(flavor = "multi_thread")]
async fn the_server_can_demand_an_abi() {
    require_wasip2!();
    let flag = std::env::temp_dir().join(format!("rattery-abi-{}.txt", std::process::id()));
    let _ = std::fs::remove_file(&flag);
    let server = Server::start(&["--require-abi-file", flag.to_str().unwrap()]);
    let url = format!("{}/app.wasm", server.url);

    // Without the flag file the server serves everyone.
    let client = reqwest::Client::new();
    let ok = client.get(&url).send().await.unwrap();
    assert_eq!(ok.status(), 200);

    // With it, the client's `rattery-abi` header decides.
    std::fs::write(&flag, "rattery:tui@9.0\n").unwrap();
    let refused = client
        .get(&url)
        .header(rattery::ABI_HEADER, rattery::ABI)
        .send()
        .await
        .unwrap();
    assert_eq!(refused.status(), 426);
    assert_eq!(refused.headers()[rattery::ABI_HEADER], "rattery:tui@9.0");
    let served = client
        .get(&url)
        .header(rattery::ABI_HEADER, "rattery:tui@9.0.1;cm-async")
        .send()
        .await
        .unwrap();
    assert_eq!(served.status(), 200);

    // The host at startup: a clear error naming both sides.
    let err = run_err(App::from_url(&url).unwrap()).await;
    assert!(
        err.contains("requires ABI rattery:tui@9.0") && err.contains("upgrade the host"),
        "{err}"
    );

    // A running app is told once and left alone.
    std::fs::remove_file(&flag).unwrap();
    let phases = std::sync::Arc::new(Mutex::new(Vec::new()));
    let seen = phases.clone();
    let flag2 = flag.clone();
    let run = tokio::spawn(
        App::from_url(&url)
            .unwrap()
            .watch(true)
            .on_phase(move |phase| seen.lock().unwrap().push(phase))
            .cookies(CookiePolicy::Ephemeral)
            .headless(headless("sleep 2500\nsnapshot\nsleep 3500\nsnapshot", 8))
            .run(),
    );
    tokio::time::sleep(Duration::from_secs(3)).await;
    std::fs::write(&flag2, "rattery:tui@9.0\n").unwrap();
    let report = run.await.unwrap().unwrap();
    let text = dump(&report);
    assert_eq!(report.status, AppStatus::TimedOut, "{text}");
    assert!(report.snapshots[1].contains("rattery counter"), "{text}");
    let phases = phases.lock().unwrap();
    let rejections: Vec<_> = phases
        .iter()
        .filter(|p| matches!(p, Phase::UpdateRejected { .. }))
        .collect();
    assert_eq!(
        rejections.len(),
        1,
        "reported once, not every poll\n{phases:?}"
    );
    assert!(
        matches!(rejections[0], Phase::UpdateRejected { requires_abi: Some(abi), .. } if abi == "rattery:tui@9.0"),
        "{rejections:?}"
    );
    assert!(!phases.contains(&Phase::Reloading), "{phases:?}");
    let _ = std::fs::remove_file(&flag);
}