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
//! Cache module - provides file caching with hash-based change detection
//!
//! This implementation follows the TypeScript reference:
//! - First read in a session: returns full content, caches it
//! - Second read (unchanged): returns "[unchanged, X lines, Y tokens saved]"
//! - File changed: returns diff showing what changed
//! - Multi-session: each session tracks independently
use crate::diff::compute_diff;
use crate::error::Error;
use crate::types::{CacheConfig, CacheStats, FileReadResult};
use rusqlite::{params, Connection};
use sha2::{Digest, Sha256};
use std::path::PathBuf;
use tokio::sync::Mutex;
use tracing::{debug, info};
/// CacheStore provides file caching with SHA-256 hash-based change detection
pub struct CacheStore {
config: CacheConfig,
conn: Mutex<Connection>,
}
impl CacheStore {
/// Create a new cache store
pub fn new(config: CacheConfig) -> Result<Self, Error> {
// Ensure the parent directory exists
if let Some(parent) = config.db_path.parent() {
std::fs::create_dir_all(parent)?;
}
let conn = Connection::open(&config.db_path)?;
Ok(Self {
config,
conn: Mutex::new(conn),
})
}
/// Initialize the database schema
pub async fn init(&self) -> Result<(), Error> {
let conn = self.conn.lock().await;
// Enable WAL mode for better concurrent read/write performance.
// WAL allows readers and writers to operate simultaneously without blocking.
conn.execute_batch(
"PRAGMA journal_mode=WAL;
PRAGMA synchronous=NORMAL;",
)?;
// Create file_versions table - stores all versions of files
conn.execute(
"CREATE TABLE IF NOT EXISTS file_versions (
path TEXT NOT NULL,
hash TEXT NOT NULL,
content TEXT NOT NULL,
lines INTEGER NOT NULL,
created_at INTEGER NOT NULL,
PRIMARY KEY (path, hash)
)",
[],
)?;
// Create session_reads table - tracks what each session last saw
conn.execute(
"CREATE TABLE IF NOT EXISTS session_reads (
session_id TEXT NOT NULL,
path TEXT NOT NULL,
hash TEXT NOT NULL,
read_at INTEGER NOT NULL,
PRIMARY KEY (session_id, path)
)",
[],
)?;
// Create stats table - global statistics
conn.execute(
"CREATE TABLE IF NOT EXISTS stats (
key TEXT PRIMARY KEY,
value INTEGER NOT NULL DEFAULT 0
)",
[],
)?;
// Create session_stats table - per-session statistics
conn.execute(
"CREATE TABLE IF NOT EXISTS session_stats (
session_id TEXT NOT NULL,
key TEXT NOT NULL,
value INTEGER NOT NULL DEFAULT 0,
PRIMARY KEY (session_id, key)
)",
[],
)?;
// Initialize stats if not exists.
// Note: files_tracked is now computed via COUNT(DISTINCT path) in get_stats(),
// so we no longer maintain a counter for it.
conn.execute(
"INSERT OR IGNORE INTO stats (key, value) VALUES ('tokens_saved', 0)",
[],
)?;
info!("Cache database initialized at {:?}", self.config.db_path);
Ok(())
}
/// Compute SHA-256 hash of file content.
///
/// Note: The original TypeScript implementation truncates to 16 hex characters
/// (`digest("hex").slice(0, 16)`), i.e. 8 bytes. This Rust version keeps the
/// full 64 hex characters (32 bytes) for stronger collision resistance, at the
/// cost of ~4x more storage per hash in the database.
fn compute_hash(content: &str) -> String {
let mut hasher = Sha256::new();
hasher.update(content.as_bytes());
format!("{:x}", hasher.finalize())
}
/// Estimate tokens from character count.
///
/// Uses ~4 characters per token, which matches the average for GPT-4 and Claude
/// tokenizers on typical source code. Ceiling division ensures non-empty inputs
/// always count as at least 1 token.
///
/// The previous formula (chars * 0.75) overstated token counts by ~3x.
pub fn estimate_tokens(text: &str) -> u64 {
(text.len().div_ceil(4)) as u64
}
/// Increment a global stat counter
fn increment_stat(conn: &Connection, key: &str, amount: i64) -> Result<(), Error> {
conn.execute(
"INSERT INTO stats (key, value) VALUES (?, ?)
ON CONFLICT(key) DO UPDATE SET value = value + ?",
params![key, amount, amount],
)?;
Ok(())
}
/// Increment a session-specific stat counter
fn increment_session_stat(
conn: &Connection,
session_id: &str,
key: &str,
amount: i64,
) -> Result<(), Error> {
conn.execute(
"INSERT INTO session_stats (session_id, key, value) VALUES (?, ?, ?)
ON CONFLICT(session_id, key) DO UPDATE SET value = value + ?",
params![session_id, key, amount, amount],
)?;
Ok(())
}
/// Slice pre-collected lines based on a 1-based range.
/// Accepts the already-collected `&[&str]` to avoid re-splitting content.
fn slice_lines(lines: &[&str], range_start: usize, range_end: usize) -> String {
let start = range_start.saturating_sub(1);
let end = range_end.min(lines.len());
if start >= lines.len() {
String::new()
} else {
lines[start..end].join("\n")
}
}
/// Read a file with caching
///
/// Follows the TypeScript algorithm:
/// 1. Check if this session has read this file before (session_reads table)
/// 2. If not: return full content, cache it
/// 3. If yes and hash matches: return "[unchanged]"
/// 4. If yes and hash differs: compute diff, return diff
pub async fn read_file(
&self,
path: &str,
offset: Option<usize>,
limit: Option<usize>,
force: bool,
) -> Result<FileReadResult, Error> {
// Resolve the file path
let full_path = self.resolve_path(path)?;
// Check if file exists
if !full_path.exists() {
return Err(Error::FileNotFound(path.to_string()));
}
// --- All file I/O and CPU work happens BEFORE acquiring the lock ---
let content = tokio::fs::read_to_string(&full_path).await?;
let hash = Self::compute_hash(&content);
// Collect lines once; reuse for counting, slicing, and token estimation
// instead of calling content.lines().collect() repeatedly in each branch.
let lines: Vec<&str> = content.lines().collect();
let total_lines = lines.len();
let conn = self.conn.lock().await;
let now = std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.unwrap()
.as_secs() as i64;
// Pre-calculate partial read parameters for use in all cases
let is_partial = offset.is_some() || limit.is_some();
let range_start = offset.unwrap_or(1);
let range_end = limit.map(|l| range_start + l - 1).unwrap_or(total_lines);
// Check what this session last saw for this file
let session_last_hash: Option<String> = conn
.query_row(
"SELECT hash FROM session_reads WHERE session_id = ? AND path = ?",
params![self.config.session_id, path],
|row| row.get(0),
)
.ok();
// Check if we have the content for that hash
let cached_content: Option<(String, usize)> = if let Some(ref last_hash) = session_last_hash
{
conn.query_row(
"SELECT content, lines FROM file_versions WHERE path = ? AND hash = ?",
params![path, last_hash],
|row| Ok((row.get(0)?, row.get(1)?)),
)
.ok()
} else {
None
};
// Determine result based on session state
let result = match (force, session_last_hash.as_ref()) {
(true, _) | (false, None) => {
// Case 1: First read in this session OR force=true
debug!(
"First read for session {}: {}",
self.config.session_id, path
);
// Store file version if not exists (content-addressed, so safe to insert duplicate)
conn.execute(
"INSERT OR IGNORE INTO file_versions (path, hash, content, lines, created_at) VALUES (?, ?, ?, ?, ?)",
params![path, hash, content, total_lines, now],
)
?;
// Update session read pointer
conn.execute(
"INSERT OR REPLACE INTO session_reads (session_id, path, hash, read_at) VALUES (?, ?, ?, ?)",
params![self.config.session_id, path, hash, now],
)
?;
// Handle partial reads for first read.
// For non-partial reads, move `content` to avoid a full-string clone.
let result_content = if is_partial {
Self::slice_lines(&lines, range_start, range_end)
} else {
content
};
FileReadResult {
cached: false,
content: result_content,
hash,
total_lines,
lines_changed: None,
diff: None,
}
}
(false, Some(last_hash)) if hash == *last_hash => {
// Case 2: File unchanged from last session read
debug!("Cache hit for session {}: {}", self.config.session_id, path);
// Update session read timestamp
conn.execute(
"UPDATE session_reads SET read_at = ? WHERE session_id = ? AND path = ?",
params![now, self.config.session_id, path],
)?;
// Calculate tokens saved based on what the user actually requested.
// For partial reads, only count tokens for the sliced range (matching TS behavior).
let tokens = if is_partial {
Self::estimate_tokens(&Self::slice_lines(&lines, range_start, range_end))
} else {
Self::estimate_tokens(&content)
};
// Update stats
Self::increment_stat(&conn, "tokens_saved", tokens as i64)?;
Self::increment_session_stat(
&conn,
&self.config.session_id,
"tokens_saved",
tokens as i64,
)?;
// Differentiate the message for partial vs full reads (matching TS behavior)
let label = if is_partial {
format!(
"[cached-context: unchanged, lines {}-{} of {}, {} tokens saved]",
range_start, range_end, total_lines, tokens
)
} else {
format!(
"[cached-context: unchanged, {} lines, {} tokens saved]",
total_lines, tokens
)
};
FileReadResult {
cached: true,
content: label,
hash,
total_lines,
lines_changed: None, // None indicates unchanged (for MCP compatibility)
diff: None,
}
}
(false, Some(_)) => {
// Case 3: File changed from last session read
debug!(
"Cache miss (changed) for session {}: {}",
self.config.session_id, path
);
if let Some((old_content, _)) = cached_content {
// Old version found - compute diff
let diff_result = compute_diff(&old_content, &content, path);
// Check if any changed lines are within the requested range
let changes_in_range = is_partial
&& diff_result
.changed_new_lines
.iter()
.any(|&line| line >= range_start && line <= range_end);
if is_partial && !changes_in_range {
// Changes are outside the requested range - we can return "unchanged in range"
debug!(
"Changes outside range {}-{} for session {}: {}",
range_start, range_end, self.config.session_id, path
);
// Update session read pointer to new hash
conn.execute(
"UPDATE session_reads SET hash = ?, read_at = ? WHERE session_id = ? AND path = ?",
params![hash, now, self.config.session_id, path],
)
?;
// Calculate tokens saved for the partial range (reuse pre-collected lines)
let partial_content = Self::slice_lines(&lines, range_start, range_end);
let tokens = Self::estimate_tokens(&partial_content);
// Update stats
Self::increment_stat(&conn, "tokens_saved", tokens as i64)?;
Self::increment_session_stat(
&conn,
&self.config.session_id,
"tokens_saved",
tokens as i64,
)?;
FileReadResult {
cached: true,
content: format!(
"[cached-context: unchanged in lines {}-{}, changes elsewhere in file, {} tokens saved]",
range_start, range_end, tokens
),
hash,
total_lines,
lines_changed: Some(0),
diff: None,
}
} else {
// Changes are in range (or full read) - store new version and return diff
// Store new version. Borrow &content for the DB insert to avoid cloning.
conn.execute(
"INSERT OR IGNORE INTO file_versions (path, hash, content, lines, created_at) VALUES (?, ?, ?, ?, ?)",
params![path, &hash, &content, total_lines, now],
)
?;
// Update session read pointer to new hash
conn.execute(
"UPDATE session_reads SET hash = ?, read_at = ? WHERE session_id = ? AND path = ?",
params![hash, now, self.config.session_id, path],
)
?;
// Calculate tokens saved (diff is usually smaller than full content)
let full_tokens = Self::estimate_tokens(&content);
let diff_tokens = Self::estimate_tokens(&diff_result.diff);
let tokens_saved = full_tokens.saturating_sub(diff_tokens);
if tokens_saved > 0 {
// Update stats
Self::increment_stat(&conn, "tokens_saved", tokens_saved as i64)?;
Self::increment_session_stat(
&conn,
&self.config.session_id,
"tokens_saved",
tokens_saved as i64,
)?;
}
// For partial reads with changes in range, return just the requested content.
// Reuse pre-collected lines instead of re-splitting content.
let result_content = if is_partial {
Self::slice_lines(&lines, range_start, range_end)
} else {
// Full read with changes - return diff with header
format!(
"[cached-context: {} lines changed out of {}]\n{}",
diff_result.lines_changed, total_lines, diff_result.diff
)
};
// For partial reads with changes in range, mark as not cached
// since we're returning the actual content they requested
FileReadResult {
cached: !is_partial,
content: result_content,
hash,
total_lines,
lines_changed: Some(diff_result.lines_changed),
diff: Some(diff_result.diff),
}
}
} else {
// Old version not found in file_versions - return content as non-cached
// (matching TS behavior: fall through to returning content with cached: false)
debug!(
"Old version not found for session {}: {}",
self.config.session_id, path
);
// Store new version
conn.execute(
"INSERT OR IGNORE INTO file_versions (path, hash, content, lines, created_at) VALUES (?, ?, ?, ?, ?)",
params![path, &hash, &content, total_lines, now],
)
?;
// Update session read pointer to new hash
conn.execute(
"UPDATE session_reads SET hash = ?, read_at = ? WHERE session_id = ? AND path = ?",
params![hash, now, self.config.session_id, path],
)
?;
let result_content = if is_partial {
Self::slice_lines(&lines, range_start, range_end)
} else {
content
};
FileReadResult {
cached: false,
content: result_content,
hash,
total_lines,
lines_changed: None,
diff: None,
}
}
}
};
// Note: Partial reads are now handled within each case above
// Case 1: Sliced in the result construction
// Case 2: Returns "unchanged" message (no slicing needed)
// Case 3: Sliced when changes are in range, or "unchanged in range" message when outside
Ok(result)
}
/// Get cache statistics
pub async fn get_stats(&self) -> Result<CacheStats, Error> {
let conn = self.conn.lock().await;
// Count distinct tracked files directly from file_versions (matching TS behavior).
// This is always accurate, unlike an incrementing counter that could drift.
let files_tracked: i64 = conn
.query_row(
"SELECT COUNT(DISTINCT path) FROM file_versions",
[],
|row| row.get(0),
)
.unwrap_or(0);
let tokens_saved: i64 = conn
.query_row(
"SELECT value FROM stats WHERE key = 'tokens_saved'",
[],
|row| row.get(0),
)
.unwrap_or(0);
// Get session stats
let session_tokens_saved: i64 = conn
.query_row(
"SELECT value FROM session_stats WHERE session_id = ? AND key = 'tokens_saved'",
params![self.config.session_id],
|row| row.get(0),
)
.unwrap_or(0);
Ok(CacheStats {
files_tracked: files_tracked as usize,
tokens_saved: tokens_saved as u64,
session_tokens_saved: session_tokens_saved as u64,
})
}
/// Clear the cache
pub async fn clear(&self) -> Result<(), Error> {
let conn = self.conn.lock().await;
conn.execute("DELETE FROM file_versions", [])?;
conn.execute("DELETE FROM session_reads", [])?;
conn.execute("DELETE FROM stats", [])?;
conn.execute("DELETE FROM session_stats", [])?;
// Re-initialize stats.
// Note: files_tracked is computed via COUNT(DISTINCT path), so clearing
// file_versions above already resets it to 0.
conn.execute(
"INSERT OR IGNORE INTO stats (key, value) VALUES ('tokens_saved', 0)",
[],
)?;
info!("Cache cleared");
Ok(())
}
/// Resolve a path relative to workdir or use as absolute
fn resolve_path(&self, path: &str) -> Result<PathBuf, Error> {
let p = PathBuf::from(path);
if p.is_absolute() {
Ok(p)
} else {
Ok(self.config.workdir.join(p))
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use std::fs;
use tempfile::TempDir;
fn create_test_config(temp_dir: &TempDir) -> CacheConfig {
CacheConfig {
db_path: temp_dir.path().join("test_cache.db"),
session_id: "test-session".to_string(),
workdir: temp_dir.path().to_path_buf(),
}
}
#[tokio::test]
async fn test_cache_new_and_init() {
let temp_dir = TempDir::new().unwrap();
let config = create_test_config(&temp_dir);
let store = CacheStore::new(config.clone()).unwrap();
store.init().await.unwrap();
// Check that database file was created
assert!(config.db_path.exists());
}
#[tokio::test]
async fn test_read_file_first_time() {
let temp_dir = TempDir::new().unwrap();
// Create a test file
let test_file = temp_dir.path().join("test.txt");
fs::write(&test_file, "line1\nline2\nline3\n").unwrap();
let config = create_test_config(&temp_dir);
let store = CacheStore::new(config).unwrap();
store.init().await.unwrap();
let result = store.read_file("test.txt", None, None, false).await.unwrap();
assert!(!result.cached);
assert!(result.hash.len() == 64); // SHA-256 hex is 64 chars
assert_eq!(result.total_lines, 3);
assert!(result.diff.is_none());
assert!(result.content.contains("line1"));
}
#[tokio::test]
async fn test_read_file_unchanged() {
let temp_dir = TempDir::new().unwrap();
// Create a test file
let test_file = temp_dir.path().join("test.txt");
fs::write(&test_file, "line1\nline2\nline3\n").unwrap();
let config = create_test_config(&temp_dir);
let store = CacheStore::new(config).unwrap();
store.init().await.unwrap();
// First read
let result1 = store.read_file("test.txt", None, None, false).await.unwrap();
assert!(!result1.cached);
// Second read - should be cached
let result2 = store.read_file("test.txt", None, None, false).await.unwrap();
assert!(result2.cached);
assert!(result2.content.contains("unchanged"));
assert_eq!(result2.lines_changed, None); // None indicates unchanged
assert_eq!(result1.hash, result2.hash);
}
#[tokio::test]
async fn test_read_file_changed() {
let temp_dir = TempDir::new().unwrap();
// Create a test file
let test_file = temp_dir.path().join("test.txt");
fs::write(&test_file, "line1\nline2\nline3\n").unwrap();
let config = create_test_config(&temp_dir);
let store = CacheStore::new(config).unwrap();
store.init().await.unwrap();
// First read
store.read_file("test.txt", None, None, false).await.unwrap();
// Modify the file
fs::write(&test_file, "line1\nline2 modified\nline3\n").unwrap();
// Read again - should detect change
let result = store.read_file("test.txt", None, None, false).await.unwrap();
assert!(result.cached); // cached = true because we have previous content to diff against
assert!(result.diff.is_some());
assert!(result.lines_changed.unwrap() > 0);
}
#[tokio::test]
async fn test_multi_session_isolation() {
let temp_dir = TempDir::new().unwrap();
let test_file = temp_dir.path().join("test.txt");
fs::write(&test_file, "content").unwrap();
let db_path = temp_dir.path().join("test_cache.db");
// Session 1
let config1 = CacheConfig {
db_path: db_path.clone(),
session_id: "session-1".to_string(),
workdir: temp_dir.path().to_path_buf(),
};
let store1 = CacheStore::new(config1).unwrap();
store1.init().await.unwrap();
// Session 1 reads and caches
let _ = store1.read_file("test.txt", None, None, false).await.unwrap();
// Session 2
let config2 = CacheConfig {
db_path,
session_id: "session-2".to_string(),
workdir: temp_dir.path().to_path_buf(),
};
let store2 = CacheStore::new(config2).unwrap();
store2.init().await.unwrap();
// Session 2 first read should NOT be cached
let result2 = store2.read_file("test.txt", None, None, false).await.unwrap();
assert!(!result2.cached, "Session 2 first read should NOT be cached");
// Session 2 second read should be cached
let result2b = store2.read_file("test.txt", None, None, false).await.unwrap();
assert!(result2b.cached, "Session 2 second read should be cached");
}
#[tokio::test]
async fn test_read_file_partial() {
let temp_dir = TempDir::new().unwrap();
// Create a test file
let test_file = temp_dir.path().join("test.txt");
fs::write(&test_file, "line1\nline2\nline3\nline4\nline5\n").unwrap();
let config = create_test_config(&temp_dir);
let store = CacheStore::new(config).unwrap();
store.init().await.unwrap();
// First read with offset and limit
let result = store
.read_file("test.txt", Some(2), Some(2), false)
.await
.unwrap();
assert_eq!(result.content, "line2\nline3");
assert_eq!(result.total_lines, 5);
}
#[test]
fn test_token_estimation() {
// Test the token estimation formula: ceil(chars / 4)
assert_eq!(CacheStore::estimate_tokens(""), 0);
assert_eq!(CacheStore::estimate_tokens("a"), 1); // ceil(1/4) = 1
assert_eq!(CacheStore::estimate_tokens("abcd"), 1); // ceil(4/4) = 1
assert_eq!(CacheStore::estimate_tokens("abcde"), 2); // ceil(5/4) = 2
assert_eq!(CacheStore::estimate_tokens(&"a".repeat(100)), 25); // ceil(100/4) = 25
assert_eq!(CacheStore::estimate_tokens(&"a".repeat(1000)), 250); // ceil(1000/4) = 250
}
#[tokio::test]
async fn test_get_stats() {
let temp_dir = TempDir::new().unwrap();
let test_file = temp_dir.path().join("test.txt");
fs::write(&test_file, "test content").unwrap();
let config = create_test_config(&temp_dir);
let store = CacheStore::new(config).unwrap();
store.init().await.unwrap();
// Read a file
store.read_file("test.txt", None, None, false).await.unwrap();
// Second read generates token savings
store.read_file("test.txt", None, None, false).await.unwrap();
let stats = store.get_stats().await.unwrap();
assert!(stats.files_tracked > 0);
assert!(stats.session_tokens_saved > 0);
}
#[tokio::test]
async fn test_clear() {
let temp_dir = TempDir::new().unwrap();
let test_file = temp_dir.path().join("test.txt");
fs::write(&test_file, "test content").unwrap();
let config = create_test_config(&temp_dir);
let store = CacheStore::new(config).unwrap();
store.init().await.unwrap();
// Read a file
store.read_file("test.txt", None, None, false).await.unwrap();
// Clear cache
store.clear().await.unwrap();
// Stats should be reset
let stats = store.get_stats().await.unwrap();
assert_eq!(stats.files_tracked, 0);
}
#[tokio::test]
async fn test_force_read() {
let temp_dir = TempDir::new().unwrap();
let test_file = temp_dir.path().join("test.txt");
fs::write(&test_file, "original content").unwrap();
let config = create_test_config(&temp_dir);
let store = CacheStore::new(config).unwrap();
store.init().await.unwrap();
// First read
let result1 = store.read_file("test.txt", None, None, false).await.unwrap();
let hash1 = result1.hash.clone();
// Second read with force=true - should re-read and return full content
let result2 = store.read_file("test.txt", None, None, true).await.unwrap();
// With force=true, we should get a fresh read
assert!(!result2.cached);
assert_eq!(hash1, result2.hash); // Hash should match since content didn't change
}
#[test]
fn test_hash_uniqueness() {
let content1 = "hello world";
let content2 = "hello rust";
let hash1 = CacheStore::compute_hash(content1);
let hash2 = CacheStore::compute_hash(content2);
assert_ne!(hash1, hash2);
assert_eq!(hash1.len(), 64);
assert_eq!(hash2.len(), 64);
}
}