#![allow(dead_code)]
use std::path::{Path, PathBuf};
use tempfile::TempDir;
use vibe_coding_tracker::utils::{HelperPaths, resolve_paths_from_home};
pub fn fixture(name: &str) -> PathBuf {
Path::new(env!("CARGO_MANIFEST_DIR"))
.join("examples")
.join(name)
}
pub fn fixture_str(name: &str) -> String {
std::fs::read_to_string(fixture(name))
.unwrap_or_else(|e| panic!("failed to read fixture {name}: {e}"))
}
pub fn append_cursor_json_blob(path: &Path, id: &str) {
let connection = rusqlite::Connection::open(path).expect("open Cursor store");
let payload = serde_json::json!({
"role": "assistant",
"content": [],
"padding": "x".repeat(8 * 1024),
})
.to_string()
.into_bytes();
connection
.execute(
"INSERT INTO blobs (id, data) VALUES (?1, ?2)",
rusqlite::params![id, payload],
)
.expect("append Cursor JSON blob");
}
pub struct TempHome {
pub dir: TempDir,
pub paths: HelperPaths,
}
impl Default for TempHome {
fn default() -> Self {
Self::new()
}
}
impl TempHome {
pub fn new() -> Self {
let dir = TempDir::new().expect("create temp home");
let paths = resolve_paths_from_home(dir.path());
Self { dir, paths }
}
pub fn home(&self) -> &Path {
self.dir.path()
}
fn write_file(path: &Path, content: &str) {
if let Some(parent) = path.parent() {
std::fs::create_dir_all(parent).expect("create parent dirs");
}
std::fs::write(path, content).expect("write file");
}
pub fn put_claude_session(&self, project: &str, file: &str, content: &str) -> PathBuf {
let p = self.paths.claude_session_dir.join(project).join(file);
Self::write_file(&p, content);
p
}
pub fn put_codex_session(&self, rel: &str, content: &str) -> PathBuf {
let p = self.paths.codex_session_dir.join(rel);
Self::write_file(&p, content);
p
}
pub fn put_gemini_session(&self, project: &str, file: &str, content: &str) -> PathBuf {
let p = self
.paths
.gemini_session_dir
.join(project)
.join("chats")
.join(file);
Self::write_file(&p, content);
p
}
pub fn put_grok_session(&self, workspace: &str, session: &str, content: &str) -> PathBuf {
let p = self
.paths
.grok_session_dir
.join(workspace)
.join(session)
.join("signals.json");
Self::write_file(&p, content);
p
}
pub fn put_grok_fixture_session(&self, workspace: &str, session: &str) -> PathBuf {
let signals = self.put_grok_session(
workspace,
session,
&fixture_str("grok_session/signals.json"),
);
let dir = signals.parent().expect("Grok fixture session directory");
Self::write_file(
&dir.join("summary.json"),
&fixture_str("grok_session/summary.json"),
);
Self::write_file(
&dir.join("updates.jsonl"),
&fixture_str("grok_session/updates.jsonl"),
);
signals
}
pub fn put_cursor_session(
&self,
project: &str,
conversation: &str,
model: &str,
timestamp_ms: i64,
context_tokens: i64,
) -> PathBuf {
fn varint(mut value: u64, out: &mut Vec<u8>) {
loop {
let mut byte = (value & 0x7f) as u8;
value >>= 7;
if value != 0 {
byte |= 0x80;
}
out.push(byte);
if value == 0 {
break;
}
}
}
fn tag(field: u64, wire: u64, out: &mut Vec<u8>) {
varint((field << 3) | wire, out);
}
let message = r#"{"role":"assistant","content":[{"type":"tool-call","toolName":"Write","toolCallId":"write","args":{"path":"/repo/output.rs","contents":"first\nsecond"}}]}"#;
let mut node = Vec::new();
tag(1, 2, &mut node);
varint(1, &mut node);
node.push(0);
tag(4, 2, &mut node);
varint(message.len() as u64, &mut node);
node.extend_from_slice(message.as_bytes());
let mut context = Vec::new();
tag(1, 0, &mut context);
varint(context_tokens as u64, &mut context);
tag(5, 2, &mut node);
varint(context.len() as u64, &mut node);
node.extend_from_slice(&context);
tag(26, 0, &mut node);
varint(timestamp_ms as u64, &mut node);
let path = self
.paths
.cursor_chats_dir
.join(project)
.join(conversation)
.join("store.db");
std::fs::create_dir_all(path.parent().unwrap()).expect("create Cursor store directory");
let connection = rusqlite::Connection::open(&path).expect("create Cursor store");
connection
.execute_batch(
"CREATE TABLE blobs (id TEXT PRIMARY KEY, data BLOB); \
CREATE TABLE meta (key TEXT PRIMARY KEY, value TEXT);",
)
.expect("create Cursor store schema");
connection
.execute(
"INSERT INTO blobs (id, data) VALUES ('assistant', ?1)",
rusqlite::params![node],
)
.expect("insert Cursor assistant node");
connection
.execute(
"INSERT INTO meta (key, value) VALUES ('metadata', ?1)",
rusqlite::params![serde_json::json!({ "lastUsedModel": model }).to_string()],
)
.expect("insert Cursor metadata");
path
}
pub fn put(&self, rel: &str, content: &str) -> PathBuf {
let p = self.home().join(rel);
Self::write_file(&p, content);
p
}
pub fn seed_pricing_cache(&self, models: &serde_json::Value) -> PathBuf {
std::fs::create_dir_all(&self.paths.cache_dir).expect("create cache dir");
let path = self.paths.cache_dir.join(format!(
"model_pricing_{}.json",
chrono::Utc::now().date_naive().format("%Y-%m-%d")
));
std::fs::write(
&path,
serde_json::to_string_pretty(models).expect("serialize pricing cache"),
)
.expect("write pricing cache");
path
}
}