1use std::fs::{self, File, OpenOptions};
15use std::io::{Read, Write};
16use std::path::{Path, PathBuf};
17use std::time::{Duration, SystemTime};
18
19use fs2::FileExt;
20
21use crate::error::{AUTH_FAILURE_MESSAGE, AppError, Result};
22
23pub const DEFAULT_TTL: Duration = Duration::from_secs(60);
25
26pub const MAX_STALE: Duration = Duration::from_secs(7 * 24 * 3600);
29
30pub const RATE_LIMIT_BACKOFF: Duration = Duration::from_secs(5 * 60);
35
36#[derive(Debug, Clone)]
40pub struct Cache {
41 dir: PathBuf,
42}
43
44impl Cache {
45 pub fn for_vendor(vendor: &str) -> Result<Self> {
48 let base = xdg_cache_dir()?.join("ai-usagebar").join(vendor);
49 Ok(Self { dir: base })
50 }
51
52 pub fn for_vendor_account(vendor: &str, label: &str) -> Result<Self> {
57 let base = xdg_cache_dir()?
58 .join("ai-usagebar")
59 .join(vendor)
60 .join(label);
61 Ok(Self { dir: base })
62 }
63
64 pub fn at(path: PathBuf) -> Self {
66 Self { dir: path }
67 }
68
69 pub fn ensure_dir(&self) -> Result<()> {
71 fs::create_dir_all(&self.dir).map_err(|e| AppError::io_at(&self.dir, e))
72 }
73
74 pub fn dir(&self) -> &Path {
75 &self.dir
76 }
77
78 pub fn payload_path(&self) -> PathBuf {
79 self.dir.join("usage.json")
80 }
81 pub fn stale_path(&self) -> PathBuf {
82 self.dir.join(".stale")
83 }
84 pub fn last_error_path(&self) -> PathBuf {
85 self.dir.join(".last_error")
86 }
87 pub fn lock_path(&self) -> PathBuf {
88 self.dir.join(".fetch.lock")
89 }
90 pub fn retry_after_path(&self) -> PathBuf {
93 self.dir.join(".retry_after")
94 }
95
96 pub fn payload_age(&self) -> Option<Duration> {
99 let meta = fs::metadata(self.payload_path()).ok()?;
100 let mtime = meta.modified().ok()?;
101 SystemTime::now().duration_since(mtime).ok()
102 }
103
104 pub fn fresh_payload(&self, ttl: Duration) -> Result<Option<Vec<u8>>> {
124 self.fresh_payload_at(ttl, SystemTime::now())
125 }
126
127 pub fn fresh_payload_at(&self, ttl: Duration, now: SystemTime) -> Result<Option<Vec<u8>>> {
131 if let Some(remaining) = self.backoff_remaining_at(now) {
132 if self.payload_age().is_some_and(|age| age <= MAX_STALE) {
133 return self.read_payload().map(Some);
134 }
135 return Err(AppError::Http {
136 status: 429,
137 body: format!("rate limited; next attempt in {}", human_backoff(remaining)),
138 });
139 }
140 let Some(age) = self.payload_age() else {
141 return Ok(None);
142 };
143 if age < ttl {
144 self.read_payload().map(Some)
145 } else {
146 Ok(None)
147 }
148 }
149
150 pub fn note_rate_limit_at(&self, now: SystemTime) {
154 let until = now + RATE_LIMIT_BACKOFF;
155 let secs = until
156 .duration_since(SystemTime::UNIX_EPOCH)
157 .map(|d| d.as_secs())
158 .unwrap_or(0);
159 let _ = atomic_write(&self.retry_after_path(), secs.to_string().as_bytes());
160 }
161
162 pub fn clear_backoff(&self) {
165 let _ = fs::remove_file(self.retry_after_path());
166 }
167
168 pub fn backoff_remaining_at(&self, now: SystemTime) -> Option<Duration> {
172 let raw = fs::read_to_string(self.retry_after_path()).ok()?;
173 let secs = raw.trim().parse::<u64>().ok()?;
174 let until = SystemTime::UNIX_EPOCH.checked_add(Duration::from_secs(secs))?;
175 let remaining = until.duration_since(now).ok()?;
176 if remaining.is_zero() {
177 None
178 } else {
179 Some(remaining)
180 }
181 }
182
183 pub fn backoff_remaining(&self) -> Option<Duration> {
185 self.backoff_remaining_at(SystemTime::now())
186 }
187
188 pub fn maybe_payload(&self) -> Result<Option<Vec<u8>>> {
194 if !self.payload_path().exists() {
195 return Ok(None);
196 }
197 self.read_payload().map(Some)
198 }
199
200 pub fn fallback_payload(&self, max_stale: Duration) -> Result<Option<Vec<u8>>> {
206 let Some(age) = self.payload_age() else {
207 return Ok(None);
208 };
209 if age > max_stale {
210 return Ok(None);
211 }
212 self.read_payload().map(Some)
213 }
214
215 fn read_payload(&self) -> Result<Vec<u8>> {
216 let p = self.payload_path();
217 let mut f = File::open(&p).map_err(|e| AppError::io_at(&p, e))?;
218 let mut buf = Vec::new();
219 f.read_to_end(&mut buf)
220 .map_err(|e| AppError::io_at(&p, e))?;
221 Ok(buf)
222 }
223
224 pub fn write_payload(&self, bytes: &[u8]) -> Result<()> {
227 self.ensure_dir()?;
228 let mut tmp = tempfile::Builder::new()
229 .prefix(".usage.")
230 .tempfile_in(&self.dir)
231 .map_err(|e| AppError::io_at(&self.dir, e))?;
232 tmp.write_all(bytes)
233 .map_err(|e| AppError::io_at(tmp.path(), e))?;
234 tmp.as_file_mut()
235 .sync_all()
236 .map_err(|e| AppError::io_at(tmp.path(), e))?;
237 tmp.persist(self.payload_path())
238 .map_err(|e| AppError::io_at(self.payload_path(), e.error))?;
239 let _ = fs::remove_file(self.stale_path());
242 let _ = fs::remove_file(self.last_error_path());
243 self.clear_backoff();
244 Ok(())
245 }
246
247 pub fn mark_stale(&self) {
249 let _ = self.ensure_dir();
250 let _ = File::create(self.stale_path());
251 }
252
253 pub fn is_stale(&self) -> bool {
254 self.stale_path().exists()
255 }
256
257 pub fn write_last_error(&self, code: u16, msg: &str) -> (u16, String) {
269 let _ = self.ensure_dir();
270 let path = self.last_error_path();
271 let msg = if matches!(code, 401 | 403) {
275 AUTH_FAILURE_MESSAGE
276 } else {
277 msg
278 };
279 let msg = crate::display::sanitize_untrusted_field(msg);
280 let body = format!("{code}\n{msg}");
281 let _ = atomic_write(&path, body.as_bytes());
282 if code == 429 {
283 self.note_rate_limit_at(SystemTime::now());
284 }
285 (code, msg)
286 }
287
288 pub fn clear_last_error(&self) {
291 let _ = fs::remove_file(self.last_error_path());
292 self.clear_backoff();
293 }
294
295 pub fn read_last_error(&self) -> Option<(u16, String)> {
296 let raw = fs::read_to_string(self.last_error_path()).ok()?;
297 let (code, msg) = raw.split_once('\n').unwrap_or((raw.as_str(), ""));
303 Some((code.parse::<u16>().ok()?, msg.to_string()))
304 }
305}
306
307fn human_backoff(remaining: Duration) -> String {
312 let secs = remaining.as_secs();
313 if secs < 60 {
314 return format!("{secs}s");
315 }
316 let minutes = secs.div_ceil(60);
317 let (hours, minutes) = (minutes / 60, minutes % 60);
318 match (hours, minutes) {
319 (0, m) => format!("{m}m"),
320 (h, 0) => format!("{h}h"),
321 (h, m) => format!("{h}h {m}m"),
322 }
323}
324
325pub async fn acquire_lock_async(path: &Path, timeout: Duration) -> Result<LockGuard> {
338 let path = path.to_path_buf();
339 tokio::task::spawn_blocking(move || acquire_lock(&path, timeout))
340 .await
341 .map_err(|e| AppError::Other(format!("cache lock task failed: {e}")))?
342}
343
344pub fn acquire_lock(path: &Path, timeout: Duration) -> Result<LockGuard> {
345 if let Some(parent) = path.parent() {
346 fs::create_dir_all(parent).map_err(|e| AppError::io_at(parent, e))?;
347 }
348 let f = OpenOptions::new()
349 .create(true)
350 .read(true)
351 .write(true)
352 .truncate(false)
353 .open(path)
354 .map_err(|e| AppError::io_at(path, e))?;
355
356 let deadline = std::time::Instant::now() + timeout;
357 loop {
358 match f.try_lock_exclusive() {
359 Ok(()) => return Ok(LockGuard { file: f }),
360 Err(_) => {
361 if std::time::Instant::now() >= deadline {
362 return Err(AppError::Other(format!(
363 "cache lock timeout after {:?}",
364 timeout
365 )));
366 }
367 std::thread::sleep(Duration::from_millis(50));
368 }
369 }
370 }
371}
372
373pub struct LockGuard {
377 file: File,
378}
379
380impl Drop for LockGuard {
381 fn drop(&mut self) {
382 let _ = FileExt::unlock(&self.file);
383 }
384}
385
386pub fn atomic_write(path: &Path, bytes: &[u8]) -> Result<()> {
389 let dir = path.parent().ok_or_else(|| {
390 AppError::Other(format!(
391 "atomic_write: path has no parent: {}",
392 path.display()
393 ))
394 })?;
395 fs::create_dir_all(dir).map_err(|e| AppError::io_at(dir, e))?;
396 let mut tmp = tempfile::Builder::new()
397 .prefix(".tmp.")
398 .tempfile_in(dir)
399 .map_err(|e| AppError::io_at(dir, e))?;
400 tmp.write_all(bytes)
401 .map_err(|e| AppError::io_at(tmp.path(), e))?;
402 tmp.as_file_mut()
403 .sync_all()
404 .map_err(|e| AppError::io_at(tmp.path(), e))?;
405 tmp.persist(path)
406 .map_err(|e| AppError::io_at(path, e.error))?;
407 Ok(())
408}
409
410pub(crate) fn xdg_cache_dir() -> Result<PathBuf> {
411 directories::BaseDirs::new()
412 .map(|b| b.cache_dir().to_path_buf())
413 .ok_or_else(|| AppError::Other("could not resolve XDG cache dir (no HOME?)".into()))
414}
415
416pub fn home_dir() -> Result<PathBuf> {
424 directories::BaseDirs::new()
425 .map(|b| b.home_dir().to_path_buf())
426 .ok_or_else(|| AppError::Other("could not resolve home directory (no HOME?)".into()))
427}
428
429#[cfg(test)]
436pub(crate) fn closed_temp_file(name: &str, contents: Option<&str>) -> (tempfile::TempDir, PathBuf) {
437 let dir = tempfile::TempDir::new().unwrap();
438 let path = dir.path().join(name);
439 if let Some(c) = contents {
440 std::fs::write(&path, c).unwrap();
441 }
442 (dir, path)
443}
444
445#[cfg(test)]
446mod tests {
447 use super::*;
448 use tempfile::TempDir;
449
450 fn fixture() -> (TempDir, Cache) {
451 let td = TempDir::new().unwrap();
452 let cache = Cache::at(td.path().join("anthropic"));
453 cache.ensure_dir().unwrap();
454 (td, cache)
455 }
456
457 #[test]
458 fn ensure_dir_is_idempotent() {
459 let (_td, cache) = fixture();
460 cache.ensure_dir().unwrap();
461 cache.ensure_dir().unwrap();
462 assert!(cache.dir().is_dir());
463 }
464
465 #[test]
466 fn write_then_read_round_trip() {
467 let (_td, cache) = fixture();
468 cache.write_payload(b"hello world").unwrap();
469 let got = cache.maybe_payload().unwrap();
470 assert_eq!(got.as_deref(), Some(&b"hello world"[..]));
471 }
472
473 #[test]
474 fn maybe_payload_returns_none_when_missing() {
475 let (_td, cache) = fixture();
476 assert!(cache.maybe_payload().unwrap().is_none());
477 }
478
479 #[test]
480 fn fresh_payload_respects_ttl() {
481 let (_td, cache) = fixture();
482 cache.write_payload(b"x").unwrap();
483 assert!(
485 cache
486 .fresh_payload(Duration::from_secs(10))
487 .unwrap()
488 .is_some()
489 );
490 assert!(
492 cache
493 .fresh_payload(Duration::from_secs(0))
494 .unwrap()
495 .is_none()
496 );
497 }
498
499 #[test]
500 fn write_clears_stale_marker_and_last_error() {
501 let (_td, cache) = fixture();
502 cache.mark_stale();
503 cache.write_last_error(429, "rate limited");
504 assert!(cache.is_stale());
505 assert!(cache.read_last_error().is_some());
506
507 cache.write_payload(b"fresh").unwrap();
508 assert!(!cache.is_stale());
509 assert!(cache.read_last_error().is_none());
510 }
511
512 fn t0() -> SystemTime {
516 SystemTime::UNIX_EPOCH + Duration::from_secs(1_800_000_000)
517 }
518
519 fn arm_backoff_at(cache: &Cache, now: SystemTime) {
520 cache.note_rate_limit_at(now);
521 assert!(cache.retry_after_path().exists());
522 }
523
524 #[test]
525 fn a_429_arms_the_backoff_and_other_statuses_do_not() {
526 let (_td, cache) = fixture();
527 cache.write_last_error(500, "upstream down");
528 assert!(cache.backoff_remaining().is_none());
529 assert!(!cache.retry_after_path().exists());
530
531 cache.write_last_error(429, "slow down");
532 let remaining = cache.backoff_remaining().expect("429 must arm the backoff");
533 assert!(remaining <= RATE_LIMIT_BACKOFF, "{remaining:?}");
536 assert!(
537 remaining >= RATE_LIMIT_BACKOFF - Duration::from_secs(5),
538 "{remaining:?}"
539 );
540 assert_eq!(cache.read_last_error(), Some((429, "slow down".into())));
542 }
543
544 #[test]
545 fn backoff_remaining_counts_down_from_the_injected_clock() {
546 let (_td, cache) = fixture();
547 arm_backoff_at(&cache, t0());
548
549 assert_eq!(cache.backoff_remaining_at(t0()), Some(RATE_LIMIT_BACKOFF));
550 assert_eq!(
551 cache.backoff_remaining_at(t0() + Duration::from_secs(60)),
552 Some(RATE_LIMIT_BACKOFF - Duration::from_secs(60))
553 );
554 assert!(
556 cache
557 .backoff_remaining_at(t0() + RATE_LIMIT_BACKOFF)
558 .is_none()
559 );
560 assert!(
561 cache
562 .backoff_remaining_at(t0() + RATE_LIMIT_BACKOFF + Duration::from_secs(1))
563 .is_none()
564 );
565 }
566
567 #[test]
568 fn during_backoff_with_no_payload_fresh_payload_refuses_the_network() {
569 let (_td, cache) = fixture();
570 arm_backoff_at(&cache, t0());
571
572 let err = cache
573 .fresh_payload_at(DEFAULT_TTL, t0() + Duration::from_secs(19))
574 .expect_err("no payload during backoff must be an error, not a fetch");
575 match err {
576 AppError::Http { status, body } => {
577 assert_eq!(status, 429);
578 assert!(body.contains("next attempt in"), "{body}");
579 assert!(body.ends_with("5m"), "{body}");
581 }
582 other => panic!("expected Http 429, got {other:?}"),
583 }
584 }
585
586 #[test]
591 fn during_backoff_an_expired_but_not_stale_payload_is_served() {
592 let (_td, cache) = fixture();
593 cache.write_payload(b"last good").unwrap();
594 arm_backoff_at(&cache, t0());
595
596 assert!(
598 cache
599 .fresh_payload_at(Duration::ZERO, t0() + RATE_LIMIT_BACKOFF)
600 .unwrap()
601 .is_none()
602 );
603 assert_eq!(
605 cache
606 .fresh_payload_at(Duration::ZERO, t0())
607 .unwrap()
608 .as_deref(),
609 Some(&b"last good"[..])
610 );
611 }
612
613 #[test]
614 fn after_the_backoff_expires_the_ttl_rule_is_back_in_charge() {
615 let (_td, cache) = fixture();
616 cache.write_payload(b"x").unwrap();
617 arm_backoff_at(&cache, t0());
618 let later = t0() + RATE_LIMIT_BACKOFF + Duration::from_secs(1);
619
620 assert!(
621 cache
622 .fresh_payload_at(Duration::from_secs(10), later)
623 .unwrap()
624 .is_some()
625 );
626 assert!(
627 cache
628 .fresh_payload_at(Duration::ZERO, later)
629 .unwrap()
630 .is_none()
631 );
632 fs::remove_file(cache.payload_path()).unwrap();
634 assert!(
635 cache
636 .fresh_payload_at(DEFAULT_TTL, later)
637 .unwrap()
638 .is_none()
639 );
640 }
641
642 #[test]
643 fn a_successful_payload_write_clears_the_backoff() {
644 let (_td, cache) = fixture();
645 arm_backoff_at(&cache, t0());
646 assert!(cache.backoff_remaining_at(t0()).is_some());
647
648 cache.write_payload(b"fresh").unwrap();
649 assert!(cache.backoff_remaining_at(t0()).is_none());
650 assert!(!cache.retry_after_path().exists());
651 }
652
653 #[test]
654 fn clear_last_error_also_clears_the_backoff() {
655 let (_td, cache) = fixture();
656 cache.write_last_error(429, "slow down");
657 assert!(cache.backoff_remaining().is_some());
658
659 cache.clear_last_error();
660 assert!(cache.backoff_remaining().is_none());
661 assert!(!cache.retry_after_path().exists());
662 }
663
664 #[test]
665 fn a_corrupt_retry_after_marker_is_no_backoff() {
666 let (_td, cache) = fixture();
667 for raw in ["", "soon", "-5", "1e9", "12 34"] {
668 fs::write(cache.retry_after_path(), raw).unwrap();
669 assert!(
670 cache.backoff_remaining_at(t0()).is_none(),
671 "{raw:?} must not pin the vendor offline"
672 );
673 assert!(cache.fresh_payload_at(DEFAULT_TTL, t0()).unwrap().is_none());
674 }
675 let until = t0() + Duration::from_secs(90);
678 let secs = until
679 .duration_since(SystemTime::UNIX_EPOCH)
680 .unwrap()
681 .as_secs();
682 fs::write(cache.retry_after_path(), format!("{secs}\n")).unwrap();
683 assert_eq!(
684 cache.backoff_remaining_at(t0()),
685 Some(Duration::from_secs(90))
686 );
687 }
688
689 #[test]
690 fn human_backoff_formats_seconds_minutes_and_hours() {
691 let s = Duration::from_secs;
692 assert_eq!(human_backoff(s(0)), "0s");
693 assert_eq!(human_backoff(s(45)), "45s");
694 assert_eq!(human_backoff(s(59)), "59s");
695 assert_eq!(human_backoff(s(60)), "1m");
696 assert_eq!(human_backoff(s(4 * 60)), "4m");
697 assert_eq!(human_backoff(s(4 * 60 + 1)), "5m");
698 assert_eq!(human_backoff(s(5 * 60)), "5m");
699 assert_eq!(human_backoff(s(60 * 60)), "1h");
700 assert_eq!(human_backoff(s(62 * 60)), "1h 2m");
701 assert_eq!(human_backoff(s(61 * 60 + 30)), "1h 2m");
702 assert_eq!(human_backoff(s(2 * 3600)), "2h");
703 }
704
705 #[test]
706 fn fallback_payload_refuses_a_payload_older_than_the_limit() {
707 let (_td, cache) = fixture();
708 cache.write_payload(b"old").unwrap();
709
710 std::thread::sleep(Duration::from_millis(60));
716
717 assert!(cache.maybe_payload().unwrap().is_some());
720
721 assert!(
725 cache
726 .fallback_payload(Duration::from_millis(5))
727 .unwrap()
728 .is_none()
729 );
730
731 assert_eq!(
734 cache.fallback_payload(MAX_STALE).unwrap().as_deref(),
735 Some(&b"old"[..])
736 );
737 }
738
739 #[test]
740 fn last_error_round_trip() {
741 let (_td, cache) = fixture();
742 cache.write_last_error(503, "service unavailable");
743 let (code, msg) = cache.read_last_error().unwrap();
744 assert_eq!(code, 503);
745 assert_eq!(msg, "service unavailable");
746 }
747
748 #[test]
749 fn last_error_with_empty_message_round_trips() {
750 let (_td, cache) = fixture();
751 cache.write_last_error(429, "");
752 let (code, msg) = cache.read_last_error().unwrap();
753 assert_eq!(code, 429);
754 assert_eq!(msg, "");
755 }
756
757 #[test]
758 fn last_error_replaces_401_body_with_credential_neutral_message() {
759 let (_td, cache) = fixture();
760 cache.write_last_error(401, "PANCEA user@example.test <credential>&token");
761
762 let persisted = fs::read_to_string(cache.last_error_path()).unwrap();
763 assert_eq!(persisted, format!("401\n{AUTH_FAILURE_MESSAGE}"));
764 assert!(!persisted.contains("PANCEA"));
765 assert!(!persisted.contains("<credential>"));
766 }
767
768 #[test]
769 fn last_error_replaces_403_body_with_credential_neutral_message() {
770 let (_td, cache) = fixture();
771 cache.write_last_error(403, "PANCEA account@example.test <credential>&token");
772
773 let persisted = fs::read_to_string(cache.last_error_path()).unwrap();
774 assert_eq!(persisted, format!("403\n{AUTH_FAILURE_MESSAGE}"));
775 assert!(!persisted.contains("PANCEA"));
776 assert!(!persisted.contains("<credential>"));
777 }
778
779 #[test]
785 fn write_last_error_returns_exactly_what_a_later_run_would_read() {
786 for (code, raw) in [
787 (401u16, "PANCEA user@example.test <credential>&token"),
788 (403, "PANCEA account@example.test <credential>&token"),
789 (429, "rate limited, retry in 60s"),
790 (500, "bad\x1b]52;c;Y2FuYXJ5\x07field"),
791 ] {
792 let (_td, cache) = fixture();
793 let returned = cache.write_last_error(code, raw);
794 assert_eq!(
795 returned,
796 cache.read_last_error().unwrap(),
797 "returned pair diverged from the persisted one for {code}"
798 );
799 }
800 }
801
802 #[test]
812 fn no_vendor_builds_a_last_error_pair_from_a_raw_http_body() {
813 let mut sites = Vec::new();
814 for file in crate::guard::rs_files_in("src") {
815 if !file.ends_with("fetch.rs") {
816 continue;
817 }
818 let source = std::fs::read_to_string(&file).expect("readable module");
819 for (n, line) in crate::guard::production_code(&source).lines().enumerate() {
820 if line.contains("(status, body") {
821 sites.push(format!("{}:{}", file.display(), n + 1));
822 }
823 }
824 }
825 assert!(
826 sites.is_empty(),
827 "a last_error pair must be the return of `write_last_error`, which \
828 redacts 401/403 — building one from the raw body puts the response \
829 body in the widget tooltip. Found: {sites:#?}"
830 );
831 }
832
833 #[test]
840 fn only_the_shared_fallback_reads_the_stale_payload() {
841 let mut sites = Vec::new();
842 for file in crate::guard::rs_files_in("src") {
843 if file.ends_with("outcome.rs") || file.ends_with("cache.rs") {
844 continue;
845 }
846 let source = std::fs::read_to_string(&file).expect("readable module");
847 for (n, line) in crate::guard::production_code(&source).lines().enumerate() {
848 if line.contains("fallback_payload(") {
849 sites.push(format!("{}:{}", file.display(), n + 1));
850 }
851 }
852 }
853 assert!(
854 sites.is_empty(),
855 "reach the stale payload through `outcome::fallback`, which decides \
856 what a cold cache means for every vendor at once. Found: {sites:#?}"
857 );
858 }
859
860 #[test]
865 fn the_returned_pair_carries_the_auth_redaction() {
866 for code in [401u16, 403] {
867 let (_td, cache) = fixture();
868 let (returned_code, msg) =
869 cache.write_last_error(code, "PANCEA user@example.test <credential>&token");
870 assert_eq!(returned_code, code);
871 assert_eq!(msg, AUTH_FAILURE_MESSAGE);
872 assert!(!msg.contains("PANCEA"), "{msg}");
873 assert!(!msg.contains("<credential>"), "{msg}");
874 }
875 }
876
877 #[test]
881 fn last_error_round_trips_a_multi_line_message() {
882 let (_td, cache) = fixture();
883 let body = "{\n \"error\": \"quota exhausted\",\n \"retry_after\": 3600\n}";
884 cache.write_last_error(429, body);
885
886 let (code, msg) = cache.read_last_error().unwrap();
887 assert_eq!(code, 429);
888 assert_eq!(msg, body);
889 assert!(
890 msg.contains("quota exhausted"),
891 "message was truncated to its first line: {msg:?}"
892 );
893 }
894
895 #[test]
896 fn last_error_strips_terminal_controls_before_persisting() {
897 let (_td, cache) = fixture();
898 cache.write_last_error(500, "bad\x1b]52;c;Y2FuYXJ5\x07\nnext\tfield");
899
900 let (code, msg) = cache.read_last_error().unwrap();
901 assert_eq!(code, 500);
902 assert_eq!(msg, "bad]52;c;Y2FuYXJ5\nnext field");
903 assert!(
904 msg.contains("Y2FuYXJ5"),
905 "non-auth diagnostic was not preserved"
906 );
907 assert!(!msg.chars().any(|ch| ch.is_control() && ch != '\n'));
908 }
909
910 #[test]
914 fn last_error_reads_files_written_by_the_previous_version() {
915 let (_td, cache) = fixture();
916
917 fs::write(cache.last_error_path(), "503\nservice unavailable").unwrap();
918 assert_eq!(
919 cache.read_last_error(),
920 Some((503, "service unavailable".into()))
921 );
922
923 fs::write(cache.last_error_path(), "429").unwrap();
924 assert_eq!(cache.read_last_error(), Some((429, String::new())));
925
926 fs::write(cache.last_error_path(), "not-a-code\nboom").unwrap();
928 assert!(cache.read_last_error().is_none());
929 }
930
931 #[test]
932 fn lock_serializes_concurrent_acquirers() {
933 let (_td, cache) = fixture();
936 let lock_path = cache.lock_path();
937 let _guard = acquire_lock(&lock_path, Duration::from_millis(500)).unwrap();
938
939 let res = acquire_lock(&lock_path, Duration::from_millis(100));
940 assert!(matches!(res, Err(AppError::Other(_))));
941 }
942
943 #[tokio::test(flavor = "current_thread")]
949 async fn async_lock_does_not_stall_the_runtime() {
950 let (_td, cache) = fixture();
951 let lock_path = cache.lock_path();
952 let _held = acquire_lock(&lock_path, Duration::from_millis(500)).unwrap();
953
954 let waiter = acquire_lock_async(&lock_path, Duration::from_millis(400));
956
957 let mut ticks = 0usize;
959 let ticker = async {
960 let mut iv = tokio::time::interval(Duration::from_millis(20));
961 iv.tick().await;
962 loop {
963 iv.tick().await;
964 ticks += 1;
965 }
966 };
967
968 tokio::select! {
969 res = waiter => {
970 assert!(matches!(res, Err(AppError::Other(_))));
972 }
973 _ = ticker => unreachable!("the ticker loops forever"),
974 }
975 assert!(
976 ticks > 1,
977 "runtime was starved while the lock was contended ({ticks} ticks)"
978 );
979 }
980
981 #[test]
982 fn atomic_write_creates_parent_dirs() {
983 let td = TempDir::new().unwrap();
984 let nested = td.path().join("a/b/c/file.txt");
985 atomic_write(&nested, b"abc").unwrap();
986 assert_eq!(fs::read(&nested).unwrap(), b"abc");
987 }
988}