tokmesh-core 0.1.3

Core library for tokmesh — session parsing, scanning, aggregation, pricing
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
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
//! Session interval derivation and time-based metrics.

use crate::sessions::UnifiedMessage;
use crate::TokenBreakdown;
use std::collections::HashMap;

/// Default idle gap threshold: 3 minutes (ms).
pub const DEFAULT_IDLE_GAP_MS: i64 = 3 * 60 * 1000;

/// A derived session interval representing one continuous usage session.
#[derive(Debug, Clone, serde::Serialize)]
pub struct SessionInterval {
    pub client: String,
    pub session_id: String,
    /// First message timestamp (Unix ms)
    pub start_ts: i64,
    /// Last message timestamp (Unix ms)
    pub end_ts: i64,
    /// Wall-clock duration: end_ts - start_ts
    pub wall_duration_ms: i64,
    /// Active duration excluding idle gaps beyond the threshold
    pub active_duration_ms: i64,
    pub message_count: i32,
    pub tokens: TokenBreakdown,
    pub cost: f64,
}

/// Time-based usage metrics computed from session intervals.
#[derive(Debug, Clone, serde::Serialize)]
pub struct TimeMetrics {
    /// Total active usage time across all sessions (ms)
    pub total_active_time_ms: i64,
    /// Total wall-clock usage time across all sessions (ms)
    pub total_wall_time_ms: i64,
    /// Longest single session active duration (ms)
    pub longest_continuous_ms: i64,
    /// Peak number of sessions overlapping at the same time
    pub max_concurrent_sessions: u32,
    /// Total number of derived session intervals
    pub session_count: u32,
}

#[derive(Debug)]
struct SessionMessageSpan<'a> {
    msg: &'a UnifiedMessage,
    start_ts: i64,
    end_ts: i64,
}

#[derive(Debug)]
struct SessionBlock {
    start_ts: i64,
    end_ts: i64,
    tokens: TokenBreakdown,
    cost: f64,
    message_count: i32,
}

impl SessionBlock {
    fn new(span: &SessionMessageSpan<'_>) -> Self {
        let mut block = Self {
            start_ts: span.start_ts,
            end_ts: span.end_ts,
            tokens: TokenBreakdown::default(),
            cost: 0.0,
            message_count: 0,
        };
        block.add(span);
        block
    }

    fn add(&mut self, span: &SessionMessageSpan<'_>) {
        self.end_ts = self.end_ts.max(span.end_ts);
        // saturating_add so clamped (i64::MAX) buckets from a corrupt source
        // can't overflow the fold.
        self.tokens.input = self.tokens.input.saturating_add(span.msg.tokens.input);
        self.tokens.output = self.tokens.output.saturating_add(span.msg.tokens.output);
        self.tokens.cache_read = self
            .tokens
            .cache_read
            .saturating_add(span.msg.tokens.cache_read);
        self.tokens.cache_write = self
            .tokens
            .cache_write
            .saturating_add(span.msg.tokens.cache_write);
        self.tokens.reasoning = self
            .tokens
            .reasoning
            .saturating_add(span.msg.tokens.reasoning);
        self.cost += span.msg.cost;
        self.message_count += span.msg.message_count.max(1);
    }
}

/// Derive session intervals from unified messages.
///
/// Groups messages by `(client, session_id)`, sorts each group by timestamp,
/// and computes per-session start/end/duration/active-time.
///
/// `idle_gap_ms` controls how much silence between messages is still counted
/// as "active". Time gaps exceeding this threshold are excluded from
/// `active_duration_ms` by splitting a source session into separate active
/// intervals.
pub fn sessionize(messages: &[UnifiedMessage], idle_gap_ms: i64) -> Vec<SessionInterval> {
    if messages.is_empty() {
        return Vec::new();
    }

    // Group by (client, session_id)
    let mut groups: HashMap<(&str, &str), Vec<&UnifiedMessage>> = HashMap::new();
    for msg in messages {
        if msg.timestamp <= 0 {
            continue;
        }
        groups
            .entry((&msg.client, &msg.session_id))
            .or_default()
            .push(msg);
    }

    let mut intervals: Vec<SessionInterval> = Vec::with_capacity(groups.len());

    for ((client, session_id), mut msgs) in groups {
        msgs.sort_unstable_by_key(|m| m.timestamp);

        let spans: Vec<SessionMessageSpan<'_>> = msgs
            .into_iter()
            .map(|msg| {
                let duration_ms = msg
                    .duration_ms
                    .filter(|duration| *duration > 0)
                    .unwrap_or(0);
                SessionMessageSpan {
                    msg,
                    start_ts: msg.timestamp,
                    end_ts: msg.timestamp.saturating_add(duration_ms),
                }
            })
            .collect();

        let mut blocks: Vec<SessionBlock> = Vec::new();
        for span in spans {
            match blocks.last_mut() {
                Some(block) if span.start_ts <= block.end_ts.saturating_add(idle_gap_ms) => {
                    block.add(&span);
                }
                _ => blocks.push(SessionBlock::new(&span)),
            }
        }

        for block in blocks {
            let wall_duration_ms = block.end_ts.saturating_sub(block.start_ts);

            intervals.push(SessionInterval {
                client: client.to_string(),
                session_id: session_id.to_string(),
                start_ts: block.start_ts,
                end_ts: block.end_ts,
                wall_duration_ms,
                active_duration_ms: wall_duration_ms,
                message_count: block.message_count,
                tokens: block.tokens,
                cost: block.cost,
            });
        }
    }

    // Sort by start time for downstream consumers
    intervals.sort_unstable_by_key(|s| s.start_ts);
    intervals
}

/// Compute time-based metrics from session intervals.
///
/// - `total_active_time_ms`: sum of all `active_duration_ms`
/// - `total_wall_time_ms`: sum of all `wall_duration_ms`
/// - `longest_continuous_ms`: longest merged activity window across ALL sessions
///   (using the idle gap threshold to merge overlapping/adjacent activity)
/// - `max_concurrent_sessions`: peak overlap of session wall-clock intervals
pub fn compute_time_metrics(intervals: &[SessionInterval], _idle_gap_ms: i64) -> TimeMetrics {
    if intervals.is_empty() {
        return TimeMetrics {
            total_active_time_ms: 0,
            total_wall_time_ms: 0,
            longest_continuous_ms: 0,
            max_concurrent_sessions: 0,
            session_count: 0,
        };
    }

    let total_active_time_ms: i64 = intervals.iter().map(|s| s.active_duration_ms).sum();
    let total_wall_time_ms: i64 = intervals.iter().map(|s| s.wall_duration_ms).sum();
    let session_count = intervals.len() as u32;

    // --- Longest continuous usage ---
    // Collect all session [start, end] as activity windows, merge overlapping
    // ones (with idle_gap_ms tolerance), find the longest merged span.
    // Use active_duration_ms instead of wall-clock span to exclude idle gaps
    // within sessions from inflating the metric.
    let longest_continuous_ms = {
        let mut windows: Vec<(i64, i64)> = intervals
            .iter()
            .filter(|s| s.start_ts > 0 && s.active_duration_ms > 0)
            .map(|s| (s.start_ts, s.start_ts + s.active_duration_ms))
            .collect();
        windows.sort_unstable_by_key(|w| w.0);

        let mut longest: i64 = 0;
        if let Some(&first) = windows.first() {
            let mut merged_start = first.0;
            let mut merged_end = first.1;

            for &(start, end) in &windows[1..] {
                if start <= merged_end + _idle_gap_ms {
                    // Overlapping or within idle gap tolerance — extend
                    merged_end = merged_end.max(end);
                } else {
                    // Gap too large — finalize previous window
                    longest = longest.max(merged_end - merged_start);
                    merged_start = start;
                    merged_end = end;
                }
            }
            longest = longest.max(merged_end - merged_start);
        }
        longest
    };

    // --- Max concurrent sessions ---
    let max_concurrent_sessions = compute_max_concurrent(intervals);

    TimeMetrics {
        total_active_time_ms,
        total_wall_time_ms,
        longest_continuous_ms,
        max_concurrent_sessions,
        session_count,
    }
}

/// Sweep-line algorithm to find peak concurrent sessions.
fn compute_max_concurrent(intervals: &[SessionInterval]) -> u32 {
    if intervals.is_empty() {
        return 0;
    }

    let mut events: Vec<(i64, i32)> = Vec::with_capacity(intervals.len() * 2);
    for s in intervals {
        if s.start_ts <= 0 {
            continue;
        }
        events.push((s.start_ts, 1));
        // For zero-duration sessions (start == end), push end as start+1
        // so the +1 event is processed before the -1 event at the same logical point
        let end = if s.end_ts <= s.start_ts {
            s.start_ts + 1
        } else {
            s.end_ts
        };
        events.push((end, -1));
    }

    // Sort by time; ties broken by start (+1) before end (-1) so concurrent
    // sessions at the same timestamp are counted together
    events.sort_unstable_by(|a, b| a.0.cmp(&b.0).then(b.1.cmp(&a.1)));

    let mut max_concurrent: u32 = 0;
    let mut current: i32 = 0;

    for (_, delta) in events {
        current += delta;
        if current > max_concurrent as i32 {
            max_concurrent = current as u32;
        }
    }

    max_concurrent
}

/// Compute per-day active time (ms) from session intervals.
///
/// For each interval, distributes its `active_duration_ms` proportionally
/// across local-time days for the active timezone. Single-day sessions get their full active
/// time assigned to that day.
pub fn compute_daily_active_time(
    intervals: &[SessionInterval],
) -> std::collections::HashMap<String, i64> {
    match crate::bucket_tz::bucket_timezone() {
        crate::bucket_tz::BucketTimezone::Local => {
            compute_daily_active_time_with_timezone(intervals, &chrono::Local)
        }
        crate::bucket_tz::BucketTimezone::Named(tz) => {
            compute_daily_active_time_with_timezone(intervals, &tz)
        }
    }
}

fn compute_daily_active_time_with_timezone<Tz>(
    intervals: &[SessionInterval],
    timezone: &Tz,
) -> std::collections::HashMap<String, i64>
where
    Tz: chrono::TimeZone,
{
    use std::collections::HashMap;

    let mut daily: HashMap<String, i64> = HashMap::new();

    for interval in intervals {
        if interval.active_duration_ms <= 0 {
            continue;
        }

        let start_date = match local_date(interval.start_ts, timezone) {
            Some(date) => date,
            None => continue,
        };
        let end_date = match local_date(interval.end_ts, timezone) {
            Some(date) => date,
            None => continue,
        };

        let wall = interval.wall_duration_ms.max(1);
        let mut day = start_date;

        loop {
            let day_key = day.format("%Y-%m-%d").to_string();
            let Some(day_start) = local_day_start(day, timezone) else {
                break;
            };

            let Some(next_day) = day.succ_opt() else {
                break;
            };
            let Some(next_day_start) = local_day_start(next_day, timezone) else {
                break;
            };

            let overlap_start = interval.start_ts.max(day_start);
            let overlap_end = interval.end_ts.min(next_day_start);
            let overlap = (overlap_end - overlap_start).max(0);
            let proportion = overlap as f64 / wall as f64;
            let active_for_day = (interval.active_duration_ms as f64 * proportion) as i64;

            if active_for_day > 0 {
                *daily.entry(day_key).or_default() += active_for_day;
            }

            if day == end_date {
                break;
            }

            if let Some(next) = day.succ_opt() {
                day = next;
            } else {
                break;
            }
        }
    }

    daily
}

fn local_date<Tz>(timestamp_ms: i64, timezone: &Tz) -> Option<chrono::NaiveDate>
where
    Tz: chrono::TimeZone,
{
    timezone
        .timestamp_millis_opt(timestamp_ms)
        .single()
        .map(|datetime| datetime.date_naive())
}

fn local_day_start<Tz>(date: chrono::NaiveDate, timezone: &Tz) -> Option<i64>
where
    Tz: chrono::TimeZone,
{
    let mut wall = date.and_time(chrono::NaiveTime::MIN);
    for _ in 0..=(24 * 60) {
        match timezone.from_local_datetime(&wall) {
            chrono::LocalResult::Single(datetime) | chrono::LocalResult::Ambiguous(datetime, _) => {
                return Some(datetime.timestamp_millis());
            }
            chrono::LocalResult::None => wall += chrono::Duration::minutes(1),
        }
    }
    None
}

#[cfg(test)]
mod tests {
    use super::*;
    use chrono::{FixedOffset, TimeZone};

    fn make_msg(client: &str, session_id: &str, timestamp: i64) -> UnifiedMessage {
        UnifiedMessage {
            client: client.to_string(),
            model_id: "test-model".to_string(),
            provider_id: "test-provider".to_string(),
            session_id: session_id.to_string(),
            workspace_key: None,
            workspace_label: None,
            timestamp,
            date: "2024-01-01".to_string(),
            tokens: TokenBreakdown {
                input: 100,
                output: 50,
                cache_read: 0,
                cache_write: 0,
                reasoning: 0,
            },
            cost: 0.01,
            cost_source: Default::default(),
            message_count: 1,
            agent: None,
            dedup_key: None,
            session_title: None,
            is_turn_start: false,
            duration_ms: None,
        }
    }

    fn make_timed_msg(
        client: &str,
        session_id: &str,
        timestamp: i64,
        duration_ms: i64,
    ) -> UnifiedMessage {
        let mut message = make_msg(client, session_id, timestamp);
        message.duration_ms = Some(duration_ms);
        message
    }

    #[test]
    fn test_sessionize_empty() {
        let result = sessionize(&[], DEFAULT_IDLE_GAP_MS);
        assert!(result.is_empty());
    }

    #[test]
    fn test_sessionize_single_message() {
        let msgs = vec![make_msg("opencode", "ses1", 1000000)];
        let result = sessionize(&msgs, DEFAULT_IDLE_GAP_MS);
        assert_eq!(result.len(), 1);
        assert_eq!(result[0].wall_duration_ms, 0);
        assert_eq!(result[0].active_duration_ms, 0);
        assert_eq!(result[0].message_count, 1);
    }

    #[test]
    fn test_sessionize_saturates_overflowing_token_fold() {
        // A parser can clamp an untrusted token count to i64::MAX (e.g.
        // antigravity_cli). Two such messages in one (client, session_id) block
        // within the idle gap fold into the same SessionBlock; a plain `+=`
        // fold would overflow (debug panic / release wrap) before it is
        // serialized into SessionInterval.tokens.
        let overlarge = |ts: i64| {
            let mut message = make_msg("antigravity-cli", "ses1", ts);
            message.tokens = TokenBreakdown {
                input: i64::MAX,
                output: 0,
                cache_read: i64::MAX,
                cache_write: 0,
                reasoning: 0,
            };
            message
        };
        let msgs = vec![overlarge(1_000_000), overlarge(1_001_000)];
        let result = sessionize(&msgs, DEFAULT_IDLE_GAP_MS);
        assert_eq!(result.len(), 1);
        assert_eq!(result[0].tokens.input, i64::MAX);
        assert_eq!(result[0].tokens.cache_read, i64::MAX);
    }

    #[test]
    fn test_sessionize_counts_single_timed_message_duration() {
        let msgs = vec![make_timed_msg("opencode", "ses1", 1_000_000, 45_000)];
        let result = sessionize(&msgs, DEFAULT_IDLE_GAP_MS);

        assert_eq!(result.len(), 1);
        assert_eq!(result[0].start_ts, 1_000_000);
        assert_eq!(result[0].end_ts, 1_045_000);
        assert_eq!(result[0].wall_duration_ms, 45_000);
        assert_eq!(result[0].active_duration_ms, 45_000);
    }

    #[test]
    fn test_sessionize_splits_timed_messages_across_idle_gap() {
        let msgs = vec![
            make_timed_msg("opencode", "ses1", 1_000_000, 60_000),
            make_timed_msg("opencode", "ses1", 1_000_000 + 10 * 60_000, 30_000),
        ];

        let result = sessionize(&msgs, DEFAULT_IDLE_GAP_MS);

        assert_eq!(result.len(), 2);
        assert_eq!(result[0].start_ts, 1_000_000);
        assert_eq!(result[0].end_ts, 1_060_000);
        assert_eq!(result[0].active_duration_ms, 60_000);
        assert_eq!(result[1].start_ts, 1_600_000);
        assert_eq!(result[1].end_ts, 1_630_000);
        assert_eq!(result[1].active_duration_ms, 30_000);
    }

    #[test]
    fn test_sessionize_continuous_session() {
        // 5 messages, each 1 minute apart (within 3-min threshold)
        let msgs: Vec<UnifiedMessage> = (0..5)
            .map(|i| make_msg("opencode", "ses1", 1000000 + i * 60_000))
            .collect();

        let result = sessionize(&msgs, DEFAULT_IDLE_GAP_MS);
        assert_eq!(result.len(), 1);
        assert_eq!(result[0].wall_duration_ms, 4 * 60_000);
        assert_eq!(result[0].active_duration_ms, 4 * 60_000); // all gaps <= 3min
        assert_eq!(result[0].message_count, 5);
    }

    #[test]
    fn test_sessionize_with_idle_gap() {
        // 3 messages: first two 1 min apart, then 5 min gap (exceeds 3-min threshold)
        let msgs = vec![
            make_msg("opencode", "ses1", 1000000),
            make_msg("opencode", "ses1", 1000000 + 60_000),
            make_msg("opencode", "ses1", 1000000 + 60_000 + 5 * 60_000),
        ];

        let result = sessionize(&msgs, DEFAULT_IDLE_GAP_MS);
        assert_eq!(result.len(), 2);
        assert_eq!(result[0].active_duration_ms, 60_000);
        assert_eq!(result[0].wall_duration_ms, 60_000);
        assert_eq!(result[0].message_count, 2);
        assert_eq!(result[1].active_duration_ms, 0);
        assert_eq!(result[1].wall_duration_ms, 0);
        assert_eq!(result[1].message_count, 1);
    }

    #[test]
    fn test_sessionize_multiple_sessions() {
        let msgs = vec![
            make_msg("opencode", "ses1", 1000000),
            make_msg("opencode", "ses1", 1060000),
            make_msg("claude", "ses2", 1000000),
            make_msg("claude", "ses2", 1120000),
        ];

        let result = sessionize(&msgs, DEFAULT_IDLE_GAP_MS);
        assert_eq!(result.len(), 2);
    }

    #[test]
    fn test_sessionize_skips_zero_timestamp() {
        let msgs = vec![
            make_msg("opencode", "ses1", 0),
            make_msg("opencode", "ses1", 1000000),
        ];

        let result = sessionize(&msgs, DEFAULT_IDLE_GAP_MS);
        assert_eq!(result.len(), 1);
        assert_eq!(result[0].message_count, 1); // only the non-zero one
    }

    #[test]
    fn test_compute_time_metrics_empty() {
        let metrics = compute_time_metrics(&[], DEFAULT_IDLE_GAP_MS);
        assert_eq!(metrics.total_active_time_ms, 0);
        assert_eq!(metrics.longest_continuous_ms, 0);
        assert_eq!(metrics.max_concurrent_sessions, 0);
        assert_eq!(metrics.session_count, 0);
    }

    #[test]
    fn test_max_concurrent_sessions() {
        // Two overlapping sessions
        let intervals = vec![
            SessionInterval {
                client: "opencode".to_string(),
                session_id: "ses1".to_string(),
                start_ts: 1000,
                end_ts: 5000,
                wall_duration_ms: 4000,
                active_duration_ms: 4000,
                message_count: 3,
                tokens: TokenBreakdown::default(),
                cost: 0.0,
            },
            SessionInterval {
                client: "claude".to_string(),
                session_id: "ses2".to_string(),
                start_ts: 3000,
                end_ts: 7000,
                wall_duration_ms: 4000,
                active_duration_ms: 4000,
                message_count: 3,
                tokens: TokenBreakdown::default(),
                cost: 0.0,
            },
        ];

        let metrics = compute_time_metrics(&intervals, DEFAULT_IDLE_GAP_MS);
        assert_eq!(metrics.max_concurrent_sessions, 2);
    }

    #[test]
    fn test_max_concurrent_non_overlapping() {
        // Two non-overlapping sessions
        let intervals = vec![
            SessionInterval {
                client: "opencode".to_string(),
                session_id: "ses1".to_string(),
                start_ts: 1000,
                end_ts: 3000,
                wall_duration_ms: 2000,
                active_duration_ms: 2000,
                message_count: 2,
                tokens: TokenBreakdown::default(),
                cost: 0.0,
            },
            SessionInterval {
                client: "claude".to_string(),
                session_id: "ses2".to_string(),
                start_ts: 5000,
                end_ts: 7000,
                wall_duration_ms: 2000,
                active_duration_ms: 2000,
                message_count: 2,
                tokens: TokenBreakdown::default(),
                cost: 0.0,
            },
        ];

        let metrics = compute_time_metrics(&intervals, DEFAULT_IDLE_GAP_MS);
        assert_eq!(metrics.max_concurrent_sessions, 1);
    }

    #[test]
    fn test_longest_continuous_is_max_session_active_duration() {
        let intervals = vec![
            SessionInterval {
                client: "opencode".to_string(),
                session_id: "ses1".to_string(),
                start_ts: 1000,
                end_ts: 5000,
                wall_duration_ms: 4000,
                active_duration_ms: 3000,
                message_count: 3,
                tokens: TokenBreakdown::default(),
                cost: 0.0,
            },
            SessionInterval {
                client: "claude".to_string(),
                session_id: "ses2".to_string(),
                start_ts: 3000,
                end_ts: 8000,
                wall_duration_ms: 5000,
                active_duration_ms: 5000,
                message_count: 3,
                tokens: TokenBreakdown::default(),
                cost: 0.0,
            },
        ];

        let metrics = compute_time_metrics(&intervals, DEFAULT_IDLE_GAP_MS);
        assert_eq!(metrics.longest_continuous_ms, 7000);
    }

    #[test]
    fn test_longest_continuous_picks_max_active() {
        let intervals = vec![
            SessionInterval {
                client: "opencode".to_string(),
                session_id: "ses1".to_string(),
                start_ts: 1,
                end_ts: 60_000,
                wall_duration_ms: 60_000,
                active_duration_ms: 60_000,
                message_count: 3,
                tokens: TokenBreakdown::default(),
                cost: 0.0,
            },
            SessionInterval {
                client: "opencode".to_string(),
                session_id: "ses2".to_string(),
                start_ts: 60_000 + 2 * 60_000,
                end_ts: 60_000 + 2 * 60_000 + 120_000,
                wall_duration_ms: 120_000,
                active_duration_ms: 120_000,
                message_count: 3,
                tokens: TokenBreakdown::default(),
                cost: 0.0,
            },
        ];

        let metrics = compute_time_metrics(&intervals, DEFAULT_IDLE_GAP_MS);
        assert_eq!(metrics.longest_continuous_ms, 299_999);
    }

    #[test]
    fn test_longest_continuous_single_session() {
        let intervals = vec![
            SessionInterval {
                client: "opencode".to_string(),
                session_id: "ses1".to_string(),
                start_ts: 1000,
                end_ts: 61_000,
                wall_duration_ms: 60_000,
                active_duration_ms: 60_000,
                message_count: 3,
                tokens: TokenBreakdown::default(),
                cost: 0.0,
            },
            SessionInterval {
                client: "opencode".to_string(),
                session_id: "ses2".to_string(),
                start_ts: 61_000 + 10 * 60_000,
                end_ts: 61_000 + 10 * 60_000 + 120_000,
                wall_duration_ms: 120_000,
                active_duration_ms: 120_000,
                message_count: 5,
                tokens: TokenBreakdown::default(),
                cost: 0.0,
            },
        ];

        let metrics = compute_time_metrics(&intervals, DEFAULT_IDLE_GAP_MS);
        assert_eq!(metrics.longest_continuous_ms, 120_000);
    }

    #[test]
    fn test_compute_daily_active_time_matches_local_day_boundaries_for_fixed_offset() {
        let interval = SessionInterval {
            client: "trae".to_string(),
            session_id: "session-local-boundary".to_string(),
            start_ts: FixedOffset::east_opt(9 * 3600)
                .unwrap()
                .with_ymd_and_hms(2026, 1, 1, 23, 30, 0)
                .single()
                .unwrap()
                .timestamp_millis(),
            end_ts: FixedOffset::east_opt(9 * 3600)
                .unwrap()
                .with_ymd_and_hms(2026, 1, 2, 0, 30, 0)
                .single()
                .unwrap()
                .timestamp_millis(),
            wall_duration_ms: 3_600_000,
            active_duration_ms: 3_600_000,
            message_count: 2,
            tokens: TokenBreakdown::default(),
            cost: 0.0,
        };

        let daily = compute_daily_active_time_with_timezone(
            &[interval],
            &FixedOffset::east_opt(9 * 3600).unwrap(),
        );

        assert_eq!(daily.get("2026-01-01"), Some(&1_800_000));
        assert_eq!(daily.get("2026-01-02"), Some(&1_800_000));
        assert_eq!(daily.len(), 2);
    }

    #[test]
    fn test_local_day_start_walks_forward_across_midnight_dst_gap() {
        let timezone = chrono_tz::America::Santiago;
        let date = chrono::NaiveDate::from_ymd_opt(2019, 9, 8).unwrap();

        let start = local_day_start(date, &timezone).unwrap();
        let expected = timezone
            .with_ymd_and_hms(2019, 9, 8, 1, 0, 0)
            .single()
            .unwrap()
            .timestamp_millis();

        assert_eq!(start, expected);
    }

    #[test]
    fn test_daily_active_time_keeps_idle_separated_timed_blocks_on_their_days() {
        let timezone = FixedOffset::east_opt(0).unwrap();
        let first_start = timezone
            .with_ymd_and_hms(2026, 1, 1, 23, 58, 0)
            .single()
            .unwrap()
            .timestamp_millis();
        let second_start = timezone
            .with_ymd_and_hms(2026, 1, 2, 0, 10, 0)
            .single()
            .unwrap()
            .timestamp_millis();
        let msgs = vec![
            make_timed_msg("opencode", "ses1", first_start, 120_000),
            make_timed_msg("opencode", "ses1", second_start, 60_000),
        ];

        let intervals = sessionize(&msgs, DEFAULT_IDLE_GAP_MS);
        let daily = compute_daily_active_time_with_timezone(&intervals, &timezone);

        assert_eq!(intervals.len(), 2);
        assert_eq!(daily.get("2026-01-01"), Some(&120_000));
        assert_eq!(daily.get("2026-01-02"), Some(&60_000));
        assert_eq!(daily.len(), 2);
    }
}