KeiSeiKit-1.0/_primitives/_rust/kei-frustration-loop/tests/bootstrap_idempotent.rs
Parfii-bot a4e667de10 KeiSeiKit-public — clean state
Single-commit clean baseline after security scrub of niche-tells,
project codenames, internal jargon, and contributor-email leaks.

Contents:
- 100 Rust crates (_primitives/_rust/)
- 37 agent manifests (_manifests/) + generated specs (_generated/)
- 67 user-invocable skills (skills/)
- 33 hooks (hooks/)
- Composition blocks (_blocks/)
- Documentation (docs/, README.md)
- TS adapter packages (_ts_packages/)
- Assembler (_assembler/)
- Roles (_roles/)
- Templates (_templates/)
- Forgejo CI (.forgejo/)

Author: Denis Parfionovich <info@greendragon.info>

License: see LICENSE.
2026-05-01 12:09:03 +08:00

63 lines
2.2 KiB
Rust

//! Bootstrap idempotency: calling `bootstrap` twice must produce the same
//! firmware path on disk and the second call must be a no-op (skipped=true).
use kei_frustration_loop::bootstrap::bootstrap;
use std::fs;
use tempfile::TempDir;
fn write_trace(dir: &std::path::Path, name: &str, body: &str) {
fs::write(dir.join(name), body).unwrap();
}
#[test]
fn second_call_is_a_skip() {
let dir = TempDir::new().unwrap();
let home = dir.path();
let traces = home.join(".claude/memory/traces");
fs::create_dir_all(&traces).unwrap();
write_trace(
&traces,
"session-1.jsonl",
r#"{"type":"user","content":"стоп опять не туда полез"}"#,
);
let first = bootstrap("test-user", &traces, home).unwrap();
assert!(!first.skipped, "first call should NOT be skipped");
assert!(
first.firmware_path.contains("test-user.firmware.gz"),
"firmware_path: {}",
first.firmware_path
);
let p_first = std::path::Path::new(&first.firmware_path);
assert!(p_first.exists(), "firmware file must exist after first call");
let mtime_first = fs::metadata(p_first).unwrap().modified().unwrap();
// Sleep 1.1s so any rewrite would be reflected in mtime.
std::thread::sleep(std::time::Duration::from_millis(1_100));
let second = bootstrap("test-user", &traces, home).unwrap();
assert!(second.skipped, "second call MUST be skipped");
assert_eq!(second.firmware_path, first.firmware_path);
// mtime unchanged — no rewrite occurred.
let mtime_second = fs::metadata(p_first).unwrap().modified().unwrap();
assert_eq!(
mtime_first, mtime_second,
"firmware file must NOT be rewritten on idempotent re-call"
);
}
#[test]
fn empty_traces_dir_still_produces_firmware() {
let dir = TempDir::new().unwrap();
let home = dir.path();
let traces = home.join(".claude/memory/traces");
fs::create_dir_all(&traces).unwrap();
let r = bootstrap("blank-user", &traces, home).unwrap();
assert!(!r.skipped);
assert!(std::path::Path::new(&r.firmware_path).exists());
// No traces ⇒ no hits, but bootstrap still completed.
assert_eq!(r.scanned_traces, 0);
assert_eq!(r.initial_hits, 0);
}