rightkit-qa 0.2.2

Rust QA harness for Right Suite apps: engine black-box scenarios, hidden native UI driving through rightkit-control, Rust-test scenario wrapper, dynamic paid-provider mocks, loudness/WAV/PNG/frame validators, run lock, orphan sweep, hashed evidence.
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
//! Scenario wrapper for plain Rust integration tests (`#[test] fn x() { h.scenario(...) }`).
//!
//! What the declarative TOML runner gives a scenario, for Rust tests:
//! * **tier gating**: `fast` always runs; `full`, `paid` (or any name) run when selected by a
//!   cargo feature ([`Harness::with_feature_tiers`]) or the settings key
//!   ([`Harness::with_tier_key`], default `RIGHTKIT_QA_TIER`, a comma list or `all`);
//! * **requires-skip**: `env:NAME` (answered by the [`Settings`] file, so it survives
//!   `rightkit cargo`), `file:path`, `cmd:program`, `platform:darwin|windows`; plus a runtime
//!   [`Scenario::skip`] for prerequisites only discoverable inside the test;
//! * **skip accounting**: libtest reports a skipped test as `ok`, so every outcome is printed
//!   (`[qa] PASS|SKIP|FAIL name`) and appended to `<evidence root>/summary.jsonl`;
//! * **per-scenario evidence**: `<evidence root>/<scenario-slug>/` holds the scenario's files and
//!   a hashed `evidence.json` [`Bundle`] (artifacts recorded with [`Scenario::keep`]);
//! * **orphan check**: every process started through [`Scenario::tracker`] must be gone when the
//!   scenario returns, or the scenario fails (survivors are killed first);
//! * optional **run lock** shared by every test binary ([`Harness::with_run_lock`]).
//!
//! ```no_run
//! use rightkit_qa::harness::Harness;
//! fn h() -> Harness {
//!     Harness::new("myapp", env!("CARGO_MANIFEST_DIR"))
//!         .unwrap()
//!         .with_feature_tiers(&[("full", cfg!(feature = "full")), ("paid", cfg!(feature = "paid"))])
//! }
//! #[test]
//! fn export_is_loud_enough() {
//!     h().scenario("export loudness", "full", &["cmd:ffmpeg"], |sc| {
//!         sc.note("measuring");
//!     });
//! }
//! ```
use crate::evidence::{Bundle, CheckStatus, RunStatus};
use crate::lock::{acquire, LockOptions};
use crate::process::Tracker;
use crate::scenario::{tier_selected, unmet_requirement_with};
use crate::settings::Settings;
use crate::util::{new_id, now_ms, sha256_hex, Result};
use serde_json::{json, Value};
use std::fs;
use std::io::Write;
use std::path::{Path, PathBuf};
use std::sync::Mutex;
use std::time::{Duration, Instant};

/// Settings key read for the tier override unless [`Harness::with_tier_key`] changes it.
pub const DEFAULT_TIER_KEY: &str = "RIGHTKIT_QA_TIER";
/// Settings key read for the evidence root (relative values resolve against the app root).
pub const EVIDENCE_KEY: &str = "RIGHTKIT_QA_EVIDENCE";

/// How a scenario ended without failing (a failure panics, so libtest marks the test failed).
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Outcome {
    Passed,
    Skipped(String),
}

impl Outcome {
    pub fn is_skipped(&self) -> bool {
        matches!(self, Outcome::Skipped(_))
    }
}

/// Shared configuration for every scenario of a test crate. Cheap to rebuild per test.
#[derive(Debug, Clone)]
pub struct Harness {
    app: String,
    root: PathBuf,
    settings: Settings,
    tier_key: String,
    feature_tiers: Vec<String>,
    explicit_tiers: Option<String>,
    evidence_root: PathBuf,
    lock: Option<(PathBuf, Duration)>,
    identity: Value,
    prefix: String,
}

impl Harness {
    /// `app` names the evidence; `root` (usually the repository root) resolves `file:`
    /// requirements and is where [`Settings::discover`] searches.
    pub fn new(app: &str, root: impl Into<PathBuf>) -> Result<Self> {
        let root = root.into();
        let settings = Settings::discover(&root)?;
        Ok(Self::with_parts(app, root, settings))
    }

    /// Like [`Harness::new`] with settings already loaded.
    pub fn with_parts(app: &str, root: PathBuf, settings: Settings) -> Self {
        let evidence_root = settings.get_path(EVIDENCE_KEY, &root).unwrap_or_else(|| {
            root.join(".cache")
                .join("qa")
                .join("evidence")
                .join(format!("rust-{}-{}", now_ms(), std::process::id()))
        });
        Self {
            app: app.to_string(),
            root,
            settings,
            tier_key: DEFAULT_TIER_KEY.into(),
            feature_tiers: vec![],
            explicit_tiers: None,
            evidence_root,
            lock: None,
            identity: Value::Null,
            prefix: "[qa]".into(),
        }
    }

    /// Tiers enabled at compile time: `&[("full", cfg!(feature = "full")), ...]`.
    pub fn with_feature_tiers(mut self, tiers: &[(&str, bool)]) -> Self {
        self.feature_tiers = tiers
            .iter()
            .filter(|(_, on)| *on)
            .map(|(t, _)| t.to_string())
            .collect();
        self
    }

    /// Settings key whose value (comma list or `all`) replaces the computed tier selection.
    pub fn with_tier_key(mut self, key: &str) -> Self {
        self.tier_key = key.into();
        self
    }

    /// Fixed selection, ignoring features and settings.
    pub fn with_tiers(mut self, list: &str) -> Self {
        self.explicit_tiers = Some(list.into());
        self
    }

    pub fn with_evidence_root(mut self, dir: impl Into<PathBuf>) -> Self {
        self.evidence_root = dir.into();
        self
    }

    /// Serialize scenarios across threads and test binaries through a [`crate::lock`] directory.
    pub fn with_run_lock(mut self, lock_dir: impl Into<PathBuf>, timeout: Duration) -> Self {
        self.lock = Some((lock_dir.into(), timeout));
        self
    }

    /// Extra identity fields bound into every evidence bundle (binary digests, revisions).
    /// `config.sha256` defaults to the settings file digest.
    pub fn with_identity(mut self, identity: Value) -> Self {
        self.identity = identity;
        self
    }

    /// Hash a file (the binary under test) into the identity as `binaries.<label>`.
    pub fn with_identity_file(mut self, label: &str, path: &Path) -> Result<Self> {
        let digest = sha256_hex(&fs::read(path)?);
        if !self.identity.is_object() {
            self.identity = json!({});
        }
        self.identity["binaries"][label] = json!({"path": path, "sha256": digest});
        Ok(self)
    }

    /// Log line prefix (default `[qa]`; GenRight greps `[e2e]`).
    pub fn with_log_prefix(mut self, prefix: &str) -> Self {
        self.prefix = prefix.into();
        self
    }

    pub fn settings(&self) -> &Settings {
        &self.settings
    }
    pub fn root(&self) -> &Path {
        &self.root
    }
    pub fn evidence_root(&self) -> &Path {
        &self.evidence_root
    }

    /// The active tier list: explicit, else the settings key, else `fast` plus feature tiers.
    pub fn selected_tiers(&self) -> String {
        if let Some(t) = &self.explicit_tiers {
            return t.clone();
        }
        if let Some(t) = self.settings.get(&self.tier_key) {
            return t;
        }
        let mut t = vec!["fast".to_string()];
        t.extend(self.feature_tiers.iter().cloned());
        t.join(",")
    }

    /// First unmet requirement, with `env:` answered by the settings (environment first).
    pub fn unmet(&self, requires: &[&str]) -> Option<String> {
        let reqs: Vec<String> = requires.iter().map(|s| s.to_string()).collect();
        unmet_requirement_with(&reqs, &self.root, &|k| self.settings.get(k))
    }

    /// Run one scenario. Returns [`Outcome::Passed`] or [`Outcome::Skipped`]; a failing body,
    /// a leaked process, or a lock timeout panics after its evidence is written.
    pub fn scenario(
        &self,
        name: &str,
        tier: &str,
        requires: &[&str],
        f: impl FnOnce(&Scenario),
    ) -> Outcome {
        let started = Instant::now();
        let dir = self.evidence_root.join(slug(name));
        let selected = self.selected_tiers();
        let gate = if !tier_selected(tier, &selected) {
            Some(format!("tier {tier} not selected (selected: {selected})"))
        } else {
            self.unmet(requires)
        };
        if let Some(reason) = gate {
            self.finish(
                name,
                tier,
                &dir,
                CheckStatus::Skipped,
                json!({"reason": reason}),
                &[],
                started,
            );
            return Outcome::Skipped(reason);
        }
        if let Err(e) = fs::create_dir_all(&dir) {
            panic!(
                "{} cannot create evidence dir {}: {e}",
                self.prefix,
                dir.display()
            );
        }
        let mut lock = match &self.lock {
            Some((lock_dir, timeout)) => {
                if let Some(p) = lock_dir.parent() {
                    let _ = fs::create_dir_all(p);
                }
                match acquire(
                    lock_dir,
                    &format!("{}:{name}", self.app),
                    &LockOptions {
                        timeout: *timeout,
                        ..Default::default()
                    },
                ) {
                    Ok(l) => Some(l),
                    Err(e) => {
                        let msg = format!("run lock: {e}");
                        self.finish(
                            name,
                            tier,
                            &dir,
                            CheckStatus::Failed,
                            json!({"error": msg}),
                            &[],
                            started,
                        );
                        panic!("{msg}");
                    }
                }
            }
            None => None,
        };
        let sc = Scenario {
            name: name.to_string(),
            tier: tier.to_string(),
            dir: dir.clone(),
            tracker: Tracker::new(),
            settings: self.settings.clone(),
            artifacts: Mutex::new(vec![]),
            notes: Mutex::new(vec![]),
            skipped: Mutex::new(None),
        };
        let result = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| f(&sc)));
        let orphans = match &result {
            Ok(()) => sc.tracker.assert_no_orphans(),
            Err(_) => {
                sc.tracker.kill_all();
                Ok(())
            }
        };
        if let Some(l) = lock.as_mut() {
            l.release();
        }
        let arts = sc.artifacts.lock().unwrap().clone();
        let notes = sc.notes.lock().unwrap().clone();
        let skipped = sc.skipped.lock().unwrap().clone();
        match (result, orphans) {
            (Ok(()), Ok(())) => match skipped {
                Some(reason) => {
                    self.finish(
                        name,
                        tier,
                        &dir,
                        CheckStatus::Skipped,
                        json!({"reason": reason, "notes": notes}),
                        &arts,
                        started,
                    );
                    Outcome::Skipped(reason)
                }
                None => {
                    self.finish(
                        name,
                        tier,
                        &dir,
                        CheckStatus::Passed,
                        json!({"notes": notes}),
                        &arts,
                        started,
                    );
                    Outcome::Passed
                }
            },
            (Ok(()), Err(e)) => {
                self.finish(
                    name,
                    tier,
                    &dir,
                    CheckStatus::Failed,
                    json!({"error": e.0, "notes": notes}),
                    &arts,
                    started,
                );
                panic!("{e}");
            }
            (Err(p), _) => {
                let msg = p
                    .downcast_ref::<String>()
                    .cloned()
                    .or_else(|| p.downcast_ref::<&str>().map(|s| s.to_string()))
                    .unwrap_or_else(|| "panic".into());
                self.finish(
                    name,
                    tier,
                    &dir,
                    CheckStatus::Failed,
                    json!({"error": msg, "notes": notes}),
                    &arts,
                    started,
                );
                std::panic::resume_unwind(p);
            }
        }
    }

    #[allow(clippy::too_many_arguments)]
    fn finish(
        &self,
        name: &str,
        tier: &str,
        dir: &Path,
        status: CheckStatus,
        mut details: Value,
        arts: &[(String, PathBuf)],
        started: Instant,
    ) {
        let ms = started.elapsed().as_millis() as u64;
        let (word, reason) = match status {
            CheckStatus::Passed => ("PASS", None),
            CheckStatus::Skipped => ("SKIP", details["reason"].as_str().map(String::from)),
            CheckStatus::Failed => ("FAIL", details["error"].as_str().map(String::from)),
        };
        match &reason {
            Some(r) => println!("{} {word} {name}: {r}", self.prefix),
            None => println!("{} {word} {name} ({ms} ms)", self.prefix),
        }
        details["ms"] = json!(ms);
        details["tier"] = json!(tier);
        let evidence = dir.join("evidence.json");
        let written = self.write_bundle(name, status, details, arts, &evidence);
        if let Err(e) = &written {
            println!("{} evidence for {name} not written: {e}", self.prefix);
        }
        let line = json!({
            "name": name, "tier": tier, "status": status, "reason": reason, "ms": ms,
            "evidence": written.ok().map(|_| evidence),
        });
        let _ = fs::create_dir_all(&self.evidence_root);
        if let Ok(mut f) = fs::OpenOptions::new()
            .create(true)
            .append(true)
            .open(self.evidence_root.join("summary.jsonl"))
        {
            let _ = f.write_all(format!("{line}\n").as_bytes());
        }
    }

    fn write_bundle(
        &self,
        name: &str,
        status: CheckStatus,
        details: Value,
        arts: &[(String, PathBuf)],
        out: &Path,
    ) -> Result<()> {
        let platform = crate::util::platform().unwrap_or(std::env::consts::OS);
        let mut b = Bundle::new(
            &self.app,
            platform,
            &format!("{}-{}", std::process::id(), new_id()),
        )?;
        let mut names = vec![];
        let mut problems = vec![];
        for (n, p) in arts {
            match b.add_artifact(n, p) {
                Ok(_) => names.push(n.clone()),
                Err(e) => problems.push(e.0),
            }
        }
        let mut identity = if self.identity.is_object() {
            self.identity.clone()
        } else {
            json!({})
        };
        if identity.get("config").is_none() {
            let digest = self
                .settings
                .source()
                .and_then(|p| fs::read(p).ok())
                .map(|b| sha256_hex(&b))
                .unwrap_or_else(|| sha256_hex(b""));
            identity["config"] = json!({"sha256": digest, "settings": self.settings.source()});
        }
        identity["scenarios"] = json!([name]);
        identity["tiers"] = json!(self.selected_tiers());
        b.set_identity(identity)?;
        let mut details = details;
        if !problems.is_empty() {
            details["artifact_errors"] = json!(problems);
        }
        // An artifact that could not be hashed downgrades a pass: the receipt would be a claim.
        let status = if status == CheckStatus::Passed && !problems.is_empty() {
            CheckStatus::Failed
        } else {
            status
        };
        b.add_check(name, status, details, &names)?;
        b.finish(if status == CheckStatus::Failed {
            RunStatus::Failed
        } else {
            RunStatus::Passed
        });
        b.write(out)
    }
}

/// The handle a scenario body receives.
pub struct Scenario {
    name: String,
    tier: String,
    dir: PathBuf,
    tracker: Tracker,
    settings: Settings,
    artifacts: Mutex<Vec<(String, PathBuf)>>,
    notes: Mutex<Vec<String>>,
    skipped: Mutex<Option<String>>,
}

impl Scenario {
    pub fn name(&self) -> &str {
        &self.name
    }
    pub fn tier(&self) -> &str {
        &self.tier
    }
    /// This scenario's evidence directory (exists; files written here sit beside `evidence.json`).
    pub fn dir(&self) -> &Path {
        &self.dir
    }
    /// Register every process the scenario starts here (`run_owned`, engine targets, sidecars,
    /// or `tracker().register(pid, label)` for anything spawned by hand).
    pub fn tracker(&self) -> &Tracker {
        &self.tracker
    }
    pub fn settings(&self) -> &Settings {
        &self.settings
    }
    /// Mark the scenario skipped from inside (a prerequisite only discoverable at run time);
    /// return from the body afterwards.
    pub fn skip(&self, reason: &str) {
        *self.skipped.lock().unwrap() = Some(reason.to_string());
    }
    /// Record a produced file; it is hashed into the evidence bundle when the scenario ends.
    pub fn keep(&self, name: &str, path: &Path) {
        self.artifacts
            .lock()
            .unwrap()
            .push((name.to_string(), path.to_path_buf()));
    }
    pub fn note(&self, msg: impl Into<String>) {
        self.notes.lock().unwrap().push(msg.into());
    }
    /// A fresh scratch directory outside the evidence tree (short path, OS temp dir).
    pub fn scratch(&self, label: &str) -> PathBuf {
        let d = std::env::temp_dir().join(format!("rkqa-{}-{}", slug(label), &new_id()[..8]));
        let _ = fs::create_dir_all(&d);
        d.canonicalize().unwrap_or(d)
    }
}

/// Filesystem-safe scenario directory name (bounded length, hash suffix keeps it unique).
pub fn slug(name: &str) -> String {
    let base: String = name
        .chars()
        .map(|c| {
            if c.is_ascii_alphanumeric() {
                c.to_ascii_lowercase()
            } else {
                '-'
            }
        })
        .collect::<String>()
        .split('-')
        .filter(|s| !s.is_empty())
        .collect::<Vec<_>>()
        .join("-");
    let short: String = base.chars().take(60).collect();
    format!("{short}-{}", &sha256_hex(name.as_bytes())[..8])
}

/// Read back `summary.jsonl` (one JSON object per finished scenario).
pub fn read_summary(evidence_root: &Path) -> Result<Vec<Value>> {
    let text = fs::read_to_string(evidence_root.join("summary.jsonl"))?;
    text.lines()
        .filter(|l| !l.trim().is_empty())
        .map(|l| Ok(serde_json::from_str(l)?))
        .collect()
}