tokensave 7.9.0

Code intelligence tool that builds a semantic knowledge graph from Rust, Go, Java, Scala, TypeScript, Python, C, C++, Kotlin, C#, Swift, and many more codebases
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
use std::path::Path;
use tempfile::TempDir;
use tokensave::accounting::CoverageState;
use tokensave::monitor::{cost_panel_lines, CostCache};

/// Helper: write an entry to a specific mmap dir.
fn write(dir: &Path, project: &Path, prefix: &str, tool: &str, delta: u64, before: u64) {
    tokensave::monitor::write_entry_to(dir, project, prefix, tool, delta, before);
}

/// Helper: open reader at a specific mmap dir.
fn reader(dir: &Path) -> tokensave::monitor::MmapReader {
    tokensave::monitor::MmapReader::open_at(dir).unwrap()
}

#[test]
fn test_write_and_read_entry() {
    let dir = TempDir::new().unwrap();
    let project = TempDir::new().unwrap();

    write(
        dir.path(),
        project.path(),
        "tokensave",
        "tokensave_context",
        63_102,
        290_000,
    );

    let r = reader(dir.path());
    assert_eq!(r.write_idx(), 1);

    let entry = r.entry(0).unwrap();
    assert_eq!(entry.prefix, "tokensave");
    assert_eq!(entry.tool_name, "tokensave_context");
    assert_eq!(entry.delta, 63_102);
    assert_eq!(entry.before, 290_000);
    assert!(entry.timestamp > 0);
    assert!(!entry.project.is_empty());
}

#[test]
fn test_ring_buffer_wraps() {
    let dir = TempDir::new().unwrap();
    let project = TempDir::new().unwrap();

    for i in 0..260u64 {
        write(
            dir.path(),
            project.path(),
            "tokensave",
            "tokensave_search",
            i + 1,
            i * 10,
        );
    }

    let r = reader(dir.path());
    assert_eq!(r.write_idx(), 260);

    // Slot 0 should now have entry 256 (index 256, delta=257)
    let entry = r.entry(0).unwrap();
    assert_eq!(entry.delta, 257);

    // Slot 3 should have entry 259 (index 259, delta=260)
    let entry = r.entry(3).unwrap();
    assert_eq!(entry.delta, 260);
}

#[test]
fn test_write_entry_accumulates() {
    let dir = TempDir::new().unwrap();
    let project = TempDir::new().unwrap();

    write(
        dir.path(),
        project.path(),
        "tokensave",
        "tokensave_context",
        100,
        500,
    );
    write(
        dir.path(),
        project.path(),
        "tokensave",
        "tokensave_search",
        50,
        200,
    );

    let r = reader(dir.path());
    assert_eq!(r.write_idx(), 2);

    let e0 = r.entry(0).unwrap();
    let e1 = r.entry(1).unwrap();
    assert_eq!(e0.delta + e1.delta, 150);
}

#[test]
fn test_entry_label_format() {
    let dir = TempDir::new().unwrap();
    let project = TempDir::new().unwrap();

    write(
        dir.path(),
        project.path(),
        "tokensave",
        "tokensave_context",
        42,
        100,
    );

    let r = reader(dir.path());
    let entry = r.entry(0).unwrap();
    let label = entry.label();
    assert!(label.starts_with("tokensave - "), "got: {label}");
    assert!(label.ends_with(" - tokensave_context"), "got: {label}");
}

#[test]
fn test_tool_name_truncation() {
    let dir = TempDir::new().unwrap();
    let project = TempDir::new().unwrap();

    let long_name = "a".repeat(100);
    write(dir.path(), project.path(), "tokensave", &long_name, 42, 100);

    let r = reader(dir.path());
    let entry = r.entry(0).unwrap();
    // Should be truncated to 31 chars (32 bytes with null)
    assert_eq!(entry.tool_name.len(), 31);
}

#[test]
fn test_multiple_projects() {
    let dir = TempDir::new().unwrap();
    let project_a = TempDir::with_prefix("alpha").unwrap();
    let project_b = TempDir::with_prefix("bravo").unwrap();

    write(
        dir.path(),
        project_a.path(),
        "tokensave",
        "tokensave_context",
        100,
        500,
    );
    write(
        dir.path(),
        project_b.path(),
        "tokensave",
        "tokensave_search",
        200,
        600,
    );

    let r = reader(dir.path());
    assert_eq!(r.write_idx(), 2);

    let e0 = r.entry(0).unwrap();
    let e1 = r.entry(1).unwrap();
    assert_ne!(e0.project, e1.project);
}

#[test]
fn test_different_prefixes() {
    let dir = TempDir::new().unwrap();
    let project = TempDir::new().unwrap();

    write(
        dir.path(),
        project.path(),
        "tokensave",
        "tokensave_context",
        100,
        500,
    );
    write(
        dir.path(),
        project.path(),
        "othertool",
        "do_stuff",
        200,
        600,
    );

    let r = reader(dir.path());
    let e0 = r.entry(0).unwrap();
    let e1 = r.entry(1).unwrap();
    assert_eq!(e0.prefix, "tokensave");
    assert_eq!(e1.prefix, "othertool");
}

// ── cost_panel_lines tests ────────────────────────────────────────────

fn base_cache() -> CostCache {
    CostCache {
        today_cost: 0.0,
        week_cost: 0.0,
        tokens_saved: 0,
        efficiency_pct: 0.0,
        top_model: String::new(),
        top_model_cost: 0.0,
        today_credits: None,
        week_credits: None,
        week_droid_tokens: 0,
        droid_state: CoverageState::Absent,
        last_refresh: std::time::Instant::now(),
    }
}

/// Droid credits and raw tokens render as lowercase compact notation (2.9k, 24.6k).
#[test]
fn droid_credits_and_tokens_formatted_compact_lowercase() {
    let cache = CostCache {
        today_credits: Some(2_900),
        week_credits: Some(2_900),
        week_droid_tokens: 24_600,
        droid_state: CoverageState::Complete,
        ..base_cache()
    };
    let lines = cost_panel_lines(&cache);
    let droid_line = &lines[1];
    assert!(
        droid_line.contains("2.9k"),
        "expected 2.9k in droid line, got: {droid_line}"
    );
    assert!(
        droid_line.contains("24.6k"),
        "expected 24.6k in droid line, got: {droid_line}"
    );
}

/// Droid line must never contain a dollar sign.
#[test]
fn droid_line_contains_no_dollar_sign() {
    let cache = CostCache {
        today_credits: Some(100),
        week_credits: Some(500),
        week_droid_tokens: 10_000,
        droid_state: CoverageState::Complete,
        ..base_cache()
    };
    let lines = cost_panel_lines(&cache);
    let droid_line = &lines[1];
    assert!(
        !droid_line.contains('$'),
        "droid line must not contain '$': {droid_line}"
    );
}

/// Panel has_activity returns true when only credits are present (no USD).
#[test]
fn has_activity_true_for_droid_only_credits() {
    let cache = CostCache {
        today_credits: None,
        week_credits: Some(500),
        week_droid_tokens: 0,
        droid_state: CoverageState::Complete,
        ..base_cache()
    };
    assert!(
        cache.has_activity(),
        "expected has_activity=true when week_credits > 0"
    );
}

/// Panel has_activity returns true when only raw droid tokens are present.
#[test]
fn has_activity_true_for_droid_only_tokens() {
    let cache = CostCache {
        week_droid_tokens: 1000,
        droid_state: CoverageState::Partial,
        ..base_cache()
    };
    assert!(
        cache.has_activity(),
        "expected has_activity=true when week_droid_tokens > 0"
    );
}

/// Panel has_activity returns false when everything is zero.
#[test]
fn has_activity_false_when_all_zero() {
    let cache = base_cache();
    assert!(
        !cache.has_activity(),
        "expected has_activity=false for empty cache"
    );
}

/// Partial coverage renders 'credits n/a' when week_credits is None.
#[test]
fn droid_partial_coverage_renders_credits_na() {
    let cache = CostCache {
        week_droid_tokens: 5_000,
        droid_state: CoverageState::Partial,
        ..base_cache() // week_credits: None
    };
    let lines = cost_panel_lines(&cache);
    let droid_line = &lines[1];
    assert!(
        droid_line.contains("credits n/a"),
        "partial line should contain 'credits n/a': {droid_line}"
    );
    assert!(
        droid_line.contains("partial"),
        "partial line should contain 'partial': {droid_line}"
    );
    assert!(
        !droid_line.contains('$'),
        "partial droid line must not contain '$': {droid_line}"
    );
}

/// Regression: global Partial state + valid window credits renders credits (not n/a)
/// and includes "partial source" in the parenthetical.
#[test]
fn partial_global_with_valid_window_credits_renders_credits_and_partial_source() {
    let cache = CostCache {
        today_credits: Some(300),
        week_credits: Some(1_500),
        week_droid_tokens: 8_000,
        droid_state: CoverageState::Partial,
        ..base_cache()
    };
    let lines = cost_panel_lines(&cache);
    let droid_line = &lines[1];
    assert!(
        !droid_line.contains("credits n/a"),
        "should render actual credits when week_credits is Some: {droid_line}"
    );
    assert!(
        droid_line.contains("1.5k"),
        "should show week credit count: {droid_line}"
    );
    assert!(
        droid_line.contains("partial source"),
        "should label partial source in parenthetical: {droid_line}"
    );
    assert!(
        !droid_line.contains('$'),
        "droid line must not contain '$': {droid_line}"
    );
}

/// Regression: global Partial state + missing window credits (None) remains 'credits n/a'.
#[test]
fn partial_global_with_missing_window_credits_renders_na() {
    let cache = CostCache {
        week_droid_tokens: 3_000,
        droid_state: CoverageState::Partial,
        ..base_cache() // week_credits: None
    };
    let lines = cost_panel_lines(&cache);
    let droid_line = &lines[1];
    assert!(
        droid_line.contains("credits n/a"),
        "should render 'credits n/a' when week_credits is None: {droid_line}"
    );
}

#[test]
fn complete_droid_source_without_week_usage_is_not_partial() {
    let cache = CostCache {
        today_cost: 1.0,
        week_cost: 2.0,
        droid_state: CoverageState::Complete,
        ..base_cache()
    };
    let lines = cost_panel_lines(&cache);
    let droid_line = &lines[1];
    assert!(droid_line.contains("no usage in 7d"), "{droid_line}");
    assert!(!droid_line.contains("partial"), "{droid_line}");
    assert!(!droid_line.contains("credits n/a"), "{droid_line}");
}

#[test]
fn complete_droid_source_with_tokens_and_no_credits_is_not_partial() {
    let cache = CostCache {
        week_droid_tokens: 2_000,
        droid_state: CoverageState::Complete,
        ..base_cache()
    };
    let lines = cost_panel_lines(&cache);
    let droid_line = &lines[1];
    assert!(droid_line.contains("credits n/a"), "{droid_line}");
    assert!(droid_line.contains("2.0k raw tokens"), "{droid_line}");
    assert!(!droid_line.contains("partial"), "{droid_line}");
}

/// Without a Droid source, preserve the pre-Droid monitor exactly.
#[test]
fn droid_absent_preserves_legacy_monitor_lines() {
    let cache = CostCache {
        today_cost: 1.23,
        week_cost: 5.67,
        tokens_saved: 100_000,
        efficiency_pct: 25.0,
        top_model: "claude-opus-4".to_string(),
        top_model_cost: 1.11,
        ..base_cache()
    };
    let lines = cost_panel_lines(&cache);
    assert_eq!(lines.len(), 2, "got: {lines:?}");
    assert_eq!(lines[0], "  Spent: $1.23 today | $5.67 7d    Saved: 100.0k");
    assert_eq!(
        lines[1],
        "  Efficiency: 25%    Top model: claude-opus-4 ($1.11)"
    );
    assert!(lines.iter().all(|line| !line.contains("Droid")));
}

/// Top-priced model shows 'n/a' when no model is set.
#[test]
fn efficiency_line_shows_na_when_no_model() {
    let cache = base_cache();
    let lines = cost_panel_lines(&cache);
    let eff_line = lines.last().expect("efficiency line");
    assert!(
        eff_line.contains("n/a"),
        "efficiency line should show n/a when no top model: {eff_line}"
    );
}

/// Narrow-width truncation keeps lines within the character limit.
#[test]
fn narrow_width_truncation_respects_limit() {
    use tokensave::monitor::truncate_to_chars;
    let long_line = "  USD spent: $1.23 today | $5.67 7d    Saved: 100.0k";
    let truncated = truncate_to_chars(long_line, 20);
    assert_eq!(
        truncated.chars().count(),
        20,
        "truncated to 20 chars: {truncated:?}"
    );
}

/// Truncation returns original when within limit.
#[test]
fn truncation_noop_when_within_limit() {
    use tokensave::monitor::truncate_to_chars;
    let line = "short";
    let truncated = truncate_to_chars(line, 80);
    assert_eq!(truncated, line);
}