heatsync-tui 0.2.0

heat-sorted live multichat in the terminal — twitch, kick + youtube (read-only), real emotes, vim keys
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
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
//! the virtual tabs: mentions, feed, whispers. they live in `app.channels`
//! like any tab (so focus, unread colouring and the tab bar need no second
//! code path) but join nothing — core's `Channel::special` has no sources, so
//! no line, part, emote fetch or history call ever reaches them. they pin left
//! of the channel tabs in [`TabKind`] order, and which are open persists.

use std::sync::mpsc::{channel, Receiver, Sender};
use std::sync::Arc;
use std::time::{Duration, Instant};

use heatsync_core::emote::{ref_set, EmoteSet};
use heatsync_core::proto::{ChatLine, SpecialLine};
use heatsync_core::{Channel, Message, Note, Platform, TabKind, Tag, TagKind};

use crate::app::{App, Feed};
use crate::followlog::{FollowLog, FollowRow, Item};
use crate::mentions::{self, Mention};
use crate::net::{ChatEvent, Outbound};
use crate::save_state;
use crate::social::Msg;
use crate::talk;
use heatsync_core::social::{Contact, Row};

/// rows a virtual tab keeps (the ring cap, like a channel's).
const CAP: usize = 500;
/// feed rows fetched when the tab opens.
const FEED_BACKLOG: usize = 30;

pub(crate) struct State {
    pub(crate) log: mentions::Log,
    /// followed-channel stream events shown among the mentions (never persisted).
    pub(crate) follow: FollowLog,
    /// recent mentions + arrival time: a live ping and the server's rule match
    /// for one message must read as one row.
    recent: Vec<(Instant, Mention)>,
    feed_tx: Sender<Vec<SpecialLine>>,
    feed_rx: Receiver<Vec<SpecialLine>>,
    /// the relay socket is signed in with the heatsync login (not HEATSYNC_TOKEN).
    pub(crate) ws_cli: bool,
    /// the "log in again" hint has been shown — it speaks once per run.
    pub(crate) hinted: bool,
    /// finished account calls (see `talk`) come back here.
    pub(crate) social_tx: Sender<Msg>,
    pub(crate) social_rx: Receiver<Msg>,
    /// people `/w` and `/dm` can address: your threads, follows, live senders.
    pub(crate) contacts: Vec<Contact>,
    /// when the last push-driven refetches ran (see [`refetch_due`]).
    pub(crate) account_at: Option<Instant>,
    pub(crate) follows_at: Option<Instant>,
}

impl State {
    pub(crate) fn new(ws_cli: bool) -> State {
        State {
            ws_cli,
            ..State::with_log(mentions::Log::load())
        }
    }

    fn with_log(log: mentions::Log) -> State {
        let (feed_tx, feed_rx) = channel();
        let (social_tx, social_rx) = channel();
        State {
            log,
            follow: FollowLog::default(),
            recent: Vec::new(),
            feed_tx,
            feed_rx,
            ws_cli: false,
            hinted: false,
            social_tx,
            social_rx,
            contacts: Vec::new(),
            account_at: None,
            follows_at: None,
        }
    }
}

/// index of the open tab of `kind`.
pub(crate) fn find(app: &App, kind: TabKind) -> Option<usize> {
    app.channels.iter().position(|c| c.kind == kind)
}

/// the tab's line, as the mention log remembers it.
fn mention_msg(m: &Mention) -> Message {
    let mut tags = vec![Tag::platform()];
    if !m.channel.is_empty() {
        tags.push(Tag::new(&format!("[{}]", m.channel), TagKind::Origin));
    }
    row(
        m.platform, &m.user, &m.color, &m.text, tags, 0.0, None, None,
    )
}

/// a followed-channel event as a dim `[chan]` row (the channel's colour when known).
fn follow_msg(f: &FollowLog, r: &FollowRow) -> Message {
    let mut m = row(
        r.platform,
        "",
        &f.color_of(r),
        &r.title,
        vec![
            Tag::platform(),
            Tag::new(&format!("[{}]", r.channel), TagKind::Origin),
        ],
        0.0,
        None,
        None,
    );
    m.note = Some(Note {
        kind: r.kind,
        what: r.what.clone(),
    });
    m.extra.ts = r.at.max(0) as u64;
    m
}

/// the mentions tab's rows: the log and the follow events in time order.
fn mention_rows(s: &State) -> Vec<Message> {
    s.follow
        .timeline(s.log.rows())
        .into_iter()
        .map(|i| match i {
            Item::Mention(m) => mention_msg(m),
            Item::Follow(r) => follow_msg(&s.follow, r),
        })
        .collect()
}

/// refill the open mentions tab from scratch — history, not news.
fn rebuild(app: &mut App) {
    if let Some(i) = find(app, TabKind::Mentions) {
        let mut ch = Channel::special(TabKind::Mentions, CAP);
        for m in mention_rows(&app.special) {
            ch.deliver(m);
        }
        ch.mark_seen();
        app.channels[i] = ch;
    }
}

/// `follow:history`: the server's replay. folded in by time, deduped against
/// what is already here, never a ping.
pub(crate) fn follow_history(app: &mut App, events: Vec<(ChatLine, u64)>) {
    crate::streamrow::replay(app, &events, crate::clock::now_secs());
    let mut fresh = false;
    for (l, ms) in events {
        if let Some(r) = FollowRow::of(&l, (ms / 1000) as i64) {
            fresh |= app.special.follow.add(r);
        }
    }
    if fresh {
        rebuild(app);
    }
}

/// `follow:colors`: repaint the rows when a channel's colour is new.
pub(crate) fn follow_colors(app: &mut App, colors: Vec<(String, String)>) {
    if app.special.follow.set_colors(colors) {
        rebuild(app);
    }
}

/// a live followed-channel event that passed the stream guards: remembered, and
/// when the tab is open it lands at the bottom; a go-live marks the tab yellow.
pub(crate) fn follow_event(app: &mut App, l: &ChatLine) {
    let Some(r) = FollowRow::of(l, crate::clock::now_secs() as i64) else {
        return;
    };
    let live = r.kind == heatsync_core::NoteKind::Live;
    if !app.special.follow.add(r.clone()) {
        return;
    }
    if let Some(i) = find(app, TabKind::Mentions) {
        let m = follow_msg(&app.special.follow, &r);
        let ch = &mut app.channels[i];
        ch.system(m);
        if app.focus != i {
            if live {
                ch.flag_event();
            }
        } else {
            ch.mark_seen();
        }
    }
}

#[allow(clippy::too_many_arguments)]
fn row(
    platform: Platform,
    user: &str,
    color: &Option<String>,
    text: &str,
    tags: Vec<Tag>,
    heat: f64,
    id: Option<String>,
    emotes: Option<Arc<EmoteSet>>,
) -> Message {
    Message {
        platform,
        user: user.to_string(),
        text: text.to_string(),
        color: color.clone(),
        badges: Vec::new(),
        reply_to: None,
        id,
        gone: None,
        note: None,
        emotes,
        tags,
        extra: Default::default(),
        uid: None,
        heat,
    }
}

fn special_msg(s: SpecialLine) -> Message {
    let l = s.line;
    let refs = (!l.emotes.is_empty()).then(|| Arc::new(ref_set(&l.emotes)));
    row(
        l.platform, &l.user, &l.color, &l.content, s.tags, s.heat, l.id, refs,
    )
}

/// make sure the tab exists (pinned in order) and return its index. fills it
/// from what we already know — the mention log — and starts the feed.
pub(crate) fn add(app: &mut App, kind: TabKind) -> usize {
    if let Some(i) = find(app, kind) {
        return i;
    }
    let at = app
        .channels
        .iter()
        .take_while(|c| c.kind.is_special() && c.kind < kind)
        .count();
    let mut ch = Channel::special(kind, CAP);
    if kind == TabKind::Mentions {
        for m in mention_rows(&app.special) {
            ch.deliver(m);
        }
        ch.mark_seen(); // history isn't news
    }
    app.channels.insert(at, ch);
    app.emotes.insert(at, app.globals.clone());
    if app.focus >= at && app.channels.len() > 1 {
        app.focus += 1; // keep the same tab focused
    }
    if app.manage_cursor >= at {
        app.manage_cursor += 1;
    }
    match kind {
        TabKind::Feed => start_feed(app),
        TabKind::Whispers => talk::load_history(app),
        TabKind::Mentions => talk::load_hits(app),
        TabKind::Channel => {}
    }
    at
}

/// open (or focus) a virtual tab.
pub(crate) fn open(app: &mut App, kind: TabKind) {
    let i = add(app, kind);
    app.focus = i;
    save_state(app);
}

/// a virtual tab is being closed: stop whatever feeds it.
pub(crate) fn on_close(app: &App, idx: usize) {
    if app.channels.get(idx).map(|c| c.kind) == Some(TabKind::Feed) {
        if let Some(out) = &app.out {
            let _ = out.send(Outbound::Feed(false));
        }
    }
}

/// join the site feed and load its latest rows. mock mode has no network.
fn start_feed(app: &App) {
    if !matches!(app.feed, Feed::Live { .. }) {
        return;
    }
    if let Some(out) = &app.out {
        let _ = out.send(Outbound::Feed(true));
    }
    let tx = app.special.feed_tx.clone();
    std::thread::spawn(move || {
        let rows = crate::http::feed_recent(FEED_BACKLOG);
        if !rows.is_empty() {
            let _ = tx.send(rows);
        }
    });
}

/// startup: the tabs the config says are open, plus their feed / history.
pub(crate) fn restore(app: &mut App, kinds: &[TabKind]) {
    for &k in kinds {
        add(app, k);
    }
}

/// the initial feed load landed: chronological rows go in behind whatever
/// live rows already arrived.
pub(crate) fn drain_backlog(app: &mut App) {
    while let Ok(rows) = app.special.feed_rx.try_recv() {
        let Some(i) = find(app, TabKind::Feed) else {
            continue;
        };
        let ch = &mut app.channels[i];
        let mut fresh: Vec<Message> = rows
            .into_iter()
            .map(special_msg)
            .filter(|m| !ch.messages.iter().any(|e| e.id.is_some() && e.id == m.id))
            .collect();
        // oldest first, then in front of the live tail (history is not unread)
        let live: Vec<Message> = ch.messages.drain(..).collect();
        for m in fresh.drain(..).chain(live) {
            ch.system(m);
        }
    }
}

/// how long two reports of one message count as the same mention.
const DUP_WINDOW: Duration = Duration::from_secs(10);

/// same message? an id match when both sides have one, else same channel +
/// user with one text the other's prefix / substring (snippets are cut short).
fn same_mention(a: &Mention, b: &Mention) -> bool {
    if let (Some(x), Some(y)) = (&a.id, &b.id) {
        return x == y;
    }
    let (t, u) = (a.text.trim(), b.text.trim());
    a.channel.eq_ignore_ascii_case(&b.channel)
        && a.user.eq_ignore_ascii_case(&b.user)
        && !t.is_empty()
        && !u.is_empty()
        && (t.contains(u) || u.contains(t))
}

/// a mention landed (a live ping, or a server rule match): always remembered,
/// and shown + flagged when the tab is open. a repeat report is dropped.
pub(crate) fn mention(app: &mut App, m: Mention) {
    let now = Instant::now();
    app.special
        .recent
        .retain(|(t, _)| now.duration_since(*t) < DUP_WINDOW);
    if app.special.recent.iter().any(|(_, r)| same_mention(r, &m)) {
        return;
    }
    app.special.recent.push((now, m.clone()));
    if let Some(i) = find(app, TabKind::Mentions) {
        let ch = &mut app.channels[i];
        ch.deliver(mention_msg(&m));
        if app.focus != i {
            ch.ping();
        } else {
            ch.mark_seen();
        }
    }
    app.special.log.push(m);
}

/// least gap between two refetches the same push may cause — a hostile or
/// buggy server can't turn a push flood into a request flood.
pub(crate) const REFETCH_GAP: Duration = Duration::from_secs(5);

/// true (and stamps `slot`) when a refetch is allowed now.
pub(crate) fn refetch_due(slot: &mut Option<Instant>) -> bool {
    let now = Instant::now();
    if slot.is_some_and(|t| now.duration_since(t) < REFETCH_GAP) {
        return false;
    }
    *slot = Some(now);
    true
}

/// `account:changed`: ask who I am again off-thread; new names join `app.me`
/// (see `feed::drain_hs`), so mention pings follow a newly linked platform.
pub(crate) fn account_changed(app: &mut App) {
    if !matches!(app.feed, Feed::Live { .. }) || !refetch_due(&mut app.special.account_at) {
        return;
    }
    let Some(c) = app.hs.clone() else { return };
    let tx = app.names_tx.clone();
    std::thread::spawn(move || {
        if let Ok(v) = c.me() {
            let _ = tx.send(crate::hsauth::my_names(&v));
        }
    });
}

/// the socket's answer to `authenticate`. a login without the `account`
/// permission is refused and stays anonymous: say so once, in plain words.
pub(crate) fn auth(app: &mut App, ok: bool) {
    if ok {
        app.status = Some("authenticated".into());
    } else if !app.special.ws_cli {
        app.status = Some("auth failed — check HEATSYNC_TOKEN".into());
    } else if !app.special.hinted {
        app.special.hinted = true;
        app.status =
            Some("run `heatsync-tui login` again to get whispers, dms and mentions".into());
    }
}

/// route the events virtual tabs care about.
pub(crate) fn event(app: &mut App, ev: ChatEvent) {
    match ev {
        ChatEvent::Special(s) => match s.tab {
            TabKind::Mentions => mention(
                app,
                Mention {
                    platform: s.line.platform,
                    channel: s.line.channel,
                    user: s.line.user,
                    color: s.line.color,
                    text: s.line.content,
                    id: s.line.id,
                    at: crate::clock::now_secs() as i64,
                },
            ),
            TabKind::Feed | TabKind::Whispers => {
                let tab = s.tab;
                let Some(i) = find(app, tab) else { return };
                if tab == TabKind::Feed
                    && s.line.id.is_some()
                    && app.channels[i].messages.iter().any(|m| m.id == s.line.id)
                {
                    return; // already in the backlog
                }
                if let Some(p) = s.peer.clone() {
                    talk::learn(app, p);
                }
                let msg = special_msg(s);
                let ch = &mut app.channels[i];
                ch.deliver(msg);
                if tab == TabKind::Whispers && app.focus != i {
                    ch.ping();
                }
                if app.focus == i {
                    ch.mark_seen();
                }
            }
            TabKind::Channel => {}
        },
        ChatEvent::FeedHeat { id, heat } => {
            if let Some(i) = find(app, TabKind::Feed) {
                if let Some(m) = app.channels[i]
                    .messages
                    .iter_mut()
                    .find(|m| m.id.as_deref() == Some(id.as_str()))
                {
                    m.heat = heat;
                }
            }
        }
        ChatEvent::FeedEdit { id, text, emotes } => {
            if let Some(i) = find(app, TabKind::Feed) {
                let refs = (!emotes.is_empty()).then(|| Arc::new(ref_set(&emotes)));
                for m in app.channels[i]
                    .messages
                    .iter_mut()
                    .filter(|m| m.id.as_deref() == Some(id.as_str()))
                {
                    m.text = text.clone();
                    m.emotes = refs.clone();
                }
            }
        }
        ChatEvent::FeedHidden { id } => {
            if let Some(i) = find(app, TabKind::Feed) {
                for m in app.channels[i]
                    .messages
                    .iter_mut()
                    .filter(|m| m.id.as_deref() == Some(id.as_str()))
                {
                    m.gone = Some("hidden".into());
                }
            }
        }
        _ => {}
    }
}

/// Enter on the mentions tab: jump to the tab the newest mention came from.
/// (no message cursor yet, so "newest" is the whole selection.)
pub(crate) fn enter(app: &mut App) {
    let Some(ch) = app.channels.get(app.focus) else {
        return;
    };
    if ch.kind != TabKind::Mentions {
        return;
    }
    let Some(m) = ch.messages.back() else {
        app.status = Some("no mentions yet".into());
        return;
    };
    let chan = m
        .tags
        .iter()
        .find(|t| t.kind == TagKind::Origin)
        .map(|t| t.text.trim_matches(|c| c == '[' || c == ']').to_string())
        .unwrap_or_default();
    match app
        .channels
        .iter()
        .position(|c| c.matches(m.platform, &chan))
    {
        Some(i) => app.focus = i,
        None => app.status = Some(format!("not open: {chan}")),
    }
}

/// text composed on a virtual tab. the feed takes posts (a reply when `r`
/// picked a row); whispers want `/w` or `/dm` (they need a name); mentions
/// are read-only. true = the line went and the composer clears.
pub(crate) fn send(app: &mut App, kind: TabKind, text: &str, reply: Option<String>) -> bool {
    match kind {
        TabKind::Feed => return talk::post(app, text, reply),
        TabKind::Whispers => {
            app.status =
                Some("say who to: /w <user> <msg> or /dm <user> <msg> — or r on a row".into())
        }
        _ => {
            app.status = Some(format!(
                "{} is read-only — open a channel to chat",
                kind.name()
            ))
        }
    }
    false
}

/// the whispers / dms history landed: oldest first, in front of whatever live
/// rows already arrived, skipping any the live feed already showed.
pub(crate) fn backfill_whispers(app: &mut App, rows: Vec<Row>) {
    let Some(i) = find(app, TabKind::Whispers) else {
        return;
    };
    let ch = &mut app.channels[i];
    let same = |a: &Message, b: &Message| {
        a.user == b.user && a.text == b.text && a.tags.first() == b.tags.first()
    };
    let fresh: Vec<Message> = rows
        .into_iter()
        .map(|r| special_msg(r.line))
        .filter(|m| !ch.messages.iter().any(|e| same(e, m)))
        .collect();
    let live: Vec<Message> = ch.messages.drain(..).collect();
    for m in fresh.into_iter().chain(live) {
        ch.system(m);
    }
}

/// the server's rule hits landed: any the local log doesn't already hold are
/// merged in by time (history, not news — nothing flashes) and the tab is
/// rebuilt from the log so they sit where they happened.
pub(crate) fn backfill_mentions(app: &mut App, hits: Vec<Mention>) {
    let mut fresh: Vec<Mention> = Vec::new();
    for m in hits {
        let held = app
            .special
            .log
            .rows()
            .chain(fresh.iter())
            .any(|r| same_mention(r, &m));
        if !held {
            fresh.push(m);
        }
    }
    if fresh.is_empty() {
        return;
    }
    app.special.log.merge(fresh);
    rebuild(app);
}

#[cfg(test)]
pub(crate) mod tests {
    use super::*;
    use crate::app::InputMode;
    use crate::{hsauth, picker};
    use heatsync_core::proto::{self, Event};
    use heatsync_core::room::RoomModes;
    use heatsync_core::streamev::StreamEvents;
    use heatsync_core::{edit, mock};

    /// an app with two real tabs, a mock feed and an in-memory mention log —
    /// nothing touches the network or ~/.config.
    pub(crate) fn app() -> App {
        let (hs_tx, hs_notes) = channel::<hsauth::Note>();
        let (emote_tx, emote_rx) = channel();
        let (hist_tx, hist_rx) = channel();
        let (pick_tx, pick_rx) = channel();
        App {
            channels: vec![
                Channel::new("xqc", Platform::Twitch, 50),
                Channel::new("kripp", Platform::Kick, 50),
            ],
            emotes: vec![EmoteSet::new(), EmoteSet::new()],
            globals: EmoteSet::new(),
            focus: 0,
            paused: false,
            feed: Feed::Mock(mock::Driver::new()),
            store: None,
            fb: None,
            mode: InputMode::Normal,
            input: String::new(),
            line: edit::Line::default(),
            status: None,
            out: None,
            twitch_tx: None,
            twitch_notes: None,
            kick_tx: None,
            hs: None,
            hs_tx,
            hs_notes,
            tab_pos: crate::config::TabPos::Top,
            manage_cursor: 0,
            completion: None,
            me: Vec::new(),
            names_rx: channel().1,
            names_tx: channel().0,
            stamps: false,
            streams: StreamEvents::new(),
            rooms: RoomModes::new(),
            seen: Default::default(),
            paints: crate::paints::Paints::new(),
            pin_tx: channel().0,
            pin_rx: channel().1,
            cursor: 0,
            reply: None,
            view_h: std::cell::Cell::new(0),
            chat_body: Default::default(),
            emote_tx,
            emote_rx,
            hist_tx,
            hist_rx,
            picker: None,
            pick_cache: picker::Cache(None),
            pick_tx,
            pick_rx,
            pick_inflight: false,
            special: State::with_log(mentions::Log::open(None)),
        }
    }

    fn kinds(app: &App) -> Vec<TabKind> {
        app.channels.iter().map(|c| c.kind).collect()
    }

    fn ev(raw: &str) -> ChatEvent {
        match proto::parse(raw) {
            Event::Special(s) => ChatEvent::Special(s),
            Event::FeedHeat { id, heat } => ChatEvent::FeedHeat { id, heat },
            Event::FeedHidden { id } => ChatEvent::FeedHidden { id },
            Event::FeedEdit { id, text, emotes } => ChatEvent::FeedEdit { id, text, emotes },
            e => panic!("{e:?}"),
        }
    }

    #[test]
    fn an_edit_push_rewrites_the_matching_feed_row_only() {
        let mut a = app();
        let i = add(&mut a, TabKind::Feed);
        for id in ["a1", "b2"] {
            let raw = format!(
                r#"{{"type":"new-message","base36_id":"{id}","content":"old","username":"u","is_op":true}}"#
            );
            event(&mut a, ev(&raw));
        }
        event(
            &mut a,
            ev(
                r##"{"type":"message-edited","message_id":"b2","content":"new Pog",
                "emote_refs":{"Pog":{"url":"https://cdn.hs/p.webp"}}}"##,
            ),
        );
        let m = &a.channels[i].messages;
        assert_eq!((m[0].text.as_str(), m[0].emotes.is_some()), ("old", false));
        assert_eq!(
            (m[1].text.as_str(), m[1].emotes.is_some()),
            ("new Pog", true)
        );
        // a later edit without refs drops them; an unknown id changes nothing
        event(
            &mut a,
            ev(r#"{"type":"message-updated","base36_id":"b2","content":"plain"}"#),
        );
        event(
            &mut a,
            ev(r#"{"type":"message-edited","message_id":"zz","content":"ghost"}"#),
        );
        let m = &a.channels[i].messages;
        assert_eq!(
            (m[1].text.as_str(), m[1].emotes.is_some()),
            ("plain", false)
        );
        assert_eq!(m.len(), 2);
    }

    #[test]
    fn refetches_are_rate_limited() {
        let mut slot = None;
        assert!(refetch_due(&mut slot));
        assert!(!refetch_due(&mut slot), "a push flood is one request");
        slot = Some(Instant::now() - REFETCH_GAP - Duration::from_millis(1));
        assert!(refetch_due(&mut slot));
    }

    #[test]
    fn account_changed_refetches_my_names_once() {
        use crate::hsauth::testkit::{client as fake_client, Fake};
        let f = Fake::new(vec![Ok((
            200,
            serde_json::json!({"twitch": {"login": "newme"}, "kick": {"linked": false}}),
        ))]);
        let mut a = app();
        let (tx, rx) = channel();
        a.names_tx = tx;
        a.hs = Some(fake_client(&f));
        // the demo never calls out
        account_changed(&mut a);
        assert!(rx.recv_timeout(Duration::from_millis(100)).is_err());
        a.feed = Feed::Live {
            rx: channel().1,
            start: Instant::now(),
            connected: true,
        };
        account_changed(&mut a);
        account_changed(&mut a); // inside the gap: no second call
        let names = rx.recv_timeout(Duration::from_secs(2)).expect("names");
        assert_eq!(names, vec!["newme".to_string()]);
        assert!(rx.recv_timeout(Duration::from_millis(100)).is_err());
        assert_eq!(f.seen.lock().unwrap().len(), 1);
    }

    fn mention_of(chan: &str, user: &str) -> Mention {
        Mention {
            platform: Platform::Twitch,
            channel: chan.into(),
            user: user.into(),
            color: None,
            text: "hey me".into(),
            id: None,
            at: 0,
        }
    }

    #[test]
    fn mention_rows_carry_platform_and_origin_tags() {
        let m = mention_msg(&Mention {
            platform: Platform::Kick,
            channel: "xqc".into(),
            user: "bob".into(),
            color: None,
            text: "hey".into(),
            id: None,
            at: 0,
        });
        assert_eq!(m.tags[0].kind, TagKind::Platform);
        assert_eq!(m.tags[1], Tag::new("[xqc]", TagKind::Origin));
        assert_eq!(m.platform, Platform::Kick);
    }

    #[test]
    fn tabs_pin_left_in_kind_order_and_focus_follows_the_same_tab() {
        let mut a = app();
        a.focus = 1; // kripp
        open(&mut a, TabKind::Whispers);
        assert_eq!(a.focus, 0, "opening focuses the new tab");
        a.focus = 1; // xqc, right after whispers
        add(&mut a, TabKind::Mentions);
        assert_eq!(
            kinds(&a),
            vec![
                TabKind::Mentions,
                TabKind::Whispers,
                TabKind::Channel,
                TabKind::Channel
            ]
        );
        assert_eq!(a.channels[a.focus].name, "xqc", "focus kept its tab");
        add(&mut a, TabKind::Feed);
        assert_eq!(
            kinds(&a)[..3],
            [TabKind::Mentions, TabKind::Feed, TabKind::Whispers]
        );
        assert_eq!(a.channels[a.focus].name, "xqc");
        assert_eq!(a.emotes.len(), a.channels.len(), "emote sets stay aligned");
    }

    #[test]
    fn opening_twice_focuses_the_existing_tab() {
        let mut a = app();
        open(&mut a, TabKind::Feed);
        a.focus = 2;
        open(&mut a, TabKind::Feed);
        assert_eq!(a.channels.len(), 3);
        assert_eq!(a.focus, 0);
    }

    #[test]
    fn mentions_are_logged_even_with_the_tab_closed_and_flag_it_when_open() {
        let mut a = app();
        mention(&mut a, mention_of("xqc", "bob"));
        assert_eq!(a.special.log.rows().count(), 1);
        // opening replays the log without marking it unread
        open(&mut a, TabKind::Mentions);
        assert_eq!(a.channels[0].messages.len(), 1);
        assert_eq!(a.channels[0].unread(), heatsync_core::Unread::None);
        // a new one while another tab is focused pings the tab
        a.focus = 1;
        mention(&mut a, mention_of("kripp", "al"));
        assert_eq!(a.channels[0].messages.len(), 2);
        assert_eq!(a.channels[0].unread(), heatsync_core::Unread::Ping);
        assert_eq!(a.special.log.rows().count(), 2);
    }

    #[test]
    fn a_ping_and_its_rule_match_are_one_row() {
        let mut a = app();
        open(&mut a, TabKind::Mentions);
        // live ping (has the id), then the server's match with a cut snippet
        let mut live = mention_of("xqc", "Bob");
        live.text = "hey mellen look at this".into();
        live.id = Some("m1".into());
        mention(&mut a, live);
        let mut rule = mention_of("xqc", "bob");
        rule.text = "hey mellen look".into();
        mention(&mut a, rule.clone());
        assert_eq!(a.channels[0].messages.len(), 1, "snippet dedupe");
        assert_eq!(a.special.log.rows().count(), 1);
        // same id wins even when the text differs
        let mut other = mention_of("kripp", "al");
        other.id = Some("m1".into());
        mention(&mut a, other);
        assert_eq!(a.channels[0].messages.len(), 1);
        // a different message from the same user is a new row
        let mut next = mention_of("xqc", "bob");
        next.text = "totally different".into();
        mention(&mut a, next);
        assert_eq!(a.channels[0].messages.len(), 2);
        // outside the window the same text counts again
        a.special.recent.clear();
        mention(&mut a, rule);
        assert_eq!(a.channels[0].messages.len(), 3);
    }

    fn follow_ev(chan: &str, what: &str, at_ms: u64) -> (ChatLine, u64) {
        let raw = format!(
            r##"{{"type":"follow:history","events":[{{"type":"follow:stream:online","platform":"twitch","channel":"{chan}","game":"{what}","color":"#ff0000","time":{at_ms}}}]}}"##
        );
        match proto::parse(&raw) {
            Event::FollowHistory(mut h) => h.remove(0),
            e => panic!("{e:?}"),
        }
    }

    fn history(chan: &str, game: &str, at_ms: u64) -> Event {
        let (l, at) = follow_ev(chan, game, at_ms);
        Event::FollowHistory(vec![(l, at)])
    }

    #[test]
    fn follow_history_merges_by_time_dedupes_and_never_persists_or_pings() {
        let mut a = app();
        let mut early = mention_of("xqc", "al");
        early.at = 1_000;
        let mut late = mention_of("xqc", "bo");
        late.at = 3_000;
        late.text = "yo me".into();
        a.special.log.push(early);
        a.special.log.push(late);
        let before = a.special.log.rows().count();
        add(&mut a, TabKind::Mentions);
        let i = find(&a, TabKind::Mentions).unwrap();
        a.focus = a.channels.len() - 1; // not on the mentions tab
        for _ in 0..2 {
            // a reconnect resends the same history
            let Event::FollowHistory(h) = history("cyr", "Doom", 2_000_000) else {
                unreachable!()
            };
            follow_history(&mut a, h);
        }
        let ch = &a.channels[i];
        assert_eq!(
            ch.messages
                .iter()
                .map(|m| m.note.is_some())
                .collect::<Vec<_>>(),
            [false, true, false]
        );
        let f = &ch.messages[1];
        assert_eq!(f.tags[1], Tag::new("[cyr]", TagKind::Origin));
        assert_eq!(f.note.as_ref().unwrap().what, "went live — Doom");
        assert_eq!(f.color.as_deref(), Some("#ff0000"));
        assert_eq!(ch.unread(), heatsync_core::Unread::None);
        assert_eq!(a.special.log.rows().count(), before, "not in the log");
    }

    #[test]
    fn live_follow_go_live_lands_in_the_tab_and_marks_it_yellow() {
        let mut a = app();
        add(&mut a, TabKind::Mentions);
        let i = find(&a, TabKind::Mentions).unwrap();
        a.focus = i + 1;
        let (l, _) = follow_ev("cyr", "Doom", 1);
        follow_event(&mut a, &l);
        follow_event(&mut a, &l); // a repeat adds nothing
        assert_eq!(a.channels[i].messages.len(), 1);
        assert_eq!(a.channels[i].unread(), heatsync_core::Unread::Event);
        assert_eq!(a.special.log.rows().count(), 0);
    }

    #[test]
    fn enter_jumps_to_the_newest_mentions_source_tab() {
        let mut a = app();
        open(&mut a, TabKind::Mentions);
        enter(&mut a);
        assert_eq!(a.status.as_deref(), Some("no mentions yet"));
        mention(&mut a, mention_of("kripp", "bob")); // twitch/kripp isn't open
        a.focus = 0;
        enter(&mut a);
        assert_eq!(a.focus, 0);
        assert_eq!(a.status.as_deref(), Some("not open: kripp"));
        mention(&mut a, mention_of("xqc", "al"));
        enter(&mut a);
        assert_eq!(a.channels[a.focus].name, "xqc");
        // Enter elsewhere does nothing
        let at = a.focus;
        enter(&mut a);
        assert_eq!(a.focus, at);
    }

    #[test]
    fn server_rule_matches_become_mentions() {
        let mut a = app();
        open(&mut a, TabKind::Mentions);
        event(
            &mut a,
            ev(
                r##"{"type":"mention:rule-match","ruleId":1,"pattern":"p","channel":"xqc",
            "platform":"twitch","username":"bob","snippet":"yo","url":"u"}"##,
            ),
        );
        assert_eq!(a.channels[0].messages.len(), 1);
        assert_eq!(a.special.log.rows().count(), 1);
    }

    #[test]
    fn feed_rows_update_in_place_and_dedupe() {
        let mut a = app();
        open(&mut a, TabKind::Feed);
        let row = r##"{"type":"new-message","base36_id":"0a","content":"hi","username":"u","heat":3,"is_op":true}"##;
        event(&mut a, ev(row));
        event(&mut a, ev(row)); // same id twice → one row
        assert_eq!(a.channels[0].messages.len(), 1);
        assert_eq!(a.channels[0].messages[0].heat, 3.0);
        event(
            &mut a,
            ev(r#"{"type":"heat:updated","messageId":"0a","newHeat":88}"#),
        );
        assert_eq!(a.channels[0].messages[0].heat, 88.0);
        event(&mut a, ev(r#"{"type":"message:hidden","id":"0a"}"#));
        assert_eq!(a.channels[0].messages[0].gone.as_deref(), Some("hidden"));
        // heat / hidden for an unknown row are no-ops
        event(
            &mut a,
            ev(r#"{"type":"heat:updated","messageId":"zz","newHeat":1}"#),
        );
    }

    #[test]
    fn rows_for_a_closed_tab_are_dropped() {
        let mut a = app();
        event(
            &mut a,
            ev(r#"{"type":"new-message","base36_id":"0a","content":"hi","username":"u"}"#),
        );
        event(
            &mut a,
            ev(r#"{"type":"whisper:new","from_display_name":"B","content":"x"}"#),
        );
        assert_eq!(a.channels.len(), 2);
    }

    #[test]
    fn whispers_ping_the_tab_when_unfocused() {
        let mut a = app();
        open(&mut a, TabKind::Whispers);
        a.focus = 1;
        event(
            &mut a,
            ev(r#"{"type":"dm:new","from_display_name":"B","content":"x"}"#),
        );
        assert_eq!(a.channels[0].messages[0].tags[0].kind, TagKind::Dm);
        assert_eq!(a.channels[0].unread(), heatsync_core::Unread::Ping);
    }

    #[test]
    fn backlog_lands_behind_live_rows_without_unread() {
        let mut a = app();
        open(&mut a, TabKind::Feed);
        event(
            &mut a,
            ev(r#"{"type":"new-message","base36_id":"03","content":"live","username":"u"}"#),
        );
        let row = |id: &str| {
            proto::feed_row(
                &serde_json::from_str(&format!(
                    r#"{{"base36_id":"{id}","content":"old {id}","username":"u"}}"#
                ))
                .unwrap(),
            )
            .unwrap()
        };
        // oldest first, overlapping the live row's id
        a.special
            .feed_tx
            .send(vec![row("01"), row("02"), row("03")])
            .unwrap();
        drain_backlog(&mut a);
        let ids: Vec<_> = a.channels[0]
            .messages
            .iter()
            .map(|m| m.id.clone().unwrap())
            .collect();
        assert_eq!(ids, vec!["01", "02", "03"]);
        assert_eq!(a.channels[0].messages[2].text, "live");
    }

    #[test]
    fn virtual_tabs_say_what_they_take() {
        let mut a = app();
        assert!(!send(&mut a, TabKind::Mentions, "hi", None));
        assert_eq!(
            a.status.as_deref(),
            Some("mentions is read-only — open a channel to chat")
        );
        // whispers need a name: the hint says how
        assert!(!send(&mut a, TabKind::Whispers, "hi", None));
        assert!(a.status.as_deref().unwrap().contains("/w <user>"));
        // the feed takes posts — without a login it says how to get one
        assert!(!send(&mut a, TabKind::Feed, "hi", None));
        assert!(a.status.as_deref().unwrap().contains("heatsync-tui login"));
    }

    #[test]
    fn a_refused_login_hints_once_and_a_bad_env_token_keeps_its_old_words() {
        let mut a = app();
        a.special.ws_cli = true;
        auth(&mut a, false);
        assert!(a.status.as_deref().unwrap().contains("heatsync-tui login"));
        a.status = None;
        auth(&mut a, false); // every reconnect fails again: stay quiet
        assert_eq!(a.status, None);
        a.special.ws_cli = false;
        auth(&mut a, false);
        assert!(a.status.as_deref().unwrap().contains("HEATSYNC_TOKEN"));
    }
}