feat(e2e): add test mode infrastructure with BTERMINAL_TEST env isolation
Rust: watcher.rs/fs_watcher.rs skip watchers in test mode, is_test_mode Tauri command. Frontend: wake-scheduler disable, App.svelte test mode detection. AppConfig centralization in bterminal-core (OnceLock pattern for path overrides).
This commit is contained in:
parent
d1a4d9f220
commit
4097253921
18 changed files with 346 additions and 29 deletions
|
|
@ -1,15 +1,27 @@
|
|||
// btmsg — Access to btmsg SQLite database
|
||||
// Database at ~/.local/share/bterminal/btmsg.db (created by btmsg CLI)
|
||||
// Path configurable via init() for test isolation.
|
||||
|
||||
use rusqlite::{params, Connection, OpenFlags};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::path::PathBuf;
|
||||
use std::sync::OnceLock;
|
||||
|
||||
static DB_PATH: OnceLock<PathBuf> = OnceLock::new();
|
||||
|
||||
/// Set the btmsg database path. Must be called before any db access.
|
||||
/// Called from lib.rs setup with AppConfig-resolved path.
|
||||
pub fn init(path: PathBuf) {
|
||||
let _ = DB_PATH.set(path);
|
||||
}
|
||||
|
||||
fn db_path() -> PathBuf {
|
||||
dirs::data_dir()
|
||||
.unwrap_or_else(|| PathBuf::from("."))
|
||||
.join("bterminal")
|
||||
.join("btmsg.db")
|
||||
DB_PATH.get().cloned().unwrap_or_else(|| {
|
||||
dirs::data_dir()
|
||||
.unwrap_or_else(|| PathBuf::from("."))
|
||||
.join("bterminal")
|
||||
.join("btmsg.db")
|
||||
})
|
||||
}
|
||||
|
||||
fn open_db() -> Result<Connection, String> {
|
||||
|
|
|
|||
|
|
@ -1,15 +1,27 @@
|
|||
// bttask — Read access to task board SQLite tables in btmsg.db
|
||||
// Tasks table created by bttask CLI, shared DB with btmsg
|
||||
// Path configurable via init() for test isolation.
|
||||
|
||||
use rusqlite::{params, Connection, OpenFlags};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::path::PathBuf;
|
||||
use std::sync::OnceLock;
|
||||
|
||||
static DB_PATH: OnceLock<PathBuf> = OnceLock::new();
|
||||
|
||||
/// Set the bttask database path. Must be called before any db access.
|
||||
/// Called from lib.rs setup with AppConfig-resolved path.
|
||||
pub fn init(path: PathBuf) {
|
||||
let _ = DB_PATH.set(path);
|
||||
}
|
||||
|
||||
fn db_path() -> PathBuf {
|
||||
dirs::data_dir()
|
||||
.unwrap_or_else(|| PathBuf::from("."))
|
||||
.join("bterminal")
|
||||
.join("btmsg.db")
|
||||
DB_PATH.get().cloned().unwrap_or_else(|| {
|
||||
dirs::data_dir()
|
||||
.unwrap_or_else(|| PathBuf::from("."))
|
||||
.join("bterminal")
|
||||
.join("btmsg.db")
|
||||
})
|
||||
}
|
||||
|
||||
fn open_db() -> Result<Connection, String> {
|
||||
|
|
|
|||
|
|
@ -29,6 +29,11 @@ pub fn open_url(url: String) -> Result<(), String> {
|
|||
Ok(())
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
pub fn is_test_mode() -> bool {
|
||||
std::env::var("BTERMINAL_TEST").map_or(false, |v| v == "1")
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
pub fn frontend_log(level: String, message: String, context: Option<serde_json::Value>) {
|
||||
match level.as_str() {
|
||||
|
|
|
|||
|
|
@ -1,8 +1,10 @@
|
|||
// ctx — Read-only access to the Claude Code context manager database
|
||||
// Database: ~/.claude-context/context.db (managed by ctx CLI tool)
|
||||
// Path configurable via new_with_path() for test isolation.
|
||||
|
||||
use rusqlite::{Connection, params};
|
||||
use serde::Serialize;
|
||||
use std::path::PathBuf;
|
||||
use std::sync::Mutex;
|
||||
|
||||
#[derive(Debug, Clone, Serialize)]
|
||||
|
|
@ -22,10 +24,11 @@ pub struct CtxSummary {
|
|||
|
||||
pub struct CtxDb {
|
||||
conn: Mutex<Option<Connection>>,
|
||||
path: PathBuf,
|
||||
}
|
||||
|
||||
impl CtxDb {
|
||||
fn db_path() -> std::path::PathBuf {
|
||||
fn default_db_path() -> PathBuf {
|
||||
dirs::home_dir()
|
||||
.unwrap_or_default()
|
||||
.join(".claude-context")
|
||||
|
|
@ -33,8 +36,11 @@ impl CtxDb {
|
|||
}
|
||||
|
||||
pub fn new() -> Self {
|
||||
let db_path = Self::db_path();
|
||||
Self::new_with_path(Self::default_db_path())
|
||||
}
|
||||
|
||||
/// Create a CtxDb with a custom database path (for test isolation).
|
||||
pub fn new_with_path(db_path: PathBuf) -> Self {
|
||||
let conn = if db_path.exists() {
|
||||
Connection::open_with_flags(
|
||||
&db_path,
|
||||
|
|
@ -44,12 +50,12 @@ impl CtxDb {
|
|||
None
|
||||
};
|
||||
|
||||
Self { conn: Mutex::new(conn) }
|
||||
Self { conn: Mutex::new(conn), path: db_path }
|
||||
}
|
||||
|
||||
/// Create the context database directory and schema, then open a read-only connection.
|
||||
pub fn init_db(&self) -> Result<(), String> {
|
||||
let db_path = Self::db_path();
|
||||
let db_path = &self.path;
|
||||
|
||||
// Create parent directory
|
||||
if let Some(parent) = db_path.parent() {
|
||||
|
|
@ -136,7 +142,7 @@ impl CtxDb {
|
|||
/// Register a project in the ctx database (creates if not exists).
|
||||
/// Opens a brief read-write connection; the main self.conn stays read-only.
|
||||
pub fn register_project(&self, name: &str, description: &str, work_dir: Option<&str>) -> Result<(), String> {
|
||||
let db_path = Self::db_path();
|
||||
let db_path = &self.path;
|
||||
let conn = Connection::open(&db_path)
|
||||
.map_err(|e| format!("ctx database not found: {e}"))?;
|
||||
|
||||
|
|
@ -257,7 +263,7 @@ mod tests {
|
|||
|
||||
/// Create a CtxDb with conn set to None, simulating a missing database.
|
||||
fn make_missing_db() -> CtxDb {
|
||||
CtxDb { conn: Mutex::new(None) }
|
||||
CtxDb { conn: Mutex::new(None), path: PathBuf::from("/nonexistent/context.db") }
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
|
|
|||
|
|
@ -73,6 +73,12 @@ impl ProjectFsWatcher {
|
|||
project_id: &str,
|
||||
cwd: &str,
|
||||
) -> Result<(), String> {
|
||||
// In test mode, skip inotify watchers to avoid resource contention and flaky events
|
||||
if std::env::var("BTERMINAL_TEST").map_or(false, |v| v == "1") {
|
||||
log::info!("Test mode: skipping fs watcher for project {project_id}");
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
let cwd_path = Path::new(cwd);
|
||||
if !cwd_path.is_dir() {
|
||||
return Err(format!("Not a directory: {cwd}"));
|
||||
|
|
|
|||
|
|
@ -1,8 +1,18 @@
|
|||
// Project group configuration
|
||||
// Reads/writes ~/.config/bterminal/groups.json
|
||||
// Path configurable via init() for test isolation.
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::path::PathBuf;
|
||||
use std::sync::OnceLock;
|
||||
|
||||
static CONFIG_PATH: OnceLock<PathBuf> = OnceLock::new();
|
||||
|
||||
/// Set the groups.json path. Must be called before any config access.
|
||||
/// Called from lib.rs setup with AppConfig-resolved path.
|
||||
pub fn init(path: PathBuf) {
|
||||
let _ = CONFIG_PATH.set(path);
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
|
|
@ -62,10 +72,12 @@ impl Default for GroupsFile {
|
|||
}
|
||||
|
||||
fn config_path() -> PathBuf {
|
||||
dirs::config_dir()
|
||||
.unwrap_or_else(|| PathBuf::from("."))
|
||||
.join("bterminal")
|
||||
.join("groups.json")
|
||||
CONFIG_PATH.get().cloned().unwrap_or_else(|| {
|
||||
dirs::config_dir()
|
||||
.unwrap_or_else(|| PathBuf::from("."))
|
||||
.join("bterminal")
|
||||
.join("groups.json")
|
||||
})
|
||||
}
|
||||
|
||||
pub fn load_groups() -> Result<GroupsFile, String> {
|
||||
|
|
|
|||
|
|
@ -13,6 +13,7 @@ mod session;
|
|||
mod telemetry;
|
||||
mod watcher;
|
||||
|
||||
use bterminal_core::config::AppConfig;
|
||||
use event_sink::TauriEventSink;
|
||||
use pty::PtyManager;
|
||||
use remote::RemoteManager;
|
||||
|
|
@ -32,6 +33,7 @@ pub(crate) struct AppState {
|
|||
pub ctx_db: Arc<ctx::CtxDb>,
|
||||
pub memora_db: Arc<memora::MemoraDb>,
|
||||
pub remote_manager: Arc<RemoteManager>,
|
||||
pub app_config: Arc<AppConfig>,
|
||||
_telemetry: telemetry::TelemetryGuard,
|
||||
}
|
||||
|
||||
|
|
@ -40,9 +42,26 @@ pub fn run() {
|
|||
// Force dark GTK theme for native dialogs (file chooser, etc.)
|
||||
std::env::set_var("GTK_THEME", "Adwaita:dark");
|
||||
|
||||
// Resolve all paths via AppConfig (respects BTERMINAL_TEST_* env vars)
|
||||
let app_config = AppConfig::from_env();
|
||||
if app_config.is_test_mode() {
|
||||
log::info!(
|
||||
"Test mode enabled: data_dir={}, config_dir={}",
|
||||
app_config.data_dir.display(),
|
||||
app_config.config_dir.display()
|
||||
);
|
||||
}
|
||||
|
||||
// Initialize subsystem paths from AppConfig (before any db access)
|
||||
btmsg::init(app_config.btmsg_db_path());
|
||||
bttask::init(app_config.btmsg_db_path());
|
||||
groups::init(app_config.groups_json_path());
|
||||
|
||||
// Initialize tracing + optional OTLP export (before any tracing macros)
|
||||
let telemetry_guard = telemetry::init();
|
||||
|
||||
let app_config_arc = Arc::new(app_config);
|
||||
|
||||
tauri::Builder::default()
|
||||
.invoke_handler(tauri::generate_handler![
|
||||
// PTY
|
||||
|
|
@ -151,6 +170,7 @@ pub fn run() {
|
|||
// Misc
|
||||
commands::misc::cli_get_group,
|
||||
commands::misc::open_url,
|
||||
commands::misc::is_test_mode,
|
||||
commands::misc::frontend_log,
|
||||
])
|
||||
.plugin(tauri_plugin_updater::Builder::new().build())
|
||||
|
|
@ -161,6 +181,8 @@ pub fn run() {
|
|||
// tracing's compatibility layer). Adding plugin-log would panic with
|
||||
// "attempted to set a logger after the logging system was already initialized".
|
||||
|
||||
let config = app_config_arc.clone();
|
||||
|
||||
// Create TauriEventSink for core managers
|
||||
let sink: Arc<dyn bterminal_core::event::EventSink> =
|
||||
Arc::new(TauriEventSink(app.handle().clone()));
|
||||
|
|
@ -178,28 +200,38 @@ pub fn run() {
|
|||
.parent()
|
||||
.unwrap()
|
||||
.to_path_buf();
|
||||
// Forward test mode env vars to sidecar processes
|
||||
let mut env_overrides = std::collections::HashMap::new();
|
||||
if config.is_test_mode() {
|
||||
env_overrides.insert("BTERMINAL_TEST".into(), "1".into());
|
||||
if let Ok(v) = std::env::var("BTERMINAL_TEST_DATA_DIR") {
|
||||
env_overrides.insert("BTERMINAL_TEST_DATA_DIR".into(), v);
|
||||
}
|
||||
if let Ok(v) = std::env::var("BTERMINAL_TEST_CONFIG_DIR") {
|
||||
env_overrides.insert("BTERMINAL_TEST_CONFIG_DIR".into(), v);
|
||||
}
|
||||
}
|
||||
|
||||
let sidecar_config = SidecarConfig {
|
||||
search_paths: vec![
|
||||
resource_dir.join("sidecar"),
|
||||
dev_root.join("sidecar"),
|
||||
],
|
||||
env_overrides,
|
||||
};
|
||||
|
||||
let pty_manager = Arc::new(PtyManager::new(sink.clone()));
|
||||
let sidecar_manager = Arc::new(SidecarManager::new(sink, sidecar_config));
|
||||
|
||||
// Initialize session database
|
||||
let data_dir = dirs::data_dir()
|
||||
.unwrap_or_else(|| std::path::PathBuf::from("."))
|
||||
.join("bterminal");
|
||||
// Initialize session database using AppConfig data_dir
|
||||
let session_db = Arc::new(
|
||||
SessionDb::open(&data_dir).expect("Failed to open session database"),
|
||||
SessionDb::open(config.sessions_db_dir()).expect("Failed to open session database"),
|
||||
);
|
||||
|
||||
let file_watcher = Arc::new(FileWatcherManager::new());
|
||||
let fs_watcher = Arc::new(ProjectFsWatcher::new());
|
||||
let ctx_db = Arc::new(ctx::CtxDb::new());
|
||||
let memora_db = Arc::new(memora::MemoraDb::new());
|
||||
let ctx_db = Arc::new(ctx::CtxDb::new_with_path(config.ctx_db_path.clone()));
|
||||
let memora_db = Arc::new(memora::MemoraDb::new_with_path(config.memora_db_path.clone()));
|
||||
let remote_manager = Arc::new(RemoteManager::new());
|
||||
|
||||
// Start local sidecar
|
||||
|
|
@ -217,6 +249,7 @@ pub fn run() {
|
|||
ctx_db,
|
||||
memora_db,
|
||||
remote_manager,
|
||||
app_config: config,
|
||||
_telemetry: telemetry_guard,
|
||||
});
|
||||
|
||||
|
|
|
|||
|
|
@ -26,7 +26,7 @@ pub struct MemoraDb {
|
|||
}
|
||||
|
||||
impl MemoraDb {
|
||||
fn db_path() -> std::path::PathBuf {
|
||||
fn default_db_path() -> std::path::PathBuf {
|
||||
dirs::data_dir()
|
||||
.unwrap_or_else(|| dirs::home_dir().unwrap_or_default().join(".local/share"))
|
||||
.join("memora")
|
||||
|
|
@ -34,8 +34,11 @@ impl MemoraDb {
|
|||
}
|
||||
|
||||
pub fn new() -> Self {
|
||||
let db_path = Self::db_path();
|
||||
Self::new_with_path(Self::default_db_path())
|
||||
}
|
||||
|
||||
/// Create a MemoraDb with a custom database path (for test isolation).
|
||||
pub fn new_with_path(db_path: std::path::PathBuf) -> Self {
|
||||
let conn = if db_path.exists() {
|
||||
Connection::open_with_flags(
|
||||
&db_path,
|
||||
|
|
|
|||
|
|
@ -34,8 +34,11 @@ pub fn init() -> TelemetryGuard {
|
|||
.with_target(true)
|
||||
.compact();
|
||||
|
||||
// In test mode, never export telemetry (avoid contaminating production data)
|
||||
let is_test = std::env::var("BTERMINAL_TEST").map_or(false, |v| v == "1");
|
||||
|
||||
match std::env::var("BTERMINAL_OTLP_ENDPOINT") {
|
||||
Ok(endpoint) if !endpoint.is_empty() => {
|
||||
Ok(endpoint) if !endpoint.is_empty() && !is_test => {
|
||||
match build_otlp_provider(&endpoint) {
|
||||
Ok(provider) => {
|
||||
let otel_layer = tracing_opentelemetry::layer()
|
||||
|
|
|
|||
|
|
@ -37,6 +37,12 @@ impl FileWatcherManager {
|
|||
pane_id: &str,
|
||||
path: &str,
|
||||
) -> Result<String, String> {
|
||||
// In test mode, skip file watching to avoid inotify noise and flaky events
|
||||
if std::env::var("BTERMINAL_TEST").map_or(false, |v| v == "1") {
|
||||
return std::fs::read_to_string(path)
|
||||
.map_err(|e| format!("Failed to read file: {e}"));
|
||||
}
|
||||
|
||||
let file_path = PathBuf::from(path);
|
||||
if !file_path.exists() {
|
||||
return Err(format!("File not found: {path}"));
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue