#![allow(dead_code)]
use std::path::{Path, PathBuf};
use kimun_core::{IndexFile, NoteVault, SystemPath, VaultConfig};
use kimun_notes::components::events::{AppEvent, InputEvent};
use ratatui::Terminal;
use ratatui::backend::TestBackend;
use ratatui::crossterm::event::{KeyCode, KeyEvent, KeyEventKind, KeyEventState, KeyModifiers};
pub const TEST_INDEX_NAME: &str = ".test-index";
pub fn sys<P: AsRef<Path>>(path: P) -> SystemPath {
SystemPath::try_absolute(&path).unwrap_or_else(|e| panic!("test path must be absolute: {e}"))
}
pub fn test_vault_config(dir: &Path) -> VaultConfig {
let root = sys(dir);
let index = IndexFile::in_dir(&root, TEST_INDEX_NAME);
VaultConfig::new(root).with_index(index)
}
pub async fn open_test_vault(dir: &Path) -> NoteVault {
let vault = NoteVault::new(test_vault_config(dir))
.await
.expect("failed to create vault");
vault
.validate_and_init()
.await
.expect("failed to init vault");
vault
}
pub fn absolute(unix_style: &str) -> PathBuf {
let trimmed = unix_style.trim_start_matches('/');
if cfg!(windows) {
PathBuf::from(format!("C:\\{}", trimmed.replace('/', "\\")))
} else {
PathBuf::from(format!("/{trimmed}"))
}
}
pub fn absolute_toml(unix_style: &str) -> String {
absolute(unix_style).to_string_lossy().replace('\\', "\\\\")
}
pub fn press(code: KeyCode, modifiers: KeyModifiers) -> KeyEvent {
KeyEvent {
code,
modifiers,
kind: KeyEventKind::Press,
state: KeyEventState::NONE,
}
}
pub fn input(key: KeyEvent) -> AppEvent {
AppEvent::Input(InputEvent::Key(key))
}
pub fn write_config(config_path: &Path, workspace: &Path) {
let toml = format!(
r#"config_version = 6
[global]
current_workspace = "default"
[workspaces.default]
path = {:?}
created = "2024-01-15T10:30:00Z"
"#,
workspace.to_string_lossy().as_ref()
);
std::fs::write(config_path, toml).expect("failed to write config file");
}
pub fn buffer_text(terminal: &Terminal<TestBackend>) -> String {
terminal
.backend()
.buffer()
.content()
.iter()
.map(|cell| cell.symbol())
.collect()
}