ai-usagebar 1.4.0

Omarchy/Waybar widgets + TUI for tracking multi-provider AI plan usage
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
//! Secure, versioned Nous credential storage.
//!
//! The store is independent from every other provider's credentials.  It uses
//! a sibling lock and same-directory atomic replacement so a refresh either
//! leaves the old complete document or exposes the new complete document.

use std::collections::BTreeMap;
use std::fmt;
use std::fs::{self, File, OpenOptions};
use std::io::{self, Write};
use std::path::{Path, PathBuf};
use std::sync::Arc;

#[cfg(unix)]
use std::os::unix::fs::OpenOptionsExt;

use chrono::{DateTime, Utc};
use fs2::FileExt;
use serde::{Deserialize, Serialize};
use tempfile::Builder;
use thiserror::Error;

pub const CREDENTIAL_STORE_VERSION: u32 = 1;
pub const DEFAULT_CREDENTIALS_FILE: &str = "credentials.json";
pub const CREDENTIALS_DIR_MODE: u32 = 0o700;
pub const CREDENTIALS_FILE_MODE: u32 = 0o600;

#[derive(Debug, Error)]
pub enum CredentialError {
    #[error("credential I/O error at {path}: {source}")]
    Io {
        path: PathBuf,
        #[source]
        source: io::Error,
    },
    #[error("credential document is invalid: {0}")]
    Invalid(String),
    #[error("credential document is unsafe at {path}: {reason}")]
    Unsafe { path: PathBuf, reason: &'static str },
    #[error("credential document could not be decoded")]
    Decode,
    #[error("credential lock could not be acquired at {path}: {source}")]
    Lock {
        path: PathBuf,
        #[source]
        source: io::Error,
    },
}

pub type Result<T> = std::result::Result<T, CredentialError>;

/// A secret-bearing Nous credential.  Do not derive `Debug` for this type.
#[derive(Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct NousCredential {
    pub client_id: String,
    pub access_token: String,
    pub refresh_token: String,
    pub expires_at: DateTime<Utc>,
}

impl fmt::Debug for NousCredential {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("NousCredential")
            .field("client_id", &self.client_id)
            .field("access_token", &"<redacted>")
            .field("refresh_token", &"<redacted>")
            .field("expires_at", &self.expires_at)
            .finish()
    }
}

impl NousCredential {
    pub fn validate(&self) -> Result<()> {
        if self.client_id.trim().is_empty() {
            return Err(CredentialError::Invalid("client_id is empty".into()));
        }
        if self.access_token.trim().is_empty() {
            return Err(CredentialError::Invalid("access token is empty".into()));
        }
        if self.refresh_token.trim().is_empty() {
            return Err(CredentialError::Invalid("refresh token is empty".into()));
        }
        if self.expires_at.timestamp() < 0 {
            return Err(CredentialError::Invalid("expiration is invalid".into()));
        }
        Ok(())
    }
}

/// Versioned top-level credential document.  Unknown top-level values are
/// preserved so logout does not erase future unrelated credential entries.
#[derive(Clone, PartialEq, Serialize, Deserialize)]
pub struct CredentialDocument {
    pub version: u32,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub nous: Option<NousCredential>,
    #[serde(flatten)]
    other: BTreeMap<String, serde_json::Value>,
}

impl fmt::Debug for CredentialDocument {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("CredentialDocument")
            .field("version", &self.version)
            .field("nous", &self.nous.as_ref().map(|_| "<present>"))
            .field("other_keys", &self.other.keys().collect::<Vec<_>>())
            .finish()
    }
}

impl CredentialDocument {
    pub fn new(nous: Option<NousCredential>) -> Self {
        Self {
            version: CREDENTIAL_STORE_VERSION,
            nous,
            other: BTreeMap::new(),
        }
    }

    pub fn validate(&self) -> Result<()> {
        if self.version != CREDENTIAL_STORE_VERSION {
            return Err(CredentialError::Invalid(format!(
                "unsupported credential document version {}",
                self.version
            )));
        }
        if let Some(nous) = &self.nous {
            nous.validate()?;
        }
        Ok(())
    }

    /// Test seam for proving that writes preserve unrelated future entries.
    #[cfg(test)]
    fn insert_other(&mut self, key: impl Into<String>, value: serde_json::Value) {
        self.other.insert(key.into(), value);
    }
}

/// Injectable current-owner lookup.  The trait keeps the UID source separate
/// from store logic and lets tests/coordinators provide a deterministic UID.
pub trait OwnerIdProvider: Send + Sync {
    fn current_uid(&self) -> io::Result<u32>;
}

#[derive(Debug, Default)]
pub struct ProcessOwner;

impl OwnerIdProvider for ProcessOwner {
    fn current_uid(&self) -> io::Result<u32> {
        #[cfg(unix)]
        {
            // Unlike `/proc/self`, this is available on every supported Unix,
            // including macOS, without introducing an unsafe FFI call here.
            Ok(rustix::process::geteuid().as_raw())
        }
        #[cfg(not(unix))]
        {
            Ok(0)
        }
    }
}

/// A file-backed credential store with an injectable path and owner policy.
pub struct CredentialStore {
    path: PathBuf,
    owner: Arc<dyn OwnerIdProvider>,
}

impl fmt::Debug for CredentialStore {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("CredentialStore")
            .field("path", &self.path)
            .finish()
    }
}

impl Clone for CredentialStore {
    fn clone(&self) -> Self {
        Self {
            path: self.path.clone(),
            owner: Arc::clone(&self.owner),
        }
    }
}

impl CredentialStore {
    pub fn at(path: impl Into<PathBuf>) -> Self {
        Self {
            path: path.into(),
            owner: Arc::new(ProcessOwner),
        }
    }

    pub fn with_owner_provider(path: impl Into<PathBuf>, owner: Arc<dyn OwnerIdProvider>) -> Self {
        Self {
            path: path.into(),
            owner,
        }
    }

    pub fn path(&self) -> &Path {
        &self.path
    }

    pub fn lock_path(&self) -> PathBuf {
        let name = self
            .path
            .file_name()
            .and_then(|name| name.to_str())
            .unwrap_or(DEFAULT_CREDENTIALS_FILE);
        self.path.with_file_name(format!("{name}.lock"))
    }

    pub fn acquire_lock(&self) -> Result<CredentialLock> {
        ensure_private_parent(self.parent(), &self.owner)?;
        let path = self.lock_path();
        reject_symlink(&path)?;
        let file = open_private_lock_file(&path).map_err(|source| io_at(&path, source))?;
        validate_file_metadata(&path, &file, &self.owner)?;
        file.lock_exclusive()
            .map_err(|source| CredentialError::Lock {
                path: path.clone(),
                source,
            })?;
        Ok(CredentialLock { file, path })
    }

    pub fn read(&self) -> Result<Option<CredentialDocument>> {
        let _lock = self.acquire_lock()?;
        self.read_unlocked()
    }

    pub fn write(&self, document: &CredentialDocument) -> Result<()> {
        document.validate()?;
        let _lock = self.acquire_lock()?;
        self.write_unlocked(document)
    }

    /// Replace a document while the caller holds this store's exclusive lock.
    /// This is the seam used by the async refresh flow; it never performs an
    /// exchange after releasing the lock.
    pub fn write_locked(&self, lock: &CredentialLock, document: &CredentialDocument) -> Result<()> {
        if lock.path != self.lock_path() {
            return Err(CredentialError::Lock {
                path: self.lock_path(),
                source: io::Error::new(
                    io::ErrorKind::InvalidInput,
                    "lock belongs to another store",
                ),
            });
        }
        document.validate()?;
        self.write_unlocked(document)
    }

    pub fn logout(&self) -> Result<()> {
        let _lock = self.acquire_lock()?;
        let Some(mut document) = self.read_unlocked()? else {
            return Ok(());
        };
        if document.nous.is_none() {
            return Ok(());
        }
        document.nous = None;
        if document.other.is_empty() {
            reject_symlink(&self.path)?;
            fs::remove_file(&self.path).map_err(|source| io_at(&self.path, source))?;
            sync_parent(self.parent())?;
        } else {
            self.write_unlocked(&document)?;
        }
        Ok(())
    }

    pub fn read_unlocked(&self) -> Result<Option<CredentialDocument>> {
        reject_symlink(&self.path)?;
        let metadata = match fs::symlink_metadata(&self.path) {
            Ok(metadata) => metadata,
            Err(error) if error.kind() == io::ErrorKind::NotFound => return Ok(None),
            Err(source) => return Err(io_at(&self.path, source)),
        };
        if !metadata.is_file() {
            return Err(CredentialError::Unsafe {
                path: self.path.clone(),
                reason: "credential path is not a regular file",
            });
        }
        validate_metadata(&self.path, &metadata, &self.owner)?;
        let bytes = fs::read(&self.path).map_err(|source| io_at(&self.path, source))?;
        let document: CredentialDocument =
            serde_json::from_slice(&bytes).map_err(|_| CredentialError::Decode)?;
        document.validate()?;
        Ok(Some(document))
    }

    fn write_unlocked(&self, document: &CredentialDocument) -> Result<()> {
        ensure_private_parent(self.parent(), &self.owner)?;
        reject_symlink(&self.path)?;
        if let Ok(metadata) = fs::symlink_metadata(&self.path) {
            validate_metadata(&self.path, &metadata, &self.owner)?;
        }

        let mut temporary = Builder::new()
            .prefix(".credentials.json.")
            .tempfile_in(self.parent())
            .map_err(|source| io_at(self.parent(), source))?;
        set_private_permissions(temporary.as_file(), CREDENTIALS_FILE_MODE)
            .map_err(|source| io_at(temporary.path(), source))?;
        let bytes = serde_json::to_vec_pretty(document).map_err(|_| {
            CredentialError::Invalid("credential document could not be encoded".into())
        })?;
        temporary
            .as_file_mut()
            .write_all(&bytes)
            .map_err(|source| io_at(temporary.path(), source))?;
        temporary
            .as_file()
            .sync_all()
            .map_err(|source| io_at(temporary.path(), source))?;

        // Recheck immediately before replacing the destination.  The caller
        // holds the sibling lock; a symlink is never intentionally replaced.
        reject_symlink(&self.path)?;
        temporary
            .persist(&self.path)
            .map_err(|error| io_at(&self.path, error.error))?;
        sync_parent(self.parent())?;

        let metadata =
            fs::symlink_metadata(&self.path).map_err(|source| io_at(&self.path, source))?;
        validate_metadata(&self.path, &metadata, &self.owner)
    }

    fn parent(&self) -> &Path {
        self.path.parent().unwrap_or_else(|| Path::new("."))
    }
}

/// An exclusive sibling lock.  Dropping it releases the lock before any
/// caller can observe a subsequent refresh/write operation.
pub struct CredentialLock {
    file: File,
    path: PathBuf,
}

impl fmt::Debug for CredentialLock {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("CredentialLock")
            .field("path", &self.path)
            .finish()
    }
}

impl Drop for CredentialLock {
    fn drop(&mut self) {
        let _ = FileExt::unlock(&self.file);
    }
}

impl Default for CredentialStore {
    fn default() -> Self {
        Self::at(default_credentials_path())
    }
}

pub fn default_credentials_path() -> PathBuf {
    directories::ProjectDirs::from("", "", "ai-usagebar")
        .map(|project| project.config_dir().join(DEFAULT_CREDENTIALS_FILE))
        .unwrap_or_else(|| PathBuf::from(".config/ai-usagebar/credentials.json"))
}

fn ensure_private_parent(path: &Path, owner: &Arc<dyn OwnerIdProvider>) -> Result<()> {
    let created = !path.exists();
    if created {
        fs::create_dir_all(path).map_err(|source| io_at(path, source))?;
    }
    let metadata = fs::symlink_metadata(path).map_err(|source| io_at(path, source))?;
    if !metadata.is_dir() {
        return Err(CredentialError::Unsafe {
            path: path.to_path_buf(),
            reason: "credential parent is not a directory",
        });
    }
    validate_owner(path, &metadata, owner)?;
    #[cfg(unix)]
    {
        let mode = file_mode(&metadata);
        if created {
            set_private_permissions_path(path, CREDENTIALS_DIR_MODE)
                .map_err(|source| io_at(path, source))?;
            let after = fs::symlink_metadata(path).map_err(|source| io_at(path, source))?;
            if file_mode(&after) != CREDENTIALS_DIR_MODE {
                return Err(CredentialError::Unsafe {
                    path: path.to_path_buf(),
                    reason: "new credential parent must be mode 0700",
                });
            }
        } else if mode & 0o022 != 0 {
            // `~/.config/ai-usagebar` predates this credential store and is
            // normally 0755. A current-user-owned, non-writable parent is safe
            // with the credential and lock files themselves fixed at 0600.
            return Err(CredentialError::Unsafe {
                path: path.to_path_buf(),
                reason: "credential parent must not be group- or world-writable",
            });
        }
    }
    Ok(())
}

fn open_private_lock_file(path: &Path) -> io::Result<File> {
    let mut options = OpenOptions::new();
    options.create(true).read(true).write(true);
    #[cfg(unix)]
    options.mode(CREDENTIALS_FILE_MODE);
    options.open(path)
}

fn validate_metadata(
    path: &Path,
    metadata: &fs::Metadata,
    owner: &Arc<dyn OwnerIdProvider>,
) -> Result<()> {
    if metadata.file_type().is_symlink() {
        return Err(CredentialError::Unsafe {
            path: path.to_path_buf(),
            reason: "credential path is a symlink",
        });
    }
    if !metadata.is_file() {
        return Err(CredentialError::Unsafe {
            path: path.to_path_buf(),
            reason: "credential path is not a regular file",
        });
    }
    validate_owner(path, metadata, owner)?;
    #[cfg(unix)]
    {
        if file_mode(metadata) != CREDENTIALS_FILE_MODE {
            return Err(CredentialError::Unsafe {
                path: path.to_path_buf(),
                reason: "credential file must be mode 0600",
            });
        }
    }
    Ok(())
}

fn validate_file_metadata(
    path: &Path,
    file: &File,
    owner: &Arc<dyn OwnerIdProvider>,
) -> Result<()> {
    let metadata = file.metadata().map_err(|source| io_at(path, source))?;
    validate_metadata(path, &metadata, owner)
}

fn validate_owner(
    path: &Path,
    metadata: &fs::Metadata,
    owner: &Arc<dyn OwnerIdProvider>,
) -> Result<()> {
    #[cfg(unix)]
    {
        use std::os::unix::fs::MetadataExt;
        let uid = owner.current_uid().map_err(|source| io_at(path, source))?;
        if metadata.uid() != uid {
            return Err(CredentialError::Unsafe {
                path: path.to_path_buf(),
                reason: "credential path is not owned by the current user",
            });
        }
    }
    #[cfg(not(unix))]
    let _ = (path, metadata, owner);
    Ok(())
}

fn reject_symlink(path: &Path) -> Result<()> {
    match fs::symlink_metadata(path) {
        Ok(metadata) => {
            if metadata.file_type().is_symlink() {
                return Err(CredentialError::Unsafe {
                    path: path.to_path_buf(),
                    reason: "credential path is a symlink",
                });
            }
            Ok(())
        }
        Err(error) if error.kind() == io::ErrorKind::NotFound => Ok(()),
        Err(source) => Err(io_at(path, source)),
    }
}

fn file_mode(metadata: &fs::Metadata) -> u32 {
    #[cfg(unix)]
    {
        use std::os::unix::fs::MetadataExt;
        metadata.mode() & 0o777
    }
    #[cfg(not(unix))]
    {
        let _ = metadata;
        CREDENTIALS_FILE_MODE
    }
}

fn set_private_permissions(file: &File, mode: u32) -> io::Result<()> {
    #[cfg(unix)]
    {
        use std::os::unix::fs::PermissionsExt;
        file.set_permissions(fs::Permissions::from_mode(mode))
    }
    #[cfg(not(unix))]
    {
        let _ = (file, mode);
        Ok(())
    }
}

fn set_private_permissions_path(path: &Path, mode: u32) -> io::Result<()> {
    #[cfg(unix)]
    {
        use std::os::unix::fs::PermissionsExt;
        fs::set_permissions(path, fs::Permissions::from_mode(mode))
    }
    #[cfg(not(unix))]
    {
        let _ = (path, mode);
        Ok(())
    }
}

fn sync_parent(parent: &Path) -> Result<()> {
    #[cfg(unix)]
    {
        File::open(parent)
            .and_then(|directory| directory.sync_all())
            .map_err(|source| io_at(parent, source))
    }
    #[cfg(not(unix))]
    {
        let _ = parent;
        Ok(())
    }
}

fn io_at(path: impl Into<PathBuf>, source: io::Error) -> CredentialError {
    CredentialError::Io {
        path: path.into(),
        source,
    }
}

#[cfg(all(test, unix))]
mod tests {
    use std::fs;
    use std::os::unix::fs::PermissionsExt;
    use std::sync::mpsc;
    use std::thread;
    use std::time::Duration;

    use chrono::{TimeZone, Utc};
    use tempfile::TempDir;

    use super::*;

    fn credential() -> NousCredential {
        NousCredential {
            client_id: "hermes-cli".into(),
            access_token: "test-access-token".into(),
            refresh_token: "test-refresh-token".into(),
            expires_at: Utc.with_ymd_and_hms(2026, 8, 16, 12, 0, 0).unwrap(),
        }
    }

    fn document() -> CredentialDocument {
        CredentialDocument::new(Some(credential()))
    }

    fn private_path(root: &TempDir) -> std::path::PathBuf {
        let parent = root.path().join("config");
        fs::create_dir(&parent).unwrap();
        fs::set_permissions(&parent, fs::Permissions::from_mode(0o700)).unwrap();
        parent.join("credentials.json")
    }

    #[test]
    fn write_creates_private_directory_and_file() {
        let root = TempDir::new().unwrap();
        let path = root.path().join("config").join("credentials.json");
        let store = CredentialStore::at(&path);

        store.write(&document()).unwrap();

        assert_eq!(
            fs::metadata(path.parent().unwrap())
                .unwrap()
                .permissions()
                .mode()
                & 0o777,
            0o700
        );
        assert_eq!(
            fs::symlink_metadata(&path).unwrap().permissions().mode() & 0o777,
            0o600
        );
        assert!(store.read().unwrap().unwrap().nous.is_some());
    }

    #[test]
    fn atomic_replacement_leaves_complete_private_json_without_temp_files() {
        let root = TempDir::new().unwrap();
        let path = private_path(&root);
        let store = CredentialStore::at(&path);
        store.write(&document()).unwrap();

        let mut replacement = document();
        replacement.nous.as_mut().unwrap().access_token = "test-new-access".into();
        store.write(&replacement).unwrap();

        let bytes = fs::read(&path).unwrap();
        let text = String::from_utf8(bytes).unwrap();
        assert!(text.contains("test-new-access"));
        assert_eq!(
            fs::metadata(&path).unwrap().permissions().mode() & 0o777,
            0o600
        );
        assert!(!fs::read_dir(path.parent().unwrap()).unwrap().any(|entry| {
            entry
                .unwrap()
                .file_name()
                .to_string_lossy()
                .starts_with(".credentials.json.")
        }));
    }

    #[test]
    fn unsafe_existing_file_is_rejected_without_chmodifying_it() {
        let root = TempDir::new().unwrap();
        let path = private_path(&root);
        fs::write(&path, b"{}").unwrap();
        fs::set_permissions(&path, fs::Permissions::from_mode(0o644)).unwrap();
        let store = CredentialStore::at(&path);

        assert!(store.read().is_err());
        assert_eq!(
            fs::metadata(&path).unwrap().permissions().mode() & 0o777,
            0o644
        );
    }

    #[test]
    fn symlink_credential_path_is_rejected() {
        let root = TempDir::new().unwrap();
        let target = root.path().join("target.json");
        let path = private_path(&root);
        fs::write(&target, b"{}").unwrap();
        std::os::unix::fs::symlink(&target, &path).unwrap();

        assert!(CredentialStore::at(&path).read().is_err());
        assert!(CredentialStore::at(&path).write(&document()).is_err());
    }

    #[test]
    fn malformed_or_wrong_version_documents_fail_closed_and_logout_preserves_other_fields() {
        let root = TempDir::new().unwrap();
        let path = private_path(&root);
        let store = CredentialStore::at(&path);
        fs::write(&path, br#"{"version":2,"other":{"keep":true}}"#).unwrap();
        fs::set_permissions(&path, fs::Permissions::from_mode(0o600)).unwrap();
        assert!(store.read().is_err());
        assert!(store.logout().is_err());

        let mut doc = document();
        doc.insert_other("future", serde_json::json!({"keep": true}));
        store.write(&doc).unwrap();
        store.logout().unwrap();
        let json: serde_json::Value = serde_json::from_slice(&fs::read(&path).unwrap()).unwrap();
        assert!(json.get("nous").is_none());
        assert_eq!(json["future"]["keep"], true);
    }

    #[test]
    fn lock_blocks_a_second_holder_until_the_first_is_released() {
        let root = TempDir::new().unwrap();
        let store = CredentialStore::at(private_path(&root));
        let first = store.acquire_lock().unwrap();
        let (started_tx, started_rx) = mpsc::channel();
        let (finished_tx, finished_rx) = mpsc::channel();
        let path = store.path().to_path_buf();
        let handle = thread::spawn(move || {
            let other = CredentialStore::at(path);
            started_tx.send(()).unwrap();
            let _second = other.acquire_lock().unwrap();
            finished_tx.send(()).unwrap();
        });
        started_rx.recv_timeout(Duration::from_secs(1)).unwrap();
        assert!(
            finished_rx
                .recv_timeout(Duration::from_millis(100))
                .is_err()
        );
        drop(first);
        finished_rx.recv_timeout(Duration::from_secs(1)).unwrap();
        handle.join().unwrap();
    }

    #[test]
    fn existing_owner_directory_with_standard_config_mode_is_accepted() {
        let root = TempDir::new().unwrap();
        let parent = root.path().join("config");
        fs::create_dir(&parent).unwrap();
        fs::set_permissions(&parent, fs::Permissions::from_mode(0o755)).unwrap();
        let store = CredentialStore::at(parent.join("credentials.json"));

        store.write(&document()).unwrap();
        assert_eq!(
            fs::metadata(&parent).unwrap().permissions().mode() & 0o777,
            0o755
        );
        assert!(store.read().unwrap().unwrap().nous.is_some());
    }

    #[test]
    fn group_or_world_writable_parent_is_rejected_without_chmodifying_it() {
        let root = TempDir::new().unwrap();
        let parent = root.path().join("config");
        fs::create_dir(&parent).unwrap();
        fs::set_permissions(&parent, fs::Permissions::from_mode(0o770)).unwrap();
        let store = CredentialStore::at(parent.join("credentials.json"));

        assert!(store.write(&document()).is_err());
        assert_eq!(
            fs::metadata(&parent).unwrap().permissions().mode() & 0o777,
            0o770
        );
    }

    #[test]
    fn process_owner_matches_files_created_by_the_effective_user() {
        use std::os::unix::fs::MetadataExt;

        let file = tempfile::tempfile().unwrap();
        assert_eq!(
            ProcessOwner.current_uid().unwrap(),
            file.metadata().unwrap().uid()
        );
    }

    #[test]
    fn secret_bearing_debug_output_contains_no_tokens() {
        let output = format!("{:?}", credential());
        assert!(!output.contains("test-access-token"));
        assert!(!output.contains("test-refresh-token"));
    }
}

#[cfg(all(test, not(unix)))]
mod non_unix_tests {
    use chrono::{TimeZone, Utc};
    use tempfile::TempDir;

    use super::*;

    fn document(access_token: &str) -> CredentialDocument {
        CredentialDocument::new(Some(NousCredential {
            client_id: "hermes-cli".into(),
            access_token: access_token.into(),
            refresh_token: "test-refresh-token".into(),
            expires_at: Utc.with_ymd_and_hms(2026, 8, 16, 12, 0, 0).unwrap(),
        }))
    }

    #[test]
    fn credential_store_writes_replaces_reads_and_logs_out() {
        let root = TempDir::new().unwrap();
        let store = CredentialStore::at(root.path().join("config").join("credentials.json"));

        store.write(&document("first-access-token")).unwrap();
        store.write(&document("second-access-token")).unwrap();
        let stored = store.read().unwrap().unwrap().nous.unwrap();
        assert_eq!(stored.access_token, "second-access-token");

        store.logout().unwrap();
        assert!(store.read().unwrap().is_none());
    }
}