axterminator 0.9.1

macOS GUI testing framework with background testing, sub-millisecond element access, and self-healing locators
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
//! Post-upgrade migration and "what's new" notification.
//!
//! # Version stamp
//!
//! On first run after an upgrade, `check_upgrade()` detects the version
//! change by comparing `~/.axterminator/version.stamp` with the compiled
//! `CARGO_PKG_VERSION`, prints what's new, and updates the stamp.
//!
//! # Migration registry
//!
//! `MIGRATIONS` is intentionally empty — axterminator has minimal config and
//! no persistent state that needs transforming.  The registry is the extension
//! point for future migrations.
//!
//! # Usage
//!
//! ```rust,no_run
//! use axterminator::upgrade::{check_upgrade, UpgradeOptions};
//!
//! // Called on every normal startup:
//! check_upgrade(&UpgradeOptions::default()).unwrap();
//!
//! // Called by `axterminator upgrade`:
//! check_upgrade(&UpgradeOptions { dry_run: true, quiet: false }).unwrap();
//! ```

use std::cmp::Ordering;
use std::path::{Path, PathBuf};

use anyhow::{Context, Result};

// ---------------------------------------------------------------------------
// Version ordering
// ---------------------------------------------------------------------------

/// A parsed semantic version triple `(major, minor, patch)`.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
struct SemVer(u32, u32, u32);

impl SemVer {
    /// Parse `"major.minor.patch"` — ignores pre-release suffixes.
    fn parse(s: &str) -> Option<Self> {
        let mut parts = s.splitn(3, '.').map(|p| {
            // Strip any pre-release suffix (e.g. "1-alpha" -> 1).
            p.split(|c: char| !c.is_ascii_digit())
                .next()
                .and_then(|n| n.parse::<u32>().ok())
        });
        Some(Self(parts.next()??, parts.next()??, parts.next()??))
    }
}

impl std::fmt::Display for SemVer {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}.{}.{}", self.0, self.1, self.2)
    }
}

// ---------------------------------------------------------------------------
// Migration registry (empty framework)
// ---------------------------------------------------------------------------

/// A migration that may run when upgrading from `from_version`.
///
/// Migrations are skipped on dry-run and when `from_version >= since`.
struct Migration {
    /// First version that requires this migration (i.e. stamp < `since`).
    since: SemVer,
    /// Human-readable description shown during upgrade.
    description: &'static str,
    /// The migration function. Called once, returns `Ok(())` on success.
    run: fn() -> Result<()>,
}

/// Registered migrations, sorted ascending by `since`.
///
/// Add entries here when a new version needs a one-time config transform.
/// Keep the list sorted; migrations execute in order.
static MIGRATIONS: &[Migration] = &[
    // Example (disabled):
    //
    // Migration {
    //     since: SemVer(1, 0, 0),
    //     description: "Migrate legacy config key 'foo' → 'bar'",
    //     run: migrate_v1_0_0,
    // },
];

// ---------------------------------------------------------------------------
// What's new
// ---------------------------------------------------------------------------

/// "What's new" entries shown when upgrading to a specific version.
struct WhatsNew {
    /// Version that introduced these changes.
    version: SemVer,
    /// Bullet points shown to the user.
    items: &'static [&'static str],
}

static WHATS_NEW: &[WhatsNew] = &[WhatsNew {
    version: SemVer(0, 9, 0),
    items: &[
        "New `upgrade` command with version stamp and migration framework",
        "Shell completions for `upgrade` subcommand",
    ],
}];

/// Print "what's new" items for all versions strictly after `from`.
fn print_whats_new(from: SemVer, current: SemVer) {
    let items: Vec<_> = WHATS_NEW
        .iter()
        .filter(|w| w.version > from && w.version <= current)
        .flat_map(|w| w.items.iter())
        .collect();

    if items.is_empty() {
        return;
    }

    println!("What's new in v{current}:");
    for item in items {
        println!("  - {item}");
    }
}

// ---------------------------------------------------------------------------
// Stamp file I/O
// ---------------------------------------------------------------------------

/// Path to the version stamp file.
pub fn stamp_path() -> Result<PathBuf> {
    let home = std::env::var("HOME").context("$HOME not set")?;
    Ok(PathBuf::from(home)
        .join(".axterminator")
        .join("version.stamp"))
}

/// Read the version string from the stamp file.
///
/// Returns `None` when the file does not exist (fresh install).
pub fn read_stamp(path: &Path) -> Result<Option<String>> {
    match std::fs::read_to_string(path) {
        Ok(s) => Ok(Some(s.trim().to_owned())),
        Err(e) if e.kind() == std::io::ErrorKind::NotFound => Ok(None),
        Err(e) => Err(e).with_context(|| format!("read stamp {}", path.display())),
    }
}

/// Write `version` to the stamp file, creating parent directories as needed.
pub fn write_stamp(path: &Path, version: &str) -> Result<()> {
    if let Some(parent) = path.parent() {
        std::fs::create_dir_all(parent)
            .with_context(|| format!("create stamp directory {}", parent.display()))?;
    }
    std::fs::write(path, version).with_context(|| format!("write stamp {}", path.display()))
}

// ---------------------------------------------------------------------------
// Public API
// ---------------------------------------------------------------------------

/// Options controlling upgrade behaviour.
#[derive(Debug, Clone, Default)]
pub struct UpgradeOptions {
    /// Print what would happen but do not update the stamp or run migrations.
    pub dry_run: bool,
    /// Suppress all non-error output.
    pub quiet: bool,
}

/// Check the version stamp and act on any version change.
///
/// This is the single entry point for both automatic startup checks and
/// the explicit `axterminator upgrade` command.
///
/// # Behaviour
///
/// | Stamp state        | Action                                           |
/// |--------------------|--------------------------------------------------|
/// | Missing            | Fresh install — write current version, no output |
/// | `stamp == current` | No-op                                            |
/// | `stamp < current`  | Print what's new, run migrations, update stamp   |
/// | `stamp > current`  | Warn about downgrade, no stamp update            |
///
/// On `dry_run`, the stamp is never modified and migrations are skipped.
pub fn check_upgrade(opts: &UpgradeOptions) -> Result<UpgradeOutcome> {
    let path = stamp_path()?;
    let current_str = env!("CARGO_PKG_VERSION");
    let current = SemVer::parse(current_str)
        .with_context(|| format!("cannot parse CARGO_PKG_VERSION {current_str:?}"))?;

    let stamp_raw = read_stamp(&path)?;

    let outcome = match stamp_raw.as_deref() {
        None => handle_fresh_install(&path, current_str, opts)?,
        Some(s) => match SemVer::parse(s) {
            None => handle_corrupt_stamp(&path, s, current_str, opts)?,
            Some(stamp) => match stamp.cmp(&current) {
                Ordering::Equal => UpgradeOutcome::UpToDate,
                Ordering::Less => handle_upgrade(&path, stamp, current, current_str, opts)?,
                Ordering::Greater => handle_downgrade(stamp, current, opts),
            },
        },
    };

    if !opts.quiet {
        print_outcome_summary(&outcome, current_str);
    }

    Ok(outcome)
}

// ---------------------------------------------------------------------------
// Outcome
// ---------------------------------------------------------------------------

/// Result of a `check_upgrade()` call.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum UpgradeOutcome {
    /// First run — stamp did not exist.
    FreshInstall,
    /// Already on the current version.
    UpToDate,
    /// Upgraded from `from` to `to`.
    Upgraded { from: String, to: String },
    /// Stamp version is newer than the binary (downgrade detected).
    Downgrade { stamp: String, binary: String },
    /// Stamp contained an unparseable version string.
    CorruptStamp,
}

// ---------------------------------------------------------------------------
// Handlers
// ---------------------------------------------------------------------------

fn handle_fresh_install(
    path: &Path,
    current: &str,
    opts: &UpgradeOptions,
) -> Result<UpgradeOutcome> {
    if !opts.dry_run {
        write_stamp(path, current)?;
    }
    Ok(UpgradeOutcome::FreshInstall)
}

fn handle_corrupt_stamp(
    path: &Path,
    bad: &str,
    current: &str,
    opts: &UpgradeOptions,
) -> Result<UpgradeOutcome> {
    if !opts.quiet {
        eprintln!(
            "axterminator: warning: version stamp contains unrecognised value {bad:?}, resetting"
        );
    }
    if !opts.dry_run {
        write_stamp(path, current)?;
    }
    Ok(UpgradeOutcome::CorruptStamp)
}

fn handle_upgrade(
    path: &Path,
    from: SemVer,
    to: SemVer,
    to_str: &str,
    opts: &UpgradeOptions,
) -> Result<UpgradeOutcome> {
    if !opts.quiet {
        print_whats_new(from, to);
    }

    for migration in MIGRATIONS.iter().filter(|m| m.since > from) {
        if opts.dry_run {
            if !opts.quiet {
                println!("  [dry-run] would run migration: {}", migration.description);
            }
        } else {
            if !opts.quiet {
                println!("  Running migration: {}", migration.description);
            }
            (migration.run)()?;
        }
    }

    if !opts.dry_run {
        write_stamp(path, to_str)?;
    }

    Ok(UpgradeOutcome::Upgraded {
        from: from.to_string(),
        to: to.to_string(),
    })
}

fn handle_downgrade(stamp: SemVer, binary: SemVer, opts: &UpgradeOptions) -> UpgradeOutcome {
    if !opts.quiet {
        eprintln!(
            "axterminator: warning: stamp version v{stamp} is newer than binary v{binary}\
             downgrade detected. Run `axterminator upgrade` to reset the stamp."
        );
    }
    UpgradeOutcome::Downgrade {
        stamp: stamp.to_string(),
        binary: binary.to_string(),
    }
}

fn print_outcome_summary(outcome: &UpgradeOutcome, current: &str) {
    match outcome {
        UpgradeOutcome::FreshInstall => {
            println!("axterminator v{current} — fresh install, stamp created.");
        }
        UpgradeOutcome::UpToDate
        | UpgradeOutcome::Downgrade { .. }
        | UpgradeOutcome::CorruptStamp => {}
        UpgradeOutcome::Upgraded { from, to } => {
            println!("axterminator upgraded v{from} → v{to}");
        }
    }
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

#[cfg(test)]
mod tests {
    use super::*;
    use tempfile::TempDir;

    fn temp_dir() -> TempDir {
        tempfile::Builder::new()
            .prefix("axt-upgrade-")
            .tempdir()
            .unwrap()
    }

    // ── SemVer parsing ───────────────────────────────────────────────────

    #[test]
    fn semver_parse_standard_triple() {
        // GIVEN: canonical "major.minor.patch"
        // WHEN: parsed
        // THEN: all components extracted
        assert_eq!(SemVer::parse("1.2.3"), Some(SemVer(1, 2, 3)));
    }

    #[test]
    fn semver_parse_strips_prerelease_suffix() {
        // GIVEN: version with pre-release label
        // WHEN: parsed
        // THEN: numeric parts kept, suffix discarded
        assert_eq!(SemVer::parse("0.9.0-alpha"), Some(SemVer(0, 9, 0)));
    }

    #[test]
    fn semver_parse_rejects_garbage() {
        // GIVEN: non-version string
        // WHEN: parsed
        // THEN: None returned
        assert_eq!(SemVer::parse("notaversion"), None);
        assert_eq!(SemVer::parse(""), None);
        assert_eq!(SemVer::parse("1.2"), None);
    }

    #[test]
    fn semver_ordering_is_correct() {
        // GIVEN: two versions
        // WHEN: compared
        // THEN: lexicographic semver order
        assert!(SemVer(1, 0, 0) > SemVer(0, 9, 99));
        assert!(SemVer(0, 9, 1) > SemVer(0, 9, 0));
        assert_eq!(SemVer(1, 2, 3), SemVer(1, 2, 3));
    }

    // ── stamp I/O ────────────────────────────────────────────────────────

    #[test]
    fn read_stamp_returns_none_when_missing() {
        // GIVEN: non-existent path
        let dir = temp_dir();
        let path = dir.path().join("version.stamp");
        // WHEN: read
        // THEN: None (not an error)
        assert_eq!(read_stamp(&path).unwrap(), None);
    }

    #[test]
    fn write_then_read_stamp_roundtrips() {
        // GIVEN: a temp path
        let dir = temp_dir();
        let path = dir.path().join("version.stamp");
        // WHEN: written and read back
        write_stamp(&path, "1.2.3").unwrap();
        // THEN: same string returned
        assert_eq!(read_stamp(&path).unwrap().as_deref(), Some("1.2.3"));
    }

    #[test]
    fn write_stamp_creates_parent_dirs() {
        // GIVEN: nested path whose parents don't exist
        let dir = temp_dir();
        let path = dir.path().join("nested").join("deep").join("version.stamp");
        // WHEN: written
        // THEN: no error, file exists
        write_stamp(&path, "0.1.0").unwrap();
        assert!(path.exists());
    }

    #[test]
    fn write_stamp_strips_trailing_whitespace_on_read() {
        // GIVEN: stamp written with trailing newline (common in editors)
        let dir = temp_dir();
        let path = dir.path().join("version.stamp");
        std::fs::write(&path, "0.9.0\n").unwrap();
        // WHEN: read
        // THEN: whitespace trimmed
        assert_eq!(read_stamp(&path).unwrap().as_deref(), Some("0.9.0"));
    }

    // ── check_upgrade scenarios ──────────────────────────────────────────

    fn opts_quiet_dry() -> UpgradeOptions {
        UpgradeOptions {
            dry_run: true,
            quiet: true,
        }
    }

    fn opts_quiet() -> UpgradeOptions {
        UpgradeOptions {
            dry_run: false,
            quiet: true,
        }
    }

    /// Invoke `check_upgrade` against a specific stamp path (not `~/.axterminator`).
    fn check_with_path(stamp: &Path, opts: &UpgradeOptions) -> Result<UpgradeOutcome> {
        // We call the internal helpers directly to avoid touching the real $HOME.
        let current_str = env!("CARGO_PKG_VERSION");
        let current = SemVer::parse(current_str).unwrap();
        let raw = read_stamp(stamp)?;
        match raw.as_deref() {
            None => handle_fresh_install(stamp, current_str, opts),
            Some(s) => match SemVer::parse(s) {
                None => handle_corrupt_stamp(stamp, s, current_str, opts),
                Some(v) => match v.cmp(&current) {
                    Ordering::Equal => Ok(UpgradeOutcome::UpToDate),
                    Ordering::Less => handle_upgrade(stamp, v, current, current_str, opts),
                    Ordering::Greater => Ok(handle_downgrade(v, current, opts)),
                },
            },
        }
    }

    #[test]
    fn fresh_install_writes_stamp_and_returns_fresh() {
        // GIVEN: no stamp file
        let dir = temp_dir();
        let stamp = dir.path().join("version.stamp");
        // WHEN: check_upgrade runs
        let outcome = check_with_path(&stamp, &opts_quiet()).unwrap();
        // THEN: FreshInstall outcome, stamp written
        assert_eq!(outcome, UpgradeOutcome::FreshInstall);
        assert!(stamp.exists());
    }

    #[test]
    fn fresh_install_dry_run_does_not_write_stamp() {
        // GIVEN: no stamp file, dry_run = true
        let dir = temp_dir();
        let stamp = dir.path().join("version.stamp");
        // WHEN: check_upgrade runs in dry-run mode
        let outcome = check_with_path(&stamp, &opts_quiet_dry()).unwrap();
        // THEN: FreshInstall, but stamp NOT created
        assert_eq!(outcome, UpgradeOutcome::FreshInstall);
        assert!(!stamp.exists());
    }

    #[test]
    fn up_to_date_stamp_is_noop() {
        // GIVEN: stamp == current version
        let dir = temp_dir();
        let stamp = dir.path().join("version.stamp");
        write_stamp(&stamp, env!("CARGO_PKG_VERSION")).unwrap();
        let mtime_before = std::fs::metadata(&stamp).unwrap().modified().unwrap();
        // WHEN: check_upgrade runs
        let outcome = check_with_path(&stamp, &opts_quiet()).unwrap();
        // THEN: UpToDate, stamp untouched
        assert_eq!(outcome, UpgradeOutcome::UpToDate);
        let mtime_after = std::fs::metadata(&stamp).unwrap().modified().unwrap();
        assert_eq!(mtime_before, mtime_after);
    }

    #[test]
    fn older_stamp_triggers_upgrade_and_updates_stamp() {
        // GIVEN: stamp at a version below current
        let dir = temp_dir();
        let stamp = dir.path().join("version.stamp");
        write_stamp(&stamp, "0.0.1").unwrap();
        // WHEN: check_upgrade runs
        let outcome = check_with_path(&stamp, &opts_quiet()).unwrap();
        // THEN: Upgraded outcome, stamp updated to current
        assert!(matches!(outcome, UpgradeOutcome::Upgraded { .. }));
        let new_stamp = read_stamp(&stamp).unwrap().unwrap();
        assert_eq!(new_stamp, env!("CARGO_PKG_VERSION"));
    }

    #[test]
    fn older_stamp_dry_run_does_not_update_stamp() {
        // GIVEN: stamp below current, dry_run = true
        let dir = temp_dir();
        let stamp = dir.path().join("version.stamp");
        write_stamp(&stamp, "0.0.1").unwrap();
        // WHEN: dry-run check_upgrade
        let outcome = check_with_path(&stamp, &opts_quiet_dry()).unwrap();
        // THEN: Upgraded outcome reported, but stamp unchanged
        assert!(matches!(outcome, UpgradeOutcome::Upgraded { .. }));
        let unchanged = read_stamp(&stamp).unwrap().unwrap();
        assert_eq!(unchanged, "0.0.1");
    }

    #[test]
    fn newer_stamp_triggers_downgrade_warning() {
        // GIVEN: stamp at a version far above current
        let dir = temp_dir();
        let stamp = dir.path().join("version.stamp");
        write_stamp(&stamp, "99.0.0").unwrap();
        // WHEN: check_upgrade runs
        let outcome = check_with_path(&stamp, &opts_quiet()).unwrap();
        // THEN: Downgrade outcome
        assert!(matches!(outcome, UpgradeOutcome::Downgrade { .. }));
    }

    #[test]
    fn corrupt_stamp_resets_to_current() {
        // GIVEN: stamp contains garbage
        let dir = temp_dir();
        let stamp = dir.path().join("version.stamp");
        std::fs::write(&stamp, "not-a-version").unwrap();
        // WHEN: check_upgrade runs
        let outcome = check_with_path(&stamp, &opts_quiet()).unwrap();
        // THEN: CorruptStamp, stamp reset to current
        assert_eq!(outcome, UpgradeOutcome::CorruptStamp);
        let reset = read_stamp(&stamp).unwrap().unwrap();
        assert_eq!(reset, env!("CARGO_PKG_VERSION"));
    }

    #[test]
    fn upgrade_outcome_from_contains_old_version() {
        // GIVEN: stamp at 0.0.1
        let dir = temp_dir();
        let stamp = dir.path().join("version.stamp");
        write_stamp(&stamp, "0.0.1").unwrap();
        // WHEN: upgraded
        let outcome = check_with_path(&stamp, &opts_quiet()).unwrap();
        // THEN: from field is "0.0.1"
        match outcome {
            UpgradeOutcome::Upgraded { from, .. } => assert_eq!(from, "0.0.1"),
            other => panic!("expected Upgraded, got {other:?}"),
        }
    }
}